source: trunk/test/unit/rotation.cpp @ 1321

Last change on this file since 1321 was 1321, checked in by sam, 8 years ago

test: add matrix determinant and quaternion norm checks in the unit tests.

  • Property svn:keywords set to Id
File size: 7.2 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "core.h"
16#include "lol/unit.h"
17
18namespace lol
19{
20
21LOLUNIT_FIXTURE(RotationTest)
22{
23    void SetUp() {}
24
25    void TearDown() {}
26
27    LOLUNIT_TEST(Rotate2D)
28    {
29        /* Rotations must be CCW */
30        mat2 m90 = mat2::rotate(90.f);
31
32        vec2 a(2.f, 3.f);
33        vec2 b = m90 * a;
34
35        LOLUNIT_ASSERT_DOUBLES_EQUAL(b.x, -a.y, 1e-5);
36        LOLUNIT_ASSERT_DOUBLES_EQUAL(b.y,  a.x, 1e-5);
37
38        float d = determinant(m90);
39
40        LOLUNIT_ASSERT_DOUBLES_EQUAL(d, 1.0, 1e-5);
41    }
42
43    LOLUNIT_TEST(Compose2D)
44    {
45        /* Rotating 20 degrees twice must equal rotating 40 degrees */
46        mat2 m20 = mat2::rotate(20.f);
47        mat2 m40 = mat2::rotate(40.f);
48        mat2 m20x20 = m20 * m20;
49
50        LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[0][0], m40[0][0], 1e-5);
51        LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[1][0], m40[1][0], 1e-5);
52
53        LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[0][1], m40[0][1], 1e-5);
54        LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[1][1], m40[1][1], 1e-5);
55    }
56
57    LOLUNIT_TEST(Rotate3D)
58    {
59        /* Rotations must be CCW along each axis */
60        mat3 m90x = mat3::rotate(90.f, 1.f, 0.f, 0.f);
61        mat3 m90y = mat3::rotate(90.f, 0.f, 1.f, 0.f);
62        mat3 m90z = mat3::rotate(90.f, 0.f, 0.f, 1.f);
63
64        vec3 a(2.f, 3.f, 4.f);
65        vec3 b = m90x * a;
66        vec3 c = m90y * a;
67        vec3 d = m90z * a;
68
69        LOLUNIT_ASSERT_DOUBLES_EQUAL(b.x,  a.x, 1e-5);
70        LOLUNIT_ASSERT_DOUBLES_EQUAL(b.y, -a.z, 1e-5);
71        LOLUNIT_ASSERT_DOUBLES_EQUAL(b.z,  a.y, 1e-5);
72
73        LOLUNIT_ASSERT_DOUBLES_EQUAL(c.x,  a.z, 1e-5);
74        LOLUNIT_ASSERT_DOUBLES_EQUAL(c.y,  a.y, 1e-5);
75        LOLUNIT_ASSERT_DOUBLES_EQUAL(c.z, -a.x, 1e-5);
76
77        LOLUNIT_ASSERT_DOUBLES_EQUAL(d.x, -a.y, 1e-5);
78        LOLUNIT_ASSERT_DOUBLES_EQUAL(d.y,  a.x, 1e-5);
79        LOLUNIT_ASSERT_DOUBLES_EQUAL(d.z,  a.z, 1e-5);
80
81        float dx = determinant(m90x);
82        float dy = determinant(m90y);
83        float dz = determinant(m90z);
84
85        LOLUNIT_ASSERT_DOUBLES_EQUAL(dx, 1.0, 1e-5);
86        LOLUNIT_ASSERT_DOUBLES_EQUAL(dy, 1.0, 1e-5);
87        LOLUNIT_ASSERT_DOUBLES_EQUAL(dz, 1.0, 1e-5);
88    }
89
90    LOLUNIT_TEST(Compose3D)
91    {
92        /* Rotating 20 degrees twice must equal rotating 40 degrees */
93        mat3 m20 = mat3::rotate(20.f, 1.f, 2.f, 3.f);
94        mat3 m40 = mat3::rotate(40.f, 1.f, 2.f, 3.f);
95        mat3 m20x20 = m20 * m20;
96
97        LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[0][0], m40[0][0], 1e-5);
98        LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[1][0], m40[1][0], 1e-5);
99        LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[2][0], m40[2][0], 1e-5);
100
101        LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[0][1], m40[0][1], 1e-5);
102        LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[1][1], m40[1][1], 1e-5);
103        LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[2][1], m40[2][1], 1e-5);
104
105        LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[0][2], m40[0][2], 1e-5);
106        LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[1][2], m40[1][2], 1e-5);
107        LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[2][2], m40[2][2], 1e-5);
108    }
109
110    LOLUNIT_TEST(QuaternionTransform)
111    {
112        /* Rotating using a quaternion must equal rotating using a matrix */
113        mat3 m20 = mat3::rotate(20.f, 1.f, 2.f, 3.f);
114        quat q20 = quat::rotate(20.f, 1.f, 2.f, 3.f);
115        vec3 a(-2.f, 4.f, 3.f);
116
117        vec3 b = m20 * a;
118        vec3 c = q20.transform(a);
119
120        LOLUNIT_ASSERT_DOUBLES_EQUAL(c.x, b.x, 1e-5);
121        LOLUNIT_ASSERT_DOUBLES_EQUAL(c.y, b.y, 1e-5);
122        LOLUNIT_ASSERT_DOUBLES_EQUAL(c.z, b.z, 1e-5);
123
124        float n = norm(q20);
125
126        LOLUNIT_ASSERT_DOUBLES_EQUAL(n, 1.0, 1e-5);
127    }
128
129    LOLUNIT_TEST(QuaternionFromMatrix)
130    {
131        /* A rotation matrix converted to a quaternion should match the
132         * quaternion built with the same parameters */
133        quat q1 = quat::rotate(20.f, 1.f, 2.f, 3.f);
134        quat q2 = quat(mat3::rotate(20.f, 1.f, 2.f, 3.f));
135
136        LOLUNIT_ASSERT_DOUBLES_EQUAL(q2.w, q1.w, 1e-5);
137        LOLUNIT_ASSERT_DOUBLES_EQUAL(q2.x, q1.x, 1e-5);
138        LOLUNIT_ASSERT_DOUBLES_EQUAL(q2.y, q1.y, 1e-5);
139        LOLUNIT_ASSERT_DOUBLES_EQUAL(q2.z, q1.z, 1e-5);
140
141        float n1 = norm(q1);
142        float n2 = norm(q2);
143
144        LOLUNIT_ASSERT_DOUBLES_EQUAL(n1, 1.0, 1e-5);
145        LOLUNIT_ASSERT_DOUBLES_EQUAL(n2, 1.0, 1e-5);
146    }
147
148    LOLUNIT_TEST(MatrixFromQuaternion)
149    {
150        /* A quaternion converted to a rotation matrix should match the
151         * rotation matrix built with the same parameters */
152        mat3 m1 = mat3::rotate(60.f, 1.f, -2.f, 3.f);
153        mat3 m2 = mat3(quat::rotate(60.f, 1.f, -2.f, 3.f));
154
155        LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[0][0], m1[0][0], 1e-5);
156        LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[1][0], m1[1][0], 1e-5);
157        LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[2][0], m1[2][0], 1e-5);
158
159        LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[0][1], m1[0][1], 1e-5);
160        LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[1][1], m1[1][1], 1e-5);
161        LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[2][1], m1[2][1], 1e-5);
162
163        LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[0][2], m1[0][2], 1e-5);
164        LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[1][2], m1[1][2], 1e-5);
165        LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[2][2], m1[2][2], 1e-5);
166
167        float d1 = determinant(m1);
168        float d2 = determinant(m2);
169
170        LOLUNIT_ASSERT_DOUBLES_EQUAL(d1, 1.0, 1e-5);
171        LOLUNIT_ASSERT_DOUBLES_EQUAL(d2, 1.0, 1e-5);
172    }
173
174    LOLUNIT_TEST(MatrixCompositionThroughQuaternions)
175    {
176        /* Combining two rotation matrices should match the matrix created
177         * from the combination of the two equivalent quaternions */
178        mat3 m1 = mat3::rotate(60.f, 1.f, -2.f, 3.f);
179        mat3 m2 = mat3::rotate(20.f, -3.f, 1.f, -3.f);
180
181        mat3 m3 = m2 * m1;
182        mat3 m4(quat(m2) * quat(m1));
183
184        LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[0][0], m3[0][0], 1e-5);
185        LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[1][0], m3[1][0], 1e-5);
186        LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[2][0], m3[2][0], 1e-5);
187
188        LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[0][1], m3[0][1], 1e-5);
189        LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[1][1], m3[1][1], 1e-5);
190        LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[2][1], m3[2][1], 1e-5);
191
192        LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[0][2], m3[0][2], 1e-5);
193        LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[1][2], m3[1][2], 1e-5);
194        LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[2][2], m3[2][2], 1e-5);
195    }
196
197    LOLUNIT_TEST(QuaternionCompositionThroughMatrices)
198    {
199        /* Combining two quaternions should match the quaternion created
200         * from the combination of the two equivalent rotation matrices */
201        quat q1 = quat::rotate(60.f, 1.f, -2.f, 3.f);
202        quat q2 = quat::rotate(20.f, -3.f, 1.f, -2.f);
203
204        quat q3 = q2 * q1;
205        quat q4(mat3(q2) * mat3(q1));
206
207        LOLUNIT_ASSERT_DOUBLES_EQUAL(q4.w, q3.w, 1e-5);
208        LOLUNIT_ASSERT_DOUBLES_EQUAL(q4.x, q3.x, 1e-5);
209        LOLUNIT_ASSERT_DOUBLES_EQUAL(q4.y, q3.y, 1e-5);
210        LOLUNIT_ASSERT_DOUBLES_EQUAL(q4.z, q3.z, 1e-5);
211    }
212};
213
214} /* namespace lol */
215
Note: See TracBrowser for help on using the repository browser.