Skip to content

Commit d878e03

Browse files
committed
Make sure there are polygons before trying to perform CSG operations
1 parent 5026c36 commit d878e03

File tree

4 files changed

+60
-29
lines changed

4 files changed

+60
-29
lines changed

src/main/java/eu/mihosoft/vrl/v3d/CSG.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,10 @@ private CSG _unionIntersectOpt(CSG csg) {
11121112
* @return the csg
11131113
*/
11141114
private CSG _unionNoOpt(CSG csg) {
1115+
if(this.getPolygons().size()==0)
1116+
return csg.clone();
1117+
if(csg.getPolygons().size()==0)
1118+
return this.clone();
11151119
Node a = new Node(this.clone().getPolygons());
11161120
Node b = new Node(csg.clone().getPolygons());
11171121
a.clipTo(b);
@@ -1303,7 +1307,12 @@ public CSG difference(CSG csg) {
13031307
private CSG _differenceCSGBoundsOpt(CSG csg) {
13041308
CSG a1 = this._differenceNoOpt(csg.getBounds().toCSG());
13051309
CSG a2 = this.intersect(csg.getBounds().toCSG());
1306-
CSG result = a2._differenceNoOpt(csg)._unionIntersectOpt(a1).optimization(getOptType());
1310+
1311+
CSG result = null;
1312+
if(a2.getPolygons().size()>0)
1313+
result = a2._differenceNoOpt(csg)._unionIntersectOpt(a1).optimization(getOptType());
1314+
else
1315+
result=a1;
13071316
if (getName().length() != 0 && csg.getName().length() != 0) {
13081317
result.setName(name);
13091318
}

src/main/java/eu/mihosoft/vrl/v3d/CSGServerHandler.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,21 @@ public void run() {
2626
try (ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
2727
ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream())) {
2828

29-
System.out.println("Client connected: " + clientSocket.getRemoteSocketAddress());
29+
// System.out.println("Client connected: " +
30+
// clientSocket.getRemoteSocketAddress());
3031

3132
// Read the CSG request
3233
CSGRequest request = (CSGRequest) ois.readObject();
33-
System.out.println("Received request: " + request.getOperation());
34+
// System.out.println("Received request: " + request.getOperation());
3435
boolean APIPass = true;
3536
String apiKey2 = request.getAPIKey();
3637
if (getAPIKEYs() != null) {
3738
APIPass = false;
38-
if (apiKey2!=null)
39+
if (apiKey2 != null)
3940
for (int i = 0; i < getAPIKEYs().length; i++)
4041
if (apiKey2.contentEquals(getAPIKEYs()[i])) {
4142
APIPass = true;
42-
System.out.println("API Key Match");
43+
// System.out.println("API Key Match");
4344
break;
4445
}
4546
}
@@ -64,7 +65,7 @@ public void run() {
6465
oos.writeObject(response);
6566
oos.flush();
6667

67-
System.out.println("Sent response: " + response);
68+
// System.out.println("Sent response: " + response);
6869

6970
} catch (IOException | ClassNotFoundException e) {
7071
System.err.println("client disconnected: ");
@@ -74,7 +75,7 @@ public void run() {
7475
}
7576

7677
private void close() {
77-
System.out.println("Closing Handler socket");
78+
//System.out.println("Closing Handler socket");
7879
try {
7980
if (!clientSocket.isClosed()) {
8081
clientSocket.close();
@@ -89,12 +90,13 @@ private CSGResponse processCSGRequest(CSGRequest request) {
8990
CSGClient.setServerCall(true);
9091
try {
9192
List<CSG> csgList = request.getCsgList();
93+
9294
switch (request.getOperation()) {
9395
case DIFFERENCE:
9496
CSG first = csgList.remove(0);
95-
if(csgList.size()==1) {
97+
if (csgList.size() == 1) {
9698
back.add(first.difference(csgList.get(0)));
97-
}else
99+
} else
98100
back.add(first.difference(csgList));
99101
break;
100102
case INTERSECT:
@@ -107,11 +109,19 @@ private CSGResponse processCSGRequest(CSGRequest request) {
107109
back.add(c.triangulate(true));
108110
break;
109111
case UNION:
110-
if(csgList.size()==2) {
111-
back.add(csgList.get(0).union(csgList.get(1)));
112-
}else
113-
back.add(CSG.unionAll(csgList));
114-
break;
112+
try {
113+
114+
if (csgList.size() == 2) {
115+
CSG csg = csgList.get(0);
116+
CSG csg2 = csgList.get(1);
117+
back.add(csg.union(csg2));
118+
} else
119+
back.add(CSG.unionAll(csgList));
120+
break;
121+
} catch (Throwable tr) {
122+
tr.printStackTrace();
123+
throw tr;
124+
}
115125
case minkowskiHullShape:
116126
CSG m1 = csgList.remove(0);
117127
CSG t = csgList.remove(0);

src/main/java/eu/mihosoft/vrl/v3d/Node.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ public Node(List<Polygon> polygons) {
8181
}
8282
}
8383

84-
/**
85-
* Constructor. Creates a node without polygons.
86-
*/
87-
public Node() {
84+
// /**
85+
// * Constructor. Creates a node without polygons.
86+
// */
87+
private Node() {
8888
this(null);
8989
}
9090

@@ -96,7 +96,7 @@ public Node() {
9696
@Override
9797
public Node clone() {
9898
Node node = new Node();
99-
node.plane = this.plane == null ? null : this.plane.clone();
99+
node.setPlane(this.getPlane() == null ? null : this.getPlane().clone());
100100
node.front = this.front == null ? null : this.front.clone();
101101
node.back = this.back == null ? null : this.back.clone();
102102
// node.polygons = new ArrayList<>();
@@ -132,16 +132,16 @@ public void invert() {
132132
polygon.flip();
133133
});
134134

135-
if (this.plane == null && !polygons.isEmpty()) {
136-
this.plane = polygons.get(0).getPlane().clone();
137-
} else if (this.plane == null && polygons.isEmpty()) {
135+
if (this.getPlane() == null && !polygons.isEmpty()) {
136+
this.setPlane(polygons.get(0).getPlane().clone());
137+
} else if (this.getPlane() == null && polygons.isEmpty()) {
138138

139139
//com.neuronrobotics.sdk.common.Log.error("Please fix me! I don't know what to do?");
140-
throw new RuntimeException("Please fix me! I don't know what to do?");
140+
throw new RuntimeException("Please fix me! Plane = "+plane+" and polygons are empty");
141141
// return;
142142
}
143143

144-
this.plane.flip();
144+
this.getPlane().flip();
145145

146146
if (this.front != null) {
147147
this.front.invert();
@@ -166,15 +166,15 @@ public void invert() {
166166
*/
167167
private List<Polygon> clipPolygons(List<Polygon> polygons) {
168168

169-
if (this.plane == null) {
169+
if (this.getPlane() == null) {
170170
return new ArrayList<>(polygons);
171171
}
172172

173173
List<Polygon> frontP = new ArrayList<>();
174174
List<Polygon> backP = new ArrayList<>();
175175

176176
for (Polygon polygon : polygons) {
177-
this.plane.splitPolygon(polygon, frontP, backP, frontP, backP);
177+
this.getPlane().splitPolygon(polygon, frontP, backP, frontP, backP);
178178
}
179179
if (this.front != null) {
180180
frontP = this.front.clipPolygons(frontP);
@@ -264,8 +264,8 @@ public final void build(List<Polygon> polygons, long depth, long maxDepth) {
264264
return;
265265
}
266266

267-
if (this.plane == null) {
268-
this.plane = polygons.get(0).getPlane().clone();
267+
if (this.getPlane() == null) {
268+
this.setPlane(polygons.get(0).getPlane().clone());
269269
}
270270
//this.polygons.add(polygons.get(0));
271271

@@ -274,7 +274,7 @@ public final void build(List<Polygon> polygons, long depth, long maxDepth) {
274274

275275
// parellel version does not work here
276276
for(int i=0;i<polygons.size();i++) {
277-
this.plane.splitPolygon(polygons.get(i), this.polygons, this.polygons, frontP, backP);
277+
this.getPlane().splitPolygon(polygons.get(i), this.polygons, this.polygons, frontP, backP);
278278
}
279279
if (frontP.size() > 0) {
280280
if (this.front == null) {
@@ -289,4 +289,14 @@ public final void build(List<Polygon> polygons, long depth, long maxDepth) {
289289
this.back.build(backP, depth + 1,maxDepth);
290290
}
291291
}
292+
293+
public Plane getPlane() {
294+
return plane;
295+
}
296+
297+
public void setPlane(Plane plane) {
298+
if(plane==null)
299+
throw new RuntimeException("Plane can not be null!");
300+
this.plane = plane;
301+
}
292302
}

src/main/java/eu/mihosoft/vrl/v3d/Polygon.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,8 @@ public Plane getPlane() {
832832
}
833833

834834
public void setPlane(Plane plane) {
835+
if(plane==null)
836+
throw new RuntimeException("Plane can not be null!");
835837
this.plane = plane;
836838
}
837839
}

0 commit comments

Comments
 (0)