Skip to content

Commit

Permalink
Move all drawing logic into Plotter class.
Browse files Browse the repository at this point in the history
  • Loading branch information
modelflat committed May 7, 2016
1 parent 2b2b90b commit a4515ec
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 50 deletions.
51 changes: 49 additions & 2 deletions src/main/java/raster/Plotter.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package raster;

class Plotter implements Runnable {
public enum Mode {SOLID, CHUNKS, CLEAR}

private RasterPlot parent;
private ColoringRule rule;

private float mix, miy, max, may;
private int w, h;
private float scaleX, scaleY;

Plotter(RasterPlot parent) {
private int begin, end;
private Mode mode;

Plotter(RasterPlot parent, Mode mode, int begin, int end) {
this(parent, mode);
this.begin = begin;
this.end = end;
}

Plotter(RasterPlot parent, Mode mode) {
this.parent = parent;
this.mode = mode;

this.rule = parent.getColoringRule();

this.mix = parent.getBounds().getMinX();
Expand All @@ -24,7 +37,18 @@ class Plotter implements Runnable {
this.scaleY = (float) h / (may - miy);
}

public void run() {
public void renderSolid() {
float scaleX = (max - mix) / (float) w;
float scaleY = (may - miy) / (float) h;
for (int yCoord = begin; yCoord < end; yCoord++) {
for (int xCoord = 0; xCoord < w; xCoord++) {
parent.plotPixels[xCoord + yCoord * w] =
rule.colorFunction(mix + (float) xCoord * scaleX, -miy - (float) yCoord * scaleY);
}
}
}

public void renderChunks() {
do {
int nextChunk = parent.pool.get();
if (nextChunk < 0) {
Expand All @@ -39,4 +63,27 @@ public void run() {
}
} while (parent.pool.freeCount() > 0);
}

public void clear() {
int color = rule.getBackColor();
for (int i = begin; i < end; i++)
parent.plotPixels[i] = color;
}

public void run() {
switch (mode) {
case CLEAR: {
clear();
break;
}
case CHUNKS: {
renderChunks();
break;
}
case SOLID: {
renderSolid();
break;
}
}
}
}
55 changes: 8 additions & 47 deletions src/main/java/raster/RasterPlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public class RasterPlot {
private ColoringRule coloringRule;
private Bounds bounds;

private float mix, miy, max, may; // trying to save few cycles in directDraw()

/**
* Constructor for <code>RasterPlot</code> class.
*
Expand Down Expand Up @@ -97,7 +95,7 @@ public synchronized RasterPlot putChunk(float[] xy) {
* Renders all chunks that currently are in render chain, drawing
* each point according to current coloring rule and bounds.
*/
public RasterPlot render() {
public RasterPlot renderChunks() {
long time = System.nanoTime();
pool = new ChunkPool(this.chunks.size());
int threadCount = this.chunks.size() < maxThreadCount ? this.chunks.size() : maxThreadCount;
Expand All @@ -106,9 +104,8 @@ public RasterPlot render() {
this.chunks.size(), threadCount));

Thread[] threads = new Thread[threadCount];

for (int i = 0; i < threadCount; i++) {
threads[i] = new Thread(new Plotter(this), "PlotterThread#" + i);
threads[i] = new Thread(new Plotter(this, Plotter.Mode.CHUNKS), "PlotterThread#" + i);
threads[i].start();
}
for (int i = 0; i < threadCount; i++) {
Expand All @@ -133,20 +130,7 @@ public RasterPlot renderSolid() {
int portion = resolution.height / this.maxThreadCount;
Thread[] threads = new Thread[4];
for (int i = 0; i < this.maxThreadCount; i++) {
final int begin = i * portion;
final int end = (i + 1) * portion;
threads[i] = new Thread(new Runnable() {
public void run() {
float scaleX = (max - mix) / (float) getResolution().getWidth();
float scaleY = (may - miy) / (float) getResolution().getHeight();
for (int yCoord = begin; yCoord < end; yCoord++) {
for (int xCoord = 0; xCoord < resolution.width; xCoord++) {
plotPixels[xCoord + yCoord * resolution.width] =
coloringRule.colorFunction(mix + (float) xCoord * scaleX, -miy - (float) yCoord * scaleY);
}
}
}
});
threads[i] = new Thread(new Plotter(this, Plotter.Mode.SOLID, i * portion, (i + 1) * portion));
threads[i].start();
}
for (int i = 0; i < this.maxThreadCount; i++) {
Expand All @@ -162,20 +146,6 @@ public void run() {
return this;
}

/**
* Draw a point at x,y on plot, using color function defined in <code>ColoringRule</code>.
*
* @param x x-coordinate
* @param y y-coordinate
*/
public void directDraw(float x, float y) {
if (x > mix && x < max && y > miy && y < may) {
plotPixels[(int) ((x - mix) / (max - mix) * resolution.width) +
(resolution.height - 1 - (int) ((y - miy) / (may - miy) * resolution.height)) * resolution.width] =
coloringRule.colorFunction(x, y);
}
}

/**
* Clears render chain of RasterPlot.
*/
Expand All @@ -194,23 +164,18 @@ public RasterPlot clearPlot() {
Thread[] threads = new Thread[maxThreadCount];
int seg = plot.getHeight() * plot.getWidth() / maxThreadCount;
for (int i = 0; i < maxThreadCount; i++) {
final int begin = seg * i;
final int end = i == (maxThreadCount - 1) ? plot.getHeight() * plot.getWidth() : seg * (i + 1);
final int color = coloringRule.getBackColor();
threads[i] = new Thread(new Runnable() {
public void run() {
for (int i = begin; i < end; i++)
plotPixels[i] = color;
}
});
threads[i] = new Thread(new Plotter(this, Plotter.Mode.CLEAR,
seg * i, i == (maxThreadCount - 1) ? plot.getHeight() * plot.getWidth() : seg * (i + 1)));
threads[i].start();
}
for (int i = 0; i < maxThreadCount; i++)
for (int i = 0; i < maxThreadCount; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
logger.error(e.getMessage());
}
}
plot.flush();
time = System.nanoTime() - time;
logger.info(String.format("Clearing took %d ms", time / 1000000));
return this;
Expand Down Expand Up @@ -240,10 +205,6 @@ public ColoringRule getColoringRule() {
*/
public RasterPlot setBounds(Bounds bounds) {
this.bounds = bounds;
this.max = bounds.getMaxX();
this.mix = bounds.getMinX();
this.miy = bounds.getMinY();
this.may = bounds.getMaxY();
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/BasicTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public int colorFunction(float x, float y) {
return x * y > 0 ? Color.GREEN.getRGB() : Color.RED.getRGB();
}
})
.render()
.renderChunks()
.saveToFile("test.png", "png");
}

Expand Down

0 comments on commit a4515ec

Please sign in to comment.