src/layers/bedforms.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/bedforms.h" | ||
| 4 | |||
| 5 | #include "layers/lattice.h" | ||
| 6 | #include "terraingen/geometry/geometry.h" | ||
| 7 | #include "terraingen/layers/bedforms_fields.h" | ||
| 8 | #include "terraingen/layers/source_resolved.h" | ||
| 9 | #include "terraingen/noise/perlin.h" | ||
| 10 | #include "terraingen/random/stream.h" | ||
| 11 | #include "terraingen/raster/cell_loops.h" | ||
| 12 | #include "terraingen/raster/grid.h" | ||
| 13 | |||
| 14 | #include <algorithm> | ||
| 15 | #include <cmath> | ||
| 16 | #include <memory> | ||
| 17 | #include <stdexcept> | ||
| 18 | |||
| 19 | namespace terraingen { | ||
| 20 | |||
| 21 | namespace { | ||
| 22 | |||
| 23 | constexpr double kRadiusMin = 0.40; | ||
| 24 | constexpr double kRadiusMax = 0.70; | ||
| 25 | constexpr double kRagged = 0.30; | ||
| 26 | constexpr double kEdgeShare = 0.35; | ||
| 27 | constexpr double kWavelengthMin = 0.8; | ||
| 28 | constexpr double kWavelengthSpan = 0.4; | ||
| 29 | constexpr double kWanderWavelengths = 5.0; | ||
| 30 | constexpr double kRagRadii = 0.5; | ||
| 31 | |||
| 32 | constexpr const char* kExposureReader = "Bedforms that read the exposure field"; | ||
| 33 | |||
| 34 | } // namespace | ||
| 35 | |||
| 36 | 37 | void ValidateBedformOptions(const BedformOptions& options) { | |
| 37 | 37 | CheckRanges(options); | |
| 38 | 30 | if (options.exposure) { | |
| 39 | 4 | ValidateExposureParams(*options.exposure); | |
| 40 | } | ||
| 41 | 30 | ValidateResolutionFade(options.fade); | |
| 42 | 29 | } | |
| 43 | |||
| 44 | 7 | BedformOptions MarsLargeRipples() { | |
| 45 | 7 | BedformOptions ripples; | |
| 46 | 7 | ripples.wavelength = 2.0; | |
| 47 | 7 | ripples.height = 0.2; | |
| 48 | 7 | const double leeSlope = std::tan(30.0 * kDegree); | |
| 49 | 7 | ripples.leeFraction = kPi * ripples.height / (2.0 * ripples.wavelength * leeSlope); | |
| 50 | 7 | ripples.sinuosity = 0.6; | |
| 51 | 7 | ripples.cover = 0.15; | |
| 52 | 7 | ripples.fieldSize = 80.0; | |
| 53 | 7 | return ripples; | |
| 54 | } | ||
| 55 | |||
| 56 | 4 | BedformOptions MarsTransverseAeolianRidges() { | |
| 57 | 4 | BedformOptions ridges; | |
| 58 | 4 | ridges.wavelength = 25.8; | |
| 59 | 4 | ridges.height = 1.3; | |
| 60 | 4 | ridges.leeFraction = 0.5; | |
| 61 | 4 | ridges.sinuosity = 0.2; | |
| 62 | 4 | ridges.cover = 0.07; | |
| 63 | 4 | ridges.fieldSize = 250.0; | |
| 64 | 4 | return ridges; | |
| 65 | } | ||
| 66 | |||
| 67 | 36 | BedformLayer::BedformLayer(BedformOptions options) : m_options(options) { | |
| 68 | 36 | ValidateBedformOptions(m_options); | |
| 69 | 28 | m_cell = m_options.fieldSize / (kRadiusMin + kRadiusMax); | |
| 70 | 28 | const double meanArea = kPi * (std::pow(kRadiusMax, 3.0) - std::pow(kRadiusMin, 3.0)) / | |
| 71 | (3.0 * (kRadiusMax - kRadiusMin)); | ||
| 72 | 28 | m_density = std::min(1.0, -std::log1p(-m_options.cover) / meanArea); | |
| 73 | 28 | m_wanderSeed = SubstreamSeed(m_options.seed, kBedformWanderStream); | |
| 74 | 28 | m_exposure = LayerExposure(m_options.exposure, m_options.exposureSpacing, kExposureReader); | |
| 75 | 36 | } | |
| 76 | |||
| 77 | 3 | LayerPtr BedformLayer::Reseeded(std::uint64_t seed) const { | |
| 78 | 3 | BedformOptions options = m_options; | |
| 79 | 3 | options.seed = ReseedWord(seed, 0); | |
| 80 | 3 | return std::make_shared<BedformLayer>(options); | |
| 81 | } | ||
| 82 | |||
| 83 | 743790 | void BedformLayer::AddField(FieldSum& sum, RandomStream& random, const Vec2d& center, | |
| 84 | const Vec2d& offset, const LayerContext& context) const { | ||
| 85 | 743790 | const double barren = random.NextDouble(); | |
| 86 | 743734 | const auto ox = static_cast<float>(97.0 * random.NextDouble()); | |
| 87 | 743772 | const auto oy = static_cast<float>(97.0 * random.NextDouble()); | |
| 88 | 743754 | const double radius = (kRadiusMin + (kRadiusMax - kRadiusMin) * random.NextDouble()) * m_cell; | |
| 89 | 743775 | const double scale = kWavelengthMin + kWavelengthSpan * random.NextDouble(); | |
| 90 | 743763 | const double azimuth = | |
| 91 | 743763 | (m_options.windAzimuthDegrees * kDegree) + | |
| 92 | 743763 | (m_options.azimuthSpreadDegrees * kDegree) * (2.0 * random.NextDouble() - 1.0); | |
| 93 | 743779 | const double phase = random.NextDouble(); | |
| 94 | 743750 | const double dx = offset.x; | |
| 95 | 743750 | const double dy = offset.y; | |
| 96 | 743750 | const double rag = static_cast<double>( | |
| 97 | 743750 | PerlinNoise2(static_cast<float>(dx / (kRagRadii * radius)) + ox, | |
| 98 | 743750 | static_cast<float>(dy / (kRagRadii * radius)) + oy, m_options.seed)); | |
| 99 | 743750 | const double inside = 1.0 - std::hypot(dx, dy) / (radius * (1.0 + kRagged * rag)); | |
| 100 | 743750 | if (inside <= 0.0) { | |
| 101 | return; | ||
| 102 | } | ||
| 103 | 2010 | if (m_exposure && | |
| 104 | 80765 | barren < m_exposure.At(center.x, center.y, context.ground, context.SurfaceOrPlane())) { | |
| 105 | return; | ||
| 106 | } | ||
| 107 | 79766 | const double cover = Smoothstep(inside / kEdgeShare); | |
| 108 | 79769 | const double wavelength = m_options.wavelength * scale; | |
| 109 | 79769 | const double wander = static_cast<double>(PerlinNoise2( | |
| 110 | 79769 | static_cast<float>(dx / (kWanderWavelengths * wavelength)) + oy, | |
| 111 | 79769 | static_cast<float>(dy / (kWanderWavelengths * wavelength)) + ox, m_wanderSeed)); | |
| 112 | 79769 | const double along = (dx * std::cos(azimuth) + dy * std::sin(azimuth)) / wavelength + | |
| 113 | 79769 | m_options.sinuosity * wander + phase; | |
| 114 | 79769 | const double t = along - std::floor(along); | |
| 115 | 79769 | const double upwind = 1.0 - m_options.leeFraction; | |
| 116 | 159538 | const double profile = t < upwind | |
| 117 | 79769 | ? 0.5 * (1.0 - std::cos(kPi * t / upwind)) | |
| 118 | 26341 | : 0.5 * (1.0 + std::cos(kPi * (t - upwind) / m_options.leeFraction)); | |
| 119 | 79769 | sum.cover += cover; | |
| 120 | 79769 | sum.relief += cover * m_options.height * scale * profile; | |
| 121 | } | ||
| 122 | |||
| 123 | 2 | double BedformLayer::GroundReach(double /*spacing*/) const { | |
| 124 | 2 | return m_exposure ? kRadiusMax * (1.0 + kRagged) * m_cell + m_exposure.Reach() : 0.0; | |
| 125 | } | ||
| 126 | |||
| 127 | 190288 | BedformLayer::FieldSum BedformLayer::SumFields(double x, double y, | |
| 128 | const LayerContext& context) const { | ||
| 129 | 190288 | FieldSum sum; | |
| 130 | 190288 | if (m_density <= 0.0) { | |
| 131 | 40001 | return sum; | |
| 132 | } | ||
| 133 | 150287 | m_exposure.Require(context.ground); | |
| 134 | 150286 | const Surface& surface = context.SurfaceOrPlane(); | |
| 135 | 150286 | const std::shared_ptr<const SurfaceCells> cells = ThreadCells(surface); | |
| 136 | 150284 | const double reach = kRadiusMax * (1.0 + kRagged) * m_cell; | |
| 137 | 1532018 | for (const CellIndex& cell : | |
| 138 | 1381733 | cells->CellsCovering(surface.Expand({x, y, x, y}, reach), m_cell)) { | |
| 139 | 1231453 | RandomStream random = LatticeStream(m_options.seed, kBedformStream, cell.i, cell.j); | |
| 140 | 1231154 | const double chance = m_density * cells->CellArea(cell, m_cell) / (m_cell * m_cell); | |
| 141 | 1231333 | if (random.NextDouble() >= chance) { | |
| 142 | 487717 | continue; | |
| 143 | } | ||
| 144 | 743786 | const double u = random.NextDouble(); | |
| 145 | 743774 | const double v = random.NextDouble(); | |
| 146 | 743773 | const Vec2d center = cells->SamplePoint(u, v, cell, m_cell); | |
| 147 | 743784 | AddField(sum, random, center, surface.Offset(center, {x, y}), context); | |
| 148 | 150281 | } | |
| 149 | 150284 | return sum; | |
| 150 | 150286 | } | |
| 151 | |||
| 152 | 132923 | double BedformLayer::MaterialCoverAt(double x, double y, const LayerContext& context) const { | |
| 153 | 132923 | return std::min(1.0, SumFields(x, y, context).cover); | |
| 154 | } | ||
| 155 | |||
| 156 | 57571 | float BedformLayer::HeightAt(float height, double x, double y, const LayerContext& context) const { | |
| 157 | 57571 | const double resolved = SourceResolvedSize(context.source, m_options.sourceResolvesPixels, | |
| 158 | context.SurfaceOrPlane(), x, y); | ||
| 159 | 57572 | const double weight = | |
| 160 | 57572 | m_options.wavelength >= resolved | |
| 161 | 57572 | ? 0.0 | |
| 162 | 57415 | : ResolutionWeight(m_options.wavelength, context.spacing, m_options.fade); | |
| 163 | 57415 | if (weight <= 0.0) { | |
| 164 | 207 | return height; | |
| 165 | } | ||
| 166 | 57365 | const FieldSum sum = SumFields(x, y, context); | |
| 167 | 57366 | if (sum.cover <= 0.0) { | |
| 168 | return height; | ||
| 169 | } | ||
| 170 | 51624 | return static_cast<float>(static_cast<double>(height) + | |
| 171 | 27428 | weight * sum.relief / std::max(1.0, sum.cover)); | |
| 172 | } | ||
| 173 | |||
| 174 | 20975 | void BedformLayer::CheckContext(const LayerContext& context) const { | |
| 175 | 20975 | m_exposure.Require(context.ground); | |
| 176 | 20974 | } | |
| 177 | |||
| 178 | } // namespace terraingen | ||
| 179 |