summaryrefslogtreecommitdiff
path: root/src/common/CollisionDetector.java
blob: a4c7b5361f1c68316e94c2030c0bfa57dbe71a34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
package common;

import java.util.ArrayList;

import javax.media.j3d.Appearance;
import javax.media.j3d.Geometry;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.Group;
import javax.media.j3d.IndexedTriangleArray;
import javax.media.j3d.IndexedTriangleFanArray;
import javax.media.j3d.IndexedTriangleStripArray;
import javax.media.j3d.Node;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TriangleArray;
import javax.media.j3d.TriangleFanArray;
import javax.media.j3d.TriangleStripArray;
import javax.vecmath.Matrix3f;
import javax.vecmath.Point3f;
import javax.vecmath.Tuple3f;
import javax.vecmath.Vector3f;

import tesseract.objects.HalfSpace;
import tesseract.objects.Particle;
import tesseract.objects.Polygon;
import tesseract.objects.Sphere;

import com.sun.j3d.utils.geometry.Primitive;

@SuppressWarnings("restriction")
public class CollisionDetector {
	public static final float EPSILON = 0.0001f;
	private static final ArrayList<CollisionInfo> EMPTY_COLLISION_LIST = new ArrayList<CollisionInfo>();

	public static class Triangle {
		private Vector3f a;
		private Vector3f b;
		private Vector3f c;
		private Vector3f normal;
		private float intercept;

		private static class Line {
			public Vector3f point;
			public Vector3f direction;
			
			public Line() {
				point = new Vector3f();
				direction = new Vector3f();
			}
		}
		
		private static class TPair {
			public float t0;
			public float t1;
		}

		public Triangle(Tuple3f a, Tuple3f b, Tuple3f c) {
			this.a = new Vector3f(a);
			this.b = new Vector3f(b);
			this.c = new Vector3f(c);
			Vector3f tmp = new Vector3f();
			tmp.scaleAdd(-1, a, c);
			Vector3f tmp2 = new Vector3f();
			tmp2.scaleAdd(-1, b, c);
			normal = new Vector3f();
			normal.cross(tmp, tmp2);
			if (normal.lengthSquared() == 0)
				throw new IllegalArgumentException("Degenerate triangle");
			normal.normalize();
			intercept = normal.dot(this.a);
		}
		
