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];
82 bool Vector<dim>::isEqualTo(
const Vector<dim>& v, CoordType epsilon)
const 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);
295 CoordType& phi)
const;
300 template<> CoordType Vector<1>::sloppyMag()
const 301 {
return std::fabs(m_elem[0]);}
304 {m_elem[0] = x; m_elem[1] = y;}
305 template<>
Vector<3>::Vector(CoordType x, CoordType y, CoordType z) : m_valid(true)
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 Vector()
Construct an uninitialized vector.
Definition: vector.h:125
Generic library namespace.
Definition: atlasconv.h:45
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
void asSpherical(CoordType &r, CoordType &theta, CoordType &phi) const
3D only: convert a vector to shperical coordinates
A dim dimensional rotation matrix. Technically, a member of the group O(dim).
Definition: const.h:53
Vector & rotateY(CoordType theta)
3D only: rotate a vector about the y axis by an angle theta
CoordType sqrMag() const
The squared magnitude of a vector.
Definition: vector_funcs.h:237
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
void asPolar(CoordType &r, CoordType &theta) const
2D only: convert a vector to polar coordinates
const Shape & getShape() const
Gets the zeroed shape.
Definition: zero.h:53
A dim dimensional vector.
Definition: const.h:55
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
float CoordType
Basic floating point type.
Definition: const.h:140
CoordType sloppyMag() const
An approximation to the magnitude of a vector.
Utility class for providing zero primitives. This class will only work with simple structures such as...
Definition: point.h:87
A normalized quaterion.
Definition: quaternion.h:35
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