source: trunk/test/unit/quat.cpp @ 1412

Last change on this file since 1412 was 1341, checked in by sam, 10 years ago

math: allow to write 1.f / q to take a quaternion's inverse.

  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 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(QuaternionTest)
22{
23    void SetUp() {}
24
25    void TearDown() {}
26
27    LOLUNIT_TEST(Equality)
28    {
29        quat a4(1.f, 2.f, 3.f, 4.f);
30        quat b4(0.f, 2.f, 3.f, 4.f);
31        quat c4(1.f, 0.f, 3.f, 4.f);
32        quat d4(1.f, 2.f, 0.f, 4.f);
33        quat e4(1.f, 2.f, 3.f, 0.f);
34
35        LOLUNIT_ASSERT_EQUAL(a4, a4);
36        LOLUNIT_ASSERT_NOT_DIFFERENT(a4, a4);
37
38        LOLUNIT_ASSERT_DIFFERENT(a4, b4);
39        LOLUNIT_ASSERT_NOT_EQUAL(a4, b4);
40        LOLUNIT_ASSERT_DIFFERENT(a4, c4);
41        LOLUNIT_ASSERT_NOT_EQUAL(a4, c4);
42        LOLUNIT_ASSERT_DIFFERENT(a4, d4);
43        LOLUNIT_ASSERT_NOT_EQUAL(a4, d4);
44        LOLUNIT_ASSERT_DIFFERENT(a4, e4);
45        LOLUNIT_ASSERT_NOT_EQUAL(a4, e4);
46    }
47
48    LOLUNIT_TEST(UnaryMinus)
49    {
50        quat a(1.f, 3.f, 2.f, 4.f);
51        quat b(-1.f, -3.f, -2.f, -4.f);
52
53        LOLUNIT_ASSERT_EQUAL(a, -b);
54        LOLUNIT_ASSERT_EQUAL(-a, b);
55    }
56
57    LOLUNIT_TEST(Conjugate)
58    {
59        quat a(1.f, 3.f, 2.f, 4.f);
60        quat b(1.f, -3.f, -2.f, -4.f);
61
62        LOLUNIT_ASSERT_EQUAL(a, ~b);
63        LOLUNIT_ASSERT_EQUAL(~a, b);
64    }
65
66    LOLUNIT_TEST(Norm)
67    {
68        quat a(2.f, -2.f, -8.f, 3.f);
69
70        LOLUNIT_ASSERT_EQUAL(norm(a), 81.f);
71
72        quat b = a * ~a;
73        quat c(norm(a), 0.f, 0.f, 0.f);
74
75        LOLUNIT_ASSERT_EQUAL(b, c);
76
77        quat d(2.f, 3.f, -4.f, -1.f);
78
79        LOLUNIT_ASSERT_EQUAL(norm(a * d), norm(a) * norm(d));
80    }
81
82    LOLUNIT_TEST(Base)
83    {
84        quat one(1.f, 0.f, 0.f, 0.f);
85        quat i(0.f, 1.f, 0.f, 0.f);
86        quat j(0.f, 0.f, 1.f, 0.f);
87        quat k(0.f, 0.f, 0.f, 1.f);
88
89        LOLUNIT_ASSERT_EQUAL(norm(one), 1.f);
90        LOLUNIT_ASSERT_EQUAL(norm(i), 1.f);
91        LOLUNIT_ASSERT_EQUAL(norm(j), 1.f);
92        LOLUNIT_ASSERT_EQUAL(norm(k), 1.f);
93
94        LOLUNIT_ASSERT_EQUAL(i * i, -one);
95        LOLUNIT_ASSERT_EQUAL(j * j, -one);
96        LOLUNIT_ASSERT_EQUAL(k * k, -one);
97        LOLUNIT_ASSERT_EQUAL(i * j * k, -one);
98
99        LOLUNIT_ASSERT_EQUAL(i * j, k);
100        LOLUNIT_ASSERT_EQUAL(j * i, -k);
101        LOLUNIT_ASSERT_EQUAL(j * k, i);
102        LOLUNIT_ASSERT_EQUAL(k * j, -i);
103        LOLUNIT_ASSERT_EQUAL(k * i, j);
104        LOLUNIT_ASSERT_EQUAL(i * k, -j);
105    }
106
107    LOLUNIT_TEST(Normalize)
108    {
109        quat a(2.f, -2.f, -8.f, 3.f);
110        quat b = normalize(a);
111
112        LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(b), 1.0, 1e-5);
113    }
114
115    LOLUNIT_TEST(Reciprocal)
116    {
117        quat a(2.f, -2.f, -8.f, 3.f);
118        quat b = re(a);
119        quat c = 1.f / a;
120
121        LOLUNIT_ASSERT_DOUBLES_EQUAL(b.w, c.w, 1e-5);
122        LOLUNIT_ASSERT_DOUBLES_EQUAL(b.x, c.x, 1e-5);
123        LOLUNIT_ASSERT_DOUBLES_EQUAL(b.y, c.y, 1e-5);
124        LOLUNIT_ASSERT_DOUBLES_EQUAL(b.z, c.z, 1e-5);
125
126        quat m1 = a * b;
127        quat m2 = b * a;
128        LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.w, m2.w, 1e-5);
129        LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.x, m2.x, 1e-5);
130        LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.y, m2.y, 1e-5);
131        LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.z, m2.z, 1e-5);
132
133        LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.w, 1.0, 1e-5);
134        LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.x, 0.0, 1e-5);
135        LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.y, 0.0, 1e-5);
136        LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.z, 0.0, 1e-5);
137    }
138
139    LOLUNIT_TEST(Rotation)
140    {
141        /* Check that rotating 10 degrees twice means rotating 20 degrees */
142        quat a = quat::rotate(10.f, vec3(1, 0, 0));
143        quat b = quat::rotate(20.f, vec3(1, 0, 0));
144        quat c = a * a;
145
146        LOLUNIT_ASSERT_DOUBLES_EQUAL(c.w, b.w, 1e-5);
147        LOLUNIT_ASSERT_DOUBLES_EQUAL(c.x, b.x, 1e-5);
148        LOLUNIT_ASSERT_DOUBLES_EQUAL(c.y, b.y, 1e-5);
149        LOLUNIT_ASSERT_DOUBLES_EQUAL(c.z, b.z, 1e-5);
150
151        /* Check that rotating 10 degrees then 20 is the same as 20 then 10 */
152        quat d = a * b;
153        quat e = b * a;
154
155        LOLUNIT_ASSERT_DOUBLES_EQUAL(e.w, d.w, 1e-5);
156        LOLUNIT_ASSERT_DOUBLES_EQUAL(e.x, d.x, 1e-5);
157        LOLUNIT_ASSERT_DOUBLES_EQUAL(e.y, d.y, 1e-5);
158        LOLUNIT_ASSERT_DOUBLES_EQUAL(e.z, d.z, 1e-5);
159    }
160};
161
162} /* namespace lol */
163
Note: See TracBrowser for help on using the repository browser.