~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to tests/testaabbox.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
// These tests are also only called for f32 and f64, due to conversion problems
 
10
// in the respective methods.
 
11
template<class T>
 
12
static bool checkCollisions()
 
13
{
 
14
        aabbox3d<T> one(0,0,0,4,4,4);
 
15
        aabbox3d<T> two(2,2,2,4,4,4);
 
16
 
 
17
        if (two.getInterpolated(one, 1) != two)
 
18
        {
 
19
                logTestString("aabbox3d<T> interpolation wrong on 1\n");
 
20
                return false;
 
21
        }
 
22
        if (two.getInterpolated(one, 0) != one)
 
23
        {
 
24
                logTestString("aabbox3d<T> interpolation wrong on 0\n");
 
25
                return false;
 
26
        }
 
27
        aabbox3d<T> three(two.getInterpolated(one, 0.5f));
 
28
        if (two == one)
 
29
        {
 
30
                logTestString("aabbox3d<T> interpolation wrong on 0.5 (right)\n");
 
31
                return false;
 
32
        }
 
33
        if (two == three)
 
34
        {
 
35
                logTestString("aabbox3d<T> interpolation wrong on 0.5 (left)\n");
 
36
                return false;
 
37
        }
 
38
        three.reset(aabbox3d<T>(2,2,2,5,5,5));
 
39
        if (!two.isFullInside(one))
 
40
        {
 
41
                logTestString("small aabbox3d<T> is not fully inside\n");
 
42
                return false;
 
43
        }
 
44
        if (three.isFullInside(one))
 
45
        {
 
46
                logTestString("large aabbox3d<T> is fully inside\n");
 
47
                return false;
 
48
        }
 
49
 
 
50
        if (!two.intersectsWithBox(one))
 
51
        {
 
52
                logTestString("small aabbox3d<T> does not intersect\n");
 
53
                return false;
 
54
        }
 
55
        if (!three.intersectsWithBox(one))
 
56
        {
 
57
                logTestString("large aabbox3d<T> does not intersect\n");
 
58
                return false;
 
59
        }
 
60
 
 
61
        core::line3d<T> line(-2,-2,-2,2,2,2);
 
62
        if (!one.intersectsWithLine(line))
 
63
        {
 
64
                logTestString("aabbox3d<T> does not intersect with line(1)\n");
 
65
                return false;
 
66
        }
 
67
        line.end.set(2,2,10);
 
68
        if (!one.intersectsWithLine(line))
 
69
        {
 
70
                logTestString("aabbox3d<T> does not intersect with line(2)\n");
 
71
                return false;
 
72
        }
 
73
        line.end.set(0,2,10);
 
74
        if (one.intersectsWithLine(line))
 
75
        {
 
76
                logTestString("aabbox3d<T> does intersect with line(3)\n");
 
77
                return false;
 
78
        }
 
79
        return true;
 
80
}
 
81
 
 
82
template<class T>
 
83
static bool checkPoints()
 
