OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreVector2.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4  (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __Vector2_H__
29 #define __Vector2_H__
30 
31 
32 #include "OgrePrerequisites.h"
33 #include "OgreMath.h"
34 
35 namespace Ogre
36 {
37 
52  {
53  public:
54  Real x, y;
55 
56  public:
61  inline Vector2()
62  {
63  }
64 
65  inline Vector2(const Real fX, const Real fY )
66  : x( fX ), y( fY )
67  {
68  }
69 
70  inline explicit Vector2( const Real scaler )
71  : x( scaler), y( scaler )
72  {
73  }
74 
75  inline explicit Vector2( const Real afCoordinate[2] )
76  : x( afCoordinate[0] ),
77  y( afCoordinate[1] )
78  {
79  }
80 
81  inline explicit Vector2( const int afCoordinate[2] )
82  {
83  x = (Real)afCoordinate[0];
84  y = (Real)afCoordinate[1];
85  }
86 
87  inline explicit Vector2( Real* const r )
88  : x( r[0] ), y( r[1] )
89  {
90  }
91 
94  inline void swap(Vector2& other)
95  {
96  std::swap(x, other.x);
97  std::swap(y, other.y);
98  }
99 
100  inline Real operator [] ( const size_t i ) const
101  {
102  assert( i < 2 );
103 
104  return *(&x+i);
105  }
106 
107  inline Real& operator [] ( const size_t i )
108  {
109  assert( i < 2 );
110 
111  return *(&x+i);
112  }
113 
115  inline Real* ptr()
116  {
117  return &x;
118  }
120  inline const Real* ptr() const
121  {
122  return &x;
123  }
124 
129  inline Vector2& operator = ( const Vector2& rkVector )
130  {
131  x = rkVector.x;
132  y = rkVector.y;
133 
134  return *this;
135  }
136 
137  inline Vector2& operator = ( const Real fScalar)
138  {
139  x = fScalar;
140  y = fScalar;
141 
142  return *this;
143  }
144 
145  inline bool operator == ( const Vector2& rkVector ) const
146  {
147  return ( x == rkVector.x && y == rkVector.y );
148  }
149 
150  inline bool operator != ( const Vector2& rkVector ) const
151  {
152  return ( x != rkVector.x || y != rkVector.y );
153  }
154 
155  // arithmetic operations
156  inline Vector2 operator + ( const Vector2& rkVector ) const
157  {
158  return Vector2(
159  x + rkVector.x,
160  y + rkVector.y);
161  }
162 
163  inline Vector2 operator - ( const Vector2& rkVector ) const
164  {
165  return Vector2(
166  x - rkVector.x,
167  y - rkVector.y);
168  }
169 
170  inline Vector2 operator * ( const Real fScalar ) const
171  {
172  return Vector2(
173  x * fScalar,
174  y * fScalar);
175  }
176 
177  inline Vector2 operator * ( const Vector2& rhs) const
178  {
179  return Vector2(
180  x * rhs.x,
181  y * rhs.y);
182  }
183 
184  inline Vector2 operator / ( const Real fScalar ) const
185  {
186  assert( fScalar != 0.0 );
187 
188  Real fInv = 1.0f / fScalar;
189 
190  return Vector2(
191  x * fInv,
192  y * fInv);
193  }
194 
195  inline Vector2 operator / ( const Vector2& rhs) const
196  {
197  return Vector2(
198  x / rhs.x,
199  y / rhs.y);
200  }
201 
202  inline const Vector2& operator + () const
203  {
204  return *this;
205  }
206 
207  inline Vector2 operator - () const
208  {
209  return Vector2(-x, -y);
210  }
211 
212  // overloaded operators to help Vector2
213  inline friend Vector2 operator * ( const Real fScalar, const Vector2& rkVector )
214  {
215  return Vector2(
216  fScalar * rkVector.x,
217  fScalar * rkVector.y);
218  }
219 
220  inline friend Vector2 operator / ( const Real fScalar, const Vector2& rkVector )
221  {
222  return Vector2(
223  fScalar / rkVector.x,
224  fScalar / rkVector.y);
225  }
226 
227  inline friend Vector2 operator + (const Vector2& lhs, const Real rhs)
228  {
229  return Vector2(
230  lhs.x + rhs,
231  lhs.y + rhs);
232  }
233 
234  inline friend Vector2 operator + (const Real lhs, const Vector2& rhs)
235  {
236  return Vector2(
237  lhs + rhs.x,
238  lhs + rhs.y);
239  }
240 
241  inline friend Vector2 operator - (const Vector2& lhs, const Real rhs)
242  {
243  return Vector2(
244  lhs.x - rhs,
245  lhs.y - rhs);
246  }
247 
248  inline friend Vector2 operator - (const Real lhs, const Vector2& rhs)
249  {
250  return Vector2(
251  lhs - rhs.x,
252  lhs - rhs.y);
253  }
254 
255  // arithmetic updates
256  inline Vector2& operator += ( const Vector2& rkVector )
257  {
258  x += rkVector.x;
259  y += rkVector.y;
260 
261  return *this;
262  }
263 
264  inline Vector2& operator += ( const Real fScaler )
265  {
266  x += fScaler;
267  y += fScaler;
268 
269  return *this;
270  }
271 
272  inline Vector2& operator -= ( const Vector2& rkVector )
273  {
274  x -= rkVector.x;
275  y -= rkVector.y;
276 
277  return *this;
278  }
279 
280  inline Vector2& operator -= ( const Real fScaler )
281  {
282  x -= fScaler;
283  y -= fScaler;
284 
285  return *this;
286  }
287 
288  inline Vector2& operator *= ( const Real fScalar )
289  {
290  x *= fScalar;
291  y *= fScalar;
292 
293  return *this;
294  }
295 
296  inline Vector2& operator *= ( const Vector2& rkVector )
297  {
298  x *= rkVector.x;
299  y *= rkVector.y;
300 
301  return *this;
302  }
303 
304  inline Vector2& operator /= ( const Real fScalar )
305  {
306  assert( fScalar != 0.0 );
307 
308  Real fInv = 1.0f / fScalar;
309 
310  x *= fInv;
311  y *= fInv;
312 
313  return *this;
314  }
315 
316  inline Vector2& operator /= ( const Vector2& rkVector )
317  {
318  x /= rkVector.x;
319  y /= rkVector.y;
320 
321  return *this;
322  }
323 
331  inline Real length () const
332  {
333  return Math::Sqrt( x * x + y * y );
334  }
335 
346  inline Real squaredLength () const
347  {
348  return x * x + y * y;
349  }
350 
358  inline Real distance(const Vector2& rhs) const
359  {
360  return (*this - rhs).length();
361  }
362 
373  inline Real squaredDistance(const Vector2& rhs) const
374  {
375  return (*this - rhs).squaredLength();
376  }
377 
392  inline Real dotProduct(const Vector2& vec) const
393  {
394  return x * vec.x + y * vec.y;
395  }
396 
407  inline Real normalise()
408  {
409  Real fLength = Math::Sqrt( x * x + y * y);
410 
411  // Will also work for zero-sized vectors, but will change nothing
412  // We're not using epsilons because we don't need to.
413  // Read http://www.ogre3d.org/forums/viewtopic.php?f=4&t=61259
414  if ( fLength > Real(0.0f) )
415  {
416  Real fInvLength = 1.0f / fLength;
417  x *= fInvLength;
418  y *= fInvLength;
419  }
420 
421  return fLength;
422  }
423 
427  inline Vector2 midPoint( const Vector2& vec ) const
428  {
429  return Vector2(
430  ( x + vec.x ) * 0.5f,
431  ( y + vec.y ) * 0.5f );
432  }
433 
437  inline bool operator < ( const Vector2& rhs ) const
438  {
439  if( x < rhs.x && y < rhs.y )
440  return true;
441  return false;
442  }
443 
447  inline bool operator > ( const Vector2& rhs ) const
448  {
449  if( x > rhs.x && y > rhs.y )
450  return true;
451  return false;
452  }
453 
461  inline void makeFloor( const Vector2& cmp )
462  {
463  if( cmp.x < x ) x = cmp.x;
464  if( cmp.y < y ) y = cmp.y;
465  }
466 
474  inline void makeCeil( const Vector2& cmp )
475  {
476  if( cmp.x > x ) x = cmp.x;
477  if( cmp.y > y ) y = cmp.y;
478  }
479 
487  inline Vector2 perpendicular(void) const
488  {
489  return Vector2 (-y, x);
490  }
491 
495  inline Real crossProduct( const Vector2& rkVector ) const
496  {
497  return x * rkVector.y - y * rkVector.x;
498  }
499 
512  inline Vector2 randomDeviant(Radian angle) const
513  {
514  angle *= Math::RangeRandom(-1, 1);
515  Real cosa = Math::Cos(angle);
516  Real sina = Math::Sin(angle);
517  return Vector2(cosa * x - sina * y,
518  sina * x + cosa * y);
519  }
520 
522  inline bool isZeroLength(void) const
523  {
524  Real sqlen = (x * x) + (y * y);
525  return (sqlen < (1e-06 * 1e-06));
526 
527  }
528 
531  inline Vector2 normalisedCopy(void) const
532  {
533  Vector2 ret = *this;
534  ret.normalise();
535  return ret;
536  }
537 
541  inline Vector2 reflect(const Vector2& normal) const
542  {
543  return Vector2( *this - ( 2 * this->dotProduct(normal) * normal ) );
544  }
545 
547  inline bool isNaN() const
548  {
549  return Math::isNaN(x) || Math::isNaN(y);
550  }
551 
556  inline Ogre::Radian angleBetween(const Ogre::Vector2& other) const
557  {
558  Ogre::Real lenProduct = length() * other.length();
559  // Divide by zero check
560  if(lenProduct < 1e-6f)
561  lenProduct = 1e-6f;
562 
563  Ogre::Real f = dotProduct(other) / lenProduct;
564 
565  f = Ogre::Math::Clamp(f, (Ogre::Real)-1.0, (Ogre::Real)1.0);
566  return Ogre::Math::ACos(f);
567  }
568 
574  inline Ogre::Radian angleTo(const Ogre::Vector2& other) const
575  {
576  Ogre::Radian angle = angleBetween(other);
577 
578  if (crossProduct(other)<0)
579  angle = (Ogre::Radian)Ogre::Math::TWO_PI - angle;
580 
581  return angle;
582  }
583 
584  // special points
585  static const Vector2 ZERO;
586  static const Vector2 UNIT_X;
587  static const Vector2 UNIT_Y;
588  static const Vector2 NEGATIVE_UNIT_X;
589  static const Vector2 NEGATIVE_UNIT_Y;
590  static const Vector2 UNIT_SCALE;
591 
594  inline _OgreExport friend std::ostream& operator <<
595  ( std::ostream& o, const Vector2& v )
596  {
597  o << "Vector2(" << v.x << ", " << v.y << ")";
598  return o;
599  }
600  };
604 }
605 #endif
Real squaredDistance(const Vector2 &rhs) const
Returns the square of the distance to another vector.
Definition: OgreVector2.h:373
Ogre::Radian angleTo(const Ogre::Vector2 &other) const
Gets the oriented angle between 2 vectors.
Definition: OgreVector2.h:574
Real length() const
Returns the length (magnitude) of the vector.
Definition: OgreVector2.h:331
Real dotProduct(const Vector2 &vec) const
Calculates the dot (scalar) product of this vector with another.
Definition: OgreVector2.h:392
float Real
Software floating point type.
#define _OgreExport
Definition: OgrePlatform.h:255
static const Vector2 UNIT_Y
Definition: OgreVector2.h:587
static T Clamp(T val, T minval, T maxval)
Clamp a value within an inclusive range.
Definition: OgreMath.h:712
Vector2(const Real fX, const Real fY)
Definition: OgreVector2.h:65
bool operator<(SharedPtr< T > const &a, SharedPtr< U > const &b)
void swap(Vector2 &other)
Exchange the contents of this vector with another.
Definition: OgreVector2.h:94
static const Vector2 NEGATIVE_UNIT_X
Definition: OgreVector2.h:588
static const Real TWO_PI
Definition: OgreMath.h:729
static const Vector2 UNIT_X
Definition: OgreVector2.h:586
Vector2(const Real afCoordinate[2])
Definition: OgreVector2.h:75
static Radian ACos(Real fValue)
Arc cosine function.
Real crossProduct(const Vector2 &rkVector) const
Calculates the 2 dimensional cross-product of 2 vectors, which results in a single floating point val...
Definition: OgreVector2.h:495
Vector2 reflect(const Vector2 &normal) const
Calculates a reflection vector to the plane with the given normal .
Definition: OgreVector2.h:541
static Real Cos(const Radian &fValue, bool useTables=false)
Cosine function.
Definition: OgreMath.h:321
Radian operator*(Real a, const Radian &b)
Definition: OgreMath.h:782
Radian operator/(Real a, const Radian &b)
Definition: OgreMath.h:787
Vector2(const Real scaler)
Definition: OgreVector2.h:70
Vector2 midPoint(const Vector2 &vec) const
Returns a vector at a point half way between this and the passed in vector.
Definition: OgreVector2.h:427
static const Vector2 ZERO
Definition: OgreVector2.h:585
bool isNaN() const
Check whether this vector contains valid values.
Definition: OgreVector2.h:547
static Real Sin(const Radian &fValue, bool useTables=false)
Sine function.
Definition: OgreMath.h:399
static Real RangeRandom(Real fLow, Real fHigh)
Generate a random number within the range provided.
static Real Sqrt(Real fValue)
Square root function.
Definition: OgreMath.h:423
Real distance(const Vector2 &rhs) const
Returns the distance to another vector.
Definition: OgreVector2.h:358
Vector2 randomDeviant(Radian angle) const
Generates a new random vector which deviates from this vector by a given angle in a random direction...
Definition: OgreVector2.h:512
Standard 2-dimensional vector.
Definition: OgreVector2.h:51
static const Vector2 UNIT_SCALE
Definition: OgreVector2.h:590
Real normalise()
Normalises the vector.
Definition: OgreVector2.h:407
static const Vector2 NEGATIVE_UNIT_Y
Definition: OgreVector2.h:589
void swap(Ogre::SmallVectorImpl< T > &LHS, Ogre::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
void makeFloor(const Vector2 &cmp)
Sets this vector's components to the minimum of its own and the ones of the passed in vector...
Definition: OgreVector2.h:461
Vector2(const int afCoordinate[2])
Definition: OgreVector2.h:81
bool isZeroLength(void) const
Returns true if this vector is zero length.
Definition: OgreVector2.h:522
Wrapper class which indicates a given angle value is in Radians.
Definition: OgreMath.h:49
const Real * ptr() const
Pointer accessor for direct copying.
Definition: OgreVector2.h:120
Vector2 perpendicular(void) const
Generates a vector perpendicular to this vector (eg an 'up' vector).
Definition: OgreVector2.h:487
static bool isNaN(Real f)
Definition: OgreMath.h:307
Vector2()
Default constructor.
Definition: OgreVector2.h:61
Vector2(Real *const r)
Definition: OgreVector2.h:87
void makeCeil(const Vector2 &cmp)
Sets this vector's components to the maximum of its own and the ones of the passed in vector...
Definition: OgreVector2.h:474
Ogre::Radian angleBetween(const Ogre::Vector2 &other) const
Gets the angle between 2 vectors.
Definition: OgreVector2.h:556
Real squaredLength() const
Returns the square of the length(magnitude) of the vector.
Definition: OgreVector2.h:346
Real * ptr()
Pointer accessor for direct copying.
Definition: OgreVector2.h:115
Vector2 normalisedCopy(void) const
As normalise, except that this vector is unaffected and the normalised vector is returned as a copy...
Definition: OgreVector2.h:531
bool operator==(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)
bool operator!=(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)