Enable TSP mode in canvas preview

This commit is contained in:
mattmcw 2021-08-11 19:22:22 -04:00
parent ca670eb3a8
commit ef84f0ba57
1 changed files with 58 additions and 28 deletions

View File

@ -107,6 +107,7 @@ public class Config {
public int maxParticles = 2000; // Max value is normally 10000. public int maxParticles = 2000; // Max value is normally 10000.
public float cutoff = 0; // White cutoff value public float cutoff = 0; // White cutoff value
public int optimize = 1000;
public boolean gammaCorrection = false; public boolean gammaCorrection = false;
public float gamma = 1.0; public float gamma = 1.0;
@ -114,6 +115,8 @@ public class Config {
public boolean fill = false; public boolean fill = false;
public float line = 1.0; public float line = 1.0;
public String mode = "stipple"; //tsp
public Config (String inputFile) { public Config (String inputFile) {
int index; int index;
String[] parts; String[] parts;
@ -286,6 +289,12 @@ public class Config {
case "line" : case "line" :
line = floatOrDie(name, val); line = floatOrDie(name, val);
break; break;
case "mode" :
mode = strOrDie(name, val);
break;
case "optimize" :
optimize = intOrDie(name, val);
break;
} }
println("[" + source + "] " + name + "=" + val); println("[" + source + "] " + name + "=" + val);
} }
@ -331,7 +340,7 @@ boolean showPath;
boolean showCells; boolean showCells;
boolean TempShowCells; boolean TempShowCells;
boolean FileModeTSP; boolean FileModeTSP = false;
int vorPointsAdded; int vorPointsAdded;
boolean VoronoiCalculated; boolean VoronoiCalculated;
@ -475,7 +484,6 @@ void MainArraysetup() {
vorPointsAdded = 0; vorPointsAdded = 0;
voronoi = new Voronoi(); // Erase mesh voronoi = new Voronoi(); // Erase mesh
TempShowCells = true; TempShowCells = true;
FileModeTSP = false;
} }
void settings () { void settings () {
@ -512,6 +520,10 @@ void setup () {
showCells = false; showCells = false;
fileLoaded = false; fileLoaded = false;
SaveNow = 0; SaveNow = 0;
if (config.mode.equals("tsp") || config.mode.equals("TSP")) {
FileModeTSP = true;
println("Using TSP mode");
}
background(0); background(0);
} }
@ -745,7 +757,7 @@ void OptimizePlotPath () {
// Nearest neighbor ("Simple, Greedy") algorithm path optimization: // Nearest neighbor ("Simple, Greedy") algorithm path optimization:
int StopPoint = RouteStep + 1000; // 1000 steps per frame displayed; you can edit this number! int StopPoint = RouteStep + config.optimize; // 1000 steps per frame displayed; you can edit this number!
if (StopPoint > (particleRouteLength - 1)) { if (StopPoint > (particleRouteLength - 1)) {
StopPoint = particleRouteLength - 1; StopPoint = particleRouteLength - 1;
@ -1271,6 +1283,18 @@ void draw () {
canvas.strokeWeight(1.0); canvas.strokeWeight(1.0);
} }
if (FileModeTSP) {
OptimizePlotPath();
canvas.background(255);
canvas.beginShape();
for ( i = 0; i < particleRouteLength; ++i) {
Vec2D p1 = particles[particleRoute[i]];
float xTemp = p1.x;
float yTemp = p1.y;
canvas.vertex(xTemp, yTemp);
}
canvas.endShape();
} else {
for ( i = cellsCalculatedLast; i < cellsCalculated; i++) { for ( i = cellsCalculatedLast; i < cellsCalculated; i++) {
int px = (int) particles[i].x; int px = (int) particles[i].x;
int py = (int) particles[i].y; int py = (int) particles[i].y;
@ -1286,7 +1310,6 @@ void draw () {
} }
if (v < cutoffScaled) { if (v < cutoffScaled) {
//println(v);
dotDiam = (config.maxDotSize - v * dotScale) * 2.0 * config.canvasScalar; dotDiam = (config.maxDotSize - v * dotScale) * 2.0 * config.canvasScalar;
canvas.ellipse(px, py, dotDiam, dotDiam); canvas.ellipse(px, py, dotDiam, dotDiam);
if (config.fill) { if (config.fill) {
@ -1301,6 +1324,7 @@ void draw () {
} }
cellsCalculatedLast = cellsCalculated; cellsCalculatedLast = cellsCalculated;
} }
}
canvas.endDraw(); canvas.endDraw();
@ -1345,7 +1369,9 @@ void draw () {
} }
if (SaveNow > 0 && config.outputSVG != null) { if (SaveNow > 0 && config.outputSVG != null) {
if (!FileModeTSP) {
OptimizePlotPath(); OptimizePlotPath();
}
StatusDisplay = "Saving SVG File"; StatusDisplay = "Saving SVG File";
FileOutput = loadStrings("header.txt"); FileOutput = loadStrings("header.txt");
String rowTemp; String rowTemp;
@ -1367,7 +1393,7 @@ void draw () {
println("Saving TSP File (SVG)"); println("Saving TSP File (SVG)");
println(config.outputSVG); println(config.outputSVG);
// Path header:: // Path header::
rowTemp = "<path style=\"fill:none;stroke:black;stroke-width:2px;stroke-linejoin:round;stroke-linecap:round;\" d=\"M "; rowTemp = "<path style=\"fill:none;stroke:black;stroke-width:1px;stroke-linejoin:round;stroke-linecap:round;\" d=\"M ";
FileOutput = append(FileOutput, rowTemp); FileOutput = append(FileOutput, rowTemp);
for ( i = 0; i < particleRouteLength; ++i) { for ( i = 0; i < particleRouteLength; ++i) {
@ -1377,7 +1403,11 @@ void draw () {
float xTemp = SVGscale * p1.x + xOffset; float xTemp = SVGscale * p1.x + xOffset;
float yTemp = SVGscale * p1.y + yOffset; float yTemp = SVGscale * p1.y + yOffset;
if (i == 0) {
rowTemp = xTemp + " " + yTemp + "\r"; rowTemp = xTemp + " " + yTemp + "\r";
} else {
rowTemp = "L " + xTemp + " " + yTemp + "\r";
}
FileOutput = append(FileOutput, rowTemp); FileOutput = append(FileOutput, rowTemp);
} }
@ -1421,7 +1451,7 @@ void draw () {
// SVG footer: // SVG footer:
FileOutput = append(FileOutput, "</g></g></svg>"); FileOutput = append(FileOutput, "</g></g></svg>");
saveStrings(config.outputSVG, FileOutput); saveStrings(config.outputSVG, FileOutput);
FileModeTSP = false; // reset for next time //FileModeTSP = false; // reset for next time
if (FileModeTSP) { if (FileModeTSP) {
ErrorDisplay = "TSP Path .SVG file Saved"; ErrorDisplay = "TSP Path .SVG file Saved";