aboutsummaryrefslogtreecommitdiff
path: root/tests/testaabbox.cpp
blob: 8270624f6d99d6013fb863775d1282f992f2c94b (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// 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;

// These tests are also only called for f32 and f64, due to conversion problems
// in the respective methods.
template<class T>
static bool checkCollisions()
{
	aabbox3d<T> one(0,0,0,4,4,4);
	aabbox3d<T> two(2,2,2,4,4,4);

	if (two.getInterpolated(one, 1) != two)
	{
		logTestString("aabbox3d<T> interpolation wrong on 1\n");
		return false;
	}
	if (two.getInterpolated(one, 0) != one)
	{
		logTestString("aabbox3d<T> interpolation wrong on 0\n");
		return false;
	}
	aabbox3d<T> three(two.getInterpolated(one, 0.5f));
	if (two == one)
	{
		logTestString("aabbox3d<T> interpolation wrong on 0.5 (right)\n");
		return false;
	}
	if (two == three)
	{
		logTestString("aabbox3d<T> interpolation wrong on 0.5 (left)\n");
		return false;
	}
	three.reset(aabbox3d<T>(2,2,2,5,5,5));
	if (!two.isFullInside(one))
	{
		logTestString("small aabbox3d<T> is not fully inside\n");
		return false;
	}
	if (three.isFullInside(one))
	{
		logTestString("large aabbox3d<T> is fully inside\n");
		return false;
	}

	if (!two.intersectsWithBox(one))
	{
		logTestString("small aabbox3d<T> does not intersect\n");
		return false;
	}
	if (!three.intersectsWithBox(one))
	{
		logTestString("large aabbox3d<T> does not intersect\n");
		return false;
	}

	core::line3d<T> line(-2,-2,-2,2,2,2);
	if (!one.intersectsWithLine(line))
	{
		logTestString("aabbox3d<T> does not intersect with line(1)\n");
		return false;
	}
	line.end.set(2,2,10);
	if (!one.intersectsWithLine(line))
	{
		logTestString("aabbox3d<T> does not intersect with line(2)\n");
		return false;
	}
	line.end.set(0,2,10);
	if (one.intersectsWithLine(line))
	{
		logTestString("aabbox3d<T> does intersect with line(3)\n");
		return false;
	}
	return true;
}

template<class T>
static bool checkPoints()
{
	aabbox3d<T> one(-1,-2,-3,2,2,2);

	if (!one.isPointInside(core::vector3d<T>(-1,-2,-3)))
	{
		logTestString("isPointInside failed with min vertex\n");
		return false;
	}
	if (!one.isPointInside(core::vector3d<T>(-1,2,-3)))
	{
		logTestString("isPointInside failed with other min vertex\n");
		return false;
	}
	if (!one.isPointInside(core::vector3d<T>(2,-2,2)))
	{
		logTestString("isPointInside failed with other max vertex\n");
		return false;
	}
	if (!one.isPointInside(core::vector3d<T>(2,2,2)))
	{
		logTestString("isPointInside failed with max vertex\n");
		return false;
	}
	if (!one.isPointInside(core::vector3d<T>(0,0,0)))
	{
		logTestString("isPointInside failed with origin\n");
		return false;
	}
	if (!one.isPointInside(core::vector3d<T>((T)1.2,-1,1)))
	{
		logTestString("isPointInside failed with random point inside\n");
		return false;
	}
	if (one.isPointInside(core::vector3d<T>(-2,-2,-3)))
	{
		logTestString("isPointInside failed near min vertex\n");
		return false;
	}
	if (one.isPointInside(core::vector3d<T>(2,3,2)))
	{
		logTestString("isPointInside failed near max vertex\n");
		return false;
	}
	if (one.isPointInside(core::vector3d<T>(3,0,0)))
	{
		logTestString("isPointInside failed near origin\n");
		return false;
	}
	if (one.isPointInside(core::vector3d<T>((T)10.2,-1,1)))
	{
		logTestString("isPointInside failed with random point outside\n");
		return false;
	}
	if (one.isPointTotalInside(core::vector3d<T>(-1,-2,-3)))
	{
		logTestString("isPointTotalInside failed with min vertex\n");
		return false;
	}
	if (one.isPointTotalInside(core::vector3d<T>(-1,2,-3)))
	{
		logTestString("isPointTotalInside failed with other min vertex\n");
		return false;
	}
	if (one.isPointTotalInside(core::vector3d<T>(2,-2,2)))
	{
		logTestString("isPointTotalInside failed with other max vertex\n");
		return false;
	}
	if (one.isPointTotalInside(core::vector3d<T>(2,2,2)))
	{
		logTestString("isPointTotalInside failed with max vertex\n");
		return false;
	}
	if (!one.isPointTotalInside(core::vector3d<T>(0,0,0)))
	{
		logTestString("isPointTotalInside failed with origin\n");
		return false;
	}
	if (!one.isPointTotalInside(core::vector3d<T>((T)1.2,-1,1)))
	{
		logTestString("isPointTotalInside failed with random point inside\n");
		return false;
	}
	if (one.isPointTotalInside(core::vector3d<T>((T)10.2,-1,1)))
	{
		logTestString("isPointTotalInside failed with random point outside\n");
		return false;
	}

	core::plane3d<T> plane(core::vector3d<T>(0,0,-1), -10);
	if (one.classifyPlaneRelation(plane) != core::ISREL3D_BACK)
	{
		logTestString("box not behind\n");
		return false;
	}
	plane.D=0;
	if (one.classifyPlaneRelation(plane) != core::ISREL3D_CLIPPED)
	{
		logTestString("box not clipped\n");
		return false;
	}
	plane.D=10;
	if (one.classifyPlaneRelation(plane) != core::ISREL3D_FRONT)
	{
		logTestString("box not in front\n");
		return false;
	}
	return true;
}

template <class T>
static bool doTests()
{
	aabbox3d<T> empty;
	aabbox3d<T> one(-1,-1,-1,1,1,1);
	if (empty != one)
	{
		logTestString("default aabbox3d<T> wrong, or comparison failed\n");
		return false;
	}
	if (empty.getCenter() != core::vector3d<T>(0,0,0))
	{
		logTestString("default aabbox3d<T> has wrong Center\n");
		return false;
	}
	if (empty.getExtent() != core::vector3d<T>(2,2,2))
	{
		logTestString("default aabbox3d<T> has wrong Extent\n");
		return false;
	}
	if (empty.isEmpty())
	{
		logTestString("default aabbox3d<T> is empty\n");
		return false;
	}
	if (empty.getVolume() != 8)
	{
		logTestString("default aabbox3d<T> has wrong volume\n");
		return false;
	}
	if (empty.getArea() != 24)
	{
		logTestString("default aabbox3d<T> has wrong area\n");
		return false;
	}
	aabbox3d<T> two(core::vector3d<T>(-1,-1,-1),core::vector3d<T>(2,2,2));
	if (empty == two)
	{
		logTestString("empty aabbox3d<T> too large, or comparison failed\n");
		return false;
	}
	if (two.getCenter() != core::vector3d<T>((T)0.5,(T)0.5,(T)0.5))
	{
		logTestString("extended aabbox3d<T> has wrong Center\n");
		return false;
	}
	if (two.getExtent() != core::vector3d<T>(3,3,3))
	{
		logTestString("extended aabbox3d<T> has wrong Extent\n");
		return false;
	}
	if (two.isEmpty())
	{
		logTestString("extended aabbox3d<T> is empty\n");
		return false;
	}
	if (two.getVolume() != 27)
	{
		logTestString("extended aabbox3d<T> has wrong volume\n");
		return false;
	}
	if (two.getArea() != 54)
	{
		logTestString("extended aabbox3d<T> has wrong area\n");
		return false;
	}
	one.reset(1,1,1);
	if (one==empty)
	{
		logTestString("reset failed, or comparison failed\n");
		return false;
	}
	if (one.getCenter() != core::vector3d<T>(1,1,1))
	{
		logTestString("singular aabbox3d<T> has wrong Center\n");
		return false;
	}
	if (one.getExtent() != core::vector3d<T>(0,0,0))
	{
		logTestString("singular aabbox3d<T> has Extent\n");
		return false;
	}
	if (!one.isEmpty())
	{
		logTestString("empty aabbox3d<T> is not empty\n");
		return false;
	}
	if (one.getVolume() != 0)
	{
		logTestString("empty aabbox3d<T> has wrong volume\n");
		return false;
	}
	if (one.getArea() != 0)
	{
		logTestString("empty aabbox3d<T> has wrong area\n");
		return false;
	}
	one.addInternalPoint(core::vector3d<T>(-1,-1,-1));
	if (one!=empty)
	{
		logTestString("addInternalPoint failed, creating default bbox\n");
		return false;
	}
	one.reset(1,1,1);
	one.reset(empty);
	if (one!=empty)
	{
		logTestString("reset with bbox failed, creating default bbox\n");
		return false;
	}
	one.addInternalPoint(core::vector3d<T>(2,2,2));
	if (one != two)
	{
		logTestString("addInternalPoint for aabbox3d<T> failed.\n");
		return false;
	}
	one.addInternalBox(empty);
	if (one != two)
	{
		logTestString("addInternalBox with smaller box failed.\n");
		return false;
	}
	one.addInternalBox(two);
	if (one != two)
	{
		logTestString("addInternalBox with same box failed.\n");
		return false;
	}
	one.addInternalPoint(-1,-2,-3);
	two.addInternalPoint(-1,-2,-3);
	empty.addInternalBox(one);
	if (empty != two)
	{
		logTestString("addInternalBox with larger box failed\n");
		return false;
	}
	if (one.getCenter() != core::vector3d<T>((T)0.5,0,(T)-0.5))
	{
		logTestString("large aabbox3d<T> has wrong Center\n");
		return false;
	}
	if (one.getExtent() != core::vector3d<T>(3,4,5))
	{
		logTestString("large aabbox3d<T> has wrong Extent\n");
		return false;
	}
	if (one.isEmpty())
	{
		logTestString("large aabbox3d<T> is empty\n");
		return false;
	}
	if (one.getVolume() != 60)
	{
		logTestString("large aabbox3d<T> has wrong volume\n");
		return false;
	}
	if (one.getArea() != 94)
	{
		logTestString("large aabbox3d<T> has wrong area\n");
		return false;
	}
	if (!checkPoints<T>())
		return false;
	return true;
}


/** Test the functionality of aabbox3d<T>. */
bool testaabbox3d(void)
{
	bool f32Success = doTests<f32>();
	f32Success &= checkCollisions<f32>();
	if(f32Success)
		logTestString("aabbox3d<f32> tests passed\n\n");
	else
		logTestString("\n*** aabbox3d<f32> tests failed ***\n\n");

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

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

	return f32Success && f64Success && s32Success;
}