From c276dbb1ff6afedc068d4b32bf596fa9b1b6df6b Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 5 May 2012 15:16:29 +0000 Subject: [PATCH] math: allow to write 1.f / q to take a quaternion's inverse. --- src/lol/math/vector.h | 52 +++++++++++++++++++++++++++---------------- test/unit/quat.cpp | 6 +++++ 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/lol/math/vector.h b/src/lol/math/vector.h index c47b9295..143c26dc 100644 --- a/src/lol/math/vector.h +++ b/src/lol/math/vector.h @@ -1103,9 +1103,7 @@ static inline Quat operator /(Quat x, Quat const &y) /* * vec *(vec, scalar) (also complex & quaternion) - * vec *(scalar, vec) (also complex & quaternion) * vec /(vec, scalar) (also complex & quaternion) - * vec /(scalar, vec) (also complex & quaternion) */ #define DECLARE_VECTOR_SCALAR_COERCE_OP(tname, op, tprefix, t1, t2, tf) \ tprefix \ @@ -1115,8 +1113,13 @@ static inline Quat operator /(Quat x, Quat const &y) for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \ ret[n] = a[n] op val; \ return ret; \ - } \ - \ + } + +/* + * vec *(scalar, vec) (also complex & quaternion) + * vec /(scalar, vec) (NOT for complex & quaternion!) + */ +#define DECLARE_SCALAR_VECTOR_COERCE_OP(tname, op, tprefix, t1, t2, tf) \ tprefix \ inline tname operator op(t1 const &val, tname const &a) \ { \ @@ -1170,15 +1173,19 @@ static inline Quat operator /(Quat x, Quat const &y) return norm ? val / norm : val * (type)0; \ } -#define DECLARE_BINARY_COERCE_OPS(tname, tprefix, t1, t2, tf) \ - DECLARE_VECTOR_SCALAR_COERCE_OP(tname, *, tprefix, t1, t2, tf) \ - DECLARE_VECTOR_SCALAR_COERCE_OP(tname, /, tprefix, t1, t2, tf) \ - \ +#define DECLARE_BINARY_NONVECTOR_COERCE_OPS(tname, tprefix, t1, t2, tf) \ DECLARE_VECTOR_VECTOR_COERCE_OP(tname, -, tprefix, t1, t2, tf) \ DECLARE_VECTOR_VECTOR_COERCE_OP(tname, +, tprefix, t1, t2, tf) \ \ + DECLARE_VECTOR_SCALAR_COERCE_OP(tname, *, tprefix, t1, t2, tf) \ + DECLARE_VECTOR_SCALAR_COERCE_OP(tname, /, tprefix, t1, t2, tf) \ + DECLARE_SCALAR_VECTOR_COERCE_OP(tname, *, tprefix, t1, t2, tf) \ + \ DECLARE_VECTOR_VECTOR_BOOLOP(tname, ==, ==, true, tprefix, t1, t2) \ - DECLARE_VECTOR_VECTOR_BOOLOP(tname, !=, ==, false, tprefix, t1, t2) \ + DECLARE_VECTOR_VECTOR_BOOLOP(tname, !=, ==, false, tprefix, t1, t2) + +#define DECLARE_BINARY_VECTOR_COERCE_OPS(tname, tprefix, t1, t2, tf) \ + DECLARE_SCALAR_VECTOR_COERCE_OP(tname, /, tprefix, t1, t2, tf) \ \ tprefix \ inline tf dot(tname const &a, tname const &b) \ @@ -1198,15 +1205,18 @@ static inline Quat operator /(Quat x, Quat const &y) (tf)(a.x * b.y) - (tf)(a.y * b.x)); \ } -#define DECLARE_BINARY_OPS(tname, tprefix, type) \ - DECLARE_BINARY_COERCE_OPS(tname, tprefix, type, type, type) \ - \ - DECLARE_VECTOR_SCALAR_OP(tname, *, tprefix, type) \ - DECLARE_VECTOR_SCALAR_OP(tname, /, tprefix, type) \ +#define DECLARE_BINARY_NONVECTOR_OPS(tname, tprefix, type) \ + DECLARE_BINARY_NONVECTOR_COERCE_OPS(tname, tprefix, type, type, type) \ \ DECLARE_VECTOR_VECTOR_OP(tname, -, tprefix, type) \ DECLARE_VECTOR_VECTOR_OP(tname, +, tprefix, type) \ \ + DECLARE_VECTOR_SCALAR_OP(tname, *, tprefix, type) \ + DECLARE_VECTOR_SCALAR_OP(tname, /, tprefix, type) + +#define DECLARE_BINARY_VECTOR_OPS(tname, tprefix, type) \ + DECLARE_BINARY_VECTOR_COERCE_OPS(tname, tprefix, type, type, type) \ + \ DECLARE_VECTOR_MINMAX_OP(tname, min, tprefix, type) \ DECLARE_VECTOR_MINMAX_OP(tname, max, tprefix, type) \ DECLARE_VECTOR_CLAMP_OP(tname, tprefix, type) @@ -1227,11 +1237,12 @@ static inline Quat operator /(Quat x, Quat const &y) DECLARE_VECTOR_VECTOR_OP(tname, /, tprefix, type) #define DECLARE_ALL_NONVECTOR_OPS(tname) \ - DECLARE_BINARY_OPS(tname, template static, T) \ + DECLARE_BINARY_NONVECTOR_OPS(tname, template static, T) \ DECLARE_UNARY_OPS(tname, template static, T) #define DECLARE_ALL_VECTOR_OPS_INNER(tname, type) \ - DECLARE_BINARY_OPS(tname, static, type) \ + DECLARE_BINARY_VECTOR_OPS(tname, static, type) \ + DECLARE_BINARY_NONVECTOR_OPS(tname, static, type) \ DECLARE_UNARY_OPS(tname, static, type) \ DECLARE_VECTOR_OPS(tname, static, type) \ @@ -1246,8 +1257,10 @@ static inline Quat operator /(Quat x, Quat const &y) } #define DECLARE_VEC_ANY_COERCE_OPS(tname, tlow, thigh) \ - DECLARE_BINARY_COERCE_OPS(tname, static, tlow, thigh, thigh) \ - DECLARE_BINARY_COERCE_OPS(tname, static, thigh, tlow, thigh) \ + DECLARE_BINARY_NONVECTOR_COERCE_OPS(tname, static, tlow, thigh, thigh) \ + DECLARE_BINARY_NONVECTOR_COERCE_OPS(tname, static, thigh, tlow, thigh) \ + DECLARE_BINARY_VECTOR_COERCE_OPS(tname, static, tlow, thigh, thigh) \ + DECLARE_BINARY_VECTOR_COERCE_OPS(tname, static, thigh, tlow, thigh) \ \ DECLARE_VECTOR_COERCE_OPS(tname, static, tlow, thigh, thigh) \ DECLARE_VECTOR_COERCE_OPS(tname, static, thigh, tlow, thigh) @@ -1466,7 +1479,8 @@ ACTIVATE_COERCE_NAMESPACES(real) #undef DECLARE_VECTOR_VECTOR_OP #undef DECLARE_VECTOR_VECTOR_BOOLOP #undef DECLARE_VECTOR_SCALAR_OP -#undef DECLARE_BINARY_OPS +#undef DECLARE_BINARY_VECTOR_OPS +#undef DECLARE_BINARY_NONVECTOR_OPS #undef DECLARE_UNARY_OPS #undef DECLARE_ALL_NONVECTOR_OPS #undef DECLARE_ALL_VECTOR_OPS_INNER diff --git a/test/unit/quat.cpp b/test/unit/quat.cpp index 17e9a076..7bd9fecb 100644 --- a/test/unit/quat.cpp +++ b/test/unit/quat.cpp @@ -116,6 +116,12 @@ LOLUNIT_FIXTURE(QuaternionTest) { quat a(2.f, -2.f, -8.f, 3.f); quat b = re(a); + quat c = 1.f / a; + + LOLUNIT_ASSERT_DOUBLES_EQUAL(b.w, c.w, 1e-5); + LOLUNIT_ASSERT_DOUBLES_EQUAL(b.x, c.x, 1e-5); + LOLUNIT_ASSERT_DOUBLES_EQUAL(b.y, c.y, 1e-5); + LOLUNIT_ASSERT_DOUBLES_EQUAL(b.z, c.z, 1e-5); quat m1 = a * b; quat m2 = b * a;