forked from hharrison/java3d-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoundingBox.java
More file actions
1904 lines (1660 loc) · 54.8 KB
/
Copy pathBoundingBox.java
File metadata and controls
1904 lines (1660 loc) · 54.8 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 1996-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
package org.scijava.java3d;
import org.scijava.vecmath.Point3d;
import org.scijava.vecmath.Point4d;
import org.scijava.vecmath.Vector3d;
import org.scijava.vecmath.Vector4d;
/**
* This class defines an axis aligned bounding box which is used for
* bounding regions.
*
*/
public class BoundingBox extends Bounds {
/**
* The corner of the bounding box with the numerically smallest values.
*/
final Point3d lower;
/**
* The corner of the bounding box with the numerically largest values.
*/
final Point3d upper;
private static final double EPS = 1.0E-8;
/**
* Constructs and initializes a BoundingBox given min,max in x,y,z.
* @param lower the "small" corner
* @param upper the "large" corner
*/
public BoundingBox(Point3d lower, Point3d upper) {
boundId = BOUNDING_BOX;
this.lower = new Point3d(lower);
this.upper = new Point3d(upper);
updateBoundsStates();
}
/**
* Constructs and initializes a 2X bounding box about the origin. The lower
* corner is initialized to (-1.0d, -1.0d, -1.0d) and the upper corner is
* initialized to (1.0d, 1.0d, 1.0d).
*/
public BoundingBox() {
boundId = BOUNDING_BOX;
lower = new Point3d(-1.0d, -1.0d, -1.0d);
upper = new Point3d( 1.0d, 1.0d, 1.0d);
boundsIsEmpty = false;
boundsIsInfinite = false;
}
/**
* Constructs a BoundingBox from a bounding object.
* @param boundsObject a bounds object
*/
public BoundingBox(Bounds boundsObject) {
boundId = BOUNDING_BOX;
lower = new Point3d();
upper = new Point3d();
if (boundsObject == null || boundsObject.boundsIsEmpty) {
setEmptyBounds();
return;
}
if (boundsObject.boundsIsInfinite) {
setInfiniteBounds();
return;
}
if( boundsObject.boundId == BOUNDING_BOX){
BoundingBox box = (BoundingBox)boundsObject;
lower.set(box.lower);
upper.set(box.upper);
}
else if( boundsObject.boundId == BOUNDING_SPHERE ) {
BoundingSphere sphere = (BoundingSphere)boundsObject;
lower.set(sphere.center.x - sphere.radius,
sphere.center.y - sphere.radius,
sphere.center.z - sphere.radius);
upper.set(sphere.center.x + sphere.radius,
sphere.center.y + sphere.radius,
sphere.center.z + sphere.radius);
}
else if(boundsObject.boundId == BOUNDING_POLYTOPE) {
BoundingPolytope polytope = (BoundingPolytope)boundsObject;
if( polytope.nVerts < 1 ) { // handle degenerate case
lower.set(-1.0d, -1.0d, -1.0d);
upper.set( 1.0d, 1.0d, 1.0d);
} else {
lower.set(polytope.verts[0]);
upper.set(polytope.verts[0]);
for(int i=1;i<polytope.nVerts;i++) {
if( polytope.verts[i].x < lower.x )
lower.x = polytope.verts[i].x;
if( polytope.verts[i].y < lower.y )
lower.y = polytope.verts[i].y;
if( polytope.verts[i].z < lower.z )
lower.z = polytope.verts[i].z;
if( polytope.verts[i].x > upper.x )
upper.x = polytope.verts[i].x;
if( polytope.verts[i].y > upper.y )
upper.y = polytope.verts[i].y;
if( polytope.verts[i].z > upper.z )
upper.z = polytope.verts[i].z;
}
}
} else {
throw new IllegalArgumentException(J3dI18N.getString("BoundingBox0"));
}
updateBoundsStates();
}
/**
* Constructs a BoundingBox from an array of bounding objects.
* @param bounds an array of bounding objects
*/
public BoundingBox(Bounds[] bounds) {
boundId = BOUNDING_BOX;
upper = new Point3d();
lower = new Point3d();
if (bounds == null || bounds.length <= 0) {
setEmptyBounds();
return;
}
int i = 0;
// find first non empty bounds object
while ((i < bounds.length) && ((bounds[i] == null) || bounds[i].boundsIsEmpty)) {
i++;
}
if (i >= bounds.length) {
// all bounds objects were empty
setEmptyBounds();
return;
}
this.set(bounds[i++]);
if(boundsIsInfinite)
return;
for(;i<bounds.length;i++) {
if( bounds[i] == null ); // do nothing
else if( bounds[i].boundsIsEmpty); // do nothing
else if( bounds[i].boundsIsInfinite ) {
setInfiniteBounds();
return; // We're done.
}
else if(bounds[i].boundId == BOUNDING_BOX){
BoundingBox box = (BoundingBox)bounds[i];
if( lower.x > box.lower.x) lower.x = box.lower.x;
if( lower.y > box.lower.y) lower.y = box.lower.y;
if( lower.z > box.lower.z) lower.z = box.lower.z;
if( upper.x < box.upper.x) upper.x = box.upper.x;
if( upper.y < box.upper.y) upper.y = box.upper.y;
if( upper.z < box.upper.z) upper.z = box.upper.z;
}
else if(bounds[i].boundId == BOUNDING_SPHERE) {
BoundingSphere sphere = (BoundingSphere)bounds[i];
if( lower.x > (sphere.center.x - sphere.radius))
lower.x = sphere.center.x - sphere.radius;
if( lower.y > (sphere.center.y - sphere.radius))
lower.y = sphere.center.y - sphere.radius;
if( lower.z > (sphere.center.z - sphere.radius))
lower.z = sphere.center.z - sphere.radius;
if( upper.x < (sphere.center.x + sphere.radius))
upper.x = sphere.center.x + sphere.radius;
if( upper.y < (sphere.center.y + sphere.radius))
upper.y = sphere.center.y + sphere.radius;
if( upper.z < (sphere.center.z + sphere.radius))
upper.z = sphere.center.z + sphere.radius;
}
else if(bounds[i].boundId == BOUNDING_POLYTOPE) {
BoundingPolytope polytope = (BoundingPolytope)bounds[i];
for(i=0;i<polytope.nVerts;i++) { // XXXX: handle polytope with no verts
if( polytope.verts[i].x < lower.x )
lower.x = polytope.verts[i].x;
if( polytope.verts[i].y < lower.y )
lower.y = polytope.verts[i].y;
if( polytope.verts[i].z < lower.z )
lower.z = polytope.verts[i].z;
if( polytope.verts[i].x > upper.x )
upper.x = polytope.verts[i].x;
if( polytope.verts[i].y > upper.y )
upper.y = polytope.verts[i].y;
if( polytope.verts[i].z > upper.z )
upper.z = polytope.verts[i].z;
}
}
else {
throw new IllegalArgumentException(J3dI18N.getString("BoundingBox1"));
}
}
updateBoundsStates();
}
/**
* Gets the lower corner of this bounding box.
* @param p1 a Point to receive the lower corner of the bounding box
*/
public void getLower(Point3d p1) {
p1.set(lower);
}
/**
* Sets the lower corner of this bounding box.
* @param xmin minimum x value of bounding box
* @param ymin minimum y value of bounding box
* @param zmin minimum z value of bounding box
*/
public void setLower(double xmin, double ymin, double zmin) {
lower.set(xmin, ymin, zmin);
updateBoundsStates();
}
/**
* Sets the lower corner of this bounding box.
* @param p1 a Point defining the new lower corner of the bounding box
*/
public void setLower(Point3d p1) {
lower.set(p1);
updateBoundsStates();
}
/**
* Gets the upper corner of this bounding box.
* @param p1 a Point to receive the upper corner of the bounding box
*/
public void getUpper(Point3d p1) {
p1.set(upper);
}
/**
* Sets the upper corner of this bounding box.
* @param xmax max x value of bounding box
* @param ymax max y value of bounding box
* @param zmax max z value of bounding box
*/
public void setUpper(double xmax, double ymax, double zmax) {
upper.set(xmax, ymax, zmax);
updateBoundsStates();
}
/**
* Sets the upper corner of this bounding box.
* @param p1 a Point defining the new upper corner of the bounding box
*/
public void setUpper(Point3d p1) {
upper.set(p1);
updateBoundsStates();
}
/**
* Sets the the value of this BoundingBox
* @param boundsObject another bounds object
*/
@Override
public void set(Bounds boundsObject) {
int i;
if (boundsObject == null || boundsObject.boundsIsEmpty) {
setEmptyBounds();
return;
}
if (boundsObject.boundsIsInfinite) {
setInfiniteBounds();
return;
}
if( boundsObject.boundId == BOUNDING_BOX){
BoundingBox box = (BoundingBox)boundsObject;
lower.x = box.lower.x;
lower.y = box.lower.y;
lower.z = box.lower.z;
upper.x = box.upper.x;
upper.y = box.upper.y;
upper.z = box.upper.z;
} else if( boundsObject.boundId == BOUNDING_SPHERE ) {
BoundingSphere sphere = (BoundingSphere)boundsObject;
lower.x = sphere.center.x - sphere.radius;
lower.y = sphere.center.y - sphere.radius;
lower.z = sphere.center.z - sphere.radius;
upper.x = sphere.center.x + sphere.radius;
upper.y = sphere.center.y + sphere.radius;
upper.z = sphere.center.z + sphere.radius;
} else if(boundsObject.boundId == BOUNDING_POLYTOPE) {
BoundingPolytope polytope = (BoundingPolytope)boundsObject;
lower.x = upper.x = polytope.verts[0].x;
lower.y = upper.y = polytope.verts[0].y;
lower.z = upper.z = polytope.verts[0].z;
for(i=1;i<polytope.nVerts;i++) {
if( polytope.verts[i].x < lower.x ) lower.x = polytope.verts[i].x;
if( polytope.verts[i].y < lower.y ) lower.y = polytope.verts[i].y;
if( polytope.verts[i].z < lower.z ) lower.z = polytope.verts[i].z;
if( polytope.verts[i].x > upper.x ) upper.x = polytope.verts[i].x;
if( polytope.verts[i].y > upper.y ) upper.y = polytope.verts[i].y;
if( polytope.verts[i].z > upper.z ) upper.z = polytope.verts[i].z;
}
} else {
throw new IllegalArgumentException(J3dI18N.getString("BoundingBox0"));
}
updateBoundsStates();
}
/**
* Creates a copy of this bounding box.
* @return a new bounding box
*/
@Override
public Object clone() {
return new BoundingBox(this.lower, this.upper);
}
/**
* Indicates whether the specified <code>bounds</code> object is
* equal to this BoundingBox object. They are equal if the
* specified <code>bounds</code> object is an instance of
* BoundingBox and all of the data
* members of <code>bounds</code> are equal to the corresponding
* data members in this BoundingBox.
* @param bounds the object with which the comparison is made.
* @return true if this BoundingBox is equal to <code>bounds</code>;
* otherwise false
*
* @since Java 3D 1.2
*/
@Override
public boolean equals(Object bounds) {
try {
BoundingBox box = (BoundingBox)bounds;
return (lower.equals(box.lower) &&
upper.equals(box.upper));
}
catch (NullPointerException e) {
return false;
}
catch (ClassCastException e) {
return false;
}
}
/**
* Returns a hash code value for this BoundingBox object
* based on the data values in this object. Two different
* BoundingBox objects with identical data values (i.e.,
* BoundingBox.equals returns true) will return the same hash
* code value. Two BoundingBox objects with different data
* members may return the same hash code value, although this is
* not likely.
* @return a hash code value for this BoundingBox object.
*
* @since Java 3D 1.2
*/
@Override
public int hashCode() {
long bits = 1L;
bits = J3dHash.mixDoubleBits(bits, lower.x);
bits = J3dHash.mixDoubleBits(bits, lower.y);
bits = J3dHash.mixDoubleBits(bits, lower.z);
bits = J3dHash.mixDoubleBits(bits, upper.x);
bits = J3dHash.mixDoubleBits(bits, upper.y);
bits = J3dHash.mixDoubleBits(bits, upper.z);
return J3dHash.finish(bits);
}
/**
* Combines this bounding box with a bounding object so that the
* resulting bounding box encloses the original bounding box and the
* specified bounds object.
* @param boundsObject another bounds object
*/
@Override
public void combine(Bounds boundsObject) {
if((boundsObject == null) || (boundsObject.boundsIsEmpty)
|| (boundsIsInfinite))
return;
if((boundsIsEmpty) || (boundsObject.boundsIsInfinite)) {
this.set(boundsObject);
return;
}
if( boundsObject.boundId == BOUNDING_BOX){
BoundingBox box = (BoundingBox)boundsObject;
if( lower.x > box.lower.x) lower.x = box.lower.x;
if( lower.y > box.lower.y) lower.y = box.lower.y;
if( lower.z > box.lower.z) lower.z = box.lower.z;
if( upper.x < box.upper.x) upper.x = box.upper.x;
if( upper.y < box.upper.y) upper.y = box.upper.y;
if( upper.z < box.upper.z) upper.z = box.upper.z;
}
else if( boundsObject.boundId == BOUNDING_SPHERE ) {
BoundingSphere sphere = (BoundingSphere)boundsObject;
if( lower.x > (sphere.center.x - sphere.radius))
lower.x = sphere.center.x - sphere.radius;
if( lower.y > (sphere.center.y - sphere.radius))
lower.y = sphere.center.y - sphere.radius;
if( lower.z > (sphere.center.z - sphere.radius))
lower.z = sphere.center.z - sphere.radius;
if( upper.x < (sphere.center.x + sphere.radius))
upper.x = sphere.center.x + sphere.radius;
if( upper.y < (sphere.center.y + sphere.radius))
upper.y = sphere.center.y + sphere.radius;
if( upper.z < (sphere.center.z + sphere.radius))
upper.z = sphere.center.z + sphere.radius;
}
else if(boundsObject.boundId == BOUNDING_POLYTOPE) {
BoundingPolytope polytope = (BoundingPolytope)boundsObject;
int i;
for(i=1;i<polytope.nVerts;i++) {
if( polytope.verts[i].x < lower.x ) lower.x = polytope.verts[i].x;
if( polytope.verts[i].y < lower.y ) lower.y = polytope.verts[i].y;
if( polytope.verts[i].z < lower.z ) lower.z = polytope.verts[i].z;
if( polytope.verts[i].x > upper.x ) upper.x = polytope.verts[i].x;
if( polytope.verts[i].y > upper.y ) upper.y = polytope.verts[i].y;
if( polytope.verts[i].z > upper.z ) upper.z = polytope.verts[i].z;
}
} else {
throw new IllegalArgumentException(J3dI18N.getString("BoundingBox3"));
}
updateBoundsStates();
}
/**
* Combines this bounding box with an array of bounding objects
* so that the resulting bounding box encloses the original bounding
* box and the array of bounding objects.
* @param bounds an array of bounds objects
*/
@Override
public void combine(Bounds[] bounds) {
int i=0;
if( (bounds == null) || (bounds.length <= 0)
|| (boundsIsInfinite))
return;
// find first non empty bounds object
while( (i<bounds.length) && ((bounds[i]==null) || bounds[i].boundsIsEmpty)) {
i++;
}
if( i >= bounds.length)
return; // no non empty bounds so do not modify current bounds
if(boundsIsEmpty)
this.set(bounds[i++]);
if(boundsIsInfinite)
return;
for(;i<bounds.length;i++) {
if( bounds[i] == null ); // do nothing
else if( bounds[i].boundsIsEmpty); // do nothing
else if( bounds[i].boundsIsInfinite ) {
lower.x = lower.y = lower.z = Double.NEGATIVE_INFINITY;
upper.x = upper.y = upper.z = Double.POSITIVE_INFINITY;
break; // We're done.
}
else if( bounds[i].boundId == BOUNDING_BOX){
BoundingBox box = (BoundingBox)bounds[i];
if( lower.x > box.lower.x) lower.x = box.lower.x;
if( lower.y > box.lower.y) lower.y = box.lower.y;
if( lower.z > box.lower.z) lower.z = box.lower.z;
if( upper.x < box.upper.x) upper.x = box.upper.x;
if( upper.y < box.upper.y) upper.y = box.upper.y;
if( upper.z < box.upper.z) upper.z = box.upper.z;
}
else if( bounds[i].boundId == BOUNDING_SPHERE ) {
BoundingSphere sphere = (BoundingSphere)bounds[i];
if( lower.x > (sphere.center.x - sphere.radius))
lower.x = sphere.center.x - sphere.radius;
if( lower.y > (sphere.center.y - sphere.radius))
lower.y = sphere.center.y - sphere.radius;
if( lower.z > (sphere.center.z - sphere.radius))
lower.z = sphere.center.z - sphere.radius;
if( upper.x < (sphere.center.x + sphere.radius))
upper.x = sphere.center.x + sphere.radius;
if( upper.y < (sphere.center.y + sphere.radius))
upper.y = sphere.center.y + sphere.radius;
if( upper.z < (sphere.center.z + sphere.radius))
upper.z = sphere.center.z + sphere.radius;
}
else if(bounds[i].boundId == BOUNDING_POLYTOPE) {
BoundingPolytope polytope = (BoundingPolytope)bounds[i];
for(i=1;i<polytope.nVerts;i++) {
if( polytope.verts[i].x < lower.x ) lower.x = polytope.verts[i].x;
if( polytope.verts[i].y < lower.y ) lower.y = polytope.verts[i].y;
if( polytope.verts[i].z < lower.z ) lower.z = polytope.verts[i].z;
if( polytope.verts[i].x > upper.x ) upper.x = polytope.verts[i].x;
if( polytope.verts[i].y > upper.y ) upper.y = polytope.verts[i].y;
if( polytope.verts[i].z > upper.z ) upper.z = polytope.verts[i].z;
}
} else {
throw new IllegalArgumentException(J3dI18N.getString("BoundingBox4"));
}
}
updateBoundsStates();
}
/**
* Combines this bounding box with a point so that the resulting
* bounding box encloses the original bounding box and the point.
* @param point a 3d point in space
*/
@Override
public void combine(Point3d point) {
if( boundsIsInfinite) {
return;
}
if( boundsIsEmpty) {
upper.x = lower.x = point.x;
upper.y = lower.y = point.y;
upper.z = lower.z = point.z;
} else {
if( point.x > upper.x) upper.x = point.x;
if( point.y > upper.y) upper.y = point.y;
if( point.z > upper.z) upper.z = point.z;
if( point.x < lower.x) lower.x = point.x;
if( point.y < lower.y) lower.y = point.y;
if( point.z < lower.z) lower.z = point.z;
}
updateBoundsStates();
}
/**
* Combines this bounding box with an array of points so that the
* resulting bounding box encloses the original bounding box and the
* array of points.
* @param points an array of 3d points in space
*/
@Override
public void combine(Point3d[] points) {
int i;
if( boundsIsInfinite) {
return;
}
if( boundsIsEmpty) {
this.setUpper(points[0]);
this.setLower(points[0]);
}
for(i=0;i<points.length;i++) {
if( points[i].x > upper.x) upper.x = points[i].x;
if( points[i].y > upper.y) upper.y = points[i].y;
if( points[i].z > upper.z) upper.z = points[i].z;
if( points[i].x < lower.x) lower.x = points[i].x;
if( points[i].y < lower.y) lower.y = points[i].y;
if( points[i].z < lower.z) lower.z = points[i].z;
}
updateBoundsStates();
}
/**
* Modifies the bounding box so that it bounds the volume
* generated by transforming the given bounding object.
* @param boundsObject the bounding object to be transformed
* @param matrix a transformation matrix
*/
@Override
public void transform( Bounds boundsObject, Transform3D matrix) {
if (boundsObject == null || boundsObject.boundsIsEmpty) {
setEmptyBounds();
return;
}
if (boundsObject.boundsIsInfinite) {
setInfiniteBounds();
return;
}
if(boundsObject.boundId == BOUNDING_BOX){
this.set(boundsObject);
this.transform(matrix);
}
else if(boundsObject.boundId == BOUNDING_SPHERE) {
BoundingSphere tmpSphere = new BoundingSphere(boundsObject);
tmpSphere.transform(matrix);
this.set(tmpSphere);
}
else if(boundsObject.boundId == BOUNDING_POLYTOPE) {
BoundingPolytope tmpPolytope = new BoundingPolytope(boundsObject);
tmpPolytope.transform(matrix);
this.set(tmpPolytope);
}
else {
throw new IllegalArgumentException(J3dI18N.getString("BoundingBox5"));
}
}
/**
* Transforms this bounding box by the given matrix.
* @param matrix a transformation matrix
*/
@Override
public void transform(Transform3D matrix) {
if (boundsIsInfinite)
return;
Point3d tmpP3d = new Point3d();
double ux, uy, uz, lx, ly, lz;
ux = upper.x; uy = upper.y; uz = upper.z;
lx = lower.x; ly = lower.y; lz = lower.z;
tmpP3d.set(ux, uy, uz);
matrix.transform( tmpP3d );
upper.x = tmpP3d.x;
upper.y = tmpP3d.y;
upper.z = tmpP3d.z;
lower.x = tmpP3d.x;
lower.y = tmpP3d.y;
lower.z = tmpP3d.z;
tmpP3d.set(lx, uy, uz);
matrix.transform( tmpP3d );
if ( tmpP3d.x > upper.x ) upper.x = tmpP3d.x;
if ( tmpP3d.y > upper.y ) upper.y = tmpP3d.y;
if ( tmpP3d.z > upper.z ) upper.z = tmpP3d.z;
if ( tmpP3d.x < lower.x ) lower.x = tmpP3d.x;
if ( tmpP3d.y < lower.y ) lower.y = tmpP3d.y;
if ( tmpP3d.z < lower.z ) lower.z = tmpP3d.z;
tmpP3d.set(lx, ly, uz);
matrix.transform( tmpP3d );
if ( tmpP3d.x > upper.x ) upper.x = tmpP3d.x;
if ( tmpP3d.y > upper.y ) upper.y = tmpP3d.y;
if ( tmpP3d.z > upper.z ) upper.z = tmpP3d.z;
if ( tmpP3d.x < lower.x ) lower.x = tmpP3d.x;
if ( tmpP3d.y < lower.y ) lower.y = tmpP3d.y;
if ( tmpP3d.z < lower.z ) lower.z = tmpP3d.z;
tmpP3d.set(ux, ly, uz);
matrix.transform( tmpP3d );
if ( tmpP3d.x > upper.x ) upper.x = tmpP3d.x;
if ( tmpP3d.y > upper.y ) upper.y = tmpP3d.y;
if ( tmpP3d.z > upper.z ) upper.z = tmpP3d.z;
if ( tmpP3d.x < lower.x ) lower.x = tmpP3d.x;
if ( tmpP3d.y < lower.y ) lower.y = tmpP3d.y;
if ( tmpP3d.z < lower.z ) lower.z = tmpP3d.z;
tmpP3d.set(lx, uy, lz);
matrix.transform( tmpP3d );
if ( tmpP3d.x > upper.x ) upper.x = tmpP3d.x;
if ( tmpP3d.y > upper.y ) upper.y = tmpP3d.y;
if ( tmpP3d.z > upper.z ) upper.z = tmpP3d.z;
if ( tmpP3d.x < lower.x ) lower.x = tmpP3d.x;
if ( tmpP3d.y < lower.y ) lower.y = tmpP3d.y;
if ( tmpP3d.z < lower.z ) lower.z = tmpP3d.z;
tmpP3d.set(ux, uy, lz);
matrix.transform( tmpP3d );
if ( tmpP3d.x > upper.x ) upper.x = tmpP3d.x;
if ( tmpP3d.y > upper.y ) upper.y = tmpP3d.y;
if ( tmpP3d.z > upper.z ) upper.z = tmpP3d.z;
if ( tmpP3d.x < lower.x ) lower.x = tmpP3d.x;
if ( tmpP3d.y < lower.y ) lower.y = tmpP3d.y;
if ( tmpP3d.z < lower.z ) lower.z = tmpP3d.z;
tmpP3d.set(lx, ly, lz);
matrix.transform( tmpP3d );
if ( tmpP3d.x > upper.x ) upper.x = tmpP3d.x;
if ( tmpP3d.y > upper.y ) upper.y = tmpP3d.y;
if ( tmpP3d.z > upper.z ) upper.z = tmpP3d.z;
if ( tmpP3d.x < lower.x ) lower.x = tmpP3d.x;
if ( tmpP3d.y < lower.y ) lower.y = tmpP3d.y;
if ( tmpP3d.z < lower.z ) lower.z = tmpP3d.z;
tmpP3d.set(ux, ly, lz);
matrix.transform( tmpP3d );
if ( tmpP3d.x > upper.x ) upper.x = tmpP3d.x;
if ( tmpP3d.y > upper.y ) upper.y = tmpP3d.y;
if ( tmpP3d.z > upper.z ) upper.z = tmpP3d.z;
if ( tmpP3d.x < lower.x ) lower.x = tmpP3d.x;
if ( tmpP3d.y < lower.y ) lower.y = tmpP3d.y;
if ( tmpP3d.z < lower.z ) lower.z = tmpP3d.z;
}
/**
* Test for intersection with a ray.
* @param origin the starting point of the ray
* @param direction the direction of the ray
* @param position3 a point defining the location of the pick w= distance to pick
* @return true or false indicating if an intersection occured
*/
@Override
boolean intersect(Point3d origin, Vector3d direction, Point4d position ) {
double t1,t2,tmp,tnear,tfar,invDir,invMag;
double dirx, diry, dirz;
/*
System.err.println("BoundingBox.intersect(p,d,p) called\n");
System.err.println("bounds = " + lower + " -> " + upper);
*/
if( boundsIsEmpty ) {
return false;
}
if( boundsIsInfinite ) {
position.x = origin.x;
position.y = origin.y;
position.z = origin.z;
position.w = 0.0;
return true;
}
double dirLen = direction.x*direction.x + direction.y*direction.y +
direction.z*direction.z;
// Handle zero length direction vector.
if(dirLen == 0.0)
return intersect(origin, position);
invMag = 1.0/Math.sqrt(dirLen);
dirx = direction.x*invMag;
diry = direction.y*invMag;
dirz = direction.z*invMag;
/*
System.err.println("dir = " + dirx + ", " + diry + ", " + dirz);
System.err.println("origin = " + origin);
*/
// initialize tnear and tfar to handle dir.? == 0 cases
tnear = -Double.MAX_VALUE;
tfar = Double.MAX_VALUE;
if(dirx == 0.0) {
//System.err.println("dirx == 0.0");
if (origin.x < lower.x || origin.x > upper.x ) {
//System.err.println( "parallel to x plane and outside");
return false;
}
} else {
invDir = 1.0/dirx;
t1 = (lower.x-origin.x)*invDir;
t2 = (upper.x-origin.x)*invDir;
//System.err.println("x t1 = " + t1 + " t2 = " + t2);
if( t1 > t2) {
tnear = t2;
tfar = t1;
}else {
tnear = t1;
tfar = t2;
}
if( tfar < 0.0 ) {
//System.err.println( "x failed: tnear="+tnear+" tfar="+tfar);
return false;
}
//System.err.println("x tnear = " + tnear + " tfar = " + tfar);
}
// y
if (diry == 0.0) {
//System.err.println("diry == 0.0");
if( origin.y < lower.y || origin.y > upper.y ){
//System.err.println( "parallel to y plane and outside");
return false;
}
} else {
invDir = 1.0/diry;
//System.err.println("invDir = " + invDir);
t1 = (lower.y-origin.y)*invDir;
t2 = (upper.y-origin.y)*invDir;
if( t1 > t2) {
tmp = t1;
t1 = t2;
t2 = tmp;
}
//System.err.println("y t1 = " + t1 + " t2 = " + t2);
if( t1 > tnear) tnear = t1;
if( t2 < tfar ) tfar = t2;
if( (tfar < 0.0) || (tnear > tfar)){
//System.err.println( "y failed: tnear="+tnear+" tfar="+tfar);
return false;
}
//System.err.println("y tnear = " + tnear + " tfar = " + tfar);
}
// z
if (dirz == 0.0) {
//System.err.println("dirz == 0.0");
if( origin.z < lower.z || origin.z > upper.z ) {
//System.err.println( "parallel to z plane and outside");
return false;
}
} else {
invDir = 1.0/dirz;
t1 = (lower.z-origin.z)*invDir;
t2 = (upper.z-origin.z)*invDir;
if( t1 > t2) {
tmp = t1;
t1 = t2;
t2 = tmp;
}
//System.err.println("z t1 = " + t1 + " t2 = " + t2);
if( t1 > tnear) tnear = t1;
if( t2 < tfar ) tfar = t2;
if( (tfar < 0.0) || (tnear > tfar)){
//System.err.println( "z failed: tnear="+tnear+" tfar="+tfar);
return false;
}
//System.err.println("z tnear = " + tnear + " tfar = " + tfar);
}
if((tnear < 0.0) && (tfar >= 0.0)) {
// origin is inside the BBox.
position.x = origin.x + dirx*tfar;
position.y = origin.y + diry*tfar;
position.z = origin.z + dirz*tfar;
position.w = tfar;
}
else {
position.x = origin.x + dirx*tnear;
position.y = origin.y + diry*tnear;
position.z = origin.z + dirz*tnear;
position.w = tnear;
}
return true;
}
/**
* Test for intersection with a point.
* @param point the pick point
* @param position a point defining the location of the pick w= distance to pick
* @return true or false indicating if an intersection occured
*/
@Override
boolean intersect(Point3d point, Point4d position ) {
if( boundsIsEmpty ) {
return false;
}
if( boundsIsInfinite ) {
position.x = point.x;
position.y = point.y;
position.z = point.z;
position.w = 0.0;
return true;
}
if( point.x <= upper.x && point.x >= lower.x &&
point.y <= upper.y && point.y >= lower.y &&
point.z <= upper.z && point.z >= lower.z) {
position.x = point.x;
position.y = point.y;
position.z = point.z;
position.w = 0.0;
return true;
} else
return false;
}
/**
* Test for intersection with a segment.
* @param start a point defining the start of the line segment
* @param end a point defining the end of the line segment
* @param position a point defining the location of the pick w= distance to pick
* @return true or false indicating if an intersection occured
*/
@Override
boolean intersect( Point3d start, Point3d end, Point4d position ) {
double t1,t2,tmp,tnear,tfar,invDir,invMag;
double dirx, diry, dirz;
if( boundsIsEmpty ) {
return false;
}
if( boundsIsInfinite ) {
position.x = start.x;
position.y = start.y;
position.z = start.z;
position.w = 0.0;
return true;
}
dirx = end.x - start.x;
diry = end.y - start.y;
dirz = end.z - start.z;
double dirLen = dirx*dirx + diry*diry + dirz*dirz;
// Optimization : Handle zero length direction vector.
if(dirLen == 0.0)
return intersect(start, position);
dirLen = Math.sqrt(dirLen);
// System.err.println("dirLen is " + dirLen);
invMag = 1.0/dirLen;
dirx = dirx*invMag;
diry = diry*invMag;
dirz = dirz*invMag;
/*
System.err.println("dir = " + dir);
System.err.println("start = " + start);
System.err.println("lower = " + lower);
System.err.println("upper = " + upper);
*/
// initialize tnear and tfar to handle dir.? == 0 cases
tnear = -Double.MAX_VALUE;
tfar = Double.MAX_VALUE;
if(dirx == 0.0) {
//System.err.println("dirx == 0.0");
if (start.x < lower.x || start.x > upper.x ) {
//System.err.println( "parallel to x plane and outside");
return false;
}
} else {
invDir = 1.0/dirx;
t1 = (lower.x-start.x)*invDir;
t2 = (upper.x-start.x)*invDir;
//System.err.println("x t1 = " + t1 + " t2 = " + t2);
if( t1 > t2) {
tnear = t2;
tfar = t1;
}else {
tnear = t1;
tfar = t2;
}