src/albedo/rock_albedo.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/albedo/rock_albedo.h" | ||
| 4 | |||
| 5 | #include "albedo/bands.h" | ||
| 6 | #include "terraingen/albedo/albedo_fields.h" | ||
| 7 | #include "terraingen/noise/hash.h" | ||
| 8 | #include "terraingen/random/samplers.h" | ||
| 9 | #include "terraingen/util/bits.h" | ||
| 10 | #include "terraingen/util/parallel.h" | ||
| 11 | |||
| 12 | #include <algorithm> | ||
| 13 | #include <cmath> | ||
| 14 | #include <stdexcept> | ||
| 15 | #include <utility> | ||
| 16 | |||
| 17 | namespace terraingen { | ||
| 18 | |||
| 19 | 18 | void ValidateRockAlbedoOptions(const RockAlbedoOptions& options) { | |
| 20 | 18 | CheckRanges(options); | |
| 21 | 16 | } | |
| 22 | |||
| 23 | 530009 | double RockShare(double resolved, const UnresolvedCover& unresolved, double normal) { | |
| 24 | 530009 | double drawn = 0.0; | |
| 25 | 530009 | if (unresolved.mean > 0.0) { | |
| 26 | 525913 | const double s2 = std::log1p(unresolved.variance / (unresolved.mean * unresolved.mean)); | |
| 27 | 525913 | drawn = | |
| 28 | 525913 | std::fmin(1.0, std::exp(std::log(unresolved.mean) - 0.5 * s2 + std::sqrt(s2) * normal)); | |
| 29 | } | ||
| 30 | 530009 | const double clamped = std::clamp(resolved, 0.0, 1.0); | |
| 31 | 530009 | return clamped + (1.0 - clamped) * drawn; | |
| 32 | } | ||
| 33 | |||
| 34 | 10 | RockAlbedo::RockAlbedo(RockAlbedoOptions options, std::vector<RockSource> sources) | |
| 35 | 10 | : m_options(options), m_sources(std::move(sources)) { | |
| 36 | 10 | ValidateRockAlbedoOptions(m_options); | |
| 37 | 18 | for (const RockSource& source : m_sources) { | |
| 38 | 10 | if (!source.layer || !source.ground) { | |
| 39 | 2 | throw std::invalid_argument( | |
| 40 | 2 | "A rock albedo source needs a boulder layer and its ground"); | |
| 41 | } | ||
| 42 | } | ||
| 43 | 10 | } | |
| 44 | |||
| 45 | namespace { | ||
| 46 | |||
| 47 | /// The context `source`'s boulder layer is read in for an albedo evaluated in `context`. | ||
| 48 | 138 | LayerContext LayerOn(const RockSource& source, const LayerContext& context) { | |
| 49 | 138 | LayerContext layer; | |
| 50 | 138 | layer.spacing = context.spacing; | |
| 51 | 138 | layer.threads = context.threads; | |
| 52 | 138 | layer.ground = source.ground.get(); | |
| 53 | 138 | layer.surface = context.surface; | |
| 54 | 138 | layer.source = context.source; | |
| 55 | 138 | return layer; | |
| 56 | } | ||
| 57 | |||
| 58 | } // namespace | ||
| 59 | |||
| 60 | 325942 | double RockAlbedo::UnresolvedNormal(double x, double y, std::size_t source) const { | |
| 61 | 325942 | RandomStream random(m_options.seed, kRockAlbedoStream, HashWord64(BitCast<std::uint64_t>(x)), | |
| 62 | 325942 | HashWord64(BitCast<std::uint64_t>(y)), static_cast<std::uint32_t>(source)); | |
| 63 | 325955 | return StandardNormal(random); | |
| 64 | } | ||
| 65 | |||
| 66 | 132 | double RockAlbedo::ShareAt(double x, double y, const LayerContext& context) const { | |
| 67 | 132 | ValidateAlbedoContext(context); | |
| 68 | 132 | double bare = 1.0; | |
| 69 | 264 | for (std::size_t n = 0; n < m_sources.size(); ++n) { | |
| 70 | 132 | const RockSource& source = m_sources[n]; | |
| 71 | 132 | const double resolved = source.layer->MaterialCoverAt(x, y, LayerOn(source, context)); | |
| 72 | 132 | const UnresolvedCover unresolved = | |
| 73 | 132 | m_options.unresolved | |
| 74 | 132 | ? source.layer->UnresolvedCoverAt(x, y, context.spacing, source.ground.get()) | |
| 75 | 1 | : UnresolvedCover{}; | |
| 76 | 132 | bare *= 1.0 - RockShare(resolved, unresolved, | |
| 77 | 132 | m_options.unresolved ? UnresolvedNormal(x, y, n) : 0.0); | |
| 78 | } | ||
| 79 | 132 | return 1.0 - bare; | |
| 80 | } | ||
| 81 | |||
| 82 | 131 | void RockAlbedo::ApplyAt(AlbedoSample& value, std::size_t bands, double x, double y, | |
| 83 | const LayerContext& context) const { | ||
| 84 | 131 | ChangeBands(value, bands, ShareAt(x, y, context) * (m_options.reflectanceRatio - 1.0)); | |
| 85 | 131 | } | |
| 86 | |||
| 87 | 5 | std::vector<double> RockAlbedo::BareShares(const Raster<float>& first, | |
| 88 | const LayerContext& context) const { | ||
| 89 | 5 | const RasterGrid grid = first.Grid(); | |
| 90 | 5 | std::vector<double> bare(grid.width * grid.height, 1.0); | |
| 91 | 11 | for (std::size_t n = 0; n < m_sources.size(); ++n) { | |
| 92 | 6 | const RockSource& source = m_sources[n]; | |
| 93 | 6 | const Raster<double> resolved = | |
| 94 | 6 | source.layer->MaterialCoverOver(grid, LayerOn(source, context)); | |
| 95 | 6 | const std::vector<UnresolvedCover> unresolved = | |
| 96 | 6 | m_options.unresolved ? source.layer->UnresolvedCoverOver( | |
| 97 | 5 | grid, context.spacing, source.ground.get(), context.threads) | |
| 98 | 6 | : std::vector<UnresolvedCover>(bare.size()); | |
| 99 | 6 | ParallelFor(grid.height, context.threads, [&](std::size_t row) { | |
| 100 | 330827 | for (std::size_t col = 0; col < grid.width; ++col) { | |
| 101 | 329881 | const std::size_t cell = row * grid.width + col; | |
| 102 | 329881 | const Vec2d center = first.CellCenter(col, row); | |
| 103 | 659762 | bare[cell] *= | |
| 104 | 329881 | 1.0 - | |
| 105 | 329865 | RockShare(resolved(col, row), unresolved[cell], | |
| 106 | 329881 | m_options.unresolved ? UnresolvedNormal(center.x, center.y, n) : 0.0); | |
| 107 | } | ||
| 108 | 946 | }); | |
| 109 | 6 | } | |
| 110 | 5 | return bare; | |
| 111 | } | ||
| 112 | |||
| 113 | 6 | void RockAlbedo::Apply(std::vector<Raster<float>>& bands, const LayerContext& context) const { | |
| 114 | 6 | if (bands.empty() || m_sources.empty()) { | |
| 115 | 1 | return; | |
| 116 | } | ||
| 117 | 5 | const std::vector<double> bare = BareShares(bands[0], context); | |
| 118 | 5 | const std::size_t width = bands[0].Width(); | |
| 119 | 5 | const std::size_t count = bands.size(); | |
| 120 | ✗ | ForEachAlbedoPixel( | |
| 121 | 5 | bands, context.threads, [&](AlbedoSample& value, std::size_t col, std::size_t row) { | |
| 122 | 328679 | ChangeBands(value, count, | |
| 123 | 328679 | (1.0 - bare[row * width + col]) * (m_options.reflectanceRatio - 1.0)); | |
| 124 | 329192 | }); | |
| 125 | 5 | } | |
| 126 | |||
| 127 | } // namespace terraingen | ||
| 128 |