affine2d.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
 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
#include <planet/ostream.hpp>
#include <felspar/test.hpp>

#include <cstring>


namespace {


    inline std::uint32_t bitpattern(float const x) {
        std::uint32_t u;
        std::memcpy(&u, &x, sizeof(x));
        return u;
    }
    inline std::uint32_t reset_sign(std::uint32_t const x) {
        return std::int32_t(x) >= 0 ? x : -std::int32_t(x);
    }
    inline std::uint32_t ulps(float const a, float const b) {
        std::uint32_t ua = bitpattern(a);
        std::uint32_t ub = bitpattern(b);
        std::uint32_t s = ub ^ ua;

        if (std::int32_t(s) >= 0) {
            return reset_sign(ua - ub);
        } else {
            return ua + ub + 0x80000000;
        }
    }


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


    auto const p2add = suite.test(
            "point2d",
            [](auto check) {
                auto p = -planet::affine::point2d{1, 2};
                check(p.x()) == -1.0f;
                check(p.y()) == -2.0f;
            },
            [](auto check) {
                auto p = planet::affine::point2d{1, 2}
                        + planet::affine::point2d{3, 4};
                check(p.x()) == 4.0f;
                check(p.y()) == 6.0f;
            },
            [](auto check) {
                auto p = planet::affine::point2d{1, 2, 2}
                        + planet::affine::point2d{3, 4, 2};
                check(p.x()) == 2.0f;
                check(p.y()) == 3.0f;
            },
            [](auto check) {
                auto p = planet::affine::point2d{1, 2, 2}
                        - planet::affine::point2d{3, 4, 2};
                check(p.x()) == -1.0f;
                check(p.y()) == -1.0f;
            });


    auto const theta = suite.test("point2d/theta", [](auto check) {
        check(planet::affine::point2d::from_polar(10, 0).x()) == 10.0f;
        check(planet::affine::point2d::from_polar(10, 0).y()) == 0.0f;
        check(planet::affine::point2d::from_polar(10, 0).theta()) == 0.0f;

        check(std::fabs(
                planet::affine::point2d::from_polar(10, 0.125f).x()
                - 10.0f * std::sqrt(2) / 2.0f))
                < 1.0e-5f;
        check(std::fabs(
                planet::affine::point2d::from_polar(10, 0.125f).y()
                - 10.0f * std::sqrt(2) / 2.0f))
                < 1.0e-5f;
        check(planet::affine::point2d::from_polar(10, 0.125).theta()) == 0.125f;

        check(std::fabs(planet::affine::point2d::from_polar(10, 0.25f).x()))
                < 1.0e-5f;
        check(planet::affine::point2d::from_polar(10, 0.25f).y()) == 10.0f;
        check(planet::affine::point2d::from_polar(10, 0.25f).theta()) == 0.25f;

        check(std::fabs(
                planet::affine::point2d::from_polar(10, 0.375f).x()
                + 10.0f * std::sqrt(2) / 2.0f))
                < 1.0e-5f;
        check(std::fabs(
                planet::affine::point2d::from_polar(10, 0.375f).y()
                - 10.0f * std::sqrt(2) / 2.0f))
                < 1.0e-5f;
        check(planet::affine::point2d::from_polar(10, 0.375f).theta())
                == 0.375f;

        check(planet::affine::point2d::from_polar(10, 0.5f).x()) == -10.0f;
        check(std::fabs(planet::affine::point2d::from_polar(10, 0.5f).y()))
                < 1.0e-5f;
        check(planet::affine::point2d::from_polar(10, 0.5f).theta()) == 0.5f;

        check(std::fabs(planet::affine::point2d::from_polar(10, 0.75f).x()))
                < 1.0e-5f;
        check(planet::affine::point2d::from_polar(10, 0.75f).y()) == -10.0f;
        check(planet::affine::point2d::from_polar(10, 0.75f).theta()) == 0.75f;
    });


