Debian Codegraph @

third_party/rust/plane-split/tests/main.rs

raw
1 use euclid::{
2 approxeq::ApproxEq,
3 default::{Rect, Size2D, Transform3D},
4 point2, point3, vec3, Angle,
5 };
6 use plane_split::{Intersection, Line, LineProjection, NegativeHemisphereError, Plane, Polygon};
7
8 #[test]
9 fn line_proj_bounds() {
10 assert_eq!(
11 (-5.0f64, 4.0),
12 LineProjection {
13 markers: [-5.0f64, 1.0, 4.0, 2.0]
14 }
15 .get_bounds()
16 );
17 assert_eq!(
18 (1f64, 4.0),
19 LineProjection {
20 markers: [4f64, 3.0, 2.0, 1.0]
21 }
22 .get_bounds()
23 );
24 }
25
26 #[test]
27 fn valid() {
28 let poly_a: Polygon<usize> = Polygon {
29 points: [
30 point3(0.0, 0.0, 0.0),
31 point3(1.0, 1.0, 1.0),
32 point3(1.0, 1.0, 0.0),
33 point3(0.0, 1.0, 1.0),
34 ],
35 plane: Plane {
36 normal: vec3(0.0, 1.0, 0.0),
37 offset: -1.0,
38 },
39 anchor: 0,
40 };
41 assert!(!poly_a.is_valid()); // points[0] is outside
42 let poly_b: Polygon<usize> = Polygon {
43 points: [
44 point3(0.0, 1.0, 0.0),
45 point3(1.0, 1.0, 1.0),
46 point3(1.0, 1.0, 0.0),
47 point3(0.0, 1.0, 1.0),
48 ],
49 plane: Plane {
50 normal: vec3(0.0, 1.0, 0.0),
51 offset: -1.0,
52 },
53 anchor: 0,
54 };
55 assert!(!poly_b.is_valid()); // winding is incorrect
56 let poly_c: Polygon<usize> = Polygon {
57 points: [
58 point3(0.0, 0.0, 1.0),
59 point3(1.0, 0.0, 1.0),
60 point3(1.0, 1.0, 1.0),
61 point3(0.0, 1.0, 1.0),
62 ],
63 plane: Plane {
64 normal: vec3(0.0, 0.0, 1.0),
65 offset: -1.0,
66 },
67 anchor: 0,
68 };
69 assert!(poly_c.is_valid());
70 }
71
72 #[test]
73 fn empty() {
74 let poly = Polygon::from_points(
75 [
76 point3(0.0, 0.0, 1.0),
77 point3(0.0, 0.0, 1.0),
78 point3(0.0, 0.00000001, 1.0),
79 point3(1.0, 0.0, 0.0),
80 ],
81 1usize,
82 );
83 assert_eq!(None, poly);
84 }
85
86 fn test_transformed(rect: Rect<f64>, transform: Transform3D<f64>) {
87 let poly = Polygon::from_transformed_rect(rect, transform, 0).unwrap();
88 assert!(poly.is_valid());
89
90 let inv_transform = transform.inverse().unwrap();
91 let poly2 =
92 Polygon::from_transformed_rect_with_inverse(rect, &transform, &inv_transform, 0).unwrap();
93 assert_eq!(poly.points, poly2.points);
94 assert!(poly.plane.offset.approx_eq(&poly2.plane.offset));
95 assert!(poly.plane.normal.dot(poly2.plane.normal).approx_eq(&1.0));
96 }
97
98 #[test]
99 fn from_transformed_rect() {
100 let rect = Rect::new(point2(10.0, 10.0), Size2D::new(20.0, 30.0));
101 let transform = Transform3D::rotation(0.5f64.sqrt(), 0.0, 0.5f64.sqrt(), Angle::radians(5.0))
102 .pre_translate(vec3(0.0, 0.0, 10.0));
103 test_transformed(rect, transform);
104 }
105
106 #[test]
107 fn from_transformed_rect_perspective() {
108 let rect = Rect::new(point2(-10.0, -5.0), Size2D::new(20.0, 30.0));
109 let mut transform = Transform3D::perspective(400.0).pre_translate(vec3(0.0, 0.0, 100.0));
110 transform.m44 = 0.7; //for fun
111 test_transformed(rect, transform);
112 }
113
114 #[test]
115 fn untransform_point() {
116 let poly: Polygon<usize> = Polygon {
117 points: [
118 point3(0.0, 0.0, 0.0),
119 point3(0.5, 1.0, 0.0),
120 point3(1.5, 1.0, 0.0),
121 point3(1.0, 0.0, 0.0),
122 ],
123 plane: Plane {
124 normal: vec3(0.0, 1.0, 0.0),
125 offset: 0.0,
126 },
127 anchor: 0,
128 };
129 assert_eq!(poly.untransform_point(poly.points[0]), point2(0.0, 0.0));
130 assert_eq!(poly.untransform_point(poly.points[1]), point2(1.0, 0.0));
131 assert_eq!(poly.untransform_point(poly.points[2]), point2(1.0, 1.0));
132 assert_eq!(poly.untransform_point(poly.points[3]), point2(0.0, 1.0));
133 }
134
135 #[test]
136 fn are_outside() {
137 let plane = Plane {
138 normal: vec3(0.0, 0.0, 1.0),
139 offset: -1.0,
140 };
141 assert!(plane.are_outside(&[point3(0.0, 0.0, 1.1), point3(1.0, 1.0, 2.0),]));
142 assert!(plane.are_outside(&[point3(0.5, 0.5, 1.0),]));
143 assert!(!plane.are_outside(&[point3(0.0, 0.0, 1.0), point3(0.0, 0.0, -1.0),]));
144 }
145
146 #[test]
147 fn intersect() {
148 let poly_a: Polygon<usize> = Polygon {
149 points: [
150 point3(0.0, 0.0, 1.0),
151 point3(1.0, 0.0, 1.0),
152 point3(1.0, 1.0, 1.0),
153 point3(0.0, 1.0, 1.0),
154 ],
155 plane: Plane {
156 normal: vec3(0.0, 0.0, 1.0),
157 offset: -1.0,
158 },
159 anchor: 0,
160 };
161 assert!(poly_a.is_valid());
162 let poly_b: Polygon<usize> = Polygon {
163 points: [
164 point3(0.5, 0.0, 2.0),
165 point3(0.5, 1.0, 2.0),
166 point3(0.5, 1.0, 0.0),
167 point3(0.5, 0.0, 0.0),
168 ],
169 plane: Plane {
170 normal: vec3(1.0, 0.0, 0.0),
171 offset: -0.5,
172 },
173 anchor: 0,
174 };
175 assert!(poly_b.is_valid());
176
177 let intersection = match poly_a.intersect(&poly_b) {
178 Intersection::Inside(result) => result,
179 _ => panic!("Bad intersection"),
180 };
181 assert!(intersection.is_valid());
182 // confirm the origin is on both planes
183 assert!(poly_a
184 .plane
185 .signed_distance_to(&intersection.origin)
186 .approx_eq(&0.0));
187 assert!(poly_b
188 .plane
189 .signed_distance_to(&intersection.origin)
190 .approx_eq(&0.0));
191 // confirm the direction is coplanar to both planes
192 assert!(poly_a.plane.normal.dot(intersection.dir).approx_eq(&0.0));
193 assert!(poly_b.plane.normal.dot(intersection.dir).approx_eq(&0.0));
194
195 let poly_c: Polygon<usize> = Polygon {
196 points: [
197 point3(0.0, -1.0, 2.0),
198 point3(0.0, -1.0, 0.0),
199 point3(0.0, 0.0, 0.0),
200 point3(0.0, 0.0, 2.0),
201 ],
202 plane: Plane {
203 normal: vec3(1.0, 0.0, 0.0),
204 offset: 0.0,
205 },
206 anchor: 0,
207 };
208 assert!(poly_c.is_valid());
209 let poly_d: Polygon<usize> = Polygon {
210 points: [
211 point3(0.0, 0.0, 0.5),
212 point3(1.0, 0.0, 0.5),
213 point3(1.0, 1.0, 0.5),
214 point3(0.0, 1.0, 0.5),
215 ],
216 plane: Plane {
217 normal: vec3(0.0, 0.0, 1.0),
218 offset: -0.5,
219 },
220 anchor: 0,
221 };
222 assert!(poly_d.is_valid());
223
224 assert!(poly_a.intersect(&poly_c).is_outside());
225 assert!(poly_a.intersect(&poly_d).is_outside());
226 }
227
228 fn test_cut(poly_base: &Polygon<usize>, extra_count: u8, line: Line) {
229 assert!(line.is_valid());
230
231 let normal = poly_base.plane.normal.cross(line.dir).normalize();
232 let mut poly = poly_base.clone();
233 let (extra1, extra2) = poly.split_with_normal(&line, &normal);
234 assert!(poly.is_valid() && poly_base.contains(&poly));
235 assert_eq!(extra_count > 0, extra1.is_some());
236 assert_eq!(extra_count > 1, extra2.is_some());
237 if let Some(extra) = extra1 {
238 assert!(extra.is_valid() && poly_base.contains(&extra));
239 }
240 if let Some(extra) = extra2 {
241 assert!(extra.is_valid() && poly_base.contains(&extra));
242 }
243 }
244
245 #[test]
246 fn split() {
247 let poly: Polygon<usize> = Polygon {
248 points: [
249 point3(0.0, 1.0, 0.0),
250 point3(1.0, 1.0, 0.0),
251 point3(1.0, 1.0, 1.0),
252 point3(0.0, 1.0, 1.0),
253 ],
254 plane: Plane {
255 normal: vec3(0.0, 1.0, 0.0),
256 offset: -1.0,
257 },
258 anchor: 0,
259 };
260
261 // non-intersecting line
262 test_cut(
263 &poly,
264 0,
265 Line {
266 origin: point3(0.0, 1.0, 0.5),
267 dir: vec3(0.0, 1.0, 0.0),
268 },
269 );
270
271 // simple cut (diff=2)
272 test_cut(
273 &poly,
274 1,
275 Line {
276 origin: point3(0.0, 1.0, 0.5),
277 dir: vec3(1.0, 0.0, 0.0),
278 },
279 );
280
281 // complex cut (diff=1, wrapped)
282 test_cut(
283 &poly,
284 2,
285 Line {
286 origin: point3(0.0, 1.0, 0.5),
287 dir: vec3(0.5f64.sqrt(), 0.0, -0.5f64.sqrt()),
288 },
289 );
290
291 // complex cut (diff=1, non-wrapped)
292 test_cut(
293 &poly,
294 2,
295 Line {
296 origin: point3(0.5, 1.0, 0.0),
297 dir: vec3(0.5f64.sqrt(), 0.0, 0.5f64.sqrt()),
298 },
299 );
300
301 // complex cut (diff=3)
302 test_cut(
303 &poly,
304 2,
305 Line {
306 origin: point3(0.5, 1.0, 0.0),
307 dir: vec3(-0.5f64.sqrt(), 0.0, 0.5f64.sqrt()),
308 },
309 );
310
311 // perfect diagonal
312 test_cut(
313 &poly,
314 1,
315 Line {
316 origin: point3(0.0, 1.0, 0.0),
317 dir: vec3(0.5f64.sqrt(), 0.0, 0.5f64.sqrt()),
318 },
319 );
320 }
321
322 #[test]
323 fn plane_unnormalized() {
324 let zero_vec = vec3(0.0000001, 0.0, 0.0);
325 let mut plane: Result<Option<Plane>, _> = Plane::from_unnormalized(zero_vec, 1.0);
326 assert_eq!(plane, Ok(None));
327 plane = Plane::from_unnormalized(zero_vec, 0.0);
328 assert_eq!(plane, Err(NegativeHemisphereError));
329 plane = Plane::from_unnormalized(zero_vec, -0.5);
330 assert_eq!(plane, Err(NegativeHemisphereError));
331
332 let plane = Plane::from_unnormalized(vec3(-3.0, 4.0, 0.0), 2.0)
333 .unwrap()
334 .unwrap();
335 let expected = Plane {
336 normal: vec3(-3.0 / 5.0, 4.0 / 5.0, 0.0),
337 offset: 2.0 / 5.0,
338 };
339 assert!(plane.normal.approx_eq(&expected.normal));
340 assert!(plane.offset.approx_eq(&expected.offset));
341 }