math/vec3: move length-agnostic functions to math/vec
This commit is contained in:
parent
7a8aca069c
commit
45d8853bbc
@ -39,13 +39,13 @@ constexpr float abs(const float n) noexcept
|
||||
constexpr float pi = 3.141592653589793;
|
||||
|
||||
template <typename T>
|
||||
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 <typename T>
|
||||
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;
|
||||
}
|
||||
|
24
math/vec.hpp
24
math/vec.hpp
@ -2,3 +2,27 @@
|
||||
|
||||
template <int L, typename T>
|
||||
struct vec;
|
||||
|
||||
template <int L, typename T>
|
||||
inline constexpr T magnitude(vec<L, T> const& v)
|
||||
{
|
||||
return sqrt(dot(v, v));
|
||||
}
|
||||
|
||||
template <int L, typename T>
|
||||
inline constexpr T magnitude_squared(vec<L, T> const& v)
|
||||
{
|
||||
return dot(v, v);
|
||||
}
|
||||
|
||||
template <int L, typename T>
|
||||
inline constexpr vec<3, T> normalize(vec<L, T> const& v)
|
||||
{
|
||||
return v / magnitude(v);
|
||||
}
|
||||
|
||||
template <int L, typename T>
|
||||
inline constexpr vec<3, T> reflect(vec<L, T> const& i, vec<L, T> const& n)
|
||||
{
|
||||
return i - dot(n, i) * n * static_cast<T>(2.0);
|
||||
}
|
||||
|
@ -137,8 +137,10 @@ inline constexpr vec<3, T> operator/(vec<3, T> const& v1, T const& scalar)
|
||||
template <typename T>
|
||||
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 <typename T>
|
||||
@ -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 <typename T>
|
||||
inline constexpr T magnitude(vec<3, T> const& v)
|
||||
{
|
||||
return sqrt(dot(v, v));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline constexpr vec<3, T> normalize(vec<3, T> const& v)
|
||||
{
|
||||
return v / magnitude(v);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline constexpr vec<3, T> reflect(vec<3, T> const& i, vec<3, T> const& n)
|
||||
{
|
||||
return i - dot(n, i) * n * static_cast<T>(2.0);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user