1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
use euclid::{
default::{Rect, Transform3D},
point3, rect, vec3, Angle,
};
use plane_split::{Clipper, Plane, Polygon};
use std::f64::consts::FRAC_PI_4;
#[test]
fn clip_in() {
let plane = Plane::from_unnormalized(vec3(1.0, 0.0, 1.0), 20.0)
.unwrap()
.unwrap();
let mut clipper = Clipper::new();
clipper.add(plane);
let poly = Polygon::from_points(
[
point3(-10.0, -10.0, 0.0),
point3(10.0, -10.0, 0.0),
point3(10.0, 10.0, 0.0),
point3(-10.0, 10.0, 0.0),
],
0,
)
.unwrap();
let results = clipper.clip(poly.clone());
assert_eq!(results[0], poly);
assert_eq!(results.len(), 1);
}
#[test]
fn clip_out() {
let plane = Plane::from_unnormalized(vec3(1.0, 0.0, 1.0), -20.0)
.unwrap()
.unwrap();
let mut clipper = Clipper::new();
clipper.add(plane);
let poly = Polygon::from_points(
[
point3(-10.0, -10.0, 0.0),
point3(10.0, -10.0, 0.0),
point3(10.0, 10.0, 0.0),
point3(-10.0, 10.0, 0.0),
],
0,
)
.unwrap();
let results = clipper.clip(poly);
assert!(results.is_empty());
}
#[test]
fn clip_parallel() {
let plane = Plane {
normal: vec3(0.0, 0.0, 1.0),
offset: 0.0,
};
let mut clipper = Clipper::new();
clipper.add(plane);
let poly = Polygon::from_points(
[
point3(-10.0, -10.0, 0.0),
point3(10.0, -10.0, 0.0),
point3(10.0, 10.0, 0.0),
point3(-10.0, 10.0, 0.0),
],
0,
)
.unwrap();
let results = clipper.clip(poly);
assert!(results.is_empty());
}
#[test]
fn clip_repeat() {
let plane = Plane::from_unnormalized(vec3(1.0, 0.0, 1.0), 0.0)
.unwrap()
.unwrap();
let mut clipper = Clipper::new();
clipper.add(plane.clone());
clipper.add(plane.clone());
let poly = Polygon::from_points(
[
point3(-10.0, -10.0, 0.0),
point3(10.0, -10.0, 0.0),
point3(10.0, 10.0, 0.0),
point3(-10.0, 10.0, 0.0),
],
0,
)
.unwrap();
let results = clipper.clip(poly);
assert_eq!(results.len(), 1);
assert!(plane.signed_distance_sum_to(&results[0]) > 0.0);
}
#[test]
fn clip_transformed() {
let t_rot: Transform3D<f64> = Transform3D::rotation(0.0, 1.0, 0.0, Angle::radians(-FRAC_PI_4));
let t_div: Transform3D<f64> = Transform3D::perspective(5.0);
let transform = t_rot.then(&t_div);
let polygon = Polygon::from_rect(rect(-10.0, -10.0, 20.0, 20.0), 0);
let bounds: Rect<f64> = rect(-1.0, -1.0, 2.0, 2.0);
let mut clipper = Clipper::new();
let results = clipper.clip_transformed(polygon, &transform, Some(bounds));
// iterating enforces the transformation checks/unwraps
assert_ne!(0, results.unwrap().count());
}
#[test]
fn clip_badly_transformed() {
let mut tx = Transform3D::<f64>::identity();
tx.m14 = -0.0000001;
tx.m44 = 0.0;
let mut clipper = Clipper::new();
let polygon = Polygon::from_rect(rect(-10.0, -10.0, 20.0, 20.0), 0);
let results = clipper.clip_transformed(polygon, &tx, None);
assert!(results.is_err());
}
#[test]
fn clip_near_coplanar() {
let tx = Transform3D::<f64>::new(
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, -960.0, -625.0, 1.0, -1.0, 100.0, -2852.0, 0.0, 1.0,
);
let mut clipper = Clipper::new();
let polygon = Polygon::from_rect(rect(0.0, 0.0, 1703.0, 4020.0), 0);
let bounds1 = rect(0.0, -430.0, 2048.0, 2048.0);
let results1 = clipper.clip_transformed(polygon.clone(), &tx, Some(bounds1));
assert_ne!(0, results1.unwrap().count());
let bounds2 = rect(0.0, 0.0, 816.0, 1039.0);
let results2 = clipper.clip_transformed(polygon, &tx, Some(bounds2));
assert_ne!(0, results2.unwrap().count());
}
|