    auto const rotate = suite.test(
            "point2d/rotate",
            [](auto check) {
                planet::affine::point2d p{10, 0};
                check(p.x()) == 10.0f;
                check(p.y()) == 0.0f;
                check(p.theta()) == 0.0f;

                p = p.rotate(0.125f);
                check(std::fabs(p.x() - 10.0f * std::sqrt(2) / 2.0f)) < 1.0e-5f;
                check(std::fabs(p.y() - 10.0f * std::sqrt(2) / 2.0f)) < 1.0e-5f;
                check(p.theta()) == 0.125f;

                p = p.rotate(0.125f);
                check(std::fabs(p.x())) < 1.0e-5f;
                check(p.y()) == 10.0f;
                check(p.theta()) == 0.25f;

                p = p.rotate(0.125f);
                check(std::fabs(p.x() + 10.0f * std::sqrt(2) / 2.0f)) < 1.0e-5f;
                check(std::fabs(p.y() - 10.0f * std::sqrt(2) / 2.0f)) < 1.0e-5f;
                check(p.theta()) == 0.375f;

                p = p.rotate(0.125f);
                check(p.x()) == -10.0f;
                check(std::fabs(p.y())) < 1.0e-5f;
                check(p.theta()) == 0.5f;

                p = p.rotate(0.25f);
                check(std::fabs(p.x())) < 1.0e-5f;
                check(p.y()) == -10.0f;
                check(p.theta()) == 0.75f;

                p = p.rotate(0.25f);
                check(p.x()) == 10.0f;
                check(std::fabs(p.y())) < 1.0e-5f;
                check(std::fabs(p.theta())) < 1.0e07f;
            },
            [](auto check) {
                planet::affine::point2d p{20, 0, 2};
                check(p.x()) == 10.0f;
                check(p.y()) == 0.0f;
                check(p.theta()) == 0.0f;

                p = p.rotate(0.125f);
                check(std::fabs(p.x() - 10.0f * std::sqrt(2) / 2.0f)) < 1.0e-5f;
                check(std::fabs(p.y() - 10.0f * std::sqrt(2) / 2.0f)) < 1.0e-5f;
                check(p.theta()) == 0.125f;
            });


    auto const mat = suite.test(
            "matrix2d",
            [](auto check) {
                auto const tr = planet::affine::matrix2d::translate({2, 3});
                check(tr[{2, 0}]) == 2.0f;
                check(tr[{2, 1}]) == 3.0f;
            },
            [](auto check) {
                auto const tr = planet::affine::matrix2d::translate({4, 6, 2});
                check(tr[{2, 0}]) == 2.0f;
                check(tr[{2, 1}]) == 3.0f;
            },
            [](auto check) {
                auto const tr = planet::affine::matrix2d::translate({2, 3, -1});
                check(tr[{2, 0}]) == -2.0f;
                check(tr[{2, 1}]) == -3.0f;
            });


    auto const transform = suite.test(
            "transform",
            [](auto check) {
                planet::affine::transform2d t;
                t.rotate(0.25f);
                auto const i = t.into({4, 5});
                check(ulps(i.x(), -5.0f)) < 2u;
                check(ulps(i.y(), 4.0f)) < 2u;

                auto const o = t.outof({-5, 4});
                check(ulps(o.x(), 4)) < 2u;
                check(ulps(o.y(), 5)) < 2u;
            },
            [](auto check) {
                planet::affine::transform2d t;
                t.reflect_y().scale(0.5f).translate({3, 7});

                auto const i = t.into({4, 5});
                check(ulps(i.x(), 5)) < 2u;
                check(ulps(i.y(), 4.5f)) < 2u;

                auto const o = t.outof({5, 4.5f});
                check(ulps(o.x(), 4)) < 2u;
                check(ulps(o.y(), 5)) < 2u;
            },
            [](auto check) {
                planet::affine::transform2d t;
                t.rotate(0.25f);
                auto const i = t.into({8, 10, 2});
                check(ulps(i.x(), -5.0f)) < 2u;
                check(ulps(i.y(), 4.0f)) < 2u;

                auto const o = t.outof({-5, 4});
                check(ulps(o.x(), 4)) < 2u;
                check(ulps(o.y(), 5)) < 2u;
            });


}