1 | /**
|
---|
2 | * @author bhouston / http://exocortex.com
|
---|
3 | */
|
---|
4 |
|
---|
5 | module( "Plane" );
|
---|
6 |
|
---|
7 | var comparePlane = function ( a, b, threshold ) {
|
---|
8 | threshold = threshold || 0.0001;
|
---|
9 | return ( a.normal.distanceTo( b.normal ) < threshold &&
|
---|
10 | Math.abs( a.constant - b.constant ) < threshold );
|
---|
11 | };
|
---|
12 |
|
---|
13 |
|
---|
14 | test( "constructor", function() {
|
---|
15 | var a = new THREE.Plane();
|
---|
16 | ok( a.normal.x == 1, "Passed!" );
|
---|
17 | ok( a.normal.y == 0, "Passed!" );
|
---|
18 | ok( a.normal.z == 0, "Passed!" );
|
---|
19 | ok( a.constant == 0, "Passed!" );
|
---|
20 |
|
---|
21 | a = new THREE.Plane( one3.clone(), 0 );
|
---|
22 | ok( a.normal.x == 1, "Passed!" );
|
---|
23 | ok( a.normal.y == 1, "Passed!" );
|
---|
24 | ok( a.normal.z == 1, "Passed!" );
|
---|
25 | ok( a.constant == 0, "Passed!" );
|
---|
26 |
|
---|
27 | a = new THREE.Plane( one3.clone(), 1 );
|
---|
28 | ok( a.normal.x == 1, "Passed!" );
|
---|
29 | ok( a.normal.y == 1, "Passed!" );
|
---|
30 | ok( a.normal.z == 1, "Passed!" );
|
---|
31 | ok( a.constant == 1, "Passed!" );
|
---|
32 | });
|
---|
33 |
|
---|
34 | test( "copy", function() {
|
---|
35 | var a = new THREE.Plane( new THREE.Vector3( x, y, z ), w );
|
---|
36 | var b = new THREE.Plane().copy( a );
|
---|
37 | ok( b.normal.x == x, "Passed!" );
|
---|
38 | ok( b.normal.y == y, "Passed!" );
|
---|
39 | ok( b.normal.z == z, "Passed!" );
|
---|
40 | ok( b.constant == w, "Passed!" );
|
---|
41 |
|
---|
42 | // ensure that it is a true copy
|
---|
43 | a.normal.x = 0;
|
---|
44 | a.normal.y = -1;
|
---|
45 | a.normal.z = -2;
|
---|
46 | a.constant = -3;
|
---|
47 | ok( b.normal.x == x, "Passed!" );
|
---|
48 | ok( b.normal.y == y, "Passed!" );
|
---|
49 | ok( b.normal.z == z, "Passed!" );
|
---|
50 | ok( b.constant == w, "Passed!" );
|
---|
51 | });
|
---|
52 |
|
---|
53 | test( "set", function() {
|
---|
54 | var a = new THREE.Plane();
|
---|
55 | ok( a.normal.x == 1, "Passed!" );
|
---|
56 | ok( a.normal.y == 0, "Passed!" );
|
---|
57 | ok( a.normal.z == 0, "Passed!" );
|
---|
58 | ok( a.constant == 0, "Passed!" );
|
---|
59 |
|
---|
60 | var b = a.clone().set( new THREE.Vector3( x, y, z ), w );
|
---|
61 | ok( b.normal.x == x, "Passed!" );
|
---|
62 | ok( b.normal.y == y, "Passed!" );
|
---|
63 | ok( b.normal.z == z, "Passed!" );
|
---|
64 | ok( b.constant == w, "Passed!" );
|
---|
65 | });
|
---|
66 |
|
---|
67 | test( "setComponents", function() {
|
---|
68 | var a = new THREE.Plane();
|
---|
69 | ok( a.normal.x == 1, "Passed!" );
|
---|
70 | ok( a.normal.y == 0, "Passed!" );
|
---|
71 | ok( a.normal.z == 0, "Passed!" );
|
---|
72 | ok( a.constant == 0, "Passed!" );
|
---|
73 |
|
---|
74 | var b = a.clone().setComponents( x, y, z , w );
|
---|
75 | ok( b.normal.x == x, "Passed!" );
|
---|
76 | ok( b.normal.y == y, "Passed!" );
|
---|
77 | ok( b.normal.z == z, "Passed!" );
|
---|
78 | ok( b.constant == w, "Passed!" );
|
---|
79 | });
|
---|
80 |
|
---|
81 | test( "setFromNormalAndCoplanarPoint", function() {
|
---|
82 | var normal = one3.clone().normalize();
|
---|
83 | var a = new THREE.Plane().setFromNormalAndCoplanarPoint( normal, zero3 );
|
---|
84 |
|
---|
85 | ok( a.normal.equals( normal ), "Passed!" );
|
---|
86 | ok( a.constant == 0, "Passed!" );
|
---|
87 | });
|
---|
88 |
|
---|
89 | test( "normalize", function() {
|
---|
90 | var a = new THREE.Plane( new THREE.Vector3( 2, 0, 0 ), 2 );
|
---|
91 |
|
---|
92 | a.normalize();
|
---|
93 | ok( a.normal.length() == 1, "Passed!" );
|
---|
94 | ok( a.normal.equals( new THREE.Vector3( 1, 0, 0 ) ), "Passed!" );
|
---|
95 | ok( a.constant == 1, "Passed!" );
|
---|
96 | });
|
---|
97 |
|
---|
98 | test( "negate/distanceToPoint", function() {
|
---|
99 | var a = new THREE.Plane( new THREE.Vector3( 2, 0, 0 ), -2 );
|
---|
100 |
|
---|
101 | a.normalize();
|
---|
102 | ok( a.distanceToPoint( new THREE.Vector3( 4, 0, 0 ) ) === 3, "Passed!" );
|
---|
103 | ok( a.distanceToPoint( new THREE.Vector3( 1, 0, 0 ) ) === 0, "Passed!" );
|
---|
104 |
|
---|
105 | a.negate();
|
---|
106 | ok( a.distanceToPoint( new THREE.Vector3( 4, 0, 0 ) ) === -3, "Passed!" );
|
---|
107 | ok( a.distanceToPoint( new THREE.Vector3( 1, 0, 0 ) ) === 0, "Passed!" );
|
---|
108 | });
|
---|
109 |
|
---|
110 | test( "distanceToPoint", function() {
|
---|
111 | var a = new THREE.Plane( new THREE.Vector3( 2, 0, 0 ), -2 );
|
---|
112 |
|
---|
113 | a.normalize();
|
---|
114 | ok( a.distanceToPoint( a.projectPoint( zero3.clone() ) ) === 0, "Passed!" );
|
---|
115 | ok( a.distanceToPoint( new THREE.Vector3( 4, 0, 0 ) ) === 3, "Passed!" );
|
---|
116 | });
|
---|
117 |
|
---|
118 | test( "distanceToSphere", function() {
|
---|
119 | var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
|
---|
120 |
|
---|
121 | var b = new THREE.Sphere( new THREE.Vector3( 2, 0, 0 ), 1 );
|
---|
122 |
|
---|
123 | ok( a.distanceToSphere( b ) === 1, "Passed!" );
|
---|
124 |
|
---|
125 | a.set( new THREE.Vector3( 1, 0, 0 ), 2 );
|
---|
126 | ok( a.distanceToSphere( b ) === 3, "Passed!" );
|
---|
127 | a.set( new THREE.Vector3( 1, 0, 0 ), -2 );
|
---|
128 | ok( a.distanceToSphere( b ) === -1, "Passed!" );
|
---|
129 | });
|
---|
130 |
|
---|
131 | test( "isInterestionLine/intersectLine", function() {
|
---|
132 | var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
|
---|
133 |
|
---|
134 | var l1 = new THREE.Line3( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) );
|
---|
135 | ok( a.isIntersectionLine( l1 ), "Passed!" );
|
---|
136 | ok( a.intersectLine( l1 ).equals( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
|
---|
137 |
|
---|
138 | a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), -3 );
|
---|
139 |
|
---|
140 | ok( a.isIntersectionLine( l1 ), "Passed!" );
|
---|
141 | ok( a.intersectLine( l1 ).equals( new THREE.Vector3( 3, 0, 0 ) ), "Passed!" );
|
---|
142 |
|
---|
143 |
|
---|
144 | a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), -11 );
|
---|
145 |
|
---|
146 | ok( ! a.isIntersectionLine( l1 ), "Passed!" );
|
---|
147 | ok( a.intersectLine( l1 ) === undefined, "Passed!" );
|
---|
148 |
|
---|
149 | a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 11 );
|
---|
150 |
|
---|
151 | ok( ! a.isIntersectionLine( l1 ), "Passed!" );
|
---|
152 | ok( a.intersectLine( l1 ) === undefined, "Passed!" );
|
---|
153 |
|
---|
154 | });
|
---|
155 |
|
---|
156 | test( "projectPoint", function() {
|
---|
157 | var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
|
---|
158 |
|
---|
159 | ok( a.projectPoint( new THREE.Vector3( 10, 0, 0 ) ).equals( zero3 ), "Passed!" );
|
---|
160 | ok( a.projectPoint( new THREE.Vector3( -10, 0, 0 ) ).equals( zero3 ), "Passed!" );
|
---|
161 |
|
---|
162 | a = new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), -1 );
|
---|
163 | ok( a.projectPoint( new THREE.Vector3( 0, 0, 0 ) ).equals( new THREE.Vector3( 0, 1, 0 ) ), "Passed!" );
|
---|
164 | ok( a.projectPoint( new THREE.Vector3( 0, 1, 0 ) ).equals( new THREE.Vector3( 0, 1, 0 ) ), "Passed!" );
|
---|
165 |
|
---|
166 | });
|
---|
167 |
|
---|
168 | test( "orthoPoint", function() {
|
---|
169 | var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
|
---|
170 |
|
---|
171 | ok( a.orthoPoint( new THREE.Vector3( 10, 0, 0 ) ).equals( new THREE.Vector3( 10, 0, 0 ) ), "Passed!" );
|
---|
172 | ok( a.orthoPoint( new THREE.Vector3( -10, 0, 0 ) ).equals( new THREE.Vector3( -10, 0, 0 ) ), "Passed!" );
|
---|
173 | });
|
---|
174 |
|
---|
175 | test( "coplanarPoint", function() {
|
---|
176 | var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
|
---|
177 | ok( a.distanceToPoint( a.coplanarPoint() ) === 0, "Passed!" );
|
---|
178 |
|
---|
179 | a = new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), -1 );
|
---|
180 | ok( a.distanceToPoint( a.coplanarPoint() ) === 0, "Passed!" );
|
---|
181 | });
|
---|
182 |
|
---|
183 | test( "applyMatrix4/translate", function() {
|
---|
184 |
|
---|
185 | var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
|
---|
186 |
|
---|
187 | var m = new THREE.Matrix4();
|
---|
188 | m.makeRotationZ( Math.PI * 0.5 );
|
---|
189 |
|
---|
190 | ok( comparePlane( a.clone().applyMatrix4( m ), new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), 0 ) ), "Passed!" );
|
---|
191 |
|
---|
192 | a = new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), -1 );
|
---|
193 | ok( comparePlane( a.clone().applyMatrix4( m ), new THREE.Plane( new THREE.Vector3( -1, 0, 0 ), -1 ) ), "Passed!" );
|
---|
194 |
|
---|
195 | m.makeTranslation( 1, 1, 1 );
|
---|
196 | ok( comparePlane( a.clone().applyMatrix4( m ), a.clone().translate( new THREE.Vector3( 1, 1, 1 ) ) ), "Passed!" );
|
---|
197 | });
|
---|