File: testmathutil.cpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (194 lines) | stat: -rw-r--r-- 7,405 bytes parent folder | download
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
#include <components/misc/mathutil.hpp>

#include <osg/io_utils>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <iomanip>
#include <limits>

MATCHER_P2(Vec3fEq, other, precision, "")
{
    return std::abs(arg.x() - other.x()) < precision && std::abs(arg.y() - other.y()) < precision
        && std::abs(arg.z() - other.z()) < precision;
}

namespace testing
{
    template <>
    inline testing::Message& Message::operator<<(const osg::Vec3f& value)
    {
        return (*this) << std::setprecision(std::numeric_limits<float>::max_exponent10) << "osg::Vec3f(" << value.x()
                       << ", " << value.y() << ", " << value.z() << ')';
    }

    template <>
    inline testing::Message& Message::operator<<(const osg::Quat& value)
    {
        return (*this) << std::setprecision(std::numeric_limits<float>::max_exponent10) << "osg::Quat(" << value.x()
                       << ", " << value.y() << ", " << value.z() << ", " << value.w() << ')';
    }
}

namespace Misc
{
    namespace
    {
        using namespace testing;

        struct MiscToEulerAnglesXZQuatTest : TestWithParam<std::pair<osg::Quat, osg::Vec3f>>
        {
        };

        TEST_P(MiscToEulerAnglesXZQuatTest, shouldReturnValueCloseTo)
        {
            const osg::Vec3f result = toEulerAnglesXZ(GetParam().first);
            EXPECT_THAT(result, Vec3fEq(GetParam().second, 1e-6))
                << "toEulerAnglesXZ(" << GetParam().first << ") = " << result;
        }

        const std::pair<osg::Quat, osg::Vec3f> eulerAnglesXZQuat[] = {
            {
                osg::Quat(1, 0, 0, 0),
                osg::Vec3f(0, 0, osg::PIf),
            },
            {
                osg::Quat(0, 1, 0, 0),
                osg::Vec3f(0, 0, 0),
            },
            {
                osg::Quat(0, 0, 1, 0),
                osg::Vec3f(0, 0, osg::PIf),
            },
            {
                osg::Quat(0, 0, 0, 1),
                osg::Vec3f(0, 0, 0),
            },
            {
                osg::Quat(-0.5, -0.5, -0.5, -0.5),
                osg::Vec3f(-osg::PI_2f, 0, 0),
            },
            {
                osg::Quat(0.5, -0.5, -0.5, -0.5),
                osg::Vec3f(0, 0, -osg::PI_2f),
            },
            {
                osg::Quat(0.5, 0.5, -0.5, -0.5),
                osg::Vec3f(osg::PI_2f, 0, 0),
            },
            {
                osg::Quat(0.5, 0.5, 0.5, -0.5),
                osg::Vec3f(0, 0, osg::PI_2f),
            },
            {
                osg::Quat(0.5, 0.5, 0.5, 0.5),
                osg::Vec3f(-osg::PI_2f, 0, 0),
            },
            {
                // normalized osg::Quat(0.1, 0.2, 0.3, 0.4)
                osg::Quat(0.18257418583505536, 0.36514837167011072, 0.54772255750516607, 0.73029674334022143),
                osg::Vec3f(-0.72972762584686279296875f, 0, -1.10714876651763916015625f),
            },
            {
                osg::Quat(-0.18257418583505536, 0.36514837167011072, 0.54772255750516607, 0.73029674334022143),
                osg::Vec3f(-0.13373161852359771728515625f, 0, -1.2277724742889404296875f),
            },
            {
                osg::Quat(0.18257418583505536, -0.36514837167011072, 0.54772255750516607, 0.73029674334022143),
                osg::Vec3f(0.13373161852359771728515625f, 0, -1.2277724742889404296875f),
            },
            {
                osg::Quat(0.18257418583505536, 0.36514837167011072, -0.54772255750516607, 0.73029674334022143),
                osg::Vec3f(0.13373161852359771728515625f, 0, 1.2277724742889404296875f),
            },
            {
                osg::Quat(0.18257418583505536, 0.36514837167011072, 0.54772255750516607, -0.73029674334022143),
                osg::Vec3f(-0.13373161852359771728515625, 0, 1.2277724742889404296875f),
            },
            {
                osg::Quat(0.246736, -0.662657, -0.662667, 0.246739),
                osg::Vec3f(-osg::PI_2f, 0, 2.5199801921844482421875f),
            },
        };

