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] = p.elements()[i];
64 if (!v.m_valid || !m_valid) {
68 CoordType delta = _ScaleEpsilon(m_elem, v.m_elem, dim, epsilon);
69 for(
int i = 0; i < dim; ++i) {
70 if(std::fabs(m_elem[i] - v.m_elem[i]) > delta) {
81 v1.m_valid = v1.m_valid && v2.m_valid;
83 for(
int i = 0; i < dim; ++i) {
84 v1.m_elem[i] += v2.m_elem[i];
93 v1.m_valid = v1.m_valid && v2.m_valid;
95 for(
int i = 0; i < dim; ++i) {
96 v1.m_elem[i] -= v2.m_elem[i];
105 for(
int i = 0; i < dim; ++i) {
115 for(
int i = 0; i < dim; ++i) {
128 ans.m_valid = v.m_valid;
130 for(
int i = 0; i < dim; ++i) {
131 ans.m_elem[i] = -v.m_elem[i];
142 assert(
"need nonzero length vector" && mag > norm / std::numeric_limits<CoordType>::max());
144 return (*
this *= norm / mag);
152 for(
int i = 0; i < dim; ++i) {
176 assert(axis1 >= 0 && axis2 >= 0 && axis1 < dim && axis2 < dim && axis1 != axis2);
178 CoordType tmp1 = m_elem[axis1], tmp2 = m_elem[axis2];
180 ctheta = std::cos(theta);
182 m_elem[axis1] = tmp1 * ctheta - tmp2 * stheta;
183 m_elem[axis2] = tmp2 * ctheta + tmp1 * stheta;
193 return operator=(
Prod(*
this, m.
rotation(v1, v2, theta)));
199 return *
this =
Prod(*
this, m);
208 double delta = _ScaleEpsilon(v1.m_elem, v2.m_elem, dim);
212 for(
int i = 0; i < dim; ++i) {
213 ans += v1.m_elem[i] * v2.m_elem[i];
216 return (std::fabs(ans) >= delta) ? ans : 0;
224 for(
int i = 0; i < dim; ++i) {
226 ans += m_elem[i] * m_elem[i];
237 for(
int i = 0; i < dim; ++i) {
238 CoordType val1 = std::fabs(v1[i]), val2 = std::fabs(v2[i]);
249 (void) std::frexp(max1, &exp1);
250 (void) std::frexp(max2, &exp2);
252 return std::fabs(Dot(v1, v2)) < std::ldexp(numeric_constants<CoordType>::epsilon(), exp1 + exp2);
284 {
return std::fabs(m_elem[0]);}
287 {m_elem[0] = x; m_elem[1] = y;}
288 template<>
Vector<3>::Vector(CoordType x, CoordType y, CoordType z) : m_valid(true)
289 {m_elem[0] = x; m_elem[1] = y; m_elem[2] = z;}
292 {
return rotate(0, 1, theta);}
295 {
return rotate(1, 2, theta);}
297 {
return rotate(2, 0, theta);}
299 {
return rotate(0, 1, theta);}
A dim dimensional rotation matrix. Technically, a member of the group O(dim).
RotMatrix & rotation(int i, int j, CoordType theta)
set the matrix to a rotation by the angle theta in the (i, j) plane
Vector & rotateY(CoordType theta)
3D only: rotate a vector about the y axis by an angle theta
Vector & rotateZ(CoordType theta)
3D only: rotate a vector about the z axis by an angle theta
Vector()
Construct an uninitialized vector.
static const Vector< dim > & ZERO()
Provides a global instance preset to zero.
Vector & rotateX(CoordType theta)
3D only: rotate a vector about the x axis by an angle theta
Vector & rotate(int axis1, int axis2, CoordType theta)
Rotate the vector in the (axis1, axis2) plane by the angle theta.
CoordType sqrMag() const
The squared magnitude of a vector.
Utility class for providing zero primitives. This class will only work with simple structures such as...
const Shape & getShape() const
Gets the zeroed shape.
Generic library namespace.
double CoordType
Basic floating point type.
RotMatrix< dim > Prod(const RotMatrix< dim > &m1, const RotMatrix< dim > &m2)
returns m1 * m2
bool Perpendicular(const Vector< dim > &v1, const Vector< dim > &v2)
Check if two vectors are perpendicular.