aboutsummaryrefslogtreecommitdiff
path: root/tests/testQuaternion.cpp
blob: f6d976ceeddceda10124289abe46709658476455 (plain)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (C) 2008-2011 Colin MacDonald
// No rights reserved: this software is in the public domain.

#include "testUtils.h"

using namespace irr;

namespace
{
inline bool compareQ(const core::vector3df& v, const core::vector3df& turn=core::vector3df(0,0,1))
{
	core::quaternion q(v*core::DEGTORAD);
	core::vector3df v2;

	const core::vector3df v3=v.rotationToDirection(turn);
	if (!v3.equals(q*turn, 0.002f))
	{
		logTestString("Inequality before quat.toEuler(): %f,%f,%f\n", v.X,v.Y,v.Z);
		return false;
	}
	
	q.toEuler(v2);
	v2*=core::RADTODEG;
	v2=v2.rotationToDirection(turn);

	// this yields pretty far values sometimes, so don't be too picky
	if (!v3.equals(v2, 0.0035f))
	{
		logTestString("Inequality: %f,%f,%f != %f,%f,%f\n", v.X,v.Y,v.Z, v2.X,v2.Y,v2.Z);
		return false;
	}
	return true;
}

const core::vector3df vals[] = {
	core::vector3df(0.f, 0.f, 0.f),
	core::vector3df(0.f, 0.f, 24.04f),
	core::vector3df(0.f, 0.f, 71.f),
	core::vector3df(0.f, 0.f, 71.19f),
	core::vector3df(0.f, 0.f, 80.f),
	core::vector3df(0.f, 0.f, 103.99f),
	core::vector3df(0.f, 0.f, 261.73f),
	core::vector3df(0.f, 0.f, 276.f),
	core::vector3df(0.f, 0.f, 286.29f),
	core::vector3df(0.f, 0.f, 295.f),
	core::vector3df(0.f, 0.f, 318.3f),
	core::vector3df(360.f, 75.55f, 155.89f),
	core::vector3df(0.f, 90.f, 159.51f),
	core::vector3df(0.f, 90.f, 249.48f),
	core::vector3df(0.f, 90.f, 269.91f),
	core::vector3df(0.f, 90.f, 270.f),
	core::vector3df(0.f, 284.45f, 155.89f),
	core::vector3df(0.01f, 0.42f, 90.38f),
	core::vector3df(0.04f, 359.99f, 9.5f),
	core::vector3df(0.34f, 89.58f, 360.f),
	core::vector3df(0.58f, 4.36f, 334.36f),
	core::vector3df(3.23f, 359.65f, 10.17f),
	core::vector3df(3.23f, 359.65f, 10.21f),
	core::vector3df(4.85f, 359.3f, 94.33f),
	core::vector3df(8.90f, 6.63f, 9.27f),
	core::vector3df(11.64f, 311.52f, 345.35f),
	core::vector3df(12.1f, 4.72f, 11.24f),
	core::vector3df(14.63f, 48.72f, 31.79f),
	core::vector3df(76.68f, 1.11f, 18.65f),
	core::vector3df(90.f, 0.f, 0.f),
	core::vector3df(90.01f, 270.49f, 360.f),
	core::vector3df(90.95f, 0.f, 0.f),
	core::vector3df(173.58f, 348.13f, 132.25f),
	core::vector3df(115.52f, 89.04f, 205.51f),
	core::vector3df(179.3f, 359.18f, 0.58f),
	core::vector3df(180.09f, 270.06f, 0.f),
	core::vector3df(180.41f, 359.94f, 179.69f),
	core::vector3df(180.92f, 10.79f, 144.53f),
	core::vector3df(181.95f, 270.03f, 0.f),
	core::vector3df(269.05f, 0.f, 0.f),
	core::vector3df(269.99f, 270.49f, 360.f),
	core::vector3df(283.32f, 358.89f, 18.65f),
	core::vector3df(347.9f, 355.28f, 11.24f),
	core::vector3df(351.1f, 353.37f, 9.27f),
	core::vector3df(355.82f, 345.96f, 273.26f),
	core::vector3df(358.24f, 358.07f, 342.82f),
	core::vector3df(359.78f, 357.69f, 7.52f),
	core::vector3df(359.96f, 0.01f, 9.5f),
	core::vector3df(-57.197479f,-90.f,0.f),
	core::vector3df(-57.187481f,-90.f,0.f)
};

bool testEulerConversion()
{
	bool result = true;
	for (u32 i=0; i<sizeof(vals)/sizeof(vals[0]); ++i)
	{
		// make sure the rotations work with different turn vectors
		result &= compareQ(vals[i]) && compareQ(vals[i], core::vector3df(1,2,3)) &&
			compareQ(vals[i], core::vector3df(0,1,0));
	}
	return result;
}

bool testRotationFromTo()
{
	bool result = true;
	core::quaternion q;

	q.rotationFromTo(core::vector3df(1.f,0.f,0.f), core::vector3df(1.f,0.f,0.f));
	if (q != core::quaternion())
	{
		logTestString("Quaternion rotationFromTo method did not yield identity.\n");
		result = false;
	}

	core::vector3df from(1.f,0.f,0.f);
	q.rotationFromTo(from, core::vector3df(-1.f,0.f,0.f));
	from=q*from;
	if (from != core::vector3df(-1.f,0.f,0.f))
	{
		logTestString("Quaternion rotationFromTo method did not yield x flip.\n");
		result = false;
	}

	from.set(1.f,2.f,3.f);
	q.rotationFromTo(from, core::vector3df(-1.f,-2.f,-3.f));
	from=q*from;
	if (from != core::vector3df(-1.f,-2.f,-3.f))
	{
		logTestString("Quaternion rotationFromTo method did not yield x flip for non-axis.\n");
		result = false;
	}

	from.set(1.f,0.f,0.f);
	q.rotationFromTo(from, core::vector3df(0.f,1.f,0.f));
	from=q*from;
	if (from != core::vector3df(0.f,1.f,0.f))
	{
		logTestString("Quaternion rotationFromTo method did not yield 90 degree rotation.\n");
		result = false;
	}

	for (u32 i=1; i<sizeof(vals)/sizeof(vals[0])-1; ++i)
	{
		from.set(vals[i]).normalize();
		core::vector3df to(vals[i+1]);
		to.normalize();
		q.rotationFromTo(from, to);
		from = q*from;
		result &= (from.equals(to, 0.00012f));
	}

	return result;
}

bool testInterpolation()
{
	bool result=true;
	core::quaternion q(1.f,2.f,3.f,4.f);
	core::quaternion q2;
	q2.lerp(q,q,0);
	if (q != q2)
	{
		logTestString("Quaternion lerp with same quaternion did not yield same quaternion back (with t==0).\n");
		result = false;
	}
	q2.lerp(q,q,0.5f);
	if (q != q2)
	{
		logTestString("Quaternion lerp with same quaternion did not yield same quaternion back (with t==0.5).\n");
		result = false;
	}
	q2.lerp(q,q,1);
	if (q != q2)
	{
		logTestString("Quaternion lerp with same quaternion did not yield same quaternion back (with t==1).\n");
		result = false;
	}
	q2.lerp(q,q,0.2345f);
	if (q != q2)
	{
		logTestString("Quaternion lerp with same quaternion did not yield same quaternion back (with t==0.2345).\n");
		result = false;
	}
	q2.slerp(q,q,0);
	if (q != q2)
	{
		logTestString("Quaternion slerp with same quaternion did not yield same quaternion back (with t==0).\n");
		result = false;
	}
	q2.slerp(q,q,0.5f);
	if (q != q2)
	{
		logTestString("Quaternion slerp with same quaternion did not yield same quaternion back (with t==0.5).\n");
		result = false;
	}
	q2.slerp(q,q,1);
	if (q != q2)
	{
		logTestString("Quaternion slerp with same quaternion did not yield same quaternion back (with t==1).\n");
		result = false;
	}
	q2.slerp(q,q,0.2345f);
	if (q != q2)
	{
		logTestString("Quaternion slerp with same quaternion did not yield same quaternion back (with t==0.2345).\n");
		result = false;
	}
	core::quaternion q3(core::vector3df(45,135,85)*core::DEGTORAD);
	q.set(core::vector3df(35,125,75)*core::DEGTORAD);
	q2.slerp(q,q3,0);
	if (q != q2)
	{
		logTestString("Quaternion slerp with different quaternions did not yield first quaternion back (with t==0).\n");
		result = false;
	}
	q2.slerp(q,q3,1);
	if (q3 != q2)
	{
		logTestString("Quaternion slerp with different quaternions did not yield second quaternion back (with t==1).\n");
		result = false;
	}
	q2.slerp(q,q3,0.5);
	if (!q2.equals(core::quaternion(-0.437f,0.742f,0.017f,0.506f),0.001f))
	{
		logTestString("Quaternion slerp with different quaternions did not yield correct result (with t==0.5).\n");
		result = false;
	}
	q2.slerp(q,q3,0.2345f);
	if (!q2.equals(core::quaternion(-0.4202f,0.7499f,0.03814f,0.5093f),0.0007f))
	{
		logTestString("Quaternion slerp with different quaternions did not yield correct result (with t==0.2345).\n");
		result = false;
	}
	return result;
}
}

