MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ray.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 _ray_h
41 #define _ray_h
42 
43 #include "crossplatformexport.h"
44 #include "vector3.h"
45 
46 namespace Ogre
47 {
48  class Ray;
49 }
50 
51 
52 namespace Mezzanine
53 {
54  class AxisAlignedBox;
55  class Plane;
56  class Sphere;
57  ///////////////////////////////////////////////////////////////////////////////
58  /// @brief This represents a line placed in 3D space and is used with spacial queries.
59  /// @details
60  class MEZZ_LIB Ray
61  {
62  public:
63  /// @brief This is a type used for the ray intersection tests performed on Planes.
64  /// @details This type provides more verbose return data that can be used for further tests.
65  typedef std::pair<Boolean,Vector3> PlaneRayTestResult;
66  /// @brief This is a type used for the return of a ray intersection test.
67  /// @details This type provides more verbose return data that can be used for further tests.
68  typedef std::pair<Boolean,Ray> GeometryRayTestResult;
69 
70  ///////////////////////////////////////////////////////////////////////////////
71  // Public Data Members
72 
73  /// @brief The origin point of the Vector.
75  /// @brief The direction of this ray or end point of this ray depending on mode.
77 
78  ///////////////////////////////////////////////////////////////////////////////
79  // Construction and Destruction
80 
81  /// @brief Default constructor.
82  /// @details This create a ray starting at 0,0,0 pointing to 0,1,0.
83  Ray();
84  /// @brief Copy constructor.
85  /// @param Other The other Ray to copy from.
86  Ray(const Ray& Other);
87  /// @brief Destination constructor.
88  /// @details This keeps the origin at 0,0,0.
89  /// @param To The destination vector for the ray.
90  Ray(const Vector3& To);
91  /// @brief Descriptive constructor.
92  /// @param From The origin for the new Ray.
93  /// @param To A point along the line for the destination line.
94  Ray(const Vector3& From, const Vector3& To);
95  /// @brief Internal constructor.
96  /// @param InternalRay This is the Ogre::Ray to copy from.
97  explicit Ray(const Ogre::Ray& InternalRay);
98  /// @brief Class destructor.
99  ~Ray();
100 
101  ///////////////////////////////////////////////////////////////////////////////
102  // Utility
103 
104  /// @brief Measures the distance of this ray.
105  /// @return This returns a real value which contains the distance from the origin to the destination.
106  Real Length() const;
107  /// @brief Gets the normal of this ray.
108  /// @return Returns a vector3 that is the direction from it's origin to it's destination.
109  Vector3 GetDirection() const;
110 
111  /// @brief Gets a copy of this ray with a unit length of 1.
112  /// @return Returns a ray that is a normalized copy of this array.
113  Ray GetNormal() const;
114  /// @brief Reduces the length of this ray to 1 unit.
115  /// @return Returns a reference to this.
116  Ray& Normalize();
117 
118  /// @brief Checks to see if this ray intersects a plane.
119  /// @param ToCheck The plane to check for a hit.
120  /// @return Returns a std::pair containing whether or not the ray hit, and if it did the point in 3D space where it hit.
121  PlaneRayTestResult Intersects(const Plane& ToCheck) const;
122  /// @brief Checks to see if this ray intersects a sphere.
123  /// @param ToCheck The sphere to check for a hit.
124  /// @return Returns a std::pair containing whether or not the ray hit, and if it did the subsection of the ray that went through the sphere.
125  GeometryRayTestResult Intersects(const Sphere& ToCheck) const;
126  /// @brief Checks to see if this ray intersects an AABB.
127  /// @param ToCheck The AABB to check for a hit.
128  /// @return Returns a std::pair containing whether or not the ray hit, and if it did the subsection of the ray that went through the AABB.
129  GeometryRayTestResult Intersects(const AxisAlignedBox& ToCheck) const;
130 
131  ///////////////////////////////////////////////////////////////////////////////
132  // Conversion Methods
133 
134  /// @brief Changes this Ray to match the Ogre Ray.
135  /// @param InternalRay The Ogre::Ray to copy.
136  void ExtractOgreRay(const Ogre::Ray& InternalRay);
137  /// @brief Gets an Ogre::Ray that contains this Rays information.
138  /// @return This returns an Ogre::Ray that contains the same information as this Rays information.
139  Ogre::Ray GetOgreRay() const;
140 
141  ///////////////////////////////////////////////////////////////////////////////
142  // Serialization
143 
144  /// @brief Convert this class to an XML::Node ready for serialization.
145  /// @param ParentNode The point in the XML hierarchy that all this renderable should be appended to.
146  void ProtoSerialize(XML::Node& ParentNode) const;
147  /// @brief Take the data stored in an XML Node and overwrite this object with it.
148  /// @param SelfRoot An XML::Node containing the data to populate this class with.
149  void ProtoDeSerialize(const XML::Node& SelfRoot);
150 
151  /// @brief Get the name of the the XML tag the proxy class will leave behind as its instances are serialized.
152  /// @return A string containing the name of this class.
153  static String GetSerializableName();
154 
155  ///////////////////////////////////////////////////////////////////////////////
156  // Operators
157 
158  /// @brief Assignment operator.
159  /// @param Other The other Ray to copy from.
160  void operator=(const Ray& Other);
161 
162  /// @brief Gets a Ray with a length longer than this one by the specified factor.
163  /// @param Factor That factor by which to increase the length of this Ray.
164  /// @return Returns a new lengthened Ray.
165  Ray operator*(const Real Factor) const;
166  /// @brief Gets a Ray with a length shorter than this one by the specified factor.
167  /// @param Factor That factor by which to decrease the length of this Ray.
168  /// @return Returns a new lengthened Ray.
169  Ray operator/(const Real Factor) const;
170  /// @brief Increases the length of this Ray by the factor provided.
171  /// @param Factor That factor by which to increase the length of this Ray.
172  /// @return Returns a reference to this.
173  Ray& operator*=(const Real Factor);
174  /// @brief Decreases the length of this Ray by the factor provided.
175  /// @param Factor That factor by which to decrease the length of this Ray.
176  /// @return Returns a reference to this.
177  Ray& operator/=(const Real Factor);
178 
179  /// @brief Equality operator.
180  /// @param Other The other Ray to compare with.
181  /// @return Returns true if this Ray is the same as the other provided Ray, false otherwise.
182  Boolean operator==(const Ray& Other) const;
183  /// @brief Inequality operator.
184  /// @param Other The other Ray to compare with.
185  /// @return Returns true if this Ray is not the same as the other provided Ray, false otherwise.
186  Boolean operator!=(const Ray& Other) const;
187  };//Ray
188 }//Mezzanine
189 
190 #ifndef SWIG
191 /// @brief Streaming output operator
192 /// @details This converts the data of the Ray into a stream Ideal for sending to a log or cout
193 /// @param stream This is the stream we send our data to.
194 /// @return This returns an std::ostream which now contains our data.
195 std::ostream& MEZZ_LIB operator << (std::ostream& stream, const Mezzanine::Ray& x);
196 #endif
197 
198 #endif