MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ray.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 _ray_cpp
41 #define _ray_cpp
42 
43 #include "ray.h"
44 #include "mathtool.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  Destination(0,1,0)
60  { }
61 
62  Ray::Ray(const Ray& Other) :
63  Origin(Other.Origin),
64  Destination(Other.Destination)
65  { }
66 
67  Ray::Ray(const Vector3& To) :
68  Destination(To)
69  { }
70 
71  Ray::Ray(const Vector3& From, const Vector3& To) :
72  Origin(From),
73  Destination(To)
74  { }
75 
76  Ray::Ray(const Ogre::Ray& InternalRay)
77  { this->ExtractOgreRay(InternalRay); }
78 
80  { }
81 
82  ///////////////////////////////////////////////////////////////////////////////
83  // Utility
84 
85  Real Ray::Length() const
86  { return this->Origin.Distance(this->Destination); }
87 
89  { return ( this->Destination - this->Origin ).Normalize(); }
90 
92  {
93  Real TempLength = this->Length();
94  if( 0 != TempLength ) {
95  return (*this) / TempLength;
96  }
97  return Ray(*this);
98  }
99 
101  {
102  Real TempLength = this->Length();
103  if( 0 != TempLength ) {
104  return (*this) /= this->Length();
105  }
106  return *this;
107  }
108 
110  { return MathTools::Intersects(ToCheck,*this); }
111 
113  { return MathTools::Intersects(ToCheck,*this); }
114 
116  { return MathTools::Intersects(ToCheck,*this); }
117 
118  ///////////////////////////////////////////////////////////////////////////////
119  // Conversion Methods
120 
121  void Ray::ExtractOgreRay(const Ogre::Ray& InternalRay)
122  { this->Origin = InternalRay.getOrigin(); this->Destination = InternalRay.getPoint(1); }
123 
124  Ogre::Ray Ray::GetOgreRay() const
125  { return Ogre::Ray(this->Origin.GetOgreVector3(),this->GetDirection().GetOgreVector3()); }
126 
127  ///////////////////////////////////////////////////////////////////////////////
128  // Serialization
129 
130  void Ray::ProtoSerialize(XML::Node& ParentNode) const
131  {
132  XML::Node SelfRoot = ParentNode.AppendChild( Ray::GetSerializableName() );
133 
134  if( SelfRoot.AppendAttribute("Version").SetValue("1") )
135  {
136  XML::Node OriginNode = SelfRoot.AppendChild("Origin");
137  this->Origin.ProtoSerialize( OriginNode );
138 
139  XML::Node DestinationNode = SelfRoot.AppendChild("Destination");
140  this->Destination.ProtoSerialize( DestinationNode );
141 
142  return;
143  }else{
144  SerializeError("Create XML Attribute Values",Ray::GetSerializableName(),true);
145  }
146  }
147 
148  void Ray::ProtoDeSerialize(const XML::Node& SelfRoot)
149  {
150  if( String(SelfRoot.Name()) == Ray::GetSerializableName() ) {
151  if(SelfRoot.GetAttribute("Version").AsInt() == 1) {
152  // Get the properties that need their own nodes
153  XML::Node OriginNode = SelfRoot.GetChild("Origin").GetFirstChild();
154  if( !OriginNode.Empty() )
155  this->Origin.ProtoDeSerialize(OriginNode);
156 
157  XML::Node DestinationNode = SelfRoot.GetChild("Destination").GetFirstChild();
158  if( !DestinationNode.Empty() )
159  this->Destination.ProtoDeSerialize(DestinationNode);
160  }else{
161  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + Ray::GetSerializableName() + ": Not Version 1.");
162  }
163  }else{
164  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,Ray::GetSerializableName() + " was not found in the provided XML node, which was expected.");
165  }
166  }
167 
169  {
170  return "Ray";
171  }
172 
173  ///////////////////////////////////////////////////////////////////////////////
174  // Operators
175 
176  void Ray::operator=(const Ray& Other)
177  { this->Origin = Other.Origin; this->Destination = Other.Destination; }
178 
179  Ray Ray::operator*(const Real Factor) const
180  { return Ray( this->Origin, ( ( this->Destination - this->Origin ) * Factor ) + this->Origin ); }
181 
182  Ray Ray::operator/(const Real Factor) const
183  { return Ray( this->Origin, ( ( this->Destination - this->Origin ) / Factor ) + this->Origin ); }
184 
185  Ray& Ray::operator*=(const Real Factor)
186  { this->Destination = ( ( this->Destination - this->Origin ) * Factor ) + this->Origin; return *this; }
187 
188  Ray& Ray::operator/=(const Real Factor)
189  { this->Destination = ( ( this->Destination - this->Origin ) / Factor ) + this->Origin; return *this; }
190 
191  Boolean Ray::operator==(const Ray& Other) const
192  { return ( this->Origin == Other.Origin && this->Destination == Other.Destination ); }
193 
194  Boolean Ray::operator!=(const Ray& Other) const
195  { return ( this->Origin != Other.Origin || this->Destination != Other.Destination ); }
196 }
197 
198 std::ostream& operator << (std::ostream& stream, const Mezzanine::Ray& x)
199 {
200  stream << "[" << x.Origin << "," << x.Destination << "]";
201  return stream;
202 }
203 
204 #endif