30 #ifndef WFMATH_VECTOR_H
31 #define WFMATH_VECTOR_H
33 #include <wfmath/const.h>
42 Vector<dim>& operator+=(Vector<dim>& v1,
const Vector<dim>& v2);
44 Vector<dim>& operator-=(Vector<dim>& v1,
const Vector<dim>& v2);
46 Vector<dim>& operator*=(Vector<dim>& v,
CoordType d);
48 Vector<dim>& operator/=(Vector<dim>& v,
CoordType d);
51 Vector<dim> operator+(
const Vector<dim>& v1,
const Vector<dim>& v2);
53 Vector<dim> operator-(
const Vector<dim>& v1,
const Vector<dim>& v2);
55 Vector<dim> operator-(
const Vector<dim>& v);
61 Vector<dim> operator/(
const Vector<dim>& v,
CoordType d);
64 CoordType Dot(
const Vector<dim>& v1,
const Vector<dim>& v2);
67 CoordType Angle(
const Vector<dim>& v,
const Vector<dim>& u);
72 Vector<dim>
Prod(
const RotMatrix<dim>& m,
const Vector<dim>& v);
75 Vector<dim>
InvProd(
const RotMatrix<dim>& m,
const Vector<dim>& v);
81 Vector<dim>
Prod(
const Vector<dim>& v,
const RotMatrix<dim>& m);
84 Vector<dim>
ProdInv(
const Vector<dim>& v,
const RotMatrix<dim>& m);
88 Vector<dim>
operator*(
const RotMatrix<dim>& m,
const Vector<dim>& v);
91 Vector<dim>
operator*(
const Vector<dim>& v,
const RotMatrix<dim>& m);
94 Vector<dim> operator-(
const Point<dim>& c1,
const Point<dim>& c2);
96 Point<dim> operator+(
const Point<dim>& c,
const Vector<dim>& v);
98 Point<dim> operator-(
const Point<dim>& c,
const Vector<dim>& v);
100 Point<dim> operator+(
const Vector<dim>& v,
const Point<dim>& c);
103 Point<dim>& operator+=(Point<dim>& p,
const Vector<dim>& v);
105 Point<dim>& operator-=(Point<dim>& p,
const Vector<dim>& v);
108 std::ostream& operator<<(std::ostream& os, const Vector<dim>& v);
110 std::istream& operator>>(std::istream& is, Vector<dim>& v);
112 template<
typename Shape>
120 template<
int dim = 3>
122 friend class ZeroPrimitive<
Vector<dim> >;
129 explicit Vector(
const AtlasInType& a);
138 friend std::ostream& operator<< <dim>(std::ostream& os,
const Vector& v);
139 friend std::istream&
operator>> <dim>(std::istream& is,
Vector& v);
148 bool isEqualTo(
const Vector& v,
double epsilon = WFMATH_EPSILON)
const;
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;}
217 {
CoordType themag =
mag();
return (*
this *= norm / themag);}
271 {
return operator-=(*
this, 2 * v *
Dot(v, *
this) / v.sqrMag());}
344 double _scaleEpsilon(
const Vector& v,
double epsilon = WFMATH_EPSILON)
const
345 {
return _ScaleEpsilon(m_elem, v.m_elem, dim, epsilon);}
347 const CoordType* elements()
const {
return m_elem;}
357 Vector<3>
Cross(
const Vector<3>& v1,
const Vector<3>& v2);
366 bool Parallel(
const Vector<dim>& v1,
const Vector<dim>& v2,
bool& same_dir);
373 bool Parallel(
const Vector<dim>& v1,
const Vector<dim>& v2);
377 bool Perpendicular(
const Vector<dim>& v1,
const Vector<dim>& v2);
381 #endif // WFMATH_VECTOR_H
Vector()
Construct an uninitialized vector.
Definition: vector.h:125
Vector & mirror(const int i)
Reflect a vector in the direction of the i'th axis.
Definition: vector.h:268
static const Vector< dim > & ZERO()
Provides a global instance preset to zero.
Definition: vector_funcs.h:60
Vector & rotateZ(CoordType theta)
3D only: rotate a vector about the z axis by an angle theta
Vector & polar(CoordType r, CoordType theta)
2D only: construct a vector from polar coordinates
CoordType operator[](const int i) const
Get the i'th element of the vector.
Definition: vector.h:188
Vector & mirror()
Reflect a vector in all directions simultaneously.
Definition: vector.h:276
A dim dimensional rotation matrix. Technically, a member of the group O(dim).
Definition: const.h:53
CoordType sqrMag() const
The squared magnitude of a vector.
Definition: vector_funcs.h:284
Vector & rotateY(CoordType theta)
3D only: rotate a vector about the y axis by an angle theta
Vector & mirrorY()
Flip the y component of a vector.
Definition: vector.h:325
Vector & mirror(const Vector &v)
Reflect a vector in the direction specified by v.
Definition: vector.h:270
static const CoordType sloppyMagMaxSqrt()
The square root of sloppyMagMax()
Vector & rotate(int axis1, int axis2, CoordType theta)
Rotate the vector in the (axis1, axis2) plane by the angle theta.
Definition: vector_funcs.h:238
Vector & rotateX(CoordType theta)
3D only: rotate a vector about the x axis by an angle theta
bool Perpendicular(const Vector< dim > &v1, const Vector< dim > &v2)
Check if two vectors are perpendicular.
Definition: vector_funcs.h:315
RotMatrix< dim > InvProd(const RotMatrix< dim > &m1, const RotMatrix< dim > &m2)
returns m1^-1 * m2
Definition: rotmatrix_funcs.h:133
Vector & mirrorX()
Flip the x component of a vector.
Definition: vector.h:323
Vector & spherical(CoordType r, CoordType theta, CoordType phi)
3D only: construct a vector from shperical coordinates
void setValid(bool valid=true)
make isValid() return true if you've initialized the vector by hand
Definition: vector.h:154
Vector & sloppyNorm(CoordType norm=1.0)
Approximately normalize a vector.
Definition: vector_funcs.h:202
CoordType & y()
Access the second component of a vector.
Definition: vector.h:316
CoordType Cross(const Vector< 2 > &v1, const Vector< 2 > &v2)
2D only: get the z component of the cross product of two vectors
A dim dimensional vector.
Definition: const.h:55
CoordType & z()
Access the third component of a vector.
Definition: vector.h:320
CoordType & x()
Access the first component of a vector.
Definition: vector.h:312
Vector & zero()
Zero the components of a vector.
Definition: vector_funcs.h:212
void asSpherical(CoordType &r, CoordType &theta, CoordType &phi) const
3D only: convert a vector to shperical coordinates
void asPolar(CoordType &r, CoordType &theta) const
2D only: convert a vector to polar coordinates
float CoordType
Basic floating point type.
Definition: const.h:79
friend CoordType Dot(const Vector &v1, const Vector &v2)
The dot product of two vectors.
Definition: vector_funcs.h:270
Vector & normalize(CoordType norm=1.0)
Normalize a vector.
Definition: vector.h:216
friend Vector & operator-=(Vector &v1, const Vector &v2)
Subtract the second vector from the first.
Definition: vector_funcs.h:106
RotMatrix< dim > operator*(const RotMatrix< dim > &m1, const RotMatrix< dim > &m2)
returns m1 * m2
Definition: rotmatrix_funcs.h:223
CoordType x() const
Access the first component of a vector.
Definition: vector.h:310
friend Vector & operator*=(Vector &v, CoordType d)
Multiply the magnitude of v by d.
Definition: vector_funcs.h:118
CoordType & operator[](const int i)
Get the i'th element of the vector.
Definition: vector.h:190
AtlasOutType toAtlas() const
Create an Atlas object from the vector.
Definition: atlasconv.h:124
CoordType y() const
Access the second component of a vector.
Definition: vector.h:314
Vector & mirrorZ()
Flip the z component of a vector.
Definition: vector.h:327
void fromAtlas(const AtlasInType &a)
Set the vector's value to that given by an Atlas object.
Definition: atlasconv.h:117
RotMatrix< dim > ProdInv(const RotMatrix< dim > &m1, const RotMatrix< dim > &m2)
returns m1 * m2^-1
Definition: rotmatrix_funcs.h:111
A normalized quaterion.
Definition: quaternion.h:39
RotMatrix< dim > Prod(const RotMatrix< dim > &m1, const RotMatrix< dim > &m2)
returns m1 * m2
Definition: rotmatrix_funcs.h:89
CoordType z() const
Access the third component of a vector.
Definition: vector.h:318
A dim dimensional point.
Definition: const.h:50
bool Parallel(const Vector< dim > &v1, const Vector< dim > &v2, bool &same_dir)
Check if two vectors are parallel.
Definition: vector_funcs.h:297
CoordType sloppyMag() const
An approximation to the magnitude of a vector.
CoordType mag() const
The magnitude of a vector.
Definition: vector.h:214
static const CoordType sloppyMagMax()
The maximum ratio of the return value of sloppyMag() to the true magnitude.