		// Inspired by Tomas Moller's "A Fast Triangle-Triangle Intersection Test"	
		public CollisionInfo getIntersection(Triangle other) {
			float d_0_0 = other.normal.dot(a) - other.intercept;
			float d_0_1 = other.normal.dot(b) - other.intercept;
			float d_0_2 = other.normal.dot(c) - other.intercept;
			if (Math.abs(d_0_0) < EPSILON)
				d_0_0 = 0;
			if (Math.abs(d_0_1) < EPSILON)
				d_0_1 = 0;
			if (Math.abs(d_0_2) < EPSILON)
				d_0_2 = 0;
			if (d_0_0 != 0 && d_0_1 != 0 && d_0_2 != 0 && Math.signum(d_0_0) == Math.signum(d_0_1) && Math.signum(d_0_1) == Math.signum(d_0_2))
				return null;
			
			float d_1_0 = normal.dot(other.a) - intercept;
			float d_1_1 = normal.dot(other.b) - intercept;
			float d_1_2 = normal.dot(other.c) - intercept;
			if (Math.abs(d_1_0) < EPSILON)
				d_1_0 = 0;
			if (Math.abs(d_1_1) < EPSILON)
				d_1_1 = 0;
			if (Math.abs(d_1_2) < EPSILON)
				d_1_2 = 0;
			if (d_1_0 != 0 && d_1_1 != 0 && d_1_2 != 0 && Math.signum(d_1_0) == Math.signum(d_1_1) && Math.signum(d_1_1) == Math.signum(d_1_2))
				return null;

			// Coplanar, assume no collision
			if (d_0_0 == 0 && d_0_1 == 0 && d_0_2 == 0)
				return null;
			
			Line line = calculateLineOfIntersection(other);
			TPair r0 = calculateRegionOfIntersection(line, d_0_0, d_0_1, d_0_2);
			TPair r1 = other.calculateRegionOfIntersection(line, d_1_0, d_1_1, d_1_2);
			
			if (r0.t1 < r1.t0 || r0.t0 > r1.t1)
				return null;
			
			Vector3f contactPoint = new Vector3f();
			if (r0.t0 >= r1.t0 && r0.t1 <= r1.t1)
				contactPoint.scaleAdd((r0.t0 + r0.t1) / 2, line.direction, line.point);
			else if (r0.t0 <= r1.t0 && r0.t1 >= r1.t1)
				contactPoint.scaleAdd((r1.t0 + r1.t1) / 2, line.direction, line.point);
			else if (r0.t0 < r1.t0)
				contactPoint.scaleAdd((r0.t1 + r1.t0) / 2, line.direction, line.point);
			else
				contactPoint.scaleAdd((r0.t0 + r1.t1) / 2, line.direction, line.point);
			
			assert(Math.abs(normal.dot(contactPoint) - intercept) < 0.01);
			assert(Math.abs(other.normal.dot(contactPoint) - other.intercept) < 0.01);
			
			float penetration = Float.NEGATIVE_INFINITY;
			boolean useThisNormal = false;
			if (d_0_0 <= 0 && d_0_0 >= penetration)
				penetration = d_0_0;
			if (d_0_1 <= 0 && d_0_1 >= penetration)
				penetration = d_0_1;
			if (d_0_2 <= 0 && d_0_2 >= penetration)
				penetration = d_0_2;
			if (d_1_0 <= 0 && d_1_0 >= penetration) {
				penetration = d_1_0;
				useThisNormal = true;
			}
			if (d_1_1 <= 0 && d_1_1 >= penetration) {
				penetration = d_1_1;
				useThisNormal = true;
			}
			if (d_1_2 <= 0 && d_1_2 >= penetration) {
				penetration = d_1_2;
				useThisNormal = true;
			}
			Vector3f contactNormal;
			if (useThisNormal)
				contactNormal = new Vector3f(normal);
			else {
				contactNormal = new Vector3f(other.normal);
				contactNormal.negate();
			}
			
			return new CollisionInfo(contactPoint, contactNormal, -penetration);
		}

		private Line calculateLineOfIntersection(Triangle other) {
			Line line = new Line();
			line.direction.cross(normal, other.normal);
			if (Math.abs(line.direction.x) < EPSILON)
				line.direction.x = 0;
			if (Math.abs(line.direction.y) < EPSILON)
				line.direction.y = 0;
			if (Math.abs(line.direction.z) < EPSILON)
				line.direction.z = 0;
			line.direction.normalize();
			
			if (line.direction.x != 0) { // x <- 0
				if (normal.y != 0) {
					line.point.z = (other.normal.y / normal.y * intercept - other.intercept) / (other.normal.y / normal.y * normal.z - other.normal.z);
					line.point.y = (intercept - normal.z * line.point.z) / normal.y;
				} else { // normal.z != 0
					line.point.y = (other.normal.z / normal.z * intercept - other.intercept) / (other.normal.z / normal.z * normal.y - other.normal.y);
					line.point.z = (intercept - normal.y * line.point.y) / normal.z;
				}
			} else if (line.direction.y != 0) { // y <- 0
				if (normal.x != 0) {
					line.point.z = (other.normal.x / normal.x * intercept - other.intercept) / (other.normal.x / normal.x * normal.z - other.normal.z);
					line.point.x = (intercept - normal.z * line.point.z) / normal.x;
				} else { // normal.z != 0
					line.point.x = (other.normal.z / normal.z * intercept - other.intercept) / (other.normal.z / normal.z * normal.x - other.normal.x);
					line.point.z = (intercept - normal.x * line.point.x) / normal.z;
				}
			} else { // z <- 0
				if (normal.x != 0) {
					line.point.y = (other.normal.x / normal.x * intercept - other.intercept) / (other.normal.x / normal.x * normal.y - other.normal.y);
					line.point.x = (intercept - normal.y * line.point.y) / normal.x;
				} else { // normal.y != 0
					line.point.x = (other.normal.y / normal.y * intercept - other.intercept) / (other.normal.y / normal.y * normal.x - other.normal.x);
					line.point.y = (intercept - normal.x * line.point.x) / normal.y;
				}
			}

			assert(Math.abs(normal.dot(line.point) - intercept) < 0.01);
			assert(Math.abs(other.normal.dot(line.point) - other.intercept) < 0.01);

			return line;
		}
		
