@@ -24,31 +24,29 @@ impl Plane {
24
24
25
25
/// Creates a new `Plane` from the ['Vector3'](./type.Vector3.html) normal and the distance from the origin.
26
26
#[ inline]
27
- pub fn new_with_normal ( normal : Vector3 , d : f32 ) -> Plane {
28
- Plane {
29
- normal : normal,
30
- d : d,
31
- }
27
+ pub fn new ( normal : Vector3 , d : f32 ) -> Plane {
28
+ Plane { normal, d }
32
29
}
33
30
34
31
/// Creates a new `Plane` from four floats.
35
32
/// a, b, c are used for the normal ['Vector3'](./type.Vector3.html).
36
33
/// d is the distance from the origin.
37
34
#[ inline]
38
- pub fn new_with_reals ( a : f32 , b : f32 , c : f32 , d : f32 ) -> Plane {
35
+ pub fn from_coordinates ( a : f32 , b : f32 , c : f32 , d : f32 ) -> Plane {
39
36
Plane {
40
37
normal : Vector3 :: new ( a, b, c) ,
41
- d : d ,
38
+ d,
42
39
}
43
40
}
44
41
45
42
/// Creates a new `Plane` from three [`Vector3`](./type.Vector3.html), given in clockwise order.
43
+ /// If all three points are collinear, the resulting coordinates will be `(NaN, NaN, NaN, NaN)`.
46
44
#[ inline]
47
- pub fn new_with_vectors ( a : Vector3 , b : Vector3 , c : Vector3 ) -> Plane {
48
- let normal = ( a - c) . cross ( a - b) ;
45
+ pub fn from_points ( a : Vector3 , b : Vector3 , c : Vector3 ) -> Plane {
46
+ let normal = ( a - c) . cross ( a - b) . normalize ( ) ;
49
47
50
48
Plane {
51
- normal : normal ,
49
+ normal,
52
50
d : normal. dot ( a) ,
53
51
}
54
52
}
@@ -154,12 +152,12 @@ impl Plane {
154
152
if l == 0.0 {
155
153
self . normal = Vector3 :: new ( 0.0 , 0.0 , 0.0 ) ;
156
154
self . d = 0.0 ;
157
- return self ;
158
155
} else {
159
156
self . normal /= l;
160
157
self . d /= l;
161
- return self ;
162
158
}
159
+
160
+ self
163
161
}
164
162
165
163
/// Returns the orthogonal projection of `point` into a point in the `Plane`.
@@ -175,18 +173,38 @@ mod test {
175
173
176
174
fn test_inputs ( ) -> ( Plane , Vector3 ) {
177
175
(
178
- Plane :: new_with_reals ( 0.01 , 0.02 , 0.04 , 0.08 ) ,
176
+ Plane :: from_coordinates ( 0.01 , 0.02 , 0.04 , 0.08 ) ,
179
177
Vector3 :: new ( 0.16 , 0.32 , 0.64 ) ,
180
178
)
181
179
}
182
180
181
+ #[ test]
182
+ fn from_points ( ) {
183
+ let a = Vector3 :: new ( -1.0 , 1.0 , 0.0 ) ;
184
+ let b = Vector3 :: new ( -1.0 , 0.0 , 0.0 ) ;
185
+ let c = Vector3 :: new ( 1.0 , 1.0 , 1.0 ) ;
186
+ let d = Vector3 :: new ( -1.0 , -1.0 , 0.0 ) ;
187
+
188
+ let test_collinear = Plane :: from_points ( a, b, d) ;
189
+
190
+ let expected_valid = Plane :: from_coordinates ( 0.447214 , 0.0 , -0.894427 , -0.447214 ) ;
191
+
192
+ assert ! ( Plane :: from_points( a, b, c) . approx_eq( expected_valid) ) ;
193
+ assert ! (
194
+ test_collinear. normal. x. is_nan( )
195
+ && test_collinear. normal. y. is_nan( )
196
+ && test_collinear. normal. z. is_nan( )
197
+ && test_collinear. d. is_nan( )
198
+ ) ;
199
+ }
200
+
183
201
#[ test]
184
202
fn center ( ) {
185
203
let ( p, _v) = test_inputs ( ) ;
186
204
187
205
let expected = Vector3 :: new ( 0.0008 , 0.0016 , 0.0032 ) ;
188
206
189
- assert_eq ! ( p. center( ) , expected) ;
207
+ assert ! ( p. center( ) . approx_eq ( & expected) ) ;
190
208
}
191
209
192
210
#[ test]
@@ -195,12 +213,12 @@ mod test {
195
213
196
214
let expected = -0.0464 ;
197
215
198
- assert_eq ! ( p. distance_to( v) , expected) ;
216
+ assert ! ( p. distance_to( v) . approx_eq ( & expected) ) ;
199
217
}
200
218
201
219
#[ test]
202
220
fn has_point ( ) {
203
- let p = Plane :: new_with_normal ( Vector3 :: new ( 1.0 , 1.0 , 1.0 ) , 1.0 ) ;
221
+ let p = Plane :: new ( Vector3 :: new ( 1.0 , 1.0 , 1.0 ) , 1.0 ) ;
204
222
205
223
let outside = Vector3 :: new ( 0.0 , 0.0 , 0.0 ) ;
206
224
let inside = Vector3 :: new ( 1.0 / 3.0 , 1.0 / 3.0 , 1.0 / 3.0 ) ;
@@ -213,13 +231,13 @@ mod test {
213
231
fn intersect_3 ( ) {
214
232
let ( p, _v) = test_inputs ( ) ;
215
233
216
- let b = Plane :: new_with_reals ( 0.08 , 0.04 , 0.03 , 0.01 ) ;
217
- let c = Plane :: new_with_reals ( 0.05 , 0.2 , 0.1 , 0.6 ) ;
234
+ let b = Plane :: from_coordinates ( 0.08 , 0.04 , 0.03 , 0.01 ) ;
235
+ let c = Plane :: from_coordinates ( 0.05 , 0.2 , 0.1 , 0.6 ) ;
218
236
219
237
let expected = Vector3 :: new ( -1.707317 , 2.95122 , 0.95122 ) ;
220
238
221
- let d = Plane :: new_with_reals ( 0.01 , 0.02 , 0.4 , 0.16 ) ;
222
- let e = Plane :: new_with_reals ( 0.01 , 0.02 , 0.4 , 0.32 ) ;
239
+ let d = Plane :: from_coordinates ( 0.01 , 0.02 , 0.4 , 0.16 ) ;
240
+ let e = Plane :: from_coordinates ( 0.01 , 0.02 , 0.4 , 0.32 ) ;
223
241
224
242
assert ! ( p. intersect_3( b, c) . unwrap( ) . approx_eq( & expected) ) ;
225
243
assert_eq ! ( p. intersect_3( d, e) , None ) ;
@@ -266,7 +284,7 @@ mod test {
266
284
fn normalize ( ) {
267
285
let ( p, _v) = test_inputs ( ) ;
268
286
269
- assert ! ( p. normalize( ) . approx_eq( Plane :: new_with_reals (
287
+ assert ! ( p. normalize( ) . approx_eq( Plane :: from_coordinates (
270
288
0.218218 , 0.436436 , 0.872872 , 1.745743
271
289
) ) ) ;
272
290
}
0 commit comments