OgreVector2.h
Go to the documentation of this file.

    00001 /*
    00002 -----------------------------------------------------------------------------
    00003 This source file is part of OGRE
    00004     (Object-oriented Graphics Rendering Engine)
    00005 For the latest info, see http://www.ogre3d.org/
    00006
    00007 Copyright (c) 2000-2012 Torus Knot Software Ltd
    00008
    00009 Permission is hereby granted, free of charge, to any person obtaining a copy
    00010 of this software and associated documentation files (the "Software"), to deal
    00011 in the Software without restriction, including without limitation the rights
    00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00013 copies of the Software, and to permit persons to whom the Software is
    00014 furnished to do so, subject to the following conditions:
    00015
    00016 The above copyright notice and this permission notice shall be included in
    00017 all copies or substantial portions of the Software.
    00018
    00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00025 THE SOFTWARE.
    00026 -----------------------------------------------------------------------------
    00027 */
    00028 #ifndef __Vector2_H__
    00029 #define __Vector2_H__
    00030
    00031
    00032 #include "OgrePrerequisites.h"
    00033 #include "OgreMath.h"
    00034
    00035 namespace Ogre
    00036 {
    00037
    00051     class _OgreExport Vector2
    00052     {
    00053     public:
    00054         Real x, y;
    00055
    00056     public:
    00057         inline Vector2()
    00058         {
    00059         }
    00060
    00061         inline Vector2(const Real fX, const Real fY )
    00062             : x( fX ), y( fY )
    00063         {
    00064         }
    00065
    00066         inline explicit Vector2( const Real scaler )
    00067             : x( scaler), y( scaler )
    00068         {
    00069         }
    00070
    00071         inline explicit Vector2( const Real afCoordinate[2] )
    00072             : x( afCoordinate[0] ),
    00073               y( afCoordinate[1] )
    00074         {
    00075         }
    00076
    00077         inline explicit Vector2( const int afCoordinate[2] )
    00078         {
    00079             x = (Real)afCoordinate[0];
    00080             y = (Real)afCoordinate[1];
    00081         }
    00082
    00083         inline explicit Vector2( Real* const r )
    00084             : x( r[0] ), y( r[1] )
    00085         {
    00086         }
    00087
    00090         inline void swap(Vector2& other)
    00091         {
    00092             std::swap(x, other.x);
    00093             std::swap(y, other.y);
    00094         }
    00095
    00096         inline Real operator [] ( const size_t i ) const
    00097         {
    00098             assert( i < 2 );
    00099
    00100             return *(&x+i);
    00101         }
    00102
    00103         inline Real& operator [] ( const size_t i )
    00104         {
    00105             assert( i < 2 );
    00106
    00107             return *(&x+i);
    00108         }
    00109
    00111         inline Real* ptr()
    00112         {
    00113             return &x;
    00114         }
    00116         inline const Real* ptr() const
    00117         {
    00118             return &x;
    00119         }
    00120
    00125         inline Vector2& operator = ( const Vector2& rkVector )
    00126         {
    00127             x = rkVector.x;
    00128             y = rkVector.y;
    00129
    00130             return *this;
    00131         }
    00132
    00133         inline Vector2& operator = ( const Real fScalar)
    00134         {
    00135             x = fScalar;
    00136             y = fScalar;
    00137
    00138             return *this;
    00139         }
    00140
    00141         inline bool operator == ( const Vector2& rkVector ) const
    00142         {
    00143             return ( x == rkVector.x && y == rkVector.y );
    00144         }
    00145
    00146         inline bool operator != ( const Vector2& rkVector ) const
    00147         {
    00148             return ( x != rkVector.x || y != rkVector.y  );
    00149         }
    00150
    00151         // arithmetic operations
    00152         inline Vector2 operator + ( const Vector2& rkVector ) const
    00153         {
    00154             return Vector2(
    00155                 x + rkVector.x,
    00156                 y + rkVector.y);
    00157         }
    00158
    00159         inline Vector2 operator - ( const Vector2& rkVector ) const
    00160         {
    00161             return Vector2(
    00162                 x - rkVector.x,
    00163                 y - rkVector.y);
    00164         }
    00165
    00166         inline Vector2 operator * ( const Real fScalar ) const
    00167         {
    00168             return Vector2(
    00169                 x * fScalar,
    00170                 y * fScalar);
    00171         }
    00172
    00173         inline Vector2 operator * ( const Vector2& rhs) const
    00174         {
    00175             return Vector2(
    00176                 x * rhs.x,
    00177                 y * rhs.y);
    00178         }
    00179
    00180         inline Vector2 operator / ( const Real fScalar ) const
    00181         {
    00182             assert( fScalar != 0.0 );
    00183
    00184             Real fInv = 1.0f / fScalar;
    00185
    00186             return Vector2(
    00187                 x * fInv,
    00188                 y * fInv);
    00189         }
    00190
    00191         inline Vector2 operator / ( const Vector2& rhs) const
    00192         {
    00193             return Vector2(
    00194                 x / rhs.x,
    00195                 y / rhs.y);
    00196         }
    00197
    00198         inline const Vector2& operator + () const
    00199         {
    00200             return *this;
    00201         }
    00202
    00203         inline Vector2 operator - () const
    00204         {
    00205             return Vector2(-x, -y);
    00206         }
    00207
    00208         // overloaded operators to help Vector2
    00209         inline friend Vector2 operator * ( const Real fScalar, const Vector2& rkVector )
    00210         {
    00211             return Vector2(
    00212                 fScalar * rkVector.x,
    00213                 fScalar * rkVector.y);
    00214         }
    00215
    00216         inline friend Vector2 operator / ( const Real fScalar, const Vector2& rkVector )
    00217         {
    00218             return Vector2(
    00219                 fScalar / rkVector.x,
    00220                 fScalar / rkVector.y);
    00221         }
    00222
    00223         inline friend Vector2 operator + (const Vector2& lhs, const Real rhs)
    00224         {
    00225             return Vector2(
    00226                 lhs.x + rhs,
    00227                 lhs.y + rhs);
    00228         }
    00229
    00230         inline friend Vector2 operator + (const Real lhs, const Vector2& rhs)
    00231         {
    00232             return Vector2(
    00233                 lhs + rhs.x,
    00234                 lhs + rhs.y);
    00235         }
    00236
    00237         inline friend Vector2 operator - (const Vector2& lhs, const Real rhs)
    00238         {
    00239             return Vector2(
    00240                 lhs.x - rhs,
    00241                 lhs.y - rhs);
    00242         }
    00243
    00244         inline friend Vector2 operator - (const Real lhs, const Vector2& rhs)
    00245         {
    00246             return Vector2(
    00247                 lhs - rhs.x,
    00248                 lhs - rhs.y);
    00249         }
    00250
    00251         // arithmetic updates
    00252         inline Vector2& operator += ( const Vector2& rkVector )
    00253         {
    00254             x += rkVector.x;
    00255             y += rkVector.y;
    00256
    00257             return *this;
    00258         }
    00259
    00260         inline Vector2& operator += ( const Real fScaler )
    00261         {
    00262             x += fScaler;
    00263             y += fScaler;
    00264
    00265             return *this;
    00266         }
    00267
    00268         inline Vector2& operator -= ( const Vector2& rkVector )
    00269         {
    00270             x -= rkVector.x;
    00271             y -= rkVector.y;
    00272
    00273             return *this;
    00274         }
    00275
    00276         inline Vector2& operator -= ( const Real fScaler )
    00277         {
    00278             x -= fScaler;
    00279             y -= fScaler;
    00280
    00281             return *this;
    00282         }
    00283
    00284         inline Vector2& operator *= ( const Real fScalar )
    00285         {
    00286             x *= fScalar;
    00287             y *= fScalar;
    00288
    00289             return *this;
    00290         }
    00291
    00292         inline Vector2& operator *= ( const Vector2& rkVector )
    00293         {
    00294             x *= rkVector.x;
    00295             y *= rkVector.y;
    00296
    00297             return *this;
    00298         }
    00299
    00300         inline Vector2& operator /= ( const Real fScalar )
    00301         {
    00302             assert( fScalar != 0.0 );
    00303
    00304             Real fInv = 1.0f / fScalar;
    00305
    00306             x *= fInv;
    00307             y *= fInv;
    00308
    00309             return *this;
    00310         }
    00311
    00312         inline Vector2& operator /= ( const Vector2& rkVector )
    00313         {
    00314             x /= rkVector.x;
    00315             y /= rkVector.y;
    00316
    00317             return *this;
    00318         }
    00319
    00327         inline Real length () const
    00328         {
    00329             return Math::Sqrt( x * x + y * y );
    00330         }
    00331
    00342         inline Real squaredLength () const
    00343         {
    00344             return x * x + y * y;
    00345         }
    00346
    00354         inline Real distance(const Vector2& rhs) const
    00355         {
    00356             return (*this - rhs).length();
    00357         }
    00358
    00369         inline Real squaredDistance(const Vector2& rhs) const
    00370         {
    00371             return (*this - rhs).squaredLength();
    00372         }
    00373
    00388         inline Real dotProduct(const Vector2& vec) const
    00389         {
    00390             return x * vec.x + y * vec.y;
    00391         }
    00392
    00403         inline Real normalise()
    00404         {
    00405             Real fLength = Math::Sqrt( x * x + y * y);
    00406
    00407             // Will also work for zero-sized vectors, but will change nothing
    00408             // We're not using epsilons because we don't need to.
    00409             // Read http://www.ogre3d.org/forums/viewtopic.php?f=4&t=61259
    00410             if ( fLength > Real(0.0f) )
    00411             {
    00412                 Real fInvLength = 1.0f / fLength;
    00413                 x *= fInvLength;
    00414                 y *= fInvLength;
    00415             }
    00416
    00417             return fLength;
    00418         }
    00419
    00423         inline Vector2 midPoint( const Vector2& vec ) const
    00424         {
    00425             return Vector2(
    00426                 ( x + vec.x ) * 0.5f,
    00427                 ( y + vec.y ) * 0.5f );
    00428         }
    00429
    00433         inline bool operator < ( const Vector2& rhs ) const
    00434         {
    00435             if( x < rhs.x && y < rhs.y )
    00436                 return true;
    00437             return false;
    00438         }
    00439
    00443         inline bool operator > ( const Vector2& rhs ) const
    00444         {
    00445             if( x > rhs.x && y > rhs.y )
    00446                 return true;
    00447             return false;
    00448         }
    00449
    00457         inline void makeFloor( const Vector2& cmp )
    00458         {
    00459             if( cmp.x < x ) x = cmp.x;
    00460             if( cmp.y < y ) y = cmp.y;
    00461         }
    00462
    00470         inline void makeCeil( const Vector2& cmp )
    00471         {
    00472             if( cmp.x > x ) x = cmp.x;
    00473             if( cmp.y > y ) y = cmp.y;
    00474         }
    00475
    00483         inline Vector2 perpendicular(void) const
    00484         {
    00485             return Vector2 (-y, x);
    00486         }
    00487
    00491         inline Real crossProduct( const Vector2& rkVector ) const
    00492         {
    00493             return x * rkVector.y - y * rkVector.x;
    00494         }
    00495
    00515         inline Vector2 randomDeviant(Real angle) const
    00516         {
    00517
    00518             angle *=  Math::UnitRandom() * Math::TWO_PI;
    00519             Real cosa = cos(angle);
    00520             Real sina = sin(angle);
    00521             return  Vector2(cosa * x - sina * y,
    00522                             sina * x + cosa * y);
    00523         }
    00524
    00526         inline bool isZeroLength(void) const
    00527         {
    00528             Real sqlen = (x * x) + (y * y);
    00529             return (sqlen < (1e-06 * 1e-06));
    00530
    00531         }
    00532
    00535         inline Vector2 normalisedCopy(void) const
    00536         {
    00537             Vector2 ret = *this;
    00538             ret.normalise();
    00539             return ret;
    00540         }
    00541
    00545         inline Vector2 reflect(const Vector2& normal) const
    00546         {
    00547             return Vector2( *this - ( 2 * this->dotProduct(normal) * normal ) );
    00548         }
    00549
    00551         inline bool isNaN() const
    00552         {
    00553             return Math::isNaN(x) || Math::isNaN(y);
    00554         }
    00555
    00560         inline Ogre::Radian angleBetween(const Ogre::Vector2& other) const
    00561         {
    00562             Ogre::Real lenProduct = length() * other.length();
    00563             // Divide by zero check
    00564             if(lenProduct < 1e-6f)
    00565                 lenProduct = 1e-6f;
    00566
    00567             Ogre::Real f = dotProduct(other) / lenProduct;
    00568
    00569             f = Ogre::Math::Clamp(f, (Ogre::Real)-1.0, (Ogre::Real)1.0);
    00570             return Ogre::Math::ACos(f);
    00571         }
    00572
    00578         inline Ogre::Radian angleTo(const Ogre::Vector2& other) const
    00579         {
    00580             Ogre::Radian angle = angleBetween(other);
    00581
    00582             if (crossProduct(other)<0)
    00583                 angle = (Ogre::Radian)Ogre::Math::TWO_PI - angle;
    00584
    00585             return angle;
    00586         }
    00587
    00588         // special points
    00589         static const Vector2 ZERO;
    00590         static const Vector2 UNIT_X;
    00591         static const Vector2 UNIT_Y;
    00592         static const Vector2 NEGATIVE_UNIT_X;
    00593         static const Vector2 NEGATIVE_UNIT_Y;
    00594         static const Vector2 UNIT_SCALE;
    00595
    00598         inline _OgreExport friend std::ostream& operator <<
    00599             ( std::ostream& o, const Vector2& v )
    00600         {
    00601             o << "Vector2(" << v.x << ", " << v.y <<  ")";
    00602             return o;
    00603         }
    00604     };
    00608 }
    00609 #endif

Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun Sep 2 2012 07:27:24