plane-intersect.cc 9.34 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
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

    // 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};
        // 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 below triangle in plane
        c = {center - n + bi, 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));
        }

        // 2 on 1 side
        c = {center - n + bi, 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));
        }
    }
Philip Trettner's avatar
Philip Trettner committed
263
}