2015-10-14 15:32:53 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2015-10-14 15:32:53 +11:00
|
|
|
*
|
2018-04-19 13:27:43 +10:00
|
|
|
* Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
|
2015-10-14 15:32:53 +11:00
|
|
|
*/
|
|
|
|
|
2018-11-19 15:36:29 +11:00
|
|
|
#pragma once
|
2015-10-14 15:32:53 +11:00
|
|
|
|
2017-08-29 12:21:03 +10:00
|
|
|
#include "../coord/fwd.hpp"
|
2018-04-19 13:27:43 +10:00
|
|
|
#include "../extent.hpp"
|
2018-11-19 15:36:29 +11:00
|
|
|
#include "../random.hpp"
|
|
|
|
|
2018-04-19 13:27:43 +10:00
|
|
|
#include "ops.hpp"
|
2017-08-29 12:21:03 +10:00
|
|
|
|
|
|
|
#include <cstddef>
|
2018-05-01 16:02:55 +10:00
|
|
|
#include <random>
|
2017-01-05 15:06:49 +11:00
|
|
|
|
2018-11-19 15:36:29 +11:00
|
|
|
|
2017-01-05 15:06:49 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::geom {
|
2018-11-19 15:41:55 +11:00
|
|
|
/// A function object that selects a uniformly random point inside a shape
|
|
|
|
/// using a provided random generator. The point will lie within the shape,
|
2017-08-29 12:21:03 +10:00
|
|
|
/// inclusive of boundaries.
|
|
|
|
///
|
2018-11-19 15:41:55 +11:00
|
|
|
/// May be specialised for arbitrary shapes but uses rejection sampling
|
|
|
|
/// as a safe default. This implies that execution may not take a constant
|
2017-08-29 12:21:03 +10:00
|
|
|
/// time.
|
2018-11-19 15:36:29 +11:00
|
|
|
template <typename ShapeT>
|
2015-10-14 15:32:53 +11:00
|
|
|
struct sampler {
|
2018-11-19 15:36:29 +11:00
|
|
|
/// Returns a point which lies within the supplied shape, inclusive
|
|
|
|
/// of borders.
|
|
|
|
template <typename GeneratorT>
|
|
|
|
static auto
|
|
|
|
eval (ShapeT const &shape, GeneratorT &&g)
|
2017-08-29 12:21:03 +10:00
|
|
|
{
|
2018-11-19 15:36:29 +11:00
|
|
|
auto b = bounds (shape);
|
2017-08-29 12:21:03 +10:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
auto p = sample (b, g);
|
2018-11-19 15:36:29 +11:00
|
|
|
if (intersects (shape, p))
|
2017-08-29 12:21:03 +10:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
2015-10-14 15:32:53 +11:00
|
|
|
};
|
|
|
|
|
2017-08-29 12:21:03 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-11-19 15:41:55 +11:00
|
|
|
/// A convenience function that calls sample::fn to select a random point
|
2017-08-29 12:21:03 +10:00
|
|
|
/// in a provided shape.
|
2015-10-14 15:32:53 +11:00
|
|
|
template <
|
2018-11-19 15:36:29 +11:00
|
|
|
typename ShapeT,
|
|
|
|
typename GeneratorT // random generator
|
2015-10-14 15:32:53 +11:00
|
|
|
>
|
2018-11-19 15:36:29 +11:00
|
|
|
auto
|
|
|
|
sample (ShapeT const &shape, GeneratorT &&gen)
|
2015-10-14 15:32:53 +11:00
|
|
|
{
|
2018-11-19 15:36:29 +11:00
|
|
|
return sampler<ShapeT>::eval (shape, std::forward<GeneratorT> (gen));
|
2015-10-14 15:32:53 +11:00
|
|
|
}
|
2018-04-19 13:27:43 +10:00
|
|
|
|
|
|
|
|
2018-11-19 15:41:55 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-08-05 14:42:02 +10:00
|
|
|
std::vector<cruft::point2f>
|
|
|
|
poisson_sample (cruft::extent2i, float distance, int samples);
|
2018-04-26 17:44:16 +10:00
|
|
|
|
|
|
|
|
2018-11-19 15:41:55 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-04-26 17:44:16 +10:00
|
|
|
namespace surface {
|
2018-11-19 15:41:55 +11:00
|
|
|
/// A generator of samples that lie on the surface of a shape
|
2018-04-26 17:44:16 +10:00
|
|
|
template <typename ShapeT>
|
|
|
|
class sampler;
|
|
|
|
|
2018-11-19 15:41:55 +11:00
|
|
|
|
2018-04-26 17:44:16 +10:00
|
|
|
template <typename ShapeT>
|
2018-04-26 18:36:29 +10:00
|
|
|
sampler (ShapeT const&) -> sampler<std::decay_t<ShapeT>>;
|
2018-04-26 17:44:16 +10:00
|
|
|
|
2018-05-01 16:02:55 +10:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
class sampler<cruft::extent<S,T>> {
|
2018-05-01 16:02:55 +10:00
|
|
|
public:
|
2018-08-05 14:42:02 +10:00
|
|
|
sampler (cruft::extent<S,T> _target):
|
2018-05-01 16:02:55 +10:00
|
|
|
target (_target)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
template <typename GeneratorT>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::point<S,T>
|
2018-05-01 16:02:55 +10:00
|
|
|
operator() (GeneratorT &&gen) const noexcept
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::point<S,T> p;
|
2018-05-01 16:02:55 +10:00
|
|
|
|
2018-11-19 15:36:29 +11:00
|
|
|
for (size_t i = 0; i < S; ++i)
|
|
|
|
p[i] = random::uniform (T{0}, target[i], gen);
|
2018-05-01 16:02:55 +10:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::extent<S,T> target;
|
2018-05-01 16:02:55 +10:00
|
|
|
};
|
|
|
|
|
2018-11-19 15:41:55 +11:00
|
|
|
|
|
|
|
/// Approximate a poisson disc sampling through the "Mitchell's Best
|
|
|
|
/// Candidate" algorithm.
|
2018-04-26 17:44:16 +10:00
|
|
|
///
|
2018-11-19 15:41:55 +11:00
|
|
|
/// Try to keep adding a new point to a set. Each new point is the
|
|
|
|
/// best of a set of candidates. The 'best' is the point that is
|
2018-04-26 17:44:16 +10:00
|
|
|
/// furthest from all selected points.
|
|
|
|
///
|
2018-11-19 15:41:55 +11:00
|
|
|
/// \return A vector of the computed points
|
2018-04-27 16:33:18 +10:00
|
|
|
template <typename SamplerT, typename DistanceT, typename GeneratorT>
|
2018-04-26 17:44:16 +10:00
|
|
|
auto
|
|
|
|
poisson (SamplerT const &target,
|
|
|
|
GeneratorT &&gen,
|
2018-04-27 16:33:18 +10:00
|
|
|
DistanceT &&minimum_distance,
|
2018-04-26 17:44:16 +10:00
|
|
|
size_t candidates_count)
|
|
|
|
|
|
|
|
{
|
|
|
|
using point_type = decltype (target (gen));
|
2018-05-01 16:02:23 +10:00
|
|
|
using value_type = typename point_type::value_type;
|
|
|
|
|
2018-04-26 17:44:16 +10:00
|
|
|
std::vector<point_type> selected;
|
|
|
|
std::vector<point_type> candidates;
|
|
|
|
|
|
|
|
// prime the found elements list with an initial point we can
|
|
|
|
// perform distance calculations on
|
|
|
|
selected.push_back (target (gen));
|
|
|
|
|
|
|
|
// keep trying to add one more new point
|
|
|
|
while (1) {
|
|
|
|
// generate a group of candidate points
|
|
|
|
candidates.clear ();
|
|
|
|
std::generate_n (
|
|
|
|
std::back_inserter (candidates),
|
|
|
|
candidates_count,
|
|
|
|
[&] (void) {
|
|
|
|
return target (gen);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// find the point whose minimum distance to the existing
|
|
|
|
// points is the greatest (while also being greater than the
|
|
|
|
// required minimum distance);
|
2018-05-01 16:02:23 +10:00
|
|
|
auto best_distance2 = std::numeric_limits<value_type>::lowest ();
|
2018-05-18 13:51:34 +10:00
|
|
|
size_t best_index = 0;
|
2018-04-26 17:44:16 +10:00
|
|
|
|
|
|
|
for (size_t i = 0; i < candidates.size (); ++i) {
|
|
|
|
auto const p = candidates[i];
|
2018-05-01 16:02:23 +10:00
|
|
|
auto d2 = std::numeric_limits<value_type>::max ();
|
2018-04-26 17:44:16 +10:00
|
|
|
|
|
|
|
// find the minimum distance from this candidate to the
|
|
|
|
// selected points
|
|
|
|
for (auto q: selected)
|
2018-08-05 14:42:02 +10:00
|
|
|
d2 = cruft::min (d2, cruft::distance2 (p, q));
|
2018-04-26 17:44:16 +10:00
|
|
|
|
|
|
|
// record if it's the furthest away
|
2018-08-05 14:42:02 +10:00
|
|
|
if (d2 > best_distance2 && d2 > cruft::pow (minimum_distance (p), 2)) {
|
2018-05-01 16:02:23 +10:00
|
|
|
best_distance2 = d2;
|
2018-04-26 17:44:16 +10:00
|
|
|
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
|
2018-05-01 16:02:23 +10:00
|
|
|
if (best_distance2 <= 0)
|
2018-04-26 17:44:16 +10:00
|
|
|
break;
|
|
|
|
|
|
|
|
selected.push_back (candidates[best_index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return selected;
|
|
|
|
}
|
|
|
|
}
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|