OgreVector3.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 __Vector3_H__
#define __Vector3_H__
#include "OgrePrerequisites.h"
#include "OgreMath.h"
#include "OgreQuaternion.h"
namespace Ogre
{
class _OgreExport Vector3
{
public:
Real x, y, z;
public:
inline Vector3()
{
}
inline Vector3( const Real fX, const Real fY, const Real fZ )
: x( fX ), y( fY ), z( fZ )
{
}
inline explicit Vector3( const Real afCoordinate[3] )
: x( afCoordinate[0] ),
y( afCoordinate[1] ),
z( afCoordinate[2] )
{
}
inline explicit Vector3( const int afCoordinate[3] )
{
x = (Real)afCoordinate[0];
y = (Real)afCoordinate[1];
z = (Real)afCoordinate[2];
}
inline explicit Vector3( Real* const r )
: x( r[0] ), y( r[1] ), z( r[2] )
{
}
inline explicit Vector3( const Real scaler )
: x( scaler )
, y( scaler )
, z( scaler )
{
}
inline void swap(Vector3& other)
{
std::swap(x, other.x);
std::swap(y, other.y);
std::swap(z, other.z);
}
inline Real operator [] ( const size_t i ) const
{
assert( i < 3 );
return *(&x+i);
}
inline Real& operator [] ( const size_t i )
{
assert( i < 3 );
return *(&x+i);
}
inline Real* ptr()
{
return &x;
}
inline const Real* ptr() const
{
return &x;
}
inline Vector3& operator = ( const Vector3& rkVector )
{
x = rkVector.x;
y = rkVector.y;
z = rkVector.z;
return *this;
}
inline Vector3& operator = ( const Real fScaler )
{
x = fScaler;
y = fScaler;
z = fScaler;
return *this;
}
inline bool operator == ( const Vector3& rkVector ) const
{
return ( x == rkVector.x && y == rkVector.y && z == rkVector.z );
}
inline bool operator != ( const Vector3& rkVector ) const
{
return ( x != rkVector.x || y != rkVector.y || z != rkVector.z );
}
// arithmetic operations
inline Vector3 operator + ( const Vector3& rkVector ) const
{
return Vector3(
x + rkVector.x,
y + rkVector.y,
z + rkVector.z);
}
inline Vector3 operator - ( const Vector3& rkVector ) const
{
return Vector3(
x - rkVector.x,
y - rkVector.y,
z - rkVector.z);
}
inline Vector3 operator * ( const Real fScalar ) const
{
return Vector3(
x * fScalar,
y * fScalar,
z * fScalar);
}
inline Vector3 operator * ( const Vector3& rhs) const
{
return Vector3(
x * rhs.x,
y * rhs.y,
z * rhs.z);
}
inline Vector3 operator / ( const Real fScalar ) const
{
assert( fScalar != 0.0 );
Real fInv = 1.0f / fScalar;
return Vector3(
x * fInv,
y * fInv,
z * fInv);
}
inline Vector3 operator / ( const Vector3& rhs) const
{
return Vector3(
x / rhs.x,
y / rhs.y,
z / rhs.z);
}
inline const Vector3& operator + () const
{
return *this;
}
inline Vector3 operator - () const
{
return Vector3(-x, -y, -z);
}
// overloaded operators to help Vector3
inline friend Vector3 operator * ( const Real fScalar, const Vector3& rkVector )
{
return Vector3(
fScalar * rkVector.x,
fScalar * rkVector.y,
fScalar * rkVector.z);
}
inline friend Vector3 operator / ( const Real fScalar, const Vector3& rkVector )
{
return Vector3(
fScalar / rkVector.x,
fScalar / rkVector.y,
fScalar / rkVector.z);
}
inline friend Vector3 operator + (const Vector3& lhs, const Real rhs)
{
return Vector3(
lhs.x + rhs,
lhs.y + rhs,
lhs.z + rhs);
}
inline friend Vector3 operator + (const Real lhs, const Vector3& rhs)
{
return Vector3(
lhs + rhs.x,
lhs + rhs.y,
lhs + rhs.z);
}
inline friend Vector3 operator - (const Vector3& lhs, const Real rhs)
{
return Vector3(
lhs.x - rhs,
lhs.y - rhs,
lhs.z - rhs);
}
inline friend Vector3 operator - (const Real lhs, const Vector3& rhs)
{
return Vector3(
lhs - rhs.x,
lhs - rhs.y,
lhs - rhs.z);
}
// arithmetic updates
inline Vector3& operator += ( const Vector3& rkVector )
{
x += rkVector.x;
y += rkVector.y;
z += rkVector.z;
return *this;
}
inline Vector3& operator += ( const Real fScalar )
{
x += fScalar;
y += fScalar;
z += fScalar;
return *this;
}
inline Vector3& operator -= ( const Vector3& rkVector )
{
x -= rkVector.x;
y -= rkVector.y;
z -= rkVector.z;
return *this;
}
inline Vector3& operator -= ( const Real fScalar )
{
x -= fScalar;
y -= fScalar;
z -= fScalar;
return *this;
}
inline Vector3& operator *= ( const Real fScalar )
{
x *= fScalar;
y *= fScalar;
z *= fScalar;
return *this;
}
inline Vector3& operator *= ( const Vector3& rkVector )
{
x *= rkVector.x;
y *= rkVector.y;
z *= rkVector.z;
return *this;
}
inline Vector3& operator /= ( const Real fScalar )
{
assert( fScalar != 0.0 );
Real fInv = 1.0f / fScalar;
x *= fInv;
y *= fInv;
z *= fInv;
return *this;
}
inline Vector3& operator /= ( const Vector3& rkVector )
{
x /= rkVector.x;
y /= rkVector.y;
z /= rkVector.z;
return *this;
}
inline Real length () const
{
return Math::Sqrt( x * x + y * y + z * z );
}
inline Real squaredLength () const
{
return x * x + y * y + z * z;
}
inline Real distance(const Vector3& rhs) const
{
return (*this - rhs).length();
}
inline Real squaredDistance(const Vector3& rhs) const
{
return (*this - rhs).squaredLength();
}
inline Real dotProduct(const Vector3& vec) const
{
return x * vec.x + y * vec.y + z * vec.z;
}
inline Real absDotProduct(const Vector3& vec) const
{
return Math::Abs(x * vec.x) + Math::Abs(y * vec.y) + Math::Abs(z * vec.z);
}
inline Real normalise()
{
Real fLength = Math::Sqrt( x * x + y * y + z * z );
// 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;
z *= fInvLength;
}
return fLength;
}
inline Vector3 crossProduct( const Vector3& rkVector ) const
{
return Vector3(
y * rkVector.z - z * rkVector.y,
z * rkVector.x - x * rkVector.z,
x * rkVector.y - y * rkVector.x);
}
inline Vector3 midPoint( const Vector3& vec ) const
{
return Vector3(
( x + vec.x ) * 0.5f,
( y + vec.y ) * 0.5f,
( z + vec.z ) * 0.5f );
}
inline bool operator < ( const Vector3& rhs ) const
{
if( x < rhs.x && y < rhs.y && z < rhs.z )
return true;
return false;
}
inline bool operator > ( const Vector3& rhs ) const
{
if( x > rhs.x && y > rhs.y && z > rhs.z )
return true;
return false;
}
inline void makeFloor( const Vector3& cmp )
{
if( cmp.x < x ) x = cmp.x;
if( cmp.y < y ) y = cmp.y;
if( cmp.z < z ) z = cmp.z;
}
inline void makeCeil( const Vector3& cmp )
{
if( cmp.x > x ) x = cmp.x;
if( cmp.y > y ) y = cmp.y;
if( cmp.z > z ) z = cmp.z;
}
inline Vector3 perpendicular(void) const
{
static const Real fSquareZero = (Real)(1e-06 * 1e-06);
Vector3 perp = this->crossProduct( Vector3::UNIT_X );
// Check length
if( perp.squaredLength() < fSquareZero )
{
/* This vector is the Y axis multiplied by a scalar, so we have
to use another axis.
*/
perp = this->crossProduct( Vector3::UNIT_Y );
}
perp.normalise();
return perp;
}
inline Vector3 randomDeviant(
const Radian& angle,
const Vector3& up = Vector3::ZERO ) const
{
Vector3 newUp;
if (up == Vector3::ZERO)
{
// Generate an up vector
newUp = this->perpendicular();
}
else
{
newUp = up;
}
// Rotate up vector by random amount around this
Quaternion q;
q.FromAngleAxis( Radian(Math::UnitRandom() * Math::TWO_PI), *this );
newUp = q * newUp;
// Finally rotate this by given angle around randomised up
q.FromAngleAxis( angle, newUp );
return q * (*this);
}
inline Radian angleBetween(const Vector3& dest) const
{
Real lenProduct = length() * dest.length();
// Divide by zero check
if(lenProduct < 1e-6f)
lenProduct = 1e-6f;
Real f = dotProduct(dest) / lenProduct;
f = Math::Clamp(f, (Real)-1.0, (Real)1.0);
return Math::ACos(f);
}
Quaternion getRotationTo(const Vector3& dest,
const Vector3& fallbackAxis = Vector3::ZERO) const
{
// Based on Stan Melax's article in Game Programming Gems
Quaternion q;
// Copy, since cannot modify local
Vector3 v0 = *this;
Vector3 v1 = dest;
v0.normalise();
v1.normalise();
Real d = v0.dotProduct(v1);
// If dot == 1, vectors are the same
if (d >= 1.0f)
{
return Quaternion::IDENTITY;
}
if (d < (1e-6f - 1.0f))
{
if (fallbackAxis != Vector3::ZERO)
{
// rotate 180 degrees about the fallback axis
q.FromAngleAxis(Radian(Math::PI), fallbackAxis);
}
else
{
// Generate an axis
Vector3 axis = Vector3::UNIT_X.crossProduct(*this);
if (axis.isZeroLength()) // pick another if colinear
axis = Vector3::UNIT_Y.crossProduct(*this);
axis.normalise();
q.FromAngleAxis(Radian(Math::PI), axis);
}
}
else
{
Real s = Math::Sqrt( (1+d)*2 );
Real invs = 1 / s;
Vector3 c = v0.crossProduct(v1);
q.x = c.x * invs;
q.y = c.y * invs;
q.z = c.z * invs;
q.w = s * 0.5f;
q.normalise();
}
return q;
}
inline bool isZeroLength(void) const
{
Real sqlen = (x * x) + (y * y) + (z * z);
return (sqlen < (1e-06 * 1e-06));
}
inline Vector3 normalisedCopy(void) const
{
Vector3 ret = *this;
ret.normalise();
return ret;
}
inline Vector3 reflect(const Vector3& normal) const
{
return Vector3( *this - ( 2 * this->dotProduct(normal) * normal ) );
}
inline bool positionEquals(const Vector3& rhs, Real tolerance = 1e-03) const
{
return Math::RealEqual(x, rhs.x, tolerance) &&
Math::RealEqual(y, rhs.y, tolerance) &&
Math::RealEqual(z, rhs.z, tolerance);
}
inline bool positionCloses(const Vector3& rhs, Real tolerance = 1e-03f) const
{
return squaredDistance(rhs) <=
(squaredLength() + rhs.squaredLength()) * tolerance;
}
inline bool directionEquals(const Vector3& rhs,
const Radian& tolerance) const
{
Real dot = dotProduct(rhs);
Radian angle = Math::ACos(dot);
return Math::Abs(angle.valueRadians()) <= tolerance.valueRadians();
}
inline bool isNaN() const
{
return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z);
}
inline Vector3 primaryAxis() const
{
Real absx = Math::Abs(x);
Real absy = Math::Abs(y);
Real absz = Math::Abs(z);
if (absx > absy)
if (absx > absz)
return x > 0 ? Vector3::UNIT_X : Vector3::NEGATIVE_UNIT_X;
else
return z > 0 ? Vector3::UNIT_Z : Vector3::NEGATIVE_UNIT_Z;
else // absx <= absy
if (absy > absz)
return y > 0 ? Vector3::UNIT_Y : Vector3::NEGATIVE_UNIT_Y;
else
return z > 0 ? Vector3::UNIT_Z : Vector3::NEGATIVE_UNIT_Z;
}
// special points
static const Vector3 ZERO;
static const Vector3 UNIT_X;
static const Vector3 UNIT_Y;
static const Vector3 UNIT_Z;
static const Vector3 NEGATIVE_UNIT_X;
static const Vector3 NEGATIVE_UNIT_Y;
static const Vector3 NEGATIVE_UNIT_Z;
static const Vector3 UNIT_SCALE;
inline _OgreExport friend std::ostream& operator <<
( std::ostream& o, const Vector3& v )
{
o << "Vector3(" << v.x << ", " << v.y << ", " << v.z << ")";
return o;
}
};
}
#endif
Ogre::Vector3::z
Real z
Definition: OgreVector3.h:54
Ogre::Vector3::NEGATIVE_UNIT_X
static const Vector3 NEGATIVE_UNIT_X
Definition: OgreVector3.h:804
Ogre::Vector3::positionEquals
bool positionEquals(const Vector3 &rhs, Real tolerance=1e-03) const
Returns whether this vector is within a positional tolerance of another vector.
Definition: OgreVector3.h:736
Ogre::Vector3::squaredLength
Real squaredLength() const
Returns the square of the length(magnitude) of the vector.
Definition: OgreVector3.h:371
Ogre::Math::ACos
static Radian ACos(Real fValue)
Arc cosine function.
Ogre::Vector3::ptr
Real * ptr()
Pointer accessor for direct copying.
Definition: OgreVector3.h:120
OgreQuaternion.h
_OgreExport
#define _OgreExport
Definition: OgrePlatform.h:257
Ogre::Vector3::Vector3
Vector3(const Real fX, const Real fY, const Real fZ)
Definition: OgreVector3.h:65
Ogre::Vector3::isNaN
bool isNaN() const
Check whether this vector contains valid values.
Definition: OgreVector3.h:774
Ogre::Vector3::NEGATIVE_UNIT_Z
static const Vector3 NEGATIVE_UNIT_Z
Definition: OgreVector3.h:806
Ogre::Math::PI
static const Real PI
Definition: OgreMath.h:706
Ogre::Vector3::dotProduct
Real dotProduct(const Vector3 &vec) const
Calculates the dot (scalar) product of this vector with another.
Definition: OgreVector3.h:417
Ogre::Quaternion
Implementation of a Quaternion, i.e.
Definition: OgreQuaternion.h:57
Ogre::Math::isNaN
static bool isNaN(Real f)
Definition: OgreMath.h:305
Ogre::Real
float Real
Software floating point type.
Definition: OgrePrerequisites.h:70
Ogre::Vector3::UNIT_X
static const Vector3 UNIT_X
Definition: OgreVector3.h:801
Ogre::Vector3::NEGATIVE_UNIT_Y
static const Vector3 NEGATIVE_UNIT_Y
Definition: OgreVector3.h:805
Ogre::Math::Abs
static Real Abs(Real fValue)
Absolute value function.
Definition: OgreMath.h:258
Ogre::Quaternion::FromAngleAxis
void FromAngleAxis(const Radian &rfAngle, const Vector3 &rkAxis)
Setups the quaternion using the supplied vector, and "roll" around that vector by the specified radia...
Ogre::Vector3
Standard 3-dimensional vector.
Definition: OgreVector3.h:51
Ogre::Vector3::midPoint
Vector3 midPoint(const Vector3 &vec) const
Returns a vector at a point half way between this and the passed in vector.
Definition: OgreVector3.h:503
OgrePrerequisites.h
Ogre::Vector3::positionCloses
bool positionCloses(const Vector3 &rhs, Real tolerance=1e-03f) const
Returns whether this vector is within a positional tolerance of another vector, also take scale of th...
Definition: OgreVector3.h:750
Ogre::Quaternion::y
Real y
Definition: OgreQuaternion.h:303
Ogre::Quaternion::normalise
Real normalise(void)
Normalises this quaternion, and returns the previous length.
Ogre::Vector3::y
Real y
Definition: OgreVector3.h:54
Ogre::Radian::valueRadians
Real valueRadians() const
Definition: OgreMath.h:59
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::Vector3::makeCeil
void makeCeil(const Vector3 &cmp)
Sets this vector&#39;s components to the maximum of its own and the ones of the passed in vector...
Definition: OgreVector3.h:552
Ogre::Quaternion::x
Real x
Definition: OgreQuaternion.h:303
Ogre::Vector3::isZeroLength
bool isZeroLength(void) const
Returns true if this vector is zero length.
Definition: OgreVector3.h:706
Ogre::Vector3::normalisedCopy
Vector3 normalisedCopy(void) const
As normalise, except that this vector is unaffected and the normalised vector is returned as a copy...
Definition: OgreVector3.h:715
Ogre::Vector3::Vector3
Vector3(Real *const r)
Definition: OgreVector3.h:84
Ogre::Math::Sqrt
static Real Sqrt(Real fValue)
Square root function.
Definition: OgreMath.h:405
Ogre::Quaternion::w
Real w
Definition: OgreQuaternion.h:303
Ogre::Vector3::absDotProduct
Real absDotProduct(const Vector3 &vec) const
Calculates the absolute dot (scalar) product of this vector with another.
Definition: OgreVector3.h:432
Ogre::Vector3::UNIT_SCALE
static const Vector3 UNIT_SCALE
Definition: OgreVector3.h:807
Ogre::Vector3::makeFloor
void makeFloor(const Vector3 &cmp)
Sets this vector&#39;s components to the minimum of its own and the ones of the passed in vector...
Definition: OgreVector3.h:538
Ogre::Vector3::UNIT_Y
static const Vector3 UNIT_Y
Definition: OgreVector3.h:802
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::Vector3::directionEquals
bool directionEquals(const Vector3 &rhs, const Radian &tolerance) const
Returns whether this vector is within a directional tolerance of another vector.
Definition: OgreVector3.h:763
Ogre::Vector3::reflect
Vector3 reflect(const Vector3 &normal) const
Calculates a reflection vector to the plane with the given normal .
Definition: OgreVector3.h:725
Ogre::Math::Clamp
static T Clamp(T val, T minval, T maxval)
Clamp a value within an inclusive range.
Definition: OgreMath.h:690
Ogre::Vector3::x
Real x
Definition: OgreVector3.h:54
Ogre
Definition: OgreAndroidLogListener.h:34
Ogre::Vector3::crossProduct
Vector3 crossProduct(const Vector3 &rkVector) const
Calculates the cross-product of 2 vectors, i.e.
Definition: OgreVector3.h:492
Ogre::Vector3::primaryAxis
Vector3 primaryAxis() const
Extract the primary (dominant) axis from this direction vector.
Definition: OgreVector3.h:780
Ogre::Vector3::Vector3
Vector3(const int afCoordinate[3])
Definition: OgreVector3.h:77
OgreMath.h
Ogre::Vector3::normalise
Real normalise()
Normalises the vector.
Definition: OgreVector3.h:446
Ogre::Vector3::Vector3
Vector3(const Real scaler)
Definition: OgreVector3.h:89
Ogre::Vector3::squaredDistance
Real squaredDistance(const Vector3 &rhs) const
Returns the square of the distance to another vector.
Definition: OgreVector3.h:398
Ogre::Vector3::ptr
const Real * ptr() const
Pointer accessor for direct copying.
Definition: OgreVector3.h:125
Ogre::Quaternion::z
Real z
Definition: OgreQuaternion.h:303
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::Math::UnitRandom
static Real UnitRandom()
Generate a random number of unit length.
Ogre::Vector3::swap
void swap(Vector3 &other)
Exchange the contents of this vector with another.
Definition: OgreVector3.h:99
Ogre::Quaternion::IDENTITY
static const Quaternion IDENTITY
Definition: OgreQuaternion.h:301
Ogre::Vector3::distance
Real distance(const Vector3 &rhs) const
Returns the distance to another vector.
Definition: OgreVector3.h:383
Ogre::Vector3::randomDeviant
Vector3 randomDeviant(const Radian &angle, const Vector3 &up=Vector3::ZERO) const
Generates a new random vector which deviates from this vector by a given angle in a random direction...
Definition: OgreVector3.h:603
Ogre::operator*
Radian operator*(Real a, const Radian &b)
Definition: OgreMath.h:747
Ogre::Vector3::angleBetween
Radian angleBetween(const Vector3 &dest) const
Gets the angle between 2 vectors.
Definition: OgreVector3.h:633
Ogre::Math::RealEqual
static bool RealEqual(Real a, Real b, Real tolerance=std::numeric_limits< Real >::epsilon())
Compare 2 reals, using tolerance for inaccuracies.
Ogre::Vector3::getRotationTo
Quaternion getRotationTo(const Vector3 &dest, const Vector3 &fallbackAxis=Vector3::ZERO) const
Gets the shortest arc quaternion to rotate this vector to the destination vector. ...
Definition: OgreVector3.h:655
Ogre::Vector3::perpendicular
Vector3 perpendicular(void) const
Generates a vector perpendicular to this vector (eg an &#39;up&#39; vector).
Definition: OgreVector3.h:566
Ogre::Vector3::length
Real length() const
Returns the length (magnitude) of the vector.
Definition: OgreVector3.h:356
Ogre::operator<
bool operator<(SharedPtr< T > const &a, SharedPtr< U > const &b)
Definition: OgreSharedPtr.h:326
Ogre::Vector3::UNIT_Z
static const Vector3 UNIT_Z
Definition: OgreVector3.h:803
Ogre::Vector3::Vector3
Vector3()
Default constructor.
Definition: OgreVector3.h:61
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
Ogre::Vector3::Vector3
Vector3(const Real afCoordinate[3])
Definition: OgreVector3.h:70
Ogre::Vector3::ZERO
static const Vector3 ZERO
Definition: OgreVector3.h:800

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