MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
axisalignedbox.cpp
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_cpp
41 #define _axisalignedbox_cpp
42 
43 /// @file
44 /// @brief This file contains the implementation for the AxisAlignedBox class for representing AABB's of objects in the world.
45 
46 #include "axisalignedbox.h"
47 #include "mathtool.h"
48 #include "plane.h"
49 #include "ray.h"
50 #include "exception.h"
51 #include "serialization.h"
52 
53 #include <Ogre.h>
54 
55 #include <algorithm>
56 
57 namespace Mezzanine
58 {
60  { }
61 
63  MinExt(Other.MinExt),
64  MaxExt(Other.MaxExt)
65  { }
66 
68  MinExt(Min),
69  MaxExt(Max)
70  { }
71 
72  AxisAlignedBox::AxisAlignedBox(const Ogre::AxisAlignedBox& InternalAABB)
73  { this->ExtractOgreAABB(InternalAABB); }
74 
76  { }
77 
78  ///////////////////////////////////////////////////////////////////////////////
79  // Utility
80 
81  Boolean AxisAlignedBox::IsZero() const
82  {
83  return ( this->MinExt.X >= this->MaxExt.X &&
84  this->MinExt.Y >= this->MaxExt.Y &&
85  this->MinExt.Z >= this->MaxExt.Z );
86  }
87 
89  {
90  Vector3 Diff = this->MaxExt - this->MinExt;
91  return ( Diff.X * Diff.Y * Diff.Z );
92  }
93 
95  {
96  Vector3 NewMin = this->MinExt, NewMax = this->MaxExt;
97  NewMin.Ceil(Other.MinExt);
98  NewMax.Floor(Other.MaxExt);
99 
100  if( NewMin.X < NewMax.X && NewMin.Y < NewMax.Y && NewMin.Z < NewMax.Z ) {
101  return AxisAlignedBox(NewMin,NewMax);
102  }else{
103  return AxisAlignedBox();
104  }
105  }
106 
107  void AxisAlignedBox::Expand(const Vector3& Point)
108  {
109  this->MinExt.Floor(Point);
110  this->MaxExt.Ceil(Point);
111  }
112 
114  {
115  this->MinExt.Floor(Other.MinExt);
116  this->MaxExt.Ceil(Other.MaxExt);
117  }
118 
119  Boolean AxisAlignedBox::IsInside(const Vector3& ToCheck) const
120  { return MathTools::IsInside(*this,ToCheck); }
121 
122  Boolean AxisAlignedBox::IsOverlapping(const Sphere& ToCheck) const
123  { return MathTools::Overlap(*this,ToCheck); }
124 
125  Boolean AxisAlignedBox::IsOverlapping(const AxisAlignedBox& ToCheck) const
126  { return MathTools::Overlap(*this,ToCheck); }
127 
128  Boolean AxisAlignedBox::IsOverlapping(const Plane& ToCheck) const
129  { return MathTools::Overlap(*this,ToCheck); }
130 
132  { return MathTools::Intersects(*this,ToCheck); }
133 
134  ///////////////////////////////////////////////////////////////////////////////
135  // Extents query
136 
137  void AxisAlignedBox::SetExtents(const Vector3& Min, const Vector3& Max)
138  { this->MinExt = Min; this->MaxExt = Max; }
139 
141  { return ( this->MaxExt - this->MinExt ); }
142 
144  { return ( this->GetSize() * 0.5 ); }
145 
147  { return ( this->MaxExt + this->MinExt ) * 0.5; }
148 
150  {
151  return Vector3( ( XEx == AE_Min ? this->MinExt.X : this->MaxExt.X ),
152  ( YEx == AE_Min ? this->MinExt.Y : this->MaxExt.Y ),
153  ( ZEx == AE_Min ? this->MinExt.Z : this->MaxExt.Z ) );
154  }
155 
156  ///////////////////////////////////////////////////////////////////////////////
157  // Conversion Methods
158 
159  void AxisAlignedBox::ExtractOgreAABB(const Ogre::AxisAlignedBox& InternalAABB)
160  { this->MinExt.ExtractOgreVector3( InternalAABB.getMinimum() ); this->MaxExt.ExtractOgreVector3( InternalAABB.getMaximum() ); }
161 
162  Ogre::AxisAlignedBox AxisAlignedBox::GetOgreAABB() const
163  { return Ogre::AxisAlignedBox(this->MinExt.GetOgreVector3(),this->MaxExt.GetOgreVector3()); }
164 
165  ///////////////////////////////////////////////////////////////////////////////
166  // Serialization
167 
169  {
170  XML::Node SelfRoot = ParentNode.AppendChild( AxisAlignedBox::GetSerializableName() );
171 
172  if( SelfRoot.AppendAttribute("Version").SetValue("1") )
173  {
174  XML::Node MinimumNode = SelfRoot.AppendChild("MinExt");
175  this->MinExt.ProtoSerialize( MinimumNode );
176 
177  XML::Node MaximumNode = SelfRoot.AppendChild("MaxExt");
178  this->MaxExt.ProtoSerialize( MaximumNode );
179 
180  return;
181  }else{
182  SerializeError("Create XML Attribute Values",AxisAlignedBox::GetSerializableName(),true);
183  }
184  }
185 
187  {
188  if( String(SelfRoot.Name()) == AxisAlignedBox::GetSerializableName() ) {
189  if(SelfRoot.GetAttribute("Version").AsInt() == 1) {
190  // Get the properties that need their own nodes
191  XML::Node MinimumNode = SelfRoot.GetChild("MinExt").GetFirstChild();
192  if( !MinimumNode.Empty() )
193  this->MinExt.ProtoDeSerialize(MinimumNode);
194 
195  XML::Node MaximumNode = SelfRoot.GetChild("MaxExt").GetFirstChild();
196  if( !MaximumNode.Empty() )
197  this->MaxExt.ProtoDeSerialize(MaximumNode);
198  }else{
199  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + AxisAlignedBox::GetSerializableName() + ": Not Version 1.");
200  }
201  }else{
202  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,AxisAlignedBox::GetSerializableName() + " was not found in the provided XML node, which was expected.");
203  }
204  }
205 
207  {
208  return "AxisAlignedBox";
209  }
210 
211  ///////////////////////////////////////////////////////////////////////////////
212  // Operators
213 
215  { this->MinExt = Other.MinExt; this->MaxExt = Other.MaxExt; }
216 
217  void AxisAlignedBox::operator=(const Ogre::AxisAlignedBox& InternalAABB)
218  { this->ExtractOgreAABB(InternalAABB); }
219 
220  Boolean AxisAlignedBox::operator>(const AxisAlignedBox& Other) const
221  { return ( this->GetVolume() > Other.GetVolume() ); }
222 
223  Boolean AxisAlignedBox::operator<(const AxisAlignedBox& Other) const
224  { return ( this->GetVolume() < Other.GetVolume() ); }
225 
226  Boolean AxisAlignedBox::operator>=(const AxisAlignedBox& Other) const
227  { return ( this->GetVolume() >= Other.GetVolume() ); }
228 
229  Boolean AxisAlignedBox::operator<=(const AxisAlignedBox& Other) const
230  { return ( this->GetVolume() <= Other.GetVolume() ); }
231 
232  Boolean AxisAlignedBox::operator==(const AxisAlignedBox& Other) const
233  { return ( this->MinExt == Other.MinExt && this->MaxExt == Other.MaxExt ); }
234 
235  Boolean AxisAlignedBox::operator!=(const AxisAlignedBox& Other) const
236  { return ( this->MinExt != Other.MinExt || this->MaxExt != Other.MaxExt ); }
237 }//Mezzanine
238 
239 #endif