bool testQuaternion(void)
{
	bool result = true;

	core::quaternion q1;
	if ((q1.W != 1.f)||(q1.X != 0.f)||(q1.Y != 0.f)||(q1.Z != 0.f))
	{
		logTestString("Default constructor did not create proper quaternion.\n");
		result = false;
	}

	core::quaternion q2(1.f,2.f,3.f,4.f);
	if ((q2.W != 4.f)||(q2.X != 1.f)||(q2.Y != 2.f)||(q2.Z != 3.f))
	{
		logTestString("Element constructor did not create proper quaternion.\n");
		result = false;
	}

	q2.set(4.f,3.f,2.f,1.f);
	if ((q2.W != 1.f)||(q2.X != 4.f)||(q2.Y != 3.f)||(q2.Z != 2.f))
	{
		logTestString("Quaternion set method not working(1).\n");
		result = false;
	}
	q2.set(0.f,0.f,0.f,1.f);
	if ((q2.W != 1.f)||(q2.X != 0.f)||(q2.Y != 0.f)||(q2.Z != 0.f))
	{
		logTestString("Quaternion set method not working(2).\n");
		result = false;
	}
	if (q1 != q2)
	{
		logTestString("Quaternion equals method not working.\n");
		result = false;
	}

	result &= testRotationFromTo();
	result &= testInterpolation();
	result &= testEulerConversion();

	return result;
}