Spinning Topp Logo BlackTopp Studios
inc
axisalignedbox.h
Go to the documentation of this file.
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine 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 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine 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 The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _axisalignedbox_h
41 #define _axisalignedbox_h
42 
43 /// @file
44 /// @brief This file contains the AxisAlignedBox class for representing AABB's of objects in the world.
45 
46 #include "vector3.h"
47 #include "plane.h"
48 
49 namespace Ogre
50 {
51  class AxisAlignedBox;
52 }
53 
54 namespace Mezzanine
55 {
56  class Plane;
57  class Ray;
58  class Sphere;
59  ///////////////////////////////////////////////////////////////////////////////
60  /// @brief This is a utility class used to represent the Axis-Aligned Bounding Boxes of objects in various subsystems.
61  /// @details
62  ///////////////////////////////////////
64  {
65  public:
66  /// @brief This enum is used to help make querys for data on the AABB.
68  {
69  AE_Min = 1,
70  AE_Max = 2
71  };
72  /// @brief This is a type used for the return of a ray intersection test.
73  /// @details This type provides more verbose return data that can be used for further tests. @n @n
74  /// The first member stores whether or not there was a hit. The second member stores ray containing
75  /// the points where the ray entered and exited the AABB.
76  typedef std::pair<Boole,Ray> RayTestResult;
77 
78  /// @brief Used to uniquely identify sides of an axis aligned box.
79  enum Side
80  {
81  SideMinX = 0,
82  SideMinY = 1,
83  SideMinZ = 2,
84  SideMaxX = 3,
85  SideMaxY = 4,
86  SideMaxZ = 5
87  };
88 
89 
90  ///////////////////////////////////////////////////////////////////////////////
91  // Public Data Members
92 
93  /// @internal
94  /// @brief The minimum extents on each axis in world space.
96  /// @internal
97  /// @brief The maximum extents on each axis in world space.
99 
100  ///////////////////////////////////////////////////////////////////////////////
101  // Construction and Destruction
102 
103  /// @brief Blank constructor.
104  AxisAlignedBox();
105  /// @brief Copy constructor.
106  /// @param Other The other AABB to copy.
107  AxisAlignedBox(const AxisAlignedBox& Other);
108  /// @brief Extents constructor.
109  /// @param Min The minimum extents on each axis in world space.
110  /// @param Max The maximum extents on each axis in world space.
111  AxisAlignedBox(const Vector3& Min, const Vector3& Max);
112  /// @brief Internal constructor.
113  /// @param InternalAABB The internal Ogre AxisAlignedBox to construct this AxisAlignedBox from.
114  explicit AxisAlignedBox(const Ogre::AxisAlignedBox& InternalAABB);
115  /// @brief Class destructor.
116  ~AxisAlignedBox();
117 
118  ///////////////////////////////////////////////////////////////////////////////
119  // Utility
120 
121  /// @brief Gets whether or not this AABB has no size.
122  /// @return Returns true if this AABB has no size/is invalid, false otherwise.
123  Boole IsZero() const;
124  /// @brief Gets the total volume of this AABB.
125  /// @return Returns the volume of this AABB expressed as cubic length units.
126  Real GetVolume() const;
127  /// @brief Gets an AABB that has the dimensions of the overlap between this AABB and another AABB.
128  /// @param Other The AABB to get the overlap for.
129  /// @return Returns a new AABB containing the overlap between this and another AABB.
130  AxisAlignedBox GetOverlap(const AxisAlignedBox& Other) const;
131  /// @brief Expands the size of this AABB to encompass it's current bounds plus a point in 3D space.
132  /// @param Point The point in 3D space to encompass.
133  void Expand(const Vector3& Point);
134  /// @brief Expands the size of this AABB to encompass it's current bounds plus another AABB.
135  /// @param Other The other AABB to encompass.
136  void Expand(const AxisAlignedBox& Other);
137 
138  /// @brief Checks to see if a point is inside this AABB.
139  /// @param ToCheck The location to check to see if it is within this AABB.
140  /// @return Returns true if the point provided is within this AABB, false otherwise.
141  Boole IsInside(const Vector3& ToCheck) const;
142  /// @brief Checks to see if a sphere overlaps with this AABB.
143  /// @param ToCheck The sphere to check for overlap.
144  /// @return Returns true if the provided sphere overlaps with this AABB, false otherwise.
145  Boole IsOverlapping(const Sphere& ToCheck) const;
146  /// @brief Checks to see if another AABB overlaps with this one.
147  /// @param ToCheck The other AABB to check for overlap.
148  /// @return Returns true if the two AABB's overlap, false otherwise.
149  Boole IsOverlapping(const AxisAlignedBox& ToCheck) const;
150  /// @brief Checks to see if a plane intersects this AABB.
151  /// @param ToCheck The plane to check for intersection.
152  /// @return Returns true if the provided plane intersects with this AABB, false otherwise.
153  Boole IsOverlapping(const Plane& ToCheck) const;
154  /// @brief Checks to see if a ray intersects this AABB.
155  /// @param ToCheck The ray to check for a hit.
156  /// @return Returns a std::pair containing whether or not the ray hit, and if it did the subsection of the ray that went through the AABB.
157  RayTestResult Intersects(const Ray& ToCheck) const;
158 
159  ///////////////////////////////////////////////////////////////////////////////
160  // Extents Methods
161 
162  /// @brief Sets the minimum and maximum extents of this AABB.
163  /// @param Min The minimum extents on each axis in world space.
164  /// @param Max The maximum extents on each axis in world space.
165  void SetExtents(const Vector3& Min, const Vector3& Max);
166 
167  /// @brief Gets the size of this AABB.
168  /// @return Returns a Vector3 representing the size of this AABB.
169  Vector3 GetSize() const;
170  /// @brief Gets half the size of this AABB.
171  /// @return Returns a Vector3 representing half the size of this AABB.
172  Vector3 GetHalfSize() const;
173  /// @brief Gets the center of this AABB.
174  /// @return Returns a Vector3 containing the center of this AABB.
175  Vector3 GetCenter() const;
176  /// @brief Gets the location of the specified corner.
177  /// @param XEx The extent for the X axis to retrieve.
178  /// @param YEx The extent for the Y axis to retrieve.
179  /// @param ZEx The extent for the Z axis to retrieve.
180  /// @return Returns a Vector3 representing the position of the specified corner.
181  Vector3 GetCorner(const AxisExtent XEx, const AxisExtent YEx, const AxisExtent ZEx) const;
182 
183 
184  Real GetSideExtent(Side WhichSideExtent) const;
185  /// @brief Get a plane corresponding to a side of the box.
186  /// @param WhichSidePlane Which side do you want a plane for?
187  Plane GetSidePlane(Side WhichSidePlane) const;
188 
189  ///////////////////////////////////////////////////////////////////////////////
190  // Conversion Methods
191 
192  /// @brief Changes this AxisAlignedBox to match the Ogre AxisAlignedBox.
193  /// @param InternalAABB The Ogre::AxisAlignedBox to copy.
194  void ExtractOgreAABB(const Ogre::AxisAlignedBox& InternalAABB);
195  /// @brief Gets an Ogre::AxisAlignedBox that contains this Spheres information.
196  /// @return This returns an Ogre::AxisAlignedBox that contains the same information as this AxisAlignedBoxes information.
197  Ogre::AxisAlignedBox GetOgreAABB() const;
198 
199  ///////////////////////////////////////////////////////////////////////////////
200  // Serialization
201 
202  /// @brief Convert this class to an XML::Node ready for serialization.
203  /// @param ParentNode The point in the XML hierarchy that all this shape should be appended to.
204  void ProtoSerialize(XML::Node& ParentNode) const;
205  /// @brief Take the data stored in an XML Node and overwrite this object with it.
206  /// @param SelfRoot An XML::Node containing the data to populate this class with.
207  void ProtoDeSerialize(const XML::Node& SelfRoot);
208 
209  /// @brief Get the name of the the XML tag this class will leave behind as its instances are serialized.
210  /// @return A string containing the name of this class.
211  static String GetSerializableName();
212 
213  ///////////////////////////////////////////////////////////////////////////////
214  // Operators
215 
216  /// @brief Assignment operator.
217  /// @param Other The other AABB to copy from.
218  void operator=(const AxisAlignedBox& Other);
219 
220  /// @brief The assignment operator from Ogre::AxisAlignedBox to Mezzanine::AxisAlignedBox.
221  /// @param InternalAABB The Ogre::AxisAlignedBox to take data from.
222  void operator=(const Ogre::AxisAlignedBox& InternalAABB);
223 
224  /// @brief Greater-than operator.
225  /// @note This operator compares the volume of both AABBs.
226  /// @param Other The other AABB to compare with.
227  /// @return Returns true if this AABB is larger than the other provided AABB, false otherwise.
228  Boole operator>(const AxisAlignedBox& Other) const;
229  /// @brief Greater-than or equals-to operator.
230  /// @note This operator compares the volume of both AABBs.
231  /// @param Other The other AABB to compare with.
232  /// @return Returns true if this AABB is larger than or equal to the other provided AABB, false otherwise.
233  Boole operator<(const AxisAlignedBox& Other) const;
234  /// @brief Less-than operator.
235  /// @note This operator compares the volume of both AABBs.
236  /// @param Other The other AABB to compare with.
237  /// @return Returns true if this AABB is smaller than the other provided AABB, false otherwise.
238  Boole operator>=(const AxisAlignedBox& Other) const;
239  /// @brief Less-than or equals-to operator.
240  /// @note This operator compares the volume of both AABBs.
241  /// @param Other The other AABB to compare with.
242  /// @return Returns true if this AABB is smaller than or equal to the other provided AABB, false otherwise.
243  Boole operator<=(const AxisAlignedBox& Other) const;
244 
245  /// @brief Equality operator.
246  /// @param Other The other AABB to compare with.
247  /// @return Returns true if this AABB is the same as the other provided AABB, false otherwise.
248  Boole operator==(const AxisAlignedBox& Other) const;
249  /// @brief Inequality operator.
250  /// @param Other The other AABB to compare with.
251  /// @return Returns true if this AABB is not the same as the other provided AABB, false otherwise.
252  Boole operator!=(const AxisAlignedBox& Other) const;
253  };//AxisAlignedBox
254 }//Mezzanine
255 
256 #endif
This is a generic sphere class used for spacial queries.
Definition: sphere.h:62
AxisExtent
This enum is used to help make querys for data on the AABB.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
Side
Used to uniquely identify sides of an axis aligned box.
This is used to represent a flat infinite slice of the game world.
Definition: plane.h:65
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
Vector3 MinExt
The minimum extents on each axis in world space.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
std::pair< Boole, Ray > RayTestResult
This is a type used for the return of a ray intersection test.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Vector3 MaxExt
The maximum extents on each axis in world space.
This represents a line placed in 3D space and is used with spacial queries.
Definition: ray.h:67
This is a utility class used to represent the Axis-Aligned Bounding Boxes of objects in various subsy...