geom/sample: add trivial poisson sample generator
This commit is contained in:
parent
f31a344912
commit
89defb3bc2
@ -240,6 +240,7 @@ list (
|
|||||||
geom/ray.hpp
|
geom/ray.hpp
|
||||||
geom/rect.cpp
|
geom/rect.cpp
|
||||||
geom/rect.hpp
|
geom/rect.hpp
|
||||||
|
geom/sample.cpp
|
||||||
geom/sample.hpp
|
geom/sample.hpp
|
||||||
geom/sphere.cpp
|
geom/sphere.cpp
|
||||||
geom/sphere.hpp
|
geom/sphere.hpp
|
||||||
|
71
geom/sample.cpp
Normal file
71
geom/sample.cpp
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "sample.hpp"
|
||||||
|
|
||||||
|
#include "../point.hpp"
|
||||||
|
|
||||||
|
#include <random>
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
std::vector<util::point2f>
|
||||||
|
util::geom::poisson_sample (util::extent2i area, float distance, int samples)
|
||||||
|
{
|
||||||
|
std::vector<util::point2f> selected;
|
||||||
|
|
||||||
|
std::vector<util::point2f> candidates (samples);
|
||||||
|
std::uniform_real_distribution<float> dist_w (0, area.w);
|
||||||
|
std::uniform_real_distribution<float> dist_h (0, area.h);
|
||||||
|
std::random_device rd;
|
||||||
|
std::default_random_engine gen;
|
||||||
|
|
||||||
|
selected.push_back ({
|
||||||
|
dist_w (gen),
|
||||||
|
dist_h (gen),
|
||||||
|
});
|
||||||
|
|
||||||
|
auto min_distance = [&selected] (auto q) {
|
||||||
|
float min_d = INFINITY;
|
||||||
|
|
||||||
|
for (auto s: selected)
|
||||||
|
if (auto s_d = util::distance2 (s, q); s_d < min_d)
|
||||||
|
min_d = s_d;
|
||||||
|
|
||||||
|
return min_d;
|
||||||
|
};
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
util::point2f p;
|
||||||
|
float d = -INFINITY;
|
||||||
|
|
||||||
|
for (int i = 0; i < samples; ++i) {
|
||||||
|
util::point2f candidate { dist_w (gen), dist_h (gen) };
|
||||||
|
|
||||||
|
if (auto p_d = min_distance (candidate); p_d > d) {
|
||||||
|
p = candidate;
|
||||||
|
d = p_d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (std::sqrt (d) < distance)
|
||||||
|
break;
|
||||||
|
|
||||||
|
selected.push_back (p);
|
||||||
|
}
|
||||||
|
|
||||||
|
return selected;
|
||||||
|
}
|
@ -11,14 +11,15 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
* Copyright 2015-2017 Danny Robson <danny@nerdcruft.net>
|
* Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __UTIL_GEOM_SAMPLE_HPP
|
#ifndef __UTIL_GEOM_SAMPLE_HPP
|
||||||
#define __UTIL_GEOM_SAMPLE_HPP
|
#define __UTIL_GEOM_SAMPLE_HPP
|
||||||
|
|
||||||
#include "../coord/fwd.hpp"
|
#include "../coord/fwd.hpp"
|
||||||
#include "./ops.hpp"
|
#include "../extent.hpp"
|
||||||
|
#include "ops.hpp"
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
@ -72,6 +73,10 @@ namespace util::geom {
|
|||||||
{
|
{
|
||||||
return sampler<S,T,K,G>::fn (k, g);
|
return sampler<S,T,K,G>::fn (k, g);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::vector<util::point2f>
|
||||||
|
poisson_sample (util::extent2i, float distance, int samples);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user