OSDN Git Service

three.jsをThirdPartyに追加
[webglgame/webgl_framework.git] / webglFramework / Thirdparty / three.js-master / src / math / Plane.js
1 import { Matrix3 } from './Matrix3';
2 import { Vector3 } from './Vector3';
3
4 /**
5  * @author bhouston / http://clara.io
6  */
7
8 function Plane( normal, constant ) {
9
10         // normal is assumed to be normalized
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                         // Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
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                 // Note: will lead to a divide by zero if the plane is invalid.
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, optionalTarget ) {
114
115                 var result = optionalTarget || new Vector3();
116
117                 return result.copy( this.normal ).multiplyScalar( - this.distanceToPoint( point ) ).add( point );
118
119         },
120
121         intersectLine: function () {
122
123                 var v1 = new Vector3();
124
125                 return function intersectLine( line, optionalTarget ) {
126
127                         var result = optionalTarget || new Vector3();
128
129                         var direction = line.delta( v1 );
130
131                         var denominator = this.normal.dot( direction );
132
133                         if ( denominator === 0 ) {
134
135                                 // line is coplanar, return origin
136                                 if ( this.distanceToPoint( line.start ) === 0 ) {
137
138                                         return result.copy( line.start );
139
140                                 }
141
142                                 // Unsure if this is the correct method to handle this case.
143                                 return undefined;
144
145                         }
146
147                         var t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;
148
149                         if ( t < 0 || t > 1 ) {
150
151                                 return undefined;
152
153                         }
154
155                         return result.copy( direction ).multiplyScalar( t ).add( line.start );
156
157                 };
158
159         }(),
160
161         intersectsLine: function ( line ) {
162
163                 // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
164
165                 var startSign = this.distanceToPoint( line.start );
166                 var endSign = this.distanceToPoint( line.end );
167
168                 return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
169
170         },
171
172         intersectsBox: function ( box ) {
173
174                 return box.intersectsPlane( this );
175
176         },
177
178         intersectsSphere: function ( sphere ) {
179
180                 return sphere.intersectsPlane( this );
181
182         },
183
184         coplanarPoint: function ( optionalTarget ) {
185
186                 var result = optionalTarget || new Vector3();
187
188                 return result.copy( this.normal ).multiplyScalar( - this.constant );
189
190         },
191
192         applyMatrix4: function () {
193
194                 var v1 = new Vector3();
195                 var m1 = new Matrix3();
196
197                 return function applyMatrix4( matrix, optionalNormalMatrix ) {
198
199                         var normalMatrix = optionalNormalMatrix || m1.getNormalMatrix( matrix );
200
201                         var referencePoint = this.coplanarPoint( v1 ).applyMatrix4( matrix );
202
203                         var normal = this.normal.applyMatrix3( normalMatrix ).normalize();
204
205                         this.constant = - referencePoint.dot( normal );
206
207                         return this;
208
209                 };
210
211         }(),
212
213         translate: function ( offset ) {
214
215                 this.constant -= offset.dot( this.normal );
216
217                 return this;
218
219         },
220
221         equals: function ( plane ) {
222
223                 return plane.normal.equals( this.normal ) && ( plane.constant === this.constant );
224
225         }
226
227 } );
228
229
230 export { Plane };