forked from hharrison/java3d-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoundingSphere.java
More file actions
1737 lines (1519 loc) · 50.4 KB
/
Copy pathBoundingSphere.java
File metadata and controls
1737 lines (1519 loc) · 50.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 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 a spherical bounding region which is defined by a
* center point and a radius.
*/
public class BoundingSphere extends Bounds {
/**
* The center of the bounding sphere.
*/
final Point3d center;
/**
* The radius of the bounding sphere.
*/
double radius;
/**
* Constructs and initializes a BoundingSphere from a center and radius.
* @param center the center of the bounding sphere
* @param radius the radius of the bounding sphere
*/
public BoundingSphere(Point3d center, double radius) {
boundId = BOUNDING_SPHERE;
this.center = new Point3d(center);
this.radius = radius;
updateBoundsStates();
}
/**
* Constructs and initializes a BoundingSphere with radius = 1 at 0 0 0.
*/
public BoundingSphere() {
boundId = BOUNDING_SPHERE;
center = new Point3d();
radius = 1.0;
}
/**
* Constructs and initializes a BoundingSphere from a bounding object.
* @param boundsObject a bounds object
*/
public BoundingSphere(Bounds boundsObject) {
boundId = BOUNDING_SPHERE;
center = new Point3d();
if (boundsObject == null || boundsObject.boundsIsEmpty) {
setEmptyBounds();
return;
}
if (boundsObject.boundsIsInfinite) {
setInfiniteBounds();
return;
}
if( boundsObject.boundId == BOUNDING_BOX){
BoundingBox box = (BoundingBox)boundsObject;
center.x = (box.upper.x+box.lower.x)/2.0;
center.y = (box.upper.y+box.lower.y)/2.0;
center.z = (box.upper.z+box.lower.z)/2.0;
radius = 0.5*(Math.sqrt((box.upper.x-box.lower.x)*
(box.upper.x-box.lower.x)+
(box.upper.y-box.lower.y)*
(box.upper.y-box.lower.y)+
(box.upper.z-box.lower.z)*
(box.upper.z-box.lower.z)));
} else if (boundsObject.boundId == BOUNDING_SPHERE) {
BoundingSphere sphere = (BoundingSphere)boundsObject;
center.set(sphere.center);
radius = sphere.radius;
} else if(boundsObject.boundId == BOUNDING_POLYTOPE) {
BoundingPolytope polytope = (BoundingPolytope)boundsObject;
double t,dis,dis_sq,rad_sq,oldc_to_new_c;
center.x = polytope.centroid.x;
center.y = polytope.centroid.y;
center.z = polytope.centroid.z;
radius = Math.sqrt( (polytope.verts[0].x - center.x)*
(polytope.verts[0].x - center.x) +
(polytope.verts[0].y - center.y)*
(polytope.verts[0].y - center.y) +
(polytope.verts[0].z - center.z)*
(polytope.verts[0].z - center.z));
for (int i = 1; i < polytope.nVerts; i++) {
rad_sq = radius * radius;
dis_sq = (polytope.verts[i].x - center.x)*
(polytope.verts[i].x - center.x) +
(polytope.verts[i].y - center.y)*
(polytope.verts[i].y - center.y) +
(polytope.verts[i].z - center.z)*
(polytope.verts[i].z - center.z);
// change sphere so one side passes through the point
// and other passes through the old sphere
if( dis_sq > rad_sq) {
dis = Math.sqrt( dis_sq);
radius = (radius + dis)*.5;
oldc_to_new_c = dis - radius;
t = oldc_to_new_c/dis;
center.x = center.x + (polytope.verts[i].x - center.x)*t;
center.y = center.y + (polytope.verts[i].y - center.y)*t;
center.z = center.z + (polytope.verts[i].z - center.z)*t;
}
}
} else {
throw new IllegalArgumentException(J3dI18N.getString("BoundingSphere0"));
}
updateBoundsStates();
}
/**
* Constructs and initializes a BoundingSphere from an array of bounding
* objects.
* @param boundsObjects an array of bounds objects
*/
public BoundingSphere(Bounds[] boundsObjects) {
boundId = BOUNDING_SPHERE;
center = new Point3d();
if (boundsObjects == null || boundsObjects.length <= 0) {
setEmptyBounds();
return;
}
// find first non empty bounds object
int i = 0;
while( boundsObjects[i] == null && i < boundsObjects.length) {
i++;
}
if (i >= boundsObjects.length) { // all bounds objects were empty
setEmptyBounds();
return;
}
this.set(boundsObjects[i++]);
if(boundsIsInfinite)
return;
Point3d[] boxVerts = null;
for(;i<boundsObjects.length;i++) {
if( boundsObjects[i] == null ); // do nothing
else if( boundsObjects[i].boundsIsEmpty); // do nothing
else if( boundsObjects[i].boundsIsInfinite ) {
setInfiniteBounds();
return; // We're done.
}
else if( boundsObjects[i].boundId == BOUNDING_BOX){
BoundingBox b = (BoundingBox)boundsObjects[i];
if (boxVerts == null) {
boxVerts = new Point3d[8];
for (int j = 0; j < 8; j++)
boxVerts[j] = new Point3d();
}
boxVerts[0].set(b.lower.x, b.lower.y, b.lower.z );
boxVerts[1].set(b.lower.x, b.upper.y, b.lower.z );
boxVerts[2].set(b.upper.x, b.lower.y, b.lower.z );
boxVerts[3].set(b.upper.x, b.upper.y, b.lower.z );
boxVerts[4].set(b.lower.x, b.lower.y, b.upper.z );
boxVerts[5].set(b.lower.x, b.upper.y, b.upper.z );
boxVerts[6].set(b.upper.x, b.lower.y, b.upper.z );
boxVerts[7].set(b.upper.x, b.upper.y, b.upper.z );
this.combine(boxVerts);
}
else if( boundsObjects[i].boundId == BOUNDING_SPHERE ) {
double dis, t, d1;
BoundingSphere sphere = (BoundingSphere)boundsObjects[i];
dis = Math.sqrt( (center.x - sphere.center.x)*
(center.x - sphere.center.x) +
(center.y - sphere.center.y)*
(center.y - sphere.center.y) +
(center.z - sphere.center.z)*
(center.z - sphere.center.z) );
if( radius > sphere.radius) {
if( (dis+sphere.radius) > radius) {
d1 = .5*(radius-sphere.radius+dis);
t = d1/dis;
radius = d1+sphere.radius;
center.x = sphere.center.x + (center.x-sphere.center.x)*t;
center.y = sphere.center.y + (center.y-sphere.center.y)*t;
center.z = sphere.center.z + (center.z-sphere.center.z)*t;
}
}else {
if( (dis+radius) <= sphere.radius) {
center.x = sphere.center.x;
center.y = sphere.center.y;
center.z = sphere.center.z;
radius = sphere.radius;
}else {
d1 = .5*(sphere.radius-radius+dis);
t = d1/dis;
radius = d1+radius;
center.x = center.x + (sphere.center.x-center.x)*t;
center.y = center.y + (sphere.center.y-center.y)*t;
center.z = center.z + (sphere.center.z-center.z)*t;
}
}
}
else if(boundsObjects[i].boundId == BOUNDING_POLYTOPE) {
BoundingPolytope polytope = (BoundingPolytope)boundsObjects[i];
this.combine(polytope.verts);
}
else {
if( boundsObjects[i] != null )
throw new IllegalArgumentException(J3dI18N.getString("BoundingSphere0"));
}
}
updateBoundsStates();
}
/**
* Returns the radius of this bounding sphere as a double.
* @return the radius of the bounding sphere
*/
public double getRadius() {
return radius;
}
/**
* Sets the radius of this bounding sphere from a double.
* @param r the new radius for the bounding sphere
*/
public void setRadius(double r) {
radius = r;
updateBoundsStates();
}
/**
* Returns the position of this bounding sphere as a point.
* @param center a Point to receive the center of the bounding sphere
*/
@Override
public void getCenter(Point3d center) {
center.set(this.center);
}
/**
* Sets the position of this bounding sphere from a point.
* @param center a Point defining the new center of the bounding sphere
*/
public void setCenter(Point3d center) {
this.center.set(center);
updateBoundsStates();
}
/**
* Sets the value of this BoundingSphere.
* @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;
center.x = (box.upper.x + box.lower.x )/2.0;
center.y = (box.upper.y + box.lower.y )/2.0;
center.z = (box.upper.z + box.lower.z )/2.0;
radius = 0.5*Math.sqrt((box.upper.x-box.lower.x)*
(box.upper.x-box.lower.x)+
(box.upper.y-box.lower.y)*
(box.upper.y-box.lower.y)+
(box.upper.z-box.lower.z)*
(box.upper.z-box.lower.z));
} else if( boundsObject.boundId == BOUNDING_SPHERE ) {
BoundingSphere sphere = (BoundingSphere)boundsObject;
radius = sphere.radius;
center.x = sphere.center.x;
center.y = sphere.center.y;
center.z = sphere.center.z;
} else if(boundsObject.boundId == BOUNDING_POLYTOPE) {
BoundingPolytope polytope = (BoundingPolytope)boundsObject;
double t,dis,dis_sq,rad_sq,oldc_to_new_c;
center.x = polytope.centroid.x;
center.y = polytope.centroid.y;
center.z = polytope.centroid.z;
radius = Math.sqrt((polytope.verts[0].x - center.x)*
(polytope.verts[0].x - center.x) +
(polytope.verts[0].y - center.y)*
(polytope.verts[0].y - center.y) +
(polytope.verts[0].z - center.z)*
(polytope.verts[0].z - center.z));
for(i=1;i<polytope.nVerts;i++) {
rad_sq = radius * radius;
dis_sq = (polytope.verts[i].x - center.x)*
(polytope.verts[i].x - center.x) +
(polytope.verts[i].y - center.y)*
(polytope.verts[i].y - center.y) +
(polytope.verts[i].z - center.z)*
(polytope.verts[i].z - center.z);
// change sphere so one side passes through the point
// and other passes through the old sphere
if( dis_sq > rad_sq) { // point is outside sphere
dis = Math.sqrt( dis_sq);
radius = (radius + dis)*.5;
oldc_to_new_c = dis - radius;
t = oldc_to_new_c/dis;
center.x = center.x + (polytope.verts[i].x - center.x)*t;
center.y = center.y + (polytope.verts[i].y - center.y)*t;
center.z = center.z + (polytope.verts[i].z - center.z)*t;
}
}
} else {
throw new IllegalArgumentException(J3dI18N.getString("BoundingSphere2"));
}
updateBoundsStates();
}
/**
* Creates a copy of the bounding sphere.
* @return a BoundingSphere
*/
@Override
public Object clone() {
return new BoundingSphere(this.center, this.radius);
}
/**
* Indicates whether the specified <code>bounds</code> object is
* equal to this BoundingSphere object. They are equal if the
* specified <code>bounds</code> object is an instance of
* BoundingSphere and all of the data
* members of <code>bounds</code> are equal to the corresponding
* data members in this BoundingSphere.
* @param bounds the object with which the comparison is made.
* @return true if this BoundingSphere is equal to <code>bounds</code>;
* otherwise false
*
* @since Java 3D 1.2
*/
@Override
public boolean equals(Object bounds) {
try {
BoundingSphere sphere = (BoundingSphere)bounds;
return (center.equals(sphere.center) &&
radius == sphere.radius);
}
catch (NullPointerException e) {
return false;
}
catch (ClassCastException e) {
return false;
}
}
/**
* Returns a hash code value for this BoundingSphere object
* based on the data values in this object. Two different
* BoundingSphere objects with identical data values (i.e.,
* BoundingSphere.equals returns true) will return the same hash
* code value. Two BoundingSphere objects with different data
* members may return the same hash code value, although this is
* not likely.
* @return a hash code value for this BoundingSphere object.
*
* @since Java 3D 1.2
*/
@Override
public int hashCode() {
long bits = 1L;
bits = J3dHash.mixDoubleBits(bits, radius);
bits = J3dHash.mixDoubleBits(bits, center.x);
bits = J3dHash.mixDoubleBits(bits, center.y);
bits = J3dHash.mixDoubleBits(bits, center.z);
return J3dHash.finish(bits);
}
/**
* Combines this bounding sphere with a bounding object so that the
* resulting bounding sphere encloses the original bounding sphere and the
* given bounds object.
* @param boundsObject another bounds object
*/
@Override
public void combine(Bounds boundsObject) {
double t,dis,d1,u,l,x,y,z,oldc_to_new_c;
BoundingSphere sphere;
if((boundsObject == null) || (boundsObject.boundsIsEmpty)
|| (boundsIsInfinite))
return;
if((boundsIsEmpty) || (boundsObject.boundsIsInfinite)) {
this.set(boundsObject);
return;
}
if( boundsObject.boundId == BOUNDING_BOX){
BoundingBox b = (BoundingBox)boundsObject;
// start with point furthest from sphere
u = b.upper.x-center.x;
l = b.lower.x-center.x;
if( u*u > l*l)
x = b.upper.x;
else
x = b.lower.x;
u = b.upper.y-center.y;
l = b.lower.y-center.y;
if( u*u > l*l)
y = b.upper.y;
else
y = b.lower.y;
u = b.upper.z-center.z;
l = b.lower.z-center.z;
if( u*u > l*l)
z = b.upper.z;
else
z = b.lower.z;
dis = Math.sqrt( (x - center.x)*(x - center.x) +
(y - center.y)*(y - center.y) +
(z - center.z)*(z - center.z) );
if( dis > radius) {
radius = (dis + radius)*.5;
oldc_to_new_c = dis - radius;
center.x = (radius*center.x + oldc_to_new_c*x)/dis;
center.y = (radius*center.y + oldc_to_new_c*y)/dis;
center.z = (radius*center.z + oldc_to_new_c*z)/dis;
combinePoint( b.upper.x, b.upper.y, b.upper.z);
combinePoint( b.upper.x, b.upper.y, b.lower.z);
combinePoint( b.upper.x, b.lower.y, b.upper.z);
combinePoint( b.upper.x, b.lower.y, b.lower.z);
combinePoint( b.lower.x, b.upper.y, b.upper.z);
combinePoint( b.lower.x, b.upper.y, b.lower.z);
combinePoint( b.lower.x, b.lower.y, b.upper.z);
combinePoint( b.lower.x, b.lower.y, b.lower.z);
}
} else if( boundsObject.boundId == BOUNDING_SPHERE ) {
sphere = (BoundingSphere)boundsObject;
dis = Math.sqrt( (center.x - sphere.center.x)*
(center.x - sphere.center.x) +
(center.y - sphere.center.y)*
(center.y - sphere.center.y) +
(center.z - sphere.center.z)*
(center.z - sphere.center.z) );
if( radius > sphere.radius) {
if( (dis+sphere.radius) > radius) {
d1 = .5*(radius-sphere.radius+dis);
t = d1/dis;
radius = d1+sphere.radius;
center.x = sphere.center.x + (center.x-sphere.center.x)*t;
center.y = sphere.center.y + (center.y-sphere.center.y)*t;
center.z = sphere.center.z + (center.z-sphere.center.z)*t;
}
}else {
if( (dis+radius) <= sphere.radius) {
center.x = sphere.center.x;
center.y = sphere.center.y;
center.z = sphere.center.z;
radius = sphere.radius;
}else {
d1 = .5*(sphere.radius-radius+dis);
t = d1/dis;
radius = d1+radius;
center.x = center.x + (sphere.center.x-center.x)*t;
center.y = center.y + (sphere.center.y-center.y)*t;
center.z = center.z + (sphere.center.z-center.z)*t;
}
}
} else if(boundsObject.boundId == BOUNDING_POLYTOPE) {
BoundingPolytope polytope = (BoundingPolytope)boundsObject;
this.combine(polytope.verts);
} else {
throw new IllegalArgumentException(J3dI18N.getString("BoundingSphere3"));
}
updateBoundsStates();
}
private void combinePoint( double x, double y, double z) {
double dis,oldc_to_new_c;
dis = Math.sqrt( (x - center.x)*(x - center.x) +
(y - center.y)*(y - center.y) +
(z - center.z)*(z - center.z) );
if( dis > radius) {
radius = (dis + radius)*.5;
oldc_to_new_c = dis - radius;
center.x = (radius*center.x + oldc_to_new_c*x)/dis;
center.y = (radius*center.y + oldc_to_new_c*y)/dis;
center.z = (radius*center.z + oldc_to_new_c*z)/dis;
}
}
/**
* Combines this bounding sphere with an array of bounding objects so that the
* resulting bounding sphere encloses the original bounding sphere and the
* given array of bounds object.
* @param boundsObjects an array of bounds objects
*/
@Override
public void combine(Bounds[] boundsObjects) {
BoundingSphere sphere;
BoundingBox b;
BoundingPolytope polytope;
double t,dis,d1,u,l,x,y,z,oldc_to_new_c;
int i=0;
if((boundsObjects == null) || (boundsObjects.length <= 0)
|| (boundsIsInfinite))
return;
// find first non empty bounds object
while((i<boundsObjects.length) &&
((boundsObjects[i] == null) || boundsObjects[i].boundsIsEmpty)) {
i++;
}
if( i >= boundsObjects.length)
return; // no non empty bounds so do not modify current bounds
if( boundsIsEmpty)
this.set(boundsObjects[i++]);
if(boundsIsInfinite)
return;
for(;i<boundsObjects.length;i++) {
if( boundsObjects[i] == null ); // do nothing
else if( boundsObjects[i].boundsIsEmpty); // do nothing
else if( boundsObjects[i].boundsIsInfinite ) {
setInfiniteBounds();
return; // We're done.
} else if( boundsObjects[i].boundId == BOUNDING_BOX){
b = (BoundingBox)boundsObjects[i];
// start with point furthest from sphere
u = b.upper.x-center.x;
l = b.lower.x-center.x;
if( u*u > l*l)
x = b.upper.x;
else
x = b.lower.x;
u = b.upper.y-center.y;
l = b.lower.y-center.y;
if( u*u > l*l)
y = b.upper.y;
else
y = b.lower.y;
u = b.upper.z-center.z;
l = b.lower.z-center.z;
if( u*u > l*l)
z = b.upper.z;
else
z = b.lower.z;
dis = Math.sqrt( (x - center.x)*(x - center.x) +
(y - center.y)*(y - center.y) +
(z - center.z)*(z - center.z) );
if( dis > radius) {
radius = (dis + radius)*.5;
oldc_to_new_c = dis - radius;
center.x = (radius*center.x + oldc_to_new_c*x)/dis;
center.y = (radius*center.y + oldc_to_new_c*y)/dis;
center.z = (radius*center.z + oldc_to_new_c*z)/dis;
combinePoint( b.upper.x, b.upper.y, b.upper.z);
combinePoint( b.upper.x, b.upper.y, b.lower.z);
combinePoint( b.upper.x, b.lower.y, b.upper.z);
combinePoint( b.upper.x, b.lower.y, b.lower.z);
combinePoint( b.lower.x, b.upper.y, b.upper.z);
combinePoint( b.lower.x, b.upper.y, b.lower.z);
combinePoint( b.lower.x, b.lower.y, b.upper.z);
combinePoint( b.lower.x, b.lower.y, b.lower.z);
}
} else if( boundsObjects[i].boundId == BOUNDING_SPHERE ) {
sphere = (BoundingSphere)boundsObjects[i];
dis = Math.sqrt( (center.x - sphere.center.x)*
(center.x - sphere.center.x) +
(center.y - sphere.center.y)*
(center.y - sphere.center.y) +
(center.z - sphere.center.z)*
(center.z - sphere.center.z) );
if( radius > sphere.radius) {
if( (dis+sphere.radius) > radius) {
d1 = .5*(radius-sphere.radius+dis);
t = d1/dis;
radius = d1+sphere.radius;
center.x = sphere.center.x + (center.x-sphere.center.x)*t;
center.y = sphere.center.y + (center.y-sphere.center.y)*t;
center.z = sphere.center.z + (center.z-sphere.center.z)*t;
}
}else {
if( (dis+radius) <= sphere.radius) {
center.x = sphere.center.x;
center.y = sphere.center.y;
center.z = sphere.center.z;
radius = sphere.radius;
}else {
d1 = .5*(sphere.radius-radius+dis);
t = d1/dis;
radius = d1+radius;
center.x = center.x + (sphere.center.x-center.x)*t;
center.y = center.y + (sphere.center.y-center.y)*t;
center.z = center.z + (sphere.center.z-center.z)*t;
}
}
} else if(boundsObjects[i].boundId == BOUNDING_POLYTOPE) {
polytope = (BoundingPolytope)boundsObjects[i];
this.combine(polytope.verts);
} else {
throw new IllegalArgumentException(J3dI18N.getString("BoundingSphere4"));
}
}
updateBoundsStates();
}
/**
* Combines this bounding sphere with a point.
* @param point a 3D point in space
*/
@Override
public void combine(Point3d point) {
double dis,oldc_to_new_c;
if( boundsIsInfinite) {
return;
}
if( boundsIsEmpty) {
radius = 0.0;
center.x = point.x;
center.y = point.y;
center.z = point.z;
} else {
dis = Math.sqrt( (point.x - center.x)*(point.x - center.x) +
(point.y - center.y)*(point.y - center.y) +
(point.z - center.z)*(point.z - center.z) );
if( dis > radius) {
radius = (dis + radius)*.5;
oldc_to_new_c = dis - radius;
center.x = (radius*center.x + oldc_to_new_c*point.x)/dis;
center.y = (radius*center.y + oldc_to_new_c*point.y)/dis;
center.z = (radius*center.z + oldc_to_new_c*point.z)/dis;
}
}
updateBoundsStates();
}
/**
* Combines this bounding sphere with an array of points.
* @param points an array of 3D points in space
*/
@Override
public void combine(Point3d[] points) {
int i;
double dis,dis_sq,rad_sq,oldc_to_new_c;
if( boundsIsInfinite) {
return;
}
if( boundsIsEmpty ) {
center.x = points[0].x;
center.y = points[0].y;
center.z = points[0].z;
radius = 0.0;
}
for(i=0;i<points.length;i++) {
rad_sq = radius * radius;
dis_sq = (points[i].x - center.x)*(points[i].x - center.x) +
(points[i].y - center.y)*(points[i].y - center.y) +
(points[i].z - center.z)*(points[i].z - center.z);
// change sphere so one side passes through the point and
// other passes through the old sphere
if( dis_sq > rad_sq) {
dis = Math.sqrt( dis_sq);
radius = (radius + dis)*.5;
oldc_to_new_c = dis - radius;
center.x = (radius*center.x + oldc_to_new_c*points[i].x)/dis;
center.y = (radius*center.y + oldc_to_new_c*points[i].y)/dis;
center.z = (radius*center.z + oldc_to_new_c*points[i].z)/dis;
}
}
updateBoundsStates();
}
/**
* Modifies the bounding sphere 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) {
BoundingBox tmpBox = new BoundingBox(boundsObject);
tmpBox.transform(matrix);
this.set(tmpBox);
}
else if (boundsObject.boundId == BOUNDING_SPHERE) {
this.set(boundsObject);
this.transform(matrix);
}
else if (boundsObject.boundId == BOUNDING_POLYTOPE) {
BoundingPolytope tmpPolytope = new BoundingPolytope(boundsObject);
tmpPolytope.transform(matrix);
this.set(tmpPolytope);
} else {
throw new IllegalArgumentException(J3dI18N.getString("BoundingSphere5"));
}
}
/**
* Transforms this bounding sphere by the given matrix.
*/
@Override
public void transform( Transform3D trans) {
double scale;
if(boundsIsInfinite)
return;
trans.transform(center);
scale = trans.getDistanceScale();
radius = radius * scale;
if (Double.isNaN(radius)) {
setEmptyBounds();
return;
}
}
/**
* 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 ) {
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 l2oc,rad2,tca,t2hc,t,invMag;
Vector3d dir = new Vector3d(); // normalized direction of ray
Point3d oc = new Point3d(); // vector from sphere center to ray origin
oc.x = center.x - origin.x;
oc.y = center.y - origin.y;
oc.z = center.z - origin.z;
l2oc = oc.x*oc.x + oc.y*oc.y + oc.z*oc.z; // center to origin squared
rad2 = radius*radius;
if( l2oc < rad2 ){
// System.err.println("ray origin inside sphere" );
return true; // ray origin inside sphere
}
invMag = 1.0/Math.sqrt(direction.x*direction.x +
direction.y*direction.y +
direction.z*direction.z);
dir.x = direction.x*invMag;
dir.y = direction.y*invMag;
dir.z = direction.z*invMag;
tca = oc.x*dir.x + oc.y*dir.y + oc.z*dir.z;
if( tca <= 0.0 ) {
// System.err.println("ray points away from sphere" );
return false; // ray points away from sphere
}
t2hc = rad2 - l2oc + tca*tca;
if( t2hc > 0.0 ){
t = tca - Math.sqrt(t2hc);
// System.err.println("ray hits sphere:"+this.toString()+" t="+t+" direction="+dir );
position.x = origin.x + dir.x*t;
position.y = origin.y + dir.y*t;
position.z = origin.z + dir.z*t;
position.w = t;
return true; // ray hits sphere
}else {
// System.err.println("ray does not hit sphere" );
return false;
}
}
/**
* 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 ) {
double x,y,z,dist;
if( boundsIsEmpty ) {
return false;
}
if( boundsIsInfinite ) {
position.x = point.x;
position.y = point.y;
position.z = point.z;
position.w = 0.0;
return true;
}
x = point.x - center.x;
y = point.y - center.y;
z = point.z - center.z;
dist = x*x + y*y + z*z;
if( dist > radius*radius)
return false;
else {
position.x = point.x;
position.y = point.y;
position.z = point.z;
position.w = Math.sqrt(dist);
return true;
}
}
/**
* 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 ) {
if( boundsIsEmpty ) {
return false;
}
if( boundsIsInfinite ) {
position.x = start.x;
position.y = start.y;
position.z = start.z;
position.w = 0.0;
return true;
}
double l2oc,rad2,tca,t2hc,invMag,t;
Vector3d dir = new Vector3d(); // normalized direction of ray
Point3d oc = new Point3d(); // vector from sphere center to ray origin
Vector3d direction = new Vector3d();
oc.x = center.x - start.x;
oc.y = center.y - start.y;
oc.z = center.z - start.z;
direction.x = end.x - start.x;
direction.y = end.y - start.y;
direction.z = end.z - start.z;
invMag = 1.0/Math.sqrt( direction.x*direction.x +
direction.y*direction.y +
direction.z*direction.z);
dir.x = direction.x*invMag;
dir.y = direction.y*invMag;
dir.z = direction.z*invMag;
l2oc = oc.x*oc.x + oc.y*oc.y + oc.z*oc.z; // center to origin squared
rad2 = radius*radius;
if( l2oc < rad2 ){
// System.err.println("ray origin inside sphere" );
return true; // ray origin inside sphere
}
tca = oc.x*dir.x + oc.y*dir.y + oc.z*dir.z;
if( tca <= 0.0 ) {
// System.err.println("ray points away from sphere" );
return false; // ray points away from sphere
}
t2hc = rad2 - l2oc + tca*tca;
if( t2hc > 0.0 ){
t = tca - Math.sqrt(t2hc);
if( t*t <= ((end.x-start.x)*(end.x-start.x)+
(end.y-start.y)*(end.y-start.y)+
(end.z-start.z)*(end.z-start.z))){
position.x = start.x + dir.x*t;
position.y = start.y + dir.x*t;
position.z = start.z + dir.x*t;
position.w = t;
return true; // segment hits sphere
}
}
return false;
}
/**
* Test for intersection with a ray.
* @param origin the starting point of the ray
* @param direction the direction of the ray
* @return true or false indicating if an intersection occured
*/
@Override
public boolean intersect(Point3d origin, Vector3d direction ) {
if( boundsIsEmpty ) {
return false;
}
if( boundsIsInfinite ) {
return true;
}