1
// Copyright (C) 2008-2011 Colin MacDonald
2
// No rights reserved: this software is in the public domain.
9
#define EQUAL_VECTORS(compare, with)\
10
if(!equalVectors(cmp_equal<core::vector3d<T> >(compare), with)) {assert(false); return false;}
12
#define LESS_VECTORS(compare, with)\
13
if(!equalVectors(cmp_less<core::vector3d<T> >(compare), with)) return false;
15
#define LESS_EQUAL_VECTORS(compare, with)\
16
if(!equalVectors(cmp_less_equal<core::vector3d<T> >(compare), with)) return false;
18
#define MORE_VECTORS(compare, with)\
19
if(!equalVectors(cmp_more<core::vector3d<T> >(compare), with)) return false;
21
#define MORE_EQUAL_VECTORS(compare, with)\
22
if(!equalVectors(cmp_more_equal<core::vector3d<T> >(compare), with)) return false;
24
// check if the vector contains a NAN (a==b is guaranteed to return false in this case)
26
static bool is_nan(const core::vector3d<T> &vec )
28
return ( !(vec.X == vec.X)
30
|| !(vec.Z == vec.Z) );
36
cmp_less(const T& a) : val(a) {}
37
bool operator()(const T& other) const
41
const char* getName() const {return "<";}
48
cmp_less_equal(const T& a) : val(a) {}
49
bool operator()(const T& other) const
53
const char* getName() const {return "<=";}
60
cmp_more(const T& a) : val(a) {}
61
bool operator()(const T& other) const
65
const char* getName() const {return ">";}
72
cmp_more_equal(const T& a) : val(a) {}
73
bool operator()(const T& other) const
77
const char* getName() const {return ">=";}
84
cmp_equal(const T& a) : val(a) {}
85
bool operator()(const T& other) const
87
return val.equals(other);
89
const char* getName() const {return "==";}
93
template<class S, class T>
94
static bool equalVectors(const S& compare,
95
const core::vector3d<T> & with)
99
logTestString("\nERROR: vector3d %.16f, %.16f, %.16f %s vector3d %.16f, %.16f, %.16f\n",
100
(f64)compare.val.X, (f64)compare.val.Y, (f64)compare.val.Z, compare.getName(),
101
(f64)with.X, (f64)with.Y, (f64)with.Z);
102
assert(compare(with));
110
static bool checkInterpolation()
112
core::vector3d<T> vec(5, 5, 0);
113
core::vector3d<T> otherVec(10, 20, 40);
115
vector3d<T> interpolated;
116
(void)interpolated.interpolate(vec, otherVec, 0.f);
117
EQUAL_VECTORS(interpolated, otherVec); // 0.f means all the second vector
119
(void)interpolated.interpolate(vec, otherVec, 0.25f);
120
EQUAL_VECTORS(interpolated, vector3d<T>((T)8.75, (T)16.25, 30));
122
(void)interpolated.interpolate(vec, otherVec, 0.75f);
123
EQUAL_VECTORS(interpolated, vector3d<T>((T)6.25, (T)8.75, 10));
125
(void)interpolated.interpolate(vec, otherVec, 1.f);
126
EQUAL_VECTORS(interpolated, vec); // 1.f means all the first vector
129
interpolated = vec.getInterpolated(otherVec, 0.f);
130
EQUAL_VECTORS(interpolated, otherVec); // 0.f means all the second vector
132
interpolated = vec.getInterpolated(otherVec, 0.25f);
133
EQUAL_VECTORS(interpolated, vector3d<T>((T)8.75, (T)16.25, 30));
135
interpolated = vec.getInterpolated(otherVec, 0.75f);
136
EQUAL_VECTORS(interpolated, vector3d<T>((T)6.25, (T)8.75, 10));
138
interpolated = vec.getInterpolated(otherVec, 1.f);
139
EQUAL_VECTORS(interpolated, vec); // 1.f means all the first vector
142
vector3d<T> thirdVec(20, 10, -30);
143
interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.f);
144
EQUAL_VECTORS(interpolated, vec); // 0.f means all the 1st vector
146
interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.25f);
147
EQUAL_VECTORS(interpolated, vector3d<T>((T)7.8125, (T)10.9375, (T)13.125));
149
interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.5f);
150
EQUAL_VECTORS(interpolated, vector3d<T>((T)11.25, (T)13.75, (T)12.5));
152
interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.75f);
153
EQUAL_VECTORS(interpolated, vector3d<T>((T)15.3125, (T)13.4375, (T)-1.875));
155
interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 1.f);
156
EQUAL_VECTORS(interpolated, thirdVec); // 1.f means all the 3rd vector
161
static bool checkAngleCalculations()
163
core::vector3d<T> vec(5, 5, 0);
164
EQUAL_VECTORS(vec.getHorizontalAngle(), vector3d<T>(315, (T)90.0, 0));
165
EQUAL_VECTORS(vec.getSphericalCoordinateAngles(), vector3d<T>((T)45.0, 0, 0));
170
static bool checkRotations()
172
core::vector3d<T> vec(5, 5, 0);
173
vector3d<T> center(0, 0, 0);
175
vec.rotateXYBy(45, center);
176
EQUAL_VECTORS(vec, vector3d<T>(0, (T)7.0710678118654755, 0));
179
// TODO: This breaks under Linux/gcc due to FP differences, but is no bug
181
EQUAL_VECTORS(vec, vector3d<T>(0, (T)1.0, 0));
184
center.set(5, 5, 10);
185
vec.rotateXYBy(-5, center);
186
// -5 means rotate clockwise slightly, so expect the X to increase
187
// slightly and the Y to decrease slightly.
188
EQUAL_VECTORS(vec, vector3d<T>((T)10.416752204197017, (T)9.5451947767204359, 10));
191
center.set(5, 10, 5);
192
vec.rotateXZBy(-5, center);
193
EQUAL_VECTORS(vec, vector3d<T>((T)10.416752204197017, 10, (T)9.5451947767204359));
196
center.set(10, 5, 5);
197
vec.rotateYZBy(-5, center);
198
EQUAL_VECTORS(vec, vector3d<T>(10, (T)10.416752204197017, (T)9.5451947767204359));
202
EQUAL_VECTORS(vec, vector3d<T>((T)0.70710681378841400, (T)0.70710681378841400, 0));
207
static bool doTests()
209
vector3d<T> vec(-5, 5, 0);
210
vector3d<T> otherVec((T)-5.1, 5, 0);
211
if(!vec.equals(otherVec, (T)0.1))
213
logTestString("vector3d::equals failed\n");
219
otherVec.set(10, 20, 0);
220
if(!equals(vec.getDistanceFrom(otherVec), (T)15.8113883))
222
logTestString("vector3d::getDistanceFrom() failed\n");
227
if (!checkRotations<T>())
230
if (!checkInterpolation<T>())
233
if (!checkAngleCalculations<T>())
241
core::vector3d<T> zeroZero(0, 0, 0);
242
core::vector3d<T> oneOne(1, 1, 1);
243
// Check if comparing (0.0, 0.0, 0.0) with (1.0, 1.0, 1.0) returns false.
244
if(zeroZero == oneOne)
246
logTestString("\nERROR: vector3d %.16f, %.16f, %.16f == vector3d %.16f, %.16f, %.16f\n",
247
(f64)zeroZero.X, (f64)zeroZero.Y, (f64)zeroZero.Z,
248
(f64)oneOne.X, (f64)oneOne.Y, (f64)oneOne.Z);
254
otherVec.set(10, 20, 40);
255
LESS_VECTORS(vec, otherVec);
256
LESS_EQUAL_VECTORS(vec, otherVec);
257
MORE_VECTORS(otherVec, vec);
258
MORE_EQUAL_VECTORS(otherVec, vec);
261
otherVec.set(1,-1,1);
262
LESS_VECTORS(vec, otherVec);
263
LESS_EQUAL_VECTORS(vec, otherVec);
264
MORE_VECTORS(otherVec, vec);
265
MORE_EQUAL_VECTORS(otherVec, vec);
267
LESS_EQUAL_VECTORS(vec, vec);
268
MORE_EQUAL_VECTORS(vec, vec);
274
/** Test the functionality of vector3d<T>, particularly methods that
275
involve calculations done using different precision than <T>.
276
Note that all reference vector3d<T>s are creating using double precision
277
values cast to (T), as we need to test <f64>. */
278
bool testVector3d(void)
280
const bool f32Success = doTests<f32>();
282
logTestString("vector3df tests passed\n\n");
284
logTestString("\n*** vector3df tests failed ***\n\n");
286
const bool f64Success = doTests<f64>();
288
logTestString("vector3d<f64> tests passed\n\n");
290
logTestString("\n*** vector3d<f64> tests failed ***\n\n");
292
const bool s32Success = doTests<s32>();
294
logTestString("vector3di tests passed\n\n");
296
logTestString("\n*** vector3di tests failed ***\n\n");
298
return f32Success && f64Success && s32Success;