src/layers/noise_layer.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/noise_layer.h" | ||
| 4 | |||
| 5 | #include "layers/kernel.h" | ||
| 6 | #include "terraingen/layers/source_resolved.h" | ||
| 7 | #include "terraingen/noise/surface_noise.h" | ||
| 8 | #include "terraingen/noise/validate.h" | ||
| 9 | #include "terraingen/random/stream.h" | ||
| 10 | #include "terraingen/raster/cell_loops.h" | ||
| 11 | #include "terraingen/util/parallel.h" | ||
| 12 | |||
| 13 | #include <algorithm> | ||
| 14 | #include <cmath> | ||
| 15 | #include <cstddef> | ||
| 16 | #include <cstdint> | ||
| 17 | #include <memory> | ||
| 18 | #include <utility> | ||
| 19 | #include <vector> | ||
| 20 | |||
| 21 | namespace terraingen { | ||
| 22 | |||
| 23 | namespace { | ||
| 24 | |||
| 25 | /// Adds the noise of `fractals` (`FractalStackAt`) to each sample of `samples` holding data, to | ||
| 26 | /// the same bits: a grid row by row over `threads` threads, or the points in one run | ||
| 27 | /// (`FractalStackRows`). | ||
| 28 | 2196915 | void AddNoiseRows(SampleBlock& samples, unsigned threads, const std::vector<Fractal>& fractals, | |
| 29 | const Vec2d& origin, const Surface& surface) { | ||
| 30 | 2196915 | if (Raster<float>* grid = samples.Grid()) { | |
| 31 | 249 | ParallelFor(grid->Height(), threads, [&](std::size_t row) { | |
| 32 | 18343 | std::vector<Vec2d> points(grid->Width()); | |
| 33 | 18348 | std::vector<float> noise(grid->Width()); | |
| 34 | 4732510 | for (std::size_t col = 0; col < points.size(); ++col) { | |
| 35 | 4714166 | points[col] = grid->CellCenter(col, row); | |
| 36 | } | ||
| 37 | 18344 | FractalStackRows(fractals, origin, surface, points, noise); | |
| 38 | 4759325 | for (std::size_t col = 0; col < points.size(); ++col) { | |
| 39 | 4740976 | float& cell = (*grid)(col, row); | |
| 40 | 4772258 | if (!grid->IsNoData(cell)) { | |
| 41 | 4740975 | cell = cell + noise[col]; | |
| 42 | } | ||
| 43 | } | ||
| 44 | 18349 | }); | |
| 45 | 249 | return; | |
| 46 | } | ||
| 47 | 2196666 | std::vector<Vec2d> points(samples.Size()); | |
| 48 | 2192835 | std::vector<float> noise(samples.Size()); | |
| 49 | 5138267 | for (std::size_t i = 0; i < points.size(); ++i) { | |
| 50 | 2941629 | points[i] = samples.Position(i); | |
| 51 | } | ||
| 52 | 2196638 | FractalStackRows(fractals, origin, surface, points, noise); | |
| 53 | 5147662 | for (std::size_t i = 0; i < points.size(); ++i) { | |
| 54 | 2945455 | if (!samples.IsNoData(i)) { | |
| 55 | 2952917 | samples.Height(i) = samples.Height(i) + noise[i]; | |
| 56 | } | ||
| 57 | } | ||
| 58 | 2202207 | } | |
| 59 | |||
| 60 | } // namespace | ||
| 61 | |||
| 62 | 392 | NoiseLayer::NoiseLayer(NoiseLayerOptions options) : m_options(std::move(options)) { | |
| 63 | 392 | ValidateFractals(m_options.fractals, 1); | |
| 64 | 389 | ValidateSourceResolvesPixels(m_options.sourceResolvesPixels); | |
| 65 | 388 | m_kernel = MakeNoiseLayerKernel(m_options.fractals); | |
| 66 | 388 | if (!m_options.sourceResolvesPixels) { | |
| 67 | return; | ||
| 68 | } | ||
| 69 | 7 | for (const Fractal& fractal : m_options.fractals) { | |
| 70 | 4 | m_wavelengths.push_back(static_cast<double>(fractal.wavelength)); | |
| 71 | } | ||
| 72 | 3 | std::sort(m_wavelengths.begin(), m_wavelengths.end()); | |
| 73 | 3 | m_wavelengths.erase(std::unique(m_wavelengths.begin(), m_wavelengths.end()), | |
| 74 | 3 | m_wavelengths.end()); | |
| 75 | 10 | for (std::size_t kept = 0; kept <= m_wavelengths.size(); ++kept) { | |
| 76 | 7 | std::vector<Fractal> stack = m_options.fractals; | |
| 77 | 17 | for (Fractal& fractal : stack) { | |
| 78 | 10 | if (kept < m_wavelengths.size() && | |
| 79 | 6 | static_cast<double>(fractal.wavelength) >= m_wavelengths[kept]) { | |
| 80 | 5 | fractal.enabled = false; | |
| 81 | } | ||
| 82 | } | ||
| 83 | 7 | m_stacks.push_back(std::move(stack)); | |
| 84 | 7 | } | |
| 85 | 12 | } | |
| 86 | |||
| 87 | 20 | LayerPtr NoiseLayer::Reseeded(std::uint64_t seed) const { | |
| 88 | 20 | NoiseLayerOptions options = m_options; | |
| 89 | 43 | for (std::size_t n = 0; n < options.fractals.size(); ++n) { | |
| 90 | 23 | options.fractals[n].seed = ReseedWord(seed, static_cast<std::uint32_t>(n)); | |
| 91 | } | ||
| 92 | 20 | return std::make_shared<NoiseLayer>(std::move(options)); | |
| 93 | 20 | } | |
| 94 | |||
| 95 | 2187885 | void NoiseLayer::ApplySamples(SampleBlock& samples, const LayerContext& context) const { | |
| 96 | 2187885 | const Surface& surface = context.SurfaceOrPlane(); | |
| 97 | 2195691 | const Vec2d origin = context.ReferencePoint(); | |
| 98 | 2196796 | if (context.source != nullptr && m_options.sourceResolvesPixels) { | |
| 99 | 380 | MapSamples(samples, context.threads, [&](float height, const Vec2d& p) { | |
| 100 | 635 | const double resolved = SourceResolvedSize( | |
| 101 | 635 | context.source, m_options.sourceResolvesPixels, surface, p.x, p.y); | |
| 102 | 635 | const auto kept = | |
| 103 | 635 | std::isnan(resolved) | |
| 104 | 1270 | ? m_wavelengths.size() | |
| 105 | : static_cast<std::size_t>( | ||
| 106 | 635 | std::lower_bound(m_wavelengths.begin(), m_wavelengths.end(), resolved) - | |
| 107 | 635 | m_wavelengths.begin()); | |
| 108 | 635 | return height + FractalStackAt(m_stacks[kept], origin, surface, p.x, p.y); | |
| 109 | }); | ||
| 110 | 395 | return; | |
| 111 | } | ||
| 112 | 2196416 | Raster<float>* grid = samples.Grid(); | |
| 113 | 2196433 | if (!surface.IsBody() && grid != nullptr && context.backend != Backend::Cpu && | |
| 114 | 17 | RunKernel(context.backend, CompiledKernel<NoiseLayerKernel>(), AsBytes(Span(&m_kernel, 1)), | |
| 115 | *grid, origin)) { | ||
| 116 | 15 | return; | |
| 117 | } | ||
| 118 | 2196401 | AddNoiseRows(samples, context.threads, m_options.fractals, origin, surface); | |
| 119 | } | ||
| 120 | |||
| 121 | } // namespace terraingen | ||
| 122 |