summaryrefslogtreecommitdiffstats
path: root/third_party/rust/plane-split/tests/main.rs
blob: 0bcac5a179b3eb2a648563f9c079a3c9eb5a54d9 (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use euclid::{
    approxeq::ApproxEq,
    default::{Rect, Size2D, Transform3D},
    point2, point3, vec3, Angle,
};
use plane_split::{Intersection, Line, LineProjection, NegativeHemisphereError, Plane, Polygon};

#[test]
fn line_proj_bounds() {
    assert_eq!(
        (-5.0f64, 4.0),
        LineProjection {
            markers: [-5.0f64, 1.0, 4.0, 2.0]
        }
        .get_bounds()
    );
    assert_eq!(
        (1f64, 4.0),
        LineProjection {
            markers: [4f64, 3.0, 2.0, 1.0]
        }
        .get_bounds()
    );
}

#[test]
fn valid() {
    let poly_a: Polygon<usize> = Polygon {
        points: [
            point3(0.0, 0.0, 0.0),
            point3(1.0, 1.0, 1.0),
            point3(1.0, 1.0, 0.0),
            point3(0.0, 1.0, 1.0),
        ],
        plane: Plane {
            normal: vec3(0.0, 1.0, 0.0),
            offset: -1.0,
        },
        anchor: 0,
    };
    assert!(!poly_a.is_valid()); // points[0] is outside
    let poly_b: Polygon<usize> = Polygon {
        points: [
            point3(0.0, 1.0, 0.0),
            point3(1.0, 1.0, 1.0),
            point3(1.0, 1.0, 0.0),
            point3(0.0, 1.0, 1.0),
        ],
        plane: Plane {
            normal: vec3(0.0, 1.0, 0.0),
            offset: -1.0,
        },
        anchor: 0,
    };
    assert!(!poly_b.is_valid()); // winding is incorrect
    let poly_c: Polygon<usize> = Polygon {
        points: [
            point3(0.0, 0.0, 1.0),
            point3(1.0, 0.0, 1.0),
            point3(1.0, 1.0, 1.0),
            point3(0.0, 1.0, 1.0),
        ],
        plane: Plane {
            normal: vec3(0.0, 0.0, 1.0),
            offset: -1.0,
        },
        anchor: 0,
    };
    assert!(poly_c.is_valid());
}

#[test]
fn empty() {
    let poly = Polygon::from_points(
        [
            point3(0.0, 0.0, 1.0),
            point3(0.0, 0.0, 1.0),
            point3(0.0, 0.00000001, 1.0),
            point3(1.0, 0.0, 0.0),
        ],
        1usize,
    );
    assert_eq!(None, poly);
}

fn test_transformed(rect: Rect<f64>, transform: Transform3D<f64>) {
    let poly = Polygon::from_transformed_rect(rect, transform, 0).unwrap();
    assert!(poly.is_valid());

    let inv_transform = transform.inverse().unwrap();
    let poly2 =
        Polygon::from_transformed_rect_with_inverse(rect, &transform, &inv_transform, 0).unwrap();
    assert_eq!(poly.points, poly2.points);
    assert!(poly.plane.offset.approx_eq(&poly2.plane.offset));
    assert!(poly.plane.normal.dot(poly2.plane.normal).approx_eq(&1.0));
}

#[test]
fn from_transformed_rect() {
    let rect = Rect::new(point2(10.0, 10.0), Size2D::new(20.0, 30.0));
    let transform = Transform3D::rotation(0.5f64.sqrt(), 0.0, 0.5f64.sqrt(), Angle::radians(5.0))
        .pre_translate(vec3(0.0, 0.0, 10.0));
    test_transformed(rect, transform);
}

#[test]
fn from_transformed_rect_perspective() {
    let rect = Rect::new(point2(-10.0, -5.0), Size2D::new(20.0, 30.0));
    let mut transform = Transform3D::perspective(400.0).pre_translate(vec3(0.0, 0.0, 100.0));
    transform.m44 = 0.7; //for fun
    test_transformed(rect, transform);
}

#[test]
fn untransform_point() {
    let poly: Polygon<usize> = Polygon {
        points: [
            point3(0.0, 0.0, 0.0),
            point3(0.5, 1.0, 0.0),
            point3(1.5, 1.0, 0.0),
            point3(1.0, 0.0, 0.0),
        ],
        plane: Plane {
            normal: vec3(0.0, 1.0, 0.0),
            offset: 0.0,
        },
        anchor: 0,
    };
    assert_eq!(poly.untransform_point(poly.points[0]), point2(0.0, 0.0));
    assert_eq!(poly.untransform_point(poly.points[1]), point2(1.0, 0.0));
    assert_eq!(poly.untransform_point(poly.points[2]), point2(1.0, 1.0));
    assert_eq!(poly.untransform_point(poly.points[3]), point2(0.0, 1.0));
}

#[test]
fn are_outside() {
    let plane = Plane {
        normal: vec3(0.0, 0.0, 1.0),
        offset: -1.0,
    };
    assert!(plane.are_outside(&[point3(0.0, 0.0, 1.1), point3(1.0, 1.0, 2.0),]));
    assert!(plane.are_outside(&[point3(0.5, 0.5, 1.0),]));
    assert!(!plane.are_outside(&[point3(0.0, 0.0, 1.0), point3(0.0, 0.0, -1.0),]));
}

#[test]
fn intersect() {
    let poly_a: Polygon<usize> = Polygon {
        points: [
            point3(0.0, 0.0, 1.0),
            point3(1.0, 0.0, 1.0),
            point3(1.0, 1.0, 1.0),
            point3(0.0, 1.0, 1.0),
        ],
        plane: Plane {
            normal: vec3(0.0, 0.0, 1.0),
            offset: -1.0,
        },
        anchor: 0,
    };
    assert!(poly_a.is_valid());
    let poly_b: Polygon<usize> = Polygon {
        points: [
            point3(0.5, 0.0, 2.0),
            point3(0.5, 1.0, 2.0),
            point3(0.5, 1.0, 0.0),
            point3(0.5, 0.0, 0.0),
        ],
        plane: Plane {
            normal: vec3(1.0, 0.0, 0.0),
            offset: -0.5,
        },
        anchor: 0,
    };
    assert!(poly_b.is_valid());

    let intersection = match poly_a.intersect(&poly_b) {
        Intersection::Inside(result) => result,
        _ => panic!("Bad intersection"),
    };
    assert!(intersection.is_valid());
    // confirm the origin is on both planes
    assert!(poly_a
        .plane
        .signed_distance_to(&intersection.origin)
        .approx_eq(&0.0));
    assert!(poly_b
        .plane
        .signed_distance_to(&intersection.origin)
        .approx_eq(&0.0));
    // confirm the direction is coplanar to both planes
    assert!(poly_a.plane.normal.dot(intersection.dir).approx_eq(&0.0));
    assert!(poly_b.plane.normal.dot(intersection.dir).approx_eq(&0.0));

    let poly_c: Polygon<usize> = Polygon {
        points: [
            point3(0.0, -1.0, 2.0),
            point3(0.0, -1.0, 0.0),
            point3(0.0, 0.0, 0.0),
            point3(0.0, 0.0, 2.0),
        ],
        plane: Plane {
            normal: vec3(1.0, 0.0, 0.0),
            offset: 0.0,
        },
        anchor: 0,
    };
    assert!(poly_c.is_valid());
    let poly_d: Polygon<usize> = Polygon {
        points: [
            point3(0.0, 0.0, 0.5),
            point3(1.0, 0.0, 0.5),
            point3(1.0, 1.0, 0.5),
            point3(0.0, 1.0, 0.5),
        ],
        plane: Plane {
            normal: vec3(0.0, 0.0, 1.0),
            offset: -0.5,
        },
        anchor: 0,
    };
    assert!(poly_d.is_valid());

    assert!(poly_a.intersect(&poly_c).is_outside());
    assert!(poly_a.intersect(&poly_d).is_outside());
}

fn test_cut(poly_base: &Polygon<usize>, extra_count: u8, line: Line) {
    assert!(line.is_valid());

    let normal = poly_base.plane.normal.cross(line.dir).normalize();
    let mut poly = poly_base.clone();
    let (extra1, extra2) = poly.split_with_normal(&line, &normal);
    assert!(poly.is_valid() && poly_base.contains(&poly));
    assert_eq!(extra_count > 0, extra1.is_some());
    assert_eq!(extra_count > 1, extra2.is_some());
    if let Some(extra) = extra1 {
        assert!(extra.is_valid() && poly_base.contains(&extra));
    }
    if let Some(extra) = extra2 {
        assert!(extra.is_valid() && poly_base.contains(&extra));
    }
}

#[test]
fn split() {
    let poly: Polygon<usize> = Polygon {
        points: [
            point3(0.0, 1.0, 0.0),
            point3(1.0, 1.0, 0.0),
            point3(1.0, 1.0, 1.0),
            point3(0.0, 1.0, 1.0),
        ],
        plane: Plane {
            normal: vec3(0.0, 1.0, 0.0),
            offset: -1.0,
        },
        anchor: 0,
    };

    // non-intersecting line
    test_cut(
        &poly,
        0,
        Line {
            origin: point3(0.0, 1.0, 0.5),
            dir: vec3(0.0, 1.0, 0.0),
        },
    );

    // simple cut (diff=2)
    test_cut(
        &poly,
        1,
        Line {
            origin: point3(0.0, 1.0, 0.5),
            dir: vec3(1.0, 0.0, 0.0),
        },
    );

    // complex cut (diff=1, wrapped)
    test_cut(
        &poly,
        2,
        Line {
            origin: point3(0.0, 1.0, 0.5),
            dir: vec3(0.5f64.sqrt(), 0.0, -0.5f64.sqrt()),
        },
    );

    // complex cut (diff=1, non-wrapped)
    test_cut(
        &poly,
        2,
        Line {
            origin: point3(0.5, 1.0, 0.0),
            dir: vec3(0.5f64.sqrt(), 0.0, 0.5f64.sqrt()),
        },
    );

    // complex cut (diff=3)
    test_cut(
        &poly,
        2,
        Line {
            origin: point3(0.5, 1.0, 0.0),
            dir: vec3(-0.5f64.sqrt(), 0.0, 0.5f64.sqrt()),
        },
    );

    // perfect diagonal
    test_cut(
        &poly,
        1,
        Line {
            origin: point3(0.0, 1.0, 0.0),
            dir: vec3(0.5f64.sqrt(), 0.0, 0.5f64.sqrt()),
        },
    );
}

#[test]
fn plane_unnormalized() {
    let zero_vec = vec3(0.0000001, 0.0, 0.0);
    let mut plane: Result<Option<Plane>, _> = Plane::from_unnormalized(zero_vec, 1.0);
    assert_eq!(plane, Ok(None));
    plane = Plane::from_unnormalized(zero_vec, 0.0);
    assert_eq!(plane, Err(NegativeHemisphereError));
    plane = Plane::from_unnormalized(zero_vec, -0.5);
    assert_eq!(plane, Err(NegativeHemisphereError));

    let plane = Plane::from_unnormalized(vec3(-3.0, 4.0, 0.0), 2.0)
        .unwrap()
        .unwrap();
    let expected = Plane {
        normal: vec3(-3.0 / 5.0, 4.0 / 5.0, 0.0),
        offset: 2.0 / 5.0,
    };
    assert!(plane.normal.approx_eq(&expected.normal));
    assert!(plane.offset.approx_eq(&expected.offset));
}