		private TPair calculateRegionOfIntersection(Line line, float d0, float d1, float d2) {
			Vector3f v0, v1, v2;
			if (Math.signum(d0) != 0 && Math.signum(d0) != Math.signum(d1) && Math.signum(d0) != Math.signum(d2)) {
				v0 = b; v1 = a; v2 = c;
				float tmp = d0; d0 = d1; d1 = tmp;
			} else if (Math.signum(d1) != 0 && Math.signum(d0) != Math.signum(d1) && Math.signum(d1) != Math.signum(d2)) {
				v0 = a; v1 = b; v2 = c;
			} else if (Math.signum(d2) != 0 && Math.signum(d0) != Math.signum(d2) && Math.signum(d1) != Math.signum(d2)) {
				v0 = a; v1 = c; v2 = b;
				float tmp = d1; d1 = d2; d2 = tmp; 
			} else if (Math.signum(d0) == 0) {
				v0 = b; v1 = a; v2 = c;
				float tmp = d0; d0 = d1; d1 = tmp;
			} else if (Math.signum(d1) == 0) {
				v0 = a; v1 = b; v2 = c;
			} else {
				v0 = a; v1 = c; v2 = b;
				float tmp = d1; d1 = d2; d2 = tmp;
			}
			
			Vector3f tmp = new Vector3f();
			tmp.scaleAdd(-1, line.point, v0);
			float p0 = line.direction.dot(tmp);
			tmp.scaleAdd(-1, line.point, v1);
			float p1 = line.direction.dot(tmp);
			tmp.scaleAdd(-1, line.point, v2);
			float p2 = line.direction.dot(tmp);
			
			TPair region = new TPair();
			region.t0 = p0 + (p1 - p0) * d0 / (d0 - d1);
			region.t1 = p2 + (p1 - p2) * d2 / (d2 - d1);
			if (region.t1 < region.t0) {
				float tmp2 = region.t0;
				region.t0 = region.t1;
				region.t1 = tmp2;
			}
			return region;
		}
	}
	
	public static ArrayList<CollisionInfo> calculateCollisions(CollidableObject a, CollidableObject b) {
		if (a == b)
			return EMPTY_COLLISION_LIST;
		if (a instanceof HalfSpace) {
			if (b instanceof HalfSpace)
				return EMPTY_COLLISION_LIST;
			if (b instanceof Particle)
				return calculateCollisions((HalfSpace)a, (Particle)b);
			if (b instanceof Sphere)
				return calculateCollisions((HalfSpace)a, (Sphere)b);
			return calculateCollisions((HalfSpace) a, b.getVertices());
		}
		if (b instanceof HalfSpace) {
			if (a instanceof Particle)
				return flipContactNormals(calculateCollisions((HalfSpace)b, (Particle)a));
			if (a instanceof Sphere)
				return flipContactNormals(calculateCollisions((HalfSpace)b, (Sphere)a));
			return flipContactNormals(calculateCollisions((HalfSpace)b, a.getVertices()));
		}
		if (a instanceof Particle) {
			if (b instanceof Particle)
				return EMPTY_COLLISION_LIST;
			if (b instanceof Sphere)
				return calculateCollisions((Particle)a, (Sphere)b);
			if (b instanceof Polygon)
				return calculateCollisions((Particle)a, (Polygon)b);
		}
		if (b instanceof Particle) {
			if (a instanceof Sphere)
				return flipContactNormals(calculateCollisions((Particle)b, (Sphere)a));
			if (a instanceof Polygon)
				return flipContactNormals(calculateCollisions((Particle)b, (Polygon)a));
		}
		if (a instanceof Sphere && b instanceof Sphere)
			return calculateCollisions((Sphere)a, (Sphere)b);
		
		if (!a.getBounds().intersect(b.getBounds()))
			return EMPTY_COLLISION_LIST;
		
		if (a instanceof Particle)
			return calculateCollisions((Particle)a, b);
		if (b instanceof Particle)
			return flipContactNormals(calculateCollisions((Particle)b, a));
		if (a instanceof Polygon)
			return calculateCollisions((Polygon)a, b);
		if (b instanceof Polygon)
			return calculateCollisions((Polygon)b, a);
		return CollisionDetector.calculateCollisions(a.getCollisionTriangles(), b.getCollisionTriangles());
	}
	
