src/sources/source_chain.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/sources/source_chain.h" | ||
| 4 | |||
| 5 | #include "sources/lattice.h" | ||
| 6 | #include "sources/seam_field.h" | ||
| 7 | #include "terraingen/random/stream.h" | ||
| 8 | #include "terraingen/util/parallel.h" | ||
| 9 | #include "util/checks.h" | ||
| 10 | |||
| 11 | #include <algorithm> | ||
| 12 | #include <cmath> | ||
| 13 | #include <iterator> | ||
| 14 | #include <limits> | ||
| 15 | #include <optional> | ||
| 16 | #include <stdexcept> | ||
| 17 | #include <string> | ||
| 18 | #include <utility> | ||
| 19 | #include <vector> | ||
| 20 | |||
| 21 | namespace terraingen { | ||
| 22 | |||
| 23 | namespace { | ||
| 24 | |||
| 25 | /// Posts a seam lattice adds beyond the reach on every side, for the stencils and interpolation. | ||
| 26 | constexpr double kSeamBorderPosts = 3.0; | ||
| 27 | |||
| 28 | /// The most posts a seam lattice may have along a side. | ||
| 29 | constexpr double kMaxSeamLatticeSide = 2147483647.0; | ||
| 30 | |||
| 31 | /// `value` shifted by `correction`, or `value` itself where the correction is 0. | ||
| 32 | 87945 | float Seamed(float value, double correction) { | |
| 33 | 87945 | return correction == 0.0 ? value : static_cast<float>(static_cast<double>(value) + correction); | |
| 34 | } | ||
| 35 | |||
| 36 | /// The smallest window holding every cell of `heights` without data; empty without one. | ||
| 37 | 31 | PixelWindow MissingWindow(const Raster<float>& heights) { | |
| 38 | 31 | PixelWindow window{heights.Width(), 0, heights.Height(), 0}; | |
| 39 | 1595 | for (std::size_t row = 0; row < heights.Height(); ++row) { | |
| 40 | 1564 | const Span<const float> values = heights.Row(row); | |
| 41 | 5886 | const auto missing = [&](float value) { return heights.IsMissing(value); }; | |
| 42 | 1564 | const float* first = std::find_if(values.begin(), values.end(), missing); | |
| 43 | 1564 | if (first == values.end()) { | |
| 44 | 28 | continue; | |
| 45 | } | ||
| 46 | 1536 | const auto last = std::find_if(std::make_reverse_iterator(values.end()), | |
| 47 | 1536 | std::make_reverse_iterator(first), missing); | |
| 48 | 3072 | window.colBegin = | |
| 49 | 1536 | std::min(window.colBegin, static_cast<std::size_t>(first - values.begin())); | |
| 50 | 1536 | window.colEnd = | |
| 51 | 1536 | std::max(window.colEnd, static_cast<std::size_t>(last.base() - values.begin())); | |
| 52 | 1536 | window.rowBegin = std::min(window.rowBegin, row); | |
| 53 | 1536 | window.rowEnd = row + 1; | |
| 54 | } | ||
| 55 | 31 | return window; | |
| 56 | } | ||
| 57 | |||
| 58 | /// `measure` of the first of `sources` with a height at (`x`, `y`) at an infinite spacing, or | ||
| 59 | /// infinity without one. Sources give a finite measure only where they have a height, so only a | ||
| 60 | /// source measuring infinity is asked for its height. | ||
| 61 | template <typename Measure> | ||
| 62 | 130 | double FirstWithHeight(const std::vector<std::shared_ptr<const Source>>& sources, double x, | |
| 63 | double y, const Measure& measure) { | ||
| 64 | 130 | constexpr double kInfinity = std::numeric_limits<double>::infinity(); | |
| 65 | 267 | for (const auto& source : sources) { | |
| 66 | 246 | const double value = measure(*source); | |
| 67 | 246 | if (std::isfinite(value) || !std::isnan(source->HeightAt(x, y, kInfinity))) { | |
| 68 | 109 | return value; | |
| 69 | } | ||
| 70 | } | ||
| 71 | return kInfinity; | ||
| 72 | } | ||
| 73 | |||
| 74 | } // namespace | ||
| 75 | |||
| 76 | 134 | void ValidateChainSeams(const ChainSeams& seams) { | |
| 77 | 134 | RequirePositive(seams.width, "The seam width"); | |
| 78 | 132 | RequirePositive(seams.decayLength, "The seam decay length"); | |
| 79 | 129 | } | |
| 80 | |||
| 81 | 85 | double SeamReach(const ChainSeams& seams) { | |
| 82 | 85 | switch (seams.mode) { | |
| 83 | 15 | case ChainSeamMode::Feather: | |
| 84 | 15 | return seams.width; | |
| 85 | 69 | case ChainSeamMode::Height: | |
| 86 | 69 | case ChainSeamMode::Slope: | |
| 87 | 69 | return kSeamReachDecays * seams.decayLength; | |
| 88 | case ChainSeamMode::None: | ||
| 89 | break; | ||
| 90 | } | ||
| 91 | return 0.0; | ||
| 92 | } | ||
| 93 | |||
| 94 | 63 | RasterGrid SeamLattice(const RasterGrid& grid, const ChainSeams& seams) { | |
| 95 | 63 | ValidateChainSeams(seams); | |
| 96 | 63 | const GeoTransform& transform = grid.transform; | |
| 97 | 63 | const double side = std::min(transform.CellWidth(), transform.CellHeight()); | |
| 98 | 63 | if (!IsPositive(side)) { | |
| 99 | 1 | throw std::invalid_argument("A seam needs grid cells of a positive, finite size"); | |
| 100 | } | ||
| 101 | 62 | const double pad = std::ceil(SeamReach(seams) / side) + kSeamBorderPosts; | |
| 102 | 62 | const double width = static_cast<double>(grid.width) + 2.0 * pad; | |
| 103 | 62 | const double height = static_cast<double>(grid.height) + 2.0 * pad; | |
| 104 | 62 | if (!(width <= kMaxSeamLatticeSide && height <= kMaxSeamLatticeSide)) { | |
| 105 | 2 | throw std::invalid_argument("A seam reaching " + std::to_string(pad) + | |
| 106 | " posts beyond the grid needs a lattice more than 2^31 - 1 " | ||
| 107 | 3 | "posts a side"); | |
| 108 | } | ||
| 109 | 61 | RasterGrid lattice{static_cast<std::size_t>(width), static_cast<std::size_t>(height), | |
| 110 | 61 | transform}; | |
| 111 | 61 | const Vec2d origin = transform.PixelToWorld(-pad, -pad); | |
| 112 | 61 | lattice.transform.originX = origin.x; | |
| 113 | 61 | lattice.transform.originY = origin.y; | |
| 114 | 61 | return lattice; | |
| 115 | } | ||
| 116 | |||
| 117 | 70 | SourceChain::SourceChain(std::vector<std::shared_ptr<const Source>> sources, ChainSeams seams) | |
| 118 | 70 | : m_sources(std::move(sources)), m_seams(seams) { | |
| 119 | 70 | if (m_sources.empty()) { | |
| 120 | 1 | throw std::invalid_argument("A source chain needs at least one source"); | |
| 121 | } | ||
| 122 | 207 | for (const auto& source : m_sources) { | |
| 123 | 139 | if (!source) { | |
| 124 | 1 | throw std::invalid_argument("A source chain cannot hold a null source"); | |
| 125 | } | ||
| 126 | } | ||
| 127 | 68 | ValidateChainSeams(m_seams); | |
| 128 | 64 | if (m_sources.size() < 2) { | |
| 129 | 2 | return; | |
| 130 | } | ||
| 131 | 62 | const std::optional<RasterGrid> grid = BoundedGrid(*m_sources[m_sources.size() - 2]); | |
| 132 | 62 | if (grid && m_seams.mode != ChainSeamMode::None) { | |
| 133 | 29 | static_cast<void>(SeamLattice(*grid, m_seams)); | |
| 134 | } | ||
| 135 | 61 | m_prefix = m_sources.size() == 2 | |
| 136 | 122 | ? m_sources.front() | |
| 137 | 69 | : std::make_shared<SourceChain>(std::vector<std::shared_ptr<const Source>>( | |
| 138 | 4 | m_sources.begin(), m_sources.end() - 1), | |
| 139 | 65 | m_seams); | |
| 140 | 7 | } | |
| 141 | |||
| 142 | 158 | SourceChain::~SourceChain() = default; | |
| 143 | |||
| 144 | 27524 | const detail::SeamField& SourceChain::Seam() const { | |
| 145 | 27524 | std::call_once(m_seamed, [this] { | |
| 146 | 34 | const std::optional<RasterGrid> grid = BoundedGrid(*m_sources[m_sources.size() - 2]); | |
| 147 | 68 | m_seam = std::make_shared<const detail::SeamField>( | |
| 148 | 34 | grid ? detail::BuildSeamField(*m_prefix, *m_sources.back(), *grid, m_seams) | |
| 149 | 34 | : detail::SeamField{}); | |
| 150 | 34 | }); | |
| 151 | 27524 | return *m_seam; | |
| 152 | } | ||
| 153 | |||
| 154 | 44193 | void SourceChain::FillSamples(SampleBlock& samples, double spacing, unsigned threads) const { | |
| 155 | 44193 | if (!m_prefix) { | |
| 156 | ✗ | FillUnmarked(*m_sources.front(), samples, spacing, threads); | |
| 157 | ✗ | return; | |
| 158 | } | ||
| 159 | 44193 | FillUnmarked(*m_prefix, samples, spacing, threads); | |
| 160 | 44193 | if (Raster<float>* grid = samples.Grid()) { | |
| 161 | 31 | FillGridGaps(*grid, spacing, threads); | |
| 162 | } else { | ||
| 163 | 44162 | FillPointGaps(samples.Points(), samples.Heights(), spacing); | |
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | 31 | void SourceChain::FillGridGaps(Raster<float>& heights, double spacing, unsigned threads) const { | |
| 168 | 31 | const PixelWindow gaps = MissingWindow(heights); | |
| 169 | 31 | if (gaps.Empty()) { | |
| 170 | 1 | return; | |
| 171 | } | ||
| 172 | 30 | if (!heights.NoData()) { | |
| 173 | 25 | heights.SetNoData(std::numeric_limits<float>::quiet_NaN()); | |
| 174 | } | ||
| 175 | 30 | const RasterGrid grid = heights.Grid(); | |
| 176 | 30 | const std::optional<RasterGrid> part = detail::ExactSubgrid(grid, gaps); | |
| 177 | 30 | const PixelWindow window = part ? gaps : WholeWindow(grid); | |
| 178 | 30 | Raster<float> next((part ? *part : grid)); | |
| 179 | 30 | SampleBlock samples(next); | |
| 180 | 30 | FillUnmarked(*m_sources.back(), samples, spacing, threads); | |
| 181 | 90 | ParallelFor(window.Height(), threads, [&](std::size_t r) { | |
| 182 | 1536 | const std::size_t row = window.rowBegin + r; | |
| 183 | 1536 | const detail::SeamField* seam = nullptr; | |
| 184 | 111187 | for (std::size_t col = window.colBegin; col < window.colEnd; ++col) { | |
| 185 | 109653 | float& cell = heights(col, row); | |
| 186 | 109514 | const float value = next(col - window.colBegin, r); | |
| 187 | 109640 | if (!heights.IsMissing(cell) || next.IsMissing(value)) { | |
| 188 | 47729 | continue; | |
| 189 | } | ||
| 190 | 61911 | if (!seam) { | |
| 191 | 1504 | seam = &Seam(); | |
| 192 | } | ||
| 193 | 61911 | const Vec2d center = heights.CellCenter(col, row); | |
| 194 | 61911 | cell = Seamed(value, seam->At(center.x, center.y)); | |
| 195 | } | ||
| 196 | 1534 | }); | |
| 197 | 30 | } | |
| 198 | |||
| 199 | 44162 | void SourceChain::FillPointGaps(Span<const Vec2d> points, Span<float> heights, | |
| 200 | double spacing) const { | ||
| 201 | 44162 | detail::EvaluateChosen( | |
| 202 | points.size(), | ||
| 203 | 132490 | [&](std::size_t i) { | |
| 204 | 44166 | return std::isnan(heights[i]) ? std::optional<Vec2d>(points[i]) : std::nullopt; | |
| 205 | }, | ||
| 206 | 114371 | [&](Span<const Vec2d> gaps, Span<float> later) { | |
| 207 | 26047 | m_sources.back()->HeightsAt(gaps, later, spacing); | |
| 208 | 26047 | }, | |
| 209 | 44162 | [&](std::size_t i, float later) { | |
| 210 | 26049 | heights[i] = | |
| 211 | 26049 | std::isnan(later) ? later : Seamed(later, Seam().At(points[i].x, points[i].y)); | |
| 212 | 26049 | }); | |
| 213 | 44162 | } | |
| 214 | |||
| 215 | 127 | double SourceChain::ResolutionAt(double x, double y) const { | |
| 216 | 127 | return FirstWithHeight(m_sources, x, y, | |
| 217 | 367 | [&](const Source& source) { return source.ResolutionAt(x, y); }); | |
| 218 | } | ||
| 219 | |||
| 220 | 3 | double SourceChain::PostSpacingAt(double x, double y) const { | |
| 221 | 3 | return FirstWithHeight(m_sources, x, y, | |
| 222 | 9 | [&](const Source& source) { return source.PostSpacingAt(x, y); }); | |
| 223 | } | ||
| 224 | |||
| 225 | 4 | std::shared_ptr<const Source> SourceChain::Reseeded(std::uint64_t seed) const { | |
| 226 | 4 | std::vector<std::shared_ptr<const Source>> sources = m_sources; | |
| 227 | bool changed = false; | ||
| 228 | 12 | for (std::size_t n = 0; n < sources.size(); ++n) { | |
| 229 | 8 | const auto word = static_cast<std::uint32_t>(2 * n); | |
| 230 | 8 | const std::uint64_t part = JoinWords(ReseedWord(seed, word), ReseedWord(seed, word + 1)); | |
| 231 | 8 | if (auto reseeded = sources[n]->Reseeded(part)) { | |
| 232 | 3 | sources[n] = std::move(reseeded); | |
| 233 | 3 | changed = true; | |
| 234 | 8 | } | |
| 235 | } | ||
| 236 | 4 | return changed ? std::make_shared<SourceChain>(std::move(sources), m_seams) : nullptr; | |
| 237 | 4 | } | |
| 238 | |||
| 239 | } // namespace terraingen | ||
| 240 |