MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vector2.h
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_h
41 #define _vector2_h
42 
43 #include "crossplatformexport.h"
44 #include "datatypes.h"
45 #ifndef SWIG
46  #include "XML/xml.h"
47  #include <limits>
48 #endif
49 
50 namespace Ogre
51 {
52  class Vector2;
53 }
54 
55 namespace Mezzanine
56 {
57  ///////////////////////////////////////////////////////////////////////////////
58  /// @class Vector2
59  /// @headerfile vector2.h
60  /// @brief This is used to represent a point on a 2 dimentional area, such as a screen.
61  /// @details This contains an X and Y value used to represent coordinates.
62  /// This also has a number of facilities to make converting to graphics subsystems
63  /// as easy as possible.
64  ///////////////////////////////////////
66  {
67  public:
68  /// @brief Coordinate on the X vector.
70  /// @brief Coordinate on the Y vector.
72 
73  /// @brief Default Constructor.
74  Vector2();
75  /// @brief Single Real value Constructor.
76  /// @param xy Value to set both x and y to.
77  Vector2(const Real& xy);
78  /// @brief Real value Constructor.
79  /// @param x Coordinate on the X vector.
80  /// @param y Coordinate on the Y vector.
81  Vector2(const Real& x, const Real& y);
82  /// @brief Ogre Value Constructor.
83  /// @param Vec The vector to be copied to make this vector.
84  Vector2(const Ogre::Vector2& Vec);
85 
86  /// @brief Gets a Ogre vector2.
87  /// @return Returns an Ogre Vector2 with the same values as this.
88  Ogre::Vector2 GetOgreVector2() const;
89  /// @brief Copies an existing Ogre vector2.
90  /// @param Thiers The vector2 to be extracted.
91  void ExtractOgreVector2(const Ogre::Vector2& Thiers);
92 
93  ///////////////////////////////////////////////////////////////////////////////
94  // Utility
95 
96  /// @brief Sets the values of this vector2 to identity values(0,0).
97  void SetIdentity();
98  /// @brief Sets the X and Y values of this vector2.
99  /// @param x The real that will have this vector's X member set to.
100  /// @param y The real that will have this vector's Y member set to.
101  void SetValues(const Real& x, const Real& y);
102  ///////////////////////////////////////////////////////////////////////////////
103  // Equality Comparison operators
104 
105  /// @brief Equality Comparison Operator.
106  /// @param Vec2 This is the other Mezzanine::Vector2 to compare with.
107  /// @return Returns true if X==X and Y==Y, otherwise returns false.
108  Boolean operator==(const Mezzanine::Vector2& Vec2) const;
109  /// @brief Equality Comparison Operator.
110  /// @param Vec2 This is the other Mezzanine::Vector2.
111  /// @return Returns true if X!=X or Y!=Y, otherwise returns false.
112  Boolean operator!=(const Mezzanine::Vector2& Vec2) const;
113  /// @brief Equality Comparison Operator.
114  /// @param Vec2 This is the other Ogre::Vector2.
115  /// @return Returns true if X==X and Y==Y, otherwise returns false.
116  Boolean operator==(const Ogre::Vector2& Vec2) const;
117  /// @brief Equality Comparison Operator.
118  /// @param Vec2 This is the other Ogre::Vector2.
119  /// @return Returns true if X!=X or Y!=Y, otherwise returns false.
120  Boolean operator!=(const Ogre::Vector2& Vec2) const;
121 
122  /// @brief Less or Equal Comparison Operator.
123  /// @details Returns true if X<=X and Y<=Y. If any of those do not hold this returns false.
124  /// @param Vec This is the other Mezzanine::Vector2.
125  /// @note Used primarily for testing. This is not implement for use with other kinds of Vector3 implementations as it is widely considered useless.
126  Boolean operator<= (const Mezzanine::Vector2 &Vec) const;
127  /// @brief Greater than or Equal Comparison Operator.
128  /// @details Returns true if X>=X and Y>=Y . If any of those do not hold this returns false.
129  /// @param Vec This is the other Mezzanine::Vector2.
130  /// @note Used primarily for testing. This is not implement for use with other kinds of Vector3 implementations as it is widely considered useless.
131  Boolean operator>= (const Mezzanine::Vector2 &Vec) const;
132 
133  ///////////////////////////////////////////////////////////////////////////////
134  // Vector2 Arithmetic with Real
135 
136  /// @brief Scaling by multiplication.
137  /// @return This returns a Vector2 that has been scaled.
138  /// @param scalar This is the amount to scale the Vector2 by.
139  Vector2 operator* (const Real& scalar) const;
140  /// @brief Scaling by Division.
141  /// @return This returns a Vector2 that has been scaled.
142  /// @param scalar This is the amount to scale the Vector2 by.
143  Vector2 operator/ (const Real& scalar) const;
144 
145  ///////////////////////////////////////////////////////////////////////////////
146  // Vector2 Arithmetic and assignment with Real
147 
148  /// @brief Scaling by multiplication.
149  /// @param scalar This is the amount to scale the Vector2 by.
150  /// @return Returns a reference to this.
151  Vector2& operator*= (const Real& scalar);
152  /// @brief Scaling by Division.
153  /// @param scalar This is the amount to scale the Vector2 by.
154  /// @return Returns a reference to this.
155  Vector2& operator/= (const Real& scalar);
156 
157  ///////////////////////////////////////////////////////////////////////////////
158  // Arithmetic Operators
159 
160  /// @brief Addition Operator.
161  /// @param Vec2 The other Vector2 to add to this.
162  /// @return Returns a new Vector2 that is the result of this operation.
163  Vector2 operator+ (const Vector2& Vec2) const;
164  /// @brief Subraction Operator.
165  /// @param Vec2 The other Vector2 to subtract from this.
166  /// @return Returns a new Vector2 that is the result of this operation.
167  Vector2 operator- (const Vector2& Vec2) const;
168  /// @brief Multiplaction Operator.
169  /// @param Vec2 The other Vector2 to multiply by this.
170  /// @return Returns a new Vector2 that is the result of this operation.
171  Vector2 operator* (const Vector2& Vec2) const;
172  /// @brief Division Operator.
173  /// @param Vec2 The other Vector2 to divide by.
174  /// @return Returns a new Vector2 that is the result of this operation.
175  Vector2 operator/ (const Vector2& Vec2) const;
176 
177  /// @brief Addition assignment Operator.
178  /// @param Vec2 The other Vector2 to add to this.
179  /// @return Returns a reference to this.
180  Vector2& operator+= (const Vector2& Vec2);
181  /// @brief Subraction assignment Operator.
182  /// @param Vec2 The other Vector2 to subtract from this.
183  /// @return Returns a reference to this.
184  Vector2& operator-= (const Vector2& Vec2);
185  /// @brief Multiplaction assignment Operator.
186  /// @param Vec2 The other Vector2 to multiply by this.
187  /// @return Returns a reference to this.
188  Vector2& operator*= (const Vector2& Vec2);
189  /// @brief Division assignment Operator.
190  /// @param Vec2 The other Vector2 to divide by.
191  /// @return Returns a reference to this.
192  Vector2& operator/= (const Vector2& Vec2);
193 
194  ///////////////////////////////////////////////////////////////////////////////
195  // Fancy Math
196 
197  /// @brief Generates a Vector2 that is perpendicular to this vector.
198  /// @return Returns a new Vector2 that is perpendicular to this.
199  Vector2 Perpendicular() const;
200  /// @brief Normalizes this Vector2.
201  /// @return Returns a reference to this.
202  Vector2& Normalize();
203 
204  ///////////////////////////////////////////////////////////////////////////////
205  // Serialization
206 
207  /// @brief Convert this class to an XML::Node ready for serialization
208  /// @param CurrentRoot The point in the XML hierarchy that all this vector2 should be appended to.
209  void ProtoSerialize(XML::Node& CurrentRoot) const;
210  /// @brief Take the data stored in an XML and overwrite this instance of this object with it
211  /// @param OneNode and XML::Node containing the data.
212  void ProtoDeSerialize(const XML::Node& OneNode);
213 
214  /// @brief Get the name of the the XML tag this class will leave behind as its instances are serialized.
215  /// @return A string containing "Vector2"
216  static String SerializableName();
217  };//Vector2
218 }//Mezzanine
219 
220 // We can skip these operators when creating bindings with swig
221 #ifndef SWIG
222 
223 /// @brief Used to Serialize an Mezzanine::Vector2 to a human readable stream
224 /// @details Sends proper XML to the output stream,
225 /// including versioning information which will be used to maintain backwards compatibility. The current XML format
226 /// will create one element with no sub-elements. The name of the xml node will be "Vector2". It will have 3 attributes.
227 /// "Version", will be set to a value of 1, indicating if came from version 1 compatible Vector2. It will also have an "X" and "Y"
228 /// attributes with values set appropriately. For example '<Vector2 Version="1" X="1" Y="2" />'.
229 /// @param x The Mezzanine::Vector2 to be converted to a stream of characters.
230 /// @param stream The place to send the characters, that define the Mezzanine::Vector2.
231 /// @return Get an std::ostream that was written to, this allow chaining of the << operators.
232 std::ostream& MEZZ_LIB operator << (std::ostream& stream, const Mezzanine::Vector2& x);
233 
234 /// @brief Used to de-serialize an Mezzanine::Vector2 from a stream
235 /// @details This reads in the xml and sets the target vector according to values from the stream.
236 /// @param Vec The Mezzanine::Vector2 that will accept the values from the xml
237 /// @param stream The place to get the characters from, that define the Mezzanine::Vector2.
238 /// @return Get an std::ostream that was read from, this allow chaining of the >> operators.
239 /// @throw Can throw any exception that any function in the Mezzanine::xml namespace could throw in addition to a Mezzanine::Exception if the serialization version doesn't match.
240 std::istream& MEZZ_LIB operator >> (std::istream& stream, Mezzanine::Vector2& Vec);
241 
242 /// @brief Converts an XML node into a Mezzanine::Vector2
243 /// @details This will convert an XML::Node will a valid serialized Mezzanine::Vector2 into a Mezzanine::Vector2
244 /// @param OneNode An XML Node containing the the text of a Vector2
245 /// @param Vec the Mezzanine::Vector2 to store the deserialized Vector2
246 /// @return This returns a reference to the XML::Node for operator chaining or whatever.
247 /// @throw Can throw any exception that any function in the Mezzanine::xml namespace could throw in addition to a Mezzanine::Exception if the serialization version doesn't match.
248 void MEZZ_LIB operator >> (const Mezzanine::XML::Node& OneNode, Mezzanine::Vector2& Vec);
249 
250 #endif // \SWIG
251 
252 namespace std
253 {
254  /// @brief Get Numeric details on Vector2
255  template<>
256  class numeric_limits<Mezzanine::Vector2>
257  {
258  public:
259  /// @brief Does this class (numeric_limits<Mezzanine::Vector2>) exist
260  static const bool is_specialized = true;
261  /// @brief Does this support negative values?
262  static const bool is_signed = true;
263  /// @brief Can this only store integer types.
264  static const bool is_integer = false;
265  /// @brief The Vector2 uses Real, which is typically a machine dependedant which can be inexact
266  static const bool is_exact = std::numeric_limits<Mezzanine::Real>::is_exact;
267  /// @brief Can This represent an infinitely large value in X, Y or Z?
268  static const bool has_infinity = std::numeric_limits<Mezzanine::Real>::has_infinity;
269  /// @brief ??? Required by std::numeric to be compliant
270  /// @todo Learn why this exists and document it.
271  static const bool has_quiet_NaN = std::numeric_limits<Mezzanine::Real>::has_quiet_NaN;
272  /// @brief ??? Required by std::numeric to be compliant
273  /// @todo Learn why this exists and document it.
274  static const bool has_signaling_NaN = std::numeric_limits<Mezzanine::Real>::has_signaling_NaN;
275  /// @brief Does this support exceptionally small numbers near 0?
276  static const std::float_denorm_style has_denorm = std::numeric_limits<Mezzanine::Real>::has_denorm;
277  /// @brief When extra precision near 0 is lost, can this type distinguish that from other imprecision.
278  static const bool has_denorm_loss = std::numeric_limits<Mezzanine::Real>::has_denorm_loss;
279  /// @brief How items that fit between the precise amount a Real can represent will be adapted.
280  static const std::float_round_style round_style = std::numeric_limits<Mezzanine::Real>::round_style;
281  /// @brief Do X, Y and Z adhere to iec 559?
282  static const bool is_iec559 = std::numeric_limits<Mezzanine::Real>::is_iec559;
283  /// @brief Is overflow of this type handle by modulo overflow?
284  static const bool is_modulo = std::numeric_limits<Mezzanine::Real>::is_modulo;
285  /// @brief How many integer digits(in machine base) of precision can this handle in each X, Y or Z without floating point component or error?
286  static const int digits = std::numeric_limits<Mezzanine::Real>::digits;
287  /// @brief How many integer digits in base 10 of precision can this handle in each X, Y or Z without floating point component or error?
288  static const int digits10 = std::numeric_limits<Mezzanine::Real>::digits10;
289  /// @brief The base of the number system that this is implemented in
290  static const int radix = std::numeric_limits<Mezzanine::Real>::radix;
291  /// @brief The smallest power of the radix that is valid floating point value
292  static const int min_exponent = std::numeric_limits<Mezzanine::Real>::min_exponent;
293  /// @brief The smallest power of 10 that is valid floating point value
294  static const int min_exponent10 = std::numeric_limits<Mezzanine::Real>::min_exponent10;
295  /// @brief The largest power of the radix that is valid floating point value
296  static const int max_exponent = std::numeric_limits<Mezzanine::Real>::max_exponent;
297  /// @brief The largest power of 10 that is valid floating point value
298  static const int max_exponent10 = std::numeric_limits<Mezzanine::Real>::max_exponent10;
299  /// @brief Can this generate a trap?
300  static const bool traps = std::numeric_limits<Mezzanine::Real>::traps;
301  /// @brief Are tiny values respected during rounding?
302  static const bool tinyness_before = std::numeric_limits<Mezzanine::Real>::tinyness_before;
303 
304  /// @brief Get the lowest positive finite value this can represent
305  /// @return A vector with 2 very small numbers
307  {
308  return Mezzanine::Vector2(std::numeric_limits<Mezzanine::Real>::min(),
309  std::numeric_limits<Mezzanine::Real>::min()
310  );
311  }
312 
313  /// @brief Get the highest positive finite value this can represent
314  /// @return A vector with 2 very large numbers
316  {
317  return Mezzanine::Vector2(std::numeric_limits<Mezzanine::Real>::max(),
318  std::numeric_limits<Mezzanine::Real>::max()
319  );
320  }
321 
322  /// @brief The smallest value representable from 1.0,1.0 to the next value
323  /// @return A vector with a very small number
325  {
326  return Mezzanine::Vector2(std::numeric_limits<Mezzanine::Real>::epsilon(),
327  std::numeric_limits<Mezzanine::Real>::epsilon()
328  );
329  }
330 
331  /// @brief Get the largest possible rounding error
332  /// @return A vector containing 2 values indicating how much they could be rounded.
334  {
335  return Mezzanine::Vector2(std::numeric_limits<Mezzanine::Real>::round_error(),
336  std::numeric_limits<Mezzanine::Real>::round_error()
337  );
338  }
339 
340  /// @brief Get the special value "Positive infinity"
341  /// @return A vector containing 2 values.
343  {
344  return Mezzanine::Vector2(std::numeric_limits<Mezzanine::Real>::infinity(),
345  std::numeric_limits<Mezzanine::Real>::infinity()
346  );
347  }
348 
349  /// @brief Get the special value "Quiet Not actual Number"
350  /// @return A vector containing 2 values.
352  {
353  return Mezzanine::Vector2(std::numeric_limits<Mezzanine::Real>::quiet_NaN(),
354  std::numeric_limits<Mezzanine::Real>::quiet_NaN()
355  );
356  }
357 
358  /// @brief Get the special value "Signaling Not actual Number"
359  /// @return A vector containing 2 values.
361  {
362  return Mezzanine::Vector2(std::numeric_limits<Mezzanine::Real>::signaling_NaN(),
363  std::numeric_limits<Mezzanine::Real>::signaling_NaN()
364  );
365  }
366 
367  /// @brief Get the closest value to 0 that is not 0 this can represent, including extra precision for being close to 0 if supported.
368  /// @return A vector containing 2 very small values.
370  {
371  return Mezzanine::Vector2(std::numeric_limits<Mezzanine::Real>::denorm_min(),
372  std::numeric_limits<Mezzanine::Real>::denorm_min()
373  );
374  }
375 
376  }; //Numeric Limits
377 
378 } // std
379 
380 
381 
382 #endif