30 #ifndef WFMATH_VECTOR_FUNCS_H 31 #define WFMATH_VECTOR_FUNCS_H 33 #include <wfmath/vector.h> 34 #include <wfmath/rotmatrix.h> 35 #include <wfmath/zero.h> 48 for(
int i = 0; i < dim; ++i) {
49 m_elem[i] = v.m_elem[i];
56 for(
int i = 0; i < dim; ++i) {
57 m_elem[i] = p.elements()[i];
74 for(
int i = 0; i < dim; ++i) {
75 m_elem[i] = v.m_elem[i];
84 double delta = _ScaleEpsilon(m_elem, v.m_elem, dim, epsilon);
86 for(
int i = 0; i < dim; ++i) {
87 if(std::fabs(m_elem[i] - v.m_elem[i]) > delta) {
98 v1.m_valid = v1.m_valid && v2.m_valid;
100 for(
int i = 0; i < dim; ++i) {
101 v1.m_elem[i] += v2.m_elem[i];
110 v1.m_valid = v1.m_valid && v2.m_valid;
112 for(
int i = 0; i < dim; ++i) {
113 v1.m_elem[i] -= v2.m_elem[i];
122 for(
int i = 0; i < dim; ++i) {
132 for(
int i = 0; i < dim; ++i) {
145 ans.m_valid = v.m_valid;
147 for(
int i = 0; i < dim; ++i) {
148 ans.m_elem[i] = -v.m_elem[i];
159 assert(
"need nonzero length vector" && mag > norm / std::numeric_limits<CoordType>::max());
161 return (*
this *= norm / mag);
169 for(
int i = 0; i < dim; ++i) {
193 assert(axis1 >= 0 && axis2 >= 0 && axis1 < dim && axis2 < dim && axis1 != axis2);
195 CoordType tmp1 = m_elem[axis1], tmp2 = m_elem[axis2];
197 ctheta = std::cos(theta);
199 m_elem[axis1] = tmp1 * ctheta - tmp2 * stheta;
200 m_elem[axis2] = tmp2 * ctheta + tmp1 * stheta;
210 return operator=(
Prod(*
this, m.
rotation(v1, v2, theta)));
216 return *
this =
Prod(*
this, m);
225 double delta = _ScaleEpsilon(v1.m_elem, v2.m_elem, dim);
229 for(
int i = 0; i < dim; ++i) {
230 ans += v1.m_elem[i] * v2.m_elem[i];
233 return (std::fabs(ans) >= delta) ? ans : 0;
241 for(
int i = 0; i < dim; ++i) {
243 ans += m_elem[i] * m_elem[i];
254 for(
int i = 0; i < dim; ++i) {
255 CoordType val1 = std::fabs(v1[i]), val2 = std::fabs(v2[i]);
266 (void) std::frexp(max1, &exp1);
267 (void) std::frexp(max2, &exp2);
269 return std::fabs(
Dot(v1, v2)) < std::ldexp(numeric_constants<CoordType>::epsilon(), exp1 + exp2);
301 {
return std::fabs(m_elem[0]);}
304 {m_elem[0] =
x; m_elem[1] =
y;}
306 {m_elem[0] =
x; m_elem[1] =
y; m_elem[2] =
z;}
309 {
return rotate(0, 1, theta);}
312 {
return rotate(1, 2, theta);}
314 {
return rotate(2, 0, theta);}
316 {
return rotate(0, 1, theta);}
321 #endif // WFMATH_VECTOR_FUNCS_H friend Vector Prod(const RotMatrix< dim > &m, const Vector &v)
returns m * v
Definition: rotmatrix_funcs.h:177
Vector()
Construct an uninitialized vector.
Definition: vector.h:125
friend Vector & operator+=(Vector &v1, const Vector &v2)
Add the second vector to the first.
Definition: vector_funcs.h:96
Generic library namespace.
Definition: atlasconv.h:45
friend CoordType Angle(const Vector &v, const Vector &u)
The angle between two vectors.
Definition: vector_funcs.h:177
static const Vector< dim > & ZERO()
Provides a global instance preset to zero.
Definition: vector_funcs.h:62
const Shape & getShape() const
Gets the zeroed shape.
Definition: zero.h:53
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
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:237
Vector & rotateY(CoordType theta)
3D only: rotate a vector about the y axis by an angle theta
friend Vector & operator/=(Vector &v, CoordType d)
Divide the magnitude of v by d.
Definition: vector_funcs.h:130
Vector & rotate(int axis1, int axis2, CoordType theta)
Rotate the vector in the (axis1, axis2) plane by the angle theta.
Definition: vector_funcs.h:191
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:250
Vector & spherical(CoordType r, CoordType theta, CoordType phi)
3D only: construct a vector from shperical coordinates
Vector & sloppyNorm(CoordType norm=1.0)
Approximately normalize a vector.
Definition: vector_funcs.h:155
A dim dimensional vector.
Definition: const.h:55
Vector & zero()
Zero the components of a vector.
Definition: vector_funcs.h:165
void asSpherical(CoordType &r, CoordType &theta, CoordType &phi) const
3D only: convert a vector to shperical coordinates
RotMatrix & rotation(const int i, const int j, CoordType theta)
set the matrix to a rotation by the angle theta in the (i, j) plane
Definition: rotmatrix_funcs.h:360
void asPolar(CoordType &r, CoordType &theta) const
2D only: convert a vector to polar coordinates
float CoordType
Basic floating point type.
Definition: const.h:140
friend CoordType Dot(const Vector &v1, const Vector &v2)
The dot product of two vectors.
Definition: vector_funcs.h:223
Utility class for providing zero primitives. This class will only work with simple structures such as...
Definition: point.h:87
friend Vector operator-(const Vector &v1, const Vector &v2)
Take the difference of two vectors.
Definition: vector.h:410
friend Vector & operator-=(Vector &v1, const Vector &v2)
Subtract the second vector from the first.
Definition: vector_funcs.h:108
CoordType x() const
Access the first component of a vector.
Definition: vector.h:313
friend Vector & operator*=(Vector &v, CoordType d)
Multiply the magnitude of v by d.
Definition: vector_funcs.h:120
CoordType y() const
Access the second component of a vector.
Definition: vector.h:317
A normalized quaterion.
Definition: quaternion.h:35
CoordType z() const
Access the third component of a vector.
A dim dimensional point.
Definition: const.h:50
CoordType sloppyMag() const
An approximation to the magnitude of a vector.
CoordType mag() const
The magnitude of a vector.
Definition: vector.h:217