30#ifndef WFMATH_VECTOR_H
31#define WFMATH_VECTOR_H
33#include <wfmath/const.h>
42Vector<dim>& operator+=(Vector<dim>& v1,
const Vector<dim>& v2);
44Vector<dim>& operator-=(Vector<dim>& v1,
const Vector<dim>& v2);
46Vector<dim>& operator*=(Vector<dim>& v,
CoordType d);
48Vector<dim>& operator/=(Vector<dim>& v,
CoordType d);
51Vector<dim> operator+(
const Vector<dim>& v1,
const Vector<dim>& v2);
53Vector<dim> operator-(
const Vector<dim>& v1,
const Vector<dim>& v2);
55Vector<dim> operator-(
const Vector<dim>& v);
61Vector<dim> operator/(
const Vector<dim>& v,
CoordType d);
64CoordType Dot(
const Vector<dim>& v1,
const Vector<dim>& v2);
67CoordType Angle(
const Vector<dim>& v,
const Vector<dim>& u);
72Vector<dim>
Prod(
const RotMatrix<dim>& m,
const Vector<dim>& v);
75Vector<dim>
InvProd(
const RotMatrix<dim>& m,
const Vector<dim>& v);
81Vector<dim>
Prod(
const Vector<dim>& v,
const RotMatrix<dim>& m);
84Vector<dim>
ProdInv(
const Vector<dim>& v,
const RotMatrix<dim>& m);
88Vector<dim>
operator*(
const RotMatrix<dim>& m,
const Vector<dim>& v);
91Vector<dim>
operator*(
const Vector<dim>& v,
const RotMatrix<dim>& m);
94Vector<dim> operator-(
const Point<dim>& c1,
const Point<dim>& c2);
96Point<dim> operator+(
const Point<dim>& c,
const Vector<dim>& v);
98Point<dim> operator-(
const Point<dim>& c,
const Vector<dim>& v);
100Point<dim> operator+(
const Vector<dim>& v,
const Point<dim>& c);
103Point<dim>& operator+=(Point<dim>& p,
const Vector<dim>& v);
105Point<dim>& operator-=(Point<dim>& p,
const Vector<dim>& v);
108std::ostream& operator<<(std::ostream& os,
const Vector<dim>& v);
110std::istream& operator>>(std::istream& is, Vector<dim>& v);
112template<
typename Shape>
138 friend std::ostream& operator<< <dim>(std::ostream& os,
const Vector& v);
139 friend std::istream&
operator>> <dim>(std::istream& is,
Vector& v);
149 bool operator==(
const Vector& v)
const {
return isEqualTo(v);}
150 bool operator!=(
const Vector& v)
const {
return !isEqualTo(v);}
152 bool isValid()
const {
return m_valid;}
154 void setValid(
bool valid =
true) {m_valid = valid;}
220 {
CoordType themag =
mag();
return (*
this *= norm / themag);}
346 const CoordType* elements()
const {
return m_elem;}
350 {
return _ScaleEpsilon(m_elem, v.m_elem, dim, epsilon);}
377Vector<3>
Cross(
const Vector<3>& v1,
const Vector<3>& v2);
386bool Parallel(
const Vector<dim>& v1,
const Vector<dim>& v2,
bool& same_dir);
393bool Parallel(
const Vector<dim>& v1,
const Vector<dim>& v2);
397bool Perpendicular(
const Vector<dim>& v1,
const Vector<dim>& v2);
456 same_dir = (dot > 0);
470inline CoordType Vector<1>::sloppyMagMax()
478 return 1.082392200292393968799446410733f;
484 return 1.145934719303161490541433900265f;
488inline CoordType Vector<1>::sloppyMagMaxSqrt()
496 return 1.040380795811030899095785063701f;
502 return 1.070483404496847625250328653179f;
A dim dimensional rotation matrix. Technically, a member of the group O(dim).
A dim dimensional vector.
Vector & rotateZ(CoordType theta)
3D only: rotate a vector about the z axis by an angle theta
CoordType & z()
Access the third component of a vector.
CoordType z() const
Access the third component of a vector.
void asPolar(CoordType &r, CoordType &theta) const
2D only: convert a vector to polar coordinates
Vector & mirror()
Reflect a vector in all directions simultaneously.
Vector & mirror(const int i)
Reflect a vector in the direction of the i'th axis.
CoordType & x()
Access the first component of a vector.
friend CoordType Cross(const Vector< 2 > &v1, const Vector< 2 > &v2)
2D only: get the z component of the cross product of two vectors
Vector()
Construct an uninitialized vector.
void asSpherical(CoordType &r, CoordType &theta, CoordType &phi) const
3D only: convert a vector to shperical coordinates
friend Vector & operator*=(Vector &v, CoordType d)
Multiply the magnitude of v by d.
static CoordType sloppyMagMax()
The maximum ratio of the return value of sloppyMag() to the true magnitude.
Vector & spherical(CoordType r, CoordType theta, CoordType phi)
3D only: construct a vector from shperical coordinates
Vector & mirrorZ()
Flip the z component of a vector.
Vector(CoordType x, CoordType y, CoordType z)
3D only: construct a vector from (x, y, z) coordinates
Vector(CoordType x, CoordType y)
2D only: construct a vector from (x, y) coordinates
Vector & zero()
Zero the components of a vector.
static CoordType sloppyMagMaxSqrt()
The square root of sloppyMagMax()
static const Vector< dim > & ZERO()
Provides a global instance preset to zero.
Vector & polar(CoordType r, CoordType theta, CoordType z)
3D only: construct a vector from polar coordinates
CoordType & y()
Access the second component of a vector.
Vector & rotate(CoordType theta)
2D only: rotate a vector by an angle theta
Vector & sloppyNorm(CoordType norm=1.0)
Approximately normalize a vector.
void asPolar(CoordType &r, CoordType &theta, CoordType &z) const
3D only: convert a vector to polar coordinates
Vector & mirrorY()
Flip the y component of a vector.
CoordType sloppyMag() const
An approximation to the magnitude of a vector.
CoordType x() const
Access the first component of a vector.
void setValid(bool valid=true)
make isValid() return true if you've initialized the vector by hand
AtlasOutType toAtlas() const
Create an Atlas object from the vector.
friend CoordType Dot(const Vector &v1, const Vector &v2)
The dot product of two vectors.
friend Vector & operator-=(Vector &v1, const Vector &v2)
Subtract the second vector from the first.
Vector & mirror(const Vector &v)
Reflect a vector in the direction specified by v.
CoordType mag() const
The magnitude of a vector.
Vector & rotate(int axis1, int axis2, CoordType theta)
Rotate the vector in the (axis1, axis2) plane by the angle theta.
Vector(const Vector &)=default
Construct a copy of a vector.
Vector & rotate(const Vector &axis, CoordType theta)
3D only: rotate a vector about the i'th axis by an angle theta
CoordType y() const
Access the second component of a vector.
CoordType sqrMag() const
The squared magnitude of a vector.
CoordType operator[](const int i) const
Get the i'th element of the vector.
CoordType & operator[](const int i)
Get the i'th element of the vector.
void fromAtlas(const AtlasInType &a)
Set the vector's value to that given by an Atlas object.
Vector & rotateX(CoordType theta)
3D only: rotate a vector about the x axis by an angle theta
Vector & rotate(const Quaternion &q)
3D only: rotate a vector using a Quaternion
Vector & rotateY(CoordType theta)
3D only: rotate a vector about the y axis by an angle theta
Vector & mirrorX()
Flip the x component of a vector.
Vector & polar(CoordType r, CoordType theta)
2D only: construct a vector from polar coordinates
Vector & normalize(CoordType norm=1.0)
Normalize a vector.
Utility class for providing zero primitives. This class will only work with simple structures such as...
Generic library namespace.
double CoordType
Basic floating point type.
RotMatrix< dim > InvProd(const RotMatrix< dim > &m1, const RotMatrix< dim > &m2)
returns m1^-1 * m2
CoordType Cross(const Vector< 2 > &v1, const Vector< 2 > &v2)
2D only: get the z component of the cross product of two vectors
RotMatrix< dim > Prod(const RotMatrix< dim > &m1, const RotMatrix< dim > &m2)
returns m1 * m2
bool Parallel(const Vector< dim > &v1, const Vector< dim > &v2, bool &same_dir)
Check if two vectors are parallel.
RotMatrix< dim > ProdInv(const RotMatrix< dim > &m1, const RotMatrix< dim > &m2)
returns m1 * m2^-1
bool Perpendicular(const Vector< dim > &v1, const Vector< dim > &v2)
Check if two vectors are perpendicular.
RotMatrix< dim > operator*(const RotMatrix< dim > &m1, const RotMatrix< dim > &m2)
returns m1 * m2