	private static ArrayList<CollisionInfo> calculateCollisions(HalfSpace a, Particle b) {
		float penetration = a.intercept - a.normal.dot(b.position);
		if (penetration < 0)
			return EMPTY_COLLISION_LIST;
		Vector3f contactPoint = new Vector3f();
		contactPoint.scaleAdd(penetration, a.normal, b.position);
		assert(Math.abs(a.normal.dot(contactPoint) - a.intercept) < 0.01);
		ArrayList<CollisionInfo> collisions = new ArrayList<CollisionInfo>();
		collisions.add(new CollisionInfo(contactPoint, new Vector3f(a.normal), penetration));
		return collisions;
	}

	private static ArrayList<CollisionInfo> calculateCollisions(HalfSpace a, Sphere b) {
		float penetration = b.radius - (a.normal.dot(b.position) - a.intercept);
		if (penetration < 0)
			return EMPTY_COLLISION_LIST;
		Vector3f contactPoint = new Vector3f();
		contactPoint.scaleAdd(-(b.radius - penetration), a.normal, b.position);
		assert(Math.abs(a.normal.dot(contactPoint) - a.intercept) < 0.01);
		ArrayList<CollisionInfo> collisions = new ArrayList<CollisionInfo>();
		collisions.add(new CollisionInfo(contactPoint, new Vector3f(a.normal), penetration));
		return collisions;
	}

	private static ArrayList<CollisionInfo> calculateCollisions(HalfSpace a, ArrayList<Vector3f> setB) {
		ArrayList<CollisionInfo> collisions = new ArrayList<CollisionInfo>();
		for (Vector3f vertex : setB) {
			float penetration = a.intercept - a.normal.dot(vertex);
			if (penetration >= 0) {
				Vector3f contactPoint = new Vector3f();
				contactPoint.scaleAdd(penetration, a.normal, vertex);
				assert(Math.abs(a.normal.dot(contactPoint) - a.intercept) < 0.01);
				collisions.add(new CollisionInfo(contactPoint, new Vector3f(a.normal), penetration));
			}
		}
		return collisions;
	}

	private static ArrayList<CollisionInfo> calculateCollisions(Particle a, Sphere b) {
		Vector3f delta = new Vector3f();
		delta.scaleAdd(-1, a.position, b.position);
		float penetration = b.radius - delta.length();
		if (penetration < 0)
			return EMPTY_COLLISION_LIST;
		
		ArrayList<CollisionInfo> collisions = new ArrayList<CollisionInfo>();
		delta.normalize();
		Vector3f contactPoint = new Vector3f();
		contactPoint.scaleAdd(-(b.radius - 0.5f * penetration), delta, b.position);
		collisions.add(new CollisionInfo(contactPoint, delta, penetration));
		return collisions;
	}

