Spinning Topp Logo BlackTopp Studios
inc
plane.cpp
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 _plane_cpp
41 #define _plane_cpp
42 
43 #include <iostream>
44 #include <memory>
45 
46 #include "exception.h"
47 #include "plane.h"
48 #include "ray.h"
49 #include "axisalignedbox.h"
50 #include "sphere.h"
51 #include "stringtool.h"
52 #include "MathTools/mathtools.h"
53 #ifndef SWIG
54  #include "XML/xml.h"
55 #endif
56 #include "serialization.h"
57 
58 #include "Ogre.h"
59 
60 namespace Mezzanine
61 {
62  ///////////////////////////////////////////////////////////////////////////////
63  // Construction and Destruction
64 
66  Normal(0,0,0),
67  Distance(0)
68  { }
69 
70  Plane::Plane(const Plane& Other) :
71  Normal(Other.Normal),
72  Distance(Other.Distance)
73  { }
74 
75  Plane::Plane(const Vector3& Norm, const Real Constant) :
76  Normal(Norm),
77  Distance(-Constant)
78  { }
79 
80  Plane::Plane(const Vector3& Norm, const Vector3& Point)
81  { this->Define(Norm,Point); }
82 
83  Plane::Plane(const Vector3& First, const Vector3& Second, const Vector3& Third)
84  { this->Define(First,Second,Third); }
85 
86  Plane::Plane(const Ogre::Plane& InternalPlane)
87  { this->ExtractOgrePlane(InternalPlane); }
88 
90  { }
91 
92  ///////////////////////////////////////////////////////////////////////////////
93  // Utility
94 
95  void Plane::Define(const Vector3& Norm, const Real Constant)
96  {
97  this->Normal = Norm;
98  this->Distance = -Constant;
99  }
100 
101  void Plane::Define(const Vector3& Norm, const Vector3& Point)
102  {
103  this->Normal = Norm;
104  this->Distance = -(Norm.DotProduct(Point));
105  }
106 
107  void Plane::Define(const Vector3& First, const Vector3& Second, const Vector3& Third)
108  {
109  Vector3 Edge1 = Second - First;
110  Vector3 Edge2 = Third - First;
111  this->Normal = Edge1.CrossProduct(Edge2);
112  this->Normal.Normalize();
113  this->Distance = -(this->Normal.DotProduct(First));
114  }
115 
116  Plane::Side Plane::GetSide(const Vector3& Point) const
117  {
118  Real Dist = this->GetDistance(Point);
119  if( Dist < 0.0 ) {
120  return Plane::S_Negative;
121  }else if( Dist > 0.0 ) {
122  return Plane::S_Positive;
123  }else{
124  return Plane::S_None;
125  }
126  }
127 
128  Plane::Side Plane::GetSide(const Vector3& Center, const Vector3& HalfSize) const
129  {
130  Real CenterDist = this->GetDistance(Center);
131  Real MaxDist = MathTools::Abs( this->Normal.DotProduct(HalfSize) );
132  if( CenterDist < -MaxDist ) {
133  return Plane::S_Negative;
134  }else if( CenterDist > +MaxDist ) {
135  return Plane::S_Positive;
136  }else{
137  return Plane::S_Both;
138  }
139  }
140 
141  Real Plane::GetDistance(const Vector3& Point) const
142  { return ( this->Normal.DotProduct(Point) + this->Distance ); }
143 
144  Ray Plane::GetOverlap(const Plane& Other) const
145  {
146  //TODO : handle the case where the plane is perpendicular to T
147  Vector3 point1;
148  Vector3 direction = this->Normal.CrossProduct( Other.Normal );
149  if( direction.SquaredLength() < 1e-08 )
150  return Ray();
151 
152  Real cp = this->Normal.X * Other.Normal.Y - Other.Normal.X * this->Normal.Y;
153  if( cp != 0 ) {
154  Real denom = 1.0 / cp;
155  point1.X = ( this->Normal.Y * Other.Distance - Other.Normal.Y * this->Distance ) * denom;
156  point1.Y = ( Other.Normal.X * this->Distance - this->Normal.X * Other.Distance ) * denom;
157  point1.Z = 0;
158  }else if( ( cp = this->Normal.Y * Other.Normal.Z - Other.Normal.Y * this->Normal.Z ) != 0 ) {
159  //special case #1
160  Real denom = 1.0 / cp;
161  point1.X = 0.0;
162  point1.Y = ( this->Normal.Z * Other.Distance - Other.Normal.Z * this->Distance ) * denom;
163  point1.Z = ( Other.Normal.Y * this->Distance - this->Normal.Y * Other.Distance ) * denom;
164  }else if( ( cp = this->Normal.X * Other.Normal.Z - Other.Normal.X * this->Normal.Z ) != 0 ) {
165  //special case #2
166  Real denom = 1.0 / cp;
167  point1.X = ( this->Normal.Z * Other.Distance - Other.Normal.Z * this->Distance ) * denom;
168  point1.Y = 0.0;
169  point1.Z = ( Other.Normal.X * this->Distance - this->Normal.X * Other.Distance ) * denom;
170  }
171 
172  return Ray(point1,direction);
173  }
174 
175  Boole Plane::IsOverlapping(const Sphere& ToCheck) const
176  { return MathTools::Overlap(*this,ToCheck); }
177 
179  { return MathTools::Overlap(ToCheck,*this); }
180 
181  Boole Plane::IsOverlapping(const Plane& ToCheck) const
182  { return MathTools::Overlap(*this,ToCheck); }
183 
185  { return MathTools::Intersects(*this,ToCheck); }
186 
187  ///////////////////////////////////////////////////////////////////////////////
188  // Conversion Methods
189 
190  Ogre::Plane Plane::GetOgrePlane() const
191  {
192  Ogre::Plane Ret;
193  Ret.normal = this->Normal.GetOgreVector3();
194  Ret.d = this->Distance;
195  return Ret;
196  }
197 
198  void Plane::ExtractOgrePlane(const Ogre::Plane& InternalPlane)
199  {
200  this->Normal = InternalPlane.normal;
201  this->Distance = InternalPlane.d;
202  }
203 
204  ///////////////////////////////////////////////////////////////////////////////
205  // Serialization
206 
207  void Plane::ProtoSerialize(XML::Node& ParentNode) const
208  {
209  XML::Node SelfRoot = ParentNode.AppendChild( Plane::GetSerializableName() );
210 
211  if( SelfRoot.AppendAttribute("Version").SetValue("1") &&
212  SelfRoot.AppendAttribute("Distance").SetValue( this->Distance ) )
213  {
214  XML::Node CenterNode = SelfRoot.AppendChild("Normal");
215  this->Normal.ProtoSerialize( CenterNode );
216 
217  return;
218  }else{
219  SerializeError("Create XML Attribute Values",Plane::GetSerializableName(),true);
220  }
221  }
222 
223  void Plane::ProtoDeSerialize(const XML::Node& SelfRoot)
224  {
225  XML::Attribute CurrAttrib;
226 
227  if( String(SelfRoot.Name()) == Plane::GetSerializableName() ) {
228  if(SelfRoot.GetAttribute("Version").AsInt() == 1) {
229  CurrAttrib = SelfRoot.GetAttribute("Distance");
230  if( !CurrAttrib.Empty() )
231  this->Distance = CurrAttrib.AsReal();
232 
233  // Get the properties that need their own nodes
234  XML::Node NormalNode = SelfRoot.GetChild("Normal").GetFirstChild();
235  if( !NormalNode.Empty() )
236  this->Normal.ProtoDeSerialize(NormalNode);
237  }else{
238  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + Plane::GetSerializableName() + ": Not Version 1.");
239  }
240  }else{
241  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,Plane::GetSerializableName() + " was not found in the provided XML node, which was expected.");
242  }
243  }
244 
246  {
247  return "Plane";
248  }
249 
250  ///////////////////////////////////////////////////////////////////////////////
251  // Operators
252 
253  void Plane::operator=(const Plane& Other)
254  { this->Normal = Other.Normal; this->Distance = Other.Distance; }
255 
256  void Plane::operator=(const Ogre::Plane& InternalPlane)
257  { this->ExtractOgrePlane(InternalPlane); }
258 
259  Boole Plane::operator==(const Plane& Other) const
260  { return ( this->Normal == Other.Normal && this->Distance == Other.Distance ); }
261 
262  Boole Plane::operator!=(const Plane& Other) const
263  { return ( this->Normal != Other.Normal || this->Distance != Other.Distance ); }
264 }
265 
266 std::ostream& operator << (std::ostream& stream, const Mezzanine::Plane& x)
267 {
268  stream << "<Plane Version=\"1\" Distance=\"" << x.Distance << "\" >" << x.Normal << "</Plane>";
269  return stream;
270 }
271 
272 
273 std::istream& MEZZ_LIB operator >> (std::istream& stream, Mezzanine::Plane& x)
274 {
277 
278  Doc->GetFirstChild() >> x;
279 
280  return stream;
281 }
282 
284 {
285  x.ProtoDeSerialize(OneNode);
286 }
287 
288 #endif
This is a generic sphere class used for spacial queries.
Definition: sphere.h:62
String GetOneTag(std::istream &stream)
Gets the first tag out of the Stream and returns it as a String.
Definition: xmlstring.cpp:72
std::ostream & operator<<(std::ostream &stream, const Mezzanine::LinearInterpolator< T > &Lint)
Used to Serialize an Mezzanine::LinearInterpolator to a human readable stream.
Definition: interpolator.h:433
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
Side GetSide(const Vector3 &Point) const
Gets which side of the plane a point in 3D space is.
Definition: plane.cpp:116
Vector3 CrossProduct(const Vector3 &Vec) const
This is used to calculate the crossproduct of this and another vector.
Definition: vector3.cpp:338
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
Real X
Coordinate on the X vector.
Definition: vector3.h:85
Real Z
Coordinate on the Z vector.
Definition: vector3.h:89
Real GetDistance(const Vector3 &Point) const
Gets the distance from the plane to a point in 3D space.
Definition: plane.cpp:141
Document * PreParseClassFromSingleTag(const String &NameSpace, const String &ClassName, const String &OneTag)
Perform a basic series of checks for extracting meaning from a single xml tag.
Definition: xmlstring.cpp:116
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
static String GetSerializableName()
Get the name of the the XML tag this class will leave behind as its instances are serialized...
Definition: plane.cpp:245
A simple reference counting pointer.
Definition: countedptr.h:70
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
Ogre::Plane GetOgrePlane() const
Gets an Ogre::Plane that contains this Planes information.
Definition: plane.cpp:190
Ray GetOverlap(const Plane &Other) const
Gets the overlap of two Planes expressed as a Ray.
Definition: plane.cpp:144
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.
void ProtoSerialize(XML::Node &ParentNode) const
Convert this class to an XML::Node ready for serialization.
Definition: plane.cpp:207
Real Distance
How from from the origin the plane is.
Definition: plane.h:87
Real Distance(const Vector3 &OtherVec) const
Gets the distance between this and another vector.
Definition: vector3.cpp:449
This file contains the AxisAlignedBox class for representing AABB's of objects in the world...
void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
Definition: plane.cpp:223
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
The interface for serialization.
void Define(const Vector3 &Norm, const Real Constant)
Defines the dimensions of this plane explicitly.
Definition: plane.cpp:95
bool SetValue(const Char8 *rhs)
Set the value of this.
This file contains a generic Sphere class for math and spacial query.
void operator=(const Plane &Other)
Assignment operator.
Definition: plane.cpp:253
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?
Vector3 & Normalize()
This will change this point into it's own normal relative to the origin.
Definition: vector3.cpp:352
Ogre::Vector3 GetOgreVector3() const
Gets a Ogre vector3.
Definition: vector3.cpp:572
~Plane()
Class destructor.
Definition: plane.cpp:89
Real AsReal(Real def=0) const
Attempts to convert the value of the attribute to a Real and returns the results. ...
Boole operator==(const Plane &Other) const
Equality operator.
Definition: plane.cpp:259
Real Y
Coordinate on the Y vector.
Definition: vector3.h:87
Vector3 Normal
The rotation of the plane.
Definition: plane.h:85
Plane()
Default constructor.
Definition: plane.cpp:65
Real DotProduct(const Vector3 &Vec) const
This is used to calculate the dotproduct of this and another vector.
Definition: vector3.cpp:347
void ProtoDeSerialize(const XML::Node &OneNode)
Take the data stored in an XML and overwrite this instance of this object with it.
Definition: vector3.cpp:614
Boole operator!=(const Plane &Other) const
Inequality operator.
Definition: plane.cpp:262
Real SquaredLength() const
Gets the length of this vector squared.
Definition: vector3.cpp:464
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
Boole IsOverlapping(const Sphere &ToCheck) const
Checks to see if a sphere overlaps with this Plane.
Definition: plane.cpp:175
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
RayTestResult Intersects(const Ray &ToCheck) const
Checks to see if a ray intersects this Plane.
Definition: plane.cpp:184
void ExtractOgrePlane(const Ogre::Plane &InternalPlane)
Changes this Plane to match the Ogre Plane.
Definition: plane.cpp:198
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
std::istream & operator>>(std::istream &stream, Mezzanine::LinearInterpolator< T > &Lint)
Used to de-serialize an Mezzanine::LinearInterpolator from a stream.
Definition: interpolator.h:448
std::pair< Boole, Vector3 > RayTestResult
This is a type used for the return of a ray intersection test.
Definition: plane.h:79
const Char8 * Name() const
ptrdiff_tGet the name of this Node.
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.
Side
An enum used to describe which side of the plane the result of a query is on.
Definition: plane.h:69
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...