aboutsummaryrefslogtreecommitdiffstats
path: root/src/javax/media/j3d/BoundingSphere.java
blob: f3e01f50e23a5edced0e6f1a001d71df5687b11d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
/*
 * 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 javax.media.j3d;

import javax.vecmath.Point3d;
import javax.vecmath.Point4d;
import javax.vecmath.Vector3d;
import javax.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;
	}

	double l2oc,rad2,tca,t2hc,mag;
	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
	}

	mag = Math.sqrt(direction.x*direction.x +
			direction.y*direction.y +
			direction.z*direction.z);
	dir.x = direction.x/mag;
	dir.y = direction.y/mag;
	dir.z = direction.z/mag;
	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 ){
	    //	System.err.println("ray hits sphere" );
	    return true;   // ray hits sphere
	}else {
	    //	System.err.println("ray does not hit sphere" );
	    return false;
	}
    }


    /**
     *	Returns the position of the intersect point if the ray intersects with
     * the sphere.
     *
     */
    boolean intersect(Point3d origin, Vector3d direction, Point3d intersectPoint ) {

	if( boundsIsEmpty ) {
	    return false;
	}

	if( boundsIsInfinite ) {
	  intersectPoint.x = origin.x;
	  intersectPoint.y = origin.y;
	  intersectPoint.z = origin.z;
	  return true;
	}

	double l2oc,rad2,tca,t2hc,mag,t;
	Point3d dir = new Point3d();  // normalized direction of ray
	Point3d oc  = new Point3d();  // vector from sphere center to ray origin

	oc.x = center.x - origin.x;   // XXXX: check if this method is still needed
	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
	}

	mag = Math.sqrt(direction.x*direction.x +
			direction.y*direction.y +
			direction.z*direction.z);
	dir.x = direction.x/mag;
	dir.y = direction.y/mag;
	dir.z = direction.z/mag;
	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);
	    intersectPoint.x = origin.x + direction.x*t;
	    intersectPoint.y = origin.y + direction.y*t;
	    intersectPoint.z = origin.z + direction.z*t;
	    //	System.err.println("ray hits sphere" );
	    return true;   // ray hits sphere
	}else {
	    //	System.err.println("ray does not hit sphere" );
	    return false;
	}
    }


    /**
     * Test for intersection with a point.
     * @param point a point defining a position in 3-space
     * @return true or false indicating if an intersection occured
     */
    @Override
    public boolean intersect(Point3d point ) {
	double x,y,z,dist;

	if( boundsIsEmpty ) {
	    return false;
	}
	if( boundsIsInfinite ) {
	    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
	    return true;

    }

    /**
     * Tests whether the bounding sphere is empty.  A bounding sphere is
     * empty if it is null (either by construction or as the result of
     * a null intersection) or if its volume is negative.  A bounding sphere
     * with a volume of zero is <i>not</i> empty.
     * @return true if the bounding sphere is empty;
     * otherwise, it returns false
     */
    @Override
    public boolean isEmpty() {
	return boundsIsEmpty;
    }

    /**
     * Test for intersection with another bounds object.
     * @param boundsObject another bounds object
     * @return true or false indicating if an intersection occured
     */
    @Override
    boolean intersect(Bounds boundsObject, Point4d position) {
	return intersect(boundsObject);
    }

    /**
     * Test for intersection with another bounds object.
     * @param boundsObject another bounds object
     * @return true or false indicating if an intersection occured
     */
    @Override
    public boolean intersect(Bounds boundsObject) {
	double distsq, radsq;
	BoundingSphere sphere;

	if( boundsObject == null ) {
	    return false;
	}

        if( boundsIsEmpty || boundsObject.boundsIsEmpty ) {
	    return false;
        }

	if( boundsIsInfinite || boundsObject.boundsIsInfinite ) {
	    return true;
	}

	if( boundsObject.boundId == BOUNDING_BOX){
	    BoundingBox box = (BoundingBox)boundsObject;
	    double dis = 0.0;
	    double rad_sq = radius*radius;

	    // find the corner closest to the center of sphere

	    if( center.x < box.lower.x )
		dis = (center.x-box.lower.x)*(center.x-box.lower.x);
	    else
		if( center.x > box.upper.x )
		    dis = (center.x-box.upper.x)*(center.x-box.upper.x);

	    if( center.y < box.lower.y )
		dis += (center.y-box.lower.y)*(center.y-box.lower.y);
	    else
		if( center.y > box.upper.y )
		    dis += (center.y-box.upper.y)*(center.y-box.upper.y);

	    if( center.z < box.lower.z )
		dis += (center.z-box.lower.z)*(center.z-box.lower.z);
	    else
		if( center.z > box.upper.z )
		    dis += (center.z-box.upper.z)*(center.z-box.upper.z);

	    return ( dis <= rad_sq );
	} else if( boundsObject.boundId == BOUNDING_SPHERE ) {
	    sphere = (BoundingSphere)boundsObject;
	    radsq = radius + sphere.radius;
	    radsq *= radsq;
	    distsq = center.distanceSquared(sphere.center);
	    return (distsq <= radsq);
	} else if(boundsObject.boundId == BOUNDING_POLYTOPE) {
	    return intersect_ptope_sphere( (BoundingPolytope)boundsObject, this);
	} else {
	    throw new IllegalArgumentException(J3dI18N.getString("BoundingSphere6"));
	}
    }

    /**
     * Test for intersection with another bounds object.
     * @param boundsObjects an array of bounding objects
     * @return true or false indicating if an intersection occured
     */
    @Override
    public boolean intersect(Bounds[] boundsObjects) {
	double distsq, radsq;
	BoundingSphere sphere;
	int i;

	if( boundsObjects == null || boundsObjects.length <= 0  )  {
	    return false;
	}

	if( boundsIsEmpty ) {
	    return false;
	}

	for(i = 0; i < boundsObjects.length; i++){
	    if( boundsObjects[i] == null || boundsObjects[i].boundsIsEmpty);
	    else if( boundsIsInfinite || boundsObjects[i].boundsIsInfinite ) {
		return true; // We're done here.
	    } else if( boundsObjects[i].boundId == BOUNDING_BOX){
		if( this.intersect( boundsObjects[i])) return true;
	    } else if( boundsObjects[i].boundId == BOUNDING_SPHERE ) {
		sphere = (BoundingSphere)boundsObjects[i];
		radsq = radius + sphere.radius;
		radsq *= radsq;
		distsq = center.distanceSquared(sphere.center);
		if (distsq <= radsq) {
		    return true;
		}
	    } else if(boundsObjects[i].boundId == BOUNDING_POLYTOPE) {
		if( this.intersect( boundsObjects[i])) return true;
	    } else {
		throw new IllegalArgumentException(J3dI18N.getString("BoundingSphere7"));
	    }
	}

	return false;

    }

    /**
     * Test for intersection with another bounds object.
     * @param boundsObject another bounds object
     * @param newBoundSphere the new bounding sphere which is the intersection of
     *      the boundsObject and this BoundingSphere
     * @return true or false indicating if an intersection occured
     */
    public boolean intersect(Bounds boundsObject, BoundingSphere newBoundSphere) {

	if (boundsObject == null || boundsIsEmpty || boundsObject.boundsIsEmpty) {
		newBoundSphere.set(null);
		return false;
	}

	if (boundsIsInfinite && !boundsObject.boundsIsInfinite) {
		newBoundSphere.set(boundsObject);
		return true;
	}

	if (boundsObject.boundsIsInfinite) {
		newBoundSphere.set(this);
		return true;
	}

	if(boundsObject.boundId == BOUNDING_BOX){
	    BoundingBox tbox =  new BoundingBox();
	    BoundingBox box = (BoundingBox)boundsObject;
	    if( this.intersect( box) ){
		BoundingBox sbox = new BoundingBox( this ); // convert sphere to box
		sbox.intersect(box, tbox);  // insersect two boxes
		newBoundSphere.set( tbox ); // set sphere to the intersection of 2 boxes
		return true;
	    } else {
			newBoundSphere.set(null);
			return false;
	    }
	} else if( boundsObject.boundId == BOUNDING_SPHERE ) {
	    BoundingSphere sphere = (BoundingSphere)boundsObject;
	    double dis,t,d2;
	    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 ( dis > radius+sphere.radius) {
			newBoundSphere.set(null);
			return false;
	    } else if( dis+radius <= sphere.radius ) { // this sphere is contained within boundsObject
		newBoundSphere.center.x = center.x;
		newBoundSphere.center.y = center.y;
		newBoundSphere.center.z = center.z;
		newBoundSphere.radius = radius;
	    } else if( dis+sphere.radius <= radius ) { // boundsObject is containted within this sphere
		newBoundSphere.center.x = sphere.center.x;
		newBoundSphere.center.y = sphere.center.y;
		newBoundSphere.center.z = sphere.center.z;
		newBoundSphere.radius = sphere.radius;
	    } else  {
		// distance from this center to center of overlapped volume
		d2 = (dis*dis + radius*radius - sphere.radius*sphere.radius)/(2.0*dis);
		newBoundSphere.radius = Math.sqrt( radius*radius - d2*d2);
		t = d2/dis;
		newBoundSphere.center.x = center.x + (sphere.center.x - center.x)*t;
		newBoundSphere.center.y = center.y + (sphere.center.y - center.y)*t;
		newBoundSphere.center.z = center.z + (sphere.center.z - center.z)*t;
	    }

	    newBoundSphere.updateBoundsStates();
	    return true;

	} else if(boundsObject.boundId == BOUNDING_POLYTOPE) {
	    BoundingBox tbox =  new BoundingBox();

	    BoundingPolytope polytope = (BoundingPolytope)boundsObject;
	    if( this.intersect( polytope) ){
		BoundingBox sbox = new BoundingBox( this ); // convert sphere to box
		BoundingBox pbox = new BoundingBox( polytope ); // convert polytope to box
		sbox.intersect(pbox,tbox);  // insersect two boxes
		newBoundSphere.set( tbox ); // set sphere to the intersection of 2 boxesf
		return true;
	    } else {
			newBoundSphere.set(null);
			return false;
	    }
	} else {
	    throw new IllegalArgumentException(J3dI18N.getString("BoundingSphere8"));
	}
    }

    /**
     * Test for intersection with an array of  bounds objects.
     * @param boundsObjects an array of bounds objects
     * @param newBoundSphere the new bounding sphere which is the intersection of
     *      the boundsObject and this BoundingSphere
     * @return true or false indicating if an intersection occured
     */
    public boolean intersect(Bounds[] boundsObjects, BoundingSphere newBoundSphere) {

	if (boundsObjects == null || boundsObjects.length <= 0 || boundsIsEmpty) {
		newBoundSphere.set(null);
		return false;
	}

	int i=0;

	// find first non null bounds object
	while( boundsObjects[i] == null && i < boundsObjects.length) {
	    i++;
	}

	if (i >= boundsObjects.length) { // all bounds objects were empty
		newBoundSphere.set(null);
		return false;
	}

	boolean status = false;
	double newRadius;
	Point3d newCenter = new Point3d();
	BoundingBox tbox = new BoundingBox();

	for(i=0;i<boundsObjects.length;i++) {
	    if( boundsObjects[i] == null || boundsObjects[i].boundsIsEmpty) ;
	    else if( boundsObjects[i].boundId == BOUNDING_BOX) {
		BoundingBox box = (BoundingBox)boundsObjects[i];
		if( this.intersect( box) ){
		    BoundingBox sbox = new BoundingBox( this ); // convert sphere to box
		    sbox.intersect(box,tbox); // insersect two boxes
		    if( status ) {
			newBoundSphere.combine( tbox );
		    } else {
			newBoundSphere.set( tbox ); // set sphere to the intersection of 2 boxesf
			status = true;
		    }
		}
	    } else if( boundsObjects[i].boundId == BOUNDING_SPHERE ) {
		BoundingSphere sphere = (BoundingSphere)boundsObjects[i];
		double dis,t,d2;
		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 ( dis > radius+sphere.radius) {
		} else if( dis+radius <= sphere.radius ) { // this sphere is contained within boundsObject
		    if( status ) {
			newBoundSphere.combine( this );
		    } else {
			newBoundSphere.center.x = center.x;
			newBoundSphere.center.y = center.y;
			newBoundSphere.center.z = center.z;
			newBoundSphere.radius = radius;
			status = true;
			newBoundSphere.updateBoundsStates();
		    }
		} else if( dis+sphere.radius <= radius ) { // boundsObject is containted within this sphere
		    if( status ) {
			newBoundSphere.combine( sphere );
		    } else {
			newBoundSphere.center.x = center.x;
			newBoundSphere.center.y = center.y;
			newBoundSphere.center.z = center.z;
			newBoundSphere.radius = sphere.radius;
			status = true;
			newBoundSphere.updateBoundsStates();
		    }
		} else  {
		    // distance from this center to center of overlapped volume
		    d2 = (dis*dis + radius*radius - sphere.radius*sphere.radius)/(2.0*dis);
		    newRadius = Math.sqrt( radius*radius - d2*d2);
		    t = d2/dis;
		    newCenter.x = center.x + (sphere.center.x - center.x)*t;
		    newCenter.y = center.y + (sphere.center.y - center.y)*t;
		    newCenter.z = center.z + (sphere.center.z - center.z)*t;
		    if( status ) {
			BoundingSphere newSphere = new BoundingSphere( newCenter,
								       newRadius );
			newBoundSphere.combine( newSphere );
		    } else {
			newBoundSphere.setRadius( newRadius );
			newBoundSphere.setCenter( newCenter );
			status = true;
		    }
		}

	    } else if(boundsObjects[i].boundId == BOUNDING_POLYTOPE) {
		BoundingPolytope polytope = (BoundingPolytope)boundsObjects[i];
		if( this.intersect( polytope) ){
		    BoundingBox sbox = new BoundingBox( this ); // convert sphere to box
		    BoundingBox pbox = new BoundingBox( polytope ); // convert polytope to box
		    sbox.intersect(pbox, tbox);            // insersect two boxes
		    if( status ) {
			newBoundSphere.combine( tbox );
		    } else {
			newBoundSphere.set( tbox );                // set sphere to the intersection of 2 boxesf
			status = true;
		    }
		}
	    } else {
		throw new IllegalArgumentException(J3dI18N.getString("BoundingSphere9"));
	    }
	}
	if (status == false)
		newBoundSphere.set(null);
	return status;
    }

    /**
     * Finds closest bounding object that intersects this bounding sphere.
     * @param boundsObjects an array of  bounds objects
     * @return closest bounding object
     */
    @Override
    public Bounds closestIntersection( Bounds[] boundsObjects) {

	if( boundsObjects == null || boundsObjects.length <= 0  ) {
	    return null;
        }

	if( boundsIsEmpty ) {
	    return null;
	}

	double dis,far_dis,pdist,x,y,z,rad_sq;
	double cenX = 0.0, cenY = 0.0, cenZ = 0.0;
	boolean contains = false;
	boolean inside;
	boolean intersect = false;
	double smallest_distance = Double.MAX_VALUE;
	int i,j,index=0;


	for(i = 0; i < boundsObjects.length; i++){
	    if( boundsObjects[i] == null ) ;

	    else if( this.intersect( boundsObjects[i])) {
		intersect = true;
		if(boundsObjects[i].boundId == BOUNDING_BOX){
		    BoundingBox box = (BoundingBox)boundsObjects[i];
		    cenX = (box.upper.x+box.lower.x)/2.0;
		    cenY = (box.upper.y+box.lower.y)/2.0;
		    cenZ = (box.upper.z+box.lower.z)/2.0;
		    dis = Math.sqrt( (center.x-cenX)*(center.x-cenX) +
				     (center.y-cenY)*(center.y-cenY) +
				     (center.z-cenZ)*(center.z-cenZ) );
		    if( (center.x-box.lower.x)*(center.x-box.lower.x) >
			(center.x-box.upper.x)*(center.x-box.upper.x) )
			far_dis = (center.x-box.lower.x)*(center.x-box.lower.x);
		    else
			far_dis = (center.x-box.upper.x)*(center.x-box.upper.x);

		    if( (center.y-box.lower.y)*(center.y-box.lower.y) >
			(center.y-box.upper.y)*(center.y-box.upper.y) )
			far_dis += (center.y-box.lower.y)*(center.y-box.lower.y);
		    else
			far_dis += (center.y-box.upper.y)*(center.y-box.upper.y);

		    if( (center.z-box.lower.z)*(center.z-box.lower.z) >
			(center.z-box.upper.z)*(center.z-box.upper.z) )
			far_dis += (center.z-box.lower.z)*(center.z-box.lower.z);
		    else
			far_dis += (center.z-box.upper.z)*(center.z-box.upper.z);

		    rad_sq = radius * radius;
		    if( far_dis <= rad_sq )  { // contains box
			if( !contains ){ // initialize smallest_distance for the first containment
			    index = i;
			    smallest_distance = dis;
			    contains = true;
			} else{
			    if( dis < smallest_distance){
				index = i;
				smallest_distance = dis;
			    }
			}
		    } else if (!contains) {
			if( dis < smallest_distance){
			    index = i;
			    smallest_distance = dis;
			}
		    }
		} else if( boundsObjects[i].boundId == BOUNDING_SPHERE ) {
		    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( (dis+sphere.radius) <= radius) { // contains the sphere
			if( !contains ){ // initialize smallest_distance for the first containment
			    index = i;
			    smallest_distance = dis;
			    contains = true;
			} else{
			    if( dis < smallest_distance){
				index = i;
				smallest_distance = dis;
			    }
			}
		    } else if (!contains) {
			if( dis < smallest_distance){
			    index = i;
			    smallest_distance = dis;
			}
		    }

		} else if(boundsObjects[i].boundId == BOUNDING_POLYTOPE) {
		    BoundingPolytope polytope = (BoundingPolytope)boundsObjects[i];
		    dis = Math.sqrt( (center.x-polytope.centroid.x)*(center.x-polytope.centroid.x) +
				     (center.y-polytope.centroid.y)*(center.y-polytope.centroid.y) +
				     (center.z-polytope.centroid.z)*(center.z-polytope.centroid.z) );
		    inside = true;
		    for(j=0;j<polytope.nVerts;j++) {
			x = polytope.verts[j].x - center.x;
			y = polytope.verts[j].y - center.y;
			z = polytope.verts[j].z - center.z;

			pdist = x*x + y*y + z*z;
			if( pdist > radius*radius)
			    inside=false;
		    }
		    if( inside ) {
			if( !contains ){ // initialize smallest_distance for the first containment
			    index = i;
			    smallest_distance = dis;
			    contains = true;
			} else{
			    if( dis < smallest_distance){
				index = i;
				smallest_distance = dis;
			    }
			}
		    } else if (!contains) {
			if( dis < smallest_distance){
			    index = i;
			    smallest_distance = dis;
			}
		    }

		} else {
		    throw new IllegalArgumentException(J3dI18N.getString("BoundingSphere10"));
		}
	    }
	}

	if ( intersect )
	    return boundsObjects[index];
	else
	    return null;

    }


    /**
     * Intersects this bounding sphere with preprocessed  frustum.
     * @return true if the bounding sphere and frustum intersect.
     */
    boolean intersect(CachedFrustum frustum) {
	int i;
	double dist;

	if( boundsIsEmpty ) {
	    return false;
	}

	if(boundsIsInfinite)
	    return true;

	for (i=0; i<6; i++) {
	    dist = frustum.clipPlanes[i].x*center.x + frustum.clipPlanes[i].y*center.y +
	        frustum.clipPlanes[i].z*center.z + frustum.clipPlanes[i].w;
	    if (dist < 0.0 && (dist + radius) < 0.0) {
		return(false);
	    }
	}
	return true;
    }

    /**
     * This intersects this bounding sphere with 6 frustum plane equations
     * @return returns true if the bounding sphere and frustum intersect.
     */
    boolean intersect(Vector4d[] planes) {
	int i;
	double dist;

	if( boundsIsEmpty ) {
	    return false;
	}

	if(boundsIsInfinite)
	    return true;

	for (i=0; i<6; i++) {
	    dist = planes[i].x*center.x + planes[i].y*center.y +
	        planes[i].z*center.z + planes[i].w;
	    if (dist < 0.0 && (dist + radius) < 0.0) {
		//System.err.println("Tossing " + i + " " + dist + " " + radius);
		return(false);
	    }
	}
	return true;
    }

    /**
     * Returns a string representation of this class.
     */
    @Override
    public String toString() {
	return new String( "Center="+center+"  Radius="+radius);
    }

private void setEmptyBounds() {
	center.set(0.0d, 0.0d, 0.0d);
	radius = -1.0;
	boundsIsInfinite = false;
	boundsIsEmpty = true;
}

private void setInfiniteBounds() {
	center.set(0.0d, 0.0d, 0.0d);
	radius = Double.POSITIVE_INFINITY;
	boundsIsEmpty = false;
	boundsIsInfinite = true;
}

    private void updateBoundsStates() {

	if (Double.isNaN(radius + center.x + center.y + center.z)) {
	     boundsIsEmpty = true;
	     boundsIsInfinite = false;
	     return;
	}

	if(radius == Double.POSITIVE_INFINITY) {
	    boundsIsEmpty = false;
	    boundsIsInfinite = true;
	}
	else {
	    boundsIsInfinite = false;
	    if( radius < 0.0 ) {
		boundsIsEmpty = true;
	    } else {
		boundsIsEmpty = false;
	    }
	}
    }

    @Override
    Point3d getCenter() {
	return center;
    }

    /**
     * if the passed the "region" is same type as this object
     * then do a copy, otherwise clone the Bounds  and
     * return
     */
    @Override
    Bounds copy(Bounds r) {
	if (r != null && this.boundId == r.boundId) {
	    BoundingSphere region = (BoundingSphere)r;
	    region.radius = radius;
	    region.center.x = center.x;
	    region.center.y = center.y;
	    region.center.z = center.z;
	    region.boundsIsEmpty = boundsIsEmpty;
	    region.boundsIsInfinite = boundsIsInfinite;
	    return region;
	}
	else {
	    return (Bounds) this.clone();
	}
    }

    @Override
    int getPickType() {
	return PickShape.PICKBOUNDINGSPHERE;
    }
}