	private static ArrayList<CollisionInfo> calculateCollisions(Particle a, Polygon b) {
		float penetration = b.intercept - b.normal.dot(a.position);
		float previousPenetration = b.intercept - b.normal.dot(a.previousPosition);
		if (Math.signum(penetration) == Math.signum(previousPenetration))
			return EMPTY_COLLISION_LIST;
		
		for (Triangle triangle : b.getCollisionTriangles()) {
			Matrix3f tmp = new Matrix3f(a.previousPosition.x - a.position.x, triangle.b.x - triangle.a.x, triangle.c.x - triangle.a.x,
			                            a.previousPosition.y - a.position.y, triangle.b.y - triangle.a.y, triangle.c.y - triangle.a.y,
			                            a.previousPosition.z - a.position.z, triangle.b.z - triangle.a.z, triangle.c.z - triangle.a.z);
			tmp.invert();
			Vector3f intercept = new Vector3f();
			intercept.scaleAdd(-1, triangle.a, a.previousPosition);
			tmp.transform(intercept);
			
			assert(intercept.x >= 0 && intercept.x <= 1);
			
			if (intercept.y >= 0 && intercept.y <= 1 && intercept.z >= 0 && intercept.z <= 1 && (intercept.y + intercept.z) <= 1) {
				Vector3f contactPoint = new Vector3f();
				contactPoint.scaleAdd(-1, a.previousPosition, a.position);
				contactPoint.scale(intercept.x);
				contactPoint.add(a.previousPosition);
				assert(Math.abs(b.normal.dot(contactPoint) - b.intercept) < 0.01);
				Vector3f contactNormal = new Vector3f(b.normal);
				if (penetration - previousPenetration > 0)
					contactNormal.negate();
				else
					penetration = -penetration;
				ArrayList<CollisionInfo> collisions = new ArrayList<CollisionInfo>();
				collisions.add(new CollisionInfo(contactPoint, contactNormal, penetration));
				return collisions;
			}
		}
		return EMPTY_COLLISION_LIST;
	}
	
	private static ArrayList<CollisionInfo> calculateCollisions(Particle a, CollidableObject b) {
		ArrayList<CollisionInfo> collisions = new ArrayList<CollisionInfo>();
		for (Triangle triangle : b.getCollisionTriangles()) {
			float penetration = triangle.intercept - triangle.normal.dot(a.position);
			if (penetration < 0 || (!collisions.isEmpty() && penetration <= collisions.get(0).penetration))
				continue;
			float previousPenetration = triangle.intercept - triangle.normal.dot(a.previousPosition);
			if (Math.signum(penetration) == Math.signum(previousPenetration))
				continue;
			
			Matrix3f tmp = new Matrix3f(a.previousPosition.x - a.position.x, triangle.b.x - triangle.a.x, triangle.c.x - triangle.a.x,
			                            a.previousPosition.y - a.position.y, triangle.b.y - triangle.a.y, triangle.c.y - triangle.a.y,
			                            a.previousPosition.z - a.position.z, triangle.b.z - triangle.a.z, triangle.c.z - triangle.a.z);
			tmp.invert();
			Vector3f intercept = new Vector3f();
			intercept.scaleAdd(-1, triangle.a, a.previousPosition);
			tmp.transform(intercept);
			
			assert(intercept.x >= 0 && intercept.x <= 1);
			
			if (intercept.y >= 0 && intercept.y <= 1 && intercept.z >= 0 && intercept.z <= 1 && (intercept.y + intercept.z) <= 1) {
				Vector3f contactPoint = new Vector3f();
				contactPoint.scaleAdd(-1, a.previousPosition, a.position);
				contactPoint.scale(intercept.x);
				contactPoint.add(a.previousPosition);
				assert(Math.abs(triangle.normal.dot(contactPoint) - triangle.intercept) < 0.01);
				Vector3f contactNormal = new Vector3f(triangle.normal);
				if (penetration - previousPenetration > 0)
					contactNormal.negate();
				else
					penetration = -penetration;				
				collisions.clear();
				collisions.add(new CollisionInfo(contactPoint, contactNormal, penetration));
			}
		}
		return collisions;
	}
	
