affine3d.tests.cpp

 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
#include <planet/affine/matrix3d.hpp>
#include <planet/affine/point3d.hpp>
#include <felspar/test.hpp>


static_assert(
        sizeof(planet::affine::point3d) == sizeof(float) * 4,
        "There must be zero extra data in the 3d point for Vulkan "
        "compatibility");
static_assert(
        sizeof(planet::affine::matrix3d) == sizeof(float) * 16,
        "There must be zero extra data in the matrix for Vulkan compatibility");


namespace {


    auto const suite = felspar::testsuite("affine3d");


    auto const p3 = suite.test(
            "point3d",
            [](auto check) {
                auto const p = planet::affine::point3d{2, 4, 8, 2};
                check(p.x()) == 1.0f;
                check(p.y()) == 2.0f;
                check(p.z()) == 4.0f;
            },
            [](auto check) {
                auto const p = planet::affine::point3d{2, 4, 8, 2}
                        + planet::affine::point3d{3, 5, 0};
                check(p.x()) == 4.0f;
                check(p.y()) == 7.0f;
                check(p.z()) == 4.0f;
            });


    auto const mat3 = suite.test("matrix", [](auto check) {
        auto const tr = planet::affine::matrix3d::translate(1, 2, 3);
        check(tr[{3, 0}]) == 1.0f;
        check(tr[{3, 1}]) == 2.0f;
        check(tr[{3, 2}]) == 3.0f;

        planet::affine::matrix3d const t2{
                planet::affine::matrix2d::translate({2, 3})};
        check(t2[{3, 0}]) == 2.0f;
        check(t2[{3, 1}]) == 3.0f;
        check(t2[{3, 2}]) == 0.0f;

        auto const t3 = planet::affine::matrix3d::translate(2, 3, 4);
        auto const mul = planet::affine::matrix3d{} * t3;
        check(mul[{0, 0}]) == t3[{0, 0}];
        check(mul[{1, 0}]) == t3[{1, 0}];
        check(mul[{2, 0}]) == t3[{2, 0}];
        check(mul[{3, 0}]) == t3[{3, 0}];
        check(mul[{0, 1}]) == t3[{0, 1}];
        check(mul[{1, 1}]) == t3[{1, 1}];
        check(mul[{2, 1}]) == t3[{2, 1}];
        check(mul[{3, 1}]) == t3[{3, 1}];
        check(mul[{0, 2}]) == t3[{0, 2}];
        check(mul[{1, 2}]) == t3[{1, 2}];
        check(mul[{2, 2}]) == t3[{2, 2}];
        check(mul[{3, 2}]) == t3[{3, 2}];
        check(mul[{0, 3}]) == t3[{0, 3}];
        check(mul[{1, 3}]) == t3[{1, 3}];
        check(mul[{2, 3}]) == t3[{2, 3}];
        check(mul[{3, 3}]) == t3[{3, 3}];

        planet::affine::matrix3d const ry{
                planet::affine::matrix2d::reflect_y()};
        check(ry[{0, 0}]) == 1.0f;
        check(ry[{1, 0}]) == 0.0f;
        check(ry[{2, 0}]) == 0.0f;
        check(ry[{3, 0}]) == 0.0f;
        check(ry[{0, 1}]) == 0.0f;
        check(ry[{1, 1}]) == -1.0f;
        check(ry[{2, 1}]) == 0.0f;
        check(ry[{3, 1}]) == 0.0f;
        check(ry[{0, 2}]) == 0.0f;
        check(ry[{1, 2}]) == 0.0f;
        check(ry[{2, 2}]) == 1.0f;
        check(ry[{3, 2}]) == 0.0f;
        check(ry[{0, 3}]) == 0.0f;
        check(ry[{1, 3}]) == 0.0f;
        check(ry[{2, 3}]) == 0.0f;
        check(ry[{3, 3}]) == 1.0f;
    });


}