~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to tests/testVector2d.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
template<class T>
 
10
static bool compareVectors(const core::vector2d<T> & compare,
 
11
                                                   const core::vector2d<T> & with)
 
12
{
 
13
        if (!compare.equals(with))
 
14
        {
 
15
                logTestString("\nERROR: vector2d %.16f, %.16f != vector2d %.16f, %.16f\n",
 
16
                        (f64)compare.X, (f64)compare.Y, (f64)with.X, (f64)with.Y);
 
17
                assert(compare == with);
 
18
                return false;
 
19
        }
 
20
 
 
21
        return true;
 
22
}
 
23
 
 
24
template <class T>
 
25
static bool doTests()
 
26
{
 
27
        #define COMPARE_VECTORS(compare, with)\
 
28
                if(!compareVectors(compare, with)) return false;
 
29
 
 
30
        vector2d<T> vec(5, 5);
 
31
        vector2d<T> otherVec(10, 20);
 
32
        if(!equals(vec.getDistanceFrom(otherVec), (T)15.8113883))
 
33
        {
 
34
                logTestString("vector2d::getDistanceFrom() failed\n");
 
35
                assert(0);
 
36
                return false;
 
37
        }
 
38
 
 
39
 
 
40
        vec.rotateBy(45); // Test implicit (0, 0) center
 
41
        COMPARE_VECTORS(vec, vector2d<T>(0, (T)7.0710678118654755));
 
42
 
 
43
        vec.normalize();
 
44
        COMPARE_VECTORS(vec, vector2d<T>(0, (T)1.0000000461060017));
 
45
 
 
46
        vec.set(10, 10);
 
47
        vector2d<T> center(5, 5);
 
48
        vec.rotateBy(-5, center);
 
49
        // -5 means rotate clockwise slightly, so expect the X to increase
 
50
        // slightly and the Y to decrease slightly.
 
51
        COMPARE_VECTORS(vec, vector2d<T>((T)10.416752204197017, (T)9.5451947767204359));
 
52
 
 
53
        vec.set(5, 5);
 
54
        vec.normalize();
 
55
        COMPARE_VECTORS(vec, vector2d<T>((T)0.7071068137884140, (T)0.7071068137884140));
 
56
 
 
57
        vec.set(5, 5);
 
58
        otherVec.set(10, 20);
 
59
 
 
60
        logTestString("vector2d interpolation\n");
 
61
        vector2d<T> interpolated;
 
62
        (void)interpolated.interpolate(vec, otherVec, 0.f);
 
63
        COMPARE_VECTORS(interpolated, otherVec); // 0.f means all the second vector
 
64
 
 
65
        (void)interpolated.interpolate(vec, otherVec, 0.25f);
 
66
        COMPARE_VECTORS(interpolated, vector2d<T>((T)8.75, (T)16.25));
 
67
 
 
68
        (void)interpolated.interpolate(vec, otherVec, 0.75f);
 
69
        COMPARE_VECTORS(interpolated, vector2d<T>((T)6.25, (T)8.75));
 
70
 
 
71
        (void)interpolated.interpolate(vec, otherVec, 1.f);
 
72
        COMPARE_VECTORS(interpolated, vec); // 1.f means all the first vector
 
73
 
 
74
 
 
75
        interpolated = vec.getInterpolated(otherVec, 0.f);
 
76
        COMPARE_VECTORS(interpolated, otherVec); // 0.f means all the second vector
 
77
 
 
78
        interpolated = vec.getInterpolated(otherVec, 0.25f);
 
79
        COMPARE_VECTORS(interpolated, vector2d<T>((T)8.75, (T)16.25));
 
80
 
 
81
        interpolated = vec.getInterpolated(otherVec, 0.75f);
 
82
        COMPARE_VECTORS(interpolated, vector2d<T>((T)6.25, (T)8.75));
 
83
 
 
84
        interpolated = vec.getInterpolated(otherVec, 1.f);
 
85
        COMPARE_VECTORS(interpolated, vec); // 1.f means all the first vector
 
86
 
 
87
 
 
88
        logTestString("vector2d quadratic interpolation\n");
 
89
        vector2d<T> thirdVec(20, 10);
 
90
        interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.f);
 
91
        COMPARE_VECTORS(interpolated, vec); // 0.f means all the 1st vector
 
92
 
 
93
        interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.25f);
 
94
        COMPARE_VECTORS(interpolated, vector2d<T>((T)7.8125, (T)10.9375));
 
95
 
 
96
        interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.5f);
 
97
        COMPARE_VECTORS(interpolated, vector2d<T>((T)11.25, (T)13.75));
 
98
 
 
99
        interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.75f);
 
100
        COMPARE_VECTORS(interpolated, vector2d<T>((T)15.3125, (T)13.4375));
 
101
 
 
102
        interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 1.f);
 
103
        COMPARE_VECTORS(interpolated, thirdVec); // 1.f means all the 3rd vector
 
104
 
 
105
        // check if getAngle returns values matching those of the double precision version
 
106
        logTestString("vector2d getAngle\n");
 
107
        for (s32 i=0; i<200; ++i)
 
