File: BoundingBoxOriented.cpp

package info (click to toggle)
0ad 0.0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 51,248 kB
  • ctags: 46,933
  • sloc: cpp: 223,208; ansic: 31,240; python: 16,343; perl: 4,083; sh: 1,011; makefile: 915; xml: 733; java: 621; ruby: 229; erlang: 53; sql: 40
file content (97 lines) | stat: -rw-r--r-- 3,111 bytes parent folder | download | duplicates (6)
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
/* Copyright (C) 2011 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 "precompiled.h"

#include "BoundingBoxOriented.h"
#include "maths/BoundingBoxAligned.h"

#include <float.h>

const CBoundingBoxOriented CBoundingBoxOriented::EMPTY = CBoundingBoxOriented();

CBoundingBoxOriented::CBoundingBoxOriented(const CBoundingBoxAligned& bound)
{
	if (bound.IsEmpty())
	{
		SetEmpty();
	}
	else
	{
		bound.GetCentre(m_Center);

		// the axes of an AABB are the world-space axes
		m_Basis[0].X = 1.f; m_Basis[0].Y = 0.f; m_Basis[0].Z = 0.f;
		m_Basis[1].X = 0.f; m_Basis[1].Y = 1.f; m_Basis[1].Z = 0.f;
		m_Basis[2].X = 0.f; m_Basis[2].Y = 0.f; m_Basis[2].Z = 1.f;

		// element-wise division by two to get half sizes (remember, [1] and [0] are the max and min coord points)
		m_HalfSizes = (bound[1] - bound[0]) * 0.5f;
	}
}

bool CBoundingBoxOriented::RayIntersect(const CVector3D& origin, const CVector3D& dir, float& tMin_out, float& tMax_out) const
{
	// See Real-Time Rendering, Third Edition, p. 743
	float tMin = -FLT_MAX;
	float tMax = FLT_MAX;

	CVector3D p = m_Center - origin;

	for (int i = 0; i < 3; ++i)
	{
		// test the ray for intersections with the slab whose normal vector is m_Basis[i]
		float e = m_Basis[i].Dot(p); // distance between the ray origin and the box center projected onto the slab normal
		float f = m_Basis[i].Dot(dir); // cosine of the angle between the slab normal and the ray direction

		if(fabsf(f) > 1e-10f)
		{
			// Determine the distances t1 and t2 from the origin of the ray to the points where it intersects
			// the slab. See docs/ray_intersect.pdf for why/how this works.
			float invF = 1.f/f;
			float t1 = (e + m_HalfSizes[i]) * invF;
			float t2 = (e - m_HalfSizes[i]) * invF;

			// make sure t1 <= t2, swap if necessary
			if (t1 > t2)
			{
				float tmp = t1;
				t1 = t2;
				t2 = tmp;
			}

			// update the overall tMin and tMax if necessary
			if (t1 > tMin) tMin = t1;
			if (t2 < tMax) tMax = t2;

			// try to break out of the loop as fast as possible by checking for some conditions
			if (tMin > tMax) return false; // ray misses the box
			if (tMax < 0) return false; // box is behind the ray origin
		}
		else
		{
			// the ray is parallel to the slab currently being tested, or is as close to parallel
			// as makes no difference; return false if the ray is outside of the slab.
			if (e > m_HalfSizes[i] || -e > m_HalfSizes[i])
				return false;
		}
	}

	tMin_out = tMin;
	tMax_out = tMax;
	return true;
}