1 | /**
|
---|
2 | * @author bhouston / http://exocortex.com
|
---|
3 | * @author mrdoob / http://mrdoob.com/
|
---|
4 | */
|
---|
5 |
|
---|
6 | THREE.Sphere = function ( center, radius ) {
|
---|
7 |
|
---|
8 | this.center = ( center !== undefined ) ? center : new THREE.Vector3();
|
---|
9 | this.radius = ( radius !== undefined ) ? radius : 0;
|
---|
10 |
|
---|
11 | };
|
---|
12 |
|
---|
13 | THREE.Sphere.prototype = {
|
---|
14 |
|
---|
15 | constructor: THREE.Sphere,
|
---|
16 |
|
---|
17 | set: function ( center, radius ) {
|
---|
18 |
|
---|
19 | this.center.copy( center );
|
---|
20 | this.radius = radius;
|
---|
21 |
|
---|
22 | return this;
|
---|
23 | },
|
---|
24 |
|
---|
25 |
|
---|
26 | setFromPoints: function () {
|
---|
27 |
|
---|
28 | var box = new THREE.Box3();
|
---|
29 |
|
---|
30 | return function ( points, optionalCenter ) {
|
---|
31 |
|
---|
32 | var center = this.center;
|
---|
33 |
|
---|
34 | if ( optionalCenter !== undefined ) {
|
---|
35 |
|
---|
36 | center.copy( optionalCenter );
|
---|
37 |
|
---|
38 | } else {
|
---|
39 |
|
---|
40 | box.setFromPoints( points ).center( center );
|
---|
41 |
|
---|
42 | }
|
---|
43 |
|
---|
44 | var maxRadiusSq = 0;
|
---|
45 |
|
---|
46 | for ( var i = 0, il = points.length; i < il; i ++ ) {
|
---|
47 |
|
---|
48 | maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
|
---|
49 |
|
---|
50 | }
|
---|
51 |
|
---|
52 | this.radius = Math.sqrt( maxRadiusSq );
|
---|
53 |
|
---|
54 | return this;
|
---|
55 |
|
---|
56 | };
|
---|
57 |
|
---|
58 | }(),
|
---|
59 |
|
---|
60 | copy: function ( sphere ) {
|
---|
61 |
|
---|
62 | this.center.copy( sphere.center );
|
---|
63 | this.radius = sphere.radius;
|
---|
64 |
|
---|
65 | return this;
|
---|
66 |
|
---|
67 | },
|
---|
68 |
|
---|
69 | empty: function () {
|
---|
70 |
|
---|
71 | return ( this.radius <= 0 );
|
---|
72 |
|
---|
73 | },
|
---|
74 |
|
---|
75 | containsPoint: function ( point ) {
|
---|
76 |
|
---|
77 | return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
|
---|
78 |
|
---|
79 | },
|
---|
80 |
|
---|
81 | distanceToPoint: function ( point ) {
|
---|
82 |
|
---|
83 | return ( point.distanceTo( this.center ) - this.radius );
|
---|
84 |
|
---|
85 | },
|
---|
86 |
|
---|
87 | intersectsSphere: function ( sphere ) {
|
---|
88 |
|
---|
89 | var radiusSum = this.radius + sphere.radius;
|
---|
90 |
|
---|
91 | return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum );
|
---|
92 |
|
---|
93 | },
|
---|
94 |
|
---|
95 | clampPoint: function ( point, optionalTarget ) {
|
---|
96 |
|
---|
97 | var deltaLengthSq = this.center.distanceToSquared( point );
|
---|
98 |
|
---|
99 | var result = optionalTarget || new THREE.Vector3();
|
---|
100 | result.copy( point );
|
---|
101 |
|
---|
102 | if ( deltaLengthSq > ( this.radius * this.radius ) ) {
|
---|
103 |
|
---|
104 | result.sub( this.center ).normalize();
|
---|
105 | result.multiplyScalar( this.radius ).add( this.center );
|
---|
106 |
|
---|
107 | }
|
---|
108 |
|
---|
109 | return result;
|
---|
110 |
|
---|
111 | },
|
---|
112 |
|
---|
113 | getBoundingBox: function ( optionalTarget ) {
|
---|
114 |
|
---|
115 | var box = optionalTarget || new THREE.Box3();
|
---|
116 |
|
---|
117 | box.set( this.center, this.center );
|
---|
118 | box.expandByScalar( this.radius );
|
---|
119 |
|
---|
120 | return box;
|
---|
121 |
|
---|
122 | },
|
---|
123 |
|
---|
124 | applyMatrix4: function ( matrix ) {
|
---|
125 |
|
---|
126 | this.center.applyMatrix4( matrix );
|
---|
127 | this.radius = this.radius * matrix.getMaxScaleOnAxis();
|
---|
128 |
|
---|
129 | return this;
|
---|
130 |
|
---|
131 | },
|
---|
132 |
|
---|
133 | translate: function ( offset ) {
|
---|
134 |
|
---|
135 | this.center.add( offset );
|
---|
136 |
|
---|
137 | return this;
|
---|
138 |
|
---|
139 | },
|
---|
140 |
|
---|
141 | equals: function ( sphere ) {
|
---|
142 |
|
---|
143 | return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
|
---|
144 |
|
---|
145 | },
|
---|
146 |
|
---|
147 | clone: function () {
|
---|
148 |
|
---|
149 | return new THREE.Sphere().copy( this );
|
---|
150 |
|
---|
151 | }
|
---|
152 |
|
---|
153 | };
|
---|