Skip to content

Commit

Permalink
De-Duplicating on the event of loading the SVG prevents downstream
Browse files Browse the repository at this point in the history
errors
  • Loading branch information
madhephaestus committed Oct 14, 2024
1 parent a40dec3 commit a7b735b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
50 changes: 32 additions & 18 deletions src/main/java/com/piro/bezier/BezierPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import eu.mihosoft.vrl.v3d.Plane;
import eu.mihosoft.vrl.v3d.Vector3d;

public class BezierPath {
Expand All @@ -14,7 +15,7 @@ public class BezierPath {

BezierListProducer path;

private ArrayList<Vector3d> pointList = new ArrayList<Vector3d>();
private ArrayList<Vector3d> plInternal = new ArrayList<Vector3d>();
double resolution = 0.075;

/** Creates a new instance of Animate */
Expand Down Expand Up @@ -60,97 +61,97 @@ protected void parsePathList(String list) {
x = nextFloat(tokens);
y = nextFloat(tokens);
path.movetoAbs(x, y);
pointList.add(new Vector3d(x, y, 0));
setThePoint(new Vector3d(x, y, 0));
curCmd = 'L';
break;
case 'm':
x = nextFloat(tokens);
y = nextFloat(tokens);
path.movetoRel(x, y);
pointList.add(new Vector3d(x, y, 0));
setThePoint(new Vector3d(x, y, 0));
curCmd = 'l';
break;
case 'L':
path.linetoAbs(nextFloat(tokens), nextFloat(tokens));
pointList.add(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(1));
setThePoint(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(1));
break;
case 'l':
path.linetoRel(nextFloat(tokens), nextFloat(tokens));
pointList.add(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(1));
setThePoint(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(1));
break;
case 'H':
path.linetoHorizontalAbs(nextFloat(tokens));

pointList.add(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(1));
setThePoint(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(1));

break;
case 'h':
path.linetoHorizontalRel(nextFloat(tokens));

pointList.add(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(1));
setThePoint(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(1));

break;
case 'V':
path.linetoVerticalAbs(nextFloat(tokens));

pointList.add(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(1));
setThePoint(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(1));

break;
case 'v':
path.linetoVerticalAbs(nextFloat(tokens));
pointList.add(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(1));
setThePoint(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(1));
break;
case 'A':
case 'a':
break;
case 'Q':
path.curvetoQuadraticAbs(nextFloat(tokens), nextFloat(tokens), nextFloat(tokens), nextFloat(tokens));
for (double i = resolution; i < 1; i += resolution) {
pointList.add(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(i));
addingPoint(i);
}
break;
case 'q':
path.curvetoQuadraticAbs(nextFloat(tokens), nextFloat(tokens), nextFloat(tokens), nextFloat(tokens));
for (double i = resolution; i < 1; i += resolution) {
pointList.add(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(i));
addingPoint(i);
}
break;
case 'T':
path.curvetoQuadraticSmoothAbs(nextFloat(tokens), nextFloat(tokens));
for (double i = resolution; i < 1; i += resolution) {
pointList.add(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(i));
addingPoint(i);
}
break;
case 't':
path.curvetoQuadraticSmoothRel(nextFloat(tokens), nextFloat(tokens));
for (double i = resolution; i < 1; i += resolution) {
pointList.add(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(i));
addingPoint(i);
}
break;
case 'C':
path.curvetoCubicAbs(nextFloat(tokens), nextFloat(tokens), nextFloat(tokens), nextFloat(tokens),
nextFloat(tokens), nextFloat(tokens));
for (double i = resolution; i < 1; i += resolution) {
pointList.add(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(i));
addingPoint(i);
}
break;
case 'c':
path.curvetoCubicRel(nextFloat(tokens), nextFloat(tokens), nextFloat(tokens), nextFloat(tokens),
nextFloat(tokens), nextFloat(tokens));
for (double i = resolution; i < 1; i += resolution) {
pointList.add(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(i));
addingPoint(i);
}
break;
case 'S':
path.curvetoCubicSmoothAbs(nextFloat(tokens), nextFloat(tokens), nextFloat(tokens), nextFloat(tokens));
for (double i = resolution; i < 1; i += resolution) {
pointList.add(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(i));
addingPoint(i);
}
break;
case 's':
path.curvetoCubicSmoothRel(nextFloat(tokens), nextFloat(tokens), nextFloat(tokens), nextFloat(tokens));
for (double i = resolution; i < 1; i += resolution) {
pointList.add(path.bezierSegs.get(path.bezierSegs.size() - 1).eval(i));
addingPoint(i);
}
break;
case 'Z':
Expand All @@ -167,6 +168,19 @@ protected void parsePathList(String list) {
}
}

private boolean addingPoint(double i) {
Vector3d eval = path.bezierSegs.get(path.bezierSegs.size() - 1).eval(i);
return setThePoint(eval);
}

private boolean setThePoint(Vector3d eval) {
for(Vector3d v:plInternal) {
if(Math.abs(v.minus(eval).magnitude())<Plane.EPSILON)
return false;
}
return plInternal.add(eval);
}

static protected float nextFloat(LinkedList<String> l) {
String s = l.removeFirst();
return Float.parseFloat(s);
Expand Down Expand Up @@ -206,7 +220,7 @@ public Vector3d eval(float interp) {
*/
public ArrayList<Vector3d> evaluate() {

return pointList;
return plInternal;
}

}
5 changes: 3 additions & 2 deletions src/main/java/eu/mihosoft/vrl/v3d/Extrude.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ private CSG monotoneExtrude(Vector3d dir, Polygon polygon1) {
Polygon polygon2 = polygon1.translated(dir);

int numvertices = polygon1.vertices.size();
System.out.println("Building Polygon "+polygon1.getPoints().size());
for (int i = 0; i < numvertices; i++) {

int nexti = (i + 1) % numvertices;
Expand All @@ -99,8 +100,8 @@ private CSG monotoneExtrude(Vector3d dir, Polygon polygon1) {
Vector3d bottomV2 = polygon1.vertices.get(nexti).pos;
Vector3d topV2 = polygon2.vertices.get(nexti).pos;
double distance = bottomV1.minus(bottomV2).magnitude();
if(Math.abs(distance)<Plane.EPSILON*10) {
//System.out.println("Skipping invalid polygon");
if(Math.abs(distance)<Plane.EPSILON) {
System.out.println("Skipping invalid polygon "+i+" to "+nexti);
continue;
}
List<Vector3d> pPoints = Arrays.asList(bottomV2, topV2, topV1, bottomV1);
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/eu/mihosoft/vrl/v3d/SVGLoadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,15 @@ private ArrayList<CSG> run(SVGLoad s) {
double depth = 5 + (layers.size() * 5);
for (int i = 0; i < layers.size(); i++) {
String layerName = layers.get(i);
CSG extrudeLayerToCSG = s.extrudeLayerToCSG(depth, layerName);
HashMap<String, ArrayList<CSG>> extrudeLayerToCSG = s.extrudeLayers(depth,0.1, layerName);
// extrudeLayerToCSG.setColor(Color.web(SVGExporter.colorNames.get(i)));
polys.add(extrudeLayerToCSG);
for(String key:extrudeLayerToCSG.keySet()) {
System.out.println("Adding layer: "+key);
polys.add(CSG.unionAll(extrudeLayerToCSG.get(key)));
// for(CSG c:extrudeLayerToCSG.get(key)) {
// polys.add(c);
// }
}
depth -= 5;
}

Expand Down

0 comments on commit a7b735b

Please sign in to comment.