plane-intersect.cc 13 KB
Newer Older
Philip Trettner's avatar
Philip Trettner committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <test.hh>

TG_FUZZ_TEST(Plane, Intersect)
{
    auto bounds = tg::aabb3(-10, 10);

    // plane - plane
    {
        auto const p0 = tg::plane(tg::uniform<tg::dir3>(rng), uniform(rng, bounds));
        auto const p1 = tg::plane(tg::uniform<tg::dir3>(rng), uniform(rng, bounds));

        auto const l = intersection(p0, p1);

        auto const p = l[uniform(rng, -10.0f, 10.0f)];

16
        // divide by 10 for further tolerance
17
18
        CHECK(distance(p, p0) / 10 == approx(0));
        CHECK(distance(p, p1) / 10 == approx(0));
Philip Trettner's avatar
Philip Trettner committed
19
    }
Julian Schakib's avatar
Julian Schakib committed
20
21
22

    // plane3 - sphere3
    {
Julian Schakib's avatar
Julian Schakib committed
23
24
        auto const r = tg::uniform(rng, 0.1f, 10.0f);

Julian Schakib's avatar
Julian Schakib committed
25
26
        auto const o = uniform(rng, bounds);
        auto const p = tg::plane(tg::uniform<tg::dir3>(rng), o);
Julian Schakib's avatar
Julian Schakib committed
27
        auto const s = tg::sphere3(o, r);
Julian Schakib's avatar
Julian Schakib committed
28
29

        CHECK(tg::intersects(p, s));
Julian Schakib's avatar
Julian Schakib committed
30
31
32
33
34
35
36
37
38
39
40
41

        // tangent
        auto const ts = tg::sphere3(o + p.normal * r, r);
        CHECK(tg::intersects(p, ts, r / 1000));

        // off
        auto const toff = tg::sphere3(o + p.normal * (r + 0.1f), r);
        CHECK(!tg::intersects(p, toff, tg::epsilon<tg::f32> * 20));

        // orthogonally moved
        auto const to = tg::sphere3(o + tg::any_normal(p.normal) * 10, r);
        CHECK(tg::intersects(p, to));
42
43

        // move plane
44
        auto const n = tg::uniform<tg::dir3>(rng);
45
46
47
48
49
        auto const pm = tg::plane(n, to.center + n * to.radius);
        auto const eps = to.radius / 10000;

        CHECK(intersects(pm, to, eps));
        CHECK(!intersects(pm, to, -eps));
Julian Schakib's avatar
Julian Schakib committed
50
    }
51

Julian Schakib's avatar
Julian Schakib committed
52
    // plane3 - inf_cylinder3_boundary
53
54
55
    {
        auto const box = tg::aabb3(tg::pos3(-10.0f), tg::pos3(10.0f));

56
        {
57
            // cornercase: plane and cylinder have *parallel* normals
58
            // confirm center position and circle shape of intersection
59

60
61
            auto const r = tg::uniform(rng, 0.1f, 10.0f);
            auto const d = tg::uniform<tg::dir3>(rng);
62

63
            auto const center = uniform(rng, box);
64

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
            auto const c = tg::inf_cylinder_boundary<3, tg::f32>(tg::line3(center, d), r);

            {
                auto const p = tg::plane3(d, center);
                // the cut should be a circle with radius r with center zero
                auto const ellipse = tg::intersection(p, c);
                // check center, directions of normal and length
                CHECK(distance(ellipse.center, center) == approx(0));
                CHECK(dot(ellipse.axisA, p.normal) == approx(0)); // orthogonal
                CHECK(length(ellipse.axisA) - length(ellipse.axisB) == approx(0));
            }

            // as above, flipped d
            {
                auto const p = tg::plane3(-d, center);
                // the cut should be a circle with radius r with center zero
                auto const ellipse = tg::intersection(p, c);
                // check center, directions of normal and length
                CHECK(distance(ellipse.center, center) == approx(0));
                CHECK(dot(ellipse.axisA, p.normal) == approx(0)); // orthogonal
                CHECK(length(ellipse.axisA) - length(ellipse.axisB) == approx(0));
            }
87
88
89
90
91
92
93
94
95
96
97
98
99
100

            // TODO fix this case in intersection.hh!!
            // cornercase: plane normal orthogonal to cylinder normal
            {
                auto const p = tg::plane3(any_normal(d), center);
                // the cut should be a circle with radius r with center zero
                auto const ellipse = tg::intersection(p, c);
                // check center, directions of normal and length
                // CHECK(distance(ellipse.center, center) == approx(0));

                (void)ellipse;
                // TODO -nanf!!
                // CHECK(length(ellipse.axisA) - length(ellipse.axisB) == approx(0));
            }
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
        }

        {
            // cylinder simply up, plane slanted
            // confirm there is a non-deformed ellipse

            auto const r = tg::uniform(rng, 0.1f, 10.0f);

            auto const center = tg::pos3::zero; // uniform(rng, box);

            // TODO identical centers result in delta = 0? issue? -> no.
            auto const c = tg::inf_cylinder_boundary<3, tg::f32>(tg::line3(center /* + tg::vec3(1, 0, 0)*/, tg::dir3::pos_y), r);

            auto const p = tg::plane3(normalize(tg::vec3(1, 1, 0)), center);
            // the cut should be a circle with radius r with center zero
            auto const ellipse = tg::intersection(p, c);

            // existing axes
            CHECK(length(ellipse.axisA) > 0);
            CHECK(length(ellipse.axisB) > 0);
        }

        {
            // random cylinder and planes
            // check if ellipse properties are meaningful

Julian Schakib's avatar
Julian Schakib committed
127
128
            // TODO tests struggle with small radii!!
            auto const c = tg::inf_cylinder_boundary<3, tg::f32>(tg::line3(tg::uniform(rng, box), tg::uniform<tg::dir3>(rng)), tg::uniform(rng, 5.0f, 10.0f));
129
130
131
132

            auto const p = tg::plane3(tg::uniform<tg::dir3>(rng), tg::uniform(rng, box));


Julian Schakib's avatar
Julian Schakib committed
133
134
135
136
137
138
139
140
            // if (tg::abs(dot(p.normal, c.axis.dir)) < 0.95)
            {
                auto const ellipse = tg::intersection(p, c);

                // existing axes
                CHECK(length(ellipse.axisA) > 0);
                CHECK(length(ellipse.axisB) > 0);
            }
141
        }
142
    }
Julian Schakib's avatar
Julian Schakib committed
143

Julian Schakib's avatar
Julian Schakib committed
144
    // plane3 - inf_cone3
Julian Schakib's avatar
Julian Schakib committed
145
146
147
148
149
150
    {
        auto const box = tg::aabb3(tg::pos3(-10.0f), tg::pos3(10.0f));

        auto const center = uniform(rng, box);
        auto const d = tg::uniform<tg::dir3>(rng);

151
152
        auto opening_angle = tg::degree(uniform(rng, 1.0f, 89.0f));
        auto c = tg::inf_cone<3, tg::f32>(center, d, opening_angle);
Julian Schakib's avatar
Julian Schakib committed
153

154
155
156
157
158
159
160
        // checking whether flipping the plane's normal makes a difference (should not)
        {
            auto p = tg::plane3(d, center + d);
            CHECK(intersects(c, p));
            auto c = tg::inf_cone<3, tg::f32>(center, -d, opening_angle);
            CHECK(!intersects(c, p));
        }
Julian Schakib's avatar
Julian Schakib committed
161

162
163
164
165
166
167
        {
            auto p = tg::plane3(-d, center + d);
            CHECK(intersects(c, p));
            auto c = tg::inf_cone<3, tg::f32>(center, -d, opening_angle);
            CHECK(!intersects(c, p));
        }
Julian Schakib's avatar
Julian Schakib committed
168

169
170
171
172
173
174
        {
            auto p = tg::plane3(d, center - d);
            CHECK(!intersects(c, p));
            auto c = tg::inf_cone<3, tg::f32>(center, -d, opening_angle);
            CHECK(intersects(c, p));
        }
175

176
177
178
179
180
181
        {
            auto p = tg::plane3(-d, center - d);
            CHECK(!intersects(c, p));
            auto c = tg::inf_cone<3, tg::f32>(center, -d, opening_angle);
            CHECK(intersects(c, p));
        }
182

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
        // rotate plane normal so that it intersects/does not intersect the cone
        {
            auto rotation_mat = tg::rotation_around(any_normal(d), tg::degree(89) - c.opening_angle);
            auto new_normal = normalize(rotation_mat * (-d));
            auto p = tg::plane3(new_normal, center - d);

            CHECK(!intersects(c, p));
        }

        {
            auto rotation_mat = tg::rotation_around(any_normal(d), tg::degree(91) - c.opening_angle);
            auto new_normal = normalize(rotation_mat * (-d));
            auto p = tg::plane3(new_normal, center - d);

            CHECK(intersects(c, p));
        }
Julian Schakib's avatar
Julian Schakib committed
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

    // plane3 - triangle3
    {
        auto const box = tg::aabb3(tg::pos3(-10.0f), tg::pos3(10.0f));
        auto const center = uniform(rng, box);
        auto const n = tg::uniform<tg::dir3>(rng);
        auto plane = tg::plane3(n, center);

        auto const t = any_normal(n);
        auto const bi = normalize(cross(n, t));

        // coplanar triangle
        auto triangle = tg::triangle3(center, center + t, center + bi);
        CHECK(intersects(triangle, plane));

        // one point of triangle in plane
        tg::array<tg::pos3, 3> c = {center, center + n, center + n + t};
        // check all possible orders
        for (auto i = 0; i < 3; i++)
        {
            triangle = tg::triangle(c[(i + 0) % 3], c[(i + 1) % 3], c[(i + 2) % 3]);
            CHECK(intersects(plane, triangle));

            triangle = tg::triangle(c[(i + 1) % 3], c[(i + 0) % 3], c[(i + 2) % 3]);
            CHECK(intersects(plane, triangle));
        }

        // all above triangle in plane
        c = {center + n + bi, center + n, center + n + t};
229
        // all 6 possible orderings
230
231
232
233
234
235
236
237
238
239
240
        for (auto i = 0; i < 3; i++)
        {
            triangle = tg::triangle(c[(i + 0) % 3], c[(i + 1) % 3], c[(i + 2) % 3]);
            CHECK(!intersects(plane, triangle));

            triangle = tg::triangle(c[(i + 1) % 3], c[(i + 0) % 3], c[(i + 2) % 3]);
            CHECK(!intersects(plane, triangle));
        }

        // all below triangle in plane
        c = {center - n + bi, center - n, center - n + t};
241
        // all 6 possible orderings
242
243
244
245
246
247
248
249
250
251
252
        for (auto i = 0; i < 3; i++)
        {
            triangle = tg::triangle(c[(i + 0) % 3], c[(i + 1) % 3], c[(i + 2) % 3]);
            CHECK(!intersects(plane, triangle));

            triangle = tg::triangle(c[(i + 1) % 3], c[(i + 0) % 3], c[(i + 2) % 3]);
            CHECK(!intersects(plane, triangle));
        }

        // 2 on 1 side
        c = {center - n + bi, center + n, center - n + t};
253
        // all 6 possible orderings
254
255
256
257
258
259
260
261
        for (auto i = 0; i < 3; i++)
        {
            triangle = tg::triangle(c[(i + 0) % 3], c[(i + 1) % 3], c[(i + 2) % 3]);
            CHECK(intersects(plane, triangle));

            triangle = tg::triangle(c[(i + 1) % 3], c[(i + 0) % 3], c[(i + 2) % 3]);
            CHECK(intersects(plane, triangle));
        }
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

        // 2 on 1 side
        c = {center + n + bi, center - n, center + n + t};
        // all 6 possible orderings
        for (auto i = 0; i < 3; i++)
        {
            triangle = tg::triangle(c[(i + 0) % 3], c[(i + 1) % 3], c[(i + 2) % 3]);
            CHECK(intersects(plane, triangle));

            triangle = tg::triangle(c[(i + 1) % 3], c[(i + 0) % 3], c[(i + 2) % 3]);
            CHECK(intersects(plane, triangle));
        }
    }

    // plane3 - aabb3
    {
        auto const size = 10;
        auto const box = tg::aabb3(tg::pos3(-size), tg::pos3(size));
        auto const center = tg::pos3::zero;
        auto const n = tg::dir3::pos_y; // tg::uniform<tg::dir3>(rng);
        auto plane = tg::plane3(n, center);

        CHECK(intersects(box, plane));

        plane = tg::plane3(n, center + n * (size + 0.1f));

        CHECK(!intersects(box, plane));

        plane = tg::plane3(n, center - n * (size + 0.1f));

        CHECK(!intersects(box, plane));

        plane = tg::plane3(tg::dir3::pos_x, center + tg::dir3::pos_x * (size + 0.1f));
        CHECK(!intersects(plane, box));

        plane = tg::plane3(tg::dir3::neg_x, center + tg::dir3::pos_x * (size + 0.1f));
        CHECK(!intersects(plane, box));

        plane = tg::plane3(tg::dir3::neg_x, center + tg::dir3::pos_x * (size - 0.1f));
        CHECK(intersects(plane, box));

        plane = tg::plane3(normalize(tg::vec3(1, 1, 0)), tg::pos3(size + 0.1f));
        CHECK(!intersects(plane, box));

        plane = tg::plane3(normalize(tg::vec3(1, 1, 0)), tg::pos3(size - 0.1f));
        CHECK(intersects(plane, box));

        {
            auto const offset = uniform(rng, box) * 100;
            plane = tg::plane3(any_normal(tg::vec3(offset)), offset);
            CHECK(intersects(plane, box));
        }

        {
            auto const offset = uniform(rng, box) * 100;
            plane = tg::plane3(normalize(tg::vec3(offset)), offset);
            CHECK(!intersects(plane, box));

            plane = tg::plane3(-normalize(tg::vec3(offset)), offset);
            CHECK(!intersects(plane, box));
        }
Julian Schakib's avatar
Julian Schakib committed
323
324
325
326
327
328
329
330
331
332

        // any plane inside the aabb (should intersect)
        {
            plane = tg::plane3(tg::uniform<tg::dir3>(rng), uniform(rng, box));
            CHECK(intersects(plane, box));
        }
    }

    // plane3 - box3
    {
333
334
        auto const origins = tg::aabb3(tg::pos3(-10), tg::pos3(10));

Julian Schakib's avatar
Julian Schakib committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
        auto half_extents = tg::mat3();
        half_extents[0] = tg::dir3::pos_x;
        half_extents[1] = tg::dir3::pos_y;
        half_extents[2] = tg::dir3::pos_z;

        auto box = tg::box3(tg::pos3::zero, half_extents);

        {
            auto const plane = tg::plane3(tg::uniform<tg::dir3>(rng), tg::pos3::zero);
            CHECK(intersects(plane, box));
        }

        {
            auto plane = tg::plane3(tg::dir3::pos_x, tg::pos3(1, 0, 0));
            CHECK(!intersects(plane, box));

            plane = tg::plane3(tg::dir3::pos_x, tg::pos3(0.9f, 0, 0));
            CHECK(intersects(plane, box));
        }

        // random box rotation
Julian Schakib's avatar
Julian Schakib committed
356
357
358
        auto const nx = tg::uniform<tg::dir3>(rng);
        auto const ny = any_normal(nx);
        auto const nz = normalize(cross(nx, ny));
Julian Schakib's avatar
Julian Schakib committed
359
360
361
362
363

        // random box length
        half_extents[0] = nx * uniform(rng, 1.0f, 10.0f);
        half_extents[1] = ny * uniform(rng, 1.0f, 10.0f);
        half_extents[2] = nz * uniform(rng, 1.0f, 10.0f);
Julian Schakib's avatar
Julian Schakib committed
364

365
        box = tg::box3(uniform(rng, origins), half_extents);
Julian Schakib's avatar
Julian Schakib committed
366

367
368
369
370
371
372
373
374
        {
            auto const plane = tg::plane3(nx, tg::pos3(box.center + half_extents[0] * 1.1f));
            CHECK(!intersects(plane, box));
        }
        {
            auto const plane = tg::plane3(nx, tg::pos3(box.center + half_extents[0] * 0.9f));
            CHECK(intersects(plane, box));
        }
375
    }
Philip Trettner's avatar
Philip Trettner committed
376
}