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