From a7b735b5b66f23dffbe1458cf10eb8145bea7ea4 Mon Sep 17 00:00:00 2001 From: Kevin harrington Date: Mon, 14 Oct 2024 12:04:45 -0400 Subject: [PATCH] De-Duplicating on the event of loading the SVG prevents downstream errors --- src/main/java/com/piro/bezier/BezierPath.java | 50 ++++++++++++------- .../java/eu/mihosoft/vrl/v3d/Extrude.java | 5 +- .../java/eu/mihosoft/vrl/v3d/SVGLoadTest.java | 10 +++- 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/piro/bezier/BezierPath.java b/src/main/java/com/piro/bezier/BezierPath.java index 82fa752f..c65be77d 100644 --- a/src/main/java/com/piro/bezier/BezierPath.java +++ b/src/main/java/com/piro/bezier/BezierPath.java @@ -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 { @@ -14,7 +15,7 @@ public class BezierPath { BezierListProducer path; - private ArrayList pointList = new ArrayList(); + private ArrayList plInternal = new ArrayList(); double resolution = 0.075; /** Creates a new instance of Animate */ @@ -60,45 +61,45 @@ 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': @@ -106,51 +107,51 @@ protected void parsePathList(String list) { 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': @@ -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()) l) { String s = l.removeFirst(); return Float.parseFloat(s); @@ -206,7 +220,7 @@ public Vector3d eval(float interp) { */ public ArrayList evaluate() { - return pointList; + return plInternal; } } diff --git a/src/main/java/eu/mihosoft/vrl/v3d/Extrude.java b/src/main/java/eu/mihosoft/vrl/v3d/Extrude.java index 595d456e..50909875 100644 --- a/src/main/java/eu/mihosoft/vrl/v3d/Extrude.java +++ b/src/main/java/eu/mihosoft/vrl/v3d/Extrude.java @@ -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; @@ -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) pPoints = Arrays.asList(bottomV2, topV2, topV1, bottomV1); diff --git a/src/test/java/eu/mihosoft/vrl/v3d/SVGLoadTest.java b/src/test/java/eu/mihosoft/vrl/v3d/SVGLoadTest.java index c5c4c724..89383220 100644 --- a/src/test/java/eu/mihosoft/vrl/v3d/SVGLoadTest.java +++ b/src/test/java/eu/mihosoft/vrl/v3d/SVGLoadTest.java @@ -58,9 +58,15 @@ private ArrayList 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> 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; }