HighMap library (C++)
Loading...
Searching...
No Matches
inverse_sampler_2d.hpp
Go to the documentation of this file.
1/* Copyright (c) 2025 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4
9#pragma once
10#include <array>
11#include <cstddef>
12#include <cstdint>
13#include <functional>
14#include <random>
15#include <utility>
16#include <vector>
17
18#include <glm/glm.hpp>
19
20#include "highmap/array.hpp"
22
23namespace hmap
24{
25
35{
36public:
37 // ==========================================================================
38 // Constructors
39 // ==========================================================================
40
41 InverseSampler2D() = default;
42
51 InverseSampler2D(const Array &density,
52 std::uint32_t seed = 0,
53 const BBox2D &bbox = BBox2D{});
54
55 // ==========================================================================
56 // Sampling
57 // ==========================================================================
58
64 glm::vec2 sample();
65
73 glm::vec2 sample(float u, float u2);
74
81 std::array<std::vector<float>, 2> sample(size_t count);
82
92 glm::vec2 sample(const BBox2D &restricted_bbox);
93
106 glm::vec2 sample(const BBox2D &restricted_bbox, float u, float u2);
107
108 // ==========================================================================
109 // Updates & Modifiers
110 // ==========================================================================
111
120 void update_region(const Array &updated_array,
121 const glm::ivec2 &range_i,
122 const glm::ivec2 &range_j);
123
133 void update_region(
134 const std::function<float(int i, int j, float current)> &updater,
135 const glm::ivec2 &range_i,
136 const glm::ivec2 &range_j);
137
145 void reset(const Array &density,
146 std::uint32_t seed = 0,
147 const BBox2D &bbox = BBox2D{});
148
149 // ==========================================================================
150 // Getters & Setters
151 // ==========================================================================
152
156 const BBox2D &get_bbox() const;
157
161 const Array &get_density() const;
162
166 float get_total_weight() const;
167
171 void set_bbox(const BBox2D &bbox);
172
176 void set_seed(std::uint32_t seed);
177
178private:
179 Array density_;
180 BBox2D bbox_ = BBox2D{};
181 std::vector<float> row_sums_;
182 std::vector<float> r_cdf_;
183 std::mt19937 rng_{0};
184 std::uniform_real_distribution<float> dist_{0.f, 1.f};
185
186 void rebuild_cdf();
187 void update_row_cdf_from(int jmin);
188};
189
190} // namespace hmap
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
Axis-aligned 2D bounding box in world coordinates.
Array class, helper to manipulate 2D float array with "(i, j)" indexing.
Definition array.hpp:31
void set_seed(std::uint32_t seed)
Sets the RNG seed.
Definition inverse_sampler_2d.cpp:341
void set_bbox(const BBox2D &bbox)
Sets the bounding box.
Definition inverse_sampler_2d.cpp:336
const BBox2D & get_bbox() const
Gets the bounding box.
Definition inverse_sampler_2d.cpp:30
glm::vec2 sample()
Samples a single point sequentially using the internal RNG.
Definition inverse_sampler_2d.cpp:83
const Array & get_density() const
Gets the current density array.
Definition inverse_sampler_2d.cpp:35
float get_total_weight() const
Gets the total integrated weight.
Definition inverse_sampler_2d.cpp:40
void reset(const Array &density, std::uint32_t seed=0, const BBox2D &bbox=BBox2D{})
Reinitializes the sampler with a new density array.
Definition inverse_sampler_2d.cpp:73
void update_region(const Array &updated_array, const glm::ivec2 &range_i, const glm::ivec2 &range_j)
Updates the density in the sub-region [imin, imax] x [jmin, jmax] from another array.
Definition inverse_sampler_2d.cpp:346
Definition opencv.hpp:20
Axis-aligned 2D bounding box in world coordinates.
Definition bbox2d.hpp:25