Spinning Topp Logo BlackTopp Studios
inc
ray.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 _ray_cpp
41 #define _ray_cpp
42 
43 #include "ray.h"
44 #include "MathTools/mathtools.h"
45 #include "axisalignedbox.h"
46 #include "plane.h"
47 #include "sphere.h"
48 #include "exception.h"
49 #include "serialization.h"
50 
51 #include <Ogre.h>
52 
53 #include <ostream>
54 
55 namespace Mezzanine
56 {
58  Origin(0,0,0),
59  Normal(0,1,0)
60  { }
61 
62  Ray::Ray(const Ray& Other) :
63  Origin(Other.Origin),
64  Normal(Other.Normal)
65  { }
66 
67  Ray::Ray(const Vector3& Dir) :
68  Normal(Dir)
69  { this->Normal.Normalize(); }
70 
71  Ray::Ray(const Vector3& From, const Vector3& Dir) :
72  Origin(From),
73  Normal(Dir)
74  { this->Normal.Normalize(); }
75 
76  Ray::Ray(const Ogre::Ray& InternalRay)
77  { this->ExtractOgreRay(InternalRay); }
78 
80  { }
81 
82  ///////////////////////////////////////////////////////////////////////////////
83  // Utility
84 
85  const Vector3& Ray::GetNormal() const
86  { return this->Normal; }
87 
88  void Ray::SetNormal(const Vector3& FreshNormal)
89  { this->Normal = FreshNormal.GetNormal(); }
90 
91  const Vector3& Ray::GetOrigin() const
92  { return this->Origin; }
93 
94  void Ray::SetOrigin(const Vector3& FreshOrigin)
95  { this->Origin = FreshOrigin; }
96 
98  { return ( this->Origin + ( this->GetNormal() * Distance ) ); }
99 
101  { return MathTools::Intersects(ToCheck,*this); }
102 
104  { return MathTools::Intersects(ToCheck,*this); }
105 
107  { return MathTools::Intersects(ToCheck,*this); }
108 
109  ///////////////////////////////////////////////////////////////////////////////
110  // Conversion Methods
111 
112  void Ray::ExtractOgreRay(const Ogre::Ray& InternalRay)
113  { this->Origin = InternalRay.getOrigin(); this->Normal = InternalRay.getDirection(); }
114 
115  Ogre::Ray Ray::GetOgreRay() const
116  { return Ogre::Ray(this->Origin.GetOgreVector3(),this->GetNormal().GetOgreVector3()); }
117 
118  ///////////////////////////////////////////////////////////////////////////////
119  // Serialization
120 
121  void Ray::ProtoSerialize(XML::Node& ParentNode) const
122  {
123  XML::Node SelfRoot = ParentNode.AppendChild( Ray::GetSerializableName() );
124 
125  if( SelfRoot.AppendAttribute("Version").SetValue("1") )
126  {
127  XML::Node OriginNode = SelfRoot.AppendChild("Origin");
128  this->Origin.ProtoSerialize( OriginNode );
129 
130  XML::Node NormalNode = SelfRoot.AppendChild("Normal");
131  this->Normal.ProtoSerialize( NormalNode );
132 
133  return;
134  }else{
135  SerializeError("Create XML Attribute Values",Ray::GetSerializableName(),true);
136  }
137  }
138 
139  void Ray::ProtoDeSerialize(const XML::Node& SelfRoot)
140  {
141  if( String(SelfRoot.Name()) == Ray::GetSerializableName() ) {
142  if(SelfRoot.GetAttribute("Version").AsInt() == 1) {
143  // Get the properties that need their own nodes
144  XML::Node OriginNode = SelfRoot.GetChild("Origin").GetFirstChild();
145  if( !OriginNode.Empty() )
146  this->Origin.ProtoDeSerialize(OriginNode);
147 
148  XML::Node NormalNode = SelfRoot.GetChild("Normal").GetFirstChild();
149  if( !NormalNode.Empty() )
150  this->Normal.ProtoDeSerialize(NormalNode);
151  }else{
152  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + Ray::GetSerializableName() + ": Not Version 1.");
153  }
154  }else{
155  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,Ray::GetSerializableName() + " was not found in the provided XML node, which was expected.");
156  }
157  }
158 
160  {
161  return "Ray";
162  }
163 
164  ///////////////////////////////////////////////////////////////////////////////
165  // Operators
166 
167  void Ray::operator=(const Ray& Other)
168  { this->Origin = Other.Origin; this->Normal = Other.Normal; }
169 
170  Boole Ray::operator==(const Ray& Other) const
171  { return ( this->Origin == Other.Origin && this->Normal == Other.Normal ); }
172 
173  Boole Ray::operator!=(const Ray& Other) const
174  { return ( this->Origin != Other.Origin || this->Normal != Other.Normal ); }
175 }
176 
177 std::ostream& operator << (std::ostream& stream, const Mezzanine::Ray& x)
178 {
179  stream << "[" << x.GetOrigin() << "," << x.GetNormal() << "]";
180  return stream;
181 }
182 
183 #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
~Ray()
Class destructor.
Definition: ray.cpp:79
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
Vector3 GetPointAtDistance(const Real &Distance)
Gets a point on the ray at a distance.
Definition: ray.cpp:97
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
Vector3 GetNormal() const
This returns the normal for this relative to the origin.
Definition: vector3.cpp:363
void operator=(const Ray &Other)
Assignment operator.
Definition: ray.cpp:167
void SetOrigin(const Vector3 &FreshOrigin)
Sets the origin of this ray.
Definition: ray.cpp:94
Thrown when the requested identity could not be found.
Definition: exception.h:94
void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
Definition: ray.cpp:139
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
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
Vector3 Origin
The origin point of the Vector.
Definition: ray.h:81
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.
Vector3 Normal
The direction this ray is pointing.
Definition: ray.h:83
static String GetSerializableName()
Get the name of the the XML tag this class will leave behind as its instances are serialized...
Definition: ray.cpp:159
This file contains the AxisAlignedBox class for representing AABB's of objects in the world...
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
The interface for serialization.
bool SetValue(const Char8 *rhs)
Set the value of this.
MathTools::GeometryRayTestResult GeometryRayTestResult
This is a type used for the return of a ray intersection test.
Definition: ray.h:76
This file contains a generic Sphere class for math and spacial query.
void ExtractOgreRay(const Ogre::Ray &InternalRay)
Changes this Ray to match the Ogre Ray.
Definition: ray.cpp:112
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
void ProtoSerialize(XML::Node &ParentNode) const
Convert this class to an XML::Node ready for serialization.
Definition: ray.cpp:121
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
const Vector3 & GetOrigin() const
Gets the origin of this ray.
Definition: ray.cpp:91
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
PlaneRayTestResult Intersects(const Plane &ToCheck) const
Checks to see if this ray intersects a plane.
Definition: ray.cpp:100
Boole operator!=(const Ray &Other) const
Inequality operator.
Definition: ray.cpp:173
Boole operator==(const Ray &Other) const
Equality operator.
Definition: ray.cpp:170
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
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
Ogre::Ray GetOgreRay() const
Gets an Ogre::Ray that contains this Rays information.
Definition: ray.cpp:115
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
const Vector3 & GetNormal() const
Gets the normal of this Ray.
Definition: ray.cpp:85
const Char8 * Name() const
ptrdiff_tGet the name of this Node.
Ray()
Default constructor.
Definition: ray.cpp:57
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.
void SetNormal(const Vector3 &FreshNormal)
Sets the normal of this Ray.
Definition: ray.cpp:88
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...