normalized_to_point/point_to_normalized for rect

This commit is contained in:
2018-10-24 20:47:33 +07:00
parent 8a36c54d2b
commit 816183071b
3 changed files with 78 additions and 0 deletions

View File

@@ -495,4 +495,21 @@ namespace e2d { namespace math
constexpr std::underlying_type_t<E> enum_to_number(E e) noexcept {
return static_cast<std::underlying_type_t<E>>(e);
}
//
// lerp/inverse_lerp
//
template < typename T >
std::enable_if_t<std::is_floating_point<T>::value, T>
lerp(T l, T r, T v) noexcept {
return l + (r - l) * v;
}
template < typename T >
std::enable_if_t<std::is_floating_point<T>::value, T>
inverse_lerp(T l, T r, T v) noexcept {
E2D_ASSERT(!is_near_zero(r - l, T(0)));
return (v - l) / (r - l);
}
}}

View File

@@ -233,6 +233,32 @@ namespace e2d { namespace math
&& min_l.y < max_r.y;
}
//
// normalized_to_point/point_to_normalized
//
template < typename T >
std::enable_if_t<std::is_floating_point<T>::value, vec2<T>>
normalized_to_point(const rect<T>& r, const vec2<T>& p) noexcept {
const vec2<T> min = minimum(r);
const vec2<T> max = maximum(r);
return {
math::lerp(min.x, max.x, p.x),
math::lerp(min.y, max.y, p.y)};
}
template < typename T >
std::enable_if_t<std::is_floating_point<T>::value, vec2<T>>
point_to_normalized(const rect<T>& r, const vec2<T>& p) noexcept {
E2D_ASSERT(!math::is_near_zero(r.size.x, T(0)));
E2D_ASSERT(!math::is_near_zero(r.size.y, T(0)));
const vec2<T> min = minimum(r);
const vec2<T> max = maximum(r);
return {
math::inverse_lerp(min.x, max.x, p.x),
math::inverse_lerp(min.y, max.y, p.y)};
}
//
// contains_nan
//

View File

@@ -125,4 +125,39 @@ TEST_CASE("rect") {
REQUIRE(math::contains_nan(r4f(1.f,std::numeric_limits<f32>::quiet_NaN())));
REQUIRE(math::contains_nan(r4f(std::numeric_limits<f32>::infinity(), 1.f)));
}
{
REQUIRE(math::normalized_to_point(r4f(10.f, 20.f), v2f(0.f, 0.f)) == v2f(0.f, 0.f));
REQUIRE(math::normalized_to_point(r4f(10.f, 20.f), v2f(0.5f, 0.5f)) == v2f(5.f, 10.f));
REQUIRE(math::normalized_to_point(r4f(10.f, 20.f), v2f(0.25f, 0.75f)) == v2f(2.5f, 15.f));
REQUIRE(math::normalized_to_point(r4f(10.f, 20.f), v2f(1.f, 1.f)) == v2f(10.f, 20.f));
REQUIRE(math::normalized_to_point(r4f(1.f, 2.f, 10.f, 20.f), v2f(0.f, 0.f)) == v2f(1.f, 2.f));
REQUIRE(math::normalized_to_point(r4f(1.f, 2.f, 10.f, 20.f), v2f(0.5f, 0.5f)) == v2f(6.f, 12.f));
REQUIRE(math::normalized_to_point(r4f(1.f, 2.f, 10.f, 20.f), v2f(0.25f, 0.75f)) == v2f(3.5f, 17.f));
REQUIRE(math::normalized_to_point(r4f(1.f, 2.f, 10.f, 20.f), v2f(1.f, 1.f)) == v2f(11.f, 22.f));
REQUIRE(math::normalized_to_point(r4f(1.f, 2.f, -11.f, -22.f), v2f(0.f, 0.f)) == v2f(-10.f, -20.f));
REQUIRE(math::normalized_to_point(r4f(1.f, 2.f, -11.f, -22.f), v2f(0.5f, 0.5f)) == v2f(-4.5f, -9.f));
REQUIRE(math::normalized_to_point(r4f(1.f, 2.f, -11.f, -22.f), v2f(1.f, 1.f)) == v2f(1.f, 2.f));
REQUIRE(math::normalized_to_point(r4f(0.f,0.f), v2f(0.f,0.f)) == v2f(0.f,0.f));
REQUIRE(math::normalized_to_point(r4f(0.f,0.f), v2f(1.f,1.f)) == v2f(0.f,0.f));
REQUIRE(math::normalized_to_point(r4f(1.f,2.f,0.f,0.f), v2f(0.f,0.f)) == v2f(1.f,2.f));
REQUIRE(math::normalized_to_point(r4f(1.f,2.f,0.f,0.f), v2f(1.f,1.f)) == v2f(1.f,2.f));
}
{
REQUIRE(math::point_to_normalized(r4f(10.f, 20.f), v2f(0.f, 0.f)) == v2f(0.f, 0.f));
REQUIRE(math::point_to_normalized(r4f(10.f, 20.f), v2f(5.f, 10.f)) == v2f(0.5f,0.5f));
REQUIRE(math::point_to_normalized(r4f(10.f, 20.f), v2f(2.5f, 15.f)) == v2f(0.25f, 0.75f));
REQUIRE(math::point_to_normalized(r4f(10.f, 20.f), v2f(10.f, 20.f)) == v2f(1.f, 1.f));
REQUIRE(math::point_to_normalized(r4f(1.f, 2.f, 10.f, 20.f), v2f(1.f, 2.f)) == v2f(0.f, 0.f));
REQUIRE(math::point_to_normalized(r4f(1.f, 2.f, 10.f, 20.f), v2f(6.f, 12.f)) == v2f(0.5f, 0.5f));
REQUIRE(math::point_to_normalized(r4f(1.f, 2.f, 10.f, 20.f), v2f(3.5f, 17.f)) == v2f(0.25f, 0.75f));
REQUIRE(math::point_to_normalized(r4f(1.f, 2.f, 10.f, 20.f), v2f(11.f, 22.f)) == v2f(1.f, 1.f));
REQUIRE(math::point_to_normalized(r4f(1.f, 2.f, -11.f, -22.f), v2f(-10.f, -20.f)) == v2f(0.f, 0.f));
REQUIRE(math::point_to_normalized(r4f(1.f, 2.f, -11.f, -22.f), v2f(-4.5f, -9.f)) == v2f(0.5f, 0.5f));
REQUIRE(math::point_to_normalized(r4f(1.f, 2.f, -11.f, -22.f), v2f(1.f, 2.f)) == v2f(1.f, 1.f));
}
}