MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vector2.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 _vector2_cpp
41 #define _vector2_cpp
42 
43 #include "serialization.h"
44 #include "stringtool.h"
45 #include "vector2.h"
46 #include "mathtool.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  // Utility
83 
85  {
86  this->X = 0;
87  this->Y = 0;
88  }
89 
90  void Vector2::SetValues(const Real& x, const Real& y)
91  {
92  this->X = x;
93  this->Y = y;
94  }
95 
96  ///////////////////////////////////////////////////////////////////////////////
97  // Equality Comparison operators
98 
99  Boolean Vector2::operator==(const Mezzanine::Vector2& Vec2) const
100  { return ( Vec2.X == this->X && Vec2.Y == this->Y ); }
101 
102  Boolean Vector2::operator!=(const Mezzanine::Vector2& Vec2) const
103  { return ( Vec2.X != this->X || Vec2.Y != this->Y ); }
104 
105  Boolean Vector2::operator==(const Ogre::Vector2& Vec2) const
106  { return ( Vec2.x == this->X && Vec2.y == this->Y ); }
107 
108  Boolean Vector2::operator!=(const Ogre::Vector2& Vec2) const
109  { return ( Vec2.x != this->X || Vec2.y != this->Y ); }
110 
111  Boolean Vector2::operator<= (const Mezzanine::Vector2 &Vec) const
112  { return ( this->X <= Vec.X && this->Y <= Vec.Y); }
113 
114  Boolean Vector2::operator>= (const Mezzanine::Vector2 &Vec) const
115  { return ( this->X >= Vec.X && this->Y >= Vec.Y); }
116 
117  ///////////////////////////////////////////////////////////////////////////////
118  // Vector2 Arithmetic with Real
119 
120  Vector2 Vector2::operator* (const Real& scalar) const
121  {
122  return Vector2(
123  this->X * scalar,
124  this->Y * scalar
125  );
126  }
127 
128  Vector2 Vector2::operator/ (const Real& scalar) const
129  {
130  return Vector2(
131  this->X / scalar,
132  this->Y / scalar
133  );
134  }
135 
136  ///////////////////////////////////////////////////////////////////////////////
137  // Vector2 Arithmetic and assignment with Real
138 
140  {
141  this->X *= scalar;
142  this->Y *= scalar;
143  return *this;
144  }
145 
147  {
148  this->X /= scalar;
149  this->Y /= scalar;
150  return *this;
151  }
152 
153  ///////////////////////////////////////////////////////////////////////////////
154  // Arithmetic Operators
155 
157  {
158  Vector2 Temp(X,Y);
159  Temp.X += Vec2.X;
160  Temp.Y += Vec2.Y;
161  return Temp;
162  }
163 
165  {
166  Vector2 Temp(X,Y);
167  Temp.X -= Vec2.X;
168  Temp.Y -= Vec2.Y;
169  return Temp;
170  }
171 
173  {
174  Vector2 Temp(X,Y);
175  Temp.X *= Vec2.X;
176  Temp.Y *= Vec2.Y;
177  return Temp;
178  }
179 
181  {
182  Vector2 Temp(X,Y);
183  Temp.X /= Vec2.X;
184  Temp.Y /= Vec2.Y;
185  return Temp;
186  }
187 
189  {
190  this->X += Vec2.X;
191  this->Y += Vec2.Y;
192  return *this;
193  }
194 
196  {
197  this->X -= Vec2.X;
198  this->Y -= Vec2.Y;
199  return *this;
200  }
201 
203  {
204  this->X *= Vec2.X;
205  this->Y *= Vec2.Y;
206  return *this;
207  }
208 
210  {
211  this->X /= Vec2.X;
212  this->Y /= Vec2.Y;
213  return *this;
214  }
215 
216  ///////////////////////////////////////////////////////////////////////////////
217  // Fancy Math
218 
220  {
221  return Vector2(-Y,X);
222  }
223 
225  {
226  Real Length = MathTools::Sqrt( X * X + Y * Y );
227 
228  if ( Length > 1e-08 )
229  {
230  Real InvLength = 1.0 / Length;
231  X *= InvLength;
232  Y *= InvLength;
233  }
234 
235  return *this;
236  }
237 
238  ///////////////////////////////////////////////////////////////////////////////
239  // Serialization
240 
241  void Vector2::ProtoSerialize(XML::Node& CurrentRoot) const
242  {
243  Mezzanine::XML::Node VecNode = CurrentRoot.AppendChild(SerializableName());
244  VecNode.SetName(SerializableName());
245 
246  Mezzanine::XML::Attribute VersionAttr = VecNode.AppendAttribute("Version");
247  Mezzanine::XML::Attribute XAttr = VecNode.AppendAttribute("X");
248  Mezzanine::XML::Attribute YAttr = VecNode.AppendAttribute("Y");
249  if( VersionAttr && XAttr && YAttr )
250  {
251  if( VersionAttr.SetValue("1") && XAttr.SetValue(this->X) && YAttr.SetValue(this->Y) )
252  {
253  return;
254  }else{
255  SerializeError("Create XML Attribute Values", SerializableName(),true);
256  }
257  }else{
258  SerializeError("Create XML Attributes", SerializableName(),true);
259  }
260  }
261 
263  {
265  {
266  if(OneNode.GetAttribute("Version").AsInt() == 1)
267  {
268  this->X=OneNode.GetAttribute("X").AsReal();
269  this->Y=OneNode.GetAttribute("Y").AsReal();
270  }else{
271  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + SerializableName() + ": Not Version 1.");
272  }
273  }else{
274  MEZZ_EXCEPTION(Exception::II_IDENTITY_INVALID_EXCEPTION,"Attempting to deserialize a " + SerializableName() + ", found a " + String(OneNode.Name()) + ".");
275  }
276  }
277 
279  { return String("Vector2"); }
280 }//Mezzanine
281 
282 ///////////////////////////////////////////////////////////////////////////////
283 // Class External << Operators for streaming or assignment
284 std::ostream& operator << (std::ostream& stream, const Mezzanine::Vector2& x)
285 {
286 
287  //stream << "<Vector2 Version=\"1\" X=\"" << x.X << "\" Y=\"" << x.Y << "\" />";
288  Serialize(stream,x);
289  return stream;
290 }
291 
292 std::istream& MEZZ_LIB operator >> (std::istream& stream, Mezzanine::Vector2& Vec)
293  { return DeSerialize(stream, Vec); }
294 
295 void operator >> (const Mezzanine::XML::Node& OneNode, Mezzanine::Vector2& Vec)
296  { Vec.ProtoDeSerialize(OneNode); }
297 
298 #endif