rlength and rlength2 functions

This commit is contained in:
BlackMATov
2021-02-23 10:24:49 +07:00
parent 2bded8a680
commit 6e3c1ba523
9 changed files with 76 additions and 4 deletions

View File

@@ -877,7 +877,7 @@ namespace vmath_hpp
template < typename T, std::size_t Size >
[[nodiscard]] constexpr vec<T, Size> project(const vec<T, Size>& v, const vec<T, Size>& normal) {
return dot(v, normal) * rcp(length2(normal)) * normal;
return dot(v, normal) * rlength2(normal) * normal;
}
// perpendicular

View File

@@ -317,12 +317,24 @@ namespace vmath_hpp
return abs(x);
}
template < typename T >
[[nodiscard]] std::enable_if_t<std::is_arithmetic_v<T>, T>
constexpr rlength(T x) noexcept {
return rcp(abs(x));
}
template < typename T >
[[nodiscard]] std::enable_if_t<std::is_arithmetic_v<T>, T>
constexpr length2(T x) noexcept {
return dot(x, x);
}
template < typename T >
[[nodiscard]] std::enable_if_t<std::is_arithmetic_v<T>, T>
constexpr rlength2(T x) noexcept {
return rcp(dot(x, x));
}
template < typename T >
[[nodiscard]] std::enable_if_t<std::is_arithmetic_v<T>, T>
constexpr distance(T x, T y) noexcept {
@@ -338,7 +350,7 @@ namespace vmath_hpp
template < typename T >
[[nodiscard]] std::enable_if_t<std::is_floating_point_v<T>, T>
normalize(T x) noexcept {
return x * rsqrt(length2(x));
return x * rlength(x);
}
}

View File

@@ -259,11 +259,21 @@ namespace vmath_hpp
return length(vec{xs});
}
template < typename T >
[[nodiscard]] T rlength(const qua<T>& xs) {
return rlength(vec{xs});
}
template < typename T >
[[nodiscard]] constexpr T length2(const qua<T>& xs) {
return length2(vec{xs});
}
template < typename T >
[[nodiscard]] constexpr T rlength2(const qua<T>& xs) {
return rlength2(vec{xs});
}
template < typename T >
[[nodiscard]] T distance(const qua<T>& xs, const qua<T>& ys) {
const qua zs = xs * conjugate(ys);
@@ -377,6 +387,6 @@ namespace vmath_hpp
template < typename T >
[[nodiscard]] constexpr qua<T> inverse(const qua<T>& q) {
return conjugate(q) * rcp(length2(q));
return conjugate(q) * rlength2(q);
}
}

View File

@@ -778,11 +778,21 @@ namespace vmath_hpp
return sqrt(dot(xs, xs));
}
template < typename T, std::size_t Size >
[[nodiscard]] T rlength(const vec<T, Size>& xs) {
return rsqrt(dot(xs, xs));
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr T length2(const vec<T, Size>& xs) {
return dot(xs, xs);
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr T rlength2(const vec<T, Size>& xs) {
return rcp(dot(xs, xs));
}
template < typename T, std::size_t Size >
[[nodiscard]] T distance(const vec<T, Size>& xs, const vec<T, Size>& ys) {
return length(ys - xs);
@@ -808,7 +818,7 @@ namespace vmath_hpp
template < typename T, std::size_t Size >
[[nodiscard]] vec<T, Size> normalize(const vec<T, Size>& xs) {
return xs * rsqrt(length2(xs));
return xs * rlength(xs);
}
}