Skip to content

Commit

Permalink
Prepared fix for issue #624.
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsschmidt1337 committed Aug 18, 2024
1 parent 8230748 commit 70f8d0c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/org/nschmidt/csg/CSG.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,18 @@ public CSG union(CSG csg) {
});

CompletableFuture<Node> f1 = CompletableFuture.supplyAsync(() -> new Node(thisPolys));
CompletableFuture<Node> f2 = CompletableFuture.supplyAsync(() -> new Node(otherPolys));
CompletableFuture.allOf(f1, f2).join();
CompletableFuture<Node> f2 = CompletableFuture.supplyAsync(() -> new Node(this.createClone().polygons));
CompletableFuture<Node> f3 = CompletableFuture.supplyAsync(() -> new Node(otherPolys));
CompletableFuture.allOf(f1, f2, f3).join();

final Node a;
final Node a1;
final Node a2;
final Node b;

try {
a = f1.get();
b = f2.get();
a1 = f1.get();
a2 = f2.get();
b = f3.get();
} catch (ExecutionException e) {
// Exceptions should (tm) already be thrown by the "join()" call.
throw new LDPartEditorException(e);
Expand All @@ -232,15 +235,12 @@ public CSG union(CSG csg) {
throw new LDPartEditorException(e);
}

a.clipTo(b);
b.clipTo(a);
b.invert();
b.clipTo(a);
b.invert();
a1.clipTo(b);
b.clipTo(a2);

final List<Node> nodes = new ArrayList<>();
final Deque<NodePolygon> st = new LinkedList<>();
st.push(new NodePolygon(a, b.allPolygons(new ArrayList<>())));
st.push(new NodePolygon(a1, b.allPolygons(new ArrayList<>())));
while (!st.isEmpty()) {
NodePolygon np = st.pop();
Node n = np.node();
Expand All @@ -251,7 +251,7 @@ public CSG union(CSG csg) {
}
}

final List<Polygon> resultPolys = a.allPolygons(nonIntersectingPolys);
final List<Polygon> resultPolys = a1.allPolygons(nonIntersectingPolys);
return CSG.fromPolygons(resultPolys);
}

Expand Down
4 changes: 4 additions & 0 deletions src/org/nschmidt/csg/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public Node(List<Polygon> polygons) {
st.push(np2);
}
}

if (it == 10000) {
NLogger.debug(getClass(), "CSG limit exceeded."); //$NON-NLS-1$
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/org/nschmidt/csg/Plane.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,29 @@ int[] getTypes(final Polygon polygon) {
final int size = polygon.vertices.size();
final int[] types = new int[size + 1];
int polygonType = 0;
int coplanarityHits = 0;
boolean noCoplanar = false;
for (int i = 0; i < size; i++) {
double t = this.normal.dot(polygon.vertices.get(i)) - this.dist;
int type = t < -Plane.epsilon ? BACK : t > Plane.epsilon ? FRONT : COPLANAR;
if (type == COPLANAR) {
coplanarityHits += 1;
if (coplanarityHits > 2 && noCoplanar) break;
} else {
noCoplanar = true;
}
polygonType |= type;
types[i] = type;
}

// When three or more points are co-planar, then the whole polygon is co-planar!
if (coplanarityHits > 2 && noCoplanar) {
polygonType = COPLANAR;
for (int i = 0; i < size; i++) {
types[i] = COPLANAR;
}
}

types[size] = polygonType;
return types;
}
Expand Down

0 comments on commit 70f8d0c

Please sign in to comment.