wfmath
1.0.3
A math library for the Worldforge system.
|
A dim dimensional vector. More...
#include <vector.h>
Public Member Functions | |
Vector () | |
Construct an uninitialized vector. More... | |
Vector (const Vector &)=default | |
Construct a copy of a vector. More... | |
Vector (const AtlasInType &a) | |
Construct a vector from an object passed by Atlas. More... | |
Vector (const Point< dim > &point) | |
Construct a vector from a point. More... | |
AtlasOutType | toAtlas () const |
Create an Atlas object from the vector. More... | |
void | fromAtlas (const AtlasInType &a) |
Set the vector's value to that given by an Atlas object. More... | |
Vector & | operator= (const Vector &v)=default |
bool | isEqualTo (const Vector &v, CoordType epsilon=numeric_constants< CoordType >::epsilon()) const |
bool | operator== (const Vector &v) const |
bool | operator!= (const Vector &v) const |
bool | isValid () const |
void | setValid (bool valid=true) |
make isValid() return true if you've initialized the vector by hand More... | |
Vector & | zero () |
Zero the components of a vector. More... | |
CoordType | operator[] (const int i) const |
Get the i'th element of the vector. More... | |
CoordType & | operator[] (const int i) |
Get the i'th element of the vector. More... | |
CoordType | sqrMag () const |
The squared magnitude of a vector. More... | |
CoordType | mag () const |
The magnitude of a vector. More... | |
Vector & | normalize (CoordType norm=1.0) |
Normalize a vector. More... | |
CoordType | sloppyMag () const |
An approximation to the magnitude of a vector. More... | |
Vector & | sloppyNorm (CoordType norm=1.0) |
Approximately normalize a vector. More... | |
Vector & | rotate (int axis1, int axis2, CoordType theta) |
Rotate the vector in the (axis1, axis2) plane by the angle theta. More... | |
Vector & | rotate (const Vector &v1, const Vector &v2, CoordType theta) |
Rotate the vector in the (v1, v2) plane by the angle theta. More... | |
Vector & | rotate (const RotMatrix< dim > &) |
Rotate the vector using a matrix. More... | |
Vector & | mirror (const int i) |
Reflect a vector in the direction of the i'th axis. More... | |
Vector & | mirror (const Vector &v) |
Reflect a vector in the direction specified by v. More... | |
Vector & | mirror () |
Reflect a vector in all directions simultaneously. More... | |
Vector (CoordType x, CoordType y) | |
2D only: construct a vector from (x, y) coordinates More... | |
Vector (CoordType x, CoordType y, CoordType z) | |
3D only: construct a vector from (x, y, z) coordinates More... | |
Vector & | rotate (CoordType theta) |
2D only: rotate a vector by an angle theta More... | |
Vector & | rotateX (CoordType theta) |
3D only: rotate a vector about the x axis by an angle theta More... | |
Vector & | rotateY (CoordType theta) |
3D only: rotate a vector about the y axis by an angle theta More... | |
Vector & | rotateZ (CoordType theta) |
3D only: rotate a vector about the z axis by an angle theta More... | |
Vector & | rotate (const Vector &axis, CoordType theta) |
3D only: rotate a vector about the i'th axis by an angle theta More... | |
Vector & | rotate (const Quaternion &q) |
3D only: rotate a vector using a Quaternion More... | |
CoordType | x () const |
Access the first component of a vector. More... | |
CoordType & | x () |
Access the first component of a vector. More... | |
CoordType | y () const |
Access the second component of a vector. More... | |
CoordType & | y () |
Access the second component of a vector. More... | |
CoordType | z () const |
Access the third component of a vector. More... | |
CoordType & | z () |
Access the third component of a vector. More... | |
Vector & | mirrorX () |
Flip the x component of a vector. More... | |
Vector & | mirrorY () |
Flip the y component of a vector. More... | |
Vector & | mirrorZ () |
Flip the z component of a vector. More... | |
Vector & | polar (CoordType r, CoordType theta) |
2D only: construct a vector from polar coordinates More... | |
void | asPolar (CoordType &r, CoordType &theta) const |
2D only: convert a vector to polar coordinates More... | |
Vector & | polar (CoordType r, CoordType theta, CoordType z) |
3D only: construct a vector from polar coordinates More... | |
void | asPolar (CoordType &r, CoordType &theta, CoordType &z) const |
3D only: convert a vector to polar coordinates More... | |
Vector & | spherical (CoordType r, CoordType theta, CoordType phi) |
3D only: construct a vector from shperical coordinates More... | |
void | asSpherical (CoordType &r, CoordType &theta, CoordType &phi) const |
3D only: convert a vector to shperical coordinates More... | |
const CoordType * | elements () const |
template<> | |
CoordType | sloppyMag () const |
template<> | |
CoordType | sloppyMag () const |
template<> | |
Vector< 3 > & | rotate (const Vector< 3 > &axis, CoordType theta) |
template<> | |
Vector< 3 > & | rotate (const Quaternion &q) |
template<> | |
Vector< 2 > & | polar (CoordType r, CoordType theta) |
template<> | |
void | asPolar (CoordType &r, CoordType &theta) const |
template<> | |
Vector< 3 > & | polar (CoordType r, CoordType theta, CoordType z) |
template<> | |
void | asPolar (CoordType &r, CoordType &theta, CoordType &z) const |
template<> | |
Vector< 3 > & | spherical (CoordType r, CoordType theta, CoordType phi) |
template<> | |
void | asSpherical (CoordType &r, CoordType &theta, CoordType &phi) const |
template<> | |
CoordType | z () const |
template<> | |
CoordType & | z () |
template<> | |
Vector< 3 > & | mirrorZ () |
template<> | |
CoordType | sloppyMagMax () |
template<> | |
CoordType | sloppyMagMax () |
template<> | |
CoordType | sloppyMagMax () |
template<> | |
CoordType | sloppyMagMaxSqrt () |
template<> | |
CoordType | sloppyMagMaxSqrt () |
template<> | |
CoordType | sloppyMagMaxSqrt () |
template<> | |
Vector< 3 > & | rotate (const Vector< 3 > &axis, CoordType theta) |
template<> | |
Vector< 3 > & | rotate (const Quaternion &q) |
template<> | |
Vector< 2 > & | polar (CoordType r, CoordType theta) |
template<> | |
void | asPolar (CoordType &r, CoordType &theta) const |
template<> | |
Vector< 3 > & | polar (CoordType r, CoordType theta, CoordType z) |
template<> | |
void | asPolar (CoordType &r, CoordType &theta, CoordType &z) const |
template<> | |
Vector< 3 > & | spherical (CoordType r, CoordType theta, CoordType phi) |
template<> | |
void | asSpherical (CoordType &r, CoordType &theta, CoordType &phi) const |
template<> | |
CoordType | sloppyMag () const |
template<> | |
CoordType | sloppyMag () const |
template<> | |
CoordType | sloppyMag () const |
template<> | |
Vector (CoordType x, CoordType y) | |
template<> | |
Vector (CoordType x, CoordType y, CoordType z) | |
template<> | |
Vector< 2 > & | rotate (CoordType theta) |
template<> | |
Vector< 3 > & | rotateX (CoordType theta) |
template<> | |
Vector< 3 > & | rotateY (CoordType theta) |
template<> | |
Vector< 3 > & | rotateZ (CoordType theta) |
Static Public Member Functions | |
static const Vector< dim > & | ZERO () |
Provides a global instance preset to zero. More... | |
static CoordType | sloppyMagMax () |
The maximum ratio of the return value of sloppyMag() to the true magnitude. More... | |
static CoordType | sloppyMagMaxSqrt () |
The square root of sloppyMagMax() More... | |
Friends | |
class | ZeroPrimitive< Vector< dim > > |
std::ostream & | operator (std::ostream &os, const Vector &v) |
std::istream & | operator>> (std::istream &is, Vector &v) |
Vector & | operator+= (Vector &v1, const Vector &v2) |
Add the second vector to the first. More... | |
Vector & | operator-= (Vector &v1, const Vector &v2) |
Subtract the second vector from the first. More... | |
Vector & | operator*= (Vector &v, CoordType d) |
Multiply the magnitude of v by d. More... | |
Vector & | operator/= (Vector &v, CoordType d) |
Divide the magnitude of v by d. More... | |
Vector | operator+ (const Vector &v1, const Vector &v2) |
Take the sum of two vectors. More... | |
Vector | operator- (const Vector &v1, const Vector &v2) |
Take the difference of two vectors. More... | |
Vector | operator- (const Vector &v) |
Reverse the direction of a vector. More... | |
Vector | operator* (CoordType d, const Vector &v) |
Multiply a vector by a scalar. More... | |
Vector | operator* (const Vector &v, CoordType d) |
Multiply a vector by a scalar. More... | |
Vector | operator/ (const Vector &v, CoordType d) |
Divide a vector by a scalar. More... | |
Vector | Prod (const RotMatrix< dim > &m, const Vector &v) |
returns m * v More... | |
Vector | InvProd (const RotMatrix< dim > &m, const Vector &v) |
returns m^-1 * v More... | |
Vector | operator- (const Point< dim > &c1, const Point< dim > &c2) |
Find the vector which gives the offset between two points. More... | |
Point< dim > | operator+ (const Point< dim > &c, const Vector &v) |
Find the point at the offset v from the point c. More... | |
Point< dim > | operator- (const Point< dim > &c, const Vector &v) |
Find the point at the offset -v from the point c. More... | |
Point< dim > | operator+ (const Vector &v, const Point< dim > &c) |
Find the point at the offset v from the point c. More... | |
Point< dim > & | operator+= (Point< dim > &p, const Vector &rhs) |
Shift a point by a vector. More... | |
Point< dim > & | operator-= (Point< dim > &p, const Vector &rhs) |
Shift a point by a vector, in the opposite direction. More... | |
CoordType | Cross (const Vector< 2 > &v1, const Vector< 2 > &v2) |
2D only: get the z component of the cross product of two vectors More... | |
Vector< 3 > | Cross (const Vector< 3 > &v1, const Vector< 3 > &v2) |
3D only: get the cross product of two vectors More... | |
CoordType | Dot (const Vector &v1, const Vector &v2) |
The dot product of two vectors. More... | |
CoordType | Angle (const Vector &v, const Vector &u) |
The angle between two vectors. More... | |
A dim dimensional vector.
This class implements the 'generic' subset of the interface in the fake class Shape.
|
inline |
Construct an uninitialized vector.
Definition at line 125 of file vector.h.
Referenced by WFMath::Vector< 3 >::mirror(), and WFMath::Vector< 3 >::Vector().
|
default |
Construct a copy of a vector.
|
inlineexplicit |
Construct a vector from an object passed by Atlas.
Definition at line 103 of file atlasconv.h.
|
explicit |
Construct a vector from a point.
Definition at line 46 of file vector_funcs.h.
WFMath::Vector< dim >::Vector | ( | CoordType | x, |
CoordType | y | ||
) |
2D only: construct a vector from (x, y) coordinates
WFMath::Vector< dim >::Vector | ( | CoordType | x, |
CoordType | y, | ||
CoordType | z | ||
) |
3D only: construct a vector from (x, y, z) coordinates
void WFMath::Vector< dim >::asPolar | ( | CoordType & | r, |
CoordType & | theta | ||
) | const |
2D only: convert a vector to polar coordinates
Referenced by WFMath::Vector< 3 >::mirrorY().
void WFMath::Vector< dim >::asPolar | ( | CoordType & | r, |
CoordType & | theta, | ||
CoordType & | z | ||
) | const |
3D only: convert a vector to polar coordinates
void WFMath::Vector< dim >::asSpherical | ( | CoordType & | r, |
CoordType & | theta, | ||
CoordType & | phi | ||
) | const |
3D only: convert a vector to shperical coordinates
Referenced by WFMath::Vector< 3 >::mirrorY().
|
inline |
Set the vector's value to that given by an Atlas object.
Definition at line 109 of file atlasconv.h.
Referenced by WFMath::RotBox< dim >::fromAtlas(), and WFMath::Vector< 3 >::Vector().
|
inline |
The magnitude of a vector.
Definition at line 217 of file vector.h.
Referenced by WFMath::Vector< 3 >::normalize(), and WFMath::Quaternion::rotation().
|
inline |
|
inline |
|
inline |
Reflect a vector in all directions simultaneously.
This is a nice way to implement the parity operation if dim is odd.
Definition at line 279 of file vector.h.
Referenced by WFMath::Vector< 3 >::mirrorX(), and WFMath::Vector< 3 >::mirrorY().
|
inline |
|
inline |
Vector& WFMath::Vector< dim >::mirrorZ | ( | ) |
Flip the z component of a vector.
Referenced by WFMath::Vector< 3 >::mirrorY().
|
inline |
|
inline |
|
inline |
Vector& WFMath::Vector< dim >::polar | ( | CoordType | r, |
CoordType | theta | ||
) |
2D only: construct a vector from polar coordinates
Referenced by WFMath::Vector< 3 >::mirrorY().
Vector& WFMath::Vector< dim >::polar | ( | CoordType | r, |
CoordType | theta, | ||
CoordType | z | ||
) |
3D only: construct a vector from polar coordinates
Vector< dim > & WFMath::Vector< dim >::rotate | ( | int | axis1, |
int | axis2, | ||
CoordType | theta | ||
) |
Rotate the vector in the (axis1, axis2) plane by the angle theta.
Definition at line 174 of file vector_funcs.h.
Referenced by WFMath::Vector< 3 >::mirror(), and WFMath::Vector< 3 >::normalize().
Vector< dim > & WFMath::Vector< dim >::rotate | ( | const Vector< dim > & | v1, |
const Vector< dim > & | v2, | ||
CoordType | theta | ||
) |
Rotate the vector in the (v1, v2) plane by the angle theta.
This throws CollinearVectors if v1 and v2 are parallel.
Definition at line 189 of file vector_funcs.h.
Vector< dim > & WFMath::Vector< dim >::rotate | ( | const RotMatrix< dim > & | m | ) |
Rotate the vector using a matrix.
Definition at line 197 of file vector_funcs.h.
Vector& WFMath::Vector< dim >::rotate | ( | CoordType | theta | ) |
2D only: rotate a vector by an angle theta
Vector& WFMath::Vector< dim >::rotate | ( | const Vector< dim > & | axis, |
CoordType | theta | ||
) |
3D only: rotate a vector about the i'th axis by an angle theta
Vector& WFMath::Vector< dim >::rotate | ( | const Quaternion & | q | ) |
3D only: rotate a vector using a Quaternion
Vector& WFMath::Vector< dim >::rotateX | ( | CoordType | theta | ) |
3D only: rotate a vector about the x axis by an angle theta
Referenced by WFMath::Vector< 3 >::mirror().
Vector& WFMath::Vector< dim >::rotateY | ( | CoordType | theta | ) |
3D only: rotate a vector about the y axis by an angle theta
Referenced by WFMath::Vector< 3 >::mirror().
Vector& WFMath::Vector< dim >::rotateZ | ( | CoordType | theta | ) |
3D only: rotate a vector about the z axis by an angle theta
Referenced by WFMath::Vector< 3 >::mirror().
|
inline |
make isValid() return true if you've initialized the vector by hand
Definition at line 154 of file vector.h.
Referenced by WFMath::RotMatrix< dim >::column(), WFMath::Cross(), WFMath::Quaternion::fromRotMatrix(), WFMath::Quaternion::Quaternion(), WFMath::Quaternion::rotation(), and WFMath::RotMatrix< dim >::row().
CoordType WFMath::Vector< dim >::sloppyMag | ( | ) | const |
An approximation to the magnitude of a vector.
The sloppyMag() function gives a value between the true magnitude and sloppyMagMax multiplied by the true magnitude. sloppyNorm() uses sloppyMag() to normalize the vector. This is currently only implemented for dim = {1, 2, 3}. For all current implementations, sloppyMagMax is greater than or equal to one. The constant sloppyMagMaxSqrt is provided for those who want to most closely approximate the true magnitude, without caring whether it's too low or too high.
Referenced by WFMath::Vector< 3 >::normalize().
|
static |
The maximum ratio of the return value of sloppyMag() to the true magnitude.
Referenced by WFMath::Vector< 3 >::normalize(), and WFMath::Parallel().
|
static |
The square root of sloppyMagMax()
This is provided for people who want to obtain maximum accuracy from sloppyMag(), without caring whether the answer is high or low. The result sloppyMag()/sloppyMagMaxSqrt() will be within sloppyMagMaxSqrt() of the true magnitude.
Referenced by WFMath::Vector< 3 >::normalize(), and WFMath::Parallel().
Vector< dim > & WFMath::Vector< dim >::sloppyNorm | ( | CoordType | norm = 1.0 | ) |
Approximately normalize a vector.
Normalize a vector using sloppyMag() instead of the true magnitude. The new length of the vector will be between norm/sloppyMagMax() and norm.
Definition at line 138 of file vector_funcs.h.
Referenced by WFMath::Vector< 3 >::normalize().
Vector& WFMath::Vector< dim >::spherical | ( | CoordType | r, |
CoordType | theta, | ||
CoordType | phi | ||
) |
3D only: construct a vector from shperical coordinates
Referenced by WFMath::Vector< 3 >::mirrorY().
CoordType WFMath::Vector< dim >::sqrMag | ( | ) | const |
The squared magnitude of a vector.
Definition at line 220 of file vector_funcs.h.
Referenced by WFMath::Vector< 3 >::mag(), WFMath::RotMatrix< dim >::mirror(), WFMath::Quaternion::normalize(), WFMath::Vector< 3 >::operator[](), WFMath::Quaternion::rotation(), and WFMath::RotMatrix< dim >::rotation().
|
inline |
Create an Atlas object from the vector.
Definition at line 122 of file atlasconv.h.
Referenced by WFMath::Vector< 3 >::Vector().
|
inline |
Access the first component of a vector.
Definition at line 313 of file vector.h.
Referenced by WFMath::Vector< 3 >::mirror().
|
inline |
|
inline |
Access the second component of a vector.
Definition at line 317 of file vector.h.
Referenced by WFMath::Vector< 3 >::mirror().
|
inline |
CoordType WFMath::Vector< dim >::z | ( | ) | const |
Access the third component of a vector.
Referenced by WFMath::Vector< 3 >::mirror(), WFMath::Vector< 3 >::mirrorY(), and WFMath::Vector< 3 >::y().
CoordType& WFMath::Vector< dim >::z | ( | ) |
Access the third component of a vector.
|
static |
Provides a global instance preset to zero.
Definition at line 54 of file vector_funcs.h.
Referenced by WFMath::Vector< 3 >::Vector().
Vector< dim > & WFMath::Vector< dim >::zero | ( | ) |
Zero the components of a vector.
Definition at line 148 of file vector_funcs.h.
Referenced by WFMath::Vector< 3 >::setValid().
The angle between two vectors.
Definition at line 160 of file vector_funcs.h.
2D only: get the z component of the cross product of two vectors
Definition at line 102 of file vector.cpp.
Referenced by WFMath::Vector< 3 >::mirrorY(), and WFMath::Vector< 3 >::operator[]().
3D only: get the cross product of two vectors
Definition at line 109 of file vector.cpp.
The dot product of two vectors.
Definition at line 206 of file vector_funcs.h.
Referenced by WFMath::Vector< 3 >::mirror(), and WFMath::Parallel().
returns m^-1 * v
Definition at line 177 of file rotmatrix_funcs.h.
Multiply the magnitude of v by d.
Definition at line 103 of file vector_funcs.h.
Referenced by WFMath::Vector< 3 >::mirror().
Add the second vector to the first.
Definition at line 79 of file vector_funcs.h.
Shift a point by a vector.
Definition at line 100 of file point_funcs.h.
Reverse the direction of a vector.
Definition at line 124 of file vector_funcs.h.
Find the vector which gives the offset between two points.
Definition at line 86 of file point_funcs.h.
Subtract the second vector from the first.
Definition at line 91 of file vector_funcs.h.
Referenced by WFMath::Vector< 3 >::mirror().
Shift a point by a vector, in the opposite direction.
Definition at line 112 of file point_funcs.h.
Divide the magnitude of v by d.
Definition at line 113 of file vector_funcs.h.
returns m * v
Definition at line 160 of file rotmatrix_funcs.h.