diff --git a/src/lol/math/vector.h b/src/lol/math/vector.h index 555bd2d1..dffcaa55 100644 --- a/src/lol/math/vector.h +++ b/src/lol/math/vector.h @@ -485,9 +485,6 @@ template struct Vec3 : BVec3 DECLARE_MEMBER_OPS(Vec3) - template - friend Vec3 cross(Vec3, Vec3); - #if !defined __ANDROID__ template friend std::ostream &operator<<(std::ostream &stream, Vec3 const &v); @@ -1077,6 +1074,15 @@ static inline Quat operator /(Quat x, Quat const &y) return ret; \ } +#define DECLARE_VEC_3_COERCE_OPS(tname, tprefix, t1, t2, tf) \ + tprefix \ + inline tname cross(tname const &a, tname const &b) \ + { \ + return tname((tf)(a.y * b.z) - (tf)(a.z * b.y), \ + (tf)(a.z * b.x) - (tf)(a.x * b.z), \ + (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) \ \ @@ -1110,18 +1116,12 @@ static inline Quat operator /(Quat x, Quat const &y) DECLARE_UNARY_OPS(tname, static, type) \ DECLARE_VECTOR_OPS(tname, static, type) \ -#define DECLARE_ALL_VECTOR_OPS(tname) \ - DECLARE_ALL_VECTOR_OPS_INNER(tname, half) \ - DECLARE_ALL_VECTOR_OPS_INNER(tname, float) \ - DECLARE_ALL_VECTOR_OPS_INNER(tname, double) \ - DECLARE_ALL_VECTOR_OPS_INNER(tname, int8_t) \ - DECLARE_ALL_VECTOR_OPS_INNER(tname, uint8_t) \ - DECLARE_ALL_VECTOR_OPS_INNER(tname, int16_t) \ - DECLARE_ALL_VECTOR_OPS_INNER(tname, uint16_t) \ - DECLARE_ALL_VECTOR_OPS_INNER(tname, int32_t) \ - DECLARE_ALL_VECTOR_OPS_INNER(tname, uint32_t) \ - DECLARE_ALL_VECTOR_OPS_INNER(tname, int64_t) \ - DECLARE_ALL_VECTOR_OPS_INNER(tname, uint64_t) +#define DECLARE_ALL_VECTOR_OPS(type) \ + DECLARE_ALL_VECTOR_OPS_INNER(Vec2, type) \ + DECLARE_ALL_VECTOR_OPS_INNER(Vec3, type) \ + DECLARE_ALL_VECTOR_OPS_INNER(Vec4, type) \ + \ + DECLARE_VEC_3_COERCE_OPS(Vec3, static, type, type, type) #define DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, tlow, thigh) \ DECLARE_BINARY_COERCE_OPS(tname, static, tlow, thigh, thigh) \ @@ -1130,100 +1130,13 @@ static inline Quat operator /(Quat x, Quat const &y) DECLARE_VECTOR_COERCE_OPS(tname, static, tlow, thigh, thigh) \ DECLARE_VECTOR_COERCE_OPS(tname, static, thigh, tlow, thigh) -#define DECLARE_ALL_VECTOR_COERCE_OPS(tname) \ - /* Apply the same coercion rules as in the C++ standard. However, */ \ - /* instead of promoting int8_t etc. to int, we apply our own rules. */ \ - /* FIXME: "half" and "real" are deactivated for now, because we do */ \ - /* not implement all combinations of operators for these types yet. */ \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, uint8_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, int16_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, uint16_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, int32_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, uint32_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, int64_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, uint64_t) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, half) */ \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, float) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, double) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, long double) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, real) */ \ - \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, int16_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, uint16_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, int32_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, uint32_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, int64_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, uint64_t) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, half) */ \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, float) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, double) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, long double) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, real) */ \ - \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, uint16_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, int32_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, uint32_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, int64_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, uint64_t) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, half) */ \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, float) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, double) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, long double) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, real) */ \ - \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, int32_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, uint32_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, int64_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, uint64_t) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, half) */ \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, float) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, double) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, long double) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, real) */ \ - \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, uint32_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, int64_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, uint64_t) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, half) */ \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, float) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, double) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, long double) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, real) */ \ - \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint32_t, int64_t) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint32_t, uint64_t) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint32_t, half) */ \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint32_t, float) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint32_t, double) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint32_t, long double) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint32_t, real) */ \ - \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int64_t, uint64_t) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int64_t, half) */ \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int64_t, float) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int64_t, double) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int64_t, long double) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int64_t, real) */ \ - \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint64_t, half) */ \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint64_t, float) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint64_t, double) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint64_t, long double) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint64_t, real) */ \ - \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, half, float) */ \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, half, double) */ \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, half, long double) */ \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, half, real) */ \ - \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, float, double) \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, float, long double) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, float, real) */ \ - \ - DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, double, long double) \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, double, real) */ \ +#define DECLARE_ALL_VECTOR_COERCE_OPS(tlow, thigh) \ + DECLARE_ALL_VECTOR_COERCE_OPS_INNER(Vec2, tlow, thigh) \ + DECLARE_ALL_VECTOR_COERCE_OPS_INNER(Vec3, tlow, thigh) \ + DECLARE_ALL_VECTOR_COERCE_OPS_INNER(Vec4, tlow, thigh) \ \ - /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, long double, real) */ + DECLARE_VEC_3_COERCE_OPS(Vec3, static, tlow, thigh, thigh) \ + DECLARE_VEC_3_COERCE_OPS(Vec3, static, thigh, tlow, thigh) /* Disable warning about unary operator applied to unsigned type */ #if defined _MSC_VER @@ -1234,9 +1147,17 @@ static inline Quat operator /(Quat x, Quat const &y) DECLARE_ALL_NONVECTOR_OPS(Cmplx) DECLARE_ALL_NONVECTOR_OPS(Quat) -DECLARE_ALL_VECTOR_OPS(Vec2) -DECLARE_ALL_VECTOR_OPS(Vec3) -DECLARE_ALL_VECTOR_OPS(Vec4) +DECLARE_ALL_VECTOR_OPS(half) +DECLARE_ALL_VECTOR_OPS(float) +DECLARE_ALL_VECTOR_OPS(double) +DECLARE_ALL_VECTOR_OPS(int8_t) +DECLARE_ALL_VECTOR_OPS(uint8_t) +DECLARE_ALL_VECTOR_OPS(int16_t) +DECLARE_ALL_VECTOR_OPS(uint16_t) +DECLARE_ALL_VECTOR_OPS(int32_t) +DECLARE_ALL_VECTOR_OPS(uint32_t) +DECLARE_ALL_VECTOR_OPS(int64_t) +DECLARE_ALL_VECTOR_OPS(uint64_t) #if defined _MSC_VER # pragma warning(pop) @@ -1257,9 +1178,99 @@ DECLARE_ALL_VECTOR_OPS(Vec4) # pragma warning(disable: 4018) #endif -DECLARE_ALL_VECTOR_COERCE_OPS(Vec2) -DECLARE_ALL_VECTOR_COERCE_OPS(Vec3) -DECLARE_ALL_VECTOR_COERCE_OPS(Vec4) +/* Apply the same coercion rules as in the C++ standard. However, */ +/* instead of promoting int8_t etc. to int, we apply our own rules. */ +/* FIXME: "half" and "real" are deactivated for now, because we do */ +/* not implement all combinations of operators for these types yet. */ +DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, uint8_t) +DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, int16_t) +DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, uint16_t) +DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, int32_t) +DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, uint32_t) +DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, int64_t) +DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, uint64_t) +/* DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, half) */ +DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, float) +DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, double) +DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, long double) +/* DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, real) */ + +DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, int16_t) +DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, uint16_t) +DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, int32_t) +DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, uint32_t) +DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, int64_t) +DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, uint64_t) +/* DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, half) */ +DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, float) +DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, double) +DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, long double) +/* DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, real) */ + +DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, uint16_t) +DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, int32_t) +DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, uint32_t) +DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, int64_t) +DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, uint64_t) +/* DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, half) */ +DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, float) +DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, double) +DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, long double) +/* DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, real) */ + +DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, int32_t) +DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, uint32_t) +DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, int64_t) +DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, uint64_t) +/* DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, half) */ +DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, float) +DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, double) +DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, long double) +/* DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, real) */ + +DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, uint32_t) +DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, int64_t) +DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, uint64_t) +/* DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, half) */ +DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, float) +DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, double) +DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, long double) +/* DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, real) */ + +DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, int64_t) +DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, uint64_t) +/* DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, half) */ +DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, float) +DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, double) +DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, long double) +/* DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, real) */ + +DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, uint64_t) +/* DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, half) */ +DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, float) +DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, double) +DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, long double) +/* DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, real) */ + +/* DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, half) */ +DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, float) +DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, double) +DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, long double) +/* DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, real) */ + +/* DECLARE_ALL_VECTOR_COERCE_OPS(half, float) */ +/* DECLARE_ALL_VECTOR_COERCE_OPS(half, double) */ +/* DECLARE_ALL_VECTOR_COERCE_OPS(half, long double) */ +/* DECLARE_ALL_VECTOR_COERCE_OPS(half, real) */ + +DECLARE_ALL_VECTOR_COERCE_OPS(float, double) +DECLARE_ALL_VECTOR_COERCE_OPS(float, long double) +/* DECLARE_ALL_VECTOR_COERCE_OPS(float, real) */ + +DECLARE_ALL_VECTOR_COERCE_OPS(double, long double) +/* DECLARE_ALL_VECTOR_COERCE_OPS(double, real) */ + +/* DECLARE_ALL_VECTOR_COERCE_OPS(long double, real) */ #if defined __GNUC__ && (__GNUC__ >= 4) # pragma GCC diagnostic pop diff --git a/src/vector.cpp b/src/vector.cpp index a2541074..9dcf0af1 100644 --- a/src/vector.cpp +++ b/src/vector.cpp @@ -36,28 +36,6 @@ using namespace std; namespace lol { -float dot(vec2 v1, vec2 v2) -{ - return v1.x * v2.x + v1.y * v2.y; -} - -float dot(vec3 v1, vec3 v2) -{ - return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; -} - -float dot(vec4 v1, vec4 v2) -{ - return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w; -} - -template<> vec3 cross(vec3 v1, vec3 v2) -{ - return vec3(v1.y * v2.z - v1.z * v2.y, - v1.z * v2.x - v1.x * v2.z, - v1.x * v2.y - v1.y * v2.x); -} - static inline float det3(float a, float b, float c, float d, float e, float f, float g, float h, float i)