Add QuatInverse to calculate 1/q.

There's still no direct quaternion division because I don't know if the
definition of q1/q2 is (1/q2)*q1 or q1*(1/q2) (it does make a difference).
This commit is contained in:
Bill Currie 2011-01-18 22:22:18 +09:00
parent 87ce9f5333
commit 33db650161
2 changed files with 18 additions and 6 deletions

View file

@ -241,7 +241,8 @@ float anglemod (float a);
void RotatePointAroundVector (vec3_t dst, const vec3_t axis, void RotatePointAroundVector (vec3_t dst, const vec3_t axis,
const vec3_t point, float degrees); const vec3_t point, float degrees);
void QuatMult (const quat_t v1, const quat_t v2, quat_t out); void QuatMult (const quat_t q1, const quat_t q2, quat_t out);
void QuatInverse (const quat_t in, quat_t out);
#define BOX_ON_PLANE_SIDE(emins, emaxs, p) \ #define BOX_ON_PLANE_SIDE(emins, emaxs, p) \
(((p)->type < 3)? \ (((p)->type < 3)? \

View file

@ -172,18 +172,29 @@ RotatePointAroundVector (vec3_t dst, const vec3_t axis, const vec3_t point,
} }
VISIBLE void VISIBLE void
QuatMult (const quat_t v1, const quat_t v2, quat_t out) QuatMult (const quat_t q1, const quat_t q2, quat_t out)
{ {
vec_t s; vec_t s;
vec3_t v; vec3_t v;
s = v1[0] * v2[0] - DotProduct (v1 + 1, v2 + 1); s = q1[0] * q2[0] - DotProduct (q1 + 1, q2 + 1);
CrossProduct (v1 + 1, v2 + 1, v); CrossProduct (q1 + 1, q2 + 1, v);
VectorMultAdd (v, v1[0], v2 + 1, v); VectorMultAdd (v, q1[0], q2 + 1, v);
VectorMultAdd (v, v2[0], v1 + 1, out + 1); VectorMultAdd (v, q2[0], q1 + 1, out + 1);
out[0] = s; out[0] = s;
} }
VISIBLE void
QuatInverse (const quat_t in, quat_t out)
{
quat_t q;
vec_t m;
m = QDotProduct (in, in); // in * in*
QuatConj (in, q);
QuatScale (q, 1 / m, out);
}
#if defined(_WIN32) && !defined(__GNUC__) #if defined(_WIN32) && !defined(__GNUC__)
# pragma optimize( "", on ) # pragma optimize( "", on )
#endif #endif