File: test_Bound.h

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (241 lines) | stat: -rw-r--r-- 7,964 bytes parent folder | download
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
/* Copyright (C) 2019 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "lib/self_test.h"

#include "lib/posix/posix.h"
#include "maths/BoundingBoxAligned.h"
#include "maths/BoundingBoxOriented.h"
#include "maths/Matrix3D.h"

#define TS_ASSERT_VEC_DELTA(v, x, y, z, delta) \
	TS_ASSERT_DELTA(v.X, x, delta); \
	TS_ASSERT_DELTA(v.Y, y, delta); \
	TS_ASSERT_DELTA(v.Z, z, delta);

class TestBound : public CxxTest::TestSuite
{
public:
	void setUp()
	{
		CxxTest::setAbortTestOnFail(true);
	}

	void test_empty_aabb()
	{
		CBoundingBoxAligned bound;
		TS_ASSERT(bound.IsEmpty());
		bound += CVector3D(1, 2, 3);
		TS_ASSERT(! bound.IsEmpty());
		bound.SetEmpty();
		TS_ASSERT(bound.IsEmpty());
	}

	void test_empty_obb()
	{
		CBoundingBoxOriented bound;
		TS_ASSERT(bound.IsEmpty());
		bound.m_Basis[0] = CVector3D(1,0,0);
		bound.m_Basis[1] = CVector3D(0,1,0);
		bound.m_Basis[2] = CVector3D(0,0,1);
		bound.m_HalfSizes = CVector3D(1,2,3);
		TS_ASSERT(!bound.IsEmpty());
		bound.SetEmpty();
		TS_ASSERT(bound.IsEmpty());
	}

	void test_extend_vector()
	{
		CBoundingBoxAligned bound;
		CVector3D v (1, 2, 3);
		bound += v;

		CVector3D center;
		bound.GetCenter(center);
		TS_ASSERT_EQUALS(center, v);
	}

	void test_extend_bound()
	{
		CBoundingBoxAligned bound;
		CVector3D v (1, 2, 3);
		CBoundingBoxAligned b (v, v);
		bound += b;

		CVector3D center;
		bound.GetCenter(center);
		TS_ASSERT_EQUALS(center, v);
	}

	void test_aabb_to_obb_translation()
	{
		CBoundingBoxAligned aabb(CVector3D(-1,-2,-1), CVector3D(1,2,1));

		CMatrix3D translation;
		translation.SetTranslation(CVector3D(1,3,7));

		CBoundingBoxOriented result;
		aabb.Transform(translation, result);

		TS_ASSERT_VEC_DELTA(result.m_Center,    1.f, 3.f, 7.f, 1e-7f);
		TS_ASSERT_VEC_DELTA(result.m_Basis[0],  1.f, 0.f, 0.f, 1e-7f);
		TS_ASSERT_VEC_DELTA(result.m_Basis[1],  0.f, 1.f, 0.f, 1e-7f);
		TS_ASSERT_VEC_DELTA(result.m_Basis[2],  0.f, 0.f, 1.f, 1e-7f);
		TS_ASSERT_VEC_DELTA(result.m_HalfSizes, 1.f, 2.f, 1.f, 1e-7f);
	}

	void test_aabb_to_obb_rotation_around_origin()
	{
		// rotate a 4x3x3 AABB centered at (5,0,0) 90 degrees CCW around the Z axis, and verify that the
		// resulting OBB is correct
		CBoundingBoxAligned aabb(CVector3D(3, -1.5f, -1.5f), CVector3D(7, 1.5f, 1.5f));

		CMatrix3D rotation;
		rotation.SetZRotation(float(M_PI)/2.f);

		CBoundingBoxOriented result;
		aabb.Transform(rotation, result);

		TS_ASSERT_VEC_DELTA(result.m_Center,    0.f, 5.f, 0.f, 1e-6f); // involves some trigonometry, lower precision
		TS_ASSERT_VEC_DELTA(result.m_Basis[0],  0.f, 1.f, 0.f, 1e-7f);
		TS_ASSERT_VEC_DELTA(result.m_Basis[1], -1.f, 0.f, 0.f, 1e-7f);
		TS_ASSERT_VEC_DELTA(result.m_Basis[2],  0.f, 0.f, 1.f, 1e-7f);
	}

	void test_aabb_to_obb_rotation_around_point()
	{
		// rotate a 4x3x3 AABB centered at (5,0,0) 45 degrees CW around the Z axis through (2,0,0)
		CBoundingBoxAligned aabb(CVector3D(3, -1.5f, -1.5f), CVector3D(7, 1.5f, 1.5f));

		// move everything so (2,0,0) becomes the origin, do the rotation, then move everything back
		CMatrix3D translate;
		CMatrix3D rotate;
		CMatrix3D translateBack;
		translate.SetTranslation(-2.f, 0, 0);
		rotate.SetZRotation(-float(M_PI)/4.f);
		translateBack.SetTranslation(2.f, 0, 0);

		CMatrix3D transform;
		transform.SetIdentity();
		transform.Concatenate(translate);
		transform.Concatenate(rotate);
		transform.Concatenate(translateBack);

		CBoundingBoxOriented result;
		aabb.Transform(transform, result);

		const float invSqrt2 = 1.f/sqrtf(2.f);

		TS_ASSERT_VEC_DELTA(result.m_Center,   3*invSqrt2 + 2, -3*invSqrt2, 0.f, 1e-6f); // involves some trigonometry, lower precision
		TS_ASSERT_VEC_DELTA(result.m_Basis[0],       invSqrt2,   -invSqrt2, 0.f, 1e-7f);
		TS_ASSERT_VEC_DELTA(result.m_Basis[1],       invSqrt2,    invSqrt2, 0.f, 1e-7f);
		TS_ASSERT_VEC_DELTA(result.m_Basis[2],            0.f,         0.f, 1.f, 1e-7f);
	}

	void test_aabb_to_obb_scale()
	{
		CBoundingBoxAligned aabb(CVector3D(3, -1.5f, -1.5f), CVector3D(7, 1.5f, 1.5f));

		CMatrix3D scale;
		scale.SetScaling(1.f, 3.f, 7.f);

		CBoundingBoxOriented result;
		aabb.Transform(scale, result);

		TS_ASSERT_VEC_DELTA(result.m_Center,    5.f,  0.f,   0.f, 1e-7f);
		TS_ASSERT_VEC_DELTA(result.m_HalfSizes, 2.f, 4.5f, 10.5f, 1e-7f);
		TS_ASSERT_VEC_DELTA(result.m_Basis[0],  1.f,  0.f,   0.f, 1e-7f);
		TS_ASSERT_VEC_DELTA(result.m_Basis[1],  0.f,  1.f,   0.f, 1e-7f);
		TS_ASSERT_VEC_DELTA(result.m_Basis[2],  0.f,  0.f,   1.f, 1e-7f);
	}

	// Verify that ray/OBB intersection is correctly determined in degenerate case where the
	// box has zero size in one of its dimensions.
	void test_degenerate_obb_ray_intersect()
	{
		// create OBB of a flat 1x1 square in the X/Z plane, with 0 size in the Y dimension
		CBoundingBoxOriented bound;
		bound.m_Basis[0] = CVector3D(1,0,0); // X
		bound.m_Basis[1] = CVector3D(0,1,0); // Y
		bound.m_Basis[2] = CVector3D(0,0,1); // Z
		bound.m_HalfSizes[0] = 1.f;
		bound.m_HalfSizes[1] = 0.f; // no height, i.e. a "flat" OBB
		bound.m_HalfSizes[2] = 1.f;
		bound.m_Center = CVector3D(0,0,0);

		// create two rays; one that should hit the OBB, and one that should miss it
		CVector3D ray1origin(-3.5f, 3.f, 0.f);
		CVector3D ray1direction(1.f, -1.f, 0.f);
		CVector3D ray2origin(-4.5f, 3.f, 0.f);
		CVector3D ray2direction(1.f, -1.f, 0.f);

		float tMin, tMax;
		TSM_ASSERT("Ray 1 should intersect the OBB", bound.RayIntersect(ray1origin, ray1direction, tMin, tMax));
		TSM_ASSERT("Ray 2 should not intersect the OBB", !bound.RayIntersect(ray2origin, ray2direction, tMin, tMax));
	}

	// Verify that transforming a flat AABB to an OBB does not produce NaN basis vectors in the
	// resulting OBB (see http://trac.wildfiregames.com/ticket/1121)
	void test_degenerate_aabb_to_obb_transform()
	{
		// create a flat AABB, transform it with some matrix (can even be the identity matrix),
		// and verify that the result does not contain any NaN values in its basis vectors
		// and/or half-sizes
		CBoundingBoxAligned flatAabb(CVector3D(-1,0,-1), CVector3D(1,0,1));

		CMatrix3D transform;
		transform.SetIdentity();

		CBoundingBoxOriented result;
		flatAabb.Transform(transform, result);

		TS_ASSERT(!isnan(result.m_Basis[0].X) && !isnan(result.m_Basis[0].Y) && !isnan(result.m_Basis[0].Z));
		TS_ASSERT(!isnan(result.m_Basis[1].X) && !isnan(result.m_Basis[1].Y) && !isnan(result.m_Basis[1].Z));
		TS_ASSERT(!isnan(result.m_Basis[2].X) && !isnan(result.m_Basis[2].Y) && !isnan(result.m_Basis[2].Z));
	}

	void test_point_visibility()
	{
		const CBoundingBoxAligned bb(CVector3D(1.0f, -10.0f, 3.0f), CVector3D(3.0f, -8.0f, 5.0f));

		TS_ASSERT(!bb.IsPointInside(CVector3D(0.0f, 0.0f, 0.0f)));
		TS_ASSERT(bb.IsPointInside(bb[0]));
		TS_ASSERT(bb.IsPointInside(bb[1]));

		CVector3D center;
		bb.GetCenter(center);
		TS_ASSERT(bb.IsPointInside(center));

		for (int offsetX = -1; offsetX <= 1; ++offsetX)
			for (int offsetY = -1; offsetY <= 1; ++offsetY)
				for (int offsetZ = -1; offsetZ <= 1; ++offsetZ)
				{
					TS_ASSERT(bb.IsPointInside(
						center + CVector3D(offsetX, offsetY, offsetZ) * 0.9f));
					const bool isInside = bb.IsPointInside(
						center + CVector3D(offsetX, offsetY, offsetZ) * 1.1f);
					if (offsetX == 0 && offsetY == 0 && offsetZ == 0)
					{
						TS_ASSERT(isInside);
					}
					else
					{
						TS_ASSERT(!isInside);
					}
				}
	}
};