34 #include "vector_funcs.h"
37 #include "quaternion.h"
45 ay = std::fabs(m_elem[1]);
46 const CoordType p = numeric_constants<CoordType>::sqrt2() - 1;
61 ay = std::fabs(m_elem[1]),
62 az = std::fabs(m_elem[2]);
63 const CoordType p = numeric_constants<CoordType>::sqrt2() - 1;
64 const CoordType q = numeric_constants<CoordType>::sqrt3() + 1 - 2 * numeric_constants<CoordType>::sqrt2();
69 if(ax > ay && ax > az)
70 return ax + p * (ay + az) + q * ay * az / ax;
72 return ay + p * (ax + az) + q * ax * az / ay;
74 return az + p * (ax + ay) + q * ax * ay / az;
83 assert(axis_sqr_mag != 0);
85 Vector<3> perp_part = *
this - axis * Dot(*
this, axis) / axis_sqr_mag;
86 Vector<3> rot90 =
Cross(axis, perp_part) / std::sqrt(axis_sqr_mag);
88 *
this += perp_part * (std::cos(theta) - 1) + rot90 * std::sin(theta);
95 *
this = (2 * q.scalar() * q.scalar() - 1) * *
this +
96 2 * q.vector() * Dot(q.vector(), *
this) +
97 2 * q.scalar() *
Cross(q.vector(), *
this);
104 CoordType ans = v1[0] * v2[1] - v2[0] * v1[1];
106 return (ans >= v1._scaleEpsilon(v2)) ? ans : 0;
113 ans.
setValid(v1.isValid() && v2.isValid());
115 ans[0] = v1[1] * v2[2] - v2[1] * v1[2];
116 ans[1] = v1[2] * v2[0] - v2[2] * v1[0];
117 ans[2] = v1[0] * v2[1] - v2[0] * v1[1];
119 double delta = v1._scaleEpsilon(v2);
121 for(
int i = 0; i < 3; ++i)
122 if(std::fabs(ans[i]) < delta)
132 _PolarToCart(d, m_elem);
141 _CartToPolar(m_elem, d);
150 _PolarToCart(d, m_elem);
160 _CartToPolar(m_elem, d);
170 _SphericalToCart(d, m_elem);
177 CoordType& phi)
const
180 _CartToSpherical(m_elem, d);
186 template class Vector<3>;
187 static_assert(std::is_standard_layout<Vector<3>>::value,
"Vector should be standard layout.");
188 static_assert(std::is_trivially_copyable<Vector<3>>::value,
"Vector should be trivially copyable.");
189 template class Vector<2>;
190 static_assert(std::is_standard_layout<Vector<2>>::value,
"Vector should be standard layout.");
191 static_assert(std::is_trivially_copyable<Vector<2>>::value,
"Vector should be trivially copyable.");
193 template Vector<3>& operator-=(Vector<3>& v1,
const Vector<3>& v2);
194 template Vector<2>& operator-=(Vector<2>& v1,
const Vector<2>& v2);
196 template Vector<3>& operator+=(Vector<3>& v1,
const Vector<3>& v2);
197 template Vector<2>& operator+=(Vector<2>& v1,
const Vector<2>& v2);
199 template Vector<3>& operator*=(Vector<3>& v1, CoordType d);
200 template Vector<2>& operator*=(Vector<2>& v1, CoordType d);
202 template Vector<3>& operator/=(Vector<3>& v1, CoordType d);
203 template Vector<2>& operator/=(Vector<2>& v1, CoordType d);
205 template CoordType Dot<3>(
const Vector<3> &,
const Vector<3> &);
Vector & polar(CoordType r, CoordType theta)
2D only: construct a vector from polar coordinates
void asPolar(CoordType &r, CoordType &theta) const
2D only: convert a vector to polar coordinates
void asSpherical(CoordType &r, CoordType &theta, CoordType &phi) const
3D only: convert a vector to shperical coordinates
CoordType sloppyMag() const
An approximation to the magnitude of a vector.
void setValid(bool valid=true)
make isValid() return true if you've initialized the vector by hand
Vector & spherical(CoordType r, CoordType theta, CoordType phi)
3D only: construct a vector from shperical coordinates
Vector & rotate(int axis1, int axis2, CoordType theta)
Rotate the vector in the (axis1, axis2) plane by the angle theta.
Generic library namespace.
double CoordType
Basic floating point type.
CoordType Cross(const Vector< 2 > &v1, const Vector< 2 > &v2)
2D only: get the z component of the cross product of two vectors