Spinning Topp Logo BlackTopp Studios
inc
ray.h
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 _ray_h
41 #define _ray_h
42 
43 #include "crossplatformexport.h"
44 #include "vector3.h"
45 #ifndef SWIG
46  #include "MathTools/mathtools.h"
47 #endif
48 
49 namespace Ogre
50 {
51  class Ray;
52 }
53 
54 
55 namespace Mezzanine
56 {
57  class AxisAlignedBox;
58  class Plane;
59  class Sphere;
60  ///////////////////////////////////////////////////////////////////////////////
61  /// @brief This represents a line placed in 3D space and is used with spacial queries.
62  /// @details This is made of Two Vector3 instances. The Origin of a Ray can be thought
63  /// of as its starting location. The Normal of the Ray will be another point defined
64  /// as a Vector3 that is always 1 unit away, as mathematical precision allows. This
65  /// normal is used to indicate direction and from the two points any other points on
66  /// the Ray can be calculated.
67  class MEZZ_LIB Ray
68  {
69  public:
70  /// @brief This is a type used for the ray intersection tests performed on Planes.
71  /// @details This type provides more verbose return data that can be used for further tests.
72  typedef std::pair<Boole,Vector3> PlaneRayTestResult;
73  /// @brief This is a type used for the return of a ray intersection test.
74  /// @details This type provides more verbose return data that can be used for further tests.
75  //typedef MathTools::PointPointInterection GeometryRayTestResult;
76  typedef MathTools::GeometryRayTestResult GeometryRayTestResult;
77  ///////////////////////////////////////////////////////////////////////////////
78  // Public Data Members
79 
80  /// @brief The origin point of the Vector.
82  /// @brief The direction this ray is pointing.
84 
85  ///////////////////////////////////////////////////////////////////////////////
86  // Construction and Destruction
87 
88  /// @brief Default constructor.
89  /// @details This create a ray starting at 0,0,0 pointing to 0,1,0.
90  Ray();
91  /// @brief Copy constructor.
92  /// @param Other The other Ray to copy from.
93  Ray(const Ray& Other);
94  /// @brief Destination constructor.
95  /// @details This keeps the origin at 0,0,0.
96  /// @param Dir The direction this Ray is pointing in.
97  Ray(const Vector3& Dir);
98  /// @brief Descriptive constructor.
99  /// @param From The origin for the new Ray.
100  /// @param Dir The direction this Ray is pointing in. This will be normalizedprior to use.
101  Ray(const Vector3& From, const Vector3& Dir);
102  /// @brief Internal constructor.
103  /// @param InternalRay This is the Ogre::Ray to copy from.
104  explicit Ray(const Ogre::Ray& InternalRay);
105  /// @brief Class destructor.
106  ~Ray();
107 
108  ///////////////////////////////////////////////////////////////////////////////
109  // Utility
110 
111  /// @brief Gets the normal of this Ray.
112  /// @return Returns a Vector3 that is the direction this ray is pointing.
113  const Vector3& GetNormal() const;
114  /// @brief Sets the normal of this Ray.
115  /// @param FreshNormal A Vector3 that will be normalized and is the direction this Ray is pointing.
116  void SetNormal(const Vector3& FreshNormal);
117 
118  /// @brief Gets the origin of this ray.
119  /// @return Returns a Vector3 containing the origin of this Ray.
120  const Vector3& GetOrigin() const;
121  /// @brief Sets the origin of this ray.
122  /// @param FreshOrigin A Vector3 that will become the origin of this Ray.
123  void SetOrigin(const Vector3& FreshOrigin);
124 
125  /// @brief Gets a point on the ray at a distance.
126  /// @param Distance The distance from the origin to get the position of.
127  /// @return Returns a point in 3D space that is on the ray at the specified distance from the origin.
128  Vector3 GetPointAtDistance(const Real& Distance);
129 
130 
131  /// @brief Checks to see if this ray intersects a plane.
132  /// @param ToCheck The plane to check for a hit.
133  /// @return Returns a std::pair containing whether or not the ray hit, and if it did the point in 3D space where it hit.
134  PlaneRayTestResult Intersects(const Plane& ToCheck) const;
135  /// @brief Checks to see if this ray intersects a sphere.
136  /// @param ToCheck The sphere to check for a hit.
137  /// @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.
138  GeometryRayTestResult Intersects(const Sphere& ToCheck) const;
139  /// @brief Checks to see if this ray intersects an AABB.
140  /// @param ToCheck The AABB to check for a hit.
141  /// @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.
142  GeometryRayTestResult Intersects(const AxisAlignedBox& ToCheck) const;
143 
144  ///////////////////////////////////////////////////////////////////////////////
145  // Conversion Methods
146 
147  /// @brief Changes this Ray to match the Ogre Ray.
148  /// @param InternalRay The Ogre::Ray to copy.
149  void ExtractOgreRay(const Ogre::Ray& InternalRay);
150  /// @brief Gets an Ogre::Ray that contains this Rays information.
151  /// @return This returns an Ogre::Ray that contains the same information as this Rays information.
152  Ogre::Ray GetOgreRay() const;
153 
154  ///////////////////////////////////////////////////////////////////////////////
155  // Serialization
156 
157  /// @brief Convert this class to an XML::Node ready for serialization.
158  /// @param ParentNode The point in the XML hierarchy that all this ray should be appended to.
159  void ProtoSerialize(XML::Node& ParentNode) const;
160  /// @brief Take the data stored in an XML Node and overwrite this object with it.
161  /// @param SelfRoot An XML::Node containing the data to populate this class with.
162  void ProtoDeSerialize(const XML::Node& SelfRoot);
163 
164  /// @brief Get the name of the the XML tag this class will leave behind as its instances are serialized.
165  /// @return A string containing the name of this class.
166  static String GetSerializableName();
167 
168  ///////////////////////////////////////////////////////////////////////////////
169  // Operators
170 
171  /// @brief Assignment operator.
172  /// @param Other The other Ray to copy from.
173  void operator=(const Ray& Other);
174 
175  /// @brief Equality operator.
176  /// @param Other The other Ray to compare with.
177  /// @return Returns true if this Ray is the same as the other provided Ray, false otherwise.
178  Boole operator==(const Ray& Other) const;
179  /// @brief Inequality operator.
180  /// @param Other The other Ray to compare with.
181  /// @return Returns true if this Ray is not the same as the other provided Ray, false otherwise.
182  Boole operator!=(const Ray& Other) const;
183  };//Ray
184 }//Mezzanine
185 
186 #ifndef SWIG
187 /// @brief Streaming output operator
188 /// @details This converts the data of the Ray into a stream Ideal for sending to a log or cout
189 /// @param stream This is the stream we send our data to.
190 /// @return This returns an std::ostream which now contains our data.
191 std::ostream& MEZZ_LIB operator << (std::ostream& stream, const Mezzanine::Ray& x);
192 #endif
193 
194 #endif
This is a generic sphere class used for spacial queries.
Definition: sphere.h:62
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
std::pair< Boole, Vector3 > PlaneRayTestResult
This is a type used for the ray intersection tests performed on Planes.
Definition: ray.h:72
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
This file is used on some platforms to determine what data should be read and written to and from a s...
Vector3 Origin
The origin point of the Vector.
Definition: ray.h:81
This is used to represent a flat infinite slice of the game world.
Definition: plane.h:65
Vector3 Normal
The direction this ray is pointing.
Definition: ray.h:83
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
MathTools::GeometryRayTestResult GeometryRayTestResult
This is a type used for the return of a ray intersection test.
Definition: ray.h:76
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
This represents a line placed in 3D space and is used with spacial queries.
Definition: ray.h:67
This is a utility class used to represent the Axis-Aligned Bounding Boxes of objects in various subsy...