Skip to content

Commit

Permalink
added avg based benchmarking to the main.cpp example; some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dsecrieru committed Mar 27, 2023
1 parent ea5f7be commit 4ac71dd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
32 changes: 21 additions & 11 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <chrono>
#include <format>
#include <iostream>
#include <numeric>

#include "pds_Bridson.hpp"

Expand All @@ -12,20 +13,31 @@ struct point2d {
point2d(float x_, float y_) : x(x_), y(y_) {}
};

const int runs = 10;
std::random_device rd;

void bench(const blue_noise::bridson_2d::config& conf) {
std::random_device rd;
std::mt19937 rne(rd());
std::vector<std::size_t> sample_counts;
std::vector<std::chrono::milliseconds> durations;

for (int i = 0; i < runs; ++i) {
std::mt19937 rng(rd());

const auto start = std::chrono::steady_clock::now();
auto points = blue_noise::bridson_2d::poisson_disc_sampling<point2d>(conf, rng);
const auto end = std::chrono::steady_clock::now();

const auto start = std::chrono::steady_clock::now();
auto points = blue_noise::bridson_2d::poisson_disc_sampling<point2d>(conf, rne);
const auto end = std::chrono::steady_clock::now();
sample_counts.push_back(points.size());
durations.push_back(std::chrono::duration_cast<std::chrono::milliseconds>(end - start));
}

std::cout << std::format("generated {:6} points in {:3}ms\n",
points.size(),
std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count());
const auto avg_count = std::reduce(sample_counts.begin(), sample_counts.end()) / float(runs);
const auto avg_duration = std::reduce(durations.begin(), durations.end()) / float(runs);

std::cout << std::format("generated avg of {:6.0f} points in ~{:3}\n", avg_count, avg_duration);
}

int main(int argc, char* argv[]) {
int main() {
blue_noise::bridson_2d::config conf1k = {
.w = 1600,
.h = 900,
Expand All @@ -49,6 +61,4 @@ int main(int argc, char* argv[]) {
.k_max_attempts = 30
};
bench(conf100k);

return 0;
}
16 changes: 8 additions & 8 deletions pds_Bridson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ struct config {
* https://www.cs.ubc.ca/~rbridson/docs/bridson-siggraph07-poissondisk.pdf
*
*/
template<typename point_t, typename random_engine_t>
requires std::uniform_random_bit_generator<random_engine_t>
std::vector<point_t> poisson_disc_sampling(const config& conf, random_engine_t& rne) {
template<typename point_t, typename rng_t>
requires std::uniform_random_bit_generator<rng_t>
std::vector<point_t> poisson_disc_sampling(const config& conf, rng_t& rng) {
const auto cell_size = conf.min_dist / std::numbers::sqrt2_v<float_t>;
const auto grid_w = static_cast<integral_t>(std::ceil(conf.w / cell_size));
const auto grid_h = static_cast<integral_t>(std::ceil(conf.h / cell_size));
Expand All @@ -44,7 +44,7 @@ std::vector<point_t> poisson_disc_sampling(const config& conf, random_engine_t&
auto grid_y = [=](const point_t& p) {
return std::min(static_cast<integral_t>(p.y / cell_size), grid_h - 1);
};
auto set_cell = [cell_size, grid_w, &grid, &grid_x, &grid_y](const point_t& p) {
auto set_cell = [grid_w, &grid, &grid_x, &grid_y](const point_t& p) {
grid[grid_y(p) * grid_w + grid_x(p)] = p;
};
const auto& get_cell = [grid_w, &grid](integral_t x, integral_t y) {
Expand Down Expand Up @@ -82,18 +82,18 @@ std::vector<point_t> poisson_disc_sampling(const config& conf, random_engine_t&
return true;
};

std::uniform_real_distribution<float> rng;
std::uniform_real_distribution<float> distrib;

auto new_candidate_around = [&](const point_t& p) {
auto radius = conf.min_dist * (rng(rne) + 1.0f);
auto angle_radians = 2.0f * std::numbers::pi_v<float_t> * rng(rne);
auto radius = conf.min_dist * (distrib(rng) + 1.0f);
auto angle_radians = 2.0f * std::numbers::pi_v<float_t> * distrib(rng);

return point_t(p.x + radius * std::cos(angle_radians), p.y + radius * sin(angle_radians));
};

std::vector<point_t> active;

point_t first(rng(rne) * (conf.w - 1.0f), rng(rne) * (conf.h - 1.0f));
point_t first(distrib(rng) * (conf.w - 1.0f), distrib(rng) * (conf.h - 1.0f));

set_cell(first);
active.push_back(first);
Expand Down

0 comments on commit 4ac71dd

Please sign in to comment.