Loading...
Searching...
No Matches
vector3d.h
Go to the documentation of this file.
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __IRR_POINT_3D_H_INCLUDED__
#define __IRR_POINT_3D_H_INCLUDED__
#include "irrMath.h"
namespace irr
{
namespace core
{
template <class T>
class vector3d
{
public:
vector3d() : X(0), Y(0), Z(0) {}
vector3d(T nx, T ny, T nz) : X(nx), Y(ny), Z(nz) {}
explicit vector3d(T n) : X(n), Y(n), Z(n) {}
vector3d(const vector3d<T>& other) : X(other.X), Y(other.Y), Z(other.Z) {}
// operators
vector3d<T> operator-() const { return vector3d<T>(-X, -Y, -Z); }
vector3d<T>& operator=(const vector3d<T>& other) { X = other.X; Y = other.Y; Z = other.Z; return *this; }
vector3d<T> operator+(const vector3d<T>& other) const { return vector3d<T>(X + other.X, Y + other.Y, Z + other.Z); }
vector3d<T>& operator+=(const vector3d<T>& other) { X+=other.X; Y+=other.Y; Z+=other.Z; return *this; }
vector3d<T> operator+(const T val) const { return vector3d<T>(X + val, Y + val, Z + val); }
vector3d<T>& operator+=(const T val) { X+=val; Y+=val; Z+=val; return *this; }
vector3d<T> operator-(const vector3d<T>& other) const { return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z); }
vector3d<T>& operator-=(const vector3d<T>& other) { X-=other.X; Y-=other.Y; Z-=other.Z; return *this; }
vector3d<T> operator-(const T val) const { return vector3d<T>(X - val, Y - val, Z - val); }
vector3d<T>& operator-=(const T val) { X-=val; Y-=val; Z-=val; return *this; }
vector3d<T> operator*(const vector3d<T>& other) const { return vector3d<T>(X * other.X, Y * other.Y, Z * other.Z); }
vector3d<T>& operator*=(const vector3d<T>& other) { X*=other.X; Y*=other.Y; Z*=other.Z; return *this; }
vector3d<T> operator*(const T v) const { return vector3d<T>(X * v, Y * v, Z * v); }
vector3d<T>& operator*=(const T v) { X*=v; Y*=v; Z*=v; return *this; }
vector3d<T> operator/(const vector3d<T>& other) const { return vector3d<T>(X / other.X, Y / other.Y, Z / other.Z); }
vector3d<T>& operator/=(const vector3d<T>& other) { X/=other.X; Y/=other.Y; Z/=other.Z; return *this; }
vector3d<T> operator/(const T v) const { T i=(T)1.0/v; return vector3d<T>(X * i, Y * i, Z * i); }
vector3d<T>& operator/=(const T v) { T i=(T)1.0/v; X*=i; Y*=i; Z*=i; return *this; }
bool operator<=(const vector3d<T>&other) const
{
return (X<other.X || core::equals(X, other.X)) ||
(core::equals(X, other.X) && (Y<other.Y || core::equals(Y, other.Y))) ||
(core::equals(X, other.X) && core::equals(Y, other.Y) && (Z<other.Z || core::equals(Z, other.Z)));
}
bool operator>=(const vector3d<T>&other) const
{
return (X>other.X || core::equals(X, other.X)) ||
(core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y))) ||
(core::equals(X, other.X) && core::equals(Y, other.Y) && (Z>other.Z || core::equals(Z, other.Z)));
}
bool operator<(const vector3d<T>&other) const
{
return (X<other.X && !core::equals(X, other.X)) ||
(core::equals(X, other.X) && Y<other.Y && !core::equals(Y, other.Y)) ||
(core::equals(X, other.X) && core::equals(Y, other.Y) && Z<other.Z && !core::equals(Z, other.Z));
}
bool operator>(const vector3d<T>&other) const
{
return (X>other.X && !core::equals(X, other.X)) ||
(core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y)) ||
(core::equals(X, other.X) && core::equals(Y, other.Y) && Z>other.Z && !core::equals(Z, other.Z));
}
bool operator==(const vector3d<T>& other) const
{
return this->equals(other);
}
bool operator!=(const vector3d<T>& other) const
{
return !this->equals(other);
}
// functions
bool equals(const vector3d<T>& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const
{
return core::equals(X, other.X, tolerance) &&
core::equals(Y, other.Y, tolerance) &&
core::equals(Z, other.Z, tolerance);
}
vector3d<T>& set(const T nx, const T ny, const T nz) {X=nx; Y=ny; Z=nz; return *this;}
vector3d<T>& set(const vector3d<T>& p) {X=p.X; Y=p.Y; Z=p.Z;return *this;}
T getLength() const { return core::squareroot( X*X + Y*Y + Z*Z ); }
T getLengthSQ() const { return X*X + Y*Y + Z*Z; }
T dotProduct(const vector3d<T>& other) const
{
return X*other.X + Y*other.Y + Z*other.Z;
}
T getDistanceFrom(const vector3d<T>& other) const
{
return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLength();
}
T getDistanceFromSQ(const vector3d<T>& other) const
{
return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLengthSQ();
}
vector3d<T> crossProduct(const vector3d<T>& p) const
{
return vector3d<T>(Y * p.Z - Z * p.Y, Z * p.X - X * p.Z, X * p.Y - Y * p.X);
}
bool isBetweenPoints(const vector3d<T>& begin, const vector3d<T>& end) const
{
const T f = (end - begin).getLengthSQ();
return getDistanceFromSQ(begin) <= f &&
getDistanceFromSQ(end) <= f;
}
vector3d<T>& normalize()
{
f64 length = X*X + Y*Y + Z*Z;
if (length == 0 ) // this check isn't an optimization but prevents getting NAN in the sqrt.
return *this;
length = core::reciprocal_squareroot(length);
X = (T)(X * length);
Y = (T)(Y * length);
Z = (T)(Z * length);
return *this;
}
vector3d<T>& setLength(T newlength)
{
normalize();
return (*this *= newlength);
}
vector3d<T>& invert()
{
X *= -1;
Y *= -1;
Z *= -1;
return *this;
}
void rotateXZBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
{
degrees *= DEGTORAD64;
f64 cs = cos(degrees);
f64 sn = sin(degrees);
X -= center.X;
Z -= center.Z;
set((T)(X*cs - Z*sn), Y, (T)(X*sn + Z*cs));
X += center.X;
Z += center.Z;
}
void rotateXYBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
{
degrees *= DEGTORAD64;
f64 cs = cos(degrees);
f64 sn = sin(degrees);
X -= center.X;
Y -= center.Y;
set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs), Z);
X += center.X;
Y += center.Y;
}
void rotateYZBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
{
degrees *= DEGTORAD64;
f64 cs = cos(degrees);
f64 sn = sin(degrees);
Z -= center.Z;
Y -= center.Y;
set(X, (T)(Y*cs - Z*sn), (T)(Y*sn + Z*cs));
Z += center.Z;
Y += center.Y;
}
vector3d<T> getInterpolated(const vector3d<T>& other, f64 d) const
{
const f64 inv = 1.0 - d;
return vector3d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d), (T)(other.Z*inv + Z*d));
}
vector3d<T> getInterpolated_quadratic(const vector3d<T>& v2, const vector3d<T>& v3, f64 d) const
{
// this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;
const f64 inv = (T) 1.0 - d;
const f64 mul0 = inv * inv;
const f64 mul1 = (T) 2.0 * d * inv;
const f64 mul2 = d * d;
return vector3d<T> ((T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
(T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2),
(T)(Z * mul0 + v2.Z * mul1 + v3.Z * mul2));
}
vector3d<T>& interpolate(const vector3d<T>& a, const vector3d<T>& b, f64 d)
{
X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));
Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));
Z = (T)((f64)b.Z + ( ( a.Z - b.Z ) * d ));
return *this;
}
vector3d<T> getHorizontalAngle() const
{
vector3d<T> angle;
const f64 tmp = (atan2((f64)X, (f64)Z) * RADTODEG64);
angle.Y = (T)tmp;
if (angle.Y < 0)
angle.Y += 360;
if (angle.Y >= 360)
angle.Y -= 360;
const f64 z1 = core::squareroot(X*X + Z*Z);
angle.X = (T)(atan2((f64)z1, (f64)Y) * RADTODEG64 - 90.0);
if (angle.X < 0)
angle.X += 360;
if (angle.X >= 360)
angle.X -= 360;
return angle;
}
vector3d<T> getSphericalCoordinateAngles() const
{
vector3d<T> angle;
const f64 length = X*X + Y*Y + Z*Z;
if (length)
{
if (X!=0)
{
angle.Y = (T)(atan2((f64)Z,(f64)X) * RADTODEG64);
}
else if (Z<0)
angle.Y=180;
angle.X = (T)(acos(Y * core::reciprocal_squareroot(length)) * RADTODEG64);
}
return angle;
}
vector3d<T> rotationToDirection(const vector3d<T> & forwards = vector3d<T>(0, 0, 1)) const
{
const f64 cr = cos( core::DEGTORAD64 * X );
const f64 sr = sin( core::DEGTORAD64 * X );
const f64 cp = cos( core::DEGTORAD64 * Y );
const f64 sp = sin( core::DEGTORAD64 * Y );
const f64 cy = cos( core::DEGTORAD64 * Z );
const f64 sy = sin( core::DEGTORAD64 * Z );
const f64 srsp = sr*sp;
const f64 crsp = cr*sp;
const f64 pseudoMatrix[] = {
( cp*cy ), ( cp*sy ), ( -sp ),
( srsp*cy-cr*sy ), ( srsp*sy+cr*cy ), ( sr*cp ),
( crsp*cy+sr*sy ), ( crsp*sy-sr*cy ), ( cr*cp )};
return vector3d<T>(
(T)(forwards.X * pseudoMatrix[0] +
forwards.Y * pseudoMatrix[3] +
forwards.Z * pseudoMatrix[6]),
(T)(forwards.X * pseudoMatrix[1] +
forwards.Y * pseudoMatrix[4] +
forwards.Z * pseudoMatrix[7]),
(T)(forwards.X * pseudoMatrix[2] +
forwards.Y * pseudoMatrix[5] +
forwards.Z * pseudoMatrix[8]));
}
void getAs4Values(T* array) const
{
array[0] = X;
array[1] = Y;
array[2] = Z;
array[3] = 0;
}
void getAs3Values(T* array) const
{
array[0] = X;
array[1] = Y;
array[2] = Z;
}
T X;
T Y;
T Z;
};
// Implementor note: inline keyword needed due to template specialization for s32. Otherwise put specialization into a .cpp
template <>
inline vector3d<s32> vector3d<s32>::operator /(s32 val) const {return core::vector3d<s32>(X/val,Y/val,Z/val);}
template <>
inline vector3d<s32>& vector3d<s32>::operator /=(s32 val) {X/=val;Y/=val;Z/=val; return *this;}
template <>
inline vector3d<s32> vector3d<s32>::getSphericalCoordinateAngles() const
{
vector3d<s32> angle;
const f64 length = X*X + Y*Y + Z*Z;
if (length)
{
if (X!=0)
{
angle.Y = round32((f32)(atan2((f64)Z,(f64)X) * RADTODEG64));
}
else if (Z<0)
angle.Y=180;
angle.X = round32((f32)(acos(Y * core::reciprocal_squareroot(length)) * RADTODEG64));
}
return angle;
}
typedef vector3d<f32> vector3df;
typedef vector3d<s32> vector3di;
template<class S, class T>
vector3d<T> operator*(const S scalar, const vector3d<T>& vector) { return vector*scalar; }
} // end namespace core
} // end namespace irr
#endif
irr::core::array
Self reallocating template array (like stl vector) with additional features.
Definition irrArray.h:23
irr::core::vector3d
3d vector template class with lots of operators and methods.
Definition vector3d.h:23
irr::core::vector3d::operator/
vector3d< T > operator/(const T v) const
Definition vector3d.h:57
irr::core::vector3d::getDistanceFromSQ
T getDistanceFromSQ(const vector3d< T > &other) const
Returns squared distance from another point.
Definition vector3d.h:139
irr::core::vector3d::vector3d
vector3d(const vector3d< T > &other)
Copy constructor.
Definition vector3d.h:32
irr::core::vector3d::vector3d
vector3d(T n)
Constructor with the same value for all elements.
Definition vector3d.h:30
irr::core::vector3d::vector3d
vector3d(T nx, T ny, T nz)
Constructor with three different values.
Definition vector3d.h:28
irr::core::vector3d::setLength
vector3d< T > & setLength(T newlength)
Sets the length of the vector to a new value.
Definition vector3d.h:182
irr::core::vector3d::set
vector3d< T > & set(const vector3d< T > &p)
Definition vector3d.h:114
irr::core::vector3d::operator/
vector3d< T > operator/(const vector3d< T > &other) const
Definition vector3d.h:55
irr::core::vector3d::operator-=
vector3d< T > & operator-=(const T val)
Definition vector3d.h:48
irr::core::vector3d::interpolate
vector3d< T > & interpolate(const vector3d< T > &a, const vector3d< T > &b, f64 d)
Sets this vector to the linearly interpolated vector between a and b.
Definition vector3d.h:278
irr::core::vector3d::operator<
bool operator<(const vector3d< T > &other) const
sort in order X, Y, Z. Difference must be above rounding tolerance.
Definition vector3d.h:77
irr::core::vector3d::operator-
vector3d< T > operator-(const vector3d< T > &other) const
Definition vector3d.h:45
irr::core::vector3d::getLengthSQ
T getLengthSQ() const
Get squared length of the vector.
Definition vector3d.h:122
irr::core::vector3d::operator*=
vector3d< T > & operator*=(const vector3d< T > &other)
Definition vector3d.h:51
irr::core::vector3d::operator*
vector3d< T > operator*(const T v) const
Definition vector3d.h:52
irr::core::vector3d::operator!=
bool operator!=(const vector3d< T > &other) const
Definition vector3d.h:98
irr::core::vector3d::operator=
vector3d< T > & operator=(const vector3d< T > &other)
Definition vector3d.h:38
irr::core::vector3d::operator<=
bool operator<=(const vector3d< T > &other) const
sort in order X, Y, Z. Equality with rounding tolerance.
Definition vector3d.h:61
irr::core::vector3d::getHorizontalAngle
vector3d< T > getHorizontalAngle() const
Get the rotations that would make a (0,0,1) direction vector point in the same direction as this dire...
Definition vector3d.h:301
irr::core::vector3d::equals
bool equals(const vector3d< T > &other, const T tolerance=(T) ROUNDING_ERROR_f32) const
returns if this vector equals the other one, taking floating point rounding errors into account
Definition vector3d.h:106
irr::core::vector3d::rotateXZBy
void rotateXZBy(f64 degrees, const vector3d< T > &center=vector3d< T >())
Rotates the vector by a specified number of degrees around the Y axis and the specified center.
Definition vector3d.h:200
irr::core::vector3d::operator+
vector3d< T > operator+(const T val) const
Definition vector3d.h:42
irr::core::vector3d::crossProduct
vector3d< T > crossProduct(const vector3d< T > &p) const
Calculates the cross product with another vector.
Definition vector3d.h:147
irr::core::vector3d::operator*
vector3d< T > operator*(const vector3d< T > &other) const
Definition vector3d.h:50
irr::core::vector3d::operator/=
vector3d< T > & operator/=(const T v)
Definition vector3d.h:58
irr::core::vector3d::getLength
T getLength() const
Get length of the vector.
Definition vector3d.h:117
irr::core::vector3d::operator/=
vector3d< T > & operator/=(const vector3d< T > &other)
Definition vector3d.h:56
irr::core::vector3d::operator-=
vector3d< T > & operator-=(const vector3d< T > &other)
Definition vector3d.h:46
irr::core::vector3d::getInterpolated
vector3d< T > getInterpolated(const vector3d< T > &other, f64 d) const
Creates an interpolated vector between this vector and another vector.
Definition vector3d.h:247
irr::core::vector3d::operator>=
bool operator>=(const vector3d< T > &other) const
sort in order X, Y, Z. Equality with rounding tolerance.
Definition vector3d.h:69
irr::core::vector3d< f32 >::X
f32 X
Definition vector3d.h:408
irr::core::vector3d::operator-
vector3d< T > operator-(const T val) const
Definition vector3d.h:47
irr::core::vector3d::operator+=
vector3d< T > & operator+=(const T val)
Definition vector3d.h:43
irr::core::vector3d::normalize
vector3d< T > & normalize()
Normalizes the vector.
Definition vector3d.h:168
irr::core::vector3d::operator*=
vector3d< T > & operator*=(const T v)
Definition vector3d.h:53
irr::core::vector3d::getAs4Values
void getAs4Values(T *array) const
Fills an array of 4 values with the vector data (usually floats).
Definition vector3d.h:389
irr::core::vector3d::operator+=
vector3d< T > & operator+=(const vector3d< T > &other)
Definition vector3d.h:41
irr::core::vector3d::getInterpolated_quadratic
vector3d< T > getInterpolated_quadratic(const vector3d< T > &v2, const vector3d< T > &v3, f64 d) const
Creates a quadratically interpolated vector between this and two other vectors.
Definition vector3d.h:259
irr::core::vector3d::rotateYZBy
void rotateYZBy(f64 degrees, const vector3d< T > &center=vector3d< T >())
Rotates the vector by a specified number of degrees around the X axis and the specified center.
Definition vector3d.h:230
irr::core::vector3d::set
vector3d< T > & set(const T nx, const T ny, const T nz)
Definition vector3d.h:113
irr::core::vector3d::getDistanceFrom
T getDistanceFrom(const vector3d< T > &other) const
Get distance from another point.
Definition vector3d.h:132
irr::core::vector3d::rotationToDirection
vector3d< T > rotationToDirection(const vector3d< T > &forwards=vector3d< T >(0, 0, 1)) const
Builds a direction vector from (this) rotation vector.
Definition vector3d.h:357
irr::core::vector3d::isBetweenPoints
bool isBetweenPoints(const vector3d< T > &begin, const vector3d< T > &end) const
Returns if this vector interpreted as a point is on a line between two other points.
Definition vector3d.h:157
irr::core::vector3d< f32 >::Z
f32 Z
Definition vector3d.h:414
irr::core::vector3d::rotateXYBy
void rotateXYBy(f64 degrees, const vector3d< T > &center=vector3d< T >())
Rotates the vector by a specified number of degrees around the Z axis and the specified center.
Definition vector3d.h:215
irr::core::vector3d::invert
vector3d< T > & invert()
Inverts the vector.
Definition vector3d.h:189
irr::core::vector3d::operator>
bool operator>(const vector3d< T > &other) const
sort in order X, Y, Z. Difference must be above rounding tolerance.
Definition vector3d.h:85
irr::core::vector3d::dotProduct
T dotProduct(const vector3d< T > &other) const
Get the dot product with another vector.
Definition vector3d.h:125
irr::core::vector3d::vector3d
vector3d()
Default constructor (null vector).
Definition vector3d.h:26
irr::core::vector3d::getSphericalCoordinateAngles
vector3d< T > getSphericalCoordinateAngles() const
Get the spherical coordinate angles.
Definition vector3d.h:330
irr::core::vector3d< f32 >::Y
f32 Y
Definition vector3d.h:411
irr::core::vector3d::operator+
vector3d< T > operator+(const vector3d< T > &other) const
Definition vector3d.h:40
irr::core::vector3d::getAs3Values
void getAs3Values(T *array) const
Fills an array of 3 values with the vector data (usually floats).
Definition vector3d.h:399
irr::core::vector3d::operator-
vector3d< T > operator-() const
Definition vector3d.h:36
irr::core::vector3d::operator==
bool operator==(const vector3d< T > &other) const
use weak float compare
Definition vector3d.h:93
irrMath.h
irr::core
Basic classes such as vectors, planes, arrays, lists, and so on can be found in this namespace.
Definition aabbox3d.h:15
irr::core::vector3df
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
Definition vector3d.h:445
irr::core::operator*
CMatrix4< T > operator*(const T scalar, const CMatrix4< T > &mat)
Definition matrix4.h:2228
irr::core::vector3di
vector3d< s32 > vector3di
Typedef for an integer 3d vector.
Definition vector3d.h:448
irr::core::DEGTORAD64
const f64 DEGTORAD64
64bit constant for converting from degrees to radians (formally known as GRAD_PI2)
Definition irrMath.h:80
irr::core::ROUNDING_ERROR_f32
const f32 ROUNDING_ERROR_f32
Definition irrMath.h:49
irr::core::equals
bool equals(const f64 a, const f64 b, const f64 tolerance=ROUNDING_ERROR_f64)
returns if a equals b, taking possible rounding errors into account
Definition irrMath.h:185
irr::core::reciprocal_squareroot
REALINLINE f64 reciprocal_squareroot(const f64 x)
Definition irrMath.h:497
irr::core::round32
REALINLINE s32 round32(f32 x)
Definition irrMath.h:680
irr::core::RADTODEG64
const f64 RADTODEG64
64bit constant for converting from radians to degrees
Definition irrMath.h:83
irr::core::squareroot
REALINLINE f32 squareroot(const f32 f)
Definition irrMath.h:471
irr
Everything in the Irrlicht Engine can be found in this namespace.
Definition aabbox3d.h:13
irr::f32
float f32
32 bit floating point variable.
Definition irrTypes.h:104
irr::f64
double f64
64 bit floating point variable.
Definition irrTypes.h:108
irr::s32
signed int s32
32 bit signed variable.
Definition irrTypes.h:66
- Irrlicht Engine Documentation © 2003-2012 by Nikolaus Gebhardt. Generated on for Irrlicht 3D Engine by Doxygen 1.15.0