aboutsummaryrefslogtreecommitdiff
path: root/tests/testVector3d.cpp
blob: 4eca17ef2f1cd6282e85382f889a37c0c9416334 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (C) 2008-2011 Colin MacDonald
// No rights reserved: this software is in the public domain.

#include "testUtils.h"

using namespace irr;
using namespace core;

#define EQUAL_VECTORS(compare, with)\
	if(!equalVectors(cmp_equal<core::vector3d<T> >(compare), with)) {assert(false); return false;}

#define LESS_VECTORS(compare, with)\
	if(!equalVectors(cmp_less<core::vector3d<T> >(compare), with)) return false;

#define LESS_EQUAL_VECTORS(compare, with)\
	if(!equalVectors(cmp_less_equal<core::vector3d<T> >(compare), with)) return false;

#define MORE_VECTORS(compare, with)\
	if(!equalVectors(cmp_more<core::vector3d<T> >(compare), with)) return false;

#define MORE_EQUAL_VECTORS(compare, with)\
	if(!equalVectors(cmp_more_equal<core::vector3d<T> >(compare), with)) return false;

// check if the vector contains a NAN (a==b is guaranteed to return false in this case)
template<class T>
static bool is_nan(const core::vector3d<T> &vec )
{
	return ( !(vec.X == vec.X)
			|| !(vec.Y == vec.Y)
			|| !(vec.Z == vec.Z) );
}

template<class T>
struct cmp_less
{
	cmp_less(const T& a) : val(a) {}
	bool operator()(const T& other) const
	{
		return val<other;
	}
	const char* getName() const {return "<";}
	const T val;
};

template<class T>
struct cmp_less_equal
{
	cmp_less_equal(const T& a) : val(a) {}
	bool operator()(const T& other) const
	{
		return val<=other;
	}
	const char* getName() const {return "<=";}
	const T val;
};

template<class T>
struct cmp_more
{
	cmp_more(const T& a) : val(a) {}
	bool operator()(const T& other) const
	{
		return val>other;
	}
	const char* getName() const {return ">";}
	const T val;
};

template<class T>
struct cmp_more_equal
{
	cmp_more_equal(const T& a) : val(a) {}
	bool operator()(const T& other) const
	{
		return val>=other;
	}
	const char* getName() const {return ">=";}
	const T val;
};

template<class T>
struct cmp_equal
{
	cmp_equal(const T& a) : val(a) {}
	bool operator()(const T& other) const
	{
		return val.equals(other);
	}
	const char* getName() const {return "==";}
	const T val;
};

template<class S, class T>
static bool equalVectors(const S& compare,
			   const core::vector3d<T> & with)
{
	if (!compare(with))
	{
		logTestString("\nERROR: vector3d %.16f, %.16f, %.16f %s vector3d %.16f, %.16f, %.16f\n",
			(f64)compare.val.X, (f64)compare.val.Y, (f64)compare.val.Z, compare.getName(),
			(f64)with.X, (f64)with.Y, (f64)with.Z);
		assert(compare(with));
		return false;
	}

	return true;
}

template <class T>
static bool checkInterpolation()
{
	core::vector3d<T> vec(5, 5, 0);
	core::vector3d<T> otherVec(10, 20, 40);

	vector3d<T> interpolated;
	(void)interpolated.interpolate(vec, otherVec, 0.f);
	EQUAL_VECTORS(interpolated, otherVec); // 0.f means all the second vector

	(void)interpolated.interpolate(vec, otherVec, 0.25f);
	EQUAL_VECTORS(interpolated, vector3d<T>((T)8.75, (T)16.25, 30));

	(void)interpolated.interpolate(vec, otherVec, 0.75f);
	EQUAL_VECTORS(interpolated, vector3d<T>((T)6.25, (T)8.75, 10));

	(void)interpolated.interpolate(vec, otherVec, 1.f);
	EQUAL_VECTORS(interpolated, vec); // 1.f means all the first vector


	interpolated = vec.getInterpolated(otherVec, 0.f);
	EQUAL_VECTORS(interpolated, otherVec); // 0.f means all the second vector

	interpolated = vec.getInterpolated(otherVec, 0.25f);
	EQUAL_VECTORS(interpolated, vector3d<T>((T)8.75, (T)16.25, 30));

	interpolated = vec.getInterpolated(otherVec, 0.75f);
	EQUAL_VECTORS(interpolated, vector3d<T>((T)6.25, (T)8.75, 10));

	interpolated = vec.getInterpolated(otherVec, 1.f);
	EQUAL_VECTORS(interpolated, vec); // 1.f means all the first vector


	vector3d<T> thirdVec(20, 10, -30);
	interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.f);
	EQUAL_VECTORS(interpolated, vec); // 0.f means all the 1st vector

	interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.25f);
	EQUAL_VECTORS(interpolated, vector3d<T>((T)7.8125, (T)10.9375, (T)13.125));

	interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.5f);
	EQUAL_VECTORS(interpolated, vector3d<T>((T)11.25, (T)13.75, (T)12.5));

	interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.75f);
	EQUAL_VECTORS(interpolated, vector3d<T>((T)15.3125, (T)13.4375, (T)-1.875));

	interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 1.f);
	EQUAL_VECTORS(interpolated, thirdVec); // 1.f means all the 3rd vector
	return true;
}

