src/sources/upsampling.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/sources/upsampling.h" | ||
| 4 | |||
| 5 | #include "raster/kernels.h" | ||
| 6 | #include "sources/lattice.h" | ||
| 7 | #include "terraingen/util/bits.h" | ||
| 8 | #include "terraingen/util/parallel.h" | ||
| 9 | |||
| 10 | #include <algorithm> | ||
| 11 | #include <array> | ||
| 12 | #include <cmath> | ||
| 13 | #include <limits> | ||
| 14 | #include <map> | ||
| 15 | #include <mutex> | ||
| 16 | #include <stdexcept> | ||
| 17 | #include <utility> | ||
| 18 | |||
| 19 | namespace terraingen { | ||
| 20 | |||
| 21 | namespace { | ||
| 22 | |||
| 23 | /// How far below the post spacing, as a share of it, a spacing still counts as the post spacing. | ||
| 24 | constexpr double kSpacingTolerance = 1e-9; | ||
| 25 | |||
| 26 | /// Rows of posts read at once when a grid on the lattice copies them. | ||
| 27 | constexpr std::size_t kStripRows = 64; | ||
| 28 | |||
| 29 | /// The post at world (`x`, `y`) of a lattice placed by `transform`, if (`x`, `y`) is one. | ||
| 30 | 112604 | std::optional<std::pair<std::int64_t, std::int64_t>> CenteredPost(const GeoTransform& transform, | |
| 31 | double x, double y) { | ||
| 32 | 112604 | const Vec2d pixel = transform.WorldToPixel(x, y); | |
| 33 | 112605 | const std::optional<std::int64_t> col = detail::NearestWhole(pixel.x - 0.5); | |
| 34 | 112605 | const std::optional<std::int64_t> row = detail::NearestWhole(pixel.y - 0.5); | |
| 35 | 112605 | if (!col || !row) { | |
| 36 | 109082 | return std::nullopt; | |
| 37 | } | ||
| 38 | 3523 | return std::pair{*col, *row}; | |
| 39 | } | ||
| 40 | |||
| 41 | /// The first and last posts along an axis the kernel reads around lattice position `u`. | ||
| 42 | 1222513 | std::pair<std::int64_t, std::int64_t> Taps(ResampleMethod method, double u) { | |
| 43 | 1222513 | if (method == ResampleMethod::Nearest) { | |
| 44 | 20850 | const auto nearest = static_cast<std::int64_t>(std::round(u)); | |
| 45 | 20850 | return {nearest, nearest}; | |
| 46 | } | ||
| 47 | 1201663 | const auto below = static_cast<std::int64_t>(std::floor(u)); | |
| 48 | 1201663 | const int radius = KernelRadius(method); | |
| 49 | 1201351 | return {below + 1 - radius, below + radius}; | |
| 50 | } | ||
| 51 | |||
| 52 | /// The posts the kernel reads around the lattice positions `lattice`. | ||
| 53 | 197634 | detail::IndexBox TapBox(ResampleMethod method, Span<const Vec2d> lattice) { | |
| 54 | 197634 | detail::IndexBox box; | |
| 55 | 809920 | for (const Vec2d& at : lattice) { | |
| 56 | 612283 | const auto [first, last] = Taps(method, at.x); | |
| 57 | 611854 | const auto [top, bottom] = Taps(method, at.y); | |
| 58 | 611984 | box.Include(first, last, top, bottom); | |
| 59 | } | ||
| 60 | 197637 | return box; | |
| 61 | } | ||
| 62 | |||
| 63 | /// `posts` interpolated with `method` at lattice position (`u`, `v`), read from `block`. | ||
| 64 | 23831 | float Interpolate(const detail::LatticeWindow<float>& block, ResampleMethod method, double u, | |
| 65 | double v) { | ||
| 66 | 23831 | const std::optional<float> value = InterpolateLattice( | |
| 67 | u, v, method, | ||
| 68 | 23831 | [&](double col, double row) { | |
| 69 | 226776 | return block.At(static_cast<std::int64_t>(col), static_cast<std::int64_t>(row)); | |
| 70 | }, | ||
| 71 | 226776 | [](float post) { return std::isnan(post); }); | |
| 72 | 23831 | return value.value_or(std::numeric_limits<float>::quiet_NaN()); | |
| 73 | } | ||
| 74 | |||
| 75 | /// The cell `KernelWeights` start from along an axis and the weights there, at lattice position | ||
| 76 | /// `u`. | ||
| 77 | 202356 | struct AxisWeights { | |
| 78 | double at = 0.0; | ||
| 79 | double first = 0.0; | ||
| 80 | std::array<double, 6> weights{}; | ||
| 81 | }; | ||
| 82 | |||
| 83 | 416391 | AxisWeights WeightsAt(ResampleMethod method, double u) { | |
| 84 | 416391 | const double first = std::floor(u); | |
| 85 | 416391 | return {u, first, KernelWeights(method, u - first)}; | |
| 86 | } | ||
| 87 | |||
| 88 | /// Sets `heights` to `block` interpolated at the lattice positions `lattice`, to the bits of | ||
| 89 | /// `Interpolate`. The weights along x of the first run of positions sharing a row, and along y of | ||
| 90 | /// each row, serve every position below them that matches them, as on a grid aligned with the | ||
| 91 | /// posts. | ||
| 92 | 197630 | void InterpolateBlock(const detail::LatticeWindow<float>& block, ResampleMethod method, | |
| 93 | Span<const Vec2d> lattice, Span<float> heights) { | ||
| 94 | 197630 | if (method == ResampleMethod::Nearest) { | |
| 95 | 13410 | for (std::size_t i = 0; i < lattice.size(); ++i) { | |
| 96 | 10422 | heights[i] = Interpolate(block, method, lattice[i].x, lattice[i].y); | |
| 97 | } | ||
| 98 | 2988 | return; | |
| 99 | } | ||
| 100 | std::size_t width = 1; | ||
| 101 | 202355 | while (width < lattice.size() && lattice[width].y == lattice[0].y) { | |
| 102 | 7713 | ++width; | |
| 103 | } | ||
| 104 | 194644 | std::vector<AxisWeights> columns(width); | |
| 105 | 396997 | for (std::size_t c = 0; c < width; ++c) { | |
| 106 | 202348 | columns[c] = WeightsAt(method, lattice[c].x); | |
| 107 | } | ||
| 108 | 194649 | const int radius = KernelRadius(method); | |
| 109 | 8513485 | const auto value = [&](double col, double row) { | |
| 110 | 8318836 | return block.At(static_cast<std::int64_t>(col), static_cast<std::int64_t>(row)); | |
| 111 | 194649 | }; | |
| 112 | 8325808 | const auto missing = [](float post) { return std::isnan(post); }; | |
| 113 | 194649 | AxisWeights row = WeightsAt(method, lattice[0].y); | |
| 114 | 790277 | for (std::size_t i = 0; i < lattice.size(); ++i) { | |
| 115 | 595636 | const Vec2d& at = lattice[i]; | |
| 116 | 595477 | const AxisWeights& column = columns[i % width]; | |
| 117 | 595477 | if (at.y != row.at) { | |
| 118 | 19392 | row = WeightsAt(method, at.y); | |
| 119 | } | ||
| 120 | 595486 | if (at.x != column.at) { | |
| 121 | 13409 | heights[i] = Interpolate(block, method, at.x, at.y); | |
| 122 | 13409 | continue; | |
| 123 | } | ||
| 124 | 582226 | heights[i] = InterpolateLatticeWeights(column.first, row.first, column.weights, row.weights, | |
| 125 | radius, value, missing) | ||
| 126 | 1158749 | .value_or(std::numeric_limits<float>::quiet_NaN()); | |
| 127 | } | ||
| 128 | 194641 | } | |
| 129 | |||
| 130 | /// The lattice transform of `posts`, which must not be rotated. | ||
| 131 | /// @throws std::invalid_argument for a rotated lattice. | ||
| 132 | 197564 | GeoTransform UnrotatedTransform(const PostLattice& posts) { | |
| 133 | 197564 | GeoTransform transform = posts.Transform(); | |
| 134 | 197566 | if (transform.IsRotated()) { | |
| 135 | 3 | throw std::invalid_argument("Upsampling cannot interpolate a rotated lattice of posts"); | |
| 136 | } | ||
| 137 | 197563 | return transform; | |
| 138 | } | ||
| 139 | |||
| 140 | /// Sets the cells of `heights`, a grid on the lattice of `posts` starting at `offset`, to the | ||
| 141 | /// posts under them, a strip of rows at a time, strips split over threads. | ||
| 142 | 138 | void CopyPosts(const PostLattice& posts, detail::LatticeOffset offset, Raster<float>& heights, | |
| 143 | unsigned threads) { | ||
| 144 | 138 | const std::size_t width = heights.Width(); | |
| 145 | 138 | const std::size_t height = heights.Height(); | |
| 146 | 138 | ParallelFor(CeilDiv(height, kStripRows), threads, [&](std::size_t strip) { | |
| 147 | 158 | const std::size_t first = strip * kStripRows; | |
| 148 | 158 | const std::size_t rows = std::min(kStripRows, height - first); | |
| 149 | 158 | posts.Read(offset.col, offset.row + static_cast<std::int64_t>(first), width, rows, | |
| 150 | 158 | heights.Values().subspan(first * width, rows * width)); | |
| 151 | 158 | }); | |
| 152 | 138 | } | |
| 153 | |||
| 154 | /// Sets the cells of `samples`, a grid, at the posts' spacing or coarser: copies the posts on | ||
| 155 | /// their lattice, otherwise `upsample(samples)` then gives the cells centered on posts the posts' | ||
| 156 | /// heights. | ||
| 157 | template <typename Upsample> | ||
| 158 | 212 | void FillGridAtPostSpacing(const PostLattice& posts, SampleBlock& samples, unsigned threads, | |
| 159 | const Upsample& upsample) { | ||
| 160 | 212 | Raster<float>& heights = *samples.Grid(); | |
| 161 | 212 | const GeoTransform transform = posts.Transform(); | |
| 162 | 212 | if (const auto offset = detail::OffsetOnLattice(transform, heights.Transform())) { | |
| 163 | 138 | CopyPosts(posts, *offset, heights, threads); | |
| 164 | 138 | return; | |
| 165 | } | ||
| 166 | 74 | upsample(samples); | |
| 167 | 148 | ParallelFor(heights.Height(), threads, [&](std::size_t row) { | |
| 168 | 9217 | for (std::size_t col = 0; col < heights.Width(); ++col) { | |
| 169 | 8667 | const Vec2d center = heights.CellCenter(col, row); | |
| 170 | 8667 | if (const auto post = CenteredPost(transform, center.x, center.y)) { | |
| 171 | 25 | posts.Read(post->first, post->second, 1, 1, Span<float>(&heights(col, row), 1)); | |
| 172 | } | ||
| 173 | } | ||
| 174 | }); | ||
| 175 | } | ||
| 176 | |||
| 177 | /// Sets the points of `samples` at the posts' spacing or coarser: those centered on posts to the | ||
| 178 | /// posts' heights, the others by `upsample` of a block of them at once. | ||
| 179 | template <typename Upsample> | ||
| 180 | 100658 | void FillPointsAtPostSpacing(const PostLattice& posts, SampleBlock& samples, | |
| 181 | const Upsample& upsample) { | ||
| 182 | 100658 | const GeoTransform transform = posts.Transform(); | |
| 183 | 100658 | const Span<const Vec2d> points = samples.Points(); | |
| 184 | 100658 | const Span<float> heights = samples.Heights(); | |
| 185 | 100658 | detail::EvaluateChosen( | |
| 186 | points.size(), | ||
| 187 | 305255 | [&](std::size_t i) -> std::optional<Vec2d> { | |
| 188 | 103939 | if (const auto post = CenteredPost(transform, points[i].x, points[i].y)) { | |
| 189 | 3498 | posts.Read(post->first, post->second, 1, 1, heights.subspan(i, 1)); | |
| 190 | 3498 | return std::nullopt; | |
| 191 | } | ||
| 192 | 100441 | return points[i]; | |
| 193 | }, | ||
| 194 | 396274 | [&](Span<const Vec2d> between, Span<float> values) { | |
| 195 | 97479 | SampleBlock rest(between, values); | |
| 196 | 97479 | upsample(rest); | |
| 197 | }, | ||
| 198 | 201099 | [&](std::size_t i, float value) { heights[i] = value; }); | |
| 199 | 100658 | } | |
| 200 | |||
| 201 | /// The kernel upsamplers' names and kernels. | ||
| 202 | const std::array<std::pair<const char*, ResampleMethod>, 6> kKernelNames{{ | ||
| 203 | {"nearest", ResampleMethod::Nearest}, | ||
| 204 | {"bilinear", ResampleMethod::Bilinear}, | ||
| 205 | {"cubic", ResampleMethod::Cubic}, | ||
| 206 | {"catmull_rom", ResampleMethod::Cubic}, | ||
| 207 | {"lanczos3", ResampleMethod::Lanczos3}, | ||
| 208 | {"cubic_bspline", ResampleMethod::CubicBSpline}, | ||
| 209 | }}; | ||
| 210 | |||
| 211 | /// The upsampler types registered, by name. | ||
| 212 | 7 | struct Registry { | |
| 213 | std::mutex mutex; | ||
| 214 | std::map<std::string, UpsamplerFactory> factories; | ||
| 215 | }; | ||
| 216 | |||
| 217 | 38 | Registry& UpsamplerRegistry() { | |
| 218 | 38 | static Registry registry; | |
| 219 | 38 | return registry; | |
| 220 | } | ||
| 221 | |||
| 222 | } // namespace | ||
| 223 | |||
| 224 | namespace detail { | ||
| 225 | |||
| 226 | 353227 | LatticeWindow<float> ReadPosts(const PostLattice& posts, const IndexBox& box) { | |
| 227 | 353227 | LatticeWindow<float> window(box); | |
| 228 | 353224 | posts.Read(box.col0, box.row0, box.Columns(), box.Rows(), Span<float>(window.values)); | |
| 229 | 353227 | return window; | |
| 230 | } | ||
| 231 | |||
| 232 | 217 | std::optional<LatticeOffset> OffsetOnLattice(const GeoTransform& lattice, | |
| 233 | const GeoTransform& grid) { | ||
| 234 | 368 | const auto same = [](double a, double b) { return std::fabs(a - b) <= 1e-12 * std::fabs(b); }; | |
| 235 | 368 | if (!same(grid.pixelWidth, lattice.pixelWidth) || | |
| 236 | 217 | !same(grid.pixelHeight, lattice.pixelHeight) || grid.rowRotation != lattice.rowRotation || | |
| 237 | 150 | grid.colRotation != lattice.colRotation) { | |
| 238 | 67 | return std::nullopt; | |
| 239 | } | ||
| 240 | 150 | const Vec2d corner = lattice.WorldToPixel(grid.originX, grid.originY); | |
| 241 | 150 | const std::optional<std::int64_t> col = detail::NearestWhole(corner.x); | |
| 242 | 150 | const std::optional<std::int64_t> row = detail::NearestWhole(corner.y); | |
| 243 | 150 | if (!col || !row) { | |
| 244 | 11 | return std::nullopt; | |
| 245 | } | ||
| 246 | 139 | return LatticeOffset{*col, *row}; | |
| 247 | } | ||
| 248 | |||
| 249 | } // namespace detail | ||
| 250 | |||
| 251 | 142 | RasterPosts::RasterPosts(std::shared_ptr<const Raster<float>> window, | |
| 252 | 142 | const RasterPlacement& placement, bool wrapColumns) | |
| 253 | 142 | : m_window(std::move(window)), m_placement(placement), m_wrapColumns(wrapColumns) { | |
| 254 | 142 | if (!m_window || m_window->Empty()) { | |
| 255 | 1 | throw std::invalid_argument("Raster posts need a raster with cells"); | |
| 256 | } | ||
| 257 | 141 | if (m_wrapColumns && (m_placement.col != 0 || m_window->Width() != m_placement.whole.width)) { | |
| 258 | 1 | throw std::invalid_argument("Wrapping raster posts must span every column"); | |
| 259 | } | ||
| 260 | 142 | } | |
| 261 | |||
| 262 | 17 | std::optional<PixelWindow> RasterPosts::Extent() const { | |
| 263 | 17 | return PixelWindow{m_placement.col, m_placement.col + m_window->Width(), m_placement.row, | |
| 264 | 17 | m_placement.row + m_window->Height()}; | |
| 265 | } | ||
| 266 | |||
| 267 | 125757 | void RasterPosts::Read(std::int64_t col0, std::int64_t row0, std::size_t width, std::size_t height, | |
| 268 | Span<float> heights) const { | ||
| 269 | 125757 | const Raster<float>& window = *m_window; | |
| 270 | 125757 | const std::vector<std::size_t> cols = | |
| 271 | 125757 | detail::PostIndices(col0, width, m_wrapColumns, m_placement.whole.width, m_placement.col, | |
| 272 | 125757 | m_placement.col + window.Width()); | |
| 273 | 125765 | const std::vector<std::size_t> rows = | |
| 274 | 125765 | detail::PostIndices(row0, height, false, m_placement.whole.height, m_placement.row, | |
| 275 | 125765 | m_placement.row + window.Height()); | |
| 276 | 489602 | for (std::size_t r = 0; r < height; ++r) { | |
| 277 | 4638906 | for (std::size_t c = 0; c < width; ++c) { | |
| 278 | 4275069 | const float value = window(cols[c] - m_placement.col, rows[r] - m_placement.row); | |
| 279 | 4275049 | heights[r * width + c] = | |
| 280 | 4275049 | window.IsMissing(value) ? std::numeric_limits<float>::quiet_NaN() : value; | |
| 281 | } | ||
| 282 | } | ||
| 283 | 125766 | } | |
| 284 | |||
| 285 | 68021 | double PostSpacing(const GeoTransform& transform) { | |
| 286 | 68021 | return std::max(transform.CellWidth(), transform.CellHeight()); | |
| 287 | } | ||
| 288 | |||
| 289 | 275694 | bool AtPostSpacing(const GeoTransform& transform, double spacing) { | |
| 290 | 275694 | const double cellSize = std::sqrt(std::fabs(transform.pixelWidth * transform.pixelHeight - | |
| 291 | 275694 | transform.rowRotation * transform.colRotation)); | |
| 292 | 275694 | return spacing >= cellSize * (1.0 - kSpacingTolerance); | |
| 293 | } | ||
| 294 | |||
| 295 | 275686 | void Upsampler::Fill(const PostLattice& posts, SampleBlock& samples, double spacing, | |
| 296 | unsigned threads) const { | ||
| 297 | 272371 | const auto upsample = [&](SampleBlock& block) { FillSamples(posts, block, spacing, threads); }; | |
| 298 | 275686 | if (samples.Size() == 0) { | |
| 299 | ✗ | return; | |
| 300 | } | ||
| 301 | 275686 | if (!AtPostSpacing(posts.Transform(), spacing)) { | |
| 302 | 174816 | upsample(samples); | |
| 303 | 100870 | } else if (samples.IsGrid()) { | |
| 304 | 212 | FillGridAtPostSpacing(posts, samples, threads, upsample); | |
| 305 | } else { | ||
| 306 | 100658 | FillPointsAtPostSpacing(posts, samples, upsample); | |
| 307 | } | ||
| 308 | } | ||
| 309 | |||
| 310 | 482 | void Upsampler::Fill(const PostLattice& posts, Raster<float>& heights, double spacing, | |
| 311 | unsigned threads) const { | ||
| 312 | 482 | SampleBlock samples(heights); | |
| 313 | 482 | Fill(posts, samples, spacing, threads); | |
| 314 | 481 | } | |
| 315 | |||
| 316 | 11977 | float Upsampler::HeightAt(const PostLattice& posts, double x, double y, double spacing) const { | |
| 317 | 11977 | return detail::PointHeight(x, y, | |
| 318 | 23954 | [&](SampleBlock& samples) { Fill(posts, samples, spacing, 1); }); | |
| 319 | } | ||
| 320 | |||
| 321 | 255080 | void Upsampler::HeightsAt(const PostLattice& posts, Span<const Vec2d> points, Span<float> heights, | |
| 322 | double spacing) const { | ||
| 323 | 255080 | SampleBlock samples(points, heights); | |
| 324 | 255080 | Fill(posts, samples, spacing, 1); | |
| 325 | 255076 | } | |
| 326 | |||
| 327 | 197564 | void KernelUpsampler::FillSamples(const PostLattice& posts, SampleBlock& samples, | |
| 328 | double /*spacing*/, unsigned threads) const { | ||
| 329 | 197564 | const GeoTransform transform = UnrotatedTransform(posts); | |
| 330 | 197563 | detail::FillByTiles(samples, threads, [&](Span<const Vec2d> points, Span<float> heights) { | |
| 331 | 197593 | std::vector<Vec2d> lattice(points.size()); | |
| 332 | 803474 | for (std::size_t i = 0; i < points.size(); ++i) { | |
| 333 | 605879 | const Vec2d pixel = transform.WorldToPixel(points[i].x, points[i].y); | |
| 334 | 605880 | lattice[i] = {pixel.x - 0.5, pixel.y - 0.5}; | |
| 335 | } | ||
| 336 | 197595 | const auto taps = static_cast<double>(2 * KernelRadius(m_method)); | |
| 337 | 197593 | detail::ExpandPoints( | |
| 338 | 197593 | Span<const Vec2d>(lattice), heights, 4.0 * taps * taps, | |
| 339 | 197635 | [&](Span<const Vec2d> at) { return TapBox(m_method, at); }, | |
| 340 | 197593 | [&](const detail::IndexBox& box, Span<const Vec2d> at, Span<float> values) { | |
| 341 | 197629 | InterpolateBlock(detail::ReadPosts(posts, box), m_method, at, values); | |
| 342 | 197628 | }); | |
| 343 | 197590 | }); | |
| 344 | 197562 | } | |
| 345 | |||
| 346 | 256 | std::shared_ptr<const Upsampler> DefaultUpsampler() { | |
| 347 | 340 | static const std::shared_ptr<const Upsampler> cubic = std::make_shared<KernelUpsampler>(); | |
| 348 | 512 | return cubic; | |
| 349 | } | ||
| 350 | |||
| 351 | 414 | std::shared_ptr<const Upsampler> FittedUpsampler(std::shared_ptr<const Upsampler> upsampler, | |
| 352 | const PostLattice& posts) { | ||
| 353 | 414 | if (!upsampler) { | |
| 354 | 418 | upsampler = DefaultUpsampler(); | |
| 355 | } | ||
| 356 | 414 | std::shared_ptr<const Upsampler> fitted = upsampler->FitTo(posts); | |
| 357 | 828 | return fitted ? fitted : upsampler; | |
| 358 | 414 | } | |
| 359 | |||
| 360 | 7 | const std::vector<std::string>& KernelUpsamplerNames() { | |
| 361 | 4 | static const std::vector<std::string> names = [] { | |
| 362 | 4 | std::vector<std::string> list; | |
| 363 | 28 | for (const auto& entry : kKernelNames) { | |
| 364 | 24 | list.emplace_back(entry.first); | |
| 365 | } | ||
| 366 | 4 | return list; | |
| 367 | 7 | }(); | |
| 368 | 7 | return names; | |
| 369 | } | ||
| 370 | |||
| 371 | 138 | std::optional<ResampleMethod> KernelUpsamplerMethod(const std::string& name) { | |
| 372 | 605 | for (const auto& entry : kKernelNames) { | |
| 373 | 559 | if (name == entry.first) { | |
| 374 | 92 | return entry.second; | |
| 375 | } | ||
| 376 | } | ||
| 377 | 46 | return std::nullopt; | |
| 378 | } | ||
| 379 | |||
| 380 | 11 | void RegisterUpsampler(const std::string& name, UpsamplerFactory factory) { | |
| 381 | 11 | if (name.empty()) { | |
| 382 | 1 | throw std::invalid_argument("An upsampler type needs a name"); | |
| 383 | } | ||
| 384 | 10 | if (KernelUpsamplerMethod(name) || name == "diamond_square") { | |
| 385 | 6 | throw std::invalid_argument("\"" + name + "\" is a built-in upsampler"); | |
| 386 | } | ||
| 387 | 8 | if (!factory) { | |
| 388 | 1 | throw std::invalid_argument("An upsampler type needs a factory"); | |
| 389 | } | ||
| 390 | 7 | Registry& registry = UpsamplerRegistry(); | |
| 391 | 7 | const std::lock_guard lock(registry.mutex); | |
| 392 | 7 | registry.factories[name] = std::move(factory); | |
| 393 | 7 | } | |
| 394 | |||
| 395 | 14 | bool IsUpsamplerRegistered(const std::string& name) { | |
| 396 | 14 | Registry& registry = UpsamplerRegistry(); | |
| 397 | 14 | const std::lock_guard lock(registry.mutex); | |
| 398 | 28 | return registry.factories.count(name) > 0; | |
| 399 | 14 | } | |
| 400 | |||
| 401 | 7 | std::vector<std::string> RegisteredUpsamplerNames() { | |
| 402 | 7 | Registry& registry = UpsamplerRegistry(); | |
| 403 | 7 | const std::lock_guard lock(registry.mutex); | |
| 404 | 7 | std::vector<std::string> names; | |
| 405 | 11 | for (const auto& entry : registry.factories) { | |
| 406 | 4 | names.push_back(entry.first); | |
| 407 | } | ||
| 408 | 7 | return names; | |
| 409 | 7 | } | |
| 410 | |||
| 411 | 10 | std::shared_ptr<const Upsampler> MakeRegisteredUpsampler(const std::string& name, | |
| 412 | const std::string& settings) { | ||
| 413 | 10 | UpsamplerFactory factory; | |
| 414 | 10 | { | |
| 415 | 10 | Registry& registry = UpsamplerRegistry(); | |
| 416 | 10 | const std::lock_guard lock(registry.mutex); | |
| 417 | 10 | const auto found = registry.factories.find(name); | |
| 418 | 10 | if (found == registry.factories.end()) { | |
| 419 | 3 | throw std::invalid_argument("No upsampler type is registered as \"" + name + "\""); | |
| 420 | } | ||
| 421 | 9 | factory = found->second; | |
| 422 | 1 | } | |
| 423 | 9 | std::shared_ptr<const Upsampler> upsampler = factory(settings); | |
| 424 | 9 | if (!upsampler) { | |
| 425 | 3 | throw std::invalid_argument("The upsampler type \"" + name + "\" made no upsampler"); | |
| 426 | } | ||
| 427 | 8 | return upsampler; | |
| 428 | 9 | } | |
| 429 | |||
| 430 | } // namespace terraingen | ||
| 431 |