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

@@ -1302,9 +1302,15 @@ constexpr T dot(T x, T y) noexcept;
template < arithmetic T >
constexpr T length(T x) noexcept;
template < arithmetic T >
constexpr T rlength(T x) noexcept;
template < arithmetic T >
constexpr T length2(T x) noexcept;
template < arithmetic T >
constexpr T rlength2(T x) noexcept;
template < arithmetic T >
constexpr T distance(T x, T y) noexcept;
@@ -1324,9 +1330,15 @@ constexpr T dot(const vec<T, Size>& xs, const vec<T, Size>& ys);
template < typename T, size_t Size >
T length(const vec<T, Size>& xs);
template < typename T, size_t Size >
T rlength(const vec<T, Size>& xs);
template < typename T, size_t Size >
constexpr T length2(const vec<T, Size>& xs);
template < typename T, size_t Size >
constexpr T rlength2(const vec<T, Size>& xs);
template < typename T, size_t Size >
T distance(const vec<T, Size>& xs, const vec<T, Size>& ys);
@@ -1352,9 +1364,15 @@ constexpr T dot(const qua<T>& xs, const qua<T>& ys);
template < typename T >
T length(const qua<T>& xs);
template < typename T >
T rlength(const qua<T>& xs);
template < typename T >
constexpr T length2(const qua<T>& xs);
template < typename T >
constexpr T rlength2(const qua<T>& xs);
template < typename T >
T distance(const qua<T>& xs, const qua<T>& ys);

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);
}
}

View File

@@ -256,7 +256,9 @@ namespace vmath_hpp
{
template fix<float> dot(const fix2f&, const fix2f&);
template fix<float> length(const fix2f&);
template fix<float> rlength(const fix2f&);
template fix<float> length2(const fix2f&);
template fix<float> rlength2(const fix2f&);
template fix<float> distance(const fix2f&, const fix2f&);
template fix<float> distance2(const fix2f&, const fix2f&);
template fix<float> cross(const fix2f&, const fix2f&);
@@ -268,7 +270,9 @@ namespace vmath_hpp
{
template fix<float> dot(const qfix&, const qfix&);
template fix<float> length(const qfix&);
template fix<float> rlength(const qfix&);
template fix<float> length2(const qfix&);
template fix<float> rlength2(const qfix&);
template fix<float> distance(const qfix&, const qfix&);
template qfix normalize(const qfix&);
}

View File

@@ -107,9 +107,15 @@ TEST_CASE("vmath/fun") {
STATIC_CHECK(length(10.f) == uapprox(10.f));
STATIC_CHECK(length(-10.f) == uapprox(10.f));
STATIC_CHECK(rlength(10.f) == uapprox(0.1f));
STATIC_CHECK(rlength(-10.f) == uapprox(0.1f));
STATIC_CHECK(length2(10.f) == uapprox(100.f));
STATIC_CHECK(length2(-10.f) == uapprox(100.f));
STATIC_CHECK(rlength2(10.f) == uapprox(0.01f));
STATIC_CHECK(rlength2(-10.f) == uapprox(0.01f));
STATIC_CHECK(distance(5.f, 10.f) == uapprox(5.f));
STATIC_CHECK(distance(-5.f, -10.f) == uapprox(5.f));

View File

@@ -136,9 +136,15 @@ TEST_CASE("vmath/qua_fun") {
CHECK(length(qfloat(10.f,0.f,0.f,0.f)) == uapprox(10.f));
CHECK(length(qfloat(-10.f,0.f,0.f,0.f)) == uapprox(10.f));
CHECK(rlength(qfloat(10.f,0.f,0.f,0.f)) == uapprox(0.1f));
CHECK(rlength(qfloat(-10.f,0.f,0.f,0.f)) == uapprox(0.1f));
STATIC_CHECK(length2(qfloat(10.f,0.f,0.f,0.f)) == uapprox(100.f));
STATIC_CHECK(length2(qfloat(-10.f,0.f,0.f,0.f)) == uapprox(100.f));
STATIC_CHECK(rlength2(qfloat(10.f,0.f,0.f,0.f)) == uapprox(0.01f));
STATIC_CHECK(rlength2(qfloat(-10.f,0.f,0.f,0.f)) == uapprox(0.01f));
CHECK(distance(qrotate_z(radians(0.f)) * 2.f, qrotate_z(radians(0.f)) * 1.5f) == uapprox(radians(0.f)));
CHECK(distance(qrotate_z(radians(0.f)) * 3.f, qrotate_z(radians(360.f)) * 2.5f) == uapprox(radians(0.f)));
CHECK(distance(qrotate_z(radians(0.f)) * 4.f, qrotate_z(radians(180.f)) * 3.5f) == uapprox(radians(180.f)));

View File

@@ -251,9 +251,15 @@ TEST_CASE("vmath/vec_fun") {
CHECK(length(float2(10.f,0.f)) == uapprox(10.f));
CHECK(length(float2(-10.f,0.f)) == uapprox(10.f));
CHECK(rlength(float2(10.f,0.f)) == uapprox(0.1f));
CHECK(rlength(float2(-10.f,0.f)) == uapprox(0.1f));
STATIC_CHECK(length2(float2(10.f,0.f)) == uapprox(100.f));
STATIC_CHECK(length2(float2(-10.f,0.f)) == uapprox(100.f));
STATIC_CHECK(rlength2(float2(10.f,0.f)) == uapprox(0.01f));
STATIC_CHECK(rlength2(float2(-10.f,0.f)) == uapprox(0.01f));
CHECK(distance(float2(5.f,0.f), float2(10.f,0.f)) == uapprox(5.f));
CHECK(distance(float2(-5.f,0.f), float2(-10.f,0.f)) == uapprox(5.f));