84
{
 
85
        aabbox3d<T> one(-1,-2,-3,2,2,2);
 
86
 
 
87
        if (!one.isPointInside(core::vector3d<T>(-1,-2,-3)))
 
88
        {
 
89
                logTestString("isPointInside failed with min vertex\n");
 
90
                return false;
 
91
        }
 
92
        if (!one.isPointInside(core::vector3d<T>(-1,2,-3)))
 
93
        {
 
94
                logTestString("isPointInside failed with other min vertex\n");
 
95
                return false;
 
96
        }
 
97
        if (!one.isPointInside(core::vector3d<T>(2,-2,2)))
 
98
        {
 
99
                logTestString("isPointInside failed with other max vertex\n");
 
100
                return false;
 
101
        }
 
102
        if (!one.isPointInside(core::vector3d<T>(2,2,2)))
 
103
        {
 
104
                logTestString("isPointInside failed with max vertex\n");
 
105
                return false;
 
106
        }
 
107
        if (!one.isPointInside(core::vector3d<T>(0,0,0)))
 
108
        {
 
109
                logTestString("isPointInside failed with origin\n");
 
110
                return false;
 
111
        }
 
112
        if (!one.isPointInside(core::vector3d<T>((T)1.2,-1,1)))
 
113
        {
 
114
                logTestString("isPointInside failed with random point inside\n");
 
115
                return false;
 
116
        }
 
117
        if (one.isPointInside(core::vector3d<T>(-2,-2,-3)))
 
118
        {
 
119
                logTestString("isPointInside failed near min vertex\n");
 
120
                return false;
 
121
        }
 
122
        if (one.isPointInside(core::vector3d<T>(2,3,2)))
 
123
        {
 
124
                logTestString("isPointInside failed near max vertex\n");
 
125
                return false;
 
126
        }
 
127
        if (one.isPointInside(core::vector3d<T>(3,0,0)))
 
128
        {
 
129
                logTestString("isPointInside failed near origin\n");
 
130
                return false;
 
131
        }
 
132
        if (one.isPointInside(core::vector3d<T>((T)10.2,-1,1)))
 
133
        {
 
134
                logTestString("isPointInside failed with random point outside\n");
 
135
                return false;
 
136
        }
 
137
        if (one.isPointTotalInside(core::vector3d<T>(-1,-2,-3)))
 
138
        {
 
139
                logTestString("isPointTotalInside failed with min vertex\n");
 
140
                return false;
 
141
        }
 
142
        if (one.isPointTotalInside(core::vector3d<T>(-1,2,-3)))
 
143
        {
 
144
                logTestString("isPointTotalInside failed with other min vertex\n");
 
145
                return false;
 
146
        }
 
147
        if (one.isPointTotalInside(core::vector3d<T>(2,-2,2)))
 
148
        {
 
149
                logTestString("isPointTotalInside failed with other max vertex\n");
 
150
                return false;
 
151
        }
 
152
        if (one.isPointTotalInside(core::vector3d<T>(2,2,2)))
 
153
        {
 
154
                logTestString("isPointTotalInside failed with max vertex\n");
 
155
                return false;
 
156
        }
 
157
        if (!one.isPointTotalInside(core::vector3d<T>(0,0,0)))
 
158
        {
 
159
                logTestString("isPointTotalInside failed with origin\n");
 
160
                return false;
 
161
        }
 
162
        if (!one.isPointTotalInside(core::vector3d<T>((T)1.2,-1,1)))
 
163
        {
 
164
                logTestString("isPointTotalInside failed with random point inside\n");
 
165
                return false;
 
166
        }
 
167
        if (one.isPointTotalInside(core::vector3d<T>((T)10.2,-1,1)))
 
168
        {
 
169
                logTestString("isPointTotalInside failed with random point outside\n");
 
170
                return false;
 
171
        }
 
172
 
 
173
        core::plane3d<T> plane(core::vector3d<T>(0,0,-1), -10);
 
174
        if (one.classifyPlaneRelation(plane) != core::ISREL3D_BACK)
 
175
        {
 
176
                logTestString("box not behind\n");
 
177
                return false;
 
178
        }
 
179
        plane.D=0;
 
180
        if (one.classifyPlaneRelation(plane) != core::ISREL3D_CLIPPED)
 
181
        {
 
182
                logTestString("box not clipped\n");
 
183
                return false;
 
184
        }
 
185
        plane.D=10;
 
186
        if (one.classifyPlaneRelation(plane) != core::ISREL3D_FRONT)
 
187
        {
 
188
                logTestString("box not in front\n");
 
189
                return false;
 
190
        }
 
191
        return true;
 
192
}
 
193
 
 
194
template <class T>
 
195
static bool doTests()
 
