1 | import { Matrix3 } from './Matrix3.js';
|
2 | import { Vector3 } from './Vector3.js';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | function Plane( normal, constant ) {
|
9 |
|
10 |
|
11 |
|
12 | this.normal = ( normal !== undefined ) ? normal : new Vector3( 1, 0, 0 );
|
13 | this.constant = ( constant !== undefined ) ? constant : 0;
|
14 |
|
15 | }
|
16 |
|
17 | Object.assign( Plane.prototype, {
|
18 |
|
19 | set: function ( normal, constant ) {
|
20 |
|
21 | this.normal.copy( normal );
|
22 | this.constant = constant;
|
23 |
|
24 | return this;
|
25 |
|
26 | },
|
27 |
|
28 | setComponents: function ( x, y, z, w ) {
|
29 |
|
30 | this.normal.set( x, y, z );
|
31 | this.constant = w;
|
32 |
|
33 | return this;
|
34 |
|
35 | },
|
36 |
|
37 | setFromNormalAndCoplanarPoint: function ( normal, point ) {
|
38 |
|
39 | this.normal.copy( normal );
|
40 | this.constant = - point.dot( this.normal );
|
41 |
|
42 | return this;
|
43 |
|
44 | },
|
45 |
|
46 | setFromCoplanarPoints: function () {
|
47 |
|
48 | var v1 = new Vector3();
|
49 | var v2 = new Vector3();
|
50 |
|
51 | return function setFromCoplanarPoints( a, b, c ) {
|
52 |
|
53 | var normal = v1.subVectors( c, b ).cross( v2.subVectors( a, b ) ).normalize();
|
54 |
|
55 |
|
56 |
|
57 | this.setFromNormalAndCoplanarPoint( normal, a );
|
58 |
|
59 | return this;
|
60 |
|
61 | };
|
62 |
|
63 | }(),
|
64 |
|
65 | clone: function () {
|
66 |
|
67 | return new this.constructor().copy( this );
|
68 |
|
69 | },
|
70 |
|
71 | copy: function ( plane ) {
|
72 |
|
73 | this.normal.copy( plane.normal );
|
74 | this.constant = plane.constant;
|
75 |
|
76 | return this;
|
77 |
|
78 | },
|
79 |
|
80 | normalize: function () {
|
81 |
|
82 |
|
83 |
|
84 | var inverseNormalLength = 1.0 / this.normal.length();
|
85 | this.normal.multiplyScalar( inverseNormalLength );
|
86 | this.constant *= inverseNormalLength;
|
87 |
|
88 | return this;
|
89 |
|
90 | },
|
91 |
|
92 | negate: function () {
|
93 |
|
94 | this.constant *= - 1;
|
95 | this.normal.negate();
|
96 |
|
97 | return this;
|
98 |
|
99 | },
|
100 |
|
101 | distanceToPoint: function ( point ) {
|
102 |
|
103 | return this.normal.dot( point ) + this.constant;
|
104 |
|
105 | },
|
106 |
|
107 | distanceToSphere: function ( sphere ) {
|
108 |
|
109 | return this.distanceToPoint( sphere.center ) - sphere.radius;
|
110 |
|
111 | },
|
112 |
|
113 | projectPoint: function ( point, target ) {
|
114 |
|
115 | if ( target === undefined ) {
|
116 |
|
117 | console.warn( 'THREE.Plane: .projectPoint() target is now required' );
|
118 | target = new Vector3();
|
119 |
|
120 | }
|
121 |
|
122 | return target.copy( this.normal ).multiplyScalar( - this.distanceToPoint( point ) ).add( point );
|
123 |
|
124 | },
|
125 |
|
126 | intersectLine: function () {
|
127 |
|
128 | var v1 = new Vector3();
|
129 |
|
130 | return function intersectLine( line, target ) {
|
131 |
|
132 | if ( target === undefined ) {
|
133 |
|
134 | console.warn( 'THREE.Plane: .intersectLine() target is now required' );
|
135 | target = new Vector3();
|
136 |
|
137 | }
|
138 |
|
139 | var direction = line.delta( v1 );
|
140 |
|
141 | var denominator = this.normal.dot( direction );
|
142 |
|
143 | if ( denominator === 0 ) {
|
144 |
|
145 |
|
146 | if ( this.distanceToPoint( line.start ) === 0 ) {
|
147 |
|
148 | return target.copy( line.start );
|
149 |
|
150 | }
|
151 |
|
152 |
|
153 | return undefined;
|
154 |
|
155 | }
|
156 |
|
157 | var t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;
|
158 |
|
159 | if ( t < 0 || t > 1 ) {
|
160 |
|
161 | return undefined;
|
162 |
|
163 | }
|
164 |
|
165 | return target.copy( direction ).multiplyScalar( t ).add( line.start );
|
166 |
|
167 | };
|
168 |
|
169 | }(),
|
170 |
|
171 | intersectsLine: function ( line ) {
|
172 |
|
173 |
|
174 |
|
175 | var startSign = this.distanceToPoint( line.start );
|
176 | var endSign = this.distanceToPoint( line.end );
|
177 |
|
178 | return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
|
179 |
|
180 | },
|
181 |
|
182 | intersectsBox: function ( box ) {
|
183 |
|
184 | return box.intersectsPlane( this );
|
185 |
|
186 | },
|
187 |
|
188 | intersectsSphere: function ( sphere ) {
|
189 |
|
190 | return sphere.intersectsPlane( this );
|
191 |
|
192 | },
|
193 |
|
194 | coplanarPoint: function ( target ) {
|
195 |
|
196 | if ( target === undefined ) {
|
197 |
|
198 | console.warn( 'THREE.Plane: .coplanarPoint() target is now required' );
|
199 | target = new Vector3();
|
200 |
|
201 | }
|
202 |
|
203 | return target.copy( this.normal ).multiplyScalar( - this.constant );
|
204 |
|
205 | },
|
206 |
|
207 | applyMatrix4: function () {
|
208 |
|
209 | var v1 = new Vector3();
|
210 | var m1 = new Matrix3();
|
211 |
|
212 | return function applyMatrix4( matrix, optionalNormalMatrix ) {
|
213 |
|
214 | var normalMatrix = optionalNormalMatrix || m1.getNormalMatrix( matrix );
|
215 |
|
216 | var referencePoint = this.coplanarPoint( v1 ).applyMatrix4( matrix );
|
217 |
|
218 | var normal = this.normal.applyMatrix3( normalMatrix ).normalize();
|
219 |
|
220 | this.constant = - referencePoint.dot( normal );
|
221 |
|
222 | return this;
|
223 |
|
224 | };
|
225 |
|
226 | }(),
|
227 |
|
228 | translate: function ( offset ) {
|
229 |
|
230 | this.constant -= offset.dot( this.normal );
|
231 |
|
232 | return this;
|
233 |
|
234 | },
|
235 |
|
236 | equals: function ( plane ) {
|
237 |
|
238 | return plane.normal.equals( this.normal ) && ( plane.constant === this.constant );
|
239 |
|
240 | }
|
241 |
|
242 | } );
|
243 |
|
244 |
|
245 | export { Plane };
|