Pyrogenesis  trunk
BoundingSphere.h
Go to the documentation of this file.
1 /* Copyright (C) 2014 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_BOUNDINGSPHERE
19 #define INCLUDED_BOUNDINGSPHERE
20 
22 #include "maths/Vector3D.h"
23 
25 {
26 private:
28  float m_Radius;
29 
30 public:
31  CBoundingSphere() : m_Radius(0) { }
32 
33  CBoundingSphere(const CVector3D& center, float radius) : m_Center(center), m_Radius(radius) { }
34 
35  /**
36  * Construct a bounding sphere that encompasses a bounding box
37  * swept through all possible rotations around the origin.
38  */
40  {
41  float maxX = std::max(fabsf(bbox[0].X), fabsf(bbox[1].X));
42  float maxY = std::max(fabsf(bbox[0].Y), fabsf(bbox[1].Y));
43  float maxZ = std::max(fabsf(bbox[0].Z), fabsf(bbox[1].Z));
44  float radius = sqrtf(maxX*maxX + maxY*maxY + maxZ*maxZ);
45 
46  return CBoundingSphere(CVector3D(0.f, 0.f, 0.f), radius);
47  }
48 
50  {
51  return m_Center;
52  }
53 
54  float GetRadius() const
55  {
56  return m_Radius;
57  }
58 
59  /**
60  * Check if the ray, defined by an origin point and a direction unit vector
61  * interesects with the sphere
62  */
63  bool RayIntersect(const CVector3D& origin, const CVector3D& dir) const
64  {
65  CVector3D v = m_Center - origin; // Vector v from the origin of the ray to the center of the sphere
66  float pcLen = dir.Dot(v); // Length of the projection of v onto the direction vector of the ray
67  if(pcLen <= 0)
68  return false; // Sphere behind the ray
69  // Get the shortest distance from the center of the sphere to the ray
70  v = (dir * pcLen) - v;
71  if (v.LengthSquared() > m_Radius * m_Radius)
72  return false; // Distance to sphere center more than radius
73  return true;
74  }
75 };
76 
77 #endif // INCLUDED_BOUNDINGSPHERE
Definition: Decompose.h:22
float Dot(const CVector3D &vector) const
Definition: Vector3D.h:99
Definition: Vector3D.h:28
CBoundingSphere()
Definition: BoundingSphere.h:31
bool RayIntersect(const CVector3D &origin, const CVector3D &dir) const
Check if the ray, defined by an origin point and a direction unit vector interesects with the sphere...
Definition: BoundingSphere.h:63
float GetRadius() const
Definition: BoundingSphere.h:54
CBoundingSphere(const CVector3D &center, float radius)
Definition: BoundingSphere.h:33
static CBoundingSphere FromSweptBox(const CBoundingBoxAligned &bbox)
Construct a bounding sphere that encompasses a bounding box swept through all possible rotations arou...
Definition: BoundingSphere.h:39
#define X(id)
Definition: CStrIntern.cpp:89
Definition: Decompose.h:22
Definition: BoundingSphere.h:24
Definition: BoundingBoxAligned.h:35
CVector3D m_Center
Definition: BoundingSphere.h:27
float LengthSquared() const
Definition: Vector3D.cpp:48
const CVector3D & GetCenter()
Definition: BoundingSphere.h:49
float m_Radius
Definition: BoundingSphere.h:28