MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
plane.h
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_h
41 #define _plane_h
42 
43 #include "vector3.h"
44 
45 #ifndef SWIG
46  #include "XML/xml.h"
47 #endif
48 
49 namespace Ogre
50 {
51  class Plane;
52 }
53 
54 namespace Mezzanine
55 {
56  class AxisAlignedBox;
57  class Ray;
58  class Sphere;
59  ///////////////////////////////////////////////////////////////////////////////
60  /// @brief This is used to represent a flat infinite slice of the game world
61  /// @details The Normal value represents how rotated the plane will be, and
62  /// The Distance with represent how far you need to move down a line perpendicular
63  /// to the plane, (ie the normal, which is defined by the Normal value) from the
64  /// Origin.
66  {
67  public:
68  /// @enum Side
69  /// @brief An enum used to describe which side of the plane the result of a query is on.
70  enum Side
71  {
72  S_None = 0,
73  S_Positive = 1,
74  S_Negative = 2,
75  S_Both = 3
76  };
77  /// @brief This is a type used for the return of a ray intersection test.
78  /// @details This type provides more verbose return data that can be used for further tests. @n @n
79  /// The first member stores whether or not there was a hit. The second member stores the point where it was hit.
80  typedef std::pair<Boolean,Vector3> RayTestResult;
81 
82  ///////////////////////////////////////////////////////////////////////////////
83  // Public Data Members
84 
85  /// @brief The rotation of the plane
87  /// @brief How from from the origin the plane is
89 
90  ///////////////////////////////////////////////////////////////////////////////
91  // Construction and Destruction
92 
93  /// @brief Default constructor
94  Plane();
95  /// @brief Copy constructor.
96  /// @param Other The other plane to copy from.
97  Plane(const Plane& Other);
98  /// @brief Descriptive constructor.
99  /// @param Norm The positive direction of the plane.
100  /// @param Constant The Constant with which to project the plane.
101  Plane(const Vector3& Norm, const Real Constant);
102  /// @brief Dual Vector constructor.
103  /// @param Norm The positive direction of the plane.
104  /// @param Point The "origin" point of the plane in 3D space.
105  Plane(const Vector3& Norm, const Vector3& Point);
106  /// @brief Triangle constructor.
107  /// @param First This is one point in the triangle.
108  /// @param Second This is another point in the triangle.
109  /// @param Third This is one point in the triangle.
110  Plane(const Vector3& First, const Vector3& Second, const Vector3& Third);
111  /// @brief Compatibility constructor.
112  /// @param InternalPlane This is the Ogre::Plane to copy from.
113  explicit Plane(const Ogre::Plane& InternalPlane);
114  /// @brief Class destructor.
115  ~Plane();
116 
117  ///////////////////////////////////////////////////////////////////////////////
118  // Utility
119 
120  /// @brief Defines the dimensions of this plane explicitly.
121  /// @param Norm The positive direction of the plane.
122  /// @param Constant The Constant with which to project the plane.
123  void Define(const Vector3& Norm, const Real Constant);
124  /// @brief Defines the dimensions of this plane based on an origin and direction.
125  /// @param Norm The positive direction of the plane.
126  /// @param Point The "origin" point of the plane in 3D space.
127  void Define(const Vector3& Norm, const Vector3& Point);
128  /// @brief Defines the dimensions of this plane from a triangle in 3D space.
129  /// @param First This is one point in the triangle.
130  /// @param Second This is another point in the triangle.
131  /// @param Third This is one point in the triangle.
132  void Define(const Vector3& First, const Vector3& Second, const Vector3& Third);
133 
134  /// @brief Gets which side of the plane a point in 3D space is.
135  /// @param Point The point in 3D space to determine on which side of the plane it lies.
136  /// @return Returns a Side enum value, indicating which side of the plane the point is on.
137  Side GetSide(const Vector3& Point) const;
138  /// @brief Gets which side of the plane a box shape is.
139  /// @param Center The point in 3D space where the center of the box is.
140  /// @param HalfSize Half of the total size on each axis.
141  /// @return Returns a Side enum value indicating which side of the plane the box is on.
142  Side GetSide(const Vector3& Center, const Vector3& HalfSize) const;
143  /// @brief Gets the distance from the plane to a point in 3D space.
144  /// @note This function will only return a true unit distance if the Normal member of this class is properly normalized. Take care when altering it's value.
145  /// @param Point The point in 3D space to get the distance to.
146  /// @return Returns the distance from the plane to the specified point. Positive values mean the point is on the positive side, and vice versa.
147  Real GetDistance(const Vector3& Point) const;
148 
149  /// @brief Checks to see if a sphere overlaps with this Plane.
150  /// @param ToCheck The sphere to check for overlap.
151  /// @return Returns true if the provided sphere overlaps with this Plane, false otherwise.
152  Boolean IsOverlapping(const Sphere& ToCheck) const;
153  /// @brief Checks to see if an AABB overlaps with this Plane.
154  /// @param ToCheck The AABB to check for overlap.
155  /// @return Returns true if the AABB overlaps with this Plane, false otherwise.
156  Boolean IsOverlapping(const AxisAlignedBox& ToCheck) const;
157  /// @brief Checks to see if another Plane intersects this one.
158  /// @param ToCheck The other Plane to check for intersection.
159  /// @return Returns true if the provided Plane intersects with this Plane, false otherwise.
160  Boolean IsOverlapping(const Plane& ToCheck) const;
161  /// @brief Checks to see if a ray intersects this Plane.
162  /// @param ToCheck The ray to check for a hit.
163  /// @return Returns true if the ray intersects this Plane, false otherwise.
164  RayTestResult Intersects(const Ray& ToCheck) const;
165 
166  ///////////////////////////////////////////////////////////////////////////////
167  // Conversion Methods
168 
169  /// @brief Changes this Plane to match the Ogre Plane.
170  /// @param InternalPlane The Ogre::Plane to copy.
171  void ExtractOgrePlane(const Ogre::Plane& InternalPlane);
172  /// @brief Gets an Ogre::Plane that contains this Planes information.
173  /// @return This returns an Ogre::Plane that contains the same information as this Planes information.
174  Ogre::Plane GetOgrePlane() const;
175 
176  ///////////////////////////////////////////////////////////////////////////////
177  // Serialization
178 
179  /// @brief Convert this class to an XML::Node ready for serialization.
180  /// @param ParentNode The point in the XML hierarchy that all this renderable should be appended to.
181  void ProtoSerialize(XML::Node& ParentNode) const;
182  /// @brief Take the data stored in an XML Node and overwrite this object with it.
183  /// @param SelfRoot An XML::Node containing the data to populate this class with.
184  void ProtoDeSerialize(const XML::Node& SelfRoot);
185 
186  /// @brief Get the name of the the XML tag the proxy class will leave behind as its instances are serialized.
187  /// @return A string containing the name of this class.
188  static String GetSerializableName();
189 
190  ///////////////////////////////////////////////////////////////////////////////
191  // Operators
192 
193  /// @brief Assignment operator.
194  /// @param Other The other Plane to copy from.
195  void operator=(const Plane& Other);
196 
197  /// @brief The assignment operator from Ogre::Plane to Mezzanine::Plane.
198  /// @param InternalPlane The Ogre::Plane to take data from.
199  void operator=(const Ogre::Plane& InternalPlane);
200 
201  /// @brief Equality operator.
202  /// @param Other The other Plane to compare with.
203  /// @return Returns true if this Plane is the same as the other provided Plane, false otherwise.
204  Boolean operator==(const Plane& Other) const;
205  /// @brief Inequality operator.
206  /// @param Other The other Plane to compare with.
207  /// @return Returns true if this Plane is not the same as the other provided Plane, false otherwise.
208  Boolean operator!=(const Plane& Other) const;
209  };//Plane
210 }//Mezzanine
211 
212 #ifndef SWIG
213 /// @brief Streaming output operator
214 /// @details This converts the data of the Plane into a snippet of xml Ideal for sending to a log or cout.
215 /// @param stream This is the stream we send our data to.
216 /// @return This returns an std::ostream which now contains our data.
217 std::ostream& MEZZ_LIB operator << (std::ostream& stream, const Mezzanine::Plane& x);
218 
219 /// @brief Used to de-serialize from a stream.
220 /// @details This reads XML input and attempts to Populate a plane.
221 /// @param stream This is the stream we get our data from.
222 /// @param x The Mezzanine::Plane to store the deserialized Plane in.
223 /// @return This returns an std::ostream to allow operator chaining.
224 /// @throw Can throw any exception that any function in the Mezzanine::xml namespace could throw in addition to a Mezzanine::Exception if the serialization version doesn't match.
225 std::istream& MEZZ_LIB operator >> (std::istream& stream, Mezzanine::Plane& x);
226 
227 /// @brief Converts an XML node into a Mezzanine::Plane
228 /// @details TThis will convert an XML::Node will a valid serialized Mezzanine::Vector3 into a Mezzanine::Vector3
229 /// @param OneNode This is the XML::Node we get our data from. It should contain a serialized Mezzanine::Plane.
230 /// @param x The Mezzanine::Plane to store the deserialized Plane in.
231 /// @return This returns an XML::Node refernce to allow operator chaining.
232 /// @throw Can throw any exception that any function in the Mezzanine::xml namespace could throw in addition to a Mezzanine::Exception if the serialization version doesn't match.
233 void MEZZ_LIB operator >> (const Mezzanine::XML::Node& OneNode, Mezzanine::Plane& x);
234 #endif
235 
236 #endif