Spinning Topp Logo BlackTopp Studios
inc
vector2.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 _vector2_cpp
41 #define _vector2_cpp
42 
43 #include "serialization.h"
44 #include "stringtool.h"
45 #include "vector2.h"
46 #include "MathTools/mathtools.h"
47 #include "exception.h"
48 
49 //#include <memory>
50 
51 #include <Ogre.h>
52 
53 namespace Mezzanine
54 {
56  { this->SetIdentity(); }
57 
59  { this->SetValues(xy, xy); }
60 
61  Vector2::Vector2(const Real& x, const Real& y)
62  { this->SetValues(x,y); }
63 
64  Vector2::Vector2(const Ogre::Vector2& Vec)
65  { this->ExtractOgreVector2(Vec); }
66 
67  Ogre::Vector2 Vector2::GetOgreVector2() const
68  {
69  Ogre::Vector2 Theirs;
70  Theirs.x = this->X;
71  Theirs.y = this->Y;
72  return Theirs;
73  }
74 
75  void Vector2::ExtractOgreVector2(const Ogre::Vector2& Ours)
76  {
77  this->X = Ours.x;
78  this->Y = Ours.y;
79  }
80 
81  ///////////////////////////////////////////////////////////////////////////////
82  // Prebuilt Vectors
83 
85  { return Vector2(1,0); }
86 
88  { return Vector2(0,1); }
89 
91  { return Vector2(-1,0); }
92 
94  { return Vector2(0,-1); }
95 
96  ///////////////////////////////////////////////////////////////////////////////
97  // Utility
98 
100  {
101  this->X = 0;
102  this->Y = 0;
103  }
104 
105  void Vector2::SetValues(const Real& x, const Real& y)
106  {
107  this->X = x;
108  this->Y = y;
109  }
110 
112  {
113  return ( this->X == 0.0 && this->Y == 0.0 );
114  }
115 
116  ///////////////////////////////////////////////////////////////////////////////
117  // Equality Comparison operators
118 
120  { return ( Vec2.X == this->X && Vec2.Y == this->Y ); }
121 
123  { return ( Vec2.X != this->X || Vec2.Y != this->Y ); }
124 
125  Boole Vector2::operator==(const Ogre::Vector2& Vec2) const
126  { return ( Vec2.x == this->X && Vec2.y == this->Y ); }
127 
128  Boole Vector2::operator!=(const Ogre::Vector2& Vec2) const
129  { return ( Vec2.x != this->X || Vec2.y != this->Y ); }
130 
132  { return ( this->X <= Vec.X && this->Y <= Vec.Y); }
133 
135  { return ( this->X >= Vec.X && this->Y >= Vec.Y); }
136 
137  ///////////////////////////////////////////////////////////////////////////////
138  // Unary Operators
139 
141  { return Vector2( -(this->X), -(this->Y) ); }
142 
143  ///////////////////////////////////////////////////////////////////////////////
144  // Vector2 Arithmetic with Real
145 
146  Vector2 Vector2::operator* (const Real& scalar) const
147  {
148  return Vector2(
149  this->X * scalar,
150  this->Y * scalar
151  );
152  }
153 
154  Vector2 Vector2::operator/ (const Real& scalar) const
155  {
156  return Vector2(
157  this->X / scalar,
158  this->Y / scalar
159  );
160  }
161 
162  ///////////////////////////////////////////////////////////////////////////////
163  // Vector2 Arithmetic and assignment with Real
164 
166  {
167  this->X *= scalar;
168  this->Y *= scalar;
169  return *this;
170  }
171 
173  {
174  this->X /= scalar;
175  this->Y /= scalar;
176  return *this;
177  }
178 
179  ///////////////////////////////////////////////////////////////////////////////
180  // Arithmetic Operators
181 
183  {
184  Vector2 Temp(X,Y);
185  Temp.X += Vec2.X;
186  Temp.Y += Vec2.Y;
187  return Temp;
188  }
189 
191  {
192  Vector2 Temp(X,Y);
193  Temp.X -= Vec2.X;
194  Temp.Y -= Vec2.Y;
195  return Temp;
196  }
197 
199  {
200  Vector2 Temp(X,Y);
201  Temp.X *= Vec2.X;
202  Temp.Y *= Vec2.Y;
203  return Temp;
204  }
205 
207  {
208  Vector2 Temp(X,Y);
209  Temp.X /= Vec2.X;
210  Temp.Y /= Vec2.Y;
211  return Temp;
212  }
213 
215  {
216  this->X += Vec2.X;
217  this->Y += Vec2.Y;
218  return *this;
219  }
220 
222  {
223  this->X -= Vec2.X;
224  this->Y -= Vec2.Y;
225  return *this;
226  }
227 
229  {
230  this->X *= Vec2.X;
231  this->Y *= Vec2.Y;
232  return *this;
233  }
234 
236  {
237  this->X /= Vec2.X;
238  this->Y /= Vec2.Y;
239  return *this;
240  }
241 
242  ///////////////////////////////////////////////////////////////////////////////
243  // Fancy Math
244 
245  Real Vector2::CrossProduct(const Vector2& Other) const
246  { return ( this->X * Other.Y ) - ( this->Y * Other.X ); }
247 
248  Real Vector2::DotProduct(const Vector2& Other) const
249  { return ( this->X * Other.X ) + ( this->Y * Other.Y ); }
250 
251  Real Vector2::Distance(const Vector2& Other) const
252  { return ( *this - Other ).Length(); }
253 
255  { return ( *this - Other ).SquaredLength(); }
256 
258  { return MathTools::Sqrt( this->SquaredLength() ); }
259 
261  { return ( this->X * this->X + this->Y * this->Y ); }
262 
264  { return Vector2(-Y,X); }
265 
266  Vector2 Vector2::Reflect(const Vector2& Normal) const
267  { return Vector2( *this - ( Normal * ( 2 * this->DotProduct(Normal) ) ) ); }
268 
270  {
271  Real Length = this->Length();
272 
273  if( Length > 1e-08 ) {
274  Real InvLength = 1.0 / Length;
275  X *= InvLength;
276  Y *= InvLength;
277  }
278 
279  return *this;
280  }
281 
283  {
284  Vector2 Ret( *this );
285  return Ret.Normalize();
286  }
287 
288  Real Vector2::AngleTo(const Vector2& Other) const
289  {
290  Real Angle = this->AngleBetween(Other);
291 
292  if( this->CrossProduct(Other) < 0 )
293  Angle = MathTools::GetTwoPi() - Angle;
294 
295  return Angle;
296  }
297 
298  Real Vector2::AngleBetween(const Vector2& Other) const
299  {
300  Real LenProduct = this->Length() * Other.Length();
301  // Divide by zero check
302  if( LenProduct < 1e-6f )
303  LenProduct = 1e-6f;
304 
305  Real f = this->DotProduct(Other) / LenProduct;
306 
307  f = MathTools::Clamp( f, (Real)-1.0, (Real)1.0 );
308  return MathTools::ACos(f);
309  }
310 
311  ///////////////////////////////////////////////////////////////////////////////
312  // Serialization
313 
314  void Vector2::ProtoSerialize(XML::Node& CurrentRoot) const
315  {
316  Mezzanine::XML::Node VecNode = CurrentRoot.AppendChild(GetSerializableName());
317  VecNode.SetName(GetSerializableName());
318 
319  Mezzanine::XML::Attribute VersionAttr = VecNode.AppendAttribute("Version");
320  Mezzanine::XML::Attribute XAttr = VecNode.AppendAttribute("X");
321  Mezzanine::XML::Attribute YAttr = VecNode.AppendAttribute("Y");
322  if( VersionAttr && XAttr && YAttr )
323  {
324  if( VersionAttr.SetValue("1") && XAttr.SetValue(this->X) && YAttr.SetValue(this->Y) )
325  {
326  return;
327  }else{
328  SerializeError("Create XML Attribute Values", GetSerializableName(),true);
329  }
330  }else{
331  SerializeError("Create XML Attributes", GetSerializableName(),true);
332  }
333  }
334 
336  {
338  {
339  if(OneNode.GetAttribute("Version").AsInt() == 1)
340  {
341  this->X=OneNode.GetAttribute("X").AsReal();
342  this->Y=OneNode.GetAttribute("Y").AsReal();
343  }else{
344  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + GetSerializableName() + ": Not Version 1.");
345  }
346  }else{
347  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_INVALID_EXCEPTION,"Attempting to deserialize a " + GetSerializableName() + ", found a " + String(OneNode.Name()) + ".");
348  }
349  }
350 
352  { return String("Vector2"); }
353 
354  ///////////////////////////////////////////////////////////////////////////////
355  // Vector2LengthCompare methods
356 
357  Boole Vector2LengthCompare::operator()(const Vector2& First, const Vector2& Second) const
358  {
359  if( ( First - Second ).SquaredLength() < 1e-6 )
360  return false;
361  if( MathTools::Abs( First.X - Second.X ) > 1e-3 )
362  return ( First.X < Second.X );
363  return ( First.Y < Second.Y );
364  }
365 }//Mezzanine
366 
367 ///////////////////////////////////////////////////////////////////////////////
368 // Class External << Operators for streaming or assignment
369 std::ostream& operator << (std::ostream& stream, const Mezzanine::Vector2& x)
370 {
371 
372  //stream << "<Vector2 Version=\"1\" X=\"" << x.X << "\" Y=\"" << x.Y << "\" />";
373  Serialize(stream,x);
374  return stream;
375 }
376 
377 std::istream& MEZZ_LIB operator >> (std::istream& stream, Mezzanine::Vector2& Vec)
378  { return DeSerialize(stream, Vec); }
379 
380 void operator >> (const Mezzanine::XML::Node& OneNode, Mezzanine::Vector2& Vec)
381  { Vec.ProtoDeSerialize(OneNode); }
382 
383 #endif
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
static Vector2 Unit_Y()
Gets a vector representing the Y unit of a Vector2.
Definition: vector2.cpp:87
Boole IsZero() const
Checks to see if the values of this vector are all zero.
Definition: vector2.cpp:111
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
Vector2 Perpendicular() const
Generates a Vector2 that is perpendicular to this vector.
Definition: vector2.cpp:263
Vector2 operator/(const Real &scalar) const
Scaling by Division.
Definition: vector2.cpp:154
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
Real Distance(const Vector2 &Other) const
Gets the distance between this and another vector.
Definition: vector2.cpp:251
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
std::ostream & Serialize(std::ostream &Stream, const T &Converted, const String &Indent=String(""))
Convert any class that supports serialization or has a serializer to a string of chars in a stream...
Vector2 & operator/=(const Real &scalar)
Scaling by Division.
Definition: vector2.cpp:172
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
void ExtractOgreVector2(const Ogre::Vector2 &Thiers)
Copies an existing Ogre vector2.
Definition: vector2.cpp:75
Ogre::Vector2 GetOgreVector2() const
Gets a Ogre vector2.
Definition: vector2.cpp:67
static String GetSerializableName()
Get the name of the the XML tag this class will leave behind as its instances are serialized...
Definition: vector2.cpp:351
Real AngleTo(const Vector2 &Other) const
Gets an oriented angle between this Vector2 and another Vector2.
Definition: vector2.cpp:288
This implements the exception hiearchy for Mezzanine.
Vector2 & operator-=(const Vector2 &Vec2)
Subraction assignment Operator.
Definition: vector2.cpp:221
Boole operator()(const Vector2 &First, const Vector2 &Second) const
Compares two Vector2's to determine which has the greater length/magnitude.
Definition: vector2.cpp:357
Real DotProduct(const Vector2 &Other) const
This is used to calculate the dotproduct of this and another vector.
Definition: vector2.cpp:248
Boole operator==(const Mezzanine::Vector2 &Vec2) const
Equality Comparison Operator.
Definition: vector2.cpp:119
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
The interface for serialization.
Vector2 Reflect(const Vector2 &Normal) const
Gets a reflection vector to the line with the given normal.
Definition: vector2.cpp:266
bool SetValue(const Char8 *rhs)
Set the value of this.
bool SetName(const Char8 *rhs)
Set the name of .
Vector2 operator+(const Vector2 &Vec2) const
Addition Operator.
Definition: vector2.cpp:182
Real Y
Coordinate on the Y vector.
Definition: vector2.h:69
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
Real X
Coordinate on the X vector.
Definition: vector2.h:67
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
Real CrossProduct(const Vector2 &Other) const
This is used to calculate the crossproduct of this and another vector.
Definition: vector2.cpp:245
void SetIdentity()
Sets the values of this vector2 to identity values(0,0).
Definition: vector2.cpp:99
Real AsReal(Real def=0) const
Attempts to convert the value of the attribute to a Real and returns the results. ...
Real SquaredLength() const
Gets the length of this vector squared.
Definition: vector2.cpp:260
static Vector2 Unit_X()
Gets a vector representing the X unit of a Vector2.
Definition: vector2.cpp:84
Vector2 operator*(const Real &scalar) const
Scaling by multiplication.
Definition: vector2.cpp:146
static Vector2 Neg_Unit_Y()
Gets a vector representing the negative Y unit of a Vector2.
Definition: vector2.cpp:93
Real SquaredDistance(const Vector2 &Other) const
Gets the squared distance between this and another vector.
Definition: vector2.cpp:254
void SetValues(const Real &x, const Real &y)
Sets the X and Y values of this vector2.
Definition: vector2.cpp:105
std::istream & DeSerialize(std::istream &Stream, T &Converted)
Deserialize the next xml tag in the stream into a specific in memory class instance.
Real Length() const
Gets the length of this vector.
Definition: vector2.cpp:257
Thrown when the identity string wasn't valid at all.
Definition: exception.h:93
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
Boole operator>=(const Mezzanine::Vector2 &Vec) const
Greater than or Equal Comparison Operator.
Definition: vector2.cpp:134
void ProtoSerialize(XML::Node &CurrentRoot) const
Convert this class to an XML::Node ready for serialization.
Definition: vector2.cpp:314
Boole operator!=(const Mezzanine::Vector2 &Vec2) const
Equality Comparison Operator.
Definition: vector2.cpp:122
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
std::istream & operator>>(std::istream &stream, Mezzanine::LinearInterpolator< T > &Lint)
Used to de-serialize an Mezzanine::LinearInterpolator from a stream.
Definition: interpolator.h:448
Vector2()
Default Constructor.
Definition: vector2.cpp:55
Vector2 & Normalize()
Normalizes this Vector2.
Definition: vector2.cpp:269
Vector2 operator-()
Unary Operator.
Definition: vector2.cpp:140
Vector2 GetNormal() const
Gets the normal of this Vector2.
Definition: vector2.cpp:282
const Char8 * Name() const
ptrdiff_tGet the name of this Node.
Vector2 & operator+=(const Vector2 &Vec2)
Addition assignment Operator.
Definition: vector2.cpp:214
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.
Vector2 & operator*=(const Real &scalar)
Scaling by multiplication.
Definition: vector2.cpp:165
static Vector2 Neg_Unit_X()
Gets a vector representing the negative X unit of a Vector2.
Definition: vector2.cpp:90
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Real AngleBetween(const Vector2 &Other) const
Gets the angle between this Vector2 and another.
Definition: vector2.cpp:298
void ProtoDeSerialize(const XML::Node &OneNode)
Take the data stored in an XML and overwrite this instance of this object with it.
Definition: vector2.cpp:335
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
Boole operator<=(const Mezzanine::Vector2 &Vec) const
Less or Equal Comparison Operator.
Definition: vector2.cpp:131