SWEET. Added centre-first path sorting. This shows the first paths to be drawn as darker on the capture preview. For speed it does not show it like that on the live preview.

This commit is contained in:
Sandy Noble 2013-04-04 00:30:34 +01:00
parent b1a4daf69f
commit 2b3bfd9c5c
4 changed files with 258 additions and 155 deletions

View File

@ -403,19 +403,16 @@ class DisplayMachine extends Machine
if (captureShape != null) if (captureShape != null)
{ {
stroke(150); //displayWebcamShapeAtFullSize(webcamShape, false, color(150,150,150));
displayWebcamShapeAtFullSize(webcamShape); displayWebcamShapeAtFullSize(captureShape, true, color(0,0,0));
stroke(255);
displayWebcamShapeAtFullSize(captureShape);
} }
else else
{ {
stroke(255); displayWebcamShapeAtFullSize(webcamShape, false, color(255,255,255));
displayWebcamShapeAtFullSize(webcamShape);
} }
} }
public void displayWebcamShapeAtFullSize(RShape vec) public void displayWebcamShapeAtFullSize(RShape vec, boolean illustrateSequence, Integer colour)
{ {
RG.ignoreStyles(); RG.ignoreStyles();
// work out scaling to make it full size on the screen // work out scaling to make it full size on the screen
@ -423,28 +420,31 @@ class DisplayMachine extends Machine
float h = height - getPanel(PANEL_NAME_GENERAL).getOutline().getTop() -10; float h = height - getPanel(PANEL_NAME_GENERAL).getOutline().getTop() -10;
float w = h * aspectRatio; float w = h * aspectRatio;
float scaler = h / vec.getHeight(); float scaler = h / vec.getHeight();
PVector position = new PVector(getPanel(PANEL_NAME_WEBCAM).getOutline().getRight()+7, height -10); PVector position = new PVector(getPanel(PANEL_NAME_WEBCAM).getOutline().getRight()+7, getPanel(PANEL_NAME_GENERAL).getOutline().getTop() -10);
// int noOfChildren = vec.countChildren(); noFill();
// List<RShape> children = new ArrayList<RShape>(noOfChildren);
// for (int i=0; i < noOfChildren; i++)
// {
// if (vec.children[i].getArea() > pathLengthHighPassCutoff)
// children.add(vec.children[i]);
// }
//
// RShape[] newArray = children.toArray(new RShape[children.size()]);
// vec.children = newArray;
RPoint[][] pointPaths = vec.getPointsInPaths(); RPoint[][] pointPaths = vec.getPointsInPaths();
if (illustrateSequence)
pointPaths = sortPathsCentreFirst(vec, pathLengthHighPassCutoff);
if (pointPaths != null) if (pointPaths != null)
{ {
float incPerPath = 0.0;
if (illustrateSequence)
incPerPath = 255.0 / (float) pointPaths.length;
for(int i = 0; i<pointPaths.length; i++) for(int i = 0; i<pointPaths.length; i++)
{ {
if (pointPaths[i].length >= pathLengthHighPassCutoff) float col = (float)i * incPerPath;
{ // if (pointPaths[i].length >= pathLengthHighPassCutoff)
// {
if (pointPaths[i] != null) if (pointPaths[i] != null)
{ {
if (illustrateSequence)
stroke((int)col, (int)col, (int)col, 128);
else
stroke(colour);
beginShape(); beginShape();
for (int j = 0; j<pointPaths[i].length; j++) for (int j = 0; j<pointPaths[i].length; j++)
{ {
@ -454,10 +454,11 @@ class DisplayMachine extends Machine
vertex(p.x, p.y); vertex(p.x, p.y);
} }
endShape(); endShape();
} // }
} }
} }
} }
noFill();
} }
public void displayVectorImage() public void displayVectorImage()

View File

@ -145,7 +145,8 @@ void button_mode_liveConfirmDraw()
// work out scaling and position // work out scaling and position
float scaling = getDisplayMachine().inMM(getDisplayMachine().getPictureFrame().getWidth()) / captureShape.getWidth(); float scaling = getDisplayMachine().inMM(getDisplayMachine().getPictureFrame().getWidth()) / captureShape.getWidth();
PVector position = new PVector(getDisplayMachine().inMM(getDisplayMachine().getPictureFrame().getPosition().x), getDisplayMachine().inMM(getDisplayMachine().getPictureFrame().getPosition().y) + (captureShape.getHeight() * scaling)); PVector position = new PVector(getDisplayMachine().inMM(getDisplayMachine().getPictureFrame().getPosition().x),
getDisplayMachine().inMM(getDisplayMachine().getPictureFrame().getPosition().y));
sendVectorShapes(captureShape, scaling, position); sendVectorShapes(captureShape, scaling, position);
button_mode_penUp(); button_mode_penUp();

View File

@ -1,31 +1,31 @@
/** /**
Polargraph controller Polargraph controller
Copyright Sandy Noble 2012. Copyright Sandy Noble 2012.
This file is part of Polargraph Controller. This file is part of Polargraph Controller.
Polargraph Controller is free software: you can redistribute it and/or modify Polargraph Controller is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
Polargraph Controller is distributed in the hope that it will be useful, Polargraph Controller is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with Polargraph Controller. If not, see <http://www.gnu.org/licenses/>. along with Polargraph Controller. If not, see <http://www.gnu.org/licenses/>.
Requires the excellent ControlP5 GUI library available from http://www.sojamo.de/libraries/controlP5/. Requires the excellent ControlP5 GUI library available from http://www.sojamo.de/libraries/controlP5/.
Requires the excellent Geomerative library available from http://www.ricardmarxer.com/geomerative/. Requires the excellent Geomerative library available from http://www.ricardmarxer.com/geomerative/.
This is an application for controlling a polargraph machine, communicating using ASCII command language over a serial link. This is an application for controlling a polargraph machine, communicating using ASCII command language over a serial link.
sandy.noble@gmail.com sandy.noble@gmail.com
http://www.polargraph.co.uk/ http://www.polargraph.co.uk/
http://code.google.com/p/polargraph/ http://code.google.com/p/polargraph/
*/ */
static final String CMD_CHANGELENGTH = "C01,"; static final String CMD_CHANGELENGTH = "C01,";
static final String CMD_CHANGEPENWIDTH = "C02,"; static final String CMD_CHANGEPENWIDTH = "C02,";
static final String CMD_CHANGEMOTORSPEED = "C03,"; static final String CMD_CHANGEMOTORSPEED = "C03,";
@ -71,7 +71,7 @@ static final String CMD_DRAW_NORWEGIAN = "C43,";
static final String CMD_DRAW_NORWEGIAN_OUTLINE = "C44,"; static final String CMD_DRAW_NORWEGIAN_OUTLINE = "C44,";
static final String CMD_SETPENLIFTRANGE = "C45,"; static final String CMD_SETPENLIFTRANGE = "C45,";
private PVector mouseVector = new PVector(0,0); private PVector mouseVector = new PVector(0, 0);
Comparator xAscending = new Comparator() Comparator xAscending = new Comparator()
{ {
@ -79,7 +79,7 @@ Comparator xAscending = new Comparator()
{ {
PVector a = (PVector) p1; PVector a = (PVector) p1;
PVector b = (PVector) p2; PVector b = (PVector) p2;
int xValue = new Float(a.x).compareTo(b.x); int xValue = new Float(a.x).compareTo(b.x);
return xValue; return xValue;
} }
@ -91,7 +91,7 @@ Comparator yAscending = new Comparator()
{ {
PVector a = (PVector) p1; PVector a = (PVector) p1;
PVector b = (PVector) p2; PVector b = (PVector) p2;
int yValue = new Float(a.y).compareTo(b.y); int yValue = new Float(a.y).compareTo(b.y);
return yValue; return yValue;
} }
@ -128,9 +128,9 @@ public PVector getMouseVector()
{ {
if (mouseVector == null) if (mouseVector == null)
{ {
mouseVector = new PVector(0,0); mouseVector = new PVector(0, 0);
} }
mouseVector.x = mouseX; mouseVector.x = mouseX;
mouseVector.y = mouseY; mouseVector.y = mouseY;
return mouseVector; return mouseVector;
@ -158,7 +158,7 @@ void sendMoveToNativePosition(boolean direct, PVector p)
command = CMD_CHANGELENGTHDIRECT+int(p.x+0.5)+","+int(p.y+0.5)+","+getMaxSegmentLength()+",END"; command = CMD_CHANGELENGTHDIRECT+int(p.x+0.5)+","+int(p.y+0.5)+","+getMaxSegmentLength()+",END";
else else
command = CMD_CHANGELENGTH+(int)p.x+","+(int)p.y+",END"; command = CMD_CHANGELENGTH+(int)p.x+","+(int)p.y+",END";
addToCommandQueue(command); addToCommandQueue(command);
} }
@ -182,13 +182,13 @@ void sendTestPenWidth()
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(testPenWidthCommand) sb.append(testPenWidthCommand)
.append(int(gridSize)) .append(int(gridSize))
.append(",") .append(",")
.append(df.format(testPenWidthStartSize)) .append(df.format(testPenWidthStartSize))
.append(",") .append(",")
.append(df.format(testPenWidthEndSize)) .append(df.format(testPenWidthEndSize))
.append(",") .append(",")
.append(df.format(testPenWidthIncrementSize)) .append(df.format(testPenWidthIncrementSize))
.append(",END"); .append(",END");
addToCommandQueue(sb.toString()); addToCommandQueue(sb.toString());
} }
@ -197,7 +197,7 @@ void sendSetPosition()
PVector p = getDisplayMachine().scaleToDisplayMachine(getMouseVector()); PVector p = getDisplayMachine().scaleToDisplayMachine(getMouseVector());
p = getDisplayMachine().convertToNative(p); p = getDisplayMachine().convertToNative(p);
p = getDisplayMachine().inSteps(p); p = getDisplayMachine().inSteps(p);
String command = CMD_SETPOSITION+int(p.x+0.5)+","+int(p.y+0.5)+",END"; String command = CMD_SETPOSITION+int(p.x+0.5)+","+int(p.y+0.5)+",END";
addToCommandQueue(command); addToCommandQueue(command);
} }
@ -207,7 +207,7 @@ void sendStartTextAtPoint()
PVector p = getDisplayMachine().scaleToDisplayMachine(getMouseVector()); PVector p = getDisplayMachine().scaleToDisplayMachine(getMouseVector());
p = getDisplayMachine().convertToNative(p); p = getDisplayMachine().convertToNative(p);
p = getDisplayMachine().inSteps(p); p = getDisplayMachine().inSteps(p);
String command = CMD_START_TEXT+(int)p.x+","+(int)p.y+","+gridSize+",2,END"; String command = CMD_START_TEXT+(int)p.x+","+(int)p.y+","+gridSize+",2,END";
addToCommandQueue(command); addToCommandQueue(command);
} }
@ -215,7 +215,7 @@ void sendStartTextAtPoint()
void sendSetHomePosition() void sendSetHomePosition()
{ {
PVector pgCoords = getDisplayMachine().asNativeCoords(getHomePoint()); PVector pgCoords = getDisplayMachine().asNativeCoords(getHomePoint());
String command = CMD_SETPOSITION+int(pgCoords.x+0.5)+","+int(pgCoords.y+0.5)+",END"; String command = CMD_SETPOSITION+int(pgCoords.x+0.5)+","+int(pgCoords.y+0.5)+",END";
addToCommandQueue(command); addToCommandQueue(command);
} }
@ -224,13 +224,13 @@ int scaleDensity(int inDens, int inMax, int outMax)
{ {
float reducedDens = (float(inDens) / float(inMax)) * float(outMax); float reducedDens = (float(inDens) / float(inMax)) * float(outMax);
reducedDens = outMax-reducedDens; reducedDens = outMax-reducedDens;
// println("inDens:"+inDens+", inMax:"+inMax+", outMax:"+outMax+", reduced:"+reducedDens); // println("inDens:"+inDens+", inMax:"+inMax+", outMax:"+outMax+", reduced:"+reducedDens);
// round up if bigger than .5 // round up if bigger than .5
int result = int(reducedDens); int result = int(reducedDens);
if (reducedDens - (result) > 0.5) if (reducedDens - (result) > 0.5)
result ++; result ++;
//result = outMax - result; //result = outMax - result;
return result; return result;
} }
@ -238,13 +238,13 @@ int scaleDensity(int inDens, int inMax, int outMax)
SortedMap<Float, List<PVector>> divideIntoRows(Set<PVector> pixels, int direction) SortedMap<Float, List<PVector>> divideIntoRows(Set<PVector> pixels, int direction)
{ {
SortedMap<Float, List<PVector>> inRows = new TreeMap<Float, List<PVector>>(); SortedMap<Float, List<PVector>> inRows = new TreeMap<Float, List<PVector>>();
for (PVector p : pixels) for (PVector p : pixels)
{ {
Float row = p.x; Float row = p.x;
if (direction == DRAW_DIR_SE || direction == DRAW_DIR_NW) if (direction == DRAW_DIR_SE || direction == DRAW_DIR_NW)
row = p.y; row = p.y;
if (!inRows.containsKey(row)) if (!inRows.containsKey(row))
{ {
inRows.put(row, new ArrayList<PVector>()); inRows.put(row, new ArrayList<PVector>());
@ -259,7 +259,7 @@ PVector sortPixelsInRowsAlternating(SortedMap<Float, List<PVector>> inRows, int
PVector startPoint = null; PVector startPoint = null;
Comparator comp = null; Comparator comp = null;
boolean rowIsAlongXAxis = true; boolean rowIsAlongXAxis = true;
if (initialDirection == DRAW_DIR_SE || initialDirection == DRAW_DIR_NW) if (initialDirection == DRAW_DIR_SE || initialDirection == DRAW_DIR_NW)
{ {
rowIsAlongXAxis = true; rowIsAlongXAxis = true;
@ -270,7 +270,7 @@ PVector sortPixelsInRowsAlternating(SortedMap<Float, List<PVector>> inRows, int
rowIsAlongXAxis = false; rowIsAlongXAxis = false;
comp = yAscending; comp = yAscending;
} }
// now sort each row, reversing the direction after each row // now sort each row, reversing the direction after each row
boolean reverse = false; boolean reverse = false;
for (Float rowCoord : inRows.keySet()) for (Float rowCoord : inRows.keySet())
@ -283,38 +283,38 @@ PVector sortPixelsInRowsAlternating(SortedMap<Float, List<PVector>> inRows, int
// reverse it (descending) // reverse it (descending)
Collections.sort(row, comp); Collections.sort(row, comp);
Collections.reverse(row); Collections.reverse(row);
// if (startPoint == null) // if (startPoint == null)
// { // {
// if (rowIsAlongXAxis) // if (rowIsAlongXAxis)
// startPoint = new PVector(row.get(0).x+(maxPixelSize/2.0), row.get(0).y); // startPoint = new PVector(row.get(0).x+(maxPixelSize/2.0), row.get(0).y);
// else // else
// startPoint = new PVector(row.get(0).x, row.get(0).y-(maxPixelSize/2.0)); // startPoint = new PVector(row.get(0).x, row.get(0).y-(maxPixelSize/2.0));
// } // }
reverse = false; reverse = false;
} }
else else
{ {
// sort row ascending // sort row ascending
Collections.sort(row, comp); Collections.sort(row, comp);
// if (startPoint == null) // if (startPoint == null)
// { // {
// if (rowIsAlongXAxis) // if (rowIsAlongXAxis)
// startPoint = new PVector(row.get(0).x-(maxPixelSize/2.0), row.get(0).y); // startPoint = new PVector(row.get(0).x-(maxPixelSize/2.0), row.get(0).y);
// else // else
// startPoint = new PVector(row.get(0).x, row.get(0).y+(maxPixelSize/2.0)); // startPoint = new PVector(row.get(0).x, row.get(0).y+(maxPixelSize/2.0));
// } // }
reverse = true; reverse = true;
} }
} }
return startPoint; return startPoint;
} }
void sortPixelsInRows(SortedMap<Float, List<PVector>> inRows, int initialDirection) void sortPixelsInRows(SortedMap<Float, List<PVector>> inRows, int initialDirection)
{ {
PVector startPoint = null; PVector startPoint = null;
Comparator comp = null; Comparator comp = null;
boolean rowIsAlongXAxis = true; boolean rowIsAlongXAxis = true;
if (initialDirection == DRAW_DIR_SE || initialDirection == DRAW_DIR_NW) if (initialDirection == DRAW_DIR_SE || initialDirection == DRAW_DIR_NW)
{ {
rowIsAlongXAxis = true; rowIsAlongXAxis = true;
@ -325,7 +325,7 @@ void sortPixelsInRows(SortedMap<Float, List<PVector>> inRows, int initialDirecti
rowIsAlongXAxis = false; rowIsAlongXAxis = false;
comp = yAscending; comp = yAscending;
} }
// now sort each row, reversing the direction after each row // now sort each row, reversing the direction after each row
for (Float rowCoord : inRows.keySet()) for (Float rowCoord : inRows.keySet())
{ {
@ -333,7 +333,7 @@ void sortPixelsInRows(SortedMap<Float, List<PVector>> inRows, int initialDirecti
List<PVector> row = inRows.get(rowCoord); List<PVector> row = inRows.get(rowCoord);
// sort row ascending // sort row ascending
Collections.sort(row, comp); Collections.sort(row, comp);
if (initialDirection == DRAW_DIR_NW || initialDirection == DRAW_DIR_NE) if (initialDirection == DRAW_DIR_NW || initialDirection == DRAW_DIR_NE)
Collections.reverse(row); Collections.reverse(row);
} }
@ -343,21 +343,21 @@ void sortPixelsInRows(SortedMap<Float, List<PVector>> inRows, int initialDirecti
void sendPixels(Set<PVector> pixels, String pixelCommand, int initialDirection, int startCorner, float maxPixelSize, boolean scaleSizeToDensity) void sendPixels(Set<PVector> pixels, String pixelCommand, int initialDirection, int startCorner, float maxPixelSize, boolean scaleSizeToDensity)
{ {
// sort it into a map of rows, keyed by y coordinate value // sort it into a map of rows, keyed by y coordinate value
SortedMap<Float, List<PVector>> inRows = divideIntoRows(pixels, initialDirection); SortedMap<Float, List<PVector>> inRows = divideIntoRows(pixels, initialDirection);
sortPixelsInRowsAlternating(inRows, initialDirection, maxPixelSize); sortPixelsInRowsAlternating(inRows, initialDirection, maxPixelSize);
// that was easy. // that was easy.
// load the queue // load the queue
// add a preamble // add a preamble
// set the first direction // set the first direction
int drawDirection = initialDirection; int drawDirection = initialDirection;
String changeDir = CMD_CHANGEDRAWINGDIRECTION+getPixelDirectionMode()+"," + drawDirection +",END"; String changeDir = CMD_CHANGEDRAWINGDIRECTION+getPixelDirectionMode()+"," + drawDirection +",END";
addToCommandQueue(changeDir); addToCommandQueue(changeDir);
// reverse the row sequence if the draw is starting from the bottom // reverse the row sequence if the draw is starting from the bottom
// and reverse the pixel sequence if it needs to be done (odd number of rows) // and reverse the pixel sequence if it needs to be done (odd number of rows)
boolean reversePixelSequence = false; boolean reversePixelSequence = false;
@ -400,14 +400,14 @@ void sendPixels(Set<PVector> pixels, String pixelCommand, int initialDirection,
startPointY-=halfSize; startPointY-=halfSize;
println("NE"); println("NE");
} }
if (startPoint != null) if (startPoint != null)
{ {
String touchdown = CMD_CHANGELENGTH+int(startPointX)+","+int(startPointY)+",END"; String touchdown = CMD_CHANGELENGTH+int(startPointX)+","+int(startPointY)+",END";
addToCommandQueue(touchdown); addToCommandQueue(touchdown);
addToCommandQueue(CMD_PENDOWN+"END"); addToCommandQueue(CMD_PENDOWN+"END");
} }
boolean penLifted = false; boolean penLifted = false;
// so for each row // so for each row
@ -416,7 +416,7 @@ void sendPixels(Set<PVector> pixels, String pixelCommand, int initialDirection,
List<PVector> row = inRows.get(key); List<PVector> row = inRows.get(key);
if (reversePixelSequence) if (reversePixelSequence)
Collections.reverse(row); Collections.reverse(row);
for (PVector v : row) for (PVector v : row)
{ {
if (isHiddenPixel(v)) // check for masked pixels, if (isHiddenPixel(v)) // check for masked pixels,
@ -432,7 +432,7 @@ void sendPixels(Set<PVector> pixels, String pixelCommand, int initialDirection,
} }
else else
{ {
// println("Pen is already lifted."); // println("Pen is already lifted.");
} }
// now convert to ints // now convert to ints
int inX = int(v.x); int inX = int(v.x);
@ -462,11 +462,11 @@ void sendPixels(Set<PVector> pixels, String pixelCommand, int initialDirection,
} }
int scaledPixelSize = int((pixelSize*getPixelScalingOverGridSize())+0.5); int scaledPixelSize = int((pixelSize*getPixelScalingOverGridSize())+0.5);
String command = pixelCommand+inX+","+inY+","+scaledPixelSize+","+density+",END"; String command = pixelCommand+inX+","+inY+","+scaledPixelSize+","+density+",END";
// put the pen down if lifting over masked pixels is on // put the pen down if lifting over masked pixels is on
if (liftPenOnMaskedPixels && penLifted) if (liftPenOnMaskedPixels && penLifted)
{ {
// println("Pen down."); // println("Pen down.");
String lowerPen = CMD_PENDOWN + "END"; String lowerPen = CMD_PENDOWN + "END";
addToCommandQueue(lowerPen); addToCommandQueue(lowerPen);
penLifted = false; penLifted = false;
@ -479,7 +479,7 @@ void sendPixels(Set<PVector> pixels, String pixelCommand, int initialDirection,
String command = CMD_CHANGEDRAWINGDIRECTION+getPixelDirectionMode()+"," + drawDirection +",END"; String command = CMD_CHANGEDRAWINGDIRECTION+getPixelDirectionMode()+"," + drawDirection +",END";
addToCommandQueue(command); addToCommandQueue(command);
} }
addToCommandQueue(CMD_PENUP+"END"); addToCommandQueue(CMD_PENUP+"END");
numberOfPixelsTotal = commandQueue.size(); numberOfPixelsTotal = commandQueue.size();
startPixelTimer(); startPixelTimer();
@ -498,7 +498,7 @@ int flipDrawDirection(int curr)
return DRAW_DIR_NE; return DRAW_DIR_NE;
else return DRAW_DIR_SE; else return DRAW_DIR_SE;
} }
int getPixelDirectionMode() int getPixelDirectionMode()
{ {
@ -545,7 +545,7 @@ void sendOutlineOfPixels(Set<PVector> pixels)
{ {
// sort it into a map of rows, keyed by y coordinate value // sort it into a map of rows, keyed by y coordinate value
SortedMap<Float, List<PVector>> inRows = divideIntoRows(pixels, DRAW_DIR_SE); SortedMap<Float, List<PVector>> inRows = divideIntoRows(pixels, DRAW_DIR_SE);
sortPixelsInRowsAlternating(inRows, DRAW_DIR_SE, getGridSize()); sortPixelsInRowsAlternating(inRows, DRAW_DIR_SE, getGridSize());
float halfGrid = getGridSize() / 2.0; float halfGrid = getGridSize() / 2.0;
@ -558,14 +558,14 @@ void sendOutlineOfPixels(Set<PVector> pixels)
String command = CMD_DRAWRECT + int(startPoint.x)+","+int(startPoint.y)+","+int(endPoint.x)+","+int(endPoint.y)+",END"; String command = CMD_DRAWRECT + int(startPoint.x)+","+int(startPoint.y)+","+int(endPoint.x)+","+int(endPoint.y)+",END";
addToCommandQueue(command); addToCommandQueue(command);
} }
} }
} }
void sendOutlineOfRows(Set<PVector> pixels, int drawDirection) void sendOutlineOfRows(Set<PVector> pixels, int drawDirection)
{ {
// sort it into a map of rows, keyed by y coordinate value // sort it into a map of rows, keyed by y coordinate value
SortedMap<Float, List<PVector>> inRows = divideIntoRows(pixels, drawDirection); SortedMap<Float, List<PVector>> inRows = divideIntoRows(pixels, drawDirection);
sortPixelsInRows(inRows, drawDirection); sortPixelsInRows(inRows, drawDirection);
PVector offset = new PVector(getGridSize() / 2.0, getGridSize() / 2.0); PVector offset = new PVector(getGridSize() / 2.0, getGridSize() / 2.0);
@ -573,7 +573,7 @@ void sendOutlineOfRows(Set<PVector> pixels, int drawDirection)
{ {
PVector startPoint = inRows.get(key).get(0); PVector startPoint = inRows.get(key).get(0);
PVector endPoint = inRows.get(key).get(inRows.get(key).size()-1); PVector endPoint = inRows.get(key).get(inRows.get(key).size()-1);
if (drawDirection == DRAW_DIR_SE) if (drawDirection == DRAW_DIR_SE)
{ {
startPoint.sub(offset); startPoint.sub(offset);
@ -594,10 +594,10 @@ void sendOutlineOfRows(Set<PVector> pixels, int drawDirection)
startPoint.add(offset); startPoint.add(offset);
endPoint.sub(offset); endPoint.sub(offset);
} }
String command = CMD_DRAWRECT + int(startPoint.x)+","+int(startPoint.y)+","+int(endPoint.x)+","+int(endPoint.y)+",END"; String command = CMD_DRAWRECT + int(startPoint.x)+","+int(startPoint.y)+","+int(endPoint.x)+","+int(endPoint.y)+",END";
addToCommandQueue(command); addToCommandQueue(command);
} }
} }
void sendGridOfBox(Set<PVector> pixels) void sendGridOfBox(Set<PVector> pixels)
@ -615,12 +615,12 @@ void sendOutlineOfBox()
PVector tr = new PVector(br.x, tl.y); PVector tr = new PVector(br.x, tl.y);
PVector bl = new PVector(tl.x, br.y); PVector bl = new PVector(tl.x, br.y);
tl = getDisplayMachine().asNativeCoords(tl); tl = getDisplayMachine().asNativeCoords(tl);
tr = getDisplayMachine().asNativeCoords(tr); tr = getDisplayMachine().asNativeCoords(tr);
bl = getDisplayMachine().asNativeCoords(bl); bl = getDisplayMachine().asNativeCoords(bl);
br = getDisplayMachine().asNativeCoords(br); br = getDisplayMachine().asNativeCoords(br);
String command = CMD_CHANGELENGTHDIRECT+(int)tl.x+","+(int)tl.y+","+getMaxSegmentLength()+",END"; String command = CMD_CHANGELENGTHDIRECT+(int)tl.x+","+(int)tl.y+","+getMaxSegmentLength()+",END";
addToCommandQueue(command); addToCommandQueue(command);
@ -635,7 +635,6 @@ void sendOutlineOfBox()
command = CMD_CHANGELENGTHDIRECT+(int)tl.x+","+(int)tl.y+","+getMaxSegmentLength()+",END"; command = CMD_CHANGELENGTHDIRECT+(int)tl.x+","+(int)tl.y+","+getMaxSegmentLength()+",END";
addToCommandQueue(command); addToCommandQueue(command);
} }
void sendVectorShapes() void sendVectorShapes()
@ -647,25 +646,26 @@ void sendVectorShapes(RShape vec, float scaling, PVector position)
{ {
println("Send vector shapes."); println("Send vector shapes.");
RPoint[][] pointPaths = vec.getPointsInPaths(); RPoint[][] pointPaths = vec.getPointsInPaths();
// sort the paths to optimise the draw sequence // sort the paths to optimise the draw sequence
pointPaths = sortPathLongestFirst(pointPaths, pathLengthHighPassCutoff); // pointPaths = sortPathsLongestFirst(pointPaths, pathLengthHighPassCutoff);
// pointPaths = sortPathsGreatestAreaFirst(vec, pathLengthHighPassCutoff);
pointPaths = sortPathsCentreFirst(vec, pathLengthHighPassCutoff);
String command = ""; String command = "";
PVector lastPoint = new PVector(); PVector lastPoint = new PVector();
boolean liftToGetToNewPoint = true; boolean liftToGetToNewPoint = true;
// go through and get each path // go through and get each path
for (int i = 0; i<pointPaths.length; i++) for (int i = 0; i<pointPaths.length; i++)
{ {
if (pointPaths[i] != null) if (pointPaths[i] != null)
{ {
boolean firstPointFound = false; boolean firstPointFound = false;
if (pointPaths[i].length > pathLengthHighPassCutoff) if (pointPaths[i].length > pathLengthHighPassCutoff)
{ {
List<PVector> filteredPoints = filterPoints(pointPaths[i], VECTOR_FILTER_LOW_PASS, minimumVectorLineLength, scaling, position); List<PVector> filteredPoints = filterPoints(pointPaths[i], VECTOR_FILTER_LOW_PASS, minimumVectorLineLength, scaling, position);
//println(filteredPoints);
if (!filteredPoints.isEmpty()) if (!filteredPoints.isEmpty())
{ {
// draw the first one with a pen up and down to get to it // draw the first one with a pen up and down to get to it
@ -674,7 +674,7 @@ void sendVectorShapes(RShape vec, float scaling, PVector position)
liftToGetToNewPoint = false; liftToGetToNewPoint = false;
else else
liftToGetToNewPoint = true; liftToGetToNewPoint = true;
// pen UP! (IF THE NEW POINT IS DIFFERENT FROM THE LAST ONE!) // pen UP! (IF THE NEW POINT IS DIFFERENT FROM THE LAST ONE!)
if (liftToGetToNewPoint) if (liftToGetToNewPoint)
addToCommandQueue(CMD_PENUP+"END"); addToCommandQueue(CMD_PENUP+"END");
@ -683,18 +683,17 @@ void sendVectorShapes(RShape vec, float scaling, PVector position)
addToCommandQueue(command); addToCommandQueue(command);
if (liftToGetToNewPoint) if (liftToGetToNewPoint)
addToCommandQueue(CMD_PENDOWN+"END"); addToCommandQueue(CMD_PENDOWN+"END");
// then just iterate through the rest // then just iterate through the rest
for (int j=1; j<filteredPoints.size(); j++) for (int j=1; j<filteredPoints.size(); j++)
{ {
p = filteredPoints.get(j); p = filteredPoints.get(j);
command = CMD_CHANGELENGTHDIRECT+(int)p.x+","+(int)p.y+","+getMaxSegmentLength()+",END"; command = CMD_CHANGELENGTHDIRECT+(int)p.x+","+(int)p.y+","+getMaxSegmentLength()+",END";
addToCommandQueue(command); addToCommandQueue(command);
} }
lastPoint = new PVector(p.x,p.y); lastPoint = new PVector(p.x, p.y);
} }
} }
} }
@ -713,23 +712,26 @@ public RPoint[][] sortPathLongestFirst(RPoint[][] pointPaths, int highPassCutoff
pathsList.add(pointPaths[i]); pathsList.add(pointPaths[i]);
} }
} }
// sort the list // sort the list
Collections.sort(pathsList, new Comparator<RPoint[]>() { Collections.sort(pathsList, new Comparator<RPoint[]>() {
public int compare(RPoint[] o1, RPoint[] o2) { public int compare(RPoint[] o1, RPoint[] o2) {
if (o1.length > o2.length) { if (o1.length > o2.length) {
return -1; return -1;
} else if (o1.length < o2.length) { }
return 1; else if (o1.length < o2.length) {
} else { return 1;
return 0; }
} else {
return 0;
}
} }
}); }
);
// filter out some short paths // filter out some short paths
pathsList = removeShortPaths(pathsList, highPassCutoff); pathsList = removeShortPaths(pathsList, highPassCutoff);
// and put them into a new array // and put them into a new array
for (int i=0; i<pathsList.size(); i++) for (int i=0; i<pathsList.size(); i++)
{ {
@ -739,13 +741,110 @@ public RPoint[][] sortPathLongestFirst(RPoint[][] pointPaths, int highPassCutoff
return pointPaths; return pointPaths;
} }
public RPoint[][] sortPathsGreatestAreaFirst(RShape vec, int highPassCutoff)
{
// put the paths into a list
SortedMap<Float, RPoint[]> pathsList = new TreeMap<Float, RPoint[]>();
int noOfChildren = vec.countChildren();
for (int i=0; i < noOfChildren; i++)
{
float area = vec.children[i].getArea();
RPoint[] path = vec.children[i].getPointsInPaths()[0];
pathsList.put(area, path);
}
RPoint[][] pointPaths = vec.getPointsInPaths();
List<RPoint[]> filtered = new ArrayList<RPoint[]>();
// and put them into a new array
int i = 0;
for (Float k : pathsList.keySet())
{
if (k >= highPassCutoff)
{
filtered.add(pathsList.get(k));
println("Filtered kept path of area " + k);
}
else
println("Filtered discarded path of area " + k);
}
pointPaths = new RPoint[filtered.size()][];
for (i = 0; i < filtered.size(); i++)
{
pointPaths[i] = filtered.get(i);
}
return pointPaths;
}
public RPoint[][] sortPathsCentreFirst(RShape vec, int highPassCutoff)
{
// put the paths into a list
int noOfChildren = vec.countChildren();
List<RShape> pathsList = new ArrayList<RShape>(noOfChildren);
for (int i=0; i < noOfChildren; i++)
pathsList.add(vec.children[i]);
List<RShape> orderedPathsList = new ArrayList<RShape>(noOfChildren);
// make a tiny area in the centre of the shape,
// plan to increment the size of the area until it covers vec entirely
// (radius of area min = 0, max = distance from shape centre to any corner.)
float aspectRatio = vec.getHeight() / vec.getWidth();
int n = 0;
float w = 1.0;
float h = w * aspectRatio;
RPoint topLeft = vec.getTopLeft();
RPoint botRight = vec.getBottomRight();
PVector centre = new PVector(vec.getWidth()/2, vec.getHeight()/2);
float vecWidth = vec.getWidth();
while (w <= vecWidth)
{
w+=6.0;
h = w * aspectRatio;
//println(n++ + ". Rect w " + w + ", h " + h);
RShape field = RShape.createRectangle(centre.x-(w/2.0), centre.y-(h/2.0), w, h);
// add all the shapes that are entirely inside the circle to orderedPathsList
ListIterator<RShape> it = pathsList.listIterator();
int shapesAdded = 0;
while (it.hasNext())
{
RShape sh = it.next();
if (field.contains(sh.getCenter()))
{
orderedPathsList.add(sh);
// remove the shapes from pathsList (so it isn't found again)
shapesAdded++;
it.remove();
}
}
// increase the size of the circle and try again
}
RPoint[][] pointPaths = new RPoint[orderedPathsList.size()][];// vec.getPointsInPaths();
for (int i = 0; i < orderedPathsList.size(); i++)
{
pointPaths[i] = orderedPathsList.get(i).getPointsInPaths()[0];
}
return pointPaths;
}
List<RPoint[]> removeShortPaths(List<RPoint[]> list, int cutoff) List<RPoint[]> removeShortPaths(List<RPoint[]> list, int cutoff)
{ {
if (cutoff > 0) if (cutoff > 0)
{ {
int numberOfPaths = list.size(); int numberOfPaths = list.size();
ListIterator<RPoint[]> it = list.listIterator(); ListIterator<RPoint[]> it = list.listIterator();
while (it.hasNext()) while (it.hasNext ())
{ {
RPoint[] paths = it.next(); RPoint[] paths = it.next();
if (paths == null || cutoff >= paths.length) if (paths == null || cutoff >= paths.length)
@ -765,7 +864,7 @@ List<PVector> filterPoints(RPoint[] points, int filterToUse, long filterParam, f
List<PVector> filterPointsLowPass(RPoint[] points, long filterParam, float scaling, PVector position) List<PVector> filterPointsLowPass(RPoint[] points, long filterParam, float scaling, PVector position)
{ {
List<PVector> result = new ArrayList<PVector>(); List<PVector> result = new ArrayList<PVector>();
// scale and convert all the points first // scale and convert all the points first
List<PVector> scaled = new ArrayList<PVector>(points.length); List<PVector> scaled = new ArrayList<PVector>(points.length);
for (int j = 0; j<points.length; j++) for (int j = 0; j<points.length; j++)
@ -781,27 +880,27 @@ List<PVector> filterPointsLowPass(RPoint[] points, long filterParam, float scali
scaled.add(p); scaled.add(p);
} }
} }
if (scaled.size() > 1) if (scaled.size() > 1)
{ {
PVector p = scaled.get(0); PVector p = scaled.get(0);
result.add(p); result.add(p);
for (int j = 1; j<scaled.size(); j++) for (int j = 1; j<scaled.size(); j++)
{ {
p = scaled.get(j); p = scaled.get(j);
// and even then, only bother drawing if it's a move of over "x" steps // and even then, only bother drawing if it's a move of over "x" steps
int diffx = int(p.x) - int(result.get(result.size()-1).x); int diffx = int(p.x) - int(result.get(result.size()-1).x);
int diffy = int(p.y) - int(result.get(result.size()-1).y); int diffy = int(p.y) - int(result.get(result.size()-1).y);
if (abs(diffx) > filterParam || abs(diffy) > filterParam) if (abs(diffx) > filterParam || abs(diffy) > filterParam)
{ {
//println("Adding point " + p + ", last: " + result.get(result.size()-1)); //println("Adding point " + p + ", last: " + result.get(result.size()-1));
result.add(p); result.add(p);
} }
} }
} }
if (result.size() < 2) if (result.size() < 2)
result.clear(); result.clear();
@ -817,12 +916,12 @@ void sendMachineStoreMode()
String overwrite = ",R"; String overwrite = ",R";
if (!getOverwriteExistingStoreFile()) if (!getOverwriteExistingStoreFile())
overwrite = ",A"; overwrite = ",A";
addToCommandQueue(CMD_MACHINE_MODE_STORE_COMMANDS + getStoreFilename()+overwrite+",END"); addToRealtimeCommandQueue(CMD_MACHINE_MODE_STORE_COMMANDS + getStoreFilename()+overwrite+",END");
} }
void sendMachineLiveMode() void sendMachineLiveMode()
{ {
addToCommandQueue(CMD_MACHINE_MODE_LIVE+"END"); addToRealtimeCommandQueue(CMD_MACHINE_MODE_LIVE+"END");
} }
void sendMachineExecMode() void sendMachineExecMode()
{ {
@ -847,5 +946,4 @@ void sendDrawRandomSprite(String spriteFilename)
addToCommandQueue(CMD_DRAW_RANDOM_SPRITE+","+spriteFilename+",100,500,END"); addToCommandQueue(CMD_DRAW_RANDOM_SPRITE+","+spriteFilename+",100,500,END");
} }

View File

@ -105,6 +105,9 @@ public RShape webcam_traceImage(Map<Integer, PImage> seps)
} }
// rotate image // rotate image
allShapes.rotate(radians(-90)); allShapes.rotate(radians(-90));
// transform it so that top left is at 0,0.
RPoint topLeft = allShapes.getTopLeft();
allShapes.translate(-topLeft.x, -topLeft.y);
return allShapes; return allShapes;
} }