	private static ArrayList<CollisionInfo> calculateCollisions(Sphere a, Sphere b) {
		Vector3f delta = new Vector3f();
		delta.scaleAdd(-1, a.position, b.position);
		float penetration = a.radius + b.radius - delta.length();
		if (penetration < 0)
			return EMPTY_COLLISION_LIST;
		
		ArrayList<CollisionInfo> collisions = new ArrayList<CollisionInfo>();
		delta.normalize();
		Vector3f contactPoint = new Vector3f();
		contactPoint.scaleAdd(a.radius - 0.5f * penetration, delta, a.position);
		collisions.add(new CollisionInfo(contactPoint, delta, penetration));
		return collisions;
	}

	private static ArrayList<CollisionInfo> calculateCollisions(Polygon a, CollidableObject b) {
		ArrayList<CollisionInfo> collisions = calculateCollisions(a.getCollisionTriangles(), b.getCollisionTriangles());
		int size = collisions.size();
		collisions.ensureCapacity(2 * size);
		for (int i = 0; i < size; i++) {
			collisions.add(collisions.get(i).clone());
			collisions.get(collisions.size() - 1).contactNormal.negate();
		}
		return collisions;
	}
	
	private static ArrayList<CollisionInfo> calculateCollisions(ArrayList<Triangle> setA, ArrayList<Triangle> setB) {
		ArrayList<CollisionInfo> collisions = new ArrayList<CollisionInfo>();
		for (int i = 0; i < setA.size(); i++)
			for (int j = 0; j < setB.size(); j++) {
				CollisionInfo collision = setA.get(i).getIntersection(setB.get(j));
				if (collision != null)
					collisions.add(collision);
			}
		return collisions;
	}

	private static ArrayList<CollisionInfo> flipContactNormals(ArrayList<CollisionInfo> collisions) {
		for (CollisionInfo collision : collisions)
			collision.contactNormal.negate();
		return collisions;
	}

	public static ArrayList<Vector3f> extractVertices(Node node) {
		ArrayList<Vector3f> vertices = new ArrayList<Vector3f>();
		extractVertices(node, vertices);
		return vertices;
	}
	
	private static void extractVertices(Node node, ArrayList<Vector3f> vertices) {
		if (node instanceof Primitive) {
			Primitive prim = (Primitive)node;
			int index = 0;
			Shape3D shape;
			Transform3D L2V = new Transform3D();
			while ((shape = prim.getShape(index++)) != null) {
				shape.getLocalToVworld(L2V);
				for (int i = 0; i < shape.numGeometries(); i++)
					extractVertices(shape.getGeometry(i), L2V, vertices);
			}
		} else if (node instanceof Shape3D) {
			Shape3D shape = (Shape3D)node;
			Transform3D L2V = new Transform3D();
			shape.getLocalToVworld(L2V);
			for (int i = 0; i < shape.numGeometries(); i++)
				extractVertices(shape.getGeometry(i), L2V, vertices);
		} else if (node instanceof Group) {
			Group group = (Group)node;
			for (int i = 0; i < group.numChildren(); i++)
				extractVertices(group.getChild(i), vertices);
		} else 
			throw new IllegalArgumentException("Illegal node type for vertex extraction");
	}

	private static void extractVertices(Geometry geometry, Transform3D transform, ArrayList<Vector3f> vertices) {
		if (geometry instanceof GeometryArray) {
			GeometryArray trueGeometry = (GeometryArray)geometry;
			vertices.ensureCapacity(vertices.size() + trueGeometry.getValidVertexCount());
			Point3f vertex = new Point3f();
			for (int i = 0; i < trueGeometry.getValidVertexCount(); i++) {
				trueGeometry.getCoordinate(i, vertex);
				transform.transform(vertex);
				vertices.add(new Vector3f(vertex));
			}
		}  else
			throw new IllegalArgumentException("Illegal geometry type for vertex extraction");
	}
	
	public static ArrayList<Triangle> triangularize(Node node) {
		ArrayList<Triangle> triangles = new ArrayList<Triangle>();
		triangularize(node, triangles);
		return triangles;
	}
	