108
        {
 
109
                core::vector2d<T> tmp((T)-1, (T)(-100+i));
 
110
                core::vector2d<f64> ref(-1, -100+i);
 
111
                if (!equals(tmp.getAngle(),ref.getAngle(), 0.0003))
 
112
                {
 
113
                        logTestString("\nERROR: angle %.16f != angle %.16f\n",
 
114
                                tmp.getAngle(), ref.getAngle());
 
115
                        return false;
 
116
                }
 
117
                f32 val = atan2f((float)tmp.Y, (float)tmp.X)*core::RADTODEG;
 
118
                if (val<=0)
 
119
                        val=-val;
 
120
                else
 
121
                        val=360-val;
 
122
                if (!equals((f32)tmp.getAngle(),val, 0.5f))
 
123
                {
 
124
                        logTestString("\nERROR: angle %.16f != atan2 %.16f\n vector %.16f, %.16f\n",
 
125
                                tmp.getAngle(), val, tmp.X, tmp.Y);
 
126
                        return false;
 
127
                }
 
128
                tmp = core::vector2d<T>((T)1, (T)(-100+i));
 
129
                ref = core::vector2d<f64>(1, -100+i);
 
130
                if (!equals(tmp.getAngle(),ref.getAngle(), 0.0003))
 
131
                {
 
132
                        logTestString("\nERROR: angle %.16f != angle %.16f\n",
 
133
                                tmp.getAngle(), ref.getAngle());
 
134
                        return false;
 
135
                }
 
136
                val = atan2f((f32)tmp.Y, (f32)tmp.X)*core::RADTODEG;
 
137
                if (val<=0)
 
138
                        val=-val;
 
139
                else
 
140
                        val=360-val;
 
141
                if (!equals((f32)tmp.getAngle(),val, 0.5f))
 
142
                {
 
143
                        logTestString("\nERROR: angle %.16f != atan2 %.16f\n vector %.16f, %.16f\n",
 
144
                                tmp.getAngle(), val, tmp.X, tmp.Y);
 
145
                        return false;
 
146
                }
 
147
        }
 
148
        core::vector2d<T> tmp(0, -100);
 
149
        core::vector2d<f64> ref(0, -100);
 
150
        if (!equals(tmp.getAngle(),ref.getAngle()))
 
151
        {
 
152
                logTestString("\nERROR: angle %.16f != angle %.16f\n",
 
153
                        tmp.getAngle(), ref.getAngle());
 
154
                return false;
 
155
        }
 
156
        tmp = core::vector2d<T>(0, 100);
 
157
        ref = core::vector2d<f64>(0, 100);
 
158
        if (!equals(tmp.getAngle(),ref.getAngle()))
 
159
        {
 
160
                logTestString("\nERROR: angle %.16f != angle %.16f\n",
 
161
                        tmp.getAngle(), ref.getAngle());
 
162
                return false;
 
163
        }
 
164
        tmp = core::vector2d<T>(static_cast<T>(-1.53080559e-16), static_cast<T>(2.49999523));
 
165
        ref = core::vector2d<f64>(-1.53080559e-16, 2.49999523);
 
166
        if (!equals(tmp.getAngle(),ref.getAngle()))
 
167
        {
 
168
                logTestString("\nERROR: angle %.16f != angle %.16f\n",
 
169
                        tmp.getAngle(), ref.getAngle());
 
170
                return false;
 
171
        }
 
172
 
 
173
        core::vector2d<T> zeroZero(0, 0);
 
174
        core::vector2d<T> oneOne(1, 1);
 
175
        // Check if comparing (0.0, 0.0) with (1.0, 1.0) returns false.
 
176
        if(zeroZero == oneOne)
 
177
        {
 
178
                logTestString("\nERROR: vector2d %.16f, %.16f == vector2d %.16f, %.16f\n",
 
179
                        (f64)zeroZero.X, (f64)zeroZero.Y, (f64)oneOne.X, (f64)oneOne.Y);
 
180
                return false;
 
181
        }
 
182
 
 
183
        return true;
 
184
}
 
185
 
 
186
/** Test the functionality of vector2d<T>, particularly methods that
 
187
        involve calculations done using different precision than <T>.
 
188
        Note that all reference vector2d<T>s are creating using double precision
 
189
        values cast to (T), as we need to test <f64>. */
 
190
bool testVector2d(void)
 
191
{
 
192
        bool f32Success = doTests<f32>();
 
193
        if(f32Success)
 
194
                logTestString("vector2df tests passed\n\n");
 
195
        else
 
196
                logTestString("\n*** vector2df tests failed ***\n\n");
 
197
 
 
198
        bool f64Success = doTests<f64>();
 
199
        if(f64Success)
 
200
                logTestString("vector2d<f64> tests passed\n\n");
 
201
        else
 
202
                logTestString("\n*** vector2d<f64> tests failed ***\n\n");
 
203
 
 
204
        bool s32Success = doTests<s32>();
 
205
        if(s32Success)
 
206
                logTestString("vector2di tests passed\n\n");
 
207
        else
 
208
                logTestString("\n*** vector2di tests failed ***\n\n");
 
209
 
 
210
        return f32Success && f64Success && s32Success;
 
211
}
 
212