~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to tests/testVector3d.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
using namespace core;
 
8
 
 
9
#define EQUAL_VECTORS(compare, with)\
 
10
        if(!equalVectors(cmp_equal<core::vector3d<T> >(compare), with)) {assert(false); return false;}
 
11
 
 
12
#define LESS_VECTORS(compare, with)\
 
13
        if(!equalVectors(cmp_less<core::vector3d<T> >(compare), with)) return false;
 
14
 
 
15
#define LESS_EQUAL_VECTORS(compare, with)\
 
16
        if(!equalVectors(cmp_less_equal<core::vector3d<T> >(compare), with)) return false;
 
17
 
 
18
#define MORE_VECTORS(compare, with)\
 
19
        if(!equalVectors(cmp_more<core::vector3d<T> >(compare), with)) return false;
 
20
 
 
21
#define MORE_EQUAL_VECTORS(compare, with)\
 
22
        if(!equalVectors(cmp_more_equal<core::vector3d<T> >(compare), with)) return false;
 
23
 
 
24
// check if the vector contains a NAN (a==b is guaranteed to return false in this case)
 
25
template<class T>
 
26
static bool is_nan(const core::vector3d<T> &vec )
 
27
{
 
28
        return ( !(vec.X == vec.X)
 
29
                        || !(vec.Y == vec.Y)
 
30
                        || !(vec.Z == vec.Z) );
 
31
}
 
32
 
 
33
template<class T>
 
34
struct cmp_less
 
35
{
 
36
        cmp_less(const T& a) : val(a) {}
 
37
        bool operator()(const T& other) const
 
38
        {
 
39
                return val<other;
 
40
        }
 
41
        const char* getName() const {return "<";}
 
42
        const T val;
 
43
};
 
44
 
 
45
template<class T>
 
46
struct cmp_less_equal
 
47
{
 
48
        cmp_less_equal(const T& a) : val(a) {}
 
49
        bool operator()(const T& other) const
 
50
        {
 
51
                return val<=other;
 
52
        }
 
53
        const char* getName() const {return "<=";}
 
54
        const T val;
 
55
};
 
56
 
 
57
template<class T>
 
58
struct cmp_more
 
59
{
 
60
        cmp_more(const T& a) : val(a) {}
 
61
        bool operator()(const T& other) const
 
62
        {
 
63
                return val>other;
 
64
        }
 
65
        const char* getName() const {return ">";}
 
66
        const T val;
 
67
};
 
68
 
 
69
template<class T>
 
70
struct cmp_more_equal
 
71
{
 
72
        cmp_more_equal(const T& a) : val(a) {}
 
73
        bool operator()(const T& other) const
 
74
        {
 
75
                return val>=other;
 
76
        }
 
77
        const char* getName() const {return ">=";}
 
78
        const T val;
 
79
};
 
80
 
 
81
template<class T>
 
82
struct cmp_equal
 
83
{
 
84
        cmp_equal(const T& a) : val(a) {}
 
85
        bool operator()(const T& other) const
 
86
        {
 
87
                return val.equals(other);
 
88
        }
 
89
        const char* getName() const {return "==";}
 
90
        const T val;
 
91
};
 
92
 
 
93
template<class S, class T>
 
94
static bool equalVectors(const S& compare,
 
95
                           const core::vector3d<T> & with)
 
96
{
 
97
        if (!compare(with))
 
98
        {
 
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));
 
103
                return false;
 
104
        }
 
105
 
 
106
        return true;
 
107
}
 
108
 
 
109
template <class T>
 
110
static bool checkInterpolation()
 
111
{
 
112
        core::vector3d<T> vec(5, 5, 0);
 
113
        core::vector3d<T> otherVec(10, 20, 40);
 
114
 
 
115
        vector3d<T> interpolated;
 
116
        (void)interpolated.interpolate(vec, otherVec, 0.f);
 
117
        EQUAL_VECTORS(interpolated, otherVec); // 0.f means all the second vector
 
118
 
 
119
        (void)interpolated.interpolate(vec, otherVec, 0.25f);
 
120
        EQUAL_VECTORS(interpolated, vector3d<T>((T)8.75, (T)16.25, 30));
 
121
 
 
122
        (void)interpolated.interpolate(vec, otherVec, 0.75f);
 
123
        EQUAL_VECTORS(interpolated, vector3d<T>((T)6.25, (T)8.75, 10));
 
124
 
 
125
        (void)interpolated.interpolate(vec, otherVec, 1.f);
 
126
        EQUAL_VECTORS(interpolated, vec); // 1.f means all the first vector
 
127
 
 
128
 
 
129
        interpolated = vec.getInterpolated(otherVec, 0.f);
 
130
        EQUAL_VECTORS(interpolated, otherVec); // 0.f means all the second vector
 
131
 
 
132
        interpolated = vec.getInterpolated(otherVec, 0.25f);
 
133
        EQUAL_VECTORS(interpolated, vector3d<T>((T)8.75, (T)16.25, 30));
 
134
 
 
135
        interpolated = vec.getInterpolated(otherVec, 0.75f);
 
136
        EQUAL_VECTORS(interpolated, vector3d<T>((T)6.25, (T)8.75, 10));
 
137
 
 
138
        interpolated = vec.getInterpolated(otherVec, 1.f);
 
139
        EQUAL_VECTORS(interpolated, vec); // 1.f means all the first vector
 
140
 
 
141
 
 
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
 
145
 
 
146
        interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.25f);
 
