src/layers/exposure.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/exposure.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/fade.h" | ||
| 6 | #include "terraingen/layers/layer.h" | ||
| 7 | #include "terraingen/layers/layer_fields.h" | ||
| 8 | #include "terraingen/noise/perlin.h" | ||
| 9 | #include "terraingen/random/stream.h" | ||
| 10 | #include "terraingen/raster/cell_loops.h" | ||
| 11 | |||
| 12 | #include <algorithm> | ||
| 13 | #include <array> | ||
| 14 | #include <cmath> | ||
| 15 | #include <stdexcept> | ||
| 16 | |||
| 17 | namespace terraingen { | ||
| 18 | |||
| 19 | namespace { | ||
| 20 | |||
| 21 | /// Smoothstep of where `value` lies between `low` and `high`. | ||
| 22 | 781642 | double BetweenSmooth(double value, double low, double high) { | |
| 23 | 781642 | return Smoothstep((value - low) / (high - low)); | |
| 24 | } | ||
| 25 | |||
| 26 | } // namespace | ||
| 27 | |||
| 28 | 27112 | void ValidateExposureParams(const ExposureParams& params) { | |
| 29 | 27112 | CheckRanges(params); | |
| 30 | 27108 | if (!(params.riseBoulder > params.riseSoil)) { | |
| 31 | 1 | throw std::invalid_argument("Exposure riseBoulder must exceed riseSoil"); | |
| 32 | } | ||
| 33 | 27107 | if (!(params.slopeBoulder > params.slopeSoil)) { | |
| 34 | 1 | throw std::invalid_argument("Exposure slopeBoulder must exceed slopeSoil"); | |
| 35 | } | ||
| 36 | 27106 | } | |
| 37 | |||
| 38 | 13561 | ExposureField::ExposureField(const ExposureParams& params, const Surface& surface) | |
| 39 | 13561 | : m_params(params), m_surface(surface) { | |
| 40 | 13561 | ValidateExposureParams(m_params); | |
| 41 | 13556 | constexpr double kDiagonal = 0.70710678118654752; | |
| 42 | 13556 | constexpr std::array<double, 8> kEast{1.0, kDiagonal, 0.0, -kDiagonal, | |
| 43 | -1.0, -kDiagonal, 0.0, kDiagonal}; | ||
| 44 | 13556 | constexpr std::array<double, 8> kNorth{0.0, kDiagonal, 1.0, kDiagonal, | |
| 45 | 0.0, -kDiagonal, -1.0, -kDiagonal}; | ||
| 46 | 122004 | for (std::size_t k = 0; k < m_ring.size(); ++k) { | |
| 47 | 108448 | m_ring[k] = {kEast[k] * m_params.reach, kNorth[k] * m_params.reach}; | |
| 48 | } | ||
| 49 | 13561 | } | |
| 50 | |||
| 51 | 251727 | double ExposureField::Exposure(const Ground& ground, double x, double y, double spacing) const { | |
| 52 | 251727 | return Exposure(ground, m_surface, x, y, spacing); | |
| 53 | } | ||
| 54 | |||
| 55 | 384943 | double ExposureField::Exposure(const Ground& ground, const Surface& surface, double x, double y, | |
| 56 | double spacing) const { | ||
| 57 | 384943 | const float here = ground.HeightAt(x, y, spacing); | |
| 58 | 384981 | if (std::isnan(here)) { | |
| 59 | return 0.0; | ||
| 60 | } | ||
| 61 | 384980 | const double reach = m_params.reach; | |
| 62 | 384980 | std::array<double, 8> heights{}; | |
| 63 | 384980 | GroundAround(ground, surface, {x, y}, m_ring, spacing, static_cast<double>(here), heights); | |
| 64 | 384964 | double mean = 0.0; | |
| 65 | 3463932 | for (const double height : heights) { | |
| 66 | 3078968 | mean += height / 8.0; | |
| 67 | } | ||
| 68 | 384964 | const double rise = (static_cast<double>(here) - mean) / reach; | |
| 69 | 384964 | const double slope = | |
| 70 | 384964 | std::hypot(heights[0] - heights[4], heights[2] - heights[6]) / (2.0 * reach); | |
| 71 | 384910 | double exposure = std::max(BetweenSmooth(rise, m_params.riseSoil, m_params.riseBoulder), | |
| 72 | 384964 | BetweenSmooth(slope, m_params.slopeSoil, m_params.slopeBoulder)); | |
| 73 | 384910 | if (m_params.floorCover > 0.0) { | |
| 74 | 12000 | const double noise = PatchNoise(surface, x, y, m_params.floorPatch, m_params.seed); | |
| 75 | 12000 | const double threshold = 1.0 - 2.0 * m_params.floorCover; | |
| 76 | 17056 | exposure = std::max(exposure, BetweenSmooth(noise, threshold - 0.2, threshold + 0.2)); | |
| 77 | } | ||
| 78 | 384910 | return exposure; | |
| 79 | } | ||
| 80 | |||
| 81 | 4 | void ExposureField::Fill(Raster<float>& raster, const Ground& ground, double spacing, | |
| 82 | unsigned threads) const { | ||
| 83 | 4 | MapCells(raster, threads, [&](float, const Vec2d& center) { | |
| 84 | 131440 | return Exposure(ground, center.x, center.y, spacing); | |
| 85 | }); | ||
| 86 | 4 | } | |
| 87 | |||
| 88 | 352 | LayerExposure::LayerExposure(const std::optional<ExposureParams>& params, double spacing, | |
| 89 | 352 | const char* reader) | |
| 90 | 352 | : m_spacing(spacing), m_reader(reader) { | |
| 91 | 352 | if (params) { | |
| 92 | 19 | m_field.emplace(*params); | |
| 93 | } | ||
| 94 | 352 | } | |
| 95 | |||
| 96 | 339288 | void LayerExposure::Require(const Ground* ground) const { | |
| 97 | 339288 | if (m_field) { | |
| 98 | 3764 | RequireGround(ground, m_reader); | |
| 99 | } | ||
| 100 | 339282 | } | |
| 101 | |||
| 102 | 133218 | double LayerExposure::At(double x, double y, const Ground* ground, const Surface& surface) const { | |
| 103 | 133218 | RequireGround(ground, m_reader); | |
| 104 | 133215 | return m_field->Exposure(*ground, surface, x, y, m_spacing); | |
| 105 | } | ||
| 106 | |||
| 107 | 2525840 | double PatchNoise(const Surface& surface, double x, double y, double size, std::uint32_t seed) { | |
| 108 | 2525840 | const std::uint32_t fineSeed = SubstreamSeed(seed, kPatchFineStream); | |
| 109 | 2525840 | double coarse = 0.0; | |
| 110 | 2525840 | double fine = 0.0; | |
| 111 | 2525840 | if (surface.IsBody()) { | |
| 112 | 2003 | const Vec3d p = surface.Position({x, y}); | |
| 113 | 2003 | const double s = 1.0 / size; | |
| 114 | 2003 | coarse = static_cast<double>(PerlinNoise3Precise(s * p.x, s * p.y, s * p.z, seed)); | |
| 115 | 2003 | fine = static_cast<double>( | |
| 116 | 2003 | PerlinNoise3Precise(4.0 * s * p.x, 4.0 * s * p.y, 4.0 * s * p.z, fineSeed)); | |
| 117 | } else { | ||
| 118 | 2523837 | const auto px = static_cast<float>(x / size); | |
| 119 | 2523837 | const auto py = static_cast<float>(y / size); | |
| 120 | 2523837 | coarse = static_cast<double>(PerlinNoise2(px, py, seed)); | |
| 121 | 2523837 | fine = static_cast<double>(PerlinNoise2(4.0f * px, 4.0f * py, fineSeed)); | |
| 122 | } | ||
| 123 | 2525840 | return std::clamp(2.5 * (0.7 * coarse + 0.3 * fine), -1.0, 1.0); | |
| 124 | } | ||
| 125 | |||
| 126 | } // namespace terraingen | ||
| 127 |