~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to tests/testQuaternion.cpp

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2008-2011 Colin MacDonald
 
2
// No rights reserved: this software is in the public domain.
 
3
 
 
4
#include "testUtils.h"
 
5
 
 
6
using namespace irr;
 
7
 
 
8
namespace
 
9
{
 
10
inline bool compareQ(const core::vector3df& v, const core::vector3df& turn=core::vector3df(0,0,1))
 
11
{
 
12
        core::quaternion q(v*core::DEGTORAD);
 
13
        core::vector3df v2;
 
14
 
 
15
        const core::vector3df v3=v.rotationToDirection(turn);
 
16
        if (!v3.equals(q*turn, 0.002f))
 
17
        {
 
18
                logTestString("Inequality before quat.toEuler(): %f,%f,%f\n", v.X,v.Y,v.Z);
 
19
                return false;
 
20
        }
 
21
        
 
22
        q.toEuler(v2);
 
23
        v2*=core::RADTODEG;
 
24
        v2=v2.rotationToDirection(turn);
 
25
 
 
26
        // this yields pretty far values sometimes, so don't be too picky
 
27
        if (!v3.equals(v2, 0.0035f))
 
28
        {
 
29
                logTestString("Inequality: %f,%f,%f != %f,%f,%f\n", v.X,v.Y,v.Z, v2.X,v2.Y,v2.Z);
 
30
                return false;
 
31
        }
 
32
        return true;
 
33
}
 
34
 
 
35
core::vector3df vals[] = {
 
36
        core::vector3df(0.f, 0.f, 0.f),
 
37
        core::vector3df(0.f, 0.f, 24.04f),
 
38
        core::vector3df(0.f, 0.f, 71.f),
 
39
        core::vector3df(0.f, 0.f, 71.19f),
 
40
        core::vector3df(0.f, 0.f, 80.f),
 
41
        core::vector3df(0.f, 0.f, 103.99f),
 
42
        core::vector3df(0.f, 0.f, 261.73f),
 
43
        core::vector3df(0.f, 0.f, 276.f),
 
44
        core::vector3df(0.f, 0.f, 286.29f),
 
45
        core::vector3df(0.f, 0.f, 295.f),
 
46
        core::vector3df(0.f, 0.f, 318.3f),
 
47
        core::vector3df(360.f, 75.55f, 155.89f),
 
48
        core::vector3df(0.f, 90.f, 159.51f),
 
49
        core::vector3df(0.f, 90.f, 249.48f),
 
50
        core::vector3df(0.f, 90.f, 269.91f),
 
51
        core::vector3df(0.f, 90.f, 270.f),
 
52
        core::vector3df(0.f, 284.45f, 155.89f),
 
53
        core::vector3df(0.01f, 0.42f, 90.38f),
 
54
        core::vector3df(0.04f, 359.99f, 9.5f),
 
55
        core::vector3df(0.34f, 89.58f, 360.f),
 
56
        core::vector3df(0.58f, 4.36f, 334.36f),
 
57
        core::vector3df(3.23f, 359.65f, 10.17f),
 
58
        core::vector3df(3.23f, 359.65f, 10.21f),
 
59
        core::vector3df(4.85f, 359.3f, 94.33f),
 
60
        core::vector3df(8.90f, 6.63f, 9.27f),
 
61
        core::vector3df(11.64f, 311.52f, 345.35f),
 
62
        core::vector3df(12.1f, 4.72f, 11.24f),
 
63
        core::vector3df(14.63f, 48.72f, 31.79f),
 
64
        core::vector3df(76.68f, 1.11f, 18.65f),
 
65
        core::vector3df(90.f, 0.f, 0.f),
 
66
        core::vector3df(90.01f, 270.49f, 360.f),
 
67
        core::vector3df(90.95f, 0.f, 0.f),
 
68
        core::vector3df(173.58f, 348.13f, 132.25f),
 
69
        core::vector3df(115.52f, 89.04f, 205.51f),
 
70
        core::vector3df(179.3f, 359.18f, 0.58f),
 
71
        core::vector3df(180.09f, 270.06f, 0.f),
 
72
        core::vector3df(180.41f, 359.94f, 179.69f),
 
73
        core::vector3df(180.92f, 10.79f, 144.53f),
 
74
        core::vector3df(181.95f, 270.03f, 0.f),
 
75
        core::vector3df(269.05f, 0.f, 0.f),
 
76
        core::vector3df(269.99f, 270.49f, 360.f),
 
77
        core::vector3df(283.32f, 358.89f, 18.65f),
 
78
        core::vector3df(347.9f, 355.28f, 11.24f),
 
79
        core::vector3df(351.1f, 353.37f, 9.27f),
 
80
        core::vector3df(355.82f, 345.96f, 273.26f),
 
81
        core::vector3df(358.24f, 358.07f, 342.82f),
 
82
        core::vector3df(359.78f, 357.69f, 7.52f),
 
83
        core::vector3df(359.96f, 0.01f, 9.5f),
 
84
        core::vector3df(-57.197479f,-90.f,0.f),
 
85
        core::vector3df(-57.187481f,-90.f,0.f)
 
86
};
 
87
 
 
88
bool testEulerConversion()
 
89
{
 
90
        bool result = true;
 
91
        for (u32 i=0; i<sizeof(vals)/sizeof(vals[0]); ++i)
 
92
        {
 
93
                // make sure the rotations work with different turn vectors
 
94
                result &= compareQ(vals[i]) && compareQ(vals[i], core::vector3df(1,2,3)) &&
 
95
                        compareQ(vals[i], core::vector3df(0,1,0));
 
96
        }
 
97
        return result;
 
98
}
 
99
 
 
100
bool testRotationFromTo()
 
101
{
 
102
        bool result = true;
 
103
        core::quaternion q1;
 
104
        core::matrix4 mat;
 
105
        core::quaternion q4(mat);
 
106
 
 
107
        q4.rotationFromTo(core::vector3df(1.f,0.f,0.f), core::vector3df(1.f,0.f,0.f));
 
108
        if (q4 != q1)
 
109
        {
 
110
                logTestString("Quaternion rotationFromTo method did not yield identity.\n");
 
111
                result = false;
 
112
        }
 
113
 
 
114
        q1.set(0.f,0.f,core::PI);
 
115
        core::quaternion q2(0.f,core::PI,0.f);
 
116
        q4.rotationFromTo(core::vector3df(1.f,0.f,0.f), core::vector3df(-1.f,0.f,0.f));
 
117
        if ((q4 != q1)&&(q4 != q2))
 
118
        {
 
119
                logTestString("Quaternion rotationFromTo method did not yield x flip.\n");
 
120
                result = false;
 
121
        }
 
122
        q4.rotationFromTo(core::vector3df(10.f,20.f,30.f), core::vector3df(-10.f,-20.f,-30.f));
 
123
        if ((q4 != q1)&&(q4 != q2))
 
124
        {
 
125
                logTestString("Quaternion rotationFromTo method did not yield x flip for non-axis.\n");
 
126
                result = false;
 
127
        }
 
128
 
 
129
        q1.set(0.f,0.f,core::PI/2);
 
130
        q4.rotationFromTo(core::vector3df(1.f,0.f,0.f), core::vector3df(0.f,1.f,0.f));
 
131
        if (!q4.equals(q1))
 
132
        {
 
133
                logTestString("Quaternion rotationFromTo method did not yield 90 degree rotation.\n");
 
134
                result = false;
 
135
        }
 
136
        return result;
 
137
}
 
138
 
 
139
bool testInterpolation()
 
140
{
 
141
        bool result=true;
 
142
        core::quaternion q(1.f,2.f,3.f,4.f);
 
143
        core::quaternion q2;
 
144
        q2.lerp(q,q,0);
 
145
        if (q != q2)
 
146
        {
 
147
                logTestString("Quaternion lerp with same quaternion did not yield same quaternion back (with t==0).\n");
 
148
                result = false;
 
149
        }
 
150
        q2.lerp(q,q,0.5f);
 
151
        if (q != q2)
 
152
        {
 
153
                logTestString("Quaternion lerp with same quaternion did not yield same quaternion back (with t==0.5).\n");
 
154
                result = false;
 
155
        }
 
156
        q2.lerp(q,q,1);
 
157
        if (q != q2)
 
158
        {
 
159
                logTestString("Quaternion lerp with same quaternion did not yield same quaternion back (with t==1).\n");
 
160
                result = false;
 
161
        }
 
162
        q2.lerp(q,q,0.2345f);
 
163
        if (q != q2)
 
164
        {
 
165
                logTestString("Quaternion lerp with same quaternion did not yield same quaternion back (with t==0.2345).\n");
 
166
                result = false;
 
167
        }
 
168
        q2.slerp(q,q,0);
 
169
        if (q != q2)
 
170
        {
 
171
                logTestString("Quaternion slerp with same quaternion did not yield same quaternion back (with t==0).\n");
 
172
                result = false;
 
173
        }
 
174
        q2.slerp(q,q,0.5f);
 
175
        if (q != q2)
 
176
        {
 
177
                logTestString("Quaternion slerp with same quaternion did not yield same quaternion back (with t==0.5).\n");
 
178
                result = false;
 
179
        }
 
180
        q2.slerp(q,q,1);
 
181
        if (q != q2)
 
182
        {
 
183
                logTestString("Quaternion slerp with same quaternion did not yield same quaternion back (with t==1).\n");
 
184
                result = false;
 
185
        }
 
186
        q2.slerp(q,q,0.2345f);
 
187
        if (q != q2)
 
188
        {
 
189
                logTestString("Quaternion slerp with same quaternion did not yield same quaternion back (with t==0.2345).\n");
 
190
                result = false;
 
191
        }
 
192
        core::quaternion q3(core::vector3df(45,135,85)*core::DEGTORAD);
 
193
        q.set(core::vector3df(35,125,75)*core::DEGTORAD);
 
194
        q2.slerp(q,q3,0);
 
195
        if (q != q2)
 
196
        {
 
197
                logTestString("Quaternion slerp with different quaternions did not yield first quaternion back (with t==0).\n");
 
198
                result = false;
 
199
        }
 
200
        q2.slerp(q,q3,1);
 
201
        if (q3 != q2)
 
202
        {
 
203
                logTestString("Quaternion slerp with different quaternions did not yield second quaternion back (with t==1).\n");
 
204
                result = false;
 
205
        }
 
206
        q2.slerp(q,q3,0.5);
 
207
        if (!q2.equals(core::quaternion(-0.437f,0.742f,0.017f,0.506f),0.001f))
 
208
        {
 
209
                logTestString("Quaternion slerp with different quaternions did not yield correct result (with t==0.5).\n");
 
210
                result = false;
 
211
        }
 
212
        q2.slerp(q,q3,0.2345f);
 
213
        if (!q2.equals(core::quaternion(-0.4202f,0.7499f,0.03814f,0.5093f),0.0007f))
 
214
        {
 
215
                logTestString("Quaternion slerp with different quaternions did not yield correct result (with t==0.2345).\n");
 
216
                result = false;
 
217
        }
 
218
        return result;
 
219
}
 
220
}
 