147
        EQUAL_VECTORS(interpolated, vector3d<T>((T)7.8125, (T)10.9375, (T)13.125));
 
148
 
 
149
        interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.5f);
 
150
        EQUAL_VECTORS(interpolated, vector3d<T>((T)11.25, (T)13.75, (T)12.5));
 
151
 
 
152
        interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.75f);
 
153
        EQUAL_VECTORS(interpolated, vector3d<T>((T)15.3125, (T)13.4375, (T)-1.875));
 
154
 
 
155
        interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 1.f);
 
156
        EQUAL_VECTORS(interpolated, thirdVec); // 1.f means all the 3rd vector
 
157
        return true;
 
158
}
 
159
 
 
160
template <class T>
 
161
static bool checkAngleCalculations()
 
162
{
 
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));
 
166
        return true;
 
167
}
 
168
 
 
169
template <class T>
 
170
static bool checkRotations()
 
171
{
 
172
        core::vector3d<T> vec(5, 5, 0);
 
173
        vector3d<T> center(0, 0, 0);
 
174
 
 
175
        vec.rotateXYBy(45, center);
 
176
        EQUAL_VECTORS(vec, vector3d<T>(0, (T)7.0710678118654755, 0));
 
177
 
 
178
        vec.normalize();
 
179
        // TODO: This breaks under Linux/gcc due to FP differences, but is no bug
 
180
        if (((T)0.5f)>0.f)
 
181
                EQUAL_VECTORS(vec, vector3d<T>(0, (T)1.0, 0));
 
182
 
 
183
        vec.set(10, 10, 10);
 
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));
 
189
 
 
190
        vec.set(10, 10, 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));
 
194
 
 
195
        vec.set(10, 10, 10);
 
196
        center.set(10, 5, 5);
 
197
        vec.rotateYZBy(-5, center);
 
198
        EQUAL_VECTORS(vec, vector3d<T>(10, (T)10.416752204197017, (T)9.5451947767204359));
 
199
 
 
200
        vec.set(5, 5, 0);
 
201
        vec.normalize();
 
202
        EQUAL_VECTORS(vec, vector3d<T>((T)0.70710681378841400, (T)0.70710681378841400, 0));
 
203
        return true;
 
204
}
 
205
 
 
206
template <class T>
 
207
static bool doTests()
 
208
{
 
209
        vector3d<T> vec(-5, 5, 0);
 
210
        vector3d<T> otherVec((T)-5.1, 5, 0);
 
211
        if(!vec.equals(otherVec, (T)0.1))
 
212
        {
 
213
                logTestString("vector3d::equals failed\n");
 
214
                assert(0);
 
215
                return false;
 
216
        }
 
217
 
 
218
        vec.set(5, 5, 0);
 
219
        otherVec.set(10, 20, 0);
 
220
        if(!equals(vec.getDistanceFrom(otherVec), (T)15.8113883))
 
221
        {
 
222
                logTestString("vector3d::getDistanceFrom() failed\n");
 
223
                assert(0);
 
224
                return false;
 
225
        }
 
226
 
 
227
        if (!checkRotations<T>())
 
228
                return false;
 
229
 
 
230
        if (!checkInterpolation<T>())
 
231
                return false;
 
232
 
 
233
        if (!checkAngleCalculations<T>())
 
234
                return false;
 
235
 
 
236
        vec.set(0,0,0);
 
237
        vec.setLength(99);
 
238
        if ( is_nan(vec) )
 
239
                return false;
 
240
 
 
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)
 
245
        {
 
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);
 
249
                return false;
 
250
        }
 
251
 
 
252
        vec.set(5, 5, 0);
 
253
 
 
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);
 
259
 
 
260
        vec.set(-1,-1,1);
 
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);
 
266
 
 
267
        LESS_EQUAL_VECTORS(vec, vec);
 
268
        MORE_EQUAL_VECTORS(vec, vec);
 
269
 
 
270
        return true;
 
271
}
 
272
 
 
273
 
 
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)
 
279
{
 
280
        const bool f32Success = doTests<f32>();
 
281
        if (f32Success)
 
282
                logTestString("vector3df tests passed\n\n");
 
283
        else
 
284
                logTestString("\n*** vector3df tests failed ***\n\n");
 
285
 
 
286
        const bool f64Success = doTests<f64>();
 
287
        if (f64Success)
 
288
                logTestString("vector3d<f64> tests passed\n\n");
 
289
        else
 
290
                logTestString("\n*** vector3d<f64> tests failed ***\n\n");
 
291
 
 
292
        const bool s32Success = doTests<s32>();
 
293
        if (s32Success)
 
294
                logTestString("vector3di tests passed\n\n");
 
295
        else
 
296
                logTestString("\n*** vector3di tests failed ***\n\n");
 
297
 
 
298
        return f32Success && f64Success && s32Success;
 
299
}
 
300