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>
46 for(
int i = 0; i < dim; ++i) {
47 m_elem[i] = v.m_elem[i];
54 for(
int i = 0; i < dim; ++i) {
55 m_elem[i] = p.elements()[i];
72 for(
int i = 0; i < dim; ++i) {
73 m_elem[i] = v.m_elem[i];
80 bool Vector<dim>::isEqualTo(
const Vector<dim>& v,
double epsilon)
const
82 double delta = _ScaleEpsilon(m_elem, v.m_elem, dim, epsilon);
84 for(
int i = 0; i < dim; ++i) {
85 if(fabs(m_elem[i] - v.m_elem[i]) > delta) {
96 v1.m_valid = v1.m_valid && v2.m_valid;
98 for(
int i = 0; i < dim; ++i) {
99 v1.m_elem[i] += v2.m_elem[i];
108 v1.m_valid = v1.m_valid && v2.m_valid;
110 for(
int i = 0; i < dim; ++i) {
111 v1.m_elem[i] -= v2.m_elem[i];
120 for(
int i = 0; i < dim; ++i) {
130 for(
int i = 0; i < dim; ++i) {
192 ans.m_valid = v.m_valid;
194 for(
int i = 0; i < dim; ++i) {
195 ans.m_elem[i] = -v.m_elem[i];
206 assert(
"need nonzero length vector" && mag > norm / WFMATH_MAX);
208 return (*
this *= norm / mag);
216 for(
int i = 0; i < dim; ++i) {
240 assert(axis1 >= 0 && axis2 >= 0 && axis1 < dim && axis2 < dim && axis1 != axis2);
242 CoordType tmp1 = m_elem[axis1], tmp2 = m_elem[axis2];
244 ctheta = std::cos(theta);
246 m_elem[axis1] = tmp1 * ctheta - tmp2 * stheta;
247 m_elem[axis2] = tmp2 * ctheta + tmp1 * stheta;
257 return operator=(
Prod(*
this, m.
rotation(v1, v2, theta)));
263 return *
this =
Prod(*
this, m);
272 double delta = _ScaleEpsilon(v1.m_elem, v2.m_elem, dim);
276 for(
int i = 0; i < dim; ++i) {
277 ans += v1.m_elem[i] * v2.m_elem[i];
280 return (fabs(ans) >= delta) ? ans : 0;
288 for(
int i = 0; i < dim; ++i) {
290 ans += m_elem[i] * m_elem[i];
301 same_dir = (dot > 0);
317 double max1 = 0, max2 = 0;
319 for(
int i = 0; i < dim; ++i) {
320 double val1 = fabs(v1[i]), val2 = fabs(v2[i]);
331 (void) frexp(max1, &exp1);
332 (void) frexp(max2, &exp2);
334 return fabs(Dot(v1, v2)) < ldexp(WFMATH_EPSILON, exp1 + exp2);
346 return (CoordType) 1.082392200292393968799446410733;
352 return (CoordType) 1.145934719303161490541433900265;
356 const CoordType Vector<1>::sloppyMagMaxSqrt()
358 return (CoordType) 1;
364 return (CoordType) 1.040380795811030899095785063701;
370 return (CoordType) 1.070483404496847625250328653179;
396 CoordType& phi)
const;
401 template<>
CoordType Vector<1>::sloppyMag()
const
402 {
return std::fabs(m_elem[0]);}
405 {m_elem[0] = x; m_elem[1] = y;}
406 template<>
Vector<3>::Vector(CoordType x, CoordType y, CoordType z) : m_valid(true)
407 {m_elem[0] = x; m_elem[1] = y; m_elem[2] = z;}
410 {
return rotate(0, 1, theta);}
413 {
return rotate(1, 2, theta);}
415 {
return rotate(2, 0, theta);}
417 {
return rotate(0, 1, theta);}
422 #endif // WFMATH_VECTOR_FUNCS_H
Vector()
Construct an uninitialized vector.
Definition: vector.h:125
static const Vector< dim > & ZERO()
Provides a global instance preset to zero.
Definition: vector_funcs.h:60
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:284
Vector & rotateY(CoordType theta)
3D only: rotate a vector about the y axis by an angle theta
bool Equal(const C &c1, const C &c2, double epsilon=WFMATH_EPSILON)
Test for equality up to precision epsilon.
Definition: const.h:103
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
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:202
A dim dimensional vector.
Definition: const.h:55
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
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:79
Utility class for providing zero primitives. This class will only work with simple structures such as...
Definition: point.h:87
RotMatrix< dim > operator*(const RotMatrix< dim > &m1, const RotMatrix< dim > &m2)
returns m1 * m2
Definition: rotmatrix_funcs.h:223
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
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.
static const CoordType sloppyMagMax()
The maximum ratio of the return value of sloppyMag() to the true magnitude.