From 8b47a2e3502b35563b1a62747edd9d66cc8ae312 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 1 May 2018 16:02:23 +1000 Subject: [PATCH] geom/sample: don't rely on float distances in poisson sampling --- geom/sample.hpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/geom/sample.hpp b/geom/sample.hpp index f88fb1b5..c0c9e61e 100644 --- a/geom/sample.hpp +++ b/geom/sample.hpp @@ -103,6 +103,8 @@ namespace util::geom { { using point_type = decltype (target (gen)); + using value_type = typename point_type::value_type; + std::vector selected; std::vector candidates; @@ -125,28 +127,28 @@ namespace util::geom { // find the point whose minimum distance to the existing // points is the greatest (while also being greater than the // required minimum distance); - float best_distance = -INFINITY; + auto best_distance2 = std::numeric_limits::lowest (); size_t best_index; for (size_t i = 0; i < candidates.size (); ++i) { auto const p = candidates[i]; - float d = INFINITY; + auto d2 = std::numeric_limits::max (); // find the minimum distance from this candidate to the // selected points for (auto q: selected) - d = util::min (d, distance (p, q)); + d2 = util::min (d2, util::distance2 (p, q)); // record if it's the furthest away - if (d > best_distance && d > minimum_distance (p)) { - best_distance = d; + if (d2 > best_distance2 && d2 > util::pow (minimum_distance (p), 2)) { + best_distance2 = d2; best_index = i; } } // if we didn't find a suitable point then we give up and // return the points we found, otherwise record the best point - if (best_distance <= 0) + if (best_distance2 <= 0) break; selected.push_back (candidates[best_index]);