Changeset 1341


Ignore:
Timestamp:
May 5, 2012, 5:16:29 PM (11 years ago)
Author:
sam
Message:

math: allow to write 1.f / q to take a quaternion's inverse.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lol/math/vector.h

    r1323 r1341  
    11041104/*
    11051105 * vec *(vec, scalar)   (also complex & quaternion)
    1106  * vec *(scalar, vec)   (also complex & quaternion)
    11071106 * vec /(vec, scalar)   (also complex & quaternion)
    1108  * vec /(scalar, vec)   (also complex & quaternion)
    11091107 */
    11101108#define DECLARE_VECTOR_SCALAR_COERCE_OP(tname, op, tprefix, t1, t2, tf) \
     
    11161114            ret[n] = a[n] op val; \
    11171115        return ret; \
    1118     } \
    1119     \
     1116    }
     1117
     1118/*
     1119 * vec *(scalar, vec)   (also complex & quaternion)
     1120 * vec /(scalar, vec)   (NOT for complex & quaternion!)
     1121 */
     1122#define DECLARE_SCALAR_VECTOR_COERCE_OP(tname, op, tprefix, t1, t2, tf) \
    11201123    tprefix \
    11211124    inline tname<tf> operator op(t1 const &val, tname<t2> const &a) \
     
    11711174    }
    11721175
    1173 #define DECLARE_BINARY_COERCE_OPS(tname, tprefix, t1, t2, tf) \
    1174     DECLARE_VECTOR_SCALAR_COERCE_OP(tname, *, tprefix, t1, t2, tf) \
    1175     DECLARE_VECTOR_SCALAR_COERCE_OP(tname, /, tprefix, t1, t2, tf) \
    1176     \
     1176#define DECLARE_BINARY_NONVECTOR_COERCE_OPS(tname, tprefix, t1, t2, tf) \
    11771177    DECLARE_VECTOR_VECTOR_COERCE_OP(tname, -, tprefix, t1, t2, tf) \
    11781178    DECLARE_VECTOR_VECTOR_COERCE_OP(tname, +, tprefix, t1, t2, tf) \
    11791179    \
     1180    DECLARE_VECTOR_SCALAR_COERCE_OP(tname, *, tprefix, t1, t2, tf) \
     1181    DECLARE_VECTOR_SCALAR_COERCE_OP(tname, /, tprefix, t1, t2, tf) \
     1182    DECLARE_SCALAR_VECTOR_COERCE_OP(tname, *, tprefix, t1, t2, tf) \
     1183    \
    11801184    DECLARE_VECTOR_VECTOR_BOOLOP(tname, ==, ==, true, tprefix, t1, t2) \
    1181     DECLARE_VECTOR_VECTOR_BOOLOP(tname, !=, ==, false, tprefix, t1, t2) \
     1185    DECLARE_VECTOR_VECTOR_BOOLOP(tname, !=, ==, false, tprefix, t1, t2)
     1186
     1187#define DECLARE_BINARY_VECTOR_COERCE_OPS(tname, tprefix, t1, t2, tf) \
     1188    DECLARE_SCALAR_VECTOR_COERCE_OP(tname, /, tprefix, t1, t2, tf) \
    11821189    \
    11831190    tprefix \
     
    11991206    }
    12001207
    1201 #define DECLARE_BINARY_OPS(tname, tprefix, type) \
    1202     DECLARE_BINARY_COERCE_OPS(tname, tprefix, type, type, type) \
    1203     \
    1204     DECLARE_VECTOR_SCALAR_OP(tname, *, tprefix, type) \
    1205     DECLARE_VECTOR_SCALAR_OP(tname, /, tprefix, type) \
     1208#define DECLARE_BINARY_NONVECTOR_OPS(tname, tprefix, type) \
     1209    DECLARE_BINARY_NONVECTOR_COERCE_OPS(tname, tprefix, type, type, type) \
    12061210    \
    12071211    DECLARE_VECTOR_VECTOR_OP(tname, -, tprefix, type) \
    12081212    DECLARE_VECTOR_VECTOR_OP(tname, +, tprefix, type) \
     1213    \
     1214    DECLARE_VECTOR_SCALAR_OP(tname, *, tprefix, type) \
     1215    DECLARE_VECTOR_SCALAR_OP(tname, /, tprefix, type)
     1216
     1217#define DECLARE_BINARY_VECTOR_OPS(tname, tprefix, type) \
     1218    DECLARE_BINARY_VECTOR_COERCE_OPS(tname, tprefix, type, type, type) \
    12091219    \
    12101220    DECLARE_VECTOR_MINMAX_OP(tname, min, tprefix, type) \
     
    12281238
    12291239#define DECLARE_ALL_NONVECTOR_OPS(tname) \
    1230     DECLARE_BINARY_OPS(tname, template<typename T> static, T) \
     1240    DECLARE_BINARY_NONVECTOR_OPS(tname, template<typename T> static, T) \
    12311241    DECLARE_UNARY_OPS(tname, template<typename T> static, T)
    12321242
    12331243#define DECLARE_ALL_VECTOR_OPS_INNER(tname, type) \
    1234     DECLARE_BINARY_OPS(tname, static, type) \
     1244    DECLARE_BINARY_VECTOR_OPS(tname, static, type) \
     1245    DECLARE_BINARY_NONVECTOR_OPS(tname, static, type) \
    12351246    DECLARE_UNARY_OPS(tname, static, type) \
    12361247    DECLARE_VECTOR_OPS(tname, static, type) \
     
    12471258
    12481259#define DECLARE_VEC_ANY_COERCE_OPS(tname, tlow, thigh) \
    1249     DECLARE_BINARY_COERCE_OPS(tname, static, tlow, thigh, thigh) \
    1250     DECLARE_BINARY_COERCE_OPS(tname, static, thigh, tlow, thigh) \
     1260    DECLARE_BINARY_NONVECTOR_COERCE_OPS(tname, static, tlow, thigh, thigh) \
     1261    DECLARE_BINARY_NONVECTOR_COERCE_OPS(tname, static, thigh, tlow, thigh) \
     1262    DECLARE_BINARY_VECTOR_COERCE_OPS(tname, static, tlow, thigh, thigh) \
     1263    DECLARE_BINARY_VECTOR_COERCE_OPS(tname, static, thigh, tlow, thigh) \
    12511264    \
    12521265    DECLARE_VECTOR_COERCE_OPS(tname, static, tlow, thigh, thigh) \
     
    14671480#undef DECLARE_VECTOR_VECTOR_BOOLOP
    14681481#undef DECLARE_VECTOR_SCALAR_OP
    1469 #undef DECLARE_BINARY_OPS
     1482#undef DECLARE_BINARY_VECTOR_OPS
     1483#undef DECLARE_BINARY_NONVECTOR_OPS
    14701484#undef DECLARE_UNARY_OPS
    14711485#undef DECLARE_ALL_NONVECTOR_OPS
  • trunk/test/unit/quat.cpp

    r1318 r1341  
    117117        quat a(2.f, -2.f, -8.f, 3.f);
    118118        quat b = re(a);
     119        quat c = 1.f / a;
     120
     121        LOLUNIT_ASSERT_DOUBLES_EQUAL(b.w, c.w, 1e-5);
     122        LOLUNIT_ASSERT_DOUBLES_EQUAL(b.x, c.x, 1e-5);
     123        LOLUNIT_ASSERT_DOUBLES_EQUAL(b.y, c.y, 1e-5);
     124        LOLUNIT_ASSERT_DOUBLES_EQUAL(b.z, c.z, 1e-5);
    119125
    120126        quat m1 = a * b;
Note: See TracChangeset for help on using the changeset viewer.