Changeset 1151


Ignore:
Timestamp:
Mar 2, 2012, 6:49:00 PM (10 years ago)
Author:
sam
Message:

math: some refactoring in the vector operations, to make it easier to
implement automatic promotion in the future.

File:
1 edited

Legend:

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

    r1150 r1151  
    926926 */
    927927
    928 #define DECLARE_VECTOR_OP(tname, op, tprefix, T) \
     928#define DECLARE_VECTOR_VECTOR_OP(tname, op, tprefix, type) \
    929929    tprefix \
    930     static inline tname<T> operator op(tname<T> const &a, tname<T> const &b) \
     930    static inline tname<type> operator op(tname<type> const &a, \
     931                                          tname<type> const &b) \
    931932    { \
    932         tname<T> ret; \
    933         for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
     933        tname<type> ret; \
     934        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
    934935            ret[n] = a[n] op b[n]; \
    935936        return ret; \
     
    937938    \
    938939    tprefix \
    939     static inline tname<T> operator op##=(tname<T> &a, tname<T> const &b) \
     940    static inline tname<type> operator op##=(tname<type> &a, \
     941                                             tname<type> const &b) \
    940942    { \
    941943        return a = a op b; \
    942944    }
    943945
    944 #define DECLARE_BOOL_OP(tname, op, op2, ret, tprefix, T) \
     946#define DECLARE_VECTOR_VECTOR_BOOLOP(tname, op, op2, ret, tprefix, type) \
    945947    tprefix \
    946     static inline bool operator op(tname<T> const &a, tname<T> const &b) \
     948    static inline bool operator op(tname<type> const &a, \
     949                                   tname<type> const &b) \
    947950    { \
    948         for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
     951        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
    949952            if (!(a[n] op2 b[n])) \
    950953                return !ret; \
     
    952955    }
    953956
    954 #define DECLARE_SCALAR_OP(tname, op, tprefix, T) \
     957#define DECLARE_VECTOR_SCALAR_OP(tname, op, tprefix, type) \
    955958    tprefix \
    956     static inline tname<T> operator op(tname<T> const &a, T const &val) \
     959    static inline tname<type> operator op(tname<type> const &a, \
     960                                          type const &val) \
    957961    { \
    958         tname<T> ret; \
    959         for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
     962        tname<type> ret; \
     963        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
    960964            ret[n] = a[n] op val; \
    961965        return ret; \
     
    963967    \
    964968    tprefix \
    965     static inline tname<T> operator op(T const &val, tname<T> const &a) \
     969    static inline tname<type> operator op(type const &val, \
     970                                          tname<type> const &a) \
    966971    { \
    967         tname<T> ret; \
    968         for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
     972        tname<type> ret; \
     973        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
    969974            ret[n] = a[n] op val; \
    970975        return ret; \
     
    972977    \
    973978    tprefix \
    974     static inline tname<T> operator op##=(tname<T> &a, T const &val) \
     979    static inline tname<type> operator op##=(tname<type> &a, type const &val) \
    975980    { \
    976981        return a = a op val; \
     
    980985#define SCALAR_PROMOTE_OP(tname, op, U) \
    981986    template<typename T> \
    982     static inline tname<U> operator op(U const &val, tname<T> const &a) \
     987    static inline tname<U> operator op(U const &val, \
     988                                       tname<T> const &a) \
    983989    { \
    984990        tname<U> ret; \
     
    988994    }
    989995
    990 #define DECLARE_OTHER_GLOBAL_OPS(tname, tprefix, T) \
    991     DECLARE_SCALAR_OP(tname, *, tprefix, T) \
    992     DECLARE_SCALAR_OP(tname, /, tprefix, T) \
    993     \
    994     DECLARE_VECTOR_OP(tname, -, tprefix, T) \
    995     DECLARE_VECTOR_OP(tname, +, tprefix, T) \
    996     \
    997     DECLARE_BOOL_OP(tname, ==, ==, true, tprefix, T) \
    998     DECLARE_BOOL_OP(tname, !=, ==, false, tprefix, T) \
    999     \
     996#define DECLARE_UNARY_OPS(tname, tprefix, type) \
    1000997    tprefix \
    1001     static inline tname<T> operator -(tname<T> const &a) \
     998    static inline tname<type> operator -(tname<type> const &a) \
    1002999    { \
    1003         tname<T> ret; \
    1004         for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
     1000        tname<type> ret; \
     1001        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
    10051002            ret[n] = -a[n]; \
    10061003        return ret; \
     
    10081005    \
    10091006    tprefix \
    1010     static inline T sqlen(tname<T> const &a) \
     1007    static inline type sqlen(tname<type> const &a) \
    10111008    { \
    1012         T acc = 0; \
    1013         for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
     1009        type acc = 0; \
     1010        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
    10141011            acc += a[n] * a[n]; \
    10151012        return acc; \
     
    10171014    \
    10181015    tprefix \
    1019     static inline double len(tname<T> const &a) \
     1016    static inline double len(tname<type> const &a) \
    10201017    { \
    10211018        using namespace std; \
     
    10241021    \
    10251022    tprefix \
    1026     static inline T dot(tname<T> const &a, tname<T> const &b) \
     1023    static inline tname<type> normalize(tname<type> const &val) \
    10271024    { \
    1028         T ret = 0; \
    1029         for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
     1025        type norm = (type)len(val); \
     1026        return norm ? val / norm : val * (type)0; \
     1027    }
     1028
     1029#define DECLARE_BINARY_OPS(tname, tprefix, type) \
     1030    DECLARE_VECTOR_SCALAR_OP(tname, *, tprefix, type) \
     1031    DECLARE_VECTOR_SCALAR_OP(tname, /, tprefix, type) \
     1032    \
     1033    DECLARE_VECTOR_VECTOR_OP(tname, -, tprefix, type) \
     1034    DECLARE_VECTOR_VECTOR_OP(tname, +, tprefix, type) \
     1035    \
     1036    DECLARE_VECTOR_VECTOR_BOOLOP(tname, ==, ==, true, tprefix, type) \
     1037    DECLARE_VECTOR_VECTOR_BOOLOP(tname, !=, ==, false, tprefix, type) \
     1038    \
     1039    tprefix \
     1040    static inline type dot(tname<type> const &a, tname<type> const &b) \
     1041    { \
     1042        type ret = 0; \
     1043        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
    10301044            ret += a[n] * b[n]; \
    10311045        return ret; \
    1032     } \
     1046    }
     1047
     1048#define DECLARE_VECTOR_OPS(tname, tprefix, type) \
     1049    DECLARE_VECTOR_VECTOR_OP(tname, *, tprefix, type) \
     1050    DECLARE_VECTOR_VECTOR_OP(tname, /, tprefix, type) \
    10331051    \
    1034     tprefix \
    1035     static inline tname<T> normalize(tname<T> const &val) \
    1036     { \
    1037         T norm = (T)len(val); \
    1038         return norm ? val / norm : val * (T)0; \
    1039     }
    1040 
    1041 #define DECLARE_GLOBAL_TEMPLATE_OPS(tname) \
    1042     DECLARE_OTHER_GLOBAL_OPS(tname, template<typename T>, T)
    1043 
    1044 #define DECLARE_ALL_GLOBAL_OPS(tname, tprefix, T) \
    1045     DECLARE_VECTOR_OP(tname, *, tprefix, T) \
    1046     DECLARE_VECTOR_OP(tname, /, tprefix, T) \
    1047     \
    1048     DECLARE_OTHER_GLOBAL_OPS(tname, tprefix, T) \
    1049     \
    1050     DECLARE_BOOL_OP(tname, <=, <=, true, tprefix, T) \
    1051     DECLARE_BOOL_OP(tname, >=, >=, true, tprefix, T) \
    1052     DECLARE_BOOL_OP(tname, <, <, true, tprefix, T) \
    1053     DECLARE_BOOL_OP(tname, >, >, true, tprefix, T)
    1054 
    1055 #define DECLARE_GLOBAL_TYPED_OPS(tname) \
    1056     DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, half) \
    1057     DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, float) \
    1058     DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, double) \
    1059     DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, int8_t) \
    1060     DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, uint8_t) \
    1061     DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, int16_t) \
    1062     DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, uint16_t) \
    1063     DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, int32_t) \
    1064     DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, uint32_t) \
    1065     DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, int64_t) \
    1066     DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, uint64_t)
    1067 
    1068 DECLARE_GLOBAL_TEMPLATE_OPS(Cmplx)
    1069 DECLARE_GLOBAL_TEMPLATE_OPS(Quat)
    1070 
    1071 DECLARE_GLOBAL_TYPED_OPS(Vec2)
    1072 DECLARE_GLOBAL_TYPED_OPS(Vec3)
    1073 DECLARE_GLOBAL_TYPED_OPS(Vec4)
     1052    DECLARE_VECTOR_VECTOR_BOOLOP(tname, <=, <=, true, tprefix, type) \
     1053    DECLARE_VECTOR_VECTOR_BOOLOP(tname, >=, >=, true, tprefix, type) \
     1054    DECLARE_VECTOR_VECTOR_BOOLOP(tname, <, <, true, tprefix, type) \
     1055    DECLARE_VECTOR_VECTOR_BOOLOP(tname, >, >, true, tprefix, type)
     1056
     1057#define DECLARE_ALL_NONVECTOR_OPS(tname) \
     1058    DECLARE_BINARY_OPS(tname, template<typename T>, T) \
     1059    DECLARE_UNARY_OPS(tname, template<typename T>, T)
     1060
     1061#define DECLARE_ALL_VECTOR_OPS_INNER(tname, tprefix, type) \
     1062    DECLARE_BINARY_OPS(tname, tprefix, type) \
     1063    DECLARE_UNARY_OPS(tname, tprefix, type) \
     1064    DECLARE_VECTOR_OPS(tname, tprefix, type) \
     1065
     1066#define DECLARE_ALL_VECTOR_OPS(tname) \
     1067    DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, half) \
     1068    DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, float) \
     1069    DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, double) \
     1070    DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, int8_t) \
     1071    DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, uint8_t) \
     1072    DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, int16_t) \
     1073    DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, uint16_t) \
     1074    DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, int32_t) \
     1075    DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, uint32_t) \
     1076    DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, int64_t) \
     1077    DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, uint64_t)
     1078
     1079DECLARE_ALL_NONVECTOR_OPS(Cmplx)
     1080DECLARE_ALL_NONVECTOR_OPS(Quat)
     1081
     1082DECLARE_ALL_VECTOR_OPS(Vec2)
     1083DECLARE_ALL_VECTOR_OPS(Vec3)
     1084DECLARE_ALL_VECTOR_OPS(Vec4)
    10741085
    10751086#undef DECLARE_VECTOR_TYPEDEFS
    10761087#undef DECLARE_MEMBER_OPS
    1077 #undef DECLARE_VECTOR_OP
    1078 #undef DECLARE_BOOL_OP
    1079 #undef DECLARE_SCALAR_OP
    1080 #undef DECLARE_GLOBAL_OPS
    1081 #undef DECLARE_GLOBAL_TEMPLATE_OPS
    1082 #undef DECLARE_ALL_GLOBAL_OPS
    1083 #undef DECLARE_GLOBAL_TYPED_OPS
     1088#undef DECLARE_VECTOR_VECTOR_OP
     1089#undef DECLARE_VECTOR_VECTOR_BOOLOP
     1090#undef DECLARE_VECTOR_SCALAR_OP
     1091#undef DECLARE_BINARY_OPS
     1092#undef DECLARE_UNARY_OPS
     1093#undef DECLARE_ALL_NONVECTOR_OPS
     1094#undef DECLARE_ALL_VECTOR_OPS_INNER
     1095#undef DECLARE_ALL_VECTOR_OPS
    10841096
    10851097/*
Note: See TracChangeset for help on using the changeset viewer.