Debian Codegraph @

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

raw
1 use euclid::{
2 default::{Rect, Transform3D},
3 rect, vec3, Angle,
4 };
5 use plane_split::PlaneCut;
6 use plane_split::{make_grid, BspSplitter, Polygon};
7 use std::f64::consts::FRAC_PI_4;
8
9 fn grid_impl(count: usize, splitter: &mut BspSplitter<usize>) {
10 let polys = make_grid(count);
11 let result = splitter.solve(&polys, vec3(0.0, 0.0, 1.0));
12 assert_eq!(result.len(), count + count * count + count * count * count);
13 }
14
15 #[test]
16 fn grid_bsp() {
17 grid_impl(2, &mut BspSplitter::new());
18 }
19
20 fn sort_rotation(splitter: &mut BspSplitter<usize>) {
21 let transform0: Transform3D<f64> =
22 Transform3D::rotation(0.0, 1.0, 0.0, Angle::radians(-FRAC_PI_4));
23 let transform1: Transform3D<f64> = Transform3D::rotation(0.0, 1.0, 0.0, Angle::radians(0.0));
24 let transform2: Transform3D<f64> =
25 Transform3D::rotation(0.0, 1.0, 0.0, Angle::radians(FRAC_PI_4));
26
27 let rect: Rect<f64> = rect(-10.0, -10.0, 20.0, 20.0);
28 let p1 = Polygon::from_transformed_rect(rect, transform0, 0);
29 let p2 = Polygon::from_transformed_rect(rect, transform1, 1);
30 let p3 = Polygon::from_transformed_rect(rect, transform2, 2);
31 assert!(
32 p1.is_some() && p2.is_some() && p3.is_some(),
33 "Cannot construct transformed polygons"
34 );
35
36 let polys = [p1.unwrap(), p2.unwrap(), p3.unwrap()];
37 let result = splitter.solve(&polys, vec3(0.0, 0.0, -1.0));
38 let ids: Vec<_> = result.iter().map(|poly| poly.anchor).collect();
39 assert_eq!(&ids, &[2, 1, 0, 1, 2]);
40 }
41
42 #[test]
43 fn rotation_bsp() {
44 sort_rotation(&mut BspSplitter::new());
45 }
46
47 fn sort_trivial(splitter: &mut BspSplitter<usize>) {
48 let anchors: Vec<_> = (0usize..10).collect();
49 let rect: Rect<f64> = rect(-10.0, -10.0, 20.0, 20.0);
50 let polys: Vec<_> = anchors
51 .iter()
52 .map(|&anchor| {
53 let transform: Transform3D<f64> = Transform3D::translation(0.0, 0.0, anchor as f64);
54 let poly = Polygon::from_transformed_rect(rect, transform, anchor);
55 assert!(poly.is_some(), "Cannot construct transformed polygons");
56 poly.unwrap()
57 })
58 .collect();
59
60 let result = splitter.solve(&polys, vec3(0.0, 0.0, -1.0));
61 let anchors1: Vec<_> = result.iter().map(|p| p.anchor).collect();
62 let mut anchors2 = anchors1.clone();
63 anchors2.sort_by_key(|&a| -(a as i32));
64 //make sure Z is sorted backwards
65 assert_eq!(anchors1, anchors2);
66 }
67
68 fn sort_external(splitter: &mut BspSplitter<usize>) {
69 let rect0: Rect<f64> = rect(-10.0, -10.0, 20.0, 20.0);
70 let poly0 = Polygon::from_rect(rect0, 0);
71 let poly1 = {
72 let transform0: Transform3D<f64> =
73 Transform3D::rotation(1.0, 0.0, 0.0, Angle::radians(2.0 * FRAC_PI_4));
74 let transform1: Transform3D<f64> = Transform3D::translation(0.0, 100.0, 0.0);
75 Polygon::from_transformed_rect(rect0, transform0.then(&transform1), 1).unwrap()
76 };
77
78 let result = splitter.solve(&[poly0, poly1], vec3(1.0, 1.0, 0.0).normalize());
79 let anchors: Vec<_> = result.iter().map(|p| p.anchor).collect();
80 // make sure the second polygon is split in half around the plane of the first one,
81 // even if geometrically their polygons don't intersect.
82 assert_eq!(anchors, vec![1, 0, 1]);
83 }
84
85 #[test]
86 fn trivial_bsp() {
87 sort_trivial(&mut BspSplitter::new());
88 }
89
90 #[test]
91 fn external_bsp() {
92 sort_external(&mut BspSplitter::new());
93 }
94
95 #[test]
96 fn test_cut() {
97 use smallvec::SmallVec;
98 let rect: Rect<f64> = rect(-10.0, -10.0, 20.0, 20.0);
99 let poly = Polygon::from_rect(rect, 0);
100 let mut poly2 = Polygon::from_rect(rect, 0);
101 // test robustness for positions
102 for p in &mut poly2.points {
103 p.z += 0.00000001;
104 }
105
106 let mut front: SmallVec<[Polygon<i32>; 2]> = SmallVec::new();
107 let mut back: SmallVec<[Polygon<i32>; 2]> = SmallVec::new();
108
109 assert_eq!(poly.cut(&poly2, &mut front, &mut back), PlaneCut::Sibling);
110 assert!(front.is_empty());
111 assert!(back.is_empty());
112
113 // test robustness for normal
114 poly2.plane.normal.z += 0.00000001;
115 assert_eq!(poly.cut(&poly2, &mut front, &mut back), PlaneCut::Sibling);
116 assert!(front.is_empty());
117 assert!(back.is_empty());
118
119 // test opposite normal handling
120 poly2.plane.normal *= -1.0;
121 assert_eq!(poly.cut(&poly2, &mut front, &mut back), PlaneCut::Sibling);
122 assert!(front.is_empty());
123 assert!(back.is_empty());
124
125 // test grouping front
126 poly2.plane.offset += 0.1;
127 assert_eq!(poly.cut(&poly2, &mut front, &mut back), PlaneCut::Cut);
128 assert_eq!(front.len(), 1);
129 assert!(back.is_empty());
130
131 front.clear();
132
133 // test grouping back
134 poly2.plane.normal *= -1.0;
135 assert_eq!(poly.cut(&poly2, &mut front, &mut back), PlaneCut::Cut);
136 assert_eq!(back.len(), 1);
137 assert!(front.is_empty());
138 }