1#ifndef NW_MATH_QUATERNION_H_
2#define NW_MATH_QUATERNION_H_
5#include <nw/math/math_Config.h>
8 #include <cafe/mtx/mtxVec.h>
12namespace nw {
namespace math {
34#if defined(NW_MATH_ENABLE_INTRINSICS)
90 explicit QUAT(
const f32* p) { x = p[0]; y = p[1]; z = p[2]; w = p[3]; }
91 explicit QUAT(
const QUAT_& rhs) { x = rhs.x; y = rhs.y; z = rhs.z; w = rhs.w; }
92 QUAT(f32 fx, f32 fy, f32 fz, f32 fw) { x = fx; y = fy; z = fz; w = fw; }
100 template <
typename ToPtr>
103 return reinterpret_cast<
ToPtr>(
this );
106 template <
typename ToPtr>
109 return reinterpret_cast<
ToPtr>(
this );
125#pragma clang diagnostic push
126#pragma clang diagnostic ignored "-Wfloat-equal"
131#pragma clang diagnostic pop
137operator * (f32 f,
const QUAT& rhs) {
QUAT tmp; (
void)QUATScale(&tmp, &rhs, f);
return tmp; }
146namespace nw {
namespace math {
148#define NW_QUAT_EPSILON 0.00001F
155 pOut->x = q1->x + q2->x;
156 pOut->y = q1->y + q2->y;
157 pOut->z = q1->z + q2->z;
158 pOut->w = q1->w + q2->w;
166 pOut->x = q1->x - q2->x;
167 pOut->y = q1->y - q2->y;
168 pOut->z = q1->z - q2->z;
169 pOut->w = q1->w - q2->w;
177 QUAT *
__restrict pDst;
180 if ( q1 == pOut || q2 == pOut )
189 pDst->w = q1->w * q2->w - q1->x * q2->x - q1->y * q2->y - q1->z * q2->z;
190 pDst->x = q1->w * q2->x + q1->x * q2->w + q1->y * q2->z - q1->z * q2->y;
191 pDst->y = q1->w * q2->y + q1->y * q2->w + q1->z * q2->x - q1->x * q2->z;
192 pDst->z = q1->w * q2->z + q1->z * q2->w + q1->x * q2->y - q1->y * q2->x;
205 return (q1->x * q2->x + q1->y * q2->y + q1->z * q2->z + q1->w * q2->w);
211 pOut->x = q->x * scale;
212 pOut->y = q->y * scale;
213 pOut->z = q->z * scale;
214 pOut->w = q->w * scale;
222 f32 mag = (q->x * q->x) + (q->y * q->y) + (q->z * q->z) + (q->w * q->w);
224 if ( mag >= NW_QUAT_EPSILON )
226 mag = 1.0F / ::std::sqrt(mag);
228 pOut->x = q->x * mag;
229 pOut->y = q->y * mag;
230 pOut->z = q->z * mag;
231 pOut->w = q->w * mag;
235 pOut->x = pOut->y = pOut->z = pOut->w = 0.0F;
246 mag = ( q->x * q->x + q->y * q->y + q->z * q->z + q->w * q->w );
254 norminv = 1.0f / mag;
255 pOut->x = -q->x * norminv;
256 pOut->y = -q->y * norminv;
257 pOut->z = -q->z * norminv;
258 pOut->w = q->w * norminv;
268 theta = ::std::sqrt( q->x * q->x + q->y * q->y + q->z * q->z );
271 if ( theta > NW_QUAT_EPSILON )
273 scale = (f32)::std::sin(theta) / theta;
276 pOut->x = scale * q->x;
277 pOut->y = scale * q->y;
278 pOut->z = scale * q->z;
279 pOut->w = (f32)::std::cos(theta);
289 scale = q->x * q->x + q->y * q->y + q->z * q->z;
291 scale = ::std::sqrt(scale);
292 theta = ::std::atan2( scale, q->w );
296 scale = theta / scale;
299 pOut->x = scale * q->x;
300 pOut->y = scale * q->y;
301 pOut->z = scale * q->z;
310 pOut->x = t * ( q2->x - q1->x ) + q1->x;
311 pOut->y = t * ( q2->y - q1->y ) + q1->y;
312 pOut->z = t * ( q2->z - q1->z ) + q1->z;
313 pOut->w = t * ( q2->w - q1->w ) + q1->w;
321 f32 theta, sin_th, cos_th, tp, tq;
323 cos_th = q1->x * q2->x + q1->y * q2->y + q1->z * q2->z + q1->w * q2->w;
332 if ( cos_th <= 1.0F - NW_QUAT_EPSILON )
334 theta = ::std::acos(cos_th);
335 sin_th = ::std::sin(theta);
336 tp = ::std::sin((1.0F - t) * theta) / sin_th;
337 tq *= ::std::sin( t * theta ) / sin_th;
346 pOut->x = tp * q1->x + tq * q2->x;
347 pOut->y = tp * q1->y + tq * q2->y;
348 pOut->z = tp * q1->z + tq * q2->z;
349 pOut->w = tp * q1->w + tq * q2->w;
359 dot = q->x * qto->x + q->y * qto->y + q->z * qto->z + q->w * qto->w;
412#if defined(NW_MATH_ENABLE_INTRINSICS)
736 t2 = 2 * t * ( 1.0F - t );
737 QUATSlerp(&pq, p, q, t);
738 QUATSlerp(&ab, a, b, t);
739 QUATSlerp(pOut, &pq, &ab, t2);
756 VEC3Normalize(&nAxis, axis);
759 sh = ::std::sin(half);
760 ch = ::std::cos(half);
762 pOut->x = sh * nAxis.x;
763 pOut->y = sh * nAxis.y;
764 pOut->z = sh * nAxis.z;
774 c.SetCross( *pFrom, *pTo );
775 f32 dPlus1 = VEC3Dot( pFrom, pTo ) + 1.f;
777 if ( dPlus1 <= nw::math::F_ULP )
787 f32 s = FSqrt( dPlus1 * 2.f );
799VEC3CalcRPY(VEC3* pOut,
const QUAT *q)
801 return internal::standard::VEC3CalcRPY(pOut, q);
808namespace nw {
namespace math {
823inline QUAT*
QUATSquad(
QUAT* pOut,
const QUAT& p,
const QUAT& a,
const QUAT& b,
const QUAT& q, f32 t) {
return QUATSquad( pOut, &p, &a, &b, &q, t ); }
#define NW_MATH_AS_INLINE
Definition math_Config.h:6
#define NW_MATH_INLINE
Definition math_Config.h:7
#define NW_MATH_IMPL_NS
Definition math_Config.h:14
Definition math_Matrix34.h:11
QUAT * QUATSlerp(QUAT *pOut, const QUAT *q1, const QUAT *q2, f32 t)
QUAT * QUATAdd(QUAT *pOut, const QUAT *q1, const QUAT *q2)
QUAT * QUATMakeClosest(QUAT *pOut, const QUAT *q, const QUAT *qto)
f32 QUATDot(const QUAT *q1, const QUAT *q2)
QUAT * QUATLerp(QUAT *pOut, const QUAT *q1, const QUAT *q2, f32 t)
QUAT * QUATLogN(QUAT *pOut, const QUAT *q)
QUAT * QUATSub(QUAT *pOut, const QUAT *q1, const QUAT *q2)
QUAT * QUATExp(QUAT *pOut, const QUAT *q)
VEC3 * VEC3CalcRPY(VEC3 *pOut, const QUAT *q)
QUAT * QUATScale(QUAT *pOut, const QUAT *q, f32 scale)
QUAT * QUATInverse(QUAT *pOut, const QUAT *q)
QUAT * QUATNormalize(QUAT *pOut, const QUAT *q)
QUAT * QUATMult(QUAT *pOut, const QUAT *q1, const QUAT *q2)
Definition math_Triangular.cpp:3
Definition math_Constant.cpp:5
struct QUAT Quaternion
Definition math_Quaternion.h:134
NW_MATH_INLINE QUAT * QUATInverse(QUAT *pOut, const QUAT *q)
NW_MATH_INLINE QUAT * QUATLogN(QUAT *pOut, const QUAT *q)
QUAT * QUATInverse(QUAT *pOut, const QUAT &q)
Definition math_Quaternion.h:815
NW_MATH_INLINE QUAT * QUATSlerp(QUAT *pOut, const QUAT *q1, const QUAT *q2, f32 t)
NW_MATH_INLINE QUAT * QUATMakeClosest(QUAT *pOut, const QUAT *q, const QUAT *qto)
QUAT * QUATScale(QUAT *pOut, const QUAT &q, f32 scale)
Definition math_Quaternion.h:816
NW_MATH_INLINE QUAT * QUATLerp(QUAT *pOut, const QUAT *q1, const QUAT *q2, f32 t)
QUAT * QUATMakeVectorRotation(QUAT *pOut, const VEC3 &from, const VEC3 &to)
Definition math_Quaternion.h:827
QUAT * QUATLogN(QUAT *pOut, const QUAT &q)
Definition math_Quaternion.h:819
QUAT * QUATExp(QUAT *pOut, const QUAT &q)
Definition math_Quaternion.h:818
QUAT * QUATNormalize(QUAT *pOut, const QUAT &q)
Definition math_Quaternion.h:817
NW_MATH_INLINE QUAT * QUATMult(QUAT *pOut, const QUAT *q1, const QUAT *q2)
QUAT * MTX34ToQUAT(QUAT *pOut, const MTX34 &mtx)
Definition math_Quaternion.h:822
QUAT * QUATSlerp(QUAT *pOut, const QUAT &q1, const QUAT &q2, f32 t)
Definition math_Quaternion.h:821
QUAT * QUATSquad(QUAT *pOut, const QUAT &p, const QUAT &a, const QUAT &b, const QUAT &q, f32 t)
Definition math_Quaternion.h:823
NW_MATH_INLINE QUAT * QUATAdd(QUAT *pOut, const QUAT *q1, const QUAT *q2)
QUAT * QUATDivide(QUAT *pOut, const QUAT &q1, const QUAT &q2)
Definition math_Quaternion.h:812
NW_MATH_INLINE QUAT * QUATDivide(QUAT *pOut, const QUAT *q1, const QUAT *q2)
QUAT * QUATLerp(QUAT *pOut, const QUAT &q1, const QUAT &q2, f32 t)
Definition math_Quaternion.h:820
NW_MATH_INLINE QUAT * MTX34ToQUAT(QUAT *pOut, const MTX34 *pMtx)
f32 QUATDot(const QUAT &q1, const QUAT &q2)
Definition math_Quaternion.h:814
QUAT * QUATMakeClosest(QUAT *pOut, const QUAT &q, const QUAT &qto)
Definition math_Quaternion.h:825
QUAT * QUATAdd(QUAT *pOut, const QUAT &q1, const QUAT &q2)
Definition math_Quaternion.h:810
NW_MATH_INLINE f32 QUATDot(const QUAT *q1, const QUAT *q2)
NW_MATH_INLINE QUAT * QUATScale(QUAT *pOut, const QUAT *q, f32 scale)
NW_MATH_INLINE QUAT * QUATSub(QUAT *pOut, const QUAT *q1, const QUAT *q2)
QUAT * QUATSub(QUAT *pOut, const QUAT &q1, const QUAT &q2)
Definition math_Quaternion.h:811
NW_MATH_INLINE QUAT * QUATMakeVectorRotation(QUAT *pOut, const VEC3 *pFrom, const VEC3 *pTo)
NW_MATH_INLINE QUAT * QUATRotAxisRad(QUAT *pOut, const VEC3 *axis, f32 rad)
NW_MATH_INLINE QUAT * QUATExp(QUAT *pOut, const QUAT *q)
QUAT * QUATRotAxisRad(QUAT *pOut, const VEC3 &axis, f32 rad)
Definition math_Quaternion.h:826
QUAT operator*(f32 f, const QUAT &rhs)
Definition math_Quaternion.h:137
NW_MATH_INLINE QUAT * QUATNormalize(QUAT *pOut, const QUAT *q)
NW_MATH_INLINE QUAT * QUATSquad(QUAT *pOut, const QUAT *p, const QUAT *a, const QUAT *b, const QUAT *q, f32 t)
QUAT * QUATMult(QUAT *pOut, const QUAT &q1, const QUAT &q2)
Definition math_Quaternion.h:813
Definition math_Constant.cpp:5
Definition math_Quaternion.h:76
f32 z
Definition math_Quaternion.h:79
f32 y
Definition math_Quaternion.h:78
f32 x
Definition math_Quaternion.h:77
f32 w
Definition math_Quaternion.h:80
Definition math_Quaternion.h:84
f32 value_type
Definition math_Quaternion.h:87
QUAT(const QUAT_ &rhs)
Definition math_Quaternion.h:91
QUAT(const f32 *p)
Definition math_Quaternion.h:90
QUAT self_type
Definition math_Quaternion.h:86
QUAT()
Definition math_Quaternion.h:89
QUAT(f32 fx, f32 fy, f32 fz, f32 fw)
Definition math_Quaternion.h:92
#define NW_PLATFORM_CAFE
Definition types.h:8