diff --git a/headers/vmath.hpp/vmath_ext.hpp b/headers/vmath.hpp/vmath_ext.hpp index ff011fe..88eb349 100644 --- a/headers/vmath.hpp/vmath_ext.hpp +++ b/headers/vmath.hpp/vmath_ext.hpp @@ -242,13 +242,13 @@ namespace vmath_hpp } template < typename T > - [[nodiscard]] constexpr mat translate(const vec& v) { - return translate(v.x, v.y, v.z); + [[nodiscard]] constexpr mat translate(const mat& m, T x, T y, T z) { + return m * translate(x, y, z); } template < typename T > - [[nodiscard]] constexpr mat translate(const mat& m, T x, T y, T z) { - return m * translate(x, y, z); + [[nodiscard]] constexpr mat translate(const vec& v) { + return translate(v.x, v.y, v.z); } template < typename T > @@ -288,6 +288,11 @@ namespace vmath_hpp 0, 0, 0, 1}; } + template < typename T > + [[nodiscard]] mat rotate(const mat& m, const qua& q) { + return m * rotate(q); + } + template < typename T > [[nodiscard]] mat rotate(T angle, const vec& axis) { /// REFERENCE: @@ -326,11 +331,6 @@ namespace vmath_hpp return m * rotate(angle, axis); } - template < typename T > - [[nodiscard]] mat rotate(const mat& m, const qua& q) { - return m * rotate(q); - } - template < typename T > [[nodiscard]] mat rotate_x(T angle) { /// REFERENCE: @@ -403,13 +403,13 @@ namespace vmath_hpp } template < typename T > - [[nodiscard]] constexpr mat scale(const vec& v) { - return scale(v.x, v.y, v.z); + [[nodiscard]] constexpr mat scale(const mat& m, T x, T y, T z) { + return m * scale(x, y, z); } template < typename T > - [[nodiscard]] constexpr mat scale(const mat& m, T x, T y, T z) { - return m * scale(x, y, z); + [[nodiscard]] constexpr mat scale(const vec& v) { + return scale(v.x, v.y, v.z); } template < typename T > @@ -480,13 +480,13 @@ namespace vmath_hpp } template < typename T > - [[nodiscard]] constexpr mat translate(const vec& v) { - return translate(v.x, v.y); + [[nodiscard]] constexpr mat translate(const mat& m, T x, T y) { + return m * translate(x, y); } template < typename T > - [[nodiscard]] constexpr mat translate(const mat& m, T x, T y) { - return m * translate(x, y); + [[nodiscard]] constexpr mat translate(const vec& v) { + return translate(v.x, v.y); } template < typename T > @@ -528,13 +528,13 @@ namespace vmath_hpp } template < typename T > - [[nodiscard]] constexpr mat scale(const vec& v) { - return scale(v.x, v.y); + [[nodiscard]] constexpr mat scale(const mat& m, T x, T y) { + return m * scale(x, y); } template < typename T > - [[nodiscard]] constexpr mat scale(const mat& m, T x, T y) { - return m * scale(x, y); + [[nodiscard]] constexpr mat scale(const vec& v) { + return scale(v.x, v.y); } template < typename T > @@ -556,13 +556,13 @@ namespace vmath_hpp } template < typename T > - [[nodiscard]] constexpr mat shear(const vec& v) { - return shear(v.x, v.y); + [[nodiscard]] constexpr mat shear(const mat& m, T x, T y) { + return m * shear(x, y); } template < typename T > - [[nodiscard]] constexpr mat shear(const mat& m, T x, T y) { - return m * shear(x, y); + [[nodiscard]] constexpr mat shear(const vec& v) { + return shear(v.x, v.y); } template < typename T > @@ -842,7 +842,7 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] constexpr vec project(const vec& v, const vec& normal) { - return dot(v, normal) / dot(normal, normal) * normal; + return dot(v, normal) * rcp(length2(normal)) * normal; } // perpendicular diff --git a/headers/vmath.hpp/vmath_fun.hpp b/headers/vmath.hpp/vmath_fun.hpp index 56276b6..0f89f7a 100644 --- a/headers/vmath.hpp/vmath_fun.hpp +++ b/headers/vmath.hpp/vmath_fun.hpp @@ -137,7 +137,7 @@ namespace vmath_hpp template < typename T > [[nodiscard]] std::enable_if_t, T> constexpr smoothstep(T edge0, T edge1, T x) noexcept { - const T t = clamp((x - edge0) / (edge1 - edge0), T(0), T(1)); + const T t = clamp((x - edge0) * rcp(edge1 - edge0), T(0), T(1)); return t * t * (T(3) - T(2) * t); } @@ -376,7 +376,7 @@ namespace vmath_hpp template < typename T > [[nodiscard]] std::enable_if_t, T> normalize(T x) noexcept { - return x * rsqrt(dot(x, x)); + return x * rsqrt(length2(x)); } template < typename T > diff --git a/headers/vmath.hpp/vmath_mat_fun.hpp b/headers/vmath.hpp/vmath_mat_fun.hpp index 1d659d9..3dc1eda 100644 --- a/headers/vmath.hpp/vmath_mat_fun.hpp +++ b/headers/vmath.hpp/vmath_mat_fun.hpp @@ -501,22 +501,16 @@ namespace vmath_hpp namespace vmath_hpp { - // any - template < typename T, std::size_t Size > [[nodiscard]] constexpr bool any(const mat& xs) { return fold_join([](bool acc, const vec& x){ return acc || any(x); }, false, xs); } - // all - template < typename T, std::size_t Size > [[nodiscard]] constexpr bool all(const mat& xs) { return fold_join([](bool acc, const vec& x){ return acc && all(x); }, true, xs); } - // approx - template < typename T, std::size_t Size > [[nodiscard]] constexpr mat approx(const mat& xs, const mat& ys) { return map_join([](const vec& x, const vec& y){ return approx(x, y); }, xs, ys); @@ -527,43 +521,31 @@ namespace vmath_hpp return map_join([epsilon](const vec& x, const vec& y){ return approx(x, y, epsilon); }, xs, ys); } - // less - template < typename T, std::size_t Size > [[nodiscard]] constexpr mat less(const mat& xs, const mat& ys) { return map_join([](const vec& x, const vec& y){ return less(x, y); }, xs, ys); } - // less_equal - template < typename T, std::size_t Size > [[nodiscard]] constexpr mat less_equal(const mat& xs, const mat& ys) { return map_join([](const vec& x, const vec& y){ return less_equal(x, y); }, xs, ys); } - // greater - template < typename T, std::size_t Size > [[nodiscard]] constexpr mat greater(const mat& xs, const mat& ys) { return map_join([](const vec& x, const vec& y){ return greater(x, y); }, xs, ys); } - // greater_equal - template < typename T, std::size_t Size > [[nodiscard]] constexpr mat greater_equal(const mat& xs, const mat& ys) { return map_join([](const vec& x, const vec& y){ return greater_equal(x, y); }, xs, ys); } - // equal_to - template < typename T, std::size_t Size > [[nodiscard]] constexpr mat equal_to(const mat& xs, const mat& ys) { return map_join([](const vec& x, const vec& y){ return equal_to(x, y); }, xs, ys); } - // not_equal_to - template < typename T, std::size_t Size > [[nodiscard]] constexpr mat not_equal_to(const mat& xs, const mat& ys) { return map_join([](const vec& x, const vec& y){ return not_equal_to(x, y); }, xs, ys); diff --git a/headers/vmath.hpp/vmath_qua_fun.hpp b/headers/vmath.hpp/vmath_qua_fun.hpp index 28e6cda..b9bde5f 100644 --- a/headers/vmath.hpp/vmath_qua_fun.hpp +++ b/headers/vmath.hpp/vmath_qua_fun.hpp @@ -241,7 +241,7 @@ namespace vmath_hpp } template < typename T > - [[nodiscard]] constexpr qua normalize(const qua& xs) { + [[nodiscard]] qua normalize(const qua& xs) { return qua(normalize(vec{xs})); } } @@ -252,8 +252,6 @@ namespace vmath_hpp namespace vmath_hpp { - // approx - template < typename T > [[nodiscard]] constexpr vec approx(const qua& xs, const qua& ys) { return approx(vec{xs}, vec{ys}); @@ -264,43 +262,31 @@ namespace vmath_hpp return approx(vec{xs}, vec{ys}, epsilon); } - // less - template < typename T > [[nodiscard]] constexpr vec less(const qua& xs, const qua& ys) { return less(vec{xs}, vec{ys}); } - // less_equal - template < typename T > [[nodiscard]] constexpr vec less_equal(const qua& xs, const qua& ys) { return less_equal(vec{xs}, vec{ys}); } - // greater - template < typename T > [[nodiscard]] constexpr vec greater(const qua& xs, const qua& ys) { return greater(vec{xs}, vec{ys}); } - // greater_equal - template < typename T > [[nodiscard]] constexpr vec greater_equal(const qua& xs, const qua& ys) { return greater_equal(vec{xs}, vec{ys}); } - // equal_to - template < typename T > [[nodiscard]] constexpr vec equal_to(const qua& xs, const qua& ys) { return equal_to(vec{xs}, vec{ys}); } - // not_equal_to - template < typename T > [[nodiscard]] constexpr vec not_equal_to(const qua& xs, const qua& ys) { return not_equal_to(vec{xs}, vec{ys}); @@ -324,6 +310,6 @@ namespace vmath_hpp template < typename T > [[nodiscard]] constexpr qua inverse(const qua& q) { - return conjugate(q) * rcp(dot(q, q)); + return conjugate(q) * rcp(length2(q)); } } diff --git a/headers/vmath.hpp/vmath_vec_fun.hpp b/headers/vmath.hpp/vmath_vec_fun.hpp index 95251d1..df12117 100644 --- a/headers/vmath.hpp/vmath_vec_fun.hpp +++ b/headers/vmath.hpp/vmath_vec_fun.hpp @@ -495,141 +495,6 @@ namespace vmath_hpp } } -// -// Angle and Trigonometric Functions -// - -namespace vmath_hpp -{ - template < typename T, std::size_t Size > - [[nodiscard]] constexpr vec radians(const vec& degrees) { - return map_join([](T x) { return radians(x); }, degrees); - } - - template < typename T, std::size_t Size > - [[nodiscard]] constexpr vec degrees(const vec& radians) { - return map_join([](T x) { return degrees(x); }, radians); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec sin(const vec& xs) { - return map_join([](T x) { return sin(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec cos(const vec& xs) { - return map_join([](T x) { return cos(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec tan(const vec& xs) { - return map_join([](T x) { return tan(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec asin(const vec& xs) { - return map_join([](T x) { return asin(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec acos(const vec& xs) { - return map_join([](T x) { return acos(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec atan(const vec& xs) { - return map_join([](T x) { return atan(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec atan2(const vec& ys, const vec& xs) { - return map_join([](T y, T x) { return atan2(y, x); }, ys, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec sinh(const vec& xs) { - return map_join([](T x) { return sinh(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec cosh(const vec& xs) { - return map_join([](T x) { return cosh(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec tanh(const vec& xs) { - return map_join([](T x) { return tanh(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec asinh(const vec& xs) { - return map_join([](T x) { return asinh(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec acosh(const vec& xs) { - return map_join([](T x) { return acosh(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec atanh(const vec& xs) { - return map_join([](T x) { return atanh(x); }, xs); - } - - template < typename T, std::size_t Size > - std::pair, vec> sincos(const vec& xs) { - return { sin(xs), cos(xs) }; - } - - template < typename T, std::size_t Size > - void sincos(const vec& xs, vec* ss, vec* cs) { - *ss = sin(xs); - *cs = cos(xs); - } -} - -// -// Exponential Functions -// - -namespace vmath_hpp -{ - template < typename T, std::size_t Size > - [[nodiscard]] vec pow(const vec& xs, const vec& ys) { - return map_join([](T x, T y) { return pow(x, y); }, xs, ys); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec exp(const vec& xs) { - return map_join([](T x) { return exp(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec log(const vec& xs) { - return map_join([](T x) { return log(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec exp2(const vec& xs) { - return map_join([](T x) { return exp2(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec log2(const vec& xs) { - return map_join([](T x) { return log2(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec sqrt(const vec& xs) { - return map_join([](T x) { return sqrt(x); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] vec rsqrt(const vec& xs) { - return map_join([](T x) { return rsqrt(x); }, xs); - } -} - // // Common Functions // @@ -849,6 +714,141 @@ namespace vmath_hpp } } +// +// Angle and Trigonometric Functions +// + +namespace vmath_hpp +{ + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec radians(const vec& degrees) { + return map_join([](T x) { return radians(x); }, degrees); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec degrees(const vec& radians) { + return map_join([](T x) { return degrees(x); }, radians); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec sin(const vec& xs) { + return map_join([](T x) { return sin(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec cos(const vec& xs) { + return map_join([](T x) { return cos(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec tan(const vec& xs) { + return map_join([](T x) { return tan(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec asin(const vec& xs) { + return map_join([](T x) { return asin(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec acos(const vec& xs) { + return map_join([](T x) { return acos(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec atan(const vec& xs) { + return map_join([](T x) { return atan(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec atan2(const vec& ys, const vec& xs) { + return map_join([](T y, T x) { return atan2(y, x); }, ys, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec sinh(const vec& xs) { + return map_join([](T x) { return sinh(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec cosh(const vec& xs) { + return map_join([](T x) { return cosh(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec tanh(const vec& xs) { + return map_join([](T x) { return tanh(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec asinh(const vec& xs) { + return map_join([](T x) { return asinh(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec acosh(const vec& xs) { + return map_join([](T x) { return acosh(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec atanh(const vec& xs) { + return map_join([](T x) { return atanh(x); }, xs); + } + + template < typename T, std::size_t Size > + std::pair, vec> sincos(const vec& xs) { + return { sin(xs), cos(xs) }; + } + + template < typename T, std::size_t Size > + void sincos(const vec& xs, vec* ss, vec* cs) { + *ss = sin(xs); + *cs = cos(xs); + } +} + +// +// Exponential Functions +// + +namespace vmath_hpp +{ + template < typename T, std::size_t Size > + [[nodiscard]] vec pow(const vec& xs, const vec& ys) { + return map_join([](T x, T y) { return pow(x, y); }, xs, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec exp(const vec& xs) { + return map_join([](T x) { return exp(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec log(const vec& xs) { + return map_join([](T x) { return log(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec exp2(const vec& xs) { + return map_join([](T x) { return exp2(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec log2(const vec& xs) { + return map_join([](T x) { return log2(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec sqrt(const vec& xs) { + return map_join([](T x) { return sqrt(x); }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] vec rsqrt(const vec& xs) { + return map_join([](T x) { return rsqrt(x); }, xs); + } +} + // // Geometric Functions // @@ -897,7 +897,7 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] vec normalize(const vec& xs) { - return xs * rsqrt(dot(xs, xs)); + return xs * rsqrt(length2(xs)); } template < typename T, std::size_t Size > @@ -924,22 +924,16 @@ namespace vmath_hpp namespace vmath_hpp { - // any - template < typename T, std::size_t Size > [[nodiscard]] constexpr bool any(const vec& xs) { return fold_join([](bool acc, T x){ return acc || any(x); }, false, xs); } - // all - template < typename T, std::size_t Size > [[nodiscard]] constexpr bool all(const vec& xs) { return fold_join([](bool acc, T x){ return acc && all(x); }, true, xs); } - // approx - template < typename T, std::size_t Size > [[nodiscard]] constexpr vec approx(const vec& xs, const vec& ys) { return map_join([](T x, T y){ return approx(x, y); }, xs, ys); @@ -950,43 +944,31 @@ namespace vmath_hpp return map_join([epsilon](T x, T y){ return approx(x, y, epsilon); }, xs, ys); } - // less - template < typename T, std::size_t Size > [[nodiscard]] constexpr vec less(const vec& xs, const vec& ys) { return map_join([](T x, T y){ return less(x, y); }, xs, ys); } - // less_equal - template < typename T, std::size_t Size > [[nodiscard]] constexpr vec less_equal(const vec& xs, const vec& ys) { return map_join([](T x, T y){ return less_equal(x, y); }, xs, ys); } - // greater - template < typename T, std::size_t Size > [[nodiscard]] constexpr vec greater(const vec& xs, const vec& ys) { return map_join([](T x, T y){ return greater(x, y); }, xs, ys); } - // greater_equal - template < typename T, std::size_t Size > [[nodiscard]] constexpr vec greater_equal(const vec& xs, const vec& ys) { return map_join([](T x, T y){ return greater_equal(x, y); }, xs, ys); } - // equal_to - template < typename T, std::size_t Size > [[nodiscard]] constexpr vec equal_to(const vec& xs, const vec& ys) { return map_join([](T x, T y){ return equal_to(x, y); }, xs, ys); } - // not_equal_to - template < typename T, std::size_t Size > [[nodiscard]] constexpr vec not_equal_to(const vec& xs, const vec& ys) { return map_join([](T x, T y){ return not_equal_to(x, y); }, xs, ys);