@@ -40,15 +40,19 @@ impl Plane {
40
40
}
41
41
42
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) `.
43
+ /// If all three points are collinear, returns `None `.
44
44
#[ inline]
45
- pub fn from_points ( a : Vector3 , b : Vector3 , c : Vector3 ) -> Plane {
45
+ pub fn from_points ( a : Vector3 , b : Vector3 , c : Vector3 ) -> Option < Plane > {
46
46
let normal = ( a - c) . cross ( a - b) . normalize ( ) ;
47
47
48
- Plane {
49
- normal,
50
- d : normal. dot ( a) ,
51
- }
48
+ if normal. x . is_nan ( ) || normal. y . is_nan ( ) || normal. z . is_nan ( ) {
49
+ None
50
+ } else {
51
+ Some ( Plane {
52
+ normal,
53
+ d : normal. dot ( a) ,
54
+ } )
55
+ }
52
56
}
53
57
54
58
/// Returns the center of the `Plane`.
@@ -185,17 +189,10 @@ mod test {
185
189
let c = Vector3 :: new ( 1.0 , 1.0 , 1.0 ) ;
186
190
let d = Vector3 :: new ( -1.0 , -1.0 , 0.0 ) ;
187
191
188
- let test_collinear = Plane :: from_points ( a, b, d) ;
189
-
190
192
let expected_valid = Plane :: from_coordinates ( 0.447214 , 0.0 , -0.894427 , -0.447214 ) ;
191
193
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
- ) ;
194
+ assert ! ( Plane :: from_points( a, b, c) . unwrap( ) . approx_eq( expected_valid) ) ;
195
+ assert_eq ! ( Plane :: from_points( a, b, d) , None ) ;
199
196
}
200
197
201
198
#[ test]
0 commit comments