-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsimple.cpp
77 lines (66 loc) · 2.67 KB
/
simple.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <array>// std::array
#include <cassert>// assert
#include <cstdint>// UINT64_C, etc
#include <cstdio>// std::printf
#include <functional>// std::function
#include <memory>// std::unique_ptr
#include <type_traits>// std::is_same_v
#define TPH_POISSON_IMPLEMENTATION
#include "thinks/tph_poisson.h"
static_assert(std::is_same_v<tph_poisson_real, float>);
int main(int /*argc*/, char * /*argv*/[])
{
// clang-format off
constexpr std::array<tph_poisson_real, 2> bounds_min{
static_cast<tph_poisson_real>(-10), static_cast<tph_poisson_real>(-10) };
constexpr std::array<tph_poisson_real, 2> bounds_max{
static_cast<tph_poisson_real>(10), static_cast<tph_poisson_real>(10) };
// clang-format on
// Configure arguments.
tph_poisson_args args = {};
args.radius = static_cast<tph_poisson_real>(3);
args.ndims = INT32_C(2);
args.bounds_min = bounds_min.data();
args.bounds_max = bounds_max.data();
args.max_sample_attempts = UINT32_C(30);
args.seed = UINT64_C(1981);
// Using default allocator (libc malloc).
const tph_poisson_allocator *alloc = NULL;
// Initialize empty sampling.
using unique_poisson_ptr =
std::unique_ptr<tph_poisson_sampling, std::function<void(tph_poisson_sampling *)>>;
auto sampling = unique_poisson_ptr{ new tph_poisson_sampling{}, [](tph_poisson_sampling *s) {
tph_poisson_destroy(s);
delete s;
} };
// Populate sampling with points.
if (const int ret = tph_poisson_create(&args, alloc, sampling.get());
ret != TPH_POISSON_SUCCESS) {
std::printf("Failed creating Poisson sampling! Error code: %d\n", ret);
return EXIT_FAILURE;
};
// Retrieve sampling points.
const tph_poisson_real *samples = tph_poisson_get_samples(sampling.get());
if (samples == nullptr) {
/* Shouldn't happen since we check the return value from tph_poisson_create! */
std::printf("Bad samples!\n");
return EXIT_FAILURE;
}
// Print first and last sample positions.
// clang-format off
assert(sampling->nsamples >= 2);
std::printf("\n%s:\n"
"samples[%td] = ( %.3f, %.3f )\n"
"...\n"
"samples[%td] = ( %.3f, %.3f )\n\n",
"simple (Cpp)",
static_cast<ptrdiff_t>(0),
static_cast<double>(samples[0]),
static_cast<double>(samples[1]),
static_cast<ptrdiff_t>(sampling->nsamples - 1),
static_cast<double>(samples[(sampling->nsamples - 1) * sampling->ndims]),
static_cast<double>(samples[(sampling->nsamples - 1) * sampling->ndims + 1]));
// clang-format on
// tph_poisson_destroy is called by unique_poisson_ptr destructor.
return EXIT_SUCCESS;
}