	private static void triangularize(Node node, ArrayList<Triangle> triangles) {
		if (node instanceof Primitive) {
			Primitive prim = (Primitive)node;
			triangles.ensureCapacity(prim.getNumTriangles());
			int index = 0;
			Shape3D shape;
			Transform3D L2V = new Transform3D();
			while ((shape = prim.getShape(index++)) != null) {
				shape.getLocalToVworld(L2V);
				for (int i = 0; i < shape.numGeometries(); i++)
					triangularize(shape.getGeometry(i), L2V, triangles);
			}
		} else if (node instanceof Shape3D) {
			Shape3D shape = (Shape3D)node;
			Transform3D L2V = new Transform3D();
			shape.getLocalToVworld(L2V);
			for (int i = 0; i < shape.numGeometries(); i++)
				triangularize(shape.getGeometry(i), L2V, triangles);
		} else if (node instanceof Group) {
			Group group = (Group)node;
			for (int i = 0; i < group.numChildren(); i++)
				triangularize(group.getChild(i), triangles);
		} else 
			throw new IllegalArgumentException("Illegal node type for triangularization");
	}

	private static void triangularize(Geometry geometry, Transform3D transform, ArrayList<Triangle> triangles) {
		if (geometry instanceof TriangleArray) {
			TriangleArray trueGeometry = (TriangleArray)geometry;
			Point3f vertices[] = new Point3f[trueGeometry.getValidVertexCount()];
			for (int i = 0; i < vertices.length; i++) {
				vertices[i] = new Point3f();
				trueGeometry.getCoordinate(i, vertices[i]);
				transform.transform(vertices[i]);
			}
			for (int i = 0; i < vertices.length; i += 3)
				try {
					triangles.add(new Triangle(vertices[i], vertices[i+1], vertices[i+2]));
				} catch (IllegalArgumentException e) {
				}
		} else if (geometry instanceof TriangleStripArray) {
			TriangleStripArray trueGeometry = (TriangleStripArray)geometry;
			int stripVertexCounts[] = new int[trueGeometry.getNumStrips()];
			trueGeometry.getStripVertexCounts(stripVertexCounts);
			Point3f vertices[] = new Point3f[trueGeometry.getValidVertexCount()];
			for (int i = 0; i < vertices.length; i++) {
				vertices[i] = new Point3f();
				trueGeometry.getCoordinate(i, vertices[i]);
				transform.transform(vertices[i]);
			}
			int base = 0;
			for (int i = 0; i < stripVertexCounts.length; i++) {
				boolean reverse = false;
				for (int j = 2; j < stripVertexCounts[i]; j++) {
					try {
						if (reverse)
							triangles.add(new Triangle(vertices[base+j-2], vertices[base+j], vertices[base+j-1]));
						else
							triangles.add(new Triangle(vertices[base+j-2], vertices[base+j-1], vertices[base+j]));
					} catch (IllegalArgumentException e) {
					}
					reverse = !reverse;
				}
				base += stripVertexCounts[i];
			}
		} else if (geometry instanceof TriangleFanArray) {
			TriangleFanArray trueGeometry = (TriangleFanArray)geometry;
			int stripVertexCounts[] = new int[trueGeometry.getNumStrips()];
			trueGeometry.getStripVertexCounts(stripVertexCounts);
			Point3f vertices[] = new Point3f[trueGeometry.getValidVertexCount()];
			for (int i = 0; i < vertices.length; i++) {
				vertices[i] = new Point3f();
				trueGeometry.getCoordinate(i, vertices[i]);
				transform.transform(vertices[i]);
			}
			int base = 0;
			for (int i = 0; i < stripVertexCounts.length; i++) {
				for (int j = 2; j < stripVertexCounts[i]; j++)
					try {
						triangles.add(new Triangle(vertices[base], vertices[base+j-1], vertices[base+j]));
					} catch (IllegalArgumentException e) {
					}
				base += stripVertexCounts[i];
			}
		} else if (geometry instanceof IndexedTriangleArray) {
			IndexedTriangleArray trueGeometry = (IndexedTriangleArray)geometry;
			Point3f vertices[] = new Point3f[trueGeometry.getValidVertexCount()];
			for (int i = 0; i < vertices.length; i++) {
				vertices[i] = new Point3f();
				trueGeometry.getCoordinate(i, vertices[i]);
				transform.transform(vertices[i]);
			}
			int indices[] = new int[trueGeometry.getValidIndexCount()];
			trueGeometry.getCoordinateIndices(0, indices);
			for (int i = 0; i < indices.length; i += 3)
				try {
					triangles.add(new Triangle(vertices[indices[i]], vertices[indices[i+1]], vertices[indices[i+2]]));
				} catch (IllegalArgumentException e) {
				}
		} else if (geometry instanceof IndexedTriangleStripArray) {
			IndexedTriangleStripArray trueGeometry = (IndexedTriangleStripArray)geometry;
			int stripIndexCounts[] = new int[trueGeometry.getNumStrips()];
			trueGeometry.getStripIndexCounts(stripIndexCounts);
			Point3f vertices[] = new Point3f[trueGeometry.getValidVertexCount()];
			for (int i = 0; i < vertices.length; i++) {
				vertices[i] = new Point3f();
				trueGeometry.getCoordinate(i, vertices[i]);
				transform.transform(vertices[i]);
			}			
			int indices[] = new int[trueGeometry.getValidIndexCount()];
			trueGeometry.getCoordinateIndices(0, indices);
			int base = 0;
			for (int i = 0; i < stripIndexCounts.length; i++) {
				boolean reverse = false;
				for (int j = 2; j < stripIndexCounts[i]; j++) {
					try {
						if (reverse)
							triangles.add(new Triangle(vertices[indices[base+j-2]], vertices[indices[base+j]], vertices[indices[base+j-1]]));
						else
							triangles.add(new Triangle(vertices[indices[base+j-2]], vertices[indices[base+j-1]], vertices[indices[base+j]]));
					} catch (IllegalArgumentException e) {
					}
					reverse = !reverse;
				}
				base += stripIndexCounts[i];
			}
		} else if (geometry instanceof IndexedTriangleFanArray) {
			IndexedTriangleFanArray trueGeometry = (IndexedTriangleFanArray)geometry;
			int stripIndexCounts[] = new int[trueGeometry.getNumStrips()];
			trueGeometry.getStripIndexCounts(stripIndexCounts);
			Point3f vertices[] = new Point3f[trueGeometry.getValidVertexCount()];
			for (int i = 0; i < vertices.length; i++) {
				vertices[i] = new Point3f();
				trueGeometry.getCoordinate(i, vertices[i]);
				transform.transform(vertices[i]);
			}			
			int indices[] = new int[trueGeometry.getValidIndexCount()];
			trueGeometry.getCoordinateIndices(0, indices);
			int base = 0;
			for (int i = 0; i < stripIndexCounts.length; i++) {
				for (int j = 2; j < stripIndexCounts[i]; j++)
					try {
						triangles.add(new Triangle(vertices[indices[base]], vertices[indices[base+j-1]], vertices[indices[base+j]]));
					} catch (IllegalArgumentException e) {
					}
				base += stripIndexCounts[i];
			}
		} else
			throw new IllegalArgumentException("Illegal geometry type for triangularization");
	}

	public static Shape3D createShape(ArrayList<Triangle> triangles) {
		TriangleArray geometry = new TriangleArray(3 * triangles.size(), TriangleArray.COORDINATES);
		for (int i = 0; i < triangles.size(); i++) {
			geometry.setCoordinate(3 * i, new Point3f(triangles.get(i).a));
			geometry.setCoordinate(3 * i + 1, new Point3f(triangles.get(i).b));
			geometry.setCoordinate(3 * i + 2, new Point3f(triangles.get(i).c));
		}
		Appearance appearance = new Appearance();
		PolygonAttributes polyAttr = new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_NONE, 0);
		appearance.setPolygonAttributes(polyAttr);
		return new Shape3D(geometry, appearance);
	}
}