poisson-tool: use function objects for distances

This commit is contained in:
Danny Robson 2018-04-27 16:33:18 +10:00
parent 864c0ecf58
commit 63cf40eeca
2 changed files with 13 additions and 7 deletions

View File

@ -94,11 +94,11 @@ namespace util::geom {
/// furthest from all selected points. /// furthest from all selected points.
/// ///
/// \return a vector of the computed points /// \return a vector of the computed points
template <typename SamplerT, typename GeneratorT> template <typename SamplerT, typename DistanceT, typename GeneratorT>
auto auto
poisson (SamplerT const &target, poisson (SamplerT const &target,
GeneratorT &&gen, GeneratorT &&gen,
float minimum_distance, DistanceT &&minimum_distance,
size_t candidates_count) size_t candidates_count)
{ {
@ -138,7 +138,7 @@ namespace util::geom {
d = util::min (d, distance (p, q)); d = util::min (d, distance (p, q));
// record if it's the furthest away // record if it's the furthest away
if (d > best_distance) { if (d > best_distance && d > minimum_distance (p)) {
best_distance = d; best_distance = d;
best_index = i; best_index = i;
} }
@ -146,7 +146,7 @@ namespace util::geom {
// if we didn't find a suitable point then we give up and // if we didn't find a suitable point then we give up and
// return the points we found, otherwise record the best point // return the points we found, otherwise record the best point
if (best_distance < minimum_distance) if (best_distance <= 0)
break; break;
selected.push_back (candidates[best_index]); selected.push_back (candidates[best_index]);

View File

@ -1,6 +1,7 @@
#include "cmdopt.hpp" #include "cmdopt.hpp"
#include "geom/sample.hpp" #include "functor.hpp"
#include "geom/aabb.hpp" #include "geom/aabb.hpp"
#include "geom/sample.hpp"
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
@ -30,6 +31,12 @@ private:
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
struct height {
float
operator() (util::point2f p) const { return std::pow (p.y / 100, 3.f); }
};
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
@ -45,11 +52,10 @@ main (int argc, char **argv)
opts.scan (argc, argv); opts.scan (argc, argv);
std::cout << "<svg height='" << area.h << "' width='" << area.h << "'>"; std::cout << "<svg height='" << area.h << "' width='" << area.h << "'>";
for (auto p: util::geom::surface::poisson (util::geom::surface::sampler (area), for (auto p: util::geom::surface::poisson (util::geom::surface::sampler (area),
std::default_random_engine (), std::default_random_engine (),
distance, util::functor::constant (distance),
samples)) samples))
{ {
std::cout << "<circle cx='" << p.x << "' cy='" << p.y << "' r='1' />"; std::cout << "<circle cx='" << p.x << "' cy='" << p.y << "' r='1' />";