src/albedo/raster_albedo.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/albedo/raster_albedo.h" | ||
| 4 | |||
| 5 | #include "terraingen/albedo/albedo_fields.h" | ||
| 6 | #include "terraingen/analysis/variogram.h" | ||
| 7 | #include "terraingen/noise/hash.h" | ||
| 8 | #include "terraingen/noise/perlin.h" | ||
| 9 | #include "terraingen/noise/structure_function.h" | ||
| 10 | #include "terraingen/random/streams.h" | ||
| 11 | |||
| 12 | #include <algorithm> | ||
| 13 | #include <array> | ||
| 14 | #include <cmath> | ||
| 15 | #include <limits> | ||
| 16 | #include <stdexcept> | ||
| 17 | #include <utility> | ||
| 18 | |||
| 19 | namespace terraingen { | ||
| 20 | |||
| 21 | namespace { | ||
| 22 | |||
| 23 | /// Step between the seeds of successive detail octaves (2^32 over the golden ratio). | ||
| 24 | constexpr std::uint32_t kOctaveSeedStep = 0x9e3779b9u; | ||
| 25 | |||
| 26 | /// The logarithm of the mean of the bands at each cell, NaN where a band has no data or is not | ||
| 27 | /// positive. | ||
| 28 | 18 | Raster<float> LogBrightness(const std::vector<Raster<float>>& bands) { | |
| 29 | 18 | Raster<float> logs(bands.front().Width(), bands.front().Height()); | |
| 30 | 458 | for (std::size_t row = 0; row < logs.Height(); ++row) { | |
| 31 | 15182 | for (std::size_t col = 0; col < logs.Width(); ++col) { | |
| 32 | 14742 | double sum = 0.0; | |
| 33 | 29996 | for (const Raster<float>& band : bands) { | |
| 34 | 15254 | const float value = band(col, row); | |
| 35 | 31563 | sum += band.IsNoData(value) || !std::isfinite(value) || value <= 0.0f | |
| 36 | 16309 | ? std::numeric_limits<double>::quiet_NaN() | |
| 37 | : static_cast<double>(value); | ||
| 38 | } | ||
| 39 | 14742 | logs(col, row) = static_cast<float>(std::log(sum / static_cast<double>(bands.size()))); | |
| 40 | } | ||
| 41 | } | ||
| 42 | 18 | return logs; | |
| 43 | } | ||
| 44 | |||
| 45 | /// The RMS of `PerlinNoise2` over a fixed set of points, so octaves can be given an RMS. | ||
| 46 | 224707 | double PerlinRms() { | |
| 47 | 7 | static const double rms = [] { | |
| 48 | 7 | double sum = 0.0; | |
| 49 | 7 | constexpr int kSide = 256; | |
| 50 | 1799 | for (int j = 0; j < kSide; ++j) { | |
| 51 | 460544 | for (int i = 0; i < kSide; ++i) { | |
| 52 | 458752 | const float v = PerlinNoise2( | |
| 53 | 458752 | i / 8, j / 8, static_cast<float>(i % 8) / 8.0f + 1.0f / 16.0f, | |
| 54 | 458752 | static_cast<float>(j % 8) / 8.0f + 1.0f / 16.0f, kAlbedoDetailRmsStream); | |
| 55 | 458752 | sum += static_cast<double>(v) * static_cast<double>(v); | |
| 56 | } | ||
| 57 | } | ||
| 58 | 7 | return std::sqrt(sum / (kSide * kSide)); | |
| 59 | 224707 | }(); | |
| 60 | 224707 | return rms; | |
| 61 | } | ||
| 62 | |||
| 63 | } // namespace | ||
| 64 | |||
| 65 | 26 | void ValidateAlbedoDetail(const AlbedoDetail& detail) { | |
| 66 | 26 | CheckRanges(detail); | |
| 67 | 22 | ValidateResolutionFade(detail.fade); | |
| 68 | 21 | } | |
| 69 | |||
| 70 | 26 | RasterAlbedo::RasterAlbedo(std::vector<Raster<float>> bands, RasterAlbedoOptions options) | |
| 71 | 26 | : m_bands(std::move(bands)), m_options(options) { | |
| 72 | 26 | if (m_bands.size() != 1 && m_bands.size() != kMaxAlbedoBands) { | |
| 73 | 1 | throw std::invalid_argument("An albedo map must have 1 or 3 bands"); | |
| 74 | } | ||
| 75 | 25 | const Raster<float>& first = m_bands.front(); | |
| 76 | 25 | if (first.Empty() || first.Transform().IsRotated()) { | |
| 77 | 2 | throw std::invalid_argument("An albedo map must be neither empty nor rotated"); | |
| 78 | } | ||
| 79 | 48 | for (const Raster<float>& band : m_bands) { | |
| 80 | 26 | if (band.Width() != first.Width() || band.Height() != first.Height() || | |
| 81 | 25 | !(band.Transform() == first.Transform())) { | |
| 82 | 1 | throw std::invalid_argument("The bands of an albedo map must share one grid"); | |
| 83 | } | ||
| 84 | } | ||
| 85 | 22 | ValidateAlbedoDetail(m_options.detail); | |
| 86 | 18 | MeasureRoughness(); | |
| 87 | 18 | if (m_options.detail.hurst) { | |
| 88 | 1 | m_hurst = *m_options.detail.hurst; | |
| 89 | } | ||
| 90 | 26 | } | |
| 91 | |||
| 92 | 18 | void RasterAlbedo::MeasureRoughness() { | |
| 93 | 18 | const Raster<float> logs = LogBrightness(m_bands); | |
| 94 | 18 | const DeviogramFit fit = FitDeviogram(logs); | |
| 95 | 18 | m_roughness = fit.cellRms / std::sqrt(2.0); | |
| 96 | 18 | if (fit.lags >= 2) { | |
| 97 | 32 | m_hurst = std::clamp(fit.slope, 0.0, 1.0); | |
| 98 | } | ||
| 99 | 18 | } | |
| 100 | |||
| 101 | 226334 | double RasterAlbedo::DetailSum(double x, double y, double spacing, const Surface& surface) const { | |
| 102 | 226334 | const AlbedoDetail& detail = m_options.detail; | |
| 103 | 226334 | const Vec2d origin = m_bands.front().CellCenter(0, 0); | |
| 104 | 226334 | const double cell = m_bands.front().Grid().Spacing(surface); | |
| 105 | double sum = 0.0; | ||
| 106 | 843511 | for (int octave = 0; octave < detail.octaves; ++octave) { | |
| 107 | 842968 | const double wavelength = cell / std::exp2(octave + 1); | |
| 108 | 842968 | const double weight = ResolutionWeight(wavelength, spacing, detail.fade); | |
| 109 | 842936 | if (weight == 0.0) { | |
| 110 | break; | ||
| 111 | } | ||
| 112 | 617177 | const std::uint32_t seed = | |
| 113 | 617177 | PcgHash(detail.seed + kOctaveSeedStep * static_cast<std::uint32_t>(octave)); | |
| 114 | 617177 | float noise = 0.0f; | |
| 115 | 617177 | if (surface.IsBody()) { | |
| 116 | 1609 | const Vec3d p = surface.Position({x, y}); | |
| 117 | 1609 | noise = PerlinNoise3Precise(p.x / wavelength, p.y / wavelength, p.z / wavelength, seed); | |
| 118 | } else { | ||
| 119 | 615568 | noise = | |
| 120 | 615568 | PerlinNoise2Precise((x - origin.x) / wavelength, (y - origin.y) / wavelength, seed); | |
| 121 | } | ||
| 122 | 617177 | sum += weight * std::exp2(-(octave + 1) * m_hurst) * static_cast<double>(noise); | |
| 123 | } | ||
| 124 | 226302 | return sum; | |
| 125 | } | ||
| 126 | |||
| 127 | 228889 | AlbedoSample RasterAlbedo::ValueAt(double x, double y, const LayerContext& context) const { | |
| 128 | 228889 | AlbedoSample value{}; | |
| 129 | 458670 | for (std::size_t band = 0; band < m_bands.size(); ++band) { | |
| 130 | 230589 | value[band] = SampleRaster(m_bands[band], x, y, m_options.method); | |
| 131 | 230607 | if (std::isnan(value[band])) { | |
| 132 | 826 | value.fill(std::numeric_limits<float>::quiet_NaN()); | |
| 133 | 826 | return value; | |
| 134 | } | ||
| 135 | } | ||
| 136 | 228081 | const AlbedoDetail& detail = m_options.detail; | |
| 137 | 228081 | if (!detail.enabled || detail.scale == 0.0 || m_roughness == 0.0) { | |
| 138 | 1747 | return value; | |
| 139 | } | ||
| 140 | 226334 | const Surface& surface = context.SurfaceOrPlane(); | |
| 141 | 226334 | const double sum = DetailSum(x, y, context.spacing, surface); | |
| 142 | 226310 | const double rms = | |
| 143 | 226310 | surface.IsBody() ? std::sqrt(0.5 * Perlin3StructureFunction(1e6)) : PerlinRms(); | |
| 144 | 226294 | const double factor = std::exp(detail.scale * m_roughness * sum / rms); | |
| 145 | 454102 | for (std::size_t band = 0; band < m_bands.size(); ++band) { | |
| 146 | 227808 | value[band] = static_cast<float>(static_cast<double>(value[band]) * factor); | |
| 147 | } | ||
| 148 | 226294 | return value; | |
| 149 | } | ||
| 150 | |||
| 151 | } // namespace terraingen | ||
| 152 |