diff --git a/math/math.hpp b/math/math.hpp index d66df29..4a79a8c 100644 --- a/math/math.hpp +++ b/math/math.hpp @@ -39,13 +39,13 @@ constexpr float abs(const float n) noexcept constexpr float pi = 3.141592653589793; template -inline constexpr float max(const T a, const T b) noexcept +inline constexpr T max(const T a, const T b) noexcept { return (a > b) ? a : b; } template -inline constexpr float min(const T a, const T b) noexcept +inline constexpr T min(const T a, const T b) noexcept { return (a < b) ? a : b; } diff --git a/math/vec.hpp b/math/vec.hpp index a63d225..930fe10 100644 --- a/math/vec.hpp +++ b/math/vec.hpp @@ -2,3 +2,27 @@ template struct vec; + +template +inline constexpr T magnitude(vec const& v) +{ + return sqrt(dot(v, v)); +} + +template +inline constexpr T magnitude_squared(vec const& v) +{ + return dot(v, v); +} + +template +inline constexpr vec<3, T> normalize(vec const& v) +{ + return v / magnitude(v); +} + +template +inline constexpr vec<3, T> reflect(vec const& i, vec const& n) +{ + return i - dot(n, i) * n * static_cast(2.0); +} diff --git a/math/vec3.hpp b/math/vec3.hpp index 9ae7e2b..98240aa 100644 --- a/math/vec3.hpp +++ b/math/vec3.hpp @@ -137,8 +137,10 @@ inline constexpr vec<3, T> operator/(vec<3, T> const& v1, T const& scalar) template inline constexpr T dot(vec<3, T> const& v1, vec<3, T> const& v2) { - vec<3, T> tmp(v1 * v2); - return tmp.x + tmp.y + tmp.z; + return + v1.x * v2.x + + v1.y * v2.y + + v1.z * v2.z; } template @@ -160,21 +162,3 @@ inline constexpr vec<3, U> functor1(U (&func) (T const& x), vec<3, T> const& v) { return vec<3, U>(func(v.x), func(v.y), func(v.z)); } - -template -inline constexpr T magnitude(vec<3, T> const& v) -{ - return sqrt(dot(v, v)); -} - -template -inline constexpr vec<3, T> normalize(vec<3, T> const& v) -{ - return v / magnitude(v); -} - -template -inline constexpr vec<3, T> reflect(vec<3, T> const& i, vec<3, T> const& n) -{ - return i - dot(n, i) * n * static_cast(2.0); -}