221
 
 
222
bool testQuaternion(void)
 
223
{
 
224
        bool result = true;
 
225
 
 
226
        core::quaternion q1;
 
227
        if ((q1.W != 1.f)||(q1.X != 0.f)||(q1.Y != 0.f)||(q1.Z != 0.f))
 
228
        {
 
229
                logTestString("Default constructor did not create proper quaternion.\n");
 
230
                result = false;
 
231
        }
 
232
 
 
233
        core::quaternion q2(1.f,2.f,3.f,4.f);
 
234
        if ((q2.W != 4.f)||(q2.X != 1.f)||(q2.Y != 2.f)||(q2.Z != 3.f))
 
235
        {
 
236
                logTestString("Element constructor did not create proper quaternion.\n");
 
237
                result = false;
 
238
        }
 
239
 
 
240
        q2.set(4.f,3.f,2.f,1.f);
 
241
        if ((q2.W != 1.f)||(q2.X != 4.f)||(q2.Y != 3.f)||(q2.Z != 2.f))
 
242
        {
 
243
                logTestString("Quaternion set method not working(1).\n");
 
244
                result = false;
 
245
        }
 
246
        q2.set(0.f,0.f,0.f,1.f);
 
247
        if ((q2.W != 1.f)||(q2.X != 0.f)||(q2.Y != 0.f)||(q2.Z != 0.f))
 
248
        {
 
249
                logTestString("Quaternion set method not working(2).\n");
 
250
                result = false;
 
251
        }
 
252
        if (q1 != q2)
 
253
        {
 
254
                logTestString("Quaternion equals method not working.\n");
 
255
                result = false;
 
256
        }
 
257
 
 
258
        result &= testRotationFromTo();
 
259
        result &= testInterpolation();
 
260
        result &= testEulerConversion();
 
261
 
 
262
        return result;
 
263
}