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-2013 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:
00061         inline Vector2()
00062         {
00063         }
00064 
00065         inline Vector2(const Real fX, const Real fY )
00066             : x( fX ), y( fY )
00067         {
00068         }
00069 
00070         inline explicit Vector2( const Real scaler )
00071             : x( scaler), y( scaler )
00072         {
00073         }
00074 
00075         inline explicit Vector2( const Real afCoordinate[2] )
00076             : x( afCoordinate[0] ),
00077               y( afCoordinate[1] )
00078         {
00079         }
00080 
00081         inline explicit Vector2( const int afCoordinate[2] )
00082         {
00083             x = (Real)afCoordinate[0];
00084             y = (Real)afCoordinate[1];
00085         }
00086 
00087         inline explicit Vector2( Real* const r )
00088             : x( r[0] ), y( r[1] )
00089         {
00090         }
00091 
00094         inline void swap(Vector2& other)
00095         {
00096             std::swap(x, other.x);
00097             std::swap(y, other.y);
00098         }
00099 
00100         inline Real operator [] ( const size_t i ) const
00101         {
00102             assert( i < 2 );
00103 
00104             return *(&x+i);
00105         }
00106 
00107         inline Real& operator [] ( const size_t i )
00108         {
00109             assert( i < 2 );
00110 
00111             return *(&x+i);
00112         }
00113 
00115         inline Real* ptr()
00116         {
00117             return &x;
00118         }
00120         inline const Real* ptr() const
00121         {
00122             return &x;
00123         }
00124 
00129         inline Vector2& operator = ( const Vector2& rkVector )
00130         {
00131             x = rkVector.x;
00132             y = rkVector.y;
00133 
00134             return *this;
00135         }
00136 
00137         inline Vector2& operator = ( const Real fScalar)
00138         {
00139             x = fScalar;
00140             y = fScalar;
00141 
00142             return *this;
00143         }
00144 
00145         inline bool operator == ( const Vector2& rkVector ) const
00146         {
00147             return ( x == rkVector.x && y == rkVector.y );
00148         }
00149 
00150         inline bool operator != ( const Vector2& rkVector ) const
00151         {
00152             return ( x != rkVector.x || y != rkVector.y  );
00153         }
00154 
00155         // arithmetic operations
00156         inline Vector2 operator + ( const Vector2& rkVector ) const
00157         {
00158             return Vector2(
00159                 x + rkVector.x,
00160                 y + rkVector.y);
00161         }
00162 
00163         inline Vector2 operator - ( const Vector2& rkVector ) const
00164         {
00165             return Vector2(
00166                 x - rkVector.x,
00167                 y - rkVector.y);
00168         }
00169 
00170         inline Vector2 operator * ( const Real fScalar ) const
00171         {
00172             return Vector2(
00173                 x * fScalar,
00174                 y * fScalar);
00175         }
00176 
00177         inline Vector2 operator * ( const Vector2& rhs) const
00178         {
00179             return Vector2(
00180                 x * rhs.x,
00181                 y * rhs.y);
00182         }
00183 
00184         inline Vector2 operator / ( const Real fScalar ) const
00185         {
00186             assert( fScalar != 0.0 );
00187 
00188             Real fInv = 1.0f / fScalar;
00189 
00190             return Vector2(
00191                 x * fInv,
00192                 y * fInv);
00193         }
00194 
00195         inline Vector2 operator / ( const Vector2& rhs) const
00196         {
00197             return Vector2(
00198                 x / rhs.x,
00199                 y / rhs.y);
00200         }
00201 
00202         inline const Vector2& operator + () const
00203         {
00204             return *this;
00205         }
00206 
00207         inline Vector2 operator - () const
00208         {
00209             return Vector2(-x, -y);
00210         }
00211 
00212         // overloaded operators to help Vector2
00213         inline friend Vector2 operator * ( const Real fScalar, const Vector2& rkVector )
00214         {
00215             return Vector2(
00216                 fScalar * rkVector.x,
00217                 fScalar * rkVector.y);
00218         }
00219 
00220         inline friend Vector2 operator / ( const Real fScalar, const Vector2& rkVector )
00221         {
00222             return Vector2(
00223                 fScalar / rkVector.x,
00224                 fScalar / rkVector.y);
00225         }
00226 
00227         inline friend Vector2 operator + (const Vector2& lhs, const Real rhs)
00228         {
00229             return Vector2(
00230                 lhs.x + rhs,
00231                 lhs.y + rhs);
00232         }
00233 
00234         inline friend Vector2 operator + (const Real lhs, const Vector2& rhs)
00235         {
00236             return Vector2(
00237                 lhs + rhs.x,
00238                 lhs + rhs.y);
00239         }
00240 
00241         inline friend Vector2 operator - (const Vector2& lhs, const Real rhs)
00242         {
00243             return Vector2(
00244                 lhs.x - rhs,
00245                 lhs.y - rhs);
00246         }
00247 
00248         inline friend Vector2 operator - (const Real lhs, const Vector2& rhs)
00249         {
00250             return Vector2(
00251                 lhs - rhs.x,
00252                 lhs - rhs.y);
00253         }
00254 
00255         // arithmetic updates
00256         inline Vector2& operator += ( const Vector2& rkVector )
00257         {
00258             x += rkVector.x;
00259             y += rkVector.y;
00260 
00261             return *this;
00262         }
00263 
00264         inline Vector2& operator += ( const Real fScaler )
00265         {
00266             x += fScaler;
00267             y += fScaler;
00268 
00269             return *this;
00270         }
00271 
00272         inline Vector2& operator -= ( const Vector2& rkVector )
00273         {
00274             x -= rkVector.x;
00275             y -= rkVector.y;
00276 
00277             return *this;
00278         }
00279 
00280         inline Vector2& operator -= ( const Real fScaler )
00281         {
00282             x -= fScaler;
00283             y -= fScaler;
00284 
00285             return *this;
00286         }
00287 
00288         inline Vector2& operator *= ( const Real fScalar )
00289         {
00290             x *= fScalar;
00291             y *= fScalar;
00292 
00293             return *this;
00294         }
00295 
00296         inline Vector2& operator *= ( const Vector2& rkVector )
00297         {
00298             x *= rkVector.x;
00299             y *= rkVector.y;
00300 
00301             return *this;
00302         }
00303 
00304         inline Vector2& operator /= ( const Real fScalar )
00305         {
00306             assert( fScalar != 0.0 );
00307 
00308             Real fInv = 1.0f / fScalar;
00309 
00310             x *= fInv;
00311             y *= fInv;
00312 
00313             return *this;
00314         }
00315 
00316         inline Vector2& operator /= ( const Vector2& rkVector )
00317         {
00318             x /= rkVector.x;
00319             y /= rkVector.y;
00320 
00321             return *this;
00322         }
00323 
00331         inline Real length () const
00332         {
00333             return Math::Sqrt( x * x + y * y );
00334         }
00335 
00346         inline Real squaredLength () const
00347         {
00348             return x * x + y * y;
00349         }
00350 
00358         inline Real distance(const Vector2& rhs) const
00359         {
00360             return (*this - rhs).length();
00361         }
00362 
00373         inline Real squaredDistance(const Vector2& rhs) const
00374         {
00375             return (*this - rhs).squaredLength();
00376         }
00377 
00392         inline Real dotProduct(const Vector2& vec) const
00393         {
00394             return x * vec.x + y * vec.y;
00395         }
00396 
00407         inline Real normalise()
00408         {
00409             Real fLength = Math::Sqrt( x * x + y * y);
00410 
00411             // Will also work for zero-sized vectors, but will change nothing
00412             // We're not using epsilons because we don't need to.
00413             // Read http://www.ogre3d.org/forums/viewtopic.php?f=4&t=61259
00414             if ( fLength > Real(0.0f) )
00415             {
00416                 Real fInvLength = 1.0f / fLength;
00417                 x *= fInvLength;
00418                 y *= fInvLength;
00419             }
00420 
00421             return fLength;
00422         }
00423 
00427         inline Vector2 midPoint( const Vector2& vec ) const
00428         {
00429             return Vector2(
00430                 ( x + vec.x ) * 0.5f,
00431                 ( y + vec.y ) * 0.5f );
00432         }
00433 
00437         inline bool operator < ( const Vector2& rhs ) const
00438         {
00439             if( x < rhs.x && y < rhs.y )
00440                 return true;
00441             return false;
00442         }
00443 
00447         inline bool operator > ( const Vector2& rhs ) const
00448         {
00449             if( x > rhs.x && y > rhs.y )
00450                 return true;
00451             return false;
00452         }
00453 
00461         inline void makeFloor( const Vector2& cmp )
00462         {
00463             if( cmp.x < x ) x = cmp.x;
00464             if( cmp.y < y ) y = cmp.y;
00465         }
00466 
00474         inline void makeCeil( const Vector2& cmp )
00475         {
00476             if( cmp.x > x ) x = cmp.x;
00477             if( cmp.y > y ) y = cmp.y;
00478         }
00479 
00487         inline Vector2 perpendicular(void) const
00488         {
00489             return Vector2 (-y, x);
00490         }
00491 
00495         inline Real crossProduct( const Vector2& rkVector ) const
00496         {
00497             return x * rkVector.y - y * rkVector.x;
00498         }
00499 
00512         inline Vector2 randomDeviant(Radian angle) const
00513         {
00514             angle *= Math::RangeRandom(-1, 1);
00515             Real cosa = Math::Cos(angle);
00516             Real sina = Math::Sin(angle);
00517             return Vector2(cosa * x - sina * y,
00518                            sina * x + cosa * y);
00519         }
00520 
00522         inline bool isZeroLength(void) const
00523         {
00524             Real sqlen = (x * x) + (y * y);
00525             return (sqlen < (1e-06 * 1e-06));
00526 
00527         }
00528 
00531         inline Vector2 normalisedCopy(void) const
00532         {
00533             Vector2 ret = *this;
00534             ret.normalise();
00535             return ret;
00536         }
00537 
00541         inline Vector2 reflect(const Vector2& normal) const
00542         {
00543             return Vector2( *this - ( 2 * this->dotProduct(normal) * normal ) );
00544         }
00545 
00547         inline bool isNaN() const
00548         {
00549             return Math::isNaN(x) || Math::isNaN(y);
00550         }
00551 
00556         inline Ogre::Radian angleBetween(const Ogre::Vector2& other) const
00557         {       
00558             Ogre::Real lenProduct = length() * other.length();
00559             // Divide by zero check
00560             if(lenProduct < 1e-6f)
00561                 lenProduct = 1e-6f;
00562         
00563             Ogre::Real f = dotProduct(other) / lenProduct;
00564     
00565             f = Ogre::Math::Clamp(f, (Ogre::Real)-1.0, (Ogre::Real)1.0);
00566             return Ogre::Math::ACos(f);
00567         }
00568 
00574         inline Ogre::Radian angleTo(const Ogre::Vector2& other) const
00575         {
00576             Ogre::Radian angle = angleBetween(other);
00577         
00578             if (crossProduct(other)<0)          
00579                 angle = (Ogre::Radian)Ogre::Math::TWO_PI - angle;       
00580 
00581             return angle;
00582         }
00583 
00584         // special points
00585         static const Vector2 ZERO;
00586         static const Vector2 UNIT_X;
00587         static const Vector2 UNIT_Y;
00588         static const Vector2 NEGATIVE_UNIT_X;
00589         static const Vector2 NEGATIVE_UNIT_Y;
00590         static const Vector2 UNIT_SCALE;
00591 
00594         inline _OgreExport friend std::ostream& operator <<
00595             ( std::ostream& o, const Vector2& v )
00596         {
00597             o << "Vector2(" << v.x << ", " << v.y <<  ")";
00598             return o;
00599         }
00600     };
00604 }
00605 #endif

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:48