src/layers/pavement.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/pavement.h" | ||
| 4 | |||
| 5 | #include "layers/lattice.h" | ||
| 6 | #include "terraingen/geometry/surface.h" | ||
| 7 | #include "terraingen/layers/anchored_plane.h" | ||
| 8 | #include "terraingen/layers/pavement_fields.h" | ||
| 9 | #include "terraingen/noise/perlin.h" | ||
| 10 | #include "terraingen/random/stream.h" | ||
| 11 | #include "terraingen/util/bits.h" | ||
| 12 | |||
| 13 | #include <algorithm> | ||
| 14 | #include <array> | ||
| 15 | #include <cmath> | ||
| 16 | #include <limits> | ||
| 17 | #include <memory> | ||
| 18 | #include <optional> | ||
| 19 | #include <stdexcept> | ||
| 20 | #include <string> | ||
| 21 | #include <utility> | ||
| 22 | |||
| 23 | namespace terraingen { | ||
| 24 | |||
| 25 | namespace { | ||
| 26 | |||
| 27 | constexpr const char* kExposureReader = "A pavement that reads the exposure field"; | ||
| 28 | /// Plate sizes per lattice unit of the coarse and fine edge warp, and the fine warp's share. | ||
| 29 | constexpr double kWarpScale = 3.0; | ||
| 30 | constexpr double kFineWarpScale = 0.75; | ||
| 31 | constexpr double kFineWarp = 0.35; | ||
| 32 | /// Cells searched each way for the nearest plate sites; enough for any jitter in [0, 1]. | ||
| 33 | constexpr std::int64_t kRings = 2; | ||
| 34 | |||
| 35 | /// One plate: its site and the plane it lies in. | ||
| 36 | struct Site { | ||
| 37 | Vec2d center; | ||
| 38 | double step = 0.0; | ||
| 39 | double slope = 0.0; | ||
| 40 | Vec2d downhill; | ||
| 41 | std::uint32_t id = 0; | ||
| 42 | |||
| 43 | 140770 | double HeightAt(const Vec2d& p) const { | |
| 44 | 140770 | return step + slope * ((p.x - center.x) * downhill.x + (p.y - center.y) * downhill.y); | |
| 45 | } | ||
| 46 | }; | ||
| 47 | |||
| 48 | /// The plate sites of the cells around a point, and the one nearest it. | ||
| 49 | struct Sites { | ||
| 50 | std::array<Site, (2 * kRings + 1) * (2 * kRings + 1)> sites; | ||
| 51 | std::size_t count = 0; | ||
| 52 | std::size_t nearest = 0; | ||
| 53 | }; | ||
| 54 | |||
| 55 | /// The sites of plates `size` across, their sites strayed by `jitter`, within `kRings` cells of | ||
| 56 | /// point `p`. | ||
| 57 | 70379 | Sites PlateSites(const Vec2d& p, double size, double jitter, double maxStep, double maxTilt, | |
| 58 | std::uint32_t seed) { | ||
| 59 | 70379 | Sites around; | |
| 60 | 70379 | double best = std::numeric_limits<double>::infinity(); | |
| 61 | 70379 | ForEachLatticeCell(p, {size, size}, kRings, [&](std::int64_t gx, std::int64_t gy) { | |
| 62 | 1758632 | RandomStream random = LatticeStream(seed, kPavementStream, gx, gy); | |
| 63 | 1758565 | const double u = random.NextDouble(); | |
| 64 | 1758519 | const double v = random.NextDouble(); | |
| 65 | 1758893 | Site& site = around.sites[around.count]; | |
| 66 | 1758893 | site.center = {(static_cast<double>(gx) + 0.5 + jitter * (u - 0.5)) * size, | |
| 67 | 1758893 | (static_cast<double>(gy) + 0.5 + jitter * (v - 0.5)) * size}; | |
| 68 | 1758893 | site.step = maxStep * (2.0 * random.NextDouble() - 1.0); | |
| 69 | 1758780 | site.slope = std::tan(maxTilt * random.NextDouble()); | |
| 70 | 1758808 | const double turn = 2.0 * kPi * random.NextDouble(); | |
| 71 | 1758832 | site.downhill = {std::cos(turn), std::sin(turn)}; | |
| 72 | 1758832 | site.id = random.NextU32(); | |
| 73 | 1758764 | const double d = std::hypot(p.x - site.center.x, p.y - site.center.y); | |
| 74 | 1758764 | if (d < best) { | |
| 75 | 458331 | best = d; | |
| 76 | 458331 | around.nearest = around.count; | |
| 77 | } | ||
| 78 | 1758764 | ++around.count; | |
| 79 | 1758764 | }); | |
| 80 | 70386 | return around; | |
| 81 | } | ||
| 82 | |||
| 83 | /// The distance from `p` to the nearest edge of the plate of the site nearest it, and the site | ||
| 84 | /// across that edge. | ||
| 85 | 70386 | std::pair<double, std::size_t> NearestEdge(const Sites& around, const Vec2d& p) { | |
| 86 | 70386 | const Site& own = around.sites[around.nearest]; | |
| 87 | 70386 | double edge = std::numeric_limits<double>::infinity(); | |
| 88 | 70386 | std::size_t neighbor = around.nearest; | |
| 89 | 1829884 | for (std::size_t k = 0; k < around.count; ++k) { | |
| 90 | 1759498 | if (k == around.nearest) { | |
| 91 | 70386 | continue; | |
| 92 | } | ||
| 93 | 1689112 | const Vec2d center = around.sites[k].center; | |
| 94 | 1689112 | const Vec2d across{center.x - own.center.x, center.y - own.center.y}; | |
| 95 | 1689112 | const double length = std::hypot(across.x, across.y); | |
| 96 | 1689112 | const Vec2d middle{0.5 * (center.x + own.center.x), 0.5 * (center.y + own.center.y)}; | |
| 97 | 1689112 | const double d = | |
| 98 | 1689112 | ((middle.x - p.x) * across.x + (middle.y - p.y) * across.y) / std::max(length, 1e-12); | |
| 99 | 1689112 | if (d < edge) { | |
| 100 | 437498 | edge = d; | |
| 101 | 437498 | neighbor = k; | |
| 102 | } | ||
| 103 | } | ||
| 104 | 70386 | return {edge, neighbor}; | |
| 105 | } | ||
| 106 | |||
| 107 | } // namespace | ||
| 108 | |||
| 109 | 34 | void ValidatePavementOptions(const PavementOptions& options) { | |
| 110 | 34 | CheckRanges(options); | |
| 111 | 21 | if (options.exposure) { | |
| 112 | 2 | ValidateExposureParams(*options.exposure); | |
| 113 | } | ||
| 114 | 21 | ValidateResolutionFade(options.fade); | |
| 115 | 21 | } | |
| 116 | |||
| 117 | 5 | PavementOptions MarsDesiccationRidges() { | |
| 118 | 5 | PavementOptions ridges; | |
| 119 | 5 | ridges.plateSize = 0.02; | |
| 120 | 5 | ridges.jitter = 0.6; | |
| 121 | 5 | ridges.warp = 0.15; | |
| 122 | 5 | ridges.raised = true; | |
| 123 | 5 | ridges.crackDepth = 0.005; | |
| 124 | 5 | ridges.crackWidth = 0.002; | |
| 125 | 5 | ridges.edgeRounding = 0.001; | |
| 126 | 5 | ridges.maxTiltDegrees = 0.0; | |
| 127 | 5 | ridges.maxStep = 0.0; | |
| 128 | 5 | return ridges; | |
| 129 | } | ||
| 130 | |||
| 131 | 34 | PavementLayer::PavementLayer(PavementOptions options) : m_options(options) { | |
| 132 | 34 | ValidatePavementOptions(m_options); | |
| 133 | 21 | m_exposure = LayerExposure(m_options.exposure, m_options.exposureSpacing, kExposureReader); | |
| 134 | 21 | double size = 1.0; | |
| 135 | 21 | double relief = 1.0; | |
| 136 | 53 | for (int k = 0; k < m_options.octaves; ++k) { | |
| 137 | 77 | m_levels.push_back( | |
| 138 | 32 | {m_options.plateSize / size, relief * m_options.crackWidth, | |
| 139 | 32 | relief * m_options.crackDepth, relief * m_options.edgeRounding, | |
| 140 | 32 | relief * (m_options.maxTiltDegrees * kDegree), relief * m_options.maxStep, | |
| 141 | 32 | SubstreamSeed(m_options.seed, kPavementOctaveStream, static_cast<std::uint32_t>(k))}); | |
| 142 | 32 | size *= m_options.lacunarity; | |
| 143 | 32 | relief *= m_options.gain; | |
| 144 | } | ||
| 145 | 47 | } | |
| 146 | |||
| 147 | 3 | LayerPtr PavementLayer::Reseeded(std::uint64_t seed) const { | |
| 148 | 3 | PavementOptions options = m_options; | |
| 149 | 3 | options.seed = ReseedWord(seed, 0); | |
| 150 | 3 | return std::make_shared<PavementLayer>(options); | |
| 151 | } | ||
| 152 | |||
| 153 | 70382 | Vec2d PavementLayer::Warp(const Vec2d& q, double size, std::uint32_t seed) const { | |
| 154 | 351884 | const auto warp = [&](double scale, double ox, double oy, std::uint32_t field) { | |
| 155 | 281502 | return static_cast<double>( | |
| 156 | 281502 | PerlinNoise2Precise(q.x / (scale * size) + ox, q.y / (scale * size) + oy, | |
| 157 | 281502 | SubstreamSeed(seed, kPavementWarpStream, field))); | |
| 158 | 70382 | }; | |
| 159 | 70382 | const double reach = m_options.warp * size; | |
| 160 | 70382 | return {q.x + reach * (warp(kWarpScale, 0.0, 0.0, 0u) + | |
| 161 | 70382 | kFineWarp * warp(kFineWarpScale, 5.3, -8.1, 2u)), | |
| 162 | 70382 | q.y + reach * (warp(kWarpScale, 31.7, -12.3, 1u) + | |
| 163 | 70382 | kFineWarp * warp(kFineWarpScale, -17.9, 4.4, 3u))}; | |
| 164 | } | ||
| 165 | |||
| 166 | 70381 | PavementLayer::Plate PavementLayer::PlateAt(const Vec2d& q, const Level& level, std::uint32_t seed, | |
| 167 | double crackWeight) const { | ||
| 168 | 70381 | const Vec2d p = Warp(q, level.size, seed); | |
| 169 | 70381 | const Sites around = | |
| 170 | 70381 | PlateSites(p, level.size, m_options.jitter, level.maxStep, level.maxTilt, seed); | |
| 171 | 70386 | const Site& own = around.sites[around.nearest]; | |
| 172 | 70386 | const auto [edge, neighbor] = NearestEdge(around, p); | |
| 173 | 70386 | const double shoulder = 0.5 * level.crackWidth + level.rounding; | |
| 174 | 70386 | const double crack = 1.0 - Smoothstep(edge / shoulder); | |
| 175 | 70385 | const double ownHeight = own.HeightAt(p); | |
| 176 | 70385 | const double plate = ownHeight + 0.5 * crack * (around.sites[neighbor].HeightAt(p) - ownHeight); | |
| 177 | 70385 | const double sign = m_options.raised ? 1.0 : -1.0; | |
| 178 | 70385 | return {plate + crackWeight * sign * level.crackDepth * crack, 1.0 - crack, own.id}; | |
| 179 | } | ||
| 180 | |||
| 181 | 30935 | double PavementLayer::Relief(const Vec2d& q, double spacing) const { | |
| 182 | 30935 | double relief = 0.0; | |
| 183 | 30935 | double interior = 1.0; | |
| 184 | 30935 | std::uint32_t parent = 0; | |
| 185 | 101320 | for (const Level& level : m_levels) { | |
| 186 | 70402 | const double plateWeight = ResolutionWeight(level.size, spacing, m_options.fade); | |
| 187 | 70399 | if (plateWeight <= 0.0 || interior <= 0.0) { | |
| 188 | break; | ||
| 189 | } | ||
| 190 | 70382 | const double crackWeight = | |
| 191 | 70382 | ResolutionWeight(level.crackWidth + 2.0 * level.rounding, spacing, m_options.fade); | |
| 192 | 70380 | const Plate plate = PlateAt( | |
| 193 | 70380 | q, level, SubstreamSeed(level.seed, kPavementParentStream, parent), crackWeight); | |
| 194 | 70385 | relief += interior * plateWeight * plate.relief; | |
| 195 | 70385 | interior *= plate.interior; | |
| 196 | 70385 | parent = plate.id; | |
| 197 | } | ||
| 198 | 30935 | return relief; | |
| 199 | } | ||
| 200 | |||
| 201 | 32428 | double PavementLayer::ExposureWeight(double x, double y, const LayerContext& context) const { | |
| 202 | 32428 | return m_exposure ? m_exposure.At(x, y, context.ground, context.SurfaceOrPlane()) : 1.0; | |
| 203 | } | ||
| 204 | |||
| 205 | 6 | double PavementLayer::MaterialCoverAt(double x, double y, const LayerContext& context) const { | |
| 206 | 6 | const AnchoredPlane plane(context.SurfaceOrPlane(), m_options.anchor); | |
| 207 | 6 | return plane.Project(x, y) ? ExposureWeight(x, y, context) : 0.0; | |
| 208 | } | ||
| 209 | |||
| 210 | 32426 | float PavementLayer::HeightAt(float height, double x, double y, const LayerContext& context) const { | |
| 211 | 32426 | const std::optional<Vec2d> q = | |
| 212 | 32426 | AnchoredPlane(context.SurfaceOrPlane(), m_options.anchor).Project(x, y); | |
| 213 | 32426 | if (!q || ResolutionWeight(m_options.plateSize, context.spacing, m_options.fade) <= 0.0) { | |
| 214 | 3 | return height; | |
| 215 | } | ||
| 216 | 32423 | const double exposure = ExposureWeight(x, y, context); | |
| 217 | 32423 | if (exposure <= 0.0) { | |
| 218 | return height; | ||
| 219 | } | ||
| 220 | 30935 | return static_cast<float>(static_cast<double>(height) + exposure * Relief(*q, context.spacing)); | |
| 221 | } | ||
| 222 | |||
| 223 | 28948 | void PavementLayer::CheckContext(const LayerContext& context) const { | |
| 224 | 28948 | m_exposure.Require(context.ground); | |
| 225 | 28947 | } | |
| 226 | |||
| 227 | } // namespace terraingen | ||
| 228 |