src/sources/synthetic_source.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/sources/synthetic_source.h" | ||
| 4 | |||
| 5 | #include "sources/lattice.h" | ||
| 6 | |||
| 7 | #include "terraingen/noise/validate.h" | ||
| 8 | #include "terraingen/random/stream.h" | ||
| 9 | #include "util/checks.h" | ||
| 10 | |||
| 11 | #include <cmath> | ||
| 12 | #include <limits> | ||
| 13 | #include <stdexcept> | ||
| 14 | #include <string> | ||
| 15 | #include <utility> | ||
| 16 | |||
| 17 | namespace terraingen { | ||
| 18 | |||
| 19 | 26 | SyntheticSource::NoisePosts::NoisePosts(const SyntheticSource& source, double pixelSize) | |
| 20 | 26 | : m_source(source), | |
| 21 | 26 | m_transform(GeoTransform::NorthUp(-0.5 * pixelSize, 0.5 * pixelSize, pixelSize)) {} | |
| 22 | |||
| 23 | 34776 | void SyntheticSource::NoisePosts::Read(std::int64_t col0, std::int64_t row0, std::size_t width, | |
| 24 | std::size_t height, Span<float> heights) const { | ||
| 25 | 34776 | std::vector<Vec2d> points(width * height); | |
| 26 | 101591 | for (std::size_t r = 0; r < height; ++r) { | |
| 27 | 66815 | const double row = static_cast<double>(row0 + static_cast<std::int64_t>(r)) + 0.5; | |
| 28 | 2221953 | for (std::size_t c = 0; c < width; ++c) { | |
| 29 | 2155138 | const double col = static_cast<double>(col0 + static_cast<std::int64_t>(c)) + 0.5; | |
| 30 | 2155138 | points[r * width + c] = m_transform.PixelToWorld(col, row); | |
| 31 | } | ||
| 32 | } | ||
| 33 | 34776 | m_source.NoiseAt(points, heights); | |
| 34 | 34776 | } | |
| 35 | |||
| 36 | 432 | SyntheticSource::SyntheticSource(std::vector<Fractal> fractals, float elevation, | |
| 37 | std::optional<double> pixelSize, | ||
| 38 | 432 | std::shared_ptr<const Upsampler> upsampler, Surface surface) | |
| 39 | 432 | : m_fractals(std::move(fractals)), m_elevation(elevation), m_givenPixelSize(pixelSize), | |
| 40 | 433 | m_pixelSize(pixelSize ? std::optional<double>(*pixelSize / surface.UnitLength()) | |
| 41 | : std::nullopt), | ||
| 42 | 864 | m_surface(surface), m_givenUpsampler(std::move(upsampler)) { | |
| 43 | 432 | ValidateFractals(m_fractals); | |
| 44 | 430 | RequireFinite(static_cast<double>(m_elevation), "Elevation"); | |
| 45 | 429 | if (!m_pixelSize) { | |
| 46 | 402 | if (m_givenUpsampler) { | |
| 47 | 2 | throw std::invalid_argument( | |
| 48 | 2 | "A synthetic source upsamples only posts, so an upsampler needs a pixel size"); | |
| 49 | } | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | 27 | RequirePositive(*m_pixelSize, "Pixel size"); | |
| 53 | 26 | m_posts.emplace(*this, *m_pixelSize); | |
| 54 | 65 | m_upsampler = FittedUpsampler(m_givenUpsampler, *m_posts); | |
| 55 | 8 | } | |
| 56 | |||
| 57 | 22 | std::shared_ptr<const Source> SyntheticSource::Reseeded(std::uint64_t seed) const { | |
| 58 | 22 | std::vector<Fractal> fractals = m_fractals; | |
| 59 | 47 | for (std::size_t n = 0; n < fractals.size(); ++n) { | |
| 60 | 25 | fractals[n].seed = ReseedWord(seed, static_cast<std::uint32_t>(n)); | |
| 61 | } | ||
| 62 | 22 | std::shared_ptr<const Upsampler> upsampler = m_givenUpsampler; | |
| 63 | 22 | if (upsampler) { | |
| 64 | 3 | if (auto reseeded = upsampler->Reseeded( | |
| 65 | 3 | JoinWords(ReseedWord(seed, kMaxFractals), ReseedWord(seed, kMaxFractals + 1)))) { | |
| 66 | 3 | upsampler = std::move(reseeded); | |
| 67 | 3 | } | |
| 68 | } | ||
| 69 | 22 | if (fractals.empty() && upsampler == m_givenUpsampler) { | |
| 70 | 2 | return nullptr; | |
| 71 | } | ||
| 72 | 20 | return std::make_shared<SyntheticSource>(std::move(fractals), m_elevation, m_givenPixelSize, | |
| 73 | 20 | std::move(upsampler), m_surface); | |
| 74 | 22 | } | |
| 75 | |||
| 76 | 125332 | void SyntheticSource::NoiseAt(Span<const Vec2d> points, Span<float> heights) const { | |
| 77 | 125332 | FractalStackRows(m_fractals, {}, m_surface, points, heights); | |
| 78 | 3746037 | for (float& height : heights) { | |
| 79 | 3620613 | height = m_elevation + height; | |
| 80 | } | ||
| 81 | 125424 | } | |
| 82 | |||
| 83 | 392 | RasterGrid SyntheticSource::Grid() const { | |
| 84 | 392 | return {0, 0, m_posts ? m_posts->Transform() : GeoTransform::NorthUp(0.0, 0.0, 1.0)}; | |
| 85 | } | ||
| 86 | |||
| 87 | 91787 | void SyntheticSource::FillSamples(SampleBlock& samples, double spacing, unsigned threads) const { | |
| 88 | 91787 | if (m_posts) { | |
| 89 | 1237 | m_upsampler->Fill(*m_posts, samples, spacing, threads); | |
| 90 | 1237 | return; | |
| 91 | } | ||
| 92 | 90550 | detail::FillByTiles(samples, threads, [&](Span<const Vec2d> points, Span<float> heights) { | |
| 93 | 90610 | NoiseAt(points, heights); | |
| 94 | 225 | }); | |
| 95 | } | ||
| 96 | |||
| 97 | 7 | double SyntheticSource::PostSpacingAt(double /*x*/, double /*y*/) const { | |
| 98 | 7 | return m_pixelSize.value_or(std::numeric_limits<double>::infinity()); | |
| 99 | } | ||
| 100 | |||
| 101 | } // namespace terraingen | ||
| 102 |