Skip to content

Commit 9f891c8

Browse files
committed
Renamed methods, changed tests to use approx_eq, fixed style issues, added test for from_points
1 parent f2740e8 commit 9f891c8

File tree

1 file changed

+39
-21
lines changed
  • gdnative-core/src/core_types/geom

1 file changed

+39
-21
lines changed

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

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,29 @@ impl Plane {
2424

2525
/// Creates a new `Plane` from the ['Vector3'](./type.Vector3.html) normal and the distance from the origin.
2626
#[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 }
3229
}
3330

3431
/// Creates a new `Plane` from four floats.
3532
/// a, b, c are used for the normal ['Vector3'](./type.Vector3.html).
3633
/// d is the distance from the origin.
3734
#[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 {
3936
Plane {
4037
normal: Vector3::new(a, b, c),
41-
d: d,
38+
d,
4239
}
4340
}
4441

4542
/// 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)`.
4644
#[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();
4947

5048
Plane {
51-
normal: normal,
49+
normal,
5250
d: normal.dot(a),
5351
}
5452
}
@@ -154,12 +152,12 @@ impl Plane {
154152
if l == 0.0 {
155153
self.normal = Vector3::new(0.0, 0.0, 0.0);
156154
self.d = 0.0;
157-
return self;
158155
} else {
159156
self.normal /= l;
160157
self.d /= l;
161-
return self;
162158
}
159+
160+
self
163161
}
164162

165163
/// Returns the orthogonal projection of `point` into a point in the `Plane`.
@@ -175,18 +173,38 @@ mod test {
175173

176174
fn test_inputs() -> (Plane, Vector3) {
177175
(
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),
179177
Vector3::new(0.16, 0.32, 0.64),
180178
)
181179
}
182180

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+
183201
#[test]
184202
fn center() {
185203
let (p, _v) = test_inputs();
186204

187205
let expected = Vector3::new(0.0008, 0.0016, 0.0032);
188206

189-
assert_eq!(p.center(), expected);
207+
assert!(p.center().approx_eq(&expected));
190208
}
191209

192210
#[test]
@@ -195,12 +213,12 @@ mod test {
195213

196214
let expected = -0.0464;
197215

198-
assert_eq!(p.distance_to(v), expected);
216+
assert!(p.distance_to(v).approx_eq(&expected));
199217
}
200218

201219
#[test]
202220
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);
204222

205223
let outside = Vector3::new(0.0, 0.0, 0.0);
206224
let inside = Vector3::new(1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0);
@@ -213,13 +231,13 @@ mod test {
213231
fn intersect_3() {
214232
let (p, _v) = test_inputs();
215233

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);
218236

219237
let expected = Vector3::new(-1.707317, 2.95122, 0.95122);
220238

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);
223241

224242
assert!(p.intersect_3(b, c).unwrap().approx_eq(&expected));
225243
assert_eq!(p.intersect_3(d, e), None);
@@ -266,7 +284,7 @@ mod test {
266284
fn normalize() {
267285
let (p, _v) = test_inputs();
268286

269-
assert!(p.normalize().approx_eq(Plane::new_with_reals(
287+
assert!(p.normalize().approx_eq(Plane::from_coordinates(
270288
0.218218, 0.436436, 0.872872, 1.745743
271289
)));
272290
}

0 commit comments

Comments
 (0)