-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathTriangle.java
More file actions
1255 lines (1078 loc) · 42.4 KB
/
Triangle.java
File metadata and controls
1255 lines (1078 loc) · 42.4 KB
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2012 United States Government as represented by the Administrator of the
* National Aeronautics and Space Administration.
* All Rights Reserved.
*/
package gov.nasa.worldwind.geom;
import gov.nasa.worldwind.util.Logging;
import com.jogamp.opengl.GL;
import java.nio.*;
import java.util.*;
/**
* Provides operations on triangles.
*
* @author Eric Dalgliesh 30/11/2006
* @version $Id: Triangle.java 1171 2013-02-11 21:45:02Z dcollins $
*/
public class Triangle
{
private static final double EPSILON = 0.0000001; // used in intersects method
private final Vec4 a;
private final Vec4 b;
private final Vec4 c;
/**
* Construct a triangle from three counter-clockwise ordered vertices. The front face of the triangle is determined
* by the right-hand rule.
*
* @param a the first vertex.
* @param b the second vertex.
* @param c the third vertex.
*
* @throws IllegalArgumentException if any vertex is null.
*/
public Triangle(Vec4 a, Vec4 b, Vec4 c)
{
if (a == null || b == null || c == null)
{
String msg = Logging.getMessage("nullValue.PointIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
this.a = a;
this.b = b;
this.c = c;
}
/**
* Returns the first vertex.
*
* @return the first vertex.
*/
public Vec4 getA()
{
return this.a;
}
/**
* Returns the second vertex.
*
* @return the second vertex.
*/
public Vec4 getB()
{
return this.b;
}
/**
* Returns the third vertex.
*
* @return the third vertex.
*/
public Vec4 getC()
{
return this.c;
}
// private Plane getPlane()
// {
// Vector ab, ac;
// ab = new Vector(this.b.subtract(this.a)).normalize();
// ac = new Vector(this.c.subtract(this.a)).normalize();
//
// Vector n = new Vector(new Point(ab.x(), ab.y(), ab.z(), ab.w()).cross(new Point(ac.x(), ac.y(), ac.z(), ac.w())));
//
// return new gov.nasa.worldwind.geom.Plane(n);
// }
// private Point temporaryIntersectPlaneAndLine(Line line, Plane plane)
// {
// Vector n = line.getDirection();
// Point v0 = Point.fromOriginAndDirection(plane.getDistance(), plane.getNormal(), Point.ZERO);
// Point p0 = line.getPointAt(0);
// Point p1 = line.getPointAt(1);
//
// double r1 = n.dot(v0.subtract(p0))/n.dot(p1.subtract(p0));
// if(r1 >= 0)
// return line.getPointAt(r1);
// return null;
// }
//
// private Triangle divide(double d)
// {
// d = 1/d;
// return new Triangle(this.a.multiply(d), this.b.multiply(d), this.c.multiply(d));
// }
/**
* Indicates whether a specified point is on the triangle.
*
* @param p the point to test. If null, the method returns false.
*
* @return true if the point is on the triangle, otherwise false.
*/
public boolean contains(Vec4 p)
{
if (p == null)
return false;
// Compute vectors
Vec4 v0 = this.c.subtract3(this.a);
Vec4 v1 = this.b.subtract3(this.a);
Vec4 v2 = p.subtract3(this.a);
// Compute dot products
double dot00 = v0.dotSelf3();
double dot01 = v0.dot3(v1);
double dot02 = v0.dot3(v2);
double dot11 = v1.dotSelf3();
double dot12 = v1.dot3(v2);
// Compute barycentric coordinates
double det = (dot00 * dot11 - dot01 * dot01);
double detInv = 1 / det;
double u = (dot11 * dot02 - dot01 * dot12) * detInv;
double v = (dot00 * dot12 - dot01 * dot02) * detInv;
// Check if point is contained in triangle (including edges and vertices)
return (u >= 0d) && (v >= 0d) && (u + v <= 1d);
// Check if point is contained inside triangle (NOT including edges or vertices)
// return (u > 0d) && (v > 0d) && (u + v < 1d);
}
/**
* Determine the intersection of the triangle with a specified line.
*
* @param line the line to test.
*
* @return the point of intersection if the line intersects the triangle, otherwise null.
*
* @throws IllegalArgumentException if the line is null.
*/
public Vec4 intersect(Line line)
{
Intersection intersection = intersect(line, this.a, this.b, this.c);
return intersection != null ? intersection.getIntersectionPoint() : null;
}
/**
* Determines the intersection of a specified line with a specified triangle. The triangle is specified by three
* points ordered counterclockwise. The triangle's front face is determined by the right-hand rule.
*
* @param line the line to test.
* @param a the first vertex of the triangle.
* @param b the second vertex of the triangle.
* @param c the third vertex of the triangle.
*
* @return the point of intersection if the line intersects the triangle, otherwise null.
*
* @throws IllegalArgumentException if the line or any of the triangle vertices is null.
*/
public static Intersection intersect(Line line, Vec4 a, Vec4 b, Vec4 c)
{
return intersect(line, a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z);
}
/**
* Determines the intersection of a specified line with a triangle specified by individual coordinates.
*
* @param line the line to test.
* @param vax the X coordinate of the first vertex of the triangle.
* @param vay the Y coordinate of the first vertex of the triangle.
* @param vaz the Z coordinate of the first vertex of the triangle.
* @param vbx the X coordinate of the second vertex of the triangle.
* @param vby the Y coordinate of the second vertex of the triangle.
* @param vbz the Z coordinate of the second vertex of the triangle.
* @param vcx the X coordinate of the third vertex of the triangle.
* @param vcy the Y coordinate of the third vertex of the triangle.
* @param vcz the Z coordinate of the third vertex of the triangle.
*
* @return the point of intersection if the line intersects the triangle, otherwise null.
*/
public static Intersection intersect(Line line,
double vax, double vay, double vaz, double vbx, double vby, double vbz, double vcx, double vcy, double vcz)
{
if (line == null)
{
String msg = Logging.getMessage("nullValue.LineIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
// taken from Moller and Trumbore
// http://www.cs.virginia.edu/~gfx/Courses/2003/ImageSynthesis/papers/Acceleration/
// Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf
Vec4 origin = line.getOrigin();
Vec4 dir = line.getDirection();
// find vectors for two edges sharing Point a: vb - va and vc - va
double edge1x = vbx - vax;
double edge1y = vby - vay;
double edge1z = vbz - vaz;
double edge2x = vcx - vax;
double edge2y = vcy - vay;
double edge2z = vcz - vaz;
// Start calculating determinant. Compute cross product of line direction and edge2.
double pvecx = (dir.y * edge2z) - (dir.z * edge2y);
double pvecy = (dir.z * edge2x) - (dir.x * edge2z);
double pvecz = (dir.x * edge2y) - (dir.y * edge2x);
// Get determinant.
double det = edge1x * pvecx + edge1y * pvecy + edge1z * pvecz; // edge1 dot pvec
if (det > -EPSILON && det < EPSILON) // If det is near zero, then ray lies on plane of triangle
return null;
double detInv = 1d / det;
// Distance from vertA to ray origin: origin - va
double tvecx = origin.x - vax;
double tvecy = origin.y - vay;
double tvecz = origin.z - vaz;
// Calculate u parameter and test bounds: 1/det * tvec dot pvec
double u = detInv * (tvecx * pvecx + tvecy * pvecy + tvecz * pvecz);
if (u < 0 || u > 1)
return null;
// Prepare to test v parameter: tvec cross edge1
double qvecx = (tvecy * edge1z) - (tvecz * edge1y);
double qvecy = (tvecz * edge1x) - (tvecx * edge1z);
double qvecz = (tvecx * edge1y) - (tvecy * edge1x);
// Calculate v parameter and test bounds: 1/det * dir dot qvec
double v = detInv * (dir.x * qvecx + dir.y * qvecy + dir.z * qvecz);
if (v < 0 || u + v > 1)
return null;
// Calculate the point of intersection on the line: t = 1/det * edge2 dot qvec;
double t = detInv * (edge2x * qvecx + edge2y * qvecy + edge2z * qvecz);
if (t < 0)
return null;
return new Intersection(line.getPointAt(t), t, false);
}
/**
* Compute the intersections of a line with a triangle strip.
*
* @param line the line to intersect.
* @param vertices the tri-strip vertices.
* @param indices the indices forming the tri-strip.
*
* @return the list of intersections with the line and the tri-strip, or null if there are no intersections.
*
* @throws IllegalArgumentException if the line, vertex buffer or index buffer is null.
*/
public static List<Intersection> intersectTriStrip(final Line line, FloatBuffer vertices, IntBuffer indices)
{
if (line == null)
{
String msg = Logging.getMessage("nullValue.LineIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (vertices == null || indices == null)
{
String msg = Logging.getMessage("nullValue.BufferIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
List<Intersection> intersections = null;
for (int n = indices.position(); n < indices.limit() - 2; n++)
{
Intersection intersection;
int i = indices.get(n) * 3;
int j = indices.get(n + 1) * 3;
int k = indices.get(n + 2) * 3;
// The triangle intersect method detects front and back face intersections so there's no reason to
// order the vertices.
intersection = intersect(line,
vertices.get(i), vertices.get(i + 1), vertices.get(i + 2),
vertices.get(j), vertices.get(j + 1), vertices.get(j + 2),
vertices.get(k), vertices.get(k + 1), vertices.get(k + 2));
if (intersection != null)
{
if (intersections == null)
intersections = new ArrayList<Intersection>();
intersections.add(intersection);
}
}
return intersections;
}
/**
* Compute the intersections of a line with a triangle strip.
*
* @param line the line to intersect.
* @param vertices the tri-strip vertices.
* @param indices the indices forming the tri-strip.
*
* @return the list of intersections with the line and the triangle strip, or null if there are no intersections.
*
* @throws IllegalArgumentException if the line, vertex array or index buffer is null.
*/
public static List<Intersection> intersectTriStrip(final Line line, Vec4[] vertices, IntBuffer indices)
{
if (line == null)
{
String msg = Logging.getMessage("nullValue.LineIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (vertices == null)
{
String msg = Logging.getMessage("nullValue.ArrayIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (indices == null)
{
String msg = Logging.getMessage("nullValue.BufferIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
List<Intersection> intersections = null;
for (int n = indices.position(); n < indices.limit() - 1; n++)
{
Intersection intersection;
int i = indices.get(n) * 3;
int j = indices.get(n + 1) * 3;
int k = indices.get(n + 2) * 3;
// The triangle intersect method detects front and back face intersections so there's no reason to
// order the vertices.
intersection = intersect(line, vertices[i], vertices[j], vertices[k]);
if (intersection != null)
{
if (intersections == null)
intersections = new ArrayList<Intersection>();
intersections.add(intersection);
}
}
return intersections;
}
/**
* Compute the intersections of a line with a triangle fan.
*
* @param line the line to intersect.
* @param vertices the tri-fan vertices.
* @param indices the indices forming the tri-fan.
*
* @return the list of intersections with the line and the triangle fan, or null if there are no intersections.
*
* @throws IllegalArgumentException if the line, vertex buffer or index buffer is null.
*/
public static List<Intersection> intersectTriFan(final Line line, FloatBuffer vertices, IntBuffer indices)
{
if (line == null)
{
String msg = Logging.getMessage("nullValue.LineIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (vertices == null || indices == null)
{
String msg = Logging.getMessage("nullValue.BufferIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
List<Intersection> intersections = null;
// Get the index and then the values of the constant vertex.
int k = indices.get(); // note that this increments the index buffer position
float v0x = vertices.get(k * 3);
float v0y = vertices.get(k * 3 + 1);
float v0z = vertices.get(k * 3 + 2);
// Starting with the second position in the index buffer, get subsequent indices and vertices.
for (int n = indices.position(); n < indices.limit() - 1; n++)
{
Intersection intersection;
int i = indices.get(n) * 3;
int j = indices.get(n + 1) * 3;
// The triangle intersect method detects front and back face intersections so there's no reason to
// order the vertices.
intersection = intersect(line,
v0x, v0y, v0z,
vertices.get(i), vertices.get(i + 1), vertices.get(i + 2),
vertices.get(j), vertices.get(j + 1), vertices.get(j + 2));
if (intersection != null)
{
if (intersections == null)
intersections = new ArrayList<Intersection>();
intersections.add(intersection);
}
}
return intersections;
}
/**
* Compute the intersections of a line with a triangle fan.
*
* @param line the line to intersect.
* @param vertices the tri-fan vertices.
* @param indices the indices forming the tri-fan.
*
* @return the list of intersections with the line and the triangle fan, or null if there are no intersections.
*
* @throws IllegalArgumentException if the line, vertex array or index buffer is null.
*/
public static List<Intersection> intersectTriFan(final Line line, Vec4[] vertices, IntBuffer indices)
{
if (line == null)
{
String msg = Logging.getMessage("nullValue.LineIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (vertices == null)
{
String msg = Logging.getMessage("nullValue.ArrayIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (indices == null)
{
String msg = Logging.getMessage("nullValue.BufferIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
List<Intersection> intersections = null;
Vec4 v0 = vertices[0];
for (int n = indices.position() + 1; n < indices.limit() - 1; n++)
{
Intersection intersection;
Vec4 v1 = vertices[indices.get(n)];
Vec4 v2 = vertices[indices.get(n + 1)];
// The triangle intersect method detects front and back face intersections so there's no reason to
// order the vertices.
intersection = intersect(line, v0, v1, v2);
if (intersection != null)
{
if (intersections == null)
intersections = new ArrayList<Intersection>();
intersections.add(intersection);
}
}
return intersections;
}
/**
* Compute the intersections of a line with a collection of triangles.
*
* @param line the line to intersect.
* @param vertices the triangles, arranged in a buffer as GL_TRIANGLES (9 floats per triangle).
*
* @return the list of intersections with the line and the triangles, or null if there are no intersections.
*
* @throws IllegalArgumentException if the line or vertex buffer is null.
*/
public static List<Intersection> intersectTriangles(final Line line, FloatBuffer vertices)
{
if (line == null)
{
String msg = Logging.getMessage("nullValue.LineIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (vertices == null)
{
String msg = Logging.getMessage("nullValue.BufferIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
List<Intersection> intersections = null;
vertices.rewind();
while (vertices.limit() - vertices.position() >= 9)
{
Intersection intersection = intersect(line,
vertices.get(), vertices.get(), vertices.get(),
vertices.get(), vertices.get(), vertices.get(),
vertices.get(), vertices.get(), vertices.get());
if (intersection != null)
{
if (intersections == null)
intersections = new ArrayList<Intersection>();
intersections.add(intersection);
}
}
return intersections;
}
/**
* Compute the intersections of a line with a collection of triangles.
*
* @param line the line to intersect.
* @param vertices the triangles, arranged in a buffer as GL_TRIANGLES (9 floats per triangle).
* @param indices the indices forming the triangles.
*
* @return the list of intersections with the line and the triangle fan, or null if there are no intersections.
*
* @throws IllegalArgumentException if the line, vertex buffer or index buffer is null.
*/
public static List<Intersection> intersectTriangles(final Line line, FloatBuffer vertices, IntBuffer indices)
{
if (line == null)
{
String msg = Logging.getMessage("nullValue.LineIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (vertices == null || indices == null)
{
String msg = Logging.getMessage("nullValue.BufferIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
List<Intersection> intersections = null;
for (int n = indices.position(); n < indices.limit(); n += 3)
{
Intersection intersection;
int i = indices.get(n) * 3;
int j = indices.get(n + 1) * 3;
int k = indices.get(n + 2) * 3;
intersection = intersect(line,
vertices.get(i), vertices.get(i + 1), vertices.get(i + 2),
vertices.get(j), vertices.get(j + 1), vertices.get(j + 2),
vertices.get(k), vertices.get(k + 1), vertices.get(k + 2));
if (intersection != null)
{
if (intersections == null)
intersections = new ArrayList<Intersection>();
intersections.add(intersection);
}
}
return intersections;
}
/**
* Compute the intersections of a line with a triangle collection.
*
* @param line the line to intersect.
* @param vertices the tri-fan vertices, in the order x, y, z, x, y, z, ...
* @param indices the indices forming the tri-fan.
* @param triangleType the type of triangle collection, either GL.GL_TRIANGLE_STRIP or GL.GL_TRIANGLE_FAN.
*
* @return the list of intersections with the line and the triangle fan, or null if there are no intersections.
*/
public static List<Intersection> intersectTriangleTypes(final Line line, FloatBuffer vertices, IntBuffer indices,
int triangleType)
{
if (triangleType == GL.GL_TRIANGLES)
return Triangle.intersectTriangles(line, vertices, indices);
else if (triangleType == GL.GL_TRIANGLE_STRIP)
return Triangle.intersectTriStrip(line, vertices, indices);
else if (triangleType == GL.GL_TRIANGLE_FAN)
return Triangle.intersectTriFan(line, vertices, indices);
return null;
}
/**
* Expands a buffer of indexed triangle vertices to a buffer of non-indexed triangle vertices.
*
* @param indices the triangle indices.
* @param inBuf the vertex buffer the indices refer to, in the order x, y, z, x, y, z, ...
* @param outBuf the buffer in which to place the expanded triangle vertices. The buffer must have a limit
* sufficient to hold the output vertices.
*
* @throws IllegalArgumentException if the index list or the input or output buffer is null, or if the output buffer
* size is insufficient.
*/
public static void expandTriangles(List<Integer> indices, FloatBuffer inBuf, FloatBuffer outBuf)
{
if (indices == null)
{
String msg = Logging.getMessage("nullValue.ListIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (inBuf == null || outBuf == null)
{
String msg = Logging.getMessage("nullValue.BufferIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
int nunTriangles = indices.size() / 3;
if (nunTriangles * 3 * 3 > outBuf.limit() - outBuf.position())
{
String msg = Logging.getMessage("generic.BufferSize", outBuf.limit() - outBuf.position());
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
for (int i = 0; i < indices.size(); i += 3)
{
int k = indices.get(i) * 3;
outBuf.put(inBuf.get(k)).put(inBuf.get(k + 1)).put(inBuf.get(k + 2));
k = indices.get(i + 1) * 3;
outBuf.put(inBuf.get(k)).put(inBuf.get(k + 1)).put(inBuf.get(k + 2));
k = indices.get(i + 2) * 3;
outBuf.put(inBuf.get(k)).put(inBuf.get(k + 1)).put(inBuf.get(k + 2));
}
}
/**
* Expands a buffer of indexed triangle fan vertices to a buffer of non-indexed general-triangle vertices.
*
* @param indices the triangle indices.
* @param inBuf the vertex buffer the indices refer to, in the order x, y, z, x, y, z, ...
* @param outBuf the buffer in which to place the expanded triangle vertices. The buffer must have a limit
* sufficient to hold the output vertices.
*
* @throws IllegalArgumentException if the index list or the input or output buffer is null, or if the output buffer
* size is insufficient.
*/
public static void expandTriangleFan(List<Integer> indices, FloatBuffer inBuf, FloatBuffer outBuf)
{
if (indices == null)
{
String msg = Logging.getMessage("nullValue.ListIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (inBuf == null || outBuf == null)
{
String msg = Logging.getMessage("nullValue.BufferIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
int nunTriangles = indices.size() - 2;
if (nunTriangles * 3 * 3 > outBuf.limit() - outBuf.position())
{
String msg = Logging.getMessage("generic.BufferSize", outBuf.limit() - outBuf.position());
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
int k = indices.get(0) * 3;
float v0x = inBuf.get(k);
float v0y = inBuf.get(k + 1);
float v0z = inBuf.get(k + 2);
for (int i = 1; i < indices.size() - 1; i++)
{
outBuf.put(v0x).put(v0y).put(v0z);
k = indices.get(i) * 3;
outBuf.put(inBuf.get(k)).put(inBuf.get(k + 1)).put(inBuf.get(k + 2));
k = indices.get(i + 1) * 3;
outBuf.put(inBuf.get(k)).put(inBuf.get(k + 1)).put(inBuf.get(k + 2));
}
}
/**
* Expands a buffer of indexed triangle strip vertices to a buffer of non-indexed general-triangle vertices.
*
* @param indices the triangle indices.
* @param inBuf the vertex buffer the indices refer to, in the order x, y, z, x, y, z, ...
* @param outBuf the buffer in which to place the expanded triangle vertices. The buffer must have a limit
* sufficient to hold the output vertices.
*
* @throws IllegalArgumentException if the index list or the input or output buffer is null, or if the output buffer
* size is insufficient.
*/
public static void expandTriangleStrip(List<Integer> indices, FloatBuffer inBuf, FloatBuffer outBuf)
{
if (indices == null)
{
String msg = Logging.getMessage("nullValue.ListIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (inBuf == null || outBuf == null)
{
String msg = Logging.getMessage("nullValue.BufferIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
int nunTriangles = indices.size() - 2;
if (nunTriangles * 3 * 3 > outBuf.limit() - outBuf.position())
{
String msg = Logging.getMessage("generic.BufferSize", outBuf.limit() - outBuf.position());
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
for (int i = 2; i < indices.size(); i++)
{
int k = indices.get(i - 2) * 3;
outBuf.put(inBuf.get(k)).put(inBuf.get(k + 1)).put(inBuf.get(k + 2));
k = indices.get(i % 2 == 0 ? i : i - 1) * 3;
outBuf.put(inBuf.get(k)).put(inBuf.get(k + 1)).put(inBuf.get(k + 2));
k = indices.get(i % 2 == 0 ? i - 1 : i) * 3;
outBuf.put(inBuf.get(k)).put(inBuf.get(k + 1)).put(inBuf.get(k + 2));
}
}
public static void expandTriangles(List<Integer> indices, IntBuffer outBuf)
{
if (indices == null)
{
String msg = Logging.getMessage("nullValue.ListIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (outBuf == null)
{
String msg = Logging.getMessage("nullValue.BufferIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
int numTriangles = indices.size() / 3;
if (numTriangles * 3 > outBuf.limit() - outBuf.position())
{
String msg = Logging.getMessage("generic.BufferSize", outBuf.limit() - outBuf.position());
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
for (int i = 0; i < indices.size(); i++)
{
outBuf.put(indices.get(i));
}
}
public static void expandTriangleFan(List<Integer> indices, IntBuffer outBuf)
{
if (indices == null)
{
String msg = Logging.getMessage("nullValue.ListIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (outBuf == null)
{
String msg = Logging.getMessage("nullValue.BufferIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
int nunTriangles = indices.size() - 2;
if (nunTriangles * 3 > outBuf.limit() - outBuf.position())
{
String msg = Logging.getMessage("generic.BufferSize", outBuf.limit() - outBuf.position());
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
int k0 = indices.get(0);
for (int i = 1; i < indices.size() - 1; i++)
{
outBuf.put(k0);
outBuf.put(indices.get(i));
outBuf.put(indices.get(i + 1));
}
}
public static void expandTriangleStrip(List<Integer> indices, IntBuffer outBuf)
{
if (indices == null)
{
String msg = Logging.getMessage("nullValue.ListIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (outBuf == null)
{
String msg = Logging.getMessage("nullValue.BufferIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
int nunTriangles = indices.size() - 2;
if (nunTriangles * 3 > outBuf.limit() - outBuf.position())
{
String msg = Logging.getMessage("generic.BufferSize", outBuf.limit() - outBuf.position());
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
for (int i = 2; i < indices.size(); i++)
{
outBuf.put(indices.get(i - 2));
outBuf.put(indices.get(i % 2 == 0 ? i - 1 : i));
outBuf.put(indices.get(i % 2 == 0 ? i : i - 1));
}
}
/**
* Defines a line segment representing the intersection of a line with and in the plane of a triangle. Used only
* within {@link #intersectTriangles}.
*/
protected static class TriangleIntersection
{
public Vec4 p0; // the first point of the line
public Vec4 p1; // the second point of the line
public double s0; // the distance along the line to the first intersection with the triangle
public double s1; // the distance along the line to the second intersection with the triangle
}
/**
* Intersects two triangles and returns their intersection vertices.
*
* @param v the Cartesian coordinates of the first triangle.
* @param u the Cartesian coordinates of the second triangle.
* @param intersectionVertices a pre-allocated two-element array in which the intersection vertices, if any, are
* returned.
*
* @return -1 if there is no intersection, 1 if there is an intersection, or 0 if the triangles are co-planar.
*/
public static int intersectTriangles(Vec4[] v, Vec4[] u, Vec4[] intersectionVertices)
{
// Taken from http://jgt.akpeters.com/papers/Moller97/tritri.html#ISECTLINE
// Compute plane equation of first triangle: n1 * x + d1 = 0.
double e1x = v[1].x - v[0].x;
double e1y = v[1].y - v[0].y;
double e1z = v[1].z - v[0].z;
double e2x = v[2].x - v[0].x;
double e2y = v[2].y - v[0].y;
double e2z = v[2].z - v[0].z;
Vec4 n1 = new Vec4(e1y * e2z - e1z * e2y, e1z * e2x - e1x * e2z, e1x * e2y - e1y * e2x);
double d1 = -n1.dot3(v[0]);
// Evaluate second triangle with plane equation 1 to determine signed distances to the plane.
double du0 = n1.dot3(u[0]) + d1;
double du1 = n1.dot3(u[1]) + d1;
double du2 = n1.dot3(u[2]) + d1;
// Coplanarity robustness check.
if (Math.abs(du0) < EPSILON)
du0 = 0;
if (Math.abs(du1) < EPSILON)
du1 = 0;
if (Math.abs(du2) < EPSILON)
du2 = 0;
double du0du1 = du0 * du1;
double du0du2 = du0 * du2;
if (du0du1 > 0 && du0du2 > 0) // same sign on all of them + != 0 ==> no intersection
return -1;
// Compute plane equation of second triangle: n2 * x + d2 = 0
e1x = u[1].x - u[0].x;
e1y = u[1].y - u[0].y;
e1z = u[1].z - u[0].z;
e2x = u[2].x - u[0].x;
e2y = u[2].y - u[0].y;
e2z = u[2].z - u[0].z;
Vec4 n2 = new Vec4(e1y * e2z - e1z * e2y, e1z * e2x - e1x * e2z, e1x * e2y - e1y * e2x);
double d2 = -n2.dot3(u[0]);
// Evaluate first triangle with plane equation 2 to determine signed distances to the plane.
double dv0 = n2.dot3(v[0]) + d2;
double dv1 = n2.dot3(v[1]) + d2;
double dv2 = n2.dot3(v[2]) + d2;
// Coplanarity robustness check.
if (Math.abs(dv0) < EPSILON)
dv0 = 0;
if (Math.abs(dv1) < EPSILON)
dv1 = 0;
if (Math.abs(dv2) < EPSILON)
dv2 = 0;
double dv0dv1 = dv0 * dv1;
double dv0dv2 = dv0 * dv2;
if (dv0dv1 > 0 && dv0dv2 > 0) // same sign on all of them + != 0 ==> no intersection
return -1;
// Compute direction of intersection line.
Vec4 ld = n1.cross3(n2);
// Compute an index to the largest component of line direction.
double max = Math.abs(ld.x);
int index = 0;
double b = Math.abs(ld.y);
double c = Math.abs(ld.z);
if (b > max)
{
max = b;
index = 1;
}
if (c > max)
{
index = 2;
}
// This is the simplified projection onto the line of intersection.
double vp0 = v[0].x;
double vp1 = v[1].x;
double vp2 = v[2].x;
double up0 = u[0].x;
double up1 = u[1].x;
double up2 = u[2].x;
if (index == 1)
{
vp0 = v[0].y;
vp1 = v[1].y;
vp2 = v[2].y;
up0 = u[0].y;
up1 = u[1].y;
up2 = u[2].y;
}
else if (index == 2)
{
vp0 = v[0].z;
vp1 = v[1].z;
vp2 = v[2].z;
up0 = u[0].z;
up1 = u[1].z;
up2 = u[2].z;
}