Skip to content

Commit 43ce5c5

Browse files
committed
itest: rust: src: Plane integration tests implementation
1 parent 7dbd80f commit 43ce5c5

File tree

2 files changed

+237
-0
lines changed

2 files changed

+237
-0
lines changed

itest/rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod node_test;
2727
mod object_test;
2828
mod option_ffi_test;
2929
mod packed_array_test;
30+
mod plane_test;
3031
mod projection_test;
3132
mod quaternion_test;
3233
mod rect2i_test;

itest/rust/src/plane_test.rs

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*/
6+
7+
use std::fmt::Debug;
8+
9+
use crate::itest;
10+
use godot::builtin::inner::InnerPlane;
11+
use godot::builtin::{real, Plane, RealConv, ToVariant, Vector3};
12+
use godot::private::class_macros::assert_eq_approx;
13+
14+
fn check_mapping_eq<T>(context: &str, outer: T, inner: T)
15+
where
16+
T: PartialEq + Debug,
17+
{
18+
assert_eq!(
19+
outer, inner,
20+
"{context}: outer != inner ({outer:?} != {inner:?})"
21+
);
22+
}
23+
24+
fn check_mapping_eq_approx_plane(context: &str, outer: Plane, inner: Plane) {
25+
assert_eq_approx!(
26+
outer,
27+
inner,
28+
|a: Plane, b: Plane| a.is_equal_approx(&b),
29+
"{context}: outer != inner ({outer:?} != {inner:?})"
30+
);
31+
}
32+
33+
#[itest]
34+
fn plane_normalized() {
35+
let a = Plane::new(Vector3::new(9.5, 3.3, 2.2).normalized(), -0.1);
36+
let inner_a = InnerPlane::from_outer(&a);
37+
check_mapping_eq_approx_plane("normalized", a.normalized(), inner_a.normalized());
38+
39+
let a = Plane {
40+
normal: Vector3::new(4.2, 2.9, 1.5),
41+
d: 2.4,
42+
};
43+
let inner_a = InnerPlane::from_outer(&a);
44+
check_mapping_eq_approx_plane("normalized", a.normalized(), inner_a.normalized());
45+
}
46+
47+
#[itest]
48+
fn plane_center() {
49+
let a = Plane::new(Vector3::new(0.5, 2.0, 2.5).normalized(), 1.0);
50+
let inner_a = InnerPlane::from_outer(&a);
51+
check_mapping_eq("center", a.center(), inner_a.get_center());
52+
}
53+
54+
#[itest]
55+
fn plane_is_finite() {
56+
let a = Plane::new(Vector3::new(9.4, -1.2, 3.0).normalized(), 1.5);
57+
let inner_a = InnerPlane::from_outer(&a);
58+
check_mapping_eq("is_finite", a.is_finite(), inner_a.is_finite());
59+
60+
let a = Plane::new(Vector3::new(real::INFINITY, -0.3, 3.0).normalized(), -1.5);
61+
let inner_a = InnerPlane::from_outer(&a);
62+
check_mapping_eq("is_finite", a.is_finite(), inner_a.is_finite());
63+
}
64+
65+
#[itest]
66+
fn plane_distance_to() {
67+
let a = Plane::new(Vector3::BACK, 0.2);
68+
let inner_a = InnerPlane::from_outer(&a);
69+
let b = Vector3::new(7.0, 9.0, 2.0);
70+
check_mapping_eq(
71+
"distance_to",
72+
a.distance_to(b).as_f64(),
73+
inner_a.distance_to(b),
74+
);
75+
76+
let b = Vector3::new(-7.0, -9.0, -2.0);
77+
check_mapping_eq(
78+
"distance_to",
79+
a.distance_to(b).as_f64(),
80+
inner_a.distance_to(b),
81+
);
82+
}
83+
84+
#[itest]
85+
fn plane_is_point_over() {
86+
let a = Plane::new(Vector3::BACK, 1.1);
87+
let inner_a = InnerPlane::from_outer(&a);
88+
let b = Vector3::new(6.7, 8.4, 8.3);
89+
check_mapping_eq(
90+
"is_point_over",
91+
a.is_point_over(b),
92+
inner_a.is_point_over(b),
93+
);
94+
95+
let b = Vector3::new(-0.5, -1.2, -7.1);
96+
check_mapping_eq(
97+
"is_point_over",
98+
a.is_point_over(b),
99+
inner_a.is_point_over(b),
100+
);
101+
}
102+
103+
#[itest]
104+
fn plane_project() {
105+
let a = Plane::new(Vector3::new(8.6, -1.1, -10.1).normalized(), 3.3);
106+
let inner_a = InnerPlane::from_outer(&a);
107+
let b = Vector3::new(7.2, -3.4, 9.9);
108+
check_mapping_eq("project", a.project(b), inner_a.project(b));
109+
}
110+
111+
#[itest]
112+
fn plane_intersect_segment() {
113+
let a = Plane::new(Vector3::BACK, -0.2);
114+
let inner_a = InnerPlane::from_outer(&a);
115+
let b = Vector3::new(7.2, 10.4, 9.9);
116+
let c = Vector3::new(-9.4, -3.4, -3.1);
117+
check_mapping_eq(
118+
"intersect_segment",
119+
a.intersect_segment(b, c)
120+
.as_ref()
121+
.map(ToVariant::to_variant)
122+
.unwrap_or_default(),
123+
inner_a.intersects_segment(b, c),
124+
);
125+
126+
let a = Plane::new(Vector3::BACK, 0.0);
127+
let inner_a = InnerPlane::from_outer(&a);
128+
let b = Vector3::BACK;
129+
let c = Vector3::new(0.0, 0.0, 0.5);
130+
check_mapping_eq(
131+
"intersect_segment",
132+
a.intersect_segment(b, c)
133+
.as_ref()
134+
.map(ToVariant::to_variant)
135+
.unwrap_or_default(),
136+
inner_a.intersects_segment(b, c),
137+
);
138+
}
139+
140+
#[itest]
141+
fn plane_intersect_ray() {
142+
let a = Plane::new(Vector3::BACK, 0.0);
143+
let inner_a = InnerPlane::from_outer(&a);
144+
let b = Vector3::new(0.1, 9.9, 5.7);
145+
let c = Vector3::new(3.5, 3.2, 0.3);
146+
check_mapping_eq(
147+
"intersect_ray",
148+
a.intersect_ray(b, c)
149+
.as_ref()
150+
.map(ToVariant::to_variant)
151+
.unwrap_or_default(),
152+
inner_a.intersects_ray(b, c),
153+
);
154+
155+
let b = Vector3::BACK;
156+
let c = Vector3::new(1.0, 0.0, 1.0);
157+
check_mapping_eq(
158+
"intersect_ray",
159+
a.intersect_ray(b, c)
160+
.as_ref()
161+
.map(ToVariant::to_variant)
162+
.unwrap_or_default(),
163+
inner_a.intersects_ray(b, c),
164+
);
165+
}
166+
167+
#[itest]
168+
fn plane_contains_point() {
169+
let a = Plane::new(Vector3::new(0.9, 6.6, 0.1).normalized(), 0.0001);
170+
let inner_a = InnerPlane::from_outer(&a);
171+
let b = Vector3::ZERO;
172+
let c: real = 0.01;
173+
check_mapping_eq(
174+
"contains_point",
175+
a.contains_point(b, Some(c)),
176+
inner_a.has_point(b, c.as_f64()),
177+
);
178+
179+
let a = Plane::new(Vector3::new(0.9, 6.6, 0.1).normalized(), 0.1);
180+
let inner_a = InnerPlane::from_outer(&a);
181+
check_mapping_eq(
182+
"contains_point",
183+
a.contains_point(b, Some(c)),
184+
inner_a.has_point(b, c.as_f64()),
185+
);
186+
}
187+
188+
#[itest]
189+
fn plane_is_equal_approx() {
190+
let a = Plane::new(Vector3::new(1.5, 6.3, 2.2).normalized(), 5.2);
191+
let inner_a = InnerPlane::from_outer(&a);
192+
let b = Plane::new(Vector3::new(1.5, 6.3, 2.2).normalized(), 5.2000001);
193+
check_mapping_eq(
194+
"is_equal_approx",
195+
a.is_equal_approx(&b),
196+
inner_a.is_equal_approx(b),
197+
);
198+
199+
let a = Plane::new(Vector3::new(-1.9, 9.0, 2.7).normalized(), 5.4);
200+
let inner_a = InnerPlane::from_outer(&a);
201+
let b = Plane::new(Vector3::new(0.0, 6.2, 2.5).normalized(), 0.4);
202+
check_mapping_eq(
203+
"is_equal_approx",
204+
a.is_equal_approx(&b),
205+
inner_a.is_equal_approx(b),
206+
);
207+
}
208+
209+
#[itest]
210+
fn plane_intersect_3() {
211+
let a = Plane::new(Vector3::new(1.0, 2.0, 0.0).normalized(), 0.0);
212+
let inner_a = InnerPlane::from_outer(&a);
213+
let b = Plane::new(Vector3::new(3.5, 6.0, -3.0).normalized(), 0.0);
214+
let c = Plane::new(Vector3::new(-1.0, 6.0, 0.5).normalized(), 0.0);
215+
check_mapping_eq(
216+
"intersect_3",
217+
a.intersect_3(&b, &c)
218+
.as_ref()
219+
.map(ToVariant::to_variant)
220+
.unwrap_or_default(),
221+
inner_a.intersect_3(b, c),
222+
);
223+
224+
let a = Plane::new(Vector3::new(1.5, 6.3, 2.2).normalized(), 5.2);
225+
let inner_a = InnerPlane::from_outer(&a);
226+
let b = Plane::new(Vector3::new(1.5, 6.3, 2.2).normalized(), 3.2);
227+
let c = Plane::new(Vector3::new(1.5, 6.3, 2.2).normalized(), 9.5);
228+
check_mapping_eq(
229+
"intersect_3",
230+
a.intersect_3(&b, &c)
231+
.as_ref()
232+
.map(ToVariant::to_variant)
233+
.unwrap_or_default(),
234+
inner_a.intersect_3(b, c),
235+
);
236+
}

0 commit comments

Comments
 (0)