Skip to content

Commit 2ad7811

Browse files
committed
move repair to an interface the is user swappable.
1 parent 7c4125f commit 2ad7811

File tree

3 files changed

+126
-99
lines changed

3 files changed

+126
-99
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ dependencies {
107107

108108
// triangulation algorithm
109109
implementation group: 'org.locationtech.jts', name: 'jts-core', version: '1.20.0'// https://mvnrepository.com/artifact/org.orbisgis/poly2tri
110-
implementation group: 'org.orbisgis', name: 'poly2tri', version: '0.7.0'
110+
//implementation group: 'org.orbisgis', name: 'poly2tri', version: '0.7.0'
111111

112112
// alternate triangulation
113113
//implementation "io.github.earcut4j:earcut4j:2.2.2"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package eu.mihosoft.vrl.v3d;
2+
3+
public interface IPolygonRepairTool {
4+
Polygon repairOverlappingEdges(Polygon concave);
5+
}

src/main/java/eu/mihosoft/vrl/v3d/ext/org/poly2tri/PolygonUtil.java

Lines changed: 120 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import eu.mihosoft.vrl.v3d.Debug3dProvider;
3838
import eu.mihosoft.vrl.v3d.Edge;
3939
import eu.mihosoft.vrl.v3d.Extrude;
40+
import eu.mihosoft.vrl.v3d.IPolygonRepairTool;
4041
import eu.mihosoft.vrl.v3d.Node;
4142
import eu.mihosoft.vrl.v3d.Plane;
4243
import eu.mihosoft.vrl.v3d.Polygon;
@@ -68,6 +69,92 @@
6869
*/
6970
public class PolygonUtil {
7071
private static final double triangleScale = 1000.0;
72+
private static IPolygonRepairTool repair = concave1 -> {
73+
74+
ArrayList<Edge> edges = new ArrayList<Edge>();
75+
ArrayList<Vertex> toRemove = new ArrayList<Vertex>();
76+
List<Vertex> v = concave1.getVertices();
77+
ArrayList<Vertex> modifiable = new ArrayList<Vertex>();
78+
modifiable.addAll(v);
79+
for (int i = 0; i < v.size(); i++) {
80+
Edge e = new Edge(v.get(i), v.get((i + 1) % v.size()));
81+
double l = e.length();
82+
if (l < 0.0001)
83+
System.out.println("Length of edge is " + l);
84+
// System.out.println(e);
85+
edges.add(e);
86+
}
87+
ArrayList<Thread> threads = new ArrayList<Thread>();
88+
int threadCount = 64;
89+
if (threadCount >= edges.size()) {
90+
threadCount = 1;
91+
}
92+
int perThread = edges.size() / threadCount;
93+
for (int k = 0; k < threadCount; k++) {
94+
int start = k * perThread;
95+
96+
Thread t = new Thread(() -> {
97+
for (int i = start; i < edges.size() && i <= (start + perThread); i++) {
98+
Edge test = edges.get(i);
99+
if (i < edges.size() - 1) {
100+
boolean c = test.colinear(edges.get(i + 1));
101+
if (c) {
102+
System.out.println("Colinear edged found " + i);
103+
}
104+
}
105+
if (start == 0) {
106+
CSG.getProgressMoniter().progressUpdate((i * (edges.size() / perThread)),
107+
edges.size(),
108+
" Repairing intersecting polygon (Hint in Inkscape->Path->Simplify): found # "
109+
+ toRemove.size(),
110+
null);
111+
// System.out.println(start+" Testing " + + " of " +edges.size()+
112+
// " found bad points# "+toRemove.size());
113+
}
114+
for (int j = 0; j < edges.size(); j++) {
115+
if (i == j)
116+
continue;
117+
Edge test2 = edges.get(j);
118+
boolean b = Edge.falseBoundaryEdgeSharedWithOtherEdge(test, test2);
119+
Optional<Vector3d> cross = test.getCrossingPoint(test2);
120+
boolean c = cross.isPresent() && i < j;
121+
if (c) {
122+
int x;
123+
for (x = i + 1; x < j; x++) {
124+
toRemove.add(v.get(x));
125+
}
126+
// System.out
127+
// .println("Edges cross! " + cross.get() +
128+
// " pruned "+(x-i));
129+
}
130+
if (b) {
131+
System.out.println("\n\nFalse Boundary " + test + " \n " + test2);
132+
try {
133+
Vertex vBad = test.getCommonPoint(test2);
134+
toRemove.add(vBad);
135+
} catch (Exception e) {
136+
throw new RuntimeException(e);
137+
}
138+
}
139+
}
140+
141+
}
142+
});
143+
threads.add(t);
144+
t.start();
145+
}
146+
for (Thread t : threads)
147+
try {
148+
t.join();
149+
} catch (InterruptedException e) {
150+
// TODO Auto-generated catch block
151+
e.printStackTrace();
152+
}
153+
for (Vertex vr : toRemove)
154+
modifiable.remove(vr);
155+
return new Polygon(modifiable, concave1.getStorage(), false, concave1.getPlane());
156+
157+
};
71158

72159
/**
73160
* Checks if two polygons overlap in 3D space.
@@ -453,113 +540,39 @@ public static List<Polygon> concaveToConvex(Polygon incoming, boolean toCCW) {
453540
} catch (java.lang.IllegalStateException ex) {
454541
// ex.printStackTrace();
455542
// throw new RuntimeException(ex);
456-
ArrayList<Edge> edges = new ArrayList<Edge>();
457-
ArrayList<Vertex> toRemove = new ArrayList<Vertex>();
458-
List<Vertex> v = concave.getVertices();
459-
ArrayList<Vertex> modifiable = new ArrayList<Vertex>();
460-
modifiable.addAll(v);
461-
for (int i = 0; i < v.size(); i++) {
462-
Edge e = new Edge(v.get(i), v.get((i + 1) % v.size()));
463-
double l = e.length();
464-
if (l < 0.0001)
465-
System.out.println("Length of edge is " + l);
466-
// System.out.println(e);
467-
edges.add(e);
468-
}
469-
ArrayList<Thread> threads = new ArrayList<Thread>();
470-
int threadCount = 64;
471-
if(threadCount>=edges.size()) {
472-
threadCount=1;
473-
}
474-
int perThread = edges.size()/threadCount;
475-
for (int k = 0; k < threadCount; k++) {
476-
int start= k*perThread;
477-
478-
Thread t = new Thread(() -> {
479-
for (int i = start; i < edges.size()&& i<=(start+perThread); i++) {
480-
Edge test = edges.get(i);
481-
if (i < edges.size() - 1) {
482-
boolean c = test.colinear(edges.get(i + 1));
483-
if (c) {
484-
System.out.println("Colinear edged found " + i);
485-
}
486-
}
487-
if(start==0) {
488-
CSG.getProgressMoniter().progressUpdate((i*(edges.size()/perThread)),
489-
edges.size(),
490-
" Repairing intersecting polygon (Hint in Inkscape->Path->Simplify): found # "+toRemove.size(), null);
491-
// System.out.println(start+" Testing " + + " of " +edges.size()+
492-
// " found bad points# "+toRemove.size());
493-
}
494-
for (int j = 0; j < edges.size(); j++) {
495-
if (i == j)
496-
continue;
497-
Edge test2 = edges.get(j);
498-
boolean b = Edge.falseBoundaryEdgeSharedWithOtherEdge(test, test2);
499-
Optional<Vector3d> cross = test.getCrossingPoint(test2);
500-
boolean c = cross.isPresent() && i<j;
501-
if (c) {
502-
int x;
503-
for(x=i+1;x<j;x++) {
504-
toRemove.add(v.get(x));
505-
}
506-
// System.out
507-
// .println("Edges cross! " + cross.get() +
508-
// " pruned "+(x-i));
509-
}
510-
if (b) {
511-
System.out.println("\n\nFalse Boundary " + test + " \n " + test2);
512-
try {
513-
Vertex vBad = test.getCommonPoint(test2);
514-
toRemove.add(vBad);
515-
} catch (Exception e) {
516-
throw new RuntimeException(e);
517-
}
518-
}
519-
}
520-
521-
}
522-
});
523-
threads.add(t);
524-
t.start();
525-
}
526-
for(Thread t:threads)
527-
try {
528-
t.join();
529-
} catch (InterruptedException e) {
530-
// TODO Auto-generated catch block
531-
e.printStackTrace();
532-
}
533-
for(Vertex vr:toRemove)
534-
modifiable.remove(vr);
535-
concave=new Polygon(modifiable, concave.getStorage(), false, concave.getPlane());
543+
int start = concave.getVertices().size();
544+
concave = repairOverlappingEdges(concave);
545+
int end = concave.getVertices().size();
536546
makeTriangles(concave, cw, result, zplane, normal, debug, orentationInv, reorent, incoming.getColor());
537-
System.out.println("Repaired the polygon! pruned "+toRemove.size());
538-
// makeTrianglespoly2triMethod(result, concave, cw, reorent, orentationInv,
539-
// normal);
547+
System.out.println("Repaired the polygon! pruned " + (start - end));
540548
}
541549

542550
return result;
543551
}
544552

545-
private static void makeTrianglespoly2triMethod(List<Polygon> result, Polygon concave, boolean cw, boolean reorent,
546-
Transform orentationInv, Vector3d normal) {
547-
ArrayList<PolygonPoint> points = new ArrayList<PolygonPoint>();
548-
for (Vector3d v : concave.getPoints()) {
549-
points.add(new PolygonPoint(v.x, v.y, v.z));
550-
551-
}
552-
org.poly2tri.geometry.polygon.Polygon poly2tri = new org.poly2tri.geometry.polygon.Polygon(points);
553-
try {
554-
Poly2Tri.triangulate(poly2tri);
555-
List<DelaunayTriangle> triangles = poly2tri.getTriangles();
556-
} catch (Exception e) {
557-
e.printStackTrace();
558-
throw new RuntimeException(e);
559-
}
553+
private static Polygon repairOverlappingEdges(Polygon concave) {
560554

555+
return getRepair().repairOverlappingEdges(concave);
561556
}
562557

558+
// private static void makeTrianglespoly2triMethod(List<Polygon> result, Polygon concave, boolean cw, boolean reorent,
559+
// Transform orentationInv, Vector3d normal) {
560+
// ArrayList<PolygonPoint> points = new ArrayList<PolygonPoint>();
561+
// for (Vector3d v : concave.getPoints()) {
562+
// points.add(new PolygonPoint(v.x, v.y, v.z));
563+
//
564+
// }
565+
// org.poly2tri.geometry.polygon.Polygon poly2tri = new org.poly2tri.geometry.polygon.Polygon(points);
566+
// try {
567+
// Poly2Tri.triangulate(poly2tri);
568+
// List<DelaunayTriangle> triangles = poly2tri.getTriangles();
569+
// } catch (Exception e) {
570+
// e.printStackTrace();
571+
// throw new RuntimeException(e);
572+
// }
573+
//
574+
// }
575+
563576
// private static void makeTrianglespoly2triMethod(Polygon incoming, List<Polygon> result, Polygon concave, boolean cw) {
564577
// // System.err.println("Failed to triangulate "+concave);
565578
// Polygon p = incoming;
@@ -719,4 +732,13 @@ private static Geometry makeTriangles(Polygon concave, boolean cw, List<Polygon>
719732
return triangles;
720733
}
721734

735+
public static IPolygonRepairTool getRepair() {
736+
737+
return repair;
738+
}
739+
740+
public static void setRepair(IPolygonRepairTool repair) {
741+
PolygonUtil.repair = repair;
742+
}
743+
722744
}

0 commit comments

Comments
 (0)