OgreVector2.h
Go to the documentation of this file.
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2013 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#ifndef __Vector2_H__
#define __Vector2_H__
#include "OgrePrerequisites.h"
#include "OgreMath.h"
namespace Ogre
{
class _OgreExport Vector2
{
public:
Real x, y;
public:
inline Vector2()
{
}
inline Vector2(const Real fX, const Real fY )
: x( fX ), y( fY )
{
}
inline explicit Vector2( const Real scaler )
: x( scaler), y( scaler )
{
}
inline explicit Vector2( const Real afCoordinate[2] )
: x( afCoordinate[0] ),
y( afCoordinate[1] )
{
}
inline explicit Vector2( const int afCoordinate[2] )
{
x = (Real)afCoordinate[0];
y = (Real)afCoordinate[1];
}
inline explicit Vector2( Real* const r )
: x( r[0] ), y( r[1] )
{
}
inline void swap(Vector2& other)
{
std::swap(x, other.x);
std::swap(y, other.y);
}
inline Real operator [] ( const size_t i ) const
{
assert( i < 2 );
return *(&x+i);
}
inline Real& operator [] ( const size_t i )
{
assert( i < 2 );
return *(&x+i);
}
inline Real* ptr()
{
return &x;
}
inline const Real* ptr() const
{
return &x;
}
inline Vector2& operator = ( const Vector2& rkVector )
{
x = rkVector.x;
y = rkVector.y;
return *this;
}
inline Vector2& operator = ( const Real fScalar)
{
x = fScalar;
y = fScalar;
return *this;
}
inline bool operator == ( const Vector2& rkVector ) const
{
return ( x == rkVector.x && y == rkVector.y );
}
inline bool operator != ( const Vector2& rkVector ) const
{
return ( x != rkVector.x || y != rkVector.y );
}
// arithmetic operations
inline Vector2 operator + ( const Vector2& rkVector ) const
{
return Vector2(
x + rkVector.x,
y + rkVector.y);
}
inline Vector2 operator - ( const Vector2& rkVector ) const
{
return Vector2(
x - rkVector.x,
y - rkVector.y);
}
inline Vector2 operator * ( const Real fScalar ) const
{
return Vector2(
x * fScalar,
y * fScalar);
}
inline Vector2 operator * ( const Vector2& rhs) const
{
return Vector2(
x * rhs.x,
y * rhs.y);
}
inline Vector2 operator / ( const Real fScalar ) const
{
assert( fScalar != 0.0 );
Real fInv = 1.0f / fScalar;
return Vector2(
x * fInv,
y * fInv);
}
inline Vector2 operator / ( const Vector2& rhs) const
{
return Vector2(
x / rhs.x,
y / rhs.y);
}
inline const Vector2& operator + () const
{
return *this;
}
inline Vector2 operator - () const
{
return Vector2(-x, -y);
}
// overloaded operators to help Vector2
inline friend Vector2 operator * ( const Real fScalar, const Vector2& rkVector )
{
return Vector2(
fScalar * rkVector.x,
fScalar * rkVector.y);
}
inline friend Vector2 operator / ( const Real fScalar, const Vector2& rkVector )
{
return Vector2(
fScalar / rkVector.x,
fScalar / rkVector.y);
}
inline friend Vector2 operator + (const Vector2& lhs, const Real rhs)
{
return Vector2(
lhs.x + rhs,
lhs.y + rhs);
}
inline friend Vector2 operator + (const Real lhs, const Vector2& rhs)
{
return Vector2(
lhs + rhs.x,
lhs + rhs.y);
}
inline friend Vector2 operator - (const Vector2& lhs, const Real rhs)
{
return Vector2(
lhs.x - rhs,
lhs.y - rhs);
}
inline friend Vector2 operator - (const Real lhs, const Vector2& rhs)
{
return Vector2(
lhs - rhs.x,
lhs - rhs.y);
}
// arithmetic updates
inline Vector2& operator += ( const Vector2& rkVector )
{
x += rkVector.x;
y += rkVector.y;
return *this;
}
inline Vector2& operator += ( const Real fScaler )
{
x += fScaler;
y += fScaler;
return *this;
}
inline Vector2& operator -= ( const Vector2& rkVector )
{
x -= rkVector.x;
y -= rkVector.y;
return *this;
}
inline Vector2& operator -= ( const Real fScaler )
{
x -= fScaler;
y -= fScaler;
return *this;
}
inline Vector2& operator *= ( const Real fScalar )
{
x *= fScalar;
y *= fScalar;
return *this;
}
inline Vector2& operator *= ( const Vector2& rkVector )
{
x *= rkVector.x;
y *= rkVector.y;
return *this;
}
inline Vector2& operator /= ( const Real fScalar )
{
assert( fScalar != 0.0 );
Real fInv = 1.0f / fScalar;
x *= fInv;
y *= fInv;
return *this;
}
inline Vector2& operator /= ( const Vector2& rkVector )
{
x /= rkVector.x;
y /= rkVector.y;
return *this;
}
inline Real length () const
{
return Math::Sqrt( x * x + y * y );
}
inline Real squaredLength () const
{
return x * x + y * y;
}
inline Real distance(const Vector2& rhs) const
{
return (*this - rhs).length();
}
inline Real squaredDistance(const Vector2& rhs) const
{
return (*this - rhs).squaredLength();
}
inline Real dotProduct(const Vector2& vec) const
{
return x * vec.x + y * vec.y;
}
inline Real normalise()
{
Real fLength = Math::Sqrt( x * x + y * y);
// Will also work for zero-sized vectors, but will change nothing
// We're not using epsilons because we don't need to.
// Read http://www.ogre3d.org/forums/viewtopic.php?f=4&t=61259
if ( fLength > Real(0.0f) )
{
Real fInvLength = 1.0f / fLength;
x *= fInvLength;
y *= fInvLength;
}
return fLength;
}
inline Vector2 midPoint( const Vector2& vec ) const
{
return Vector2(
( x + vec.x ) * 0.5f,
( y + vec.y ) * 0.5f );
}
inline bool operator < ( const Vector2& rhs ) const
{
if( x < rhs.x && y < rhs.y )
return true;
return false;
}
inline bool operator > ( const Vector2& rhs ) const
{
if( x > rhs.x && y > rhs.y )
return true;
return false;
}
inline void makeFloor( const Vector2& cmp )
{
if( cmp.x < x ) x = cmp.x;
if( cmp.y < y ) y = cmp.y;
}
inline void makeCeil( const Vector2& cmp )
{
if( cmp.x > x ) x = cmp.x;
if( cmp.y > y ) y = cmp.y;
}
inline Vector2 perpendicular(void) const
{
return Vector2 (-y, x);
}
inline Real crossProduct( const Vector2& rkVector ) const
{
return x * rkVector.y - y * rkVector.x;
}
inline Vector2 randomDeviant(Radian angle) const
{
angle *= Math::RangeRandom(-1, 1);
Real cosa = Math::Cos(angle);
Real sina = Math::Sin(angle);
return Vector2(cosa * x - sina * y,
sina * x + cosa * y);
}
inline bool isZeroLength(void) const
{
Real sqlen = (x * x) + (y * y);
return (sqlen < (1e-06 * 1e-06));
}
inline Vector2 normalisedCopy(void) const
{
Vector2 ret = *this;
ret.normalise();
return ret;
}
inline Vector2 reflect(const Vector2& normal) const
{
return Vector2( *this - ( 2 * this->dotProduct(normal) * normal ) );
}
inline bool isNaN() const
{
return Math::isNaN(x) || Math::isNaN(y);
}
inline Ogre::Radian angleBetween(const Ogre::Vector2& other) const
{
Ogre::Real lenProduct = length() * other.length();
// Divide by zero check
if(lenProduct < 1e-6f)
lenProduct = 1e-6f;
Ogre::Real f = dotProduct(other) / lenProduct;
f = Ogre::Math::Clamp(f, (Ogre::Real)-1.0, (Ogre::Real)1.0);
return Ogre::Math::ACos(f);
}
inline Ogre::Radian angleTo(const Ogre::Vector2& other) const
{
Ogre::Radian angle = angleBetween(other);
if (crossProduct(other)<0)
angle = (Ogre::Radian)Ogre::Math::TWO_PI - angle;
return angle;
}
// special points
static const Vector2 ZERO;
static const Vector2 UNIT_X;
static const Vector2 UNIT_Y;
static const Vector2 NEGATIVE_UNIT_X;
static const Vector2 NEGATIVE_UNIT_Y;
static const Vector2 UNIT_SCALE;
inline _OgreExport friend std::ostream& operator <<
( std::ostream& o, const Vector2& v )
{
o << "Vector2(" << v.x << ", " << v.y << ")";
return o;
}
};
}
#endif
Ogre::Vector2::angleTo
Ogre::Radian angleTo(const Ogre::Vector2 &other) const
Gets the oriented angle between 2 vectors.
Definition: OgreVector2.h:574
Ogre::Math::ACos
static Radian ACos(Real fValue)
Arc cosine function.
Ogre::Vector2::squaredDistance
Real squaredDistance(const Vector2 &rhs) const
Returns the square of the distance to another vector.
Definition: OgreVector2.h:373
Ogre::Vector2::randomDeviant
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
Ogre::Vector2::Vector2
Vector2(Real *const r)
Definition: OgreVector2.h:87
_OgreExport
#define _OgreExport
Definition: OgrePlatform.h:257
Ogre::Vector2::normalisedCopy
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
Ogre::Math::Cos
static Real Cos(const Radian &fValue, bool useTables=false)
Cosine function.
Definition: OgreMath.h:319
Ogre::Vector2::x
Real x
Definition: OgreVector2.h:54
Ogre::Math::isNaN
static bool isNaN(Real f)
Definition: OgreMath.h:305
Ogre::Vector2
Standard 2-dimensional vector.
Definition: OgreVector2.h:51
Ogre::Real
float Real
Software floating point type.
Definition: OgrePrerequisites.h:70
Ogre::Vector2::NEGATIVE_UNIT_X
static const Vector2 NEGATIVE_UNIT_X
Definition: OgreVector2.h:588
Ogre::Vector2::ptr
const Real * ptr() const
Pointer accessor for direct copying.
Definition: OgreVector2.h:120
Ogre::Vector2::Vector2
Vector2(const Real scaler)
Definition: OgreVector2.h:70
Ogre::Vector2::reflect
Vector2 reflect(const Vector2 &normal) const
Calculates a reflection vector to the plane with the given normal .
Definition: OgreVector2.h:541
Ogre::Vector2::Vector2
Vector2()
Default constructor.
Definition: OgreVector2.h:61
Ogre::Vector2::squaredLength
Real squaredLength() const
Returns the square of the length(magnitude) of the vector.
Definition: OgreVector2.h:346
Ogre::Vector2::makeFloor
void makeFloor(const Vector2 &cmp)
Sets this vector&#39;s components to the minimum of its own and the ones of the passed in vector...
Definition: OgreVector2.h:461
Ogre::Vector2::makeCeil
void makeCeil(const Vector2 &cmp)
Sets this vector&#39;s components to the maximum of its own and the ones of the passed in vector...
Definition: OgreVector2.h:474
Ogre::Vector2::ZERO
static const Vector2 ZERO
Definition: OgreVector2.h:585
Ogre::Vector2::UNIT_SCALE
static const Vector2 UNIT_SCALE
Definition: OgreVector2.h:590
OgrePrerequisites.h
Ogre::Vector2::crossProduct
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
Ogre::operator!=
bool operator!=(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)
Definition: OgreMemorySTLAllocator.h:202
Ogre::Vector2::distance
Real distance(const Vector2 &rhs) const
Returns the distance to another vector.
Definition: OgreVector2.h:358
Ogre::Vector2::normalise
Real normalise()
Normalises the vector.
Definition: OgreVector2.h:407
Ogre::Vector2::ptr
Real * ptr()
Pointer accessor for direct copying.
Definition: OgreVector2.h:115
Ogre::Vector2::angleBetween
Ogre::Radian angleBetween(const Ogre::Vector2 &other) const
Gets the angle between 2 vectors.
Definition: OgreVector2.h:556
Ogre::Math::Sqrt
static Real Sqrt(Real fValue)
Square root function.
Definition: OgreMath.h:405
Ogre::Vector2::Vector2
Vector2(const Real afCoordinate[2])
Definition: OgreVector2.h:75
Ogre::Vector2::NEGATIVE_UNIT_Y
static const Vector2 NEGATIVE_UNIT_Y
Definition: OgreVector2.h:589
std::swap
void swap(Ogre::SmallVectorImpl< T > &LHS, Ogre::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
Definition: OgreSmallVector.h:802
Ogre::Vector2::dotProduct
Real dotProduct(const Vector2 &vec) const
Calculates the dot (scalar) product of this vector with another.
Definition: OgreVector2.h:392
Ogre::Math::RangeRandom
static Real RangeRandom(Real fLow, Real fHigh)
Generate a random number within the range provided.
Ogre::Vector2::midPoint
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
Ogre::Math::Clamp
static T Clamp(T val, T minval, T maxval)
Clamp a value within an inclusive range.
Definition: OgreMath.h:690
Ogre
Definition: OgreAndroidLogListener.h:34
Ogre::Vector2::isNaN
bool isNaN() const
Check whether this vector contains valid values.
Definition: OgreVector2.h:547
OgreMath.h
Ogre::operator==
bool operator==(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)
Definition: OgreMemorySTLAllocator.h:184
Ogre::operator/
Radian operator/(Real a, const Radian &b)
Definition: OgreMath.h:752
Ogre::Vector2::perpendicular
Vector2 perpendicular(void) const
Generates a vector perpendicular to this vector (eg an &#39;up&#39; vector).
Definition: OgreVector2.h:487
Ogre::Vector2::UNIT_X
static const Vector2 UNIT_X
Definition: OgreVector2.h:586
Ogre::Math::Sin
static Real Sin(const Radian &fValue, bool useTables=false)
Sine function.
Definition: OgreMath.h:381
Ogre::Vector2::y
Real y
Definition: OgreVector2.h:54
Ogre::Vector2::length
Real length() const
Returns the length (magnitude) of the vector.
Definition: OgreVector2.h:331
Ogre::Vector2::UNIT_Y
static const Vector2 UNIT_Y
Definition: OgreVector2.h:587
Ogre::operator*
Radian operator*(Real a, const Radian &b)
Definition: OgreMath.h:747
Ogre::Vector2::Vector2
Vector2(const Real fX, const Real fY)
Definition: OgreVector2.h:65
Ogre::Vector2::Vector2
Vector2(const int afCoordinate[2])
Definition: OgreVector2.h:81
Ogre::Vector2::swap
void swap(Vector2 &other)
Exchange the contents of this vector with another.
Definition: OgreVector2.h:94
Ogre::Vector2::isZeroLength
bool isZeroLength(void) const
Returns true if this vector is zero length.
Definition: OgreVector2.h:522
Ogre::operator<
bool operator<(SharedPtr< T > const &a, SharedPtr< U > const &b)
Definition: OgreSharedPtr.h:326
Ogre::Radian
Wrapper class which indicates a given angle value is in Radians.
Definition: OgreMath.h:47
Ogre::Math::TWO_PI
static const Real TWO_PI
Definition: OgreMath.h:707

Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.