MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sphere.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 _sphere_cpp
41 #define _sphere_cpp
42 
43 /// @file
44 /// @brief This file contains the implementation for the generic Sphere class for math and spacial query.
45 
46 #include "sphere.h"
47 #include "mathtool.h"
48 #include "axisalignedbox.h"
49 #include "plane.h"
50 #include "ray.h"
51 #include "exception.h"
52 #include "serialization.h"
53 
54 #include <Ogre.h>
55 
56 namespace Mezzanine
57 {
59  Radius(0)
60  { }
61 
62  Sphere::Sphere(const Sphere& Other) :
63  Center(Other.Center),
64  Radius(Other.Radius)
65  { }
66 
67  Sphere::Sphere(const Real SphereRadius) :
68  Radius(SphereRadius)
69  { }
70 
71  Sphere::Sphere(const Vector3& SphereCenter, const Real SphereRadius) :
72  Center(SphereCenter),
73  Radius(SphereRadius)
74  { }
75 
76  Sphere::Sphere(const Ogre::Sphere& InternalSphere)
77  { this->ExtractOgreSphere(InternalSphere); }
78 
80  { }
81 
82  ///////////////////////////////////////////////////////////////////////////////
83  // Utility
84 
85  Boolean Sphere::IsInside(const Vector3& ToCheck) const
86  { return MathTools::IsInside(*this,ToCheck); }
87 
88  Boolean Sphere::IsOverlapping(const Sphere& ToCheck) const
89  { return MathTools::Overlap(*this,ToCheck); }
90 
91  Boolean Sphere::IsOverlapping(const AxisAlignedBox& ToCheck) const
92  { return MathTools::Overlap(ToCheck,*this); }
93 
94  Boolean Sphere::IsOverlapping(const Plane& ToCheck) const
95  { return MathTools::Overlap(ToCheck,*this); }
96 
98  { return MathTools::Intersects(*this,ToCheck); }
99 
100  ///////////////////////////////////////////////////////////////////////////////
101  // Conversion Methods
102 
103  void Sphere::ExtractOgreSphere(const Ogre::Sphere& InternalSphere)
104  { this->Center = InternalSphere.getCenter(); this->Radius = InternalSphere.getRadius(); }
105 
106  Ogre::Sphere Sphere::GetOgreSphere() const
107  { return Ogre::Sphere(this->Center.GetOgreVector3(),this->Radius); }
108 
109  ///////////////////////////////////////////////////////////////////////////////
110  // Serialization
111 
112  void Sphere::ProtoSerialize(XML::Node& ParentNode) const
113  {
114  XML::Node SelfRoot = ParentNode.AppendChild( Sphere::GetSerializableName() );
115 
116  if( SelfRoot.AppendAttribute("Version").SetValue("1") &&
117  SelfRoot.AppendAttribute("Radius").SetValue( this->Radius ) )
118  {
119  XML::Node CenterNode = SelfRoot.AppendChild("Center");
120  this->Center.ProtoSerialize( CenterNode );
121 
122  return;
123  }else{
124  SerializeError("Create XML Attribute Values",Sphere::GetSerializableName(),true);
125  }
126  }
127 
128  void Sphere::ProtoDeSerialize(const XML::Node& SelfRoot)
129  {
130  XML::Attribute CurrAttrib;
131 
132  if( String(SelfRoot.Name()) == Sphere::GetSerializableName() ) {
133  if(SelfRoot.GetAttribute("Version").AsInt() == 1) {
134  CurrAttrib = SelfRoot.GetAttribute("Radius");
135  if( !CurrAttrib.Empty() )
136  this->Radius = CurrAttrib.AsReal();
137 
138  // Get the properties that need their own nodes
139  XML::Node CenterNode = SelfRoot.GetChild("Center").GetFirstChild();
140  if( !CenterNode.Empty() ) {
141  Vector3 Cen(CenterNode);
142  this->Center = Cen;
143  }
144  }else{
145  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + Sphere::GetSerializableName() + ": Not Version 1.");
146  }
147  }else{
148  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,Sphere::GetSerializableName() + " was not found in the provided XML node, which was expected.");
149  }
150  }
151 
153  {
154  return "Sphere";
155  }
156 
157  ///////////////////////////////////////////////////////////////////////////////
158  // Operators
159 
160  void Sphere::operator=(const Sphere& Other)
161  { this->Center = Other.Center; this->Radius = Other.Radius; }
162 
163  void Sphere::operator=(const Ogre::Sphere& InternalSphere)
164  { this->ExtractOgreSphere(InternalSphere); }
165 
166  Boolean Sphere::operator>(const Sphere& Other) const
167  { return ( this->Radius > Other.Radius ); }
168 
169  Boolean Sphere::operator<(const Sphere& Other) const
170  { return ( this->Radius < Other.Radius ); }
171 
172  Boolean Sphere::operator>=(const Sphere& Other) const
173  { return ( this->Radius >= Other.Radius ); }
174 
175  Boolean Sphere::operator<=(const Sphere& Other) const
176  { return ( this->Radius <= Other.Radius ); }
177 
178  Boolean Sphere::operator==(const Sphere& Other) const
179  { return ( this->Center == Other.Center && this->Radius == Other.Radius ); }
180 
181  Boolean Sphere::operator!=(const Sphere& Other) const
182  { return ( this->Center != Other.Center || this->Radius != Other.Radius ); }
183 }//Mezzanine
184 
185 #endif