Changeset 1151
 Timestamp:
 Mar 2, 2012, 6:49:00 PM (10 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/src/lol/math/vector.h
r1150 r1151 926 926 */ 927 927 928 #define DECLARE_VECTOR_ OP(tname, op, tprefix, T) \928 #define DECLARE_VECTOR_VECTOR_OP(tname, op, tprefix, type) \ 929 929 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) \ 931 932 { \ 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++) \ 934 935 ret[n] = a[n] op b[n]; \ 935 936 return ret; \ … … 937 938 \ 938 939 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) \ 940 942 { \ 941 943 return a = a op b; \ 942 944 } 943 945 944 #define DECLARE_ BOOL_OP(tname, op, op2, ret, tprefix, T) \946 #define DECLARE_VECTOR_VECTOR_BOOLOP(tname, op, op2, ret, tprefix, type) \ 945 947 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) \ 947 950 { \ 948 for (size_t n = 0; n < sizeof(a) / sizeof( T); n++) \951 for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \ 949 952 if (!(a[n] op2 b[n])) \ 950 953 return !ret; \ … … 952 955 } 953 956 954 #define DECLARE_ SCALAR_OP(tname, op, tprefix, T) \957 #define DECLARE_VECTOR_SCALAR_OP(tname, op, tprefix, type) \ 955 958 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) \ 957 961 { \ 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++) \ 960 964 ret[n] = a[n] op val; \ 961 965 return ret; \ … … 963 967 \ 964 968 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) \ 966 971 { \ 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++) \ 969 974 ret[n] = a[n] op val; \ 970 975 return ret; \ … … 972 977 \ 973 978 tprefix \ 974 static inline tname< T> operator op##=(tname<T> &a, Tconst &val) \979 static inline tname<type> operator op##=(tname<type> &a, type const &val) \ 975 980 { \ 976 981 return a = a op val; \ … … 980 985 #define SCALAR_PROMOTE_OP(tname, op, U) \ 981 986 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) \ 983 989 { \ 984 990 tname<U> ret; \ … … 988 994 } 989 995 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) \ 1000 997 tprefix \ 1001 static inline tname< T> operator (tname<T> const &a) \998 static inline tname<type> operator (tname<type> const &a) \ 1002 999 { \ 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++) \ 1005 1002 ret[n] = a[n]; \ 1006 1003 return ret; \ … … 1008 1005 \ 1009 1006 tprefix \ 1010 static inline T sqlen(tname<T> const &a) \1007 static inline type sqlen(tname<type> const &a) \ 1011 1008 { \ 1012 Tacc = 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++) \ 1014 1011 acc += a[n] * a[n]; \ 1015 1012 return acc; \ … … 1017 1014 \ 1018 1015 tprefix \ 1019 static inline double len(tname< T> const &a) \1016 static inline double len(tname<type> const &a) \ 1020 1017 { \ 1021 1018 using namespace std; \ … … 1024 1021 \ 1025 1022 tprefix \ 1026 static inline T dot(tname<T> const &a, tname<T> const &b) \1023 static inline tname<type> normalize(tname<type> const &val) \ 1027 1024 { \ 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++) \ 1030 1044 ret += a[n] * b[n]; \ 1031 1045 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) \ 1033 1051 \ 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 1079 DECLARE_ALL_NONVECTOR_OPS(Cmplx) 1080 DECLARE_ALL_NONVECTOR_OPS(Quat) 1081 1082 DECLARE_ALL_VECTOR_OPS(Vec2) 1083 DECLARE_ALL_VECTOR_OPS(Vec3) 1084 DECLARE_ALL_VECTOR_OPS(Vec4) 1074 1085 1075 1086 #undef DECLARE_VECTOR_TYPEDEFS 1076 1087 #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 1084 1096 1085 1097 /*
Note: See TracChangeset
for help on using the changeset viewer.