src/layers/craters/shaping/slope.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/craters/shaping/slope.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/craters/crater_fields.h" | ||
| 6 | #include "terraingen/layers/fade.h" | ||
| 7 | #include "terraingen/random/stream.h" | ||
| 8 | |||
| 9 | #include <cmath> | ||
| 10 | #include <stdexcept> | ||
| 11 | |||
| 12 | namespace terraingen { | ||
| 13 | |||
| 14 | namespace { | ||
| 15 | |||
| 16 | /// The largest floor offset in diameters that keeps the shifted interior from folding. | ||
| 17 | constexpr double kMaxFloorOffset = 0.23; | ||
| 18 | |||
| 19 | } // namespace | ||
| 20 | |||
| 21 | 161424 | void ValidateCraterSlopeResponse(const CraterSlopeResponse& response) { | |
| 22 | 161424 | CheckRanges(response); | |
| 23 | 161701 | ValidateValueDistribution(response.floorOffset); | |
| 24 | 323493 | if (!(response.floorOffset.Lowest() >= 0.0 && | |
| 25 | 161576 | response.floorOffset.Highest() < kMaxFloorOffset)) { | |
| 26 | 2 | throw std::invalid_argument("Crater floor offset must be in [0, 0.23) diameters"); | |
| 27 | } | ||
| 28 | 161742 | } | |
| 29 | |||
| 30 | 1847 | double SlopeWearRate(const CraterSlopeResponse& response, double east, double north) { | |
| 31 | 1847 | if (!response.criticalGradient) { | |
| 32 | return 1.0; | ||
| 33 | } | ||
| 34 | 604 | const double share = std::hypot(east, north) / *response.criticalGradient; | |
| 35 | 604 | const double room = 1.0 - share * share; | |
| 36 | 604 | return room > 1.0 / response.maxWearRate ? 1.0 / room : response.maxWearRate; | |
| 37 | } | ||
| 38 | |||
| 39 | 2371 | CraterSlopeShape SlopeShape(const Crater& crater, double east, double north, | |
| 40 | const CraterSlopeResponse& response) { | ||
| 41 | 2371 | const double gradient = std::hypot(east, north); | |
| 42 | 2371 | if (!response.asymmetric || !(gradient > 0.0)) { | |
| 43 | 113 | return {}; | |
| 44 | } | ||
| 45 | 2258 | const double strength = Smoothstep(std::atan(gradient) / (response.fullSlopeDegrees * kDegree)); | |
| 46 | 2258 | const IdWords id = SplitId(crater.id); | |
| 47 | 2258 | RandomStream random(response.seed.value_or(0u), kCraterSlopeStream, id.low, id.high); | |
| 48 | 2258 | const double offset = strength * response.floorOffset.Draw(random) * crater.diameter; | |
| 49 | 2258 | const Vec2d up{east / gradient, north / gradient}; | |
| 50 | 2258 | const double removed = strength * (1.0 - response.upslopeRim); | |
| 51 | 2258 | return {{-offset * up.x, -offset * up.y}, {removed * up.x, removed * up.y}}; | |
| 52 | } | ||
| 53 | |||
| 54 | } // namespace terraingen | ||
| 55 |