        INSTANTIATE_TEST_SUITE_P(FromQuat, MiscToEulerAnglesXZQuatTest, ValuesIn(eulerAnglesXZQuat));

        struct MiscToEulerAnglesZYXQuatTest : TestWithParam<std::pair<osg::Quat, osg::Vec3f>>
        {
        };

        TEST_P(MiscToEulerAnglesZYXQuatTest, shouldReturnValueCloseTo)
        {
            const osg::Vec3f result = toEulerAnglesZYX(GetParam().first);
            EXPECT_THAT(result, Vec3fEq(GetParam().second, std::numeric_limits<float>::epsilon()))
                << "toEulerAnglesZYX(" << GetParam().first << ") = " << result;
        }

        const std::pair<osg::Quat, osg::Vec3f> eulerAnglesZYXQuat[] = {
            {
                osg::Quat(1, 0, 0, 0),
                osg::Vec3f(osg::PIf, 0, 0),
            },
            {
                osg::Quat(0, 1, 0, 0),
                osg::Vec3f(osg::PIf, 0, osg::PIf),
            },
            {
                osg::Quat(0, 0, 1, 0),
                osg::Vec3f(0, 0, osg::PIf),
            },
            {
                osg::Quat(0, 0, 0, 1),
                osg::Vec3f(0, 0, 0),
            },
            {
                osg::Quat(-0.5, -0.5, -0.5, -0.5),
                osg::Vec3f(0, -osg::PI_2f, -osg::PI_2f),
            },
            {
                osg::Quat(0.5, -0.5, -0.5, -0.5),
                osg::Vec3f(osg::PI_2f, 0, -osg::PI_2f),
            },
            {
                osg::Quat(0.5, 0.5, -0.5, -0.5),
                osg::Vec3f(0, osg::PI_2f, -osg::PI_2f),
            },
            {
                osg::Quat(0.5, 0.5, 0.5, -0.5),
                osg::Vec3f(osg::PI_2f, 0, osg::PI_2f),
            },
            {
                osg::Quat(0.5, 0.5, 0.5, 0.5),
                osg::Vec3f(0, -osg::PI_2f, -osg::PI_2f),
            },
            {
                // normalized osg::Quat(0.1, 0.2, 0.3, 0.4)
                osg::Quat(0.18257418583505536, 0.36514837167011072, 0.54772255750516607, 0.73029674334022143),
                osg::Vec3f(0.1973955929279327392578125f, -0.8232119083404541015625f, -1.37340080738067626953125f),
            },
            {
                osg::Quat(-0.18257418583505536, 0.36514837167011072, 0.54772255750516607, 0.73029674334022143),
                osg::Vec3f(0.78539812564849853515625f, -0.339836895465850830078125f, -1.428899288177490234375f),
            },
            {
                osg::Quat(0.18257418583505536, -0.36514837167011072, 0.54772255750516607, 0.73029674334022143),
                osg::Vec3f(-0.78539812564849853515625f, 0.339836895465850830078125f, -1.428899288177490234375f),
            },
            {
                osg::Quat(0.18257418583505536, 0.36514837167011072, -0.54772255750516607, 0.73029674334022143),
                osg::Vec3f(-0.78539812564849853515625f, -0.339836895465850830078125f, 1.428899288177490234375f),
            },
            {
                osg::Quat(0.18257418583505536, 0.36514837167011072, 0.54772255750516607, -0.73029674334022143),
                osg::Vec3f(0.78539812564849853515625f, 0.339836895465850830078125f, 1.428899288177490234375f),
            },
            {
                osg::Quat(0.246736, -0.662657, 0.246739, -0.662667),
                osg::Vec3f(0.06586204469203948974609375f, -osg::PI_2f, 0.64701664447784423828125f),
            },
        };

        INSTANTIATE_TEST_SUITE_P(FromQuat, MiscToEulerAnglesZYXQuatTest, ValuesIn(eulerAnglesZYXQuat));
    }
}