Skip to content

Commit 2bf4e93

Browse files
committed
Add error checking for from_points
1 parent 9f891c8 commit 2bf4e93

File tree

1 file changed

+12
-15
lines changed
  • gdnative-core/src/core_types/geom

1 file changed

+12
-15
lines changed

gdnative-core/src/core_types/geom/plane.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,19 @@ impl Plane {
4040
}
4141

4242
/// 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`.
4444
#[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> {
4646
let normal = (a - c).cross(a - b).normalize();
4747

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+
}
5256
}
5357

5458
/// Returns the center of the `Plane`.
@@ -185,17 +189,10 @@ mod test {
185189
let c = Vector3::new(1.0, 1.0, 1.0);
186190
let d = Vector3::new(-1.0, -1.0, 0.0);
187191

188-
let test_collinear = Plane::from_points(a, b, d);
189-
190192
let expected_valid = Plane::from_coordinates(0.447214, 0.0, -0.894427, -0.447214);
191193

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);
199196
}
200197

201198
#[test]

0 commit comments

Comments
 (0)