MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
plane.cpp
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 _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 "mathtool.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::Fabs( 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  Boolean Plane::IsOverlapping(const Sphere& ToCheck) const
145  { return MathTools::Overlap(*this,ToCheck); }
146 
147  Boolean Plane::IsOverlapping(const AxisAlignedBox& ToCheck) const
148  { return MathTools::Overlap(ToCheck,*this); }
149 
150  Boolean Plane::IsOverlapping(const Plane& ToCheck) const
151  { return MathTools::Overlap(*this,ToCheck); }
152 
154  { return MathTools::Intersects(*this,ToCheck); }
155 
156  ///////////////////////////////////////////////////////////////////////////////
157  // Conversion Methods
158 
159  Ogre::Plane Plane::GetOgrePlane() const
160  {
161  Ogre::Plane Ret;
162  Ret.normal = this->Normal.GetOgreVector3();
163  Ret.d = this->Distance;
164  return Ret;
165  }
166 
167  void Plane::ExtractOgrePlane(const Ogre::Plane& InternalPlane)
168  {
169  this->Normal = InternalPlane.normal;
170  this->Distance = InternalPlane.d;
171  }
172 
173  ///////////////////////////////////////////////////////////////////////////////
174  // Serialization
175 
176  void Plane::ProtoSerialize(XML::Node& ParentNode) const
177  {
178  XML::Node SelfRoot = ParentNode.AppendChild( Plane::GetSerializableName() );
179 
180  if( SelfRoot.AppendAttribute("Version").SetValue("1") &&
181  SelfRoot.AppendAttribute("Distance").SetValue( this->Distance ) )
182  {
183  XML::Node CenterNode = SelfRoot.AppendChild("Normal");
184  this->Normal.ProtoSerialize( CenterNode );
185 
186  return;
187  }else{
188  SerializeError("Create XML Attribute Values",Plane::GetSerializableName(),true);
189  }
190  }
191 
192  void Plane::ProtoDeSerialize(const XML::Node& SelfRoot)
193  {
194  XML::Attribute CurrAttrib;
195 
196  if( String(SelfRoot.Name()) == Plane::GetSerializableName() ) {
197  if(SelfRoot.GetAttribute("Version").AsInt() == 1) {
198  CurrAttrib = SelfRoot.GetAttribute("Distance");
199  if( !CurrAttrib.Empty() )
200  this->Distance = CurrAttrib.AsReal();
201 
202  // Get the properties that need their own nodes
203  XML::Node NormalNode = SelfRoot.GetChild("Normal").GetFirstChild();
204  if( !NormalNode.Empty() )
205  this->Normal.ProtoDeSerialize(NormalNode);
206  }else{
207  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + Plane::GetSerializableName() + ": Not Version 1.");
208  }
209  }else{
210  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,Plane::GetSerializableName() + " was not found in the provided XML node, which was expected.");
211  }
212  }
213 
215  {
216  return "Plane";
217  }
218 
219  ///////////////////////////////////////////////////////////////////////////////
220  // Operators
221 
222  void Plane::operator=(const Plane& Other)
223  { this->Normal = Other.Normal; this->Distance = Other.Distance; }
224 
225  void Plane::operator=(const Ogre::Plane& InternalPlane)
226  { this->ExtractOgrePlane(InternalPlane); }
227 
228  Boolean Plane::operator==(const Plane& Other) const
229  { return ( this->Normal == Other.Normal && this->Distance == Other.Distance ); }
230 
231  Boolean Plane::operator!=(const Plane& Other) const
232  { return ( this->Normal != Other.Normal || this->Distance != Other.Distance ); }
233 }
234 
235 std::ostream& operator << (std::ostream& stream, const Mezzanine::Plane& x)
236 {
237  stream << "<Plane Version=\"1\" Distance=\"" << x.Distance << "\" >" << x.Normal << "</Plane>";
238  return stream;
239 }
240 
241 
242 std::istream& MEZZ_LIB operator >> (std::istream& stream, Mezzanine::Plane& x)
243 {
246 
247  Doc->GetFirstChild() >> x;
248 
249  return stream;
250 }
251 
252 void MEZZ_LIB operator >> (const Mezzanine::XML::Node& OneNode, Mezzanine::Plane& x)
253 {
254  x.ProtoDeSerialize(OneNode);
255 }
256 
257 #endif