project.cc 2.32 KB
Newer Older
1
2
3
4
#include "test.hh"

TG_FUZZ_TEST(TypedGeometry, Project)
{
Philip Trettner's avatar
Philip Trettner committed
5
6
7
    auto range1 = tg::aabb1(tg::pos1(-10), tg::pos1(10));
    auto range2 = tg::aabb2(tg::pos2(-10), tg::pos2(10));
    auto range3 = tg::aabb3(tg::pos3(-10), tg::pos3(10));
8

9
10
    TG_UNUSED(range2);

11
12
    {
        // plane
Philip Trettner's avatar
Philip Trettner committed
13
        auto n = normalize(uniform(rng, range3) - tg::zero<tg::pos3>());
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
        auto d = uniform(rng, -10.0f, 10.0f);
        auto pl = tg::plane(n, d);

        // point
        auto p = uniform(rng, range3);


        auto pp = tg::project(p, pl);

        // distance initial point to plane
        auto dist0 = tg::distance(p, pl);
        // distance initial point to point on plane
        auto dist1 = tg::distance(p, pp);
        // distance point on plane to plane
        auto dist2 = tg::distance(pp, pl);

30
31
        CHECK(dist0 == approx(dist1));
        CHECK(dist2 == approx(0.0f));
32
    }
33
34
35
36
37

    {
        // line
        auto pos = uniform(rng, range3);
        auto dir = tg::vec3();
38
        while (length_sqr(dir) == 0)
39
40
41
42
43
44
45
46
47
48
            dir = tg::vec3(uniform(rng, range3));
        auto line = tg::line3(pos, normalize(dir));

        auto s = uniform(rng, range1).x;

        // point
        auto p = pos + s * line.dir;

        // create vector which is orthogonal to line direction
        auto ortho = tg::vec3();
49
        while (length_sqr(ortho) == 0)
50
51
52
53
54
55
56
            ortho = cross(dir, tg::vec3(uniform(rng, range3)));

        // create second point "above" line
        auto pn = p + normalize(ortho);

        // project that point back, it should lie at p's position
        pn = tg::project(pn, line);
57
        auto dist1 = tg::distance_sqr(p, pn);
58
        CHECK(dist1 == approx(0.0f));
59
    }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

    {
        // ray
        auto pos = uniform(rng, range3);
        auto dir = tg::vec3();
        while (length_sqr(dir) == 0)
            dir = tg::vec3(uniform(rng, range3));
        auto dirN = normalize(dir);
        auto line = tg::line3(pos, dirN);
        auto ray = tg::ray3(pos, dirN);

        auto p = uniform(rng, range3);

        auto projLine = tg::project(p, line);
        auto projRay = tg::project(p, ray);

        // The projection onto the ray is the same as onto the line if in positive direction, otherwise at the ray origin
        auto diff = tg::distance_sqr(projRay,dot(p - pos, dir) > 0 ? projLine : pos);
78
        CHECK(diff == approx(0.0f));
79
    }
80
}