18 template<
class Real__>
47 static const int dim = 4;
65 #define _ORD(i,p,r,f) (((((((i)<<1)+(p))<<1)+(r))<<1)+(f))
77 xyz_s =
_ORD(eulAxX, eulParEven, eulRepNo, eulFrmS),
78 xyx_s =
_ORD(eulAxX, eulParEven, eulRepYes, eulFrmS),
79 xzy_s =
_ORD(eulAxX, eulParOdd, eulRepNo, eulFrmS),
80 xzx_s =
_ORD(eulAxX, eulParOdd, eulRepYes, eulFrmS),
81 yzx_s =
_ORD(eulAxY, eulParEven, eulRepNo, eulFrmS),
82 yzy_s =
_ORD(eulAxY, eulParEven, eulRepYes, eulFrmS),
83 yxz_s =
_ORD(eulAxY, eulParOdd, eulRepNo, eulFrmS),
84 yxy_s =
_ORD(eulAxY, eulParOdd, eulRepYes, eulFrmS),
85 zxy_s =
_ORD(eulAxZ, eulParEven, eulRepNo, eulFrmS),
86 zxz_s =
_ORD(eulAxZ, eulParEven, eulRepYes, eulFrmS),
87 zyx_s =
_ORD(eulAxZ, eulParOdd, eulRepNo, eulFrmS),
88 zyz_s =
_ORD(eulAxZ, eulParOdd, eulRepYes, eulFrmS),
90 zyx_r =
_ORD(eulAxX, eulParEven, eulRepNo, eulFrmR),
91 xyx_r =
_ORD(eulAxX, eulParEven, eulRepYes, eulFrmR),
92 yzx_r =
_ORD(eulAxX, eulParOdd, eulRepNo, eulFrmR),
93 xzx_r =
_ORD(eulAxX, eulParOdd, eulRepYes, eulFrmR),
94 xzy_r =
_ORD(eulAxY, eulParEven, eulRepNo, eulFrmR),
95 yzy_r =
_ORD(eulAxY, eulParEven, eulRepYes, eulFrmR),
96 zxy_r =
_ORD(eulAxY, eulParOdd, eulRepNo, eulFrmR),
97 yxy_r =
_ORD(eulAxY, eulParOdd, eulRepYes, eulFrmR),
98 yxz_r =
_ORD(eulAxZ, eulParEven, eulRepNo, eulFrmR),
99 zxz_r =
_ORD(eulAxZ, eulParEven, eulRepYes, eulFrmR),
100 xyz_r =
_ORD(eulAxZ, eulParOdd, eulRepNo, eulFrmR),
101 zyz_r =
_ORD(eulAxZ, eulParOdd, eulRepYes, eulFrmR)
109 Quat_(Real
x, Real
y, Real
z, Real
w) : x(x), y(y), z(z), w(w) {}
154 -
x*rhs.
z +
y*rhs.
w +
z*rhs.
x +
w*rhs.
y,
155 x*rhs.
y -
y*rhs.
x +
z*rhs.
w +
w*rhs.
z,
156 -
x*rhs.
x -
y*rhs.
y -
z*rhs.
z +
w*rhs.
w);
162 Vec3 uv = qvec.cross(rhs);
163 Vec3 uuv = qvec.cross(uv);
166 return rhs + uv + uuv;
185 operator Real*() {
return &
x; }
186 operator const Real*()
const {
return &
x; }
197 if (l > Real_::zeroTol)
200 return Quat_(-
x*l_inv, -
y*l_inv, -
z*l_inv,
w*l_inv);
216 if (l > Real_::zeroTol)
228 static const Real recurse1 = 0.91521198;
229 static const Real recurse2 = 0.65211970;
232 Real k = sqrtInverse_fast(s);
235 k = k*sqrtInverse_fast(k*k*s);
237 k = k*sqrtInverse_fast(k*k*s);
243 void axisAngle(Vec3& axis, Real& angle)
const;
259 Matrix4&
toMatrix(Matrix4& rot,
bool b3x3 =
false)
const;
267 return (dot >= 0) ? slerp_fast(t, q0, q1, dot) : slerp_fast(t, q0, -q1, -dot);
289 for (
int i = 0; i <
dim; ++i)
300 static void eulGetOrd(
EulerOrder ord,
int& i,
int& j,
int& k,
int& h,
int& n,
int& s,
int& f)
302 static const int eulSafe[] = {0,1,2,0};
303 static const int eulNext[] = {1,2,0,1};
309 i=eulSafe[o&3]; j=eulNext[i+n]; k=eulNext[i+1-n];
314 static Quat_ slerp_fast(Real t,
const Quat_& q0,
const Quat_& q1, Real cosAlpha);
316 static Real slerpCorrection(Real t, Real cosAlpha)
318 static const Real tcor = 0.58549219;
319 static const Real tcoratten = 0.82279687;
321 Real factor = 1 - tcoratten*cosAlpha;
322 factor = factor*factor;
323 Real k = tcor*factor;
327 return t*(t*(b*t + c) + d);
333 static const Real neighborhood = 0.959066;
335 static const Real additive = 1.02143;
336 static const Real factor = -0.53235;
337 return additive + (x - neighborhood)*factor;
363 #define Section_Header
364 #include "Honey/Math/Alge/platform/Quat.h"
365 #undef Section_Header
(m x n)-dimensional matrix
Definition: Matrix.h:24
Algebra.
Definition: Alge.h:13
Quat_ normalize_fast() const
Fast normalization, only accurate when quaternion is close to unit length.
Definition: Quat.h:226
Quat_ & operator-=(const Quat_ &rhs)
Definition: Quat.h:149
const Real & operator[](int i) const
Access quaternion components.
Definition: Quat.h:181
Quat_ & operator*=(const Quat_ &rhs)
Definition: Quat.h:170
static optnull_t optnull
Null optional, use to reset an optional to an uninitialized state or test for initialization.
Definition: Optional.h:12
Quat_ & fromEulerAngles(const Vec3 &eulerAngles)
Construct from euler angles in radians and order xyz_s
Definition: Quat.cpp:27
EulerOrder
Euler angle order.
Definition: Quat.h:75
Quat_ operator-() const
Definition: Quat.h:147
unsigned int uint32
Definition: Core.h:16
Quat_ operator*(const Quat_ &rhs) const
Definition: Quat.h:151
Quat_ operator/(Real rhs) const
Definition: Quat.h:174
Quat_ operator-(const Quat_ &rhs) const
Definition: Quat.h:148
Vec3 axisY() const
Definition: Quat.cpp:284
Matrix4 & toMatrix(Matrix4 &rot, bool b3x3=false) const
Convert quaternion to 4x4 homogeneous rotation matrix. Set b3x3 to true to store the result only in t...
Definition: Quat.cpp:422
Interpolation math.
Definition: Quat.h:10
Quat_(const Vec3 &axis, Real angle)
Construct from axis and angle in radians.
Definition: Quat.h:111
Quat_ ln() const
Definition: Quat.cpp:237
Quat_ operator*(Real rhs) const
Definition: Quat.h:169
Quat_ normalize(optional< Real & > len=optnull) const
Get unit quaternion. The pre-normalized length will be returned in len if specified.
Definition: Quat.h:213
Vec3 eulerAngles() const
Get euler angles in radians and order xyz_s
Definition: Quat.cpp:354
Quat_ & fromMatrix(const Matrix4 &rot)
Construct from 4x4 homogeneous matrix. Rotation is extracted from upper-left 3x3 submatrix.
Definition: Quat.cpp:91
static Real sqrt(Real x)
Square Root.
Definition: Alge.h:63
Quat_(Real x, Real y, Real z, Real w)
Construct with imaginary vector components x,y,z and real scalar component w.
Definition: Quat.h:109
Real & operator[](int i)
Definition: Quat.h:182
Quat_ operator+(const Quat_ &rhs) const
Definition: Quat.h:144
Quat_ & operator/=(Real rhs)
Definition: Quat.h:176
Quat_(const Vec3 &eulerAngles, EulerOrder order)
Construct from euler angles in order.
Definition: Quat.h:119
Real dot(const Quat_ &q) const
Definition: Quat.h:188
Real w
Definition: Quat.h:344
Quat_(const Vec3 &from, const Vec3 &to)
Construct a quaternion that rotates unit vector from towards unit vector to.
Definition: Quat.h:115
static Quat_ squad(Real t, const Quat_ &q1, const Quat_ &a, const Quat_ &b, const Quat_ &c)
Spherical quadratic interpolation between q1 and c. t ranges from [0,1].
Definition: Quat.cpp:487
Real y
Definition: Quat.h:342
Quat_()
Construct with identity.
Definition: Quat.h:107
Vec3 axisX() const
Get quaternion's rotated unit axis.
Definition: Quat.cpp:268
Quat_ & operator*=(Real rhs)
Definition: Quat.h:171
static std::common_type< Num, Num2, Num3 >::type clamp(Num val, Num2 min, Num3 max)
Ensure that a number is within a range.
Definition: Alge.h:99
Quat_(const Vec3 &eulerAngles)
Construct from euler angles in radians and order xyz_s
Definition: Quat.h:117
Quat_ inverse() const
Assumes that quaternion is unit length, same as conjugate()
Definition: Quat.h:191
Quat_ & operator/=(const Quat_ &rhs)
Definition: Quat.h:175
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
Quat_ & fromAxes(const Vec3 &axisX, const Vec3 &axisY, const Vec3 &axisZ)
Construct from 3 unit vectors.
Definition: Quat.cpp:133
bool operator!=(const Quat_ &rhs) const
Definition: Quat.h:141
static const Quat_ identity
Definition: Quat.h:347
Real length() const
Definition: Quat.h:210
Quat_ & fromIdentity()
Set quaternion to identity.
Definition: Quat.h:126
Numeric type information, use numeral() to get instance safely from a static context.
Definition: Numeral.h:17
Vec3 axisZ() const
Definition: Quat.cpp:300
Quat_(const Vec3 &axisX, const Vec3 &axisY, const Vec3 &axisZ)
Construct from 3 unit vectors.
Definition: Quat.h:113
Enables any type to be optional so it can exist in an uninitialized null state.
Definition: Optional.h:52
Quat_ & fromAxisAngle(const Vec3 &axis, Real angle)
Construct from axis and angle in radians.
Definition: Quat.cpp:14
Quat_ conjugate() const
Definition: Quat.h:189
Real x
Definition: Quat.h:341
Quat_ & fromAlign(const Vec3 &v1, const Vec3 &v2)
Construct a quaternion that rotates unit vector v1 towards unit vector v2. The resulting quat's axis ...
Definition: Quat.cpp:143
Quat_< Double > Quat_d
Definition: Quat.h:355
Quat_< Real > Quat
Definition: Quat.h:353
Real__ Real
Definition: Quat.h:22
Quat_ operator/(const Quat_ &rhs) const
Definition: Quat.h:173
Quaternion rotation class. Represents a counter-clockwise rotation of an angle about its axis...
Definition: Matrix4.h:10
Quat_ operator+() const
Definition: Quat.h:143
static const int dim
Definition: Quat.h:47
friend Quat_ operator*(Real lhs, const Quat_ &rhs)
Definition: Quat.h:178
Quat_< Float > Quat_f
Definition: Quat.h:354
void axisAngle(Vec3 &axis, Real &angle) const
Get quaternion axis and angle in radians.
Definition: Quat.cpp:326
Quat_ & fromZero()
Set quaternion to zero.
Definition: Quat.h:124
Real lengthSqr() const
Square of the length.
Definition: Quat.h:209
Real z
Definition: Quat.h:343
#define _ORD(i, p, r, f)
Definition: Quat.h:65
bool operator==(const Quat_ &rhs) const
Definition: Quat.h:140
Quat_(const Matrix4 &rot)
Construct from 4x4 homogeneous matrix. Rotation is extracted from upper-left 3x3 submatrix.
Definition: Quat.h:121
Vec3 operator*(const Vec3 &rhs) const
Definition: Quat.h:159
static const Quat_ zero
Definition: Quat.h:346
Quat_ inverseNonUnit() const
Proper quaternion inverse. Only use if quaternion is expected to be non-unit length.
Definition: Quat.h:194
friend ostream & operator<<(ostream &os, const Quat_ &val)
Definition: Quat.h:286
static void squadSetup(const Quat_ &q0, const Quat_ &q1, const Quat_ &q2, const Quat_ &q3, Quat_ &a, Quat_ &b, Quat_ &c)
Calc intermediate quats required for Squad. Ex. To interpolate between q1 and q2: setup(q0...
Definition: Quat.cpp:472
Global Honeycomb namespace.
Quat_ exp() const
Definition: Quat.cpp:205
Quat_ & operator+=(const Quat_ &rhs)
Definition: Quat.h:145
static Quat_ baryCentric(Real f, Real g, const Quat_ &q0, const Quat_ &q1, const Quat_ &q2)
Triangular bary-centric interpolation.
Definition: Quat.cpp:496
static Quat_ slerp(Real t, const Quat_ &q0, const Quat_ &q1)
Spherical linear interpolation from q0 to q1. t ranges from [0,1].
Definition: Quat.h:262
Trigonometry.
Definition: Trig.h:52
void axes(Vec3 &axisX, Vec3 &axisY, Vec3 &axisZ) const
Get unit axes that represent this quaternion.
Definition: Quat.cpp:316
Quat_ sqrt() const
Definition: Quat.h:207