196
{
 
197
        aabbox3d<T> empty;
 
198
        aabbox3d<T> one(-1,-1,-1,1,1,1);
 
199
        if (empty != one)
 
200
        {
 
201
                logTestString("default aabbox3d<T> wrong, or comparison failed\n");
 
202
                return false;
 
203
        }
 
204
        if (empty.getCenter() != core::vector3d<T>(0,0,0))
 
205
        {
 
206
                logTestString("default aabbox3d<T> has wrong Center\n");
 
207
                return false;
 
208
        }
 
209
        if (empty.getExtent() != core::vector3d<T>(2,2,2))
 
210
        {
 
211
                logTestString("default aabbox3d<T> has wrong Extent\n");
 
212
                return false;
 
213
        }
 
214
        if (empty.isEmpty())
 
215
        {
 
216
                logTestString("default aabbox3d<T> is empty\n");
 
217
                return false;
 
218
        }
 
219
        if (empty.getVolume() != 8)
 
220
        {
 
221
                logTestString("default aabbox3d<T> has wrong volume\n");
 
222
                return false;
 
223
        }
 
224
        if (empty.getArea() != 24)
 
225
        {
 
226
                logTestString("default aabbox3d<T> has wrong area\n");
 
227
                return false;
 
228
        }
 
229
        aabbox3d<T> two(core::vector3d<T>(-1,-1,-1),core::vector3d<T>(2,2,2));
 
230
        if (empty == two)
 
231
        {
 
232
                logTestString("empty aabbox3d<T> too large, or comparison failed\n");
 
233
                return false;
 
234
        }
 
235
        if (two.getCenter() != core::vector3d<T>((T)0.5,(T)0.5,(T)0.5))
 
236
        {
 
237
                logTestString("extended aabbox3d<T> has wrong Center\n");
 
238
                return false;
 
239
        }
 
240
        if (two.getExtent() != core::vector3d<T>(3,3,3))
 
241
        {
 
242
                logTestString("extended aabbox3d<T> has wrong Extent\n");
 
243
                return false;
 
244
        }
 
245
        if (two.isEmpty())
 
246
        {
 
247
                logTestString("extended aabbox3d<T> is empty\n");
 
248
                return false;
 
249
        }
 
250
        if (two.getVolume() != 27)
 
251
        {
 
252
                logTestString("extended aabbox3d<T> has wrong volume\n");
 
253
                return false;
 
254
        }
 
255
        if (two.getArea() != 54)
 
256
        {
 
257
                logTestString("extended aabbox3d<T> has wrong area\n");
 
258
                return false;
 
259
        }
 
260
        one.reset(1,1,1);
 
261
        if (one==empty)
 
262
        {
 
263
                logTestString("reset failed, or comparison failed\n");
 
264
                return false;
 
265
        }
 
266
        if (one.getCenter() != core::vector3d<T>(1,1,1))
 
267
        {
 
268
                logTestString("singular aabbox3d<T> has wrong Center\n");
 
269
                return false;
 
270
        }
 
271
        if (one.getExtent() != core::vector3d<T>(0,0,0))
 
272
        {
 
273
                logTestString("singular aabbox3d<T> has Extent\n");
 
274
                return false;
 
275
        }
 
276
        if (!one.isEmpty())
 
277
        {
 
278
                logTestString("empty aabbox3d<T> is not empty\n");
 
279
                return false;
 
280
        }
 
281
        if (one.getVolume() != 0)
 
282
        {
 
283
                logTestString("empty aabbox3d<T> has wrong volume\n");
 
284
                return false;
 
285
        }
 
286
        if (one.getArea() != 0)
 
287
        {
 
288
                logTestString("empty aabbox3d<T> has wrong area\n");
 
289
                return false;
 
290
        }
 
291
        one.addInternalPoint(core::vector3d<T>(-1,-1,-1));
 
292
        if (one!=empty)
 
293
        {
 
294
                logTestString("addInternalPoint failed, creating default bbox\n");
 
295
                return false;
 
296
        }
 
297
        one.reset(1,1,1);
 
298
        one.reset(empty);
 
299
        if (one!=empty)
 
300
        {
 
301
                logTestString("reset with bbox failed, creating default bbox\n");
 
302
                return false;
 
303
        }
 
304
        one.addInternalPoint(core::vector3d<T>(2,2,2));
 
305
        if (one != two)
 
306
        {
 
307
                logTestString("addInternalPoint for aabbox3d<T> failed.\n");
 
308
                return false;
 
309
        }
 
310
        one.addInternalBox(empty);
 
311
        if (one != two)
 
312
        {
 
313
                logTestString("addInternalBox with smaller box failed.\n");
 
314
                return false;
 
315
        }
 
316
        one.addInternalBox(two);
 
317
        if (one != two)
 
318
        {
 
319
                logTestString("addInternalBox with same box failed.\n");
 
320
                return false;
 
321
        }
 
322
        one.addInternalPoint(-1,-2,-3);
 
323
        two.addInternalPoint(-1,-2,-3);
 
324
        empty.addInternalBox(one);
 
325
        if (empty != two)
 
326
        {
 
327
                logTestString("addInternalBox with larger box failed\n");
 
328
                return false;
 
329
        }
 
330
        if (one.getCenter() != core::vector3d<T>((T)0.5,0,(T)-0.5))
 
331
        {
 
332
                logTestString("large aabbox3d<T> has wrong Center\n");
 
333
                return false;
 
334
        }
 
335
        if (one.getExtent() != core::vector3d<T>(3,4,5))
 
336
        {
 
337
                logTestString("large aabbox3d<T> has wrong Extent\n");
 
338
                return false;
 
339
        }
 
340
        if (one.isEmpty())
 
341
        {
 
342
                logTestString("large aabbox3d<T> is empty\n");
 
343
                return false;
 
344
        }
 
345
        if (one.getVolume() != 60)
 
346
        {
 
347
                logTestString("large aabbox3d<T> has wrong volume\n");
 
348
                return false;
 
349
        }
 
350
        if (one.getArea() != 94)
 
351
        {
 
352
                logTestString("large aabbox3d<T> has wrong area\n");
 
353
                return false;
 
354
        }
 
355
        if (!checkPoints<T>())
 
356
                return false;
 
357
        return true;
 
358
}
 
359
 
 
360
 
 
361
/** Test the functionality of aabbox3d<T>. */
 
362
bool testaabbox3d(void)
 
363
{
 
364
        bool f32Success = doTests<f32>();
 
365
        f32Success &= checkCollisions<f32>();
 
366
        if(f32Success)
 
367
                logTestString("aabbox3d<f32> tests passed\n\n");
 
368
        else
 
369
                logTestString("\n*** aabbox3d<f32> tests failed ***\n\n");
 
370
 
 
371
        bool f64Success = doTests<f64>();
 
372
        f64Success &= checkCollisions<f64>();
 
373
        if(f64Success)
 
374
                logTestString("aabbox3d<f64> tests passed\n\n");
 
375
        else
 
376
                logTestString("\n*** aabbox3d<f64> tests failed ***\n\n");
 
377
 
 
378
        bool s32Success = doTests<s32>();
 
379
        if(s32Success)
 
380
                logTestString("aabbox3d<s32> tests passed\n\n");
 
381
        else
 
382
                logTestString("\n*** aabbox3d<s32> tests failed ***\n\n");
 
383
 
 
384
        return f32Success && f64Success && s32Success;
 
385
}