MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
axisalignedbox.h
Go to the documentation of this file.
1 // © Copyright 2010 - 2014 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 
48 namespace Ogre
49 {
50  class AxisAlignedBox;
51 }
52 
53 namespace Mezzanine
54 {
55  class Plane;
56  class Ray;
57  class Sphere;
58  ///////////////////////////////////////////////////////////////////////////////
59  /// @brief This is a utility class used to represent the Axis-Aligned Bounding Boxes of objects in various subsystems.
60  /// @details
61  ///////////////////////////////////////
63  {
64  public:
65  /// @enum AxisExtent
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 the points where the ray entered and exited the AABB.
75  typedef std::pair<Boolean,Ray> RayTestResult;
76 
77  ///////////////////////////////////////////////////////////////////////////////
78  // Public Data Members
79 
80  /// @internal
81  /// @brief The minimum extents on each axis in world space.
83  /// @internal
84  /// @brief The maximum extents on each axis in world space.
86 
87  ///////////////////////////////////////////////////////////////////////////////
88  // Construction and Destruction
89 
90  /// @brief Blank constructor.
92  /// @brief Copy constructor.
93  /// @param Other The other AABB to copy.
94  AxisAlignedBox(const AxisAlignedBox& Other);
95  /// @brief Extents constructor.
96  /// @param Min The minimum extents on each axis in world space.
97  /// @param Max The maximum extents on each axis in world space.
98  AxisAlignedBox(const Vector3& Min, const Vector3& Max);
99  /// @brief Internal constructor.
100  /// @param InternalAABB The internal Ogre AxisAlignedBox to construct this AxisAlignedBox from.
101  explicit AxisAlignedBox(const Ogre::AxisAlignedBox& InternalAABB);
102  /// @brief Class destructor.
103  ~AxisAlignedBox();
104 
105  ///////////////////////////////////////////////////////////////////////////////
106  // Utility
107 
108  /// @brief Gets whether or not this AABB has no size.
109  /// @return Returns true if this AABB has no size/is invalid, false otherwise.
110  Boolean IsZero() const;
111  /// @brief Gets the total volume of this AABB.
112  /// @return Returns the volume of this AABB expressed as cubic length units.
113  Real GetVolume() const;
114  /// @brief Gets an AABB that has the dimensions of the overlap between this AABB and another AABB.
115  /// @param Other The AABB to get the overlap for.
116  /// @return Returns a new AABB containing the overlap between this and another AABB.
117  AxisAlignedBox GetOverlap(const AxisAlignedBox& Other) const;
118  /// @brief Expands the size of this AABB to encompass it's current bounds plus a point in 3D space.
119  /// @param Point The point in 3D space to encompass.
120  void Expand(const Vector3& Point);
121  /// @brief Expands the size of this AABB to encompass it's current bounds plus another AABB.
122  /// @param Other The other AABB to encompass.
123  void Expand(const AxisAlignedBox& Other);
124 
125  /// @brief Checks to see if a point is inside this AABB.
126  /// @param ToCheck The location to check to see if it is within this AABB.
127  /// @return Returns true if the point provided is within this AABB, false otherwise.
128  Boolean IsInside(const Vector3& ToCheck) const;
129  /// @brief Checks to see if a sphere overlaps with this AABB.
130  /// @param ToCheck The sphere to check for overlap.
131  /// @return Returns true if the provided sphere overlaps with this AABB, false otherwise.
132  Boolean IsOverlapping(const Sphere& ToCheck) const;
133  /// @brief Checks to see if another AABB overlaps with this one.
134  /// @param ToCheck The other AABB to check for overlap.
135  /// @return Returns true if the two AABB's overlap, false otherwise.
136  Boolean IsOverlapping(const AxisAlignedBox& ToCheck) const;
137  /// @brief Checks to see if a plane intersects this AABB.
138  /// @param ToCheck The plane to check for intersection.
139  /// @return Returns true if the provided plane intersects with this AABB, false otherwise.
140  Boolean IsOverlapping(const Plane& ToCheck) const;
141  /// @brief Checks to see if a ray intersects this AABB.
142  /// @param ToCheck The ray to check for a hit.
143  /// @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.
144  RayTestResult Intersects(const Ray& ToCheck) const;
145 
146  ///////////////////////////////////////////////////////////////////////////////
147  // Extents Methods
148 
149  /// @brief Sets the minimum and maximum extents of this AABB.
150  /// @param Min The minimum extents on each axis in world space.
151  /// @param Max The maximum extents on each axis in world space.
152  void SetExtents(const Vector3& Min, const Vector3& Max);
153 
154  /// @brief Gets the size of this AABB.
155  /// @return Returns a Vector3 representing the size of this AABB.
156  Vector3 GetSize() const;
157  /// @brief Gets half the size of this AABB.
158  /// @return Returns a Vector3 representing half the size of this AABB.
159  Vector3 GetHalfSize() const;
160  /// @brief Gets the center of this AABB.
161  /// @return Returns a Vector3 containing the center of this AABB.
162  Vector3 GetCenter() const;
163  /// @brief Gets the location of the specified corner.
164  /// @param XEx The extent for the X axis to retrieve.
165  /// @param YEx The extent for the Y axis to retrieve.
166  /// @param ZEx The extent for the Z axis to retrieve.
167  /// @return Returns a Vector3 representing the position of the specified corner.
168  Vector3 GetCorner(const AxisExtent XEx, const AxisExtent YEx, const AxisExtent ZEx) const;
169 
170  ///////////////////////////////////////////////////////////////////////////////
171  // Conversion Methods
172 
173  /// @brief Changes this AxisAlignedBox to match the Ogre AxisAlignedBox.
174  /// @param InternalAABB The Ogre::AxisAlignedBox to copy.
175  void ExtractOgreAABB(const Ogre::AxisAlignedBox& InternalAABB);
176  /// @brief Gets an Ogre::AxisAlignedBox that contains this Spheres information.
177  /// @return This returns an Ogre::AxisAlignedBox that contains the same information as this AxisAlignedBoxes information.
178  Ogre::AxisAlignedBox GetOgreAABB() const;
179 
180  ///////////////////////////////////////////////////////////////////////////////
181  // Serialization
182 
183  /// @brief Convert this class to an XML::Node ready for serialization.
184  /// @param ParentNode The point in the XML hierarchy that all this renderable should be appended to.
185  void ProtoSerialize(XML::Node& ParentNode) const;
186  /// @brief Take the data stored in an XML Node and overwrite this object with it.
187  /// @param SelfRoot An XML::Node containing the data to populate this class with.
188  void ProtoDeSerialize(const XML::Node& SelfRoot);
189 
190  /// @brief Get the name of the the XML tag the proxy class will leave behind as its instances are serialized.
191  /// @return A string containing the name of this class.
192  static String GetSerializableName();
193 
194  ///////////////////////////////////////////////////////////////////////////////
195  // Operators
196 
197  /// @brief Assignment operator.
198  /// @param Other The other AABB to copy from.
199  void operator=(const AxisAlignedBox& Other);
200 
201  /// @brief The assignment operator from Ogre::AxisAlignedBox to Mezzanine::AxisAlignedBox.
202  /// @param InternalAABB The Ogre::AxisAlignedBox to take data from.
203  void operator=(const Ogre::AxisAlignedBox& InternalAABB);
204 
205  /// @brief Greater-than operator.
206  /// @note This operator compares the volume of both AABBs.
207  /// @param Other The other AABB to compare with.
208  /// @return Returns true if this AABB is larger than the other provided AABB, false otherwise.
209  Boolean operator>(const AxisAlignedBox& Other) const;
210  /// @brief Greater-than or equals-to operator.
211  /// @note This operator compares the volume of both AABBs.
212  /// @param Other The other AABB to compare with.
213  /// @return Returns true if this AABB is larger than or equal to the other provided AABB, false otherwise.
214  Boolean operator<(const AxisAlignedBox& Other) const;
215  /// @brief Less-than operator.
216  /// @note This operator compares the volume of both AABBs.
217  /// @param Other The other AABB to compare with.
218  /// @return Returns true if this AABB is smaller than the other provided AABB, false otherwise.
219  Boolean operator>=(const AxisAlignedBox& Other) const;
220  /// @brief Less-than or equals-to operator.
221  /// @note This operator compares the volume of both AABBs.
222  /// @param Other The other AABB to compare with.
223  /// @return Returns true if this AABB is smaller than or equal to the other provided AABB, false otherwise.
224  Boolean operator<=(const AxisAlignedBox& Other) const;
225 
226  /// @brief Equality operator.
227  /// @param Other The other AABB to compare with.
228  /// @return Returns true if this AABB is the same as the other provided AABB, false otherwise.
229  Boolean operator==(const AxisAlignedBox& Other) const;
230  /// @brief Inequality operator.
231  /// @param Other The other AABB to compare with.
232  /// @return Returns true if this AABB is not the same as the other provided AABB, false otherwise.
233  Boolean operator!=(const AxisAlignedBox& Other) const;
234  };//AxisAlignedBox
235 }//Mezzanine
236 
237 #endif