diff --git a/include/blt/math/math.h b/include/blt/math/math.h index 58abd31..adbba6c 100644 --- a/include/blt/math/math.h +++ b/include/blt/math/math.h @@ -12,12 +12,6 @@ namespace blt { - constexpr float EPSILON = 0.0001f; - - static inline constexpr bool f_equal(float v1, float v2) { - return v1 >= v2 - EPSILON && v1 <= v2 + EPSILON; - } - /** * fast number integer */ diff --git a/include/blt/math/vectors.h b/include/blt/math/vectors.h index bdf5d67..a6d6eb0 100644 --- a/include/blt/math/vectors.h +++ b/include/blt/math/vectors.h @@ -12,6 +12,12 @@ namespace blt { + constexpr float EPSILON = 0.0001f; + + static inline constexpr bool f_equal(float v1, float v2) { + return v1 >= v2 - EPSILON && v1 <= v2 + EPSILON; + } + template struct vec { private: @@ -130,14 +136,14 @@ namespace blt { /** * performs the dot product of left * right */ - static inline T dot(const vec& left, const vec& right) { + static inline constexpr T dot(const vec& left, const vec& right) { T dot = 0; for (int i = 0; i < size; i++) dot += left[i] * right[i]; return dot; } - static inline vec cross(const vec& left, const vec& right) { + static inline constexpr vec cross(const vec& left, const vec& right) { // cross is only defined on vectors of size 3. 2D could be implemented, which is a TODO static_assert(size == 3); return {left.y() * right.z() - left.z() * right.y(), @@ -145,7 +151,7 @@ namespace blt { left.x() * right.y() - left.y() * right.x()}; } - static inline vec project(const vec& u, const vec& v){ + static inline constexpr vec project(const vec& u, const vec& v){ float du = dot(u); float dv = dot(v); return (du / dv) * v; @@ -153,7 +159,7 @@ namespace blt { }; template - inline vec operator+(const vec& left, const vec& right) { + inline constexpr vec operator+(const vec& left, const vec& right) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] + right[i]; @@ -161,7 +167,7 @@ namespace blt { } template - inline vec operator-(const vec& left, const vec& right) { + inline constexpr vec operator-(const vec& left, const vec& right) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] - right[i]; @@ -169,7 +175,7 @@ namespace blt { } template - inline vec operator+(const vec& left, float f) { + inline constexpr vec operator+(const vec& left, float f) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] + f; @@ -177,7 +183,7 @@ namespace blt { } template - inline vec operator-(const vec& left, float f) { + inline constexpr vec operator-(const vec& left, float f) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] + f; @@ -185,7 +191,7 @@ namespace blt { } template - inline vec operator+(float f, const vec& right) { + inline constexpr vec operator+(float f, const vec& right) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = f + right[i]; @@ -193,7 +199,7 @@ namespace blt { } template - inline vec operator-(float f, const vec& right) { + inline constexpr vec operator-(float f, const vec& right) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = f - right[i]; @@ -201,7 +207,7 @@ namespace blt { } template - inline vec operator*(const vec& left, const vec& right) { + inline constexpr vec operator*(const vec& left, const vec& right) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] * right[i]; @@ -209,7 +215,7 @@ namespace blt { } template - inline vec operator*(const vec& left, float f) { + inline constexpr vec operator*(const vec& left, float f) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] * f; @@ -217,7 +223,7 @@ namespace blt { } template - inline vec operator*(float f, const vec& right) { + inline constexpr vec operator*(float f, const vec& right) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = f * right[i]; @@ -225,13 +231,29 @@ namespace blt { } template - inline vec operator/(const vec& left, float f) { + inline constexpr vec operator/(const vec& left, float f) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] / f; return vec{initializer}; } + template + inline constexpr bool operator==(const vec& left, const vec& right) { + for (int i = 0; i < size; i++) + if (left[i] != right[i]) + return false; + return true; + } + + template + inline constexpr bool operator&&(const vec& left, const vec& right) { + for (int i = 0; i < size; i++) + if (!f_equal(left[i], right[i])) + return false; + return true; + } + typedef vec vec2f; typedef vec vec3f; typedef vec vec4f;