File: test_Quaternion.cpp

package info (click to toggle)
rkcommon 1.14.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,276 kB
  • sloc: cpp: 33,504; sh: 31; makefile: 13
file content (248 lines) | stat: -rw-r--r-- 5,192 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
195
196
197
198
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
// Copyright 2009 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "../catch.hpp"
#include "rkcommon/math/Quaternion.h"

using namespace rkcommon::math;

template <typename T>
inline bool CmpT(const QuaternionT<T> &a, const QuaternionT<T> &b, T epsilon = ulp)
{
  QuaternionT<T> d = b - a;
  if ((abs(d.r) > epsilon) || (abs(d.i) > epsilon) || (abs(d.j) > epsilon)
        || (abs(d.k) > epsilon)) return false;

  return true;
}

///////////////////////////////////////////////////////////////////////////////
// Constructors

template <typename T>
inline void test_constructFromVectors()
{
  using V = typename T::Vector;
  REQUIRE(CmpT(T(V(1, 0, 0), V(0, 1, 0), V(0, 0, 1)), T(1, 0, 0, 0)));
}

template <typename T>
inline void test_constructFromAngles()
{
  using S = typename T::Scalar;
  REQUIRE(CmpT(T(0, S(half_pi), -S(half_pi)), T(.5, .5, .5, -.5)));
}

TEST_CASE("Quaternion constructors", "[quat]")
{
  SECTION("Test from 3 vectors")
  {
    test_constructFromVectors<quatf>();
    test_constructFromVectors<quatd>();
  }

  SECTION("Test from yaw, pitch, roll")
  {
    test_constructFromAngles<quatf>();
    test_constructFromAngles<quatd>();
  }
}

///////////////////////////////////////////////////////////////////////////////
// Operators

template <typename T>
inline void test_unary_neg()
{
  REQUIRE(T(-1, -1, -1, -1) == -T(1, 1, 1, 1));
}

template <typename T>
inline void test_binary_add()
{
  using S = typename T::Scalar;
  REQUIRE(T(1, 1, 1, 1) + T(2, 2, 2, 2) == T(3, 3, 3, 3));
  REQUIRE(T(1, 1, 1, 1) + S(2) == T(3, 1, 1, 1));
  REQUIRE(S(1) + T(2, 2, 2, 2) == T(3, 2, 2, 2));
}

template <typename T>
inline void test_binary_sub()
{
  using S = typename T::Scalar;
  REQUIRE(T(3, 3, 3, 3) - T(1, 1, 1, 1) == T(2, 2, 2, 2));
  REQUIRE(T(3, 3, 3, 3) - S(1) == T(2, 3, 3, 3));
  REQUIRE(S(3) - T(1, 1, 1, 1) == T(2, -1, -1, -1));
}

template <typename T>
inline void test_binary_mul()
{
  using S = typename T::Scalar;
  using V = typename T::Vector;
  REQUIRE(T(1, 1, 1, 1) * S(2) == T(2, 2, 2, 2));
  REQUIRE(S(2) * T(1, 1, 1, 1) == T(2, 2, 2, 2));
  REQUIRE(T(1, 1, 1, 1) * 2.f == T(2, 2, 2, 2));
  REQUIRE(2.f * T(1, 1, 1, 1) == T(2, 2, 2, 2));
  REQUIRE(T(.5, .5, .5, .5) * V(1, 0, 0) == V(0, 1, 0));
  REQUIRE(T(1, 1, 1, 1) * T(2, 2, 2, 2) == T(-4, 4, 4, 4));
}

template <typename T>
inline void test_binary_div()
{
  using S = typename T::Scalar;
  REQUIRE(CmpT(T(2, 2, 2, 2) / T(1, 1, 1, 1), T(2, 0, 0, 0)));
  REQUIRE(CmpT(T(2, 2, 2, 2) / S(1), T(2, 2, 2, 2)));
  REQUIRE(CmpT(S(2) / T(1, 1, 1, 1), T(.5, -.5, -.5, -.5)));
}

TEST_CASE("Quaternion operators", "[quat]")
{
  SECTION("Test unary neg")
  {
    test_unary_neg<quatf>();
    test_unary_neg<quatd>();
  }

  SECTION("Test binary add")
  {
    test_binary_add<quatf>();
    test_binary_add<quatd>();
  }

  SECTION("Test binary sub")
  {
    test_binary_sub<quatf>();
    test_binary_sub<quatd>();
  }

  SECTION("Test binary mul")
  {
    test_binary_mul<quatf>();
    test_binary_mul<quatd>();
  }

  SECTION("Test binary div")
  {
    test_binary_div<quatf>();
    test_binary_div<quatd>();
  }
}

///////////////////////////////////////////////////////////////////////////////
// Functions
template <typename T>
inline void test_conj()
{
  REQUIRE(conj(T(1, 1, 1, 1)) == T(1, -1, -1, -1));
}

template <typename T>
inline void test_rcp()
{
  REQUIRE(CmpT(rcp(T(1, 1, 1, 1)), T(.25, -.25, -.25, -.25)));
}

template <typename T>
inline void test_abs()
{
  REQUIRE(abs(T(2, 2, 2, 2)) == 4);
}

template <typename T>
inline void test_normalize()
{
  REQUIRE(CmpT(normalize(T(1, 1, 1, 1)), T(.5, .5, .5, .5)));
}

template <typename T>
inline void test_dot()
{
  REQUIRE(dot(T(.5, .5, .5, .5), T(.5, .5, .5, .5)) == 1);
}

template <typename T>
inline void test_xfmPoint()
{
  using V = typename T::Vector;
  REQUIRE(xfmPoint(T(.5, .5, .5, .5), V(1, 0, 0)) == V(0, 1, 0));
}

template <typename T>
inline void test_xfmQuaternion()
{
  REQUIRE(xfmQuaternion(T(1, 1, 1, 1), T(2, 2, 2, 2)) == T(-4, 4, 4, 4));
}

template <typename T>
inline void test_xfmNormal()
{
  using V = typename T::Vector;
  REQUIRE(
      xfmNormal(T(.5, .5, .5, .5), V(1, 0, 0)) == V(0, 1, 0));
}

template <typename T>
inline void test_slerp()
{
  typename T::Scalar two = 2;
  REQUIRE(CmpT(slerp(.5f, T(1, 0, 0, 0), T(0, 1, 0, 0)), T(rsqrt(two), rsqrt(two), 0, 0)));
}

TEST_CASE("Quaternion functions", "[quat]")
{
  SECTION("Test conj")
  {
    test_conj<quatf>();
    test_conj<quatd>();
  }

  SECTION("Test rcp")
  {
    test_rcp<quatf>();
    test_rcp<quatd>();
  }

  SECTION("Test abs")
  {
    test_abs<quatf>();
    test_abs<quatd>();
  }

  SECTION("Test normalize")
  {
    test_normalize<quatf>();
    test_normalize<quatd>();
  }

  SECTION("Test dot")
  {
    test_dot<quatf>();
    test_dot<quatd>();
  }

  SECTION("Test xfmPoint")
  {
    test_xfmPoint<quatf>();
    test_xfmPoint<quatd>();
  }

  SECTION("Test xfmQuaternion")
  {
    test_xfmQuaternion<quatf>();
    test_xfmQuaternion<quatd>();
  }

  SECTION("Test xfmNormal")
  {
    test_xfmNormal<quatf>();
    test_xfmNormal<quatd>();
  }

  SECTION("Test slerp")
  {
    test_slerp<quatf>();
    test_slerp<quatd>();
  }
}