From b01173d82b6dc434056eee96d8d80d9b7a07cb26 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 3 May 2018 21:43:48 +1000 Subject: [PATCH] maths: rename limit to clamp --- bezier1.cpp | 2 +- coord/ops.hpp | 14 +++++++------- geom/aabb.hpp | 2 +- maths.cpp | 2 -- maths.hpp | 6 +++--- region.cpp | 2 +- test/coord.cpp | 10 +++++----- 7 files changed, 18 insertions(+), 20 deletions(-) diff --git a/bezier1.cpp b/bezier1.cpp index 003351f2..e31df613 100644 --- a/bezier1.cpp +++ b/bezier1.cpp @@ -71,7 +71,7 @@ namespace util { bezier<1>::distance (util::point2f q) const noexcept { const auto ab = m_points[1] - m_points[0]; - const auto t = limit (closest (q), 0.f, 1.f); + const auto t = clamp (closest (q), 0.f, 1.f); const auto p = m_points[0] + t * ab; return util::distance (q, p); diff --git a/coord/ops.hpp b/coord/ops.hpp index 3ee60483..7c7e9cb5 100644 --- a/coord/ops.hpp +++ b/coord/ops.hpp @@ -1001,7 +1001,7 @@ namespace util { > > constexpr auto - limit (K k, K lo, K hi) + clamp (K k, K lo, K hi) { assert (all (lo <= hi)); return max (min (k, hi), lo); @@ -1016,9 +1016,9 @@ namespace util { > > constexpr auto - limit (K k, typename K::value_type lo, K hi) + clamp (K k, typename K::value_type lo, K hi) { - return limit (k, K {lo}, hi); + return clamp (k, K {lo}, hi); } @@ -1030,9 +1030,9 @@ namespace util { > > constexpr auto - limit (K k, K lo, typename K::value_type hi) + clamp (K k, K lo, typename K::value_type hi) { - return limit (k, lo, K {hi}); + return clamp (k, lo, K {hi}); } @@ -1044,9 +1044,9 @@ namespace util { > > constexpr auto - limit (K k, typename K::value_type lo, typename K::value_type hi) + clamp (K k, typename K::value_type lo, typename K::value_type hi) { - return limit (k, K {lo}, K {hi}); + return clamp (k, K {lo}, K {hi}); } diff --git a/geom/aabb.hpp b/geom/aabb.hpp index 56986d23..774d40cc 100644 --- a/geom/aabb.hpp +++ b/geom/aabb.hpp @@ -68,7 +68,7 @@ namespace util::geom { point closest (point query) const { - return limit (query, lo, hi); + return clamp (query, lo, hi); } diff --git a/maths.cpp b/maths.cpp index e4f50a9c..3e4d8baf 100644 --- a/maths.cpp +++ b/maths.cpp @@ -36,7 +36,5 @@ template uint64_t util::log2up (uint64_t); // so it's easier to instantiate early and check for broken code at library // build time. -template float util::limit (float, float, float); - template float util::smoothstep (float, float, float); template double util::smoothstep (double, double, double); diff --git a/maths.hpp b/maths.hpp index 8f30ff36..b4a861ff 100644 --- a/maths.hpp +++ b/maths.hpp @@ -713,7 +713,7 @@ namespace util { std::is_scalar_v && std::is_scalar_v && std::is_scalar_v, std::common_type_t > - limit (const T val, const U lo, const V hi) + clamp (const T val, const U lo, const V hi) { assert (lo <= hi); @@ -731,7 +731,7 @@ namespace util { smoothstep (T a, T b, T x) { assert (a <= b); - x = limit ((x - a) / (b - a), T{0}, T{1}); + x = clamp ((x - a) / (b - a), T{0}, T{1}); return x * x * (3 - 2 * x); } @@ -787,7 +787,7 @@ namespace util { size_t available = sizeof (U) * 8; size_t shift = std::max (available, usable) - usable; - t = limit (t, 0, 1); + t = clamp (t, 0, 1); // construct an integer of the float's mantissa size, multiply it by our // parameter, then shift it back into the full range of the integer type. diff --git a/region.cpp b/region.cpp index 0c96454e..c2ea6334 100644 --- a/region.cpp +++ b/region.cpp @@ -172,7 +172,7 @@ typename region::point_t region::constrain (point_t q) const noexcept { for (size_t i = 0; i < S; ++i) - q[i] = limit (q[i], p[i], p[i] + e[i]); + q[i] = clamp (q[i], p[i], p[i] + e[i]); return q; } diff --git a/test/coord.cpp b/test/coord.cpp index 17421054..abb1068b 100644 --- a/test/coord.cpp +++ b/test/coord.cpp @@ -78,17 +78,17 @@ main (void) tap.expect_eq (select (a > b, a, b), hi, "select with points and max"); }; - // ensure that util::limit resolves to the coord overload. the exact + // ensure that util::clamp resolves to the coord overload. the exact // values are less useful than exercising the compiler/linker. { const util::vector3f val { 0, -1, 2 }; const util::vector3f lo { -1, 1, -2 }; const util::vector3f hi { 1, 2, 0 }; - tap.expect_eq (limit (val, lo, hi), util::vector3f { 0, 1, 0 }, "limit with vec/vec/vec"); - tap.expect_eq (limit (val, 0.f, hi), util::vector3f { 0, 0, 0 }, "limit with vec/num/vec"); - tap.expect_eq (limit (val, lo, 2.f), util::vector3f { 0, 1, 2 }, "limit with vec/vec/num"); - tap.expect_eq (limit (val, 0.f, 2.f), util::vector3f { 0, 0, 2 }, "limit with vec/num/num"); + tap.expect_eq (clamp (val, lo, hi), util::vector3f { 0, 1, 0 }, "clamp with vec/vec/vec"); + tap.expect_eq (clamp (val, 0.f, hi), util::vector3f { 0, 0, 0 }, "clamp with vec/num/vec"); + tap.expect_eq (clamp (val, lo, 2.f), util::vector3f { 0, 1, 2 }, "clamp with vec/vec/num"); + tap.expect_eq (clamp (val, 0.f, 2.f), util::vector3f { 0, 0, 2 }, "clamp with vec/num/num"); } // ensure that klass::indices appears to link correctly