template <class T>
static bool checkAngleCalculations()
{
	core::vector3d<T> vec(5, 5, 0);
	EQUAL_VECTORS(vec.getHorizontalAngle(), vector3d<T>(315, (T)90.0, 0));
	EQUAL_VECTORS(vec.getSphericalCoordinateAngles(), vector3d<T>((T)45.0, 0, 0));
	return true;
}

template <class T>
static bool checkRotations()
{
	core::vector3d<T> vec(5, 5, 0);
	vector3d<T> center(0, 0, 0);

	vec.rotateXYBy(45, center);
	EQUAL_VECTORS(vec, vector3d<T>(0, (T)7.0710678118654755, 0));

	vec.normalize();
	// TODO: This breaks under Linux/gcc due to FP differences, but is no bug
	if (((T)0.5f)>0.f)
		EQUAL_VECTORS(vec, vector3d<T>(0, (T)1.0, 0));

	vec.set(10, 10, 10);
	center.set(5, 5, 10);
	vec.rotateXYBy(-5, center);
	// -5 means rotate clockwise slightly, so expect the X to increase
	// slightly and the Y to decrease slightly.
	EQUAL_VECTORS(vec, vector3d<T>((T)10.416752204197017, (T)9.5451947767204359, 10));

	vec.set(10, 10, 10);
	center.set(5, 10, 5);
	vec.rotateXZBy(-5, center);
	EQUAL_VECTORS(vec, vector3d<T>((T)10.416752204197017, 10, (T)9.5451947767204359));

	vec.set(10, 10, 10);
	center.set(10, 5, 5);
	vec.rotateYZBy(-5, center);
	EQUAL_VECTORS(vec, vector3d<T>(10, (T)10.416752204197017, (T)9.5451947767204359));

	vec.set(5, 5, 0);
	vec.normalize();
	EQUAL_VECTORS(vec, vector3d<T>((T)0.70710681378841400, (T)0.70710681378841400, 0));
	return true;
}

template <class T>
static bool doTests()
{
	vector3d<T> vec(-5, 5, 0);
	vector3d<T> otherVec((T)-5.1, 5, 0);
	if(!vec.equals(otherVec, (T)0.1))
	{
		logTestString("vector3d::equals failed\n");
		assert(0);
		return false;
	}

	vec.set(5, 5, 0);
	otherVec.set(10, 20, 0);
	if(!equals(vec.getDistanceFrom(otherVec), (T)15.8113883))
	{
		logTestString("vector3d::getDistanceFrom() failed\n");
		assert(0);
		return false;
	}

	if (!checkRotations<T>())
		return false;

	if (!checkInterpolation<T>())
		return false;

	if (!checkAngleCalculations<T>())
		return false;

	vec.set(0,0,0);
	vec.setLength(99);
	if ( is_nan(vec) )
		return false;

	core::vector3d<T> zeroZero(0, 0, 0);
	core::vector3d<T> oneOne(1, 1, 1);
	// Check if comparing (0.0, 0.0, 0.0) with (1.0, 1.0, 1.0) returns false.
	if(zeroZero == oneOne)
	{
		logTestString("\nERROR: vector3d %.16f, %.16f, %.16f == vector3d %.16f, %.16f, %.16f\n",
			(f64)zeroZero.X, (f64)zeroZero.Y, (f64)zeroZero.Z,
			(f64)oneOne.X, (f64)oneOne.Y, (f64)oneOne.Z);
		return false;
	}

	vec.set(5, 5, 0);

	otherVec.set(10, 20, 40);
	LESS_VECTORS(vec, otherVec);
	LESS_EQUAL_VECTORS(vec, otherVec);
	MORE_VECTORS(otherVec, vec);
	MORE_EQUAL_VECTORS(otherVec, vec);

	vec.set(-1,-1,1);
	otherVec.set(1,-1,1);
	LESS_VECTORS(vec, otherVec);
	LESS_EQUAL_VECTORS(vec, otherVec);
	MORE_VECTORS(otherVec, vec);
	MORE_EQUAL_VECTORS(otherVec, vec);

	LESS_EQUAL_VECTORS(vec, vec);
	MORE_EQUAL_VECTORS(vec, vec);

	return true;
}


/** Test the functionality of vector3d<T>, particularly methods that
	involve calculations done using different precision than <T>.
	Note that all reference vector3d<T>s are creating using double precision
	values cast to (T), as we need to test <f64>. */
bool testVector3d(void)
{
	const bool f32Success = doTests<f32>();
	if (f32Success)
		logTestString("vector3df tests passed\n\n");
	else
		logTestString("\n*** vector3df tests failed ***\n\n");

	const bool f64Success = doTests<f64>();
	if (f64Success)
		logTestString("vector3d<f64> tests passed\n\n");
	else
		logTestString("\n*** vector3d<f64> tests failed ***\n\n");

	const bool s32Success = doTests<s32>();
	if (s32Success)
		logTestString("vector3di tests passed\n\n");
	else
		logTestString("\n*** vector3di tests failed ***\n\n");

	return f32Success && f64Success && s32Success;
}