mirror of
https://github.com/euphy/polargraphcontroller
synced 2025-01-09 19:55:16 +01:00
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:
parent
b1a4daf69f
commit
2b3bfd9c5c
@ -403,19 +403,16 @@ class DisplayMachine extends Machine
|
||||
|
||||
if (captureShape != null)
|
||||
{
|
||||
stroke(150);
|
||||
displayWebcamShapeAtFullSize(webcamShape);
|
||||
stroke(255);
|
||||
displayWebcamShapeAtFullSize(captureShape);
|
||||
//displayWebcamShapeAtFullSize(webcamShape, false, color(150,150,150));
|
||||
displayWebcamShapeAtFullSize(captureShape, true, color(0,0,0));
|
||||
}
|
||||
else
|
||||
{
|
||||
stroke(255);
|
||||
displayWebcamShapeAtFullSize(webcamShape);
|
||||
displayWebcamShapeAtFullSize(webcamShape, false, color(255,255,255));
|
||||
}
|
||||
}
|
||||
|
||||
public void displayWebcamShapeAtFullSize(RShape vec)
|
||||
public void displayWebcamShapeAtFullSize(RShape vec, boolean illustrateSequence, Integer colour)
|
||||
{
|
||||
RG.ignoreStyles();
|
||||
// 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 w = h * aspectRatio;
|
||||
float scaler = h / vec.getHeight();
|
||||
PVector position = new PVector(getPanel(PANEL_NAME_WEBCAM).getOutline().getRight()+7, height -10);
|
||||
|
||||
// int noOfChildren = vec.countChildren();
|
||||
// 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;
|
||||
PVector position = new PVector(getPanel(PANEL_NAME_WEBCAM).getOutline().getRight()+7, getPanel(PANEL_NAME_GENERAL).getOutline().getTop() -10);
|
||||
|
||||
noFill();
|
||||
RPoint[][] pointPaths = vec.getPointsInPaths();
|
||||
if (illustrateSequence)
|
||||
pointPaths = sortPathsCentreFirst(vec, pathLengthHighPassCutoff);
|
||||
|
||||
if (pointPaths != null)
|
||||
{
|
||||
float incPerPath = 0.0;
|
||||
if (illustrateSequence)
|
||||
incPerPath = 255.0 / (float) pointPaths.length;
|
||||
|
||||
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 (illustrateSequence)
|
||||
stroke((int)col, (int)col, (int)col, 128);
|
||||
else
|
||||
stroke(colour);
|
||||
|
||||
beginShape();
|
||||
for (int j = 0; j<pointPaths[i].length; j++)
|
||||
{
|
||||
@ -454,10 +454,11 @@ class DisplayMachine extends Machine
|
||||
vertex(p.x, p.y);
|
||||
}
|
||||
endShape();
|
||||
}
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
noFill();
|
||||
}
|
||||
|
||||
public void displayVectorImage()
|
||||
|
@ -145,7 +145,8 @@ void button_mode_liveConfirmDraw()
|
||||
|
||||
// work out scaling and position
|
||||
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);
|
||||
button_mode_penUp();
|
||||
|
242
drawing.pde
242
drawing.pde
@ -1,31 +1,31 @@
|
||||
/**
|
||||
Polargraph controller
|
||||
Copyright Sandy Noble 2012.
|
||||
Polargraph controller
|
||||
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
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
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
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Polargraph Controller is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
Polargraph Controller is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Polargraph Controller. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU General Public License
|
||||
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 Geomerative library available from http://www.ricardmarxer.com/geomerative/.
|
||||
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/.
|
||||
|
||||
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
|
||||
http://www.polargraph.co.uk/
|
||||
http://code.google.com/p/polargraph/
|
||||
*/
|
||||
sandy.noble@gmail.com
|
||||
http://www.polargraph.co.uk/
|
||||
http://code.google.com/p/polargraph/
|
||||
*/
|
||||
static final String CMD_CHANGELENGTH = "C01,";
|
||||
static final String CMD_CHANGEPENWIDTH = "C02,";
|
||||
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_SETPENLIFTRANGE = "C45,";
|
||||
|
||||
private PVector mouseVector = new PVector(0,0);
|
||||
private PVector mouseVector = new PVector(0, 0);
|
||||
|
||||
Comparator xAscending = new Comparator()
|
||||
{
|
||||
@ -128,7 +128,7 @@ public PVector getMouseVector()
|
||||
{
|
||||
if (mouseVector == null)
|
||||
{
|
||||
mouseVector = new PVector(0,0);
|
||||
mouseVector = new PVector(0, 0);
|
||||
}
|
||||
|
||||
mouseVector.x = mouseX;
|
||||
@ -182,13 +182,13 @@ void sendTestPenWidth()
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(testPenWidthCommand)
|
||||
.append(int(gridSize))
|
||||
.append(",")
|
||||
.append(df.format(testPenWidthStartSize))
|
||||
.append(",")
|
||||
.append(df.format(testPenWidthEndSize))
|
||||
.append(",")
|
||||
.append(df.format(testPenWidthIncrementSize))
|
||||
.append(",END");
|
||||
.append(",")
|
||||
.append(df.format(testPenWidthStartSize))
|
||||
.append(",")
|
||||
.append(df.format(testPenWidthEndSize))
|
||||
.append(",")
|
||||
.append(df.format(testPenWidthIncrementSize))
|
||||
.append(",END");
|
||||
addToCommandQueue(sb.toString());
|
||||
}
|
||||
|
||||
@ -224,7 +224,7 @@ int scaleDensity(int inDens, int inMax, int outMax)
|
||||
{
|
||||
float reducedDens = (float(inDens) / float(inMax)) * float(outMax);
|
||||
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
|
||||
int result = int(reducedDens);
|
||||
@ -283,26 +283,26 @@ PVector sortPixelsInRowsAlternating(SortedMap<Float, List<PVector>> inRows, int
|
||||
// reverse it (descending)
|
||||
Collections.sort(row, comp);
|
||||
Collections.reverse(row);
|
||||
// if (startPoint == null)
|
||||
// {
|
||||
// if (rowIsAlongXAxis)
|
||||
// startPoint = new PVector(row.get(0).x+(maxPixelSize/2.0), row.get(0).y);
|
||||
// else
|
||||
// startPoint = new PVector(row.get(0).x, row.get(0).y-(maxPixelSize/2.0));
|
||||
// }
|
||||
// if (startPoint == null)
|
||||
// {
|
||||
// if (rowIsAlongXAxis)
|
||||
// startPoint = new PVector(row.get(0).x+(maxPixelSize/2.0), row.get(0).y);
|
||||
// else
|
||||
// startPoint = new PVector(row.get(0).x, row.get(0).y-(maxPixelSize/2.0));
|
||||
// }
|
||||
reverse = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// sort row ascending
|
||||
Collections.sort(row, comp);
|
||||
// if (startPoint == null)
|
||||
// {
|
||||
// if (rowIsAlongXAxis)
|
||||
// startPoint = new PVector(row.get(0).x-(maxPixelSize/2.0), row.get(0).y);
|
||||
// else
|
||||
// startPoint = new PVector(row.get(0).x, row.get(0).y+(maxPixelSize/2.0));
|
||||
// }
|
||||
// if (startPoint == null)
|
||||
// {
|
||||
// if (rowIsAlongXAxis)
|
||||
// startPoint = new PVector(row.get(0).x-(maxPixelSize/2.0), row.get(0).y);
|
||||
// else
|
||||
// startPoint = new PVector(row.get(0).x, row.get(0).y+(maxPixelSize/2.0));
|
||||
// }
|
||||
reverse = true;
|
||||
}
|
||||
}
|
||||
@ -353,7 +353,7 @@ void sendPixels(Set<PVector> pixels, String pixelCommand, int initialDirection,
|
||||
// load the queue
|
||||
// add a preamble
|
||||
|
||||
// set the first direction
|
||||
// set the first direction
|
||||
int drawDirection = initialDirection;
|
||||
String changeDir = CMD_CHANGEDRAWINGDIRECTION+getPixelDirectionMode()+"," + drawDirection +",END";
|
||||
addToCommandQueue(changeDir);
|
||||
@ -432,7 +432,7 @@ void sendPixels(Set<PVector> pixels, String pixelCommand, int initialDirection,
|
||||
}
|
||||
else
|
||||
{
|
||||
// println("Pen is already lifted.");
|
||||
// println("Pen is already lifted.");
|
||||
}
|
||||
// now convert to ints
|
||||
int inX = int(v.x);
|
||||
@ -466,7 +466,7 @@ void sendPixels(Set<PVector> pixels, String pixelCommand, int initialDirection,
|
||||
// put the pen down if lifting over masked pixels is on
|
||||
if (liftPenOnMaskedPixels && penLifted)
|
||||
{
|
||||
// println("Pen down.");
|
||||
// println("Pen down.");
|
||||
String lowerPen = CMD_PENDOWN + "END";
|
||||
addToCommandQueue(lowerPen);
|
||||
penLifted = false;
|
||||
@ -635,7 +635,6 @@ void sendOutlineOfBox()
|
||||
|
||||
command = CMD_CHANGELENGTHDIRECT+(int)tl.x+","+(int)tl.y+","+getMaxSegmentLength()+",END";
|
||||
addToCommandQueue(command);
|
||||
|
||||
}
|
||||
|
||||
void sendVectorShapes()
|
||||
@ -649,7 +648,9 @@ void sendVectorShapes(RShape vec, float scaling, PVector position)
|
||||
RPoint[][] pointPaths = vec.getPointsInPaths();
|
||||
|
||||
// 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 = "";
|
||||
PVector lastPoint = new PVector();
|
||||
@ -665,7 +666,6 @@ void sendVectorShapes(RShape vec, float scaling, PVector position)
|
||||
if (pointPaths[i].length > pathLengthHighPassCutoff)
|
||||
{
|
||||
List<PVector> filteredPoints = filterPoints(pointPaths[i], VECTOR_FILTER_LOW_PASS, minimumVectorLineLength, scaling, position);
|
||||
//println(filteredPoints);
|
||||
if (!filteredPoints.isEmpty())
|
||||
{
|
||||
// draw the first one with a pen up and down to get to it
|
||||
@ -693,8 +693,7 @@ void sendVectorShapes(RShape vec, float scaling, PVector position)
|
||||
command = CMD_CHANGELENGTHDIRECT+(int)p.x+","+(int)p.y+","+getMaxSegmentLength()+",END";
|
||||
addToCommandQueue(command);
|
||||
}
|
||||
lastPoint = new PVector(p.x,p.y);
|
||||
|
||||
lastPoint = new PVector(p.x, p.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -717,15 +716,18 @@ public RPoint[][] sortPathLongestFirst(RPoint[][] pointPaths, int highPassCutoff
|
||||
// sort the list
|
||||
Collections.sort(pathsList, new Comparator<RPoint[]>() {
|
||||
public int compare(RPoint[] o1, RPoint[] o2) {
|
||||
if (o1.length > o2.length) {
|
||||
return -1;
|
||||
} else if (o1.length < o2.length) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
if (o1.length > o2.length) {
|
||||
return -1;
|
||||
}
|
||||
else if (o1.length < o2.length) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
// filter out some short paths
|
||||
pathsList = removeShortPaths(pathsList, highPassCutoff);
|
||||
@ -739,13 +741,110 @@ public RPoint[][] sortPathLongestFirst(RPoint[][] pointPaths, int highPassCutoff
|
||||
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)
|
||||
{
|
||||
if (cutoff > 0)
|
||||
{
|
||||
int numberOfPaths = list.size();
|
||||
ListIterator<RPoint[]> it = list.listIterator();
|
||||
while (it.hasNext())
|
||||
while (it.hasNext ())
|
||||
{
|
||||
RPoint[] paths = it.next();
|
||||
if (paths == null || cutoff >= paths.length)
|
||||
@ -790,15 +889,15 @@ List<PVector> filterPointsLowPass(RPoint[] points, long filterParam, float scali
|
||||
for (int j = 1; j<scaled.size(); j++)
|
||||
{
|
||||
p = scaled.get(j);
|
||||
// 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 diffy = int(p.y) - int(result.get(result.size()-1).y);
|
||||
// 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 diffy = int(p.y) - int(result.get(result.size()-1).y);
|
||||
|
||||
if (abs(diffx) > filterParam || abs(diffy) > filterParam)
|
||||
{
|
||||
//println("Adding point " + p + ", last: " + result.get(result.size()-1));
|
||||
result.add(p);
|
||||
}
|
||||
if (abs(diffx) > filterParam || abs(diffy) > filterParam)
|
||||
{
|
||||
//println("Adding point " + p + ", last: " + result.get(result.size()-1));
|
||||
result.add(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -818,11 +917,11 @@ void sendMachineStoreMode()
|
||||
if (!getOverwriteExistingStoreFile())
|
||||
overwrite = ",A";
|
||||
|
||||
addToCommandQueue(CMD_MACHINE_MODE_STORE_COMMANDS + getStoreFilename()+overwrite+",END");
|
||||
addToRealtimeCommandQueue(CMD_MACHINE_MODE_STORE_COMMANDS + getStoreFilename()+overwrite+",END");
|
||||
}
|
||||
void sendMachineLiveMode()
|
||||
{
|
||||
addToCommandQueue(CMD_MACHINE_MODE_LIVE+"END");
|
||||
addToRealtimeCommandQueue(CMD_MACHINE_MODE_LIVE+"END");
|
||||
}
|
||||
void sendMachineExecMode()
|
||||
{
|
||||
@ -848,4 +947,3 @@ void sendDrawRandomSprite(String spriteFilename)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -105,6 +105,9 @@ public RShape webcam_traceImage(Map<Integer, PImage> seps)
|
||||
}
|
||||
// rotate image
|
||||
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;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user