Spinning Topp Logo BlackTopp Studios
inc
sphere.cpp
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 _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 "MathTools/mathtools.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  Boole Sphere::IsInside(const Vector3& ToCheck) const
86  { return MathTools::IsInside(*this,ToCheck); }
87 
88  Boole Sphere::IsOverlapping(const Sphere& ToCheck) const
89  { return MathTools::Overlap(*this,ToCheck); }
90 
92  { return MathTools::Overlap(ToCheck,*this); }
93 
94  Boole 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(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + Sphere::GetSerializableName() + ": Not Version 1.");
146  }
147  }else{
148  MEZZ_EXCEPTION(ExceptionBase::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  Boole Sphere::operator>(const Sphere& Other) const
167  { return ( this->Radius > Other.Radius ); }
168 
169  Boole Sphere::operator<(const Sphere& Other) const
170  { return ( this->Radius < Other.Radius ); }
171 
172  Boole Sphere::operator>=(const Sphere& Other) const
173  { return ( this->Radius >= Other.Radius ); }
174 
175  Boole Sphere::operator<=(const Sphere& Other) const
176  { return ( this->Radius <= Other.Radius ); }
177 
178  Boole Sphere::operator==(const Sphere& Other) const
179  { return ( this->Center == Other.Center && this->Radius == Other.Radius ); }
180 
181  Boole Sphere::operator!=(const Sphere& Other) const
182  { return ( this->Center != Other.Center || this->Radius != Other.Radius ); }
183 }//Mezzanine
184 
185 #endif
This is a generic sphere class used for spacial queries.
Definition: sphere.h:62
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
Definition: sphere.cpp:128
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
Boole operator>=(const Sphere &Other) const
Less-than operator.
Definition: sphere.cpp:172
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
Thrown when the requested identity could not be found.
Definition: exception.h:94
Node GetFirstChild() const
Get the first child Node of this Node.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
Real Radius
The radius of the sphere.
Definition: sphere.h:76
void ProtoSerialize(XML::Node &ParentNode) const
Convert this class to an XML::Node ready for serialization.
Definition: sphere.cpp:112
~Sphere()
Class destructor.
Definition: sphere.cpp:79
static String GetSerializableName()
Get the name of the the XML tag this class will leave behind as its instances are serialized...
Definition: sphere.cpp:152
bool Empty() const
Is this storing anything at all?
This is used to represent a flat infinite slice of the game world.
Definition: plane.h:65
This implements the exception hiearchy for Mezzanine.
Ogre::Sphere GetOgreSphere() const
Gets an Ogre::Sphere that contains this Spheres information.
Definition: sphere.cpp:106
Sphere()
Blank constructor.
Definition: sphere.cpp:58
This file contains the AxisAlignedBox class for representing AABB's of objects in the world...
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
The interface for serialization.
bool SetValue(const Char8 *rhs)
Set the value of this.
Boole operator<=(const Sphere &Other) const
Less-than or equals-to operator.
Definition: sphere.cpp:175
This file contains a generic Sphere class for math and spacial query.
void operator=(const Sphere &Other)
Assignment operator.
Definition: sphere.cpp:160
Boole IsInside(const Vector3 &ToCheck) const
Checks to see if a point is inside this sphere.
Definition: sphere.cpp:85
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
bool Empty() const
Is this storing anything at all?
Boole operator==(const Sphere &Other) const
Equality operator.
Definition: sphere.cpp:178
Ogre::Vector3 GetOgreVector3() const
Gets a Ogre vector3.
Definition: vector3.cpp:572
RayTestResult Intersects(const Ray &ToCheck) const
Checks to see if a ray intersects this sphere.
Definition: sphere.cpp:97
Real AsReal(Real def=0) const
Attempts to convert the value of the attribute to a Real and returns the results. ...
Boole IsOverlapping(const Sphere &ToCheck) const
Checks to see if another sphere overlaps with this one.
Definition: sphere.cpp:88
void ExtractOgreSphere(const Ogre::Sphere &InternalSphere)
Changes this Sphere to match the Ogre Sphere.
Definition: sphere.cpp:103
Boole operator<(const Sphere &Other) const
Greater-than or equals-to operator.
Definition: sphere.cpp:169
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
Vector3 Center
The point in world space that is the center of the sphere.
Definition: sphere.h:74
Boole operator>(const Sphere &Other) const
Greater-than operator.
Definition: sphere.cpp:166
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
const Char8 * Name() const
ptrdiff_tGet the name of this Node.
std::pair< Boole, Ray > RayTestResult
This is a type used for the return of a ray intersection test.
Definition: sphere.h:68
Boole operator!=(const Sphere &Other) const
Inequality operator.
Definition: sphere.cpp:181
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
void ProtoSerialize(XML::Node &CurrentRoot) const
Convert this class to an XML::Node ready for serialization.
Definition: vector3.cpp:588
This represents a line placed in 3D space and is used with spacial queries.
Definition: ray.h:67
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.
This is a utility class used to represent the Axis-Aligned Bounding Boxes of objects in various subsy...