src/layers/craters/shaping/crater_kernel.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "layers/craters/shaping/crater_kernel.h" | ||
| 4 | |||
| 5 | #include "terraingen/geometry/geometry.h" | ||
| 6 | #include "terraingen/layers/craters/degradation/diffusion.h" | ||
| 7 | #include "terraingen/layers/craters/profiles/complex.h" | ||
| 8 | #include "terraingen/layers/craters/profiles/fassett_thomson.h" | ||
| 9 | #include "terraingen/layers/craters/profiles/martin_parkes_dunstan.h" | ||
| 10 | #include "terraingen/layers/craters/shaping/apply.h" | ||
| 11 | #include "terraingen/layers/fade.h" | ||
| 12 | #include "terraingen/random/stream.h" | ||
| 13 | #include "terraingen/random/streams.h" | ||
| 14 | #include "util/numerics.h" | ||
| 15 | |||
| 16 | #include <algorithm> | ||
| 17 | #include <array> | ||
| 18 | #include <cmath> | ||
| 19 | #include <map> | ||
| 20 | #include <stdexcept> | ||
| 21 | |||
| 22 | namespace terraingen { | ||
| 23 | |||
| 24 | namespace { | ||
| 25 | |||
| 26 | /// Share of the wall the benches cover from the full terrace diameter: even fully terraced | ||
| 27 | /// craters have terraces of limited length, up to about half the rim in craters 20-130 km across | ||
| 28 | /// (Du et al. 2024, LPSC 55, abstract 1382). | ||
| 29 | constexpr double kFullTerraceCover = 0.9; | ||
| 30 | |||
| 31 | /// Width in harmonic units over which a bench's arc fades in and out, wide enough that a bench | ||
| 32 | /// tapers into the unstepped wall over tens of degrees rather than ending in a radial cliff. | ||
| 33 | constexpr double kTerraceArcWidth = 0.3; | ||
| 34 | |||
| 35 | /// Samples of a crater's wall height across the wall, for splitting it into steps by height. | ||
| 36 | constexpr int kTerraceWallSamples = 256; | ||
| 37 | |||
| 38 | /// Narrowest step across the wall, as a share of an even step's width. | ||
| 39 | constexpr double kNarrowestTerraceStep = 0.25; | ||
| 40 | |||
| 41 | /// Share of a half-scarp over which its slope rounds into its brink or toe (`KernelTerraceEase`). | ||
| 42 | constexpr double kTerraceScarpRounding = 0.4; | ||
| 43 | |||
| 44 | /// Most share of a step its scarps take, so even steep steps keep a narrow bench. | ||
| 45 | constexpr double kMostTerraceScarpShare = 0.9; | ||
| 46 | |||
| 47 | /// Share of a step its scarps would need to stay at the scarp slope from which its bench fades, | ||
| 48 | /// gone where the step is as steep as the scarps: walls too steep for a bench keep their slope. | ||
| 49 | constexpr double kSteepTerraceStep = 0.7; | ||
| 50 | |||
| 51 | /// Headroom on the scarp slope for scallops and block offsets, which narrow some steps. | ||
| 52 | constexpr double kTerraceScarpSqueeze = 0.75; | ||
| 53 | |||
| 54 | /// Samples of a step for its mean height, which its bench level keeps. | ||
| 55 | constexpr int kTerraceMeanSamples = 64; | ||
| 56 | |||
| 57 | /// Bisection steps for the bench level that keeps a step's mean height. | ||
| 58 | constexpr int kTerraceLevelIterations = 30; | ||
| 59 | |||
| 60 | /// Nearest a bench's level comes to the bottom or top of its step, as a share of the step. | ||
| 61 | constexpr double kTerraceLevelMargin = 0.1; | ||
| 62 | |||
| 63 | /// A scarp's room, as a share of the narrower of its two steps. | ||
| 64 | constexpr double kTerraceScarpRoom = 0.4; | ||
| 65 | |||
| 66 | /// Most a scarp may move, scallops, wobble and block offset together, in units of its room: with | ||
| 67 | /// the room 0.4 of the narrower step, two neighboring scarps stay 4% of a step apart. | ||
| 68 | constexpr double kTerraceScarpReach = 1.2; | ||
| 69 | |||
| 70 | /// Fewest and most scallops a scarp has around the rim, as slump blocks scallop the walls of | ||
| 71 | /// fresh complex craters. | ||
| 72 | constexpr double kMinTerraceScallops = 6.0; | ||
| 73 | constexpr double kMaxTerraceScallops = 12.0; | ||
| 74 | |||
| 75 | /// Height of the rim crest's scallops, in radii, per unit of `CraterTerraces::rimVariation`. | ||
| 76 | constexpr double kTerraceRimScallop = 0.5; | ||
| 77 | |||
| 78 | /// How far a central peak's massifs may reach before warping, in peak base radii; the kernel | ||
| 79 | /// reads them out to 2.6. | ||
| 80 | constexpr double kPeakRoom = 2.3; | ||
| 81 | |||
| 82 | /// Radii of the main and of a typical lesser massif of a two-massif peak, in peak base radii; | ||
| 83 | /// with more massifs they shrink with the square root of their number, so a cluster of | ||
| 84 | /// separate mountains covers about the peak's base. | ||
| 85 | constexpr double kMainMassifRadius = 1.0; | ||
| 86 | constexpr double kLesserMassifRadius = 0.6; | ||
| 87 | |||
| 88 | /// Furthest a massif's summit sits off its center, in units of its footprint, at full | ||
| 89 | /// lopsidedness. | ||
| 90 | constexpr double kMaxSummitPull = 0.45; | ||
| 91 | |||
| 92 | /// Whether the simple profile shapes `crater` in `state`: not where the table or the complex | ||
| 93 | /// form does. | ||
| 94 | 161349 | bool ShapesSimply(const CraterShapeKernel& kernel, double diameter, const CraterState& state) { | |
| 95 | 161349 | const CraterProfileKernel& p = kernel.profile; | |
| 96 | 161349 | if (p.diffused == 1u && state.diffusion > 0.0) { | |
| 97 | return false; | ||
| 98 | } | ||
| 99 | 120635 | return !(p.complex == 1u && diameter >= static_cast<double>(p.transitionDiameter)); | |
| 100 | } | ||
| 101 | |||
| 102 | /// `profile` as the kernel evaluates it in closed form, or nothing for a profile it does not | ||
| 103 | /// know, which it tabulates. | ||
| 104 | 239135 | std::optional<CraterProfileKernel> ClosedFormProfile(const CraterProfile& profile) { | |
| 105 | 239135 | CraterProfileKernel p; | |
| 106 | 239135 | const CraterProfile* fresh = &profile; | |
| 107 | 239135 | if (const auto* diffused = dynamic_cast<const DiffusedProfile*>(&profile)) { | |
| 108 | 36503 | p = diffused->Kernel(); | |
| 109 | 36503 | fresh = &diffused->Fresh(); | |
| 110 | } | ||
| 111 | 239135 | if (const auto* complex = dynamic_cast<const ComplexCraterProfile*>(fresh)) { | |
| 112 | 31215 | SetKernelComplexShape(p, complex->Shape()); | |
| 113 | 31215 | fresh = &complex->Simple(); | |
| 114 | } | ||
| 115 | 239135 | if (const auto* ft = dynamic_cast<const FassettThomsonProfile*>(fresh)) { | |
| 116 | 108855 | p.simple = ft->Kernel().simple; | |
| 117 | 108855 | p.rim = ft->Kernel().rim; | |
| 118 | 130280 | } else if (const auto* mpd = dynamic_cast<const MartinParkesDunstanProfile*>(fresh)) { | |
| 119 | 79838 | p.simple = mpd->Kernel().simple; | |
| 120 | } else { | ||
| 121 | 50442 | return std::nullopt; | |
| 122 | } | ||
| 123 | 188693 | return p; | |
| 124 | } | ||
| 125 | |||
| 126 | /// `profile` tabulated per crater (`CraterProfileKind::Tabulated`). | ||
| 127 | 239061 | CraterProfileKernel TabulatedProfile(const CraterProfile& profile) { | |
| 128 | 239061 | CraterProfileKernel p; | |
| 129 | 239061 | p.simple = CraterProfileKind::Tabulated; | |
| 130 | 239061 | p.tableExtent = static_cast<float>(0.5 * profile.ReachRadii()); | |
| 131 | 239100 | p.tableCells = kProfileTableCells; | |
| 132 | 239100 | return p; | |
| 133 | } | ||
| 134 | |||
| 135 | /// Sets how `options` shape craters on `k`: reach, blend, rim smoothing, noise and texture. | ||
| 136 | 239086 | void SetShapeOptions(CraterShapeKernel& k, const CraterProfile& profile, | |
| 137 | const CraterApplyOptions& options) { | ||
| 138 | 239086 | k.reachRadii = static_cast<float>(CraterReachRadii(profile, options)); | |
| 139 | 239079 | k.profileReachRadii = static_cast<float>(profile.ReachRadii()); | |
| 140 | 239083 | k.blendStart = static_cast<float>(options.blend.startRadii); | |
| 141 | 239083 | k.blendEnd = static_cast<float>(options.blend.endRadii); | |
| 142 | 239083 | k.blendShape = static_cast<std::uint32_t>(options.blend.shape); | |
| 143 | 239083 | k.interiorReplacement = static_cast<float>(options.interiorReplacement); | |
| 144 | 239083 | k.replaceStart = static_cast<float>(options.replacement.startRadii); | |
| 145 | 239083 | k.replaceEnd = static_cast<float>(options.replacement.EndRadii(profile.ReachRadii())); | |
| 146 | 239076 | k.mantling = static_cast<float>(options.texture.mantling); | |
| 147 | 239076 | k.ejectaReachRadii = static_cast<float>(options.texture.ejectaReachRadii); | |
| 148 | 239076 | const RimSmoothing& rim = options.rimSmoothing; | |
| 149 | 239076 | k.rimSigma = static_cast<float>(rim.sigmaRadii); | |
| 150 | 239076 | k.rimInner = static_cast<float>(rim.innerRadii); | |
| 151 | 239076 | k.rimOuter = static_cast<float>(rim.outerRadii); | |
| 152 | 239076 | k.rimStrength = static_cast<float>(rim.strength); | |
| 153 | 239076 | k.rimShape = static_cast<std::uint32_t>(rim.shape); | |
| 154 | 239076 | const CraterNoise& noise = options.noise; | |
| 155 | 239076 | k.noiseAmplitude = static_cast<float>(noise.amplitude); | |
| 156 | 239076 | k.noiseWavelength = static_cast<float>(noise.wavelengthRadii); | |
| 157 | 239076 | k.noiseOctaves = noise.octaves; | |
| 158 | 239076 | k.noiseGain = static_cast<float>(noise.gain); | |
| 159 | 239076 | k.noiseRimAmplitude = static_cast<float>(noise.rimAmplitude); | |
| 160 | 239076 | k.noiseRimWavelength = static_cast<float>(noise.rimWavelengthRadii); | |
| 161 | 239076 | k.noiseRimInner = static_cast<float>(noise.rimInnerRadii); | |
| 162 | 239076 | k.noiseRimOuter = static_cast<float>(noise.rimOuterRadii); | |
| 163 | 239076 | const CraterTexture& texture = options.texture; | |
| 164 | 239076 | k.wallStreaks = static_cast<float>(texture.wallStreaks); | |
| 165 | 239076 | k.wallStreakWidth = static_cast<float>(texture.wallStreakWidthRadii); | |
| 166 | 239076 | k.ejecta = static_cast<float>(texture.ejecta); | |
| 167 | 239076 | k.ejectaLineation = static_cast<float>(texture.ejectaLineationRadii); | |
| 168 | 239076 | k.minFreshness = static_cast<float>(texture.minFreshness); | |
| 169 | 239076 | } | |
| 170 | |||
| 171 | /// Checks the state a Martin, Parkes and Dunstan profile shapes a crater in. | ||
| 172 | /// @throws std::invalid_argument for a state the profile rejects. | ||
| 173 | 343588 | void CheckProfileState(const CraterShapeKernel& kernel, double diameter, const CraterState& state) { | |
| 174 | 343588 | if (kernel.profile.simple != CraterProfileKind::MartinParkesDunstan || | |
| 175 | 161366 | !ShapesSimply(kernel, diameter, state)) { | |
| 176 | return; | ||
| 177 | } | ||
| 178 | 120639 | const CraterMorphometry& shaped = kernel.profile.diffused == 1u ? state.fresh : state.current; | |
| 179 | 120639 | if (!(shaped.depthRatio > 0.0 && shaped.rimHeightRatio >= 0.0 && | |
| 180 | 120639 | shaped.rimHeightRatio <= state.fresh.rimHeightRatio)) { | |
| 181 | 1 | throw std::invalid_argument("Crater state needs a positive depth and 0 <= rim " | |
| 182 | 1 | "height <= fresh rim height"); | |
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | /// The record of prepared crater `p`, without its outline and profile tables. | ||
| 187 | 343603 | CraterRecord BaseRecord(const PreparedCrater& p, const CraterApplyOptions& options) { | |
| 188 | 343603 | const CraterState& state = p.state; | |
| 189 | 343603 | CraterRecord r = KernelStateRecord(p.crater.diameter, state); | |
| 190 | 343603 | r.resolution = static_cast<float>(p.resolution); | |
| 191 | 343603 | if (p.datum) { | |
| 192 | 109293 | r.hasDatum = 1u; | |
| 193 | 109293 | r.datumA = static_cast<float>(p.datum->a); | |
| 194 | 109293 | r.datumB = static_cast<float>(p.datum->b); | |
| 195 | 109293 | r.datumC = static_cast<float>(p.datum->c); | |
| 196 | } | ||
| 197 | 343603 | r.replace = p.crater.replace ? 1u : 0u; | |
| 198 | 343603 | r.shiftX = static_cast<float>(p.slope.shift.x); | |
| 199 | 343603 | r.shiftY = static_cast<float>(p.slope.shift.y); | |
| 200 | 343603 | r.upslopeX = static_cast<float>(p.slope.upslope.x); | |
| 201 | 343603 | r.upslopeY = static_cast<float>(p.slope.upslope.y); | |
| 202 | 507888 | r.noiseKey = CraterKey(p.crater.id, options.noise.seed.value_or(0u)); | |
| 203 | 687206 | r.textureKey = CraterKey( | |
| 204 | 343603 | p.crater.id, SubstreamSeed(options.texture.seed.value_or(0u), kCraterTextureStream)); | |
| 205 | 343603 | return r; | |
| 206 | } | ||
| 207 | |||
| 208 | /// The profile tables of a batch, each appended to its data once. | ||
| 209 | class ProfileTables { | ||
| 210 | public: | ||
| 211 | 239474 | ProfileTables(const CraterShapeKernel& kernel, const CraterProfile& profile) | |
| 212 | 239474 | : m_kernel(kernel), m_profile(profile), | |
| 213 | 239474 | m_diffused(dynamic_cast<const DiffusedProfile*>(&profile)) {} | |
| 214 | |||
| 215 | /// Appends the ages of a diffused profile's tables to `data`, which must be empty. | ||
| 216 | 239505 | void AppendAges(std::vector<float>& data) const { | |
| 217 | 239505 | if (m_kernel.profile.diffused == 1u && m_diffused != nullptr) { | |
| 218 | 5868235 | for (const double age : m_diffused->Ages()) { | |
| 219 | 5832011 | data.push_back(static_cast<float>(age)); | |
| 220 | } | ||
| 221 | } | ||
| 222 | 239505 | } | |
| 223 | |||
| 224 | /// Points `r` at the table crater `p` is shaped from, appending it to `data` when new. | ||
| 225 | 343578 | void Append(const PreparedCrater& p, CraterRecord& r, std::vector<float>& data) { | |
| 226 | 343578 | const CraterState& state = p.state; | |
| 227 | 343578 | const double diameter = p.crater.diameter; | |
| 228 | 343578 | if (m_kernel.profile.simple == CraterProfileKind::Tabulated) { | |
| 229 | 50732 | const std::array<double, 6> key{diameter, | |
| 230 | 50732 | state.fresh.depthRatio, | |
| 231 | 50732 | state.fresh.rimHeightRatio, | |
| 232 | 50732 | state.current.depthRatio, | |
| 233 | 50732 | state.current.rimHeightRatio, | |
| 234 | 50732 | state.diffusion}; | |
| 235 | 101464 | const auto [at, added] = | |
| 236 | 50732 | m_tabulated.try_emplace(key, static_cast<std::uint32_t>(data.size())); | |
| 237 | 50732 | if (added) { | |
| 238 | 50731 | AppendRow(diameter, state, data); | |
| 239 | } | ||
| 240 | 50732 | r.profileOffset = at->second; | |
| 241 | 50732 | return; | |
| 242 | } | ||
| 243 | 292846 | if (m_kernel.profile.diffused == 1u && m_diffused != nullptr && state.diffusion > 0.0) { | |
| 244 | 71038 | const std::shared_ptr<const std::vector<float>> table = | |
| 245 | 71038 | m_diffused->HeightTable(state.fresh, diameter); | |
| 246 | 142083 | const auto [at, added] = | |
| 247 | 71041 | diffused_tables_.try_emplace(table.get(), static_cast<std::uint32_t>(data.size())); | |
| 248 | 71042 | if (added) { | |
| 249 | 30177 | data.insert(data.end(), table->begin(), table->end()); | |
| 250 | } | ||
| 251 | 71040 | r.profileOffset = at->second; | |
| 252 | 71045 | } | |
| 253 | } | ||
| 254 | |||
| 255 | private: | ||
| 256 | /// Appends the profile's heights over the diameter along one row for a crater of `diameter` | ||
| 257 | /// meters in `state`. | ||
| 258 | 50731 | void AppendRow(double diameter, const CraterState& state, std::vector<float>& data) const { | |
| 259 | 50731 | const double extent = 0.5 * m_profile.ReachRadii(); | |
| 260 | 50731 | const double step = extent / static_cast<double>(m_kernel.profile.tableCells); | |
| 261 | 26075734 | for (std::uint32_t i = 0; i <= m_kernel.profile.tableCells; ++i) { | |
| 262 | 26025003 | const double distance = static_cast<double>(i) * step * diameter; | |
| 263 | 52050006 | data.push_back( | |
| 264 | 26025003 | static_cast<float>(m_profile.Height(distance, diameter, state) / diameter)); | |
| 265 | } | ||
| 266 | 50731 | } | |
| 267 | |||
| 268 | const CraterShapeKernel& m_kernel; | ||
| 269 | const CraterProfile& m_profile; | ||
| 270 | const DiffusedProfile* m_diffused; | ||
| 271 | std::map<const std::vector<float>*, std::uint32_t> diffused_tables_; | ||
| 272 | std::map<std::array<double, 6>, std::uint32_t> m_tabulated; | ||
| 273 | }; | ||
| 274 | |||
| 275 | } // namespace | ||
| 276 | |||
| 277 | 238832 | CraterShapeKernel MakeCraterShapeKernel(const CraterProfile& profile, | |
| 278 | const CraterApplyOptions& options) { | ||
| 279 | 238832 | CraterShapeKernel k; | |
| 280 | 238832 | k.profile = ClosedFormProfile(profile).value_or(TabulatedProfile(profile)); | |
| 281 | 239079 | SetShapeOptions(k, profile, options); | |
| 282 | 239046 | return k; | |
| 283 | } | ||
| 284 | |||
| 285 | 81909 | CraterFrame MakeCraterFrame(const Surface& surface, const Crater& crater) { | |
| 286 | 81909 | const Vec2d center{crater.x, crater.y}; | |
| 287 | 81909 | const Vec3d position = surface.Position(center); | |
| 288 | 81907 | const LocalFrame frame = surface.Frame(center); | |
| 289 | 81913 | CraterFrame f; | |
| 290 | 81913 | SplitCoordinate(position.x, f.x, f.xLow); | |
| 291 | 81913 | SplitCoordinate(position.y, f.y, f.yLow); | |
| 292 | 81913 | SplitCoordinate(position.z, f.z, f.zLow); | |
| 293 | 81913 | f.eastX = static_cast<float>(frame.east.x); | |
| 294 | 81913 | f.eastY = static_cast<float>(frame.east.y); | |
| 295 | 81913 | f.eastZ = static_cast<float>(frame.east.z); | |
| 296 | 81913 | f.northX = static_cast<float>(frame.north.x); | |
| 297 | 81913 | f.northY = static_cast<float>(frame.north.y); | |
| 298 | 81913 | f.northZ = static_cast<float>(frame.north.z); | |
| 299 | 81913 | f.sphereRadius = surface.IsSphere() ? static_cast<float>(surface.Radius()) : 0.0f; | |
| 300 | 81913 | return f; | |
| 301 | } | ||
| 302 | |||
| 303 | 7186915 | SplitPoint MakeSplitPoint(const Surface& surface, const Vec2d& p) { | |
| 304 | 7186915 | const Vec3d at = surface.IsBody() ? surface.Position(p) : Vec3d{p.x, p.y, 0.0}; | |
| 305 | 7189309 | SplitPoint split{}; | |
| 306 | 7189309 | SplitCoordinate(at.x, split.x, split.xLow); | |
| 307 | 7189309 | SplitCoordinate(at.y, split.y, split.yLow); | |
| 308 | 7189309 | SplitCoordinate(at.z, split.z, split.zLow); | |
| 309 | 7189309 | return split; | |
| 310 | } | ||
| 311 | |||
| 312 | 343293 | void AppendKernelTerraces(const PreparedCrater& p, const CraterTerraces& terraces, | |
| 313 | const CraterProfile& profile, CraterRecord& record, | ||
| 314 | std::vector<float>& data) { | ||
| 315 | 343293 | const double diameter = p.crater.diameter; | |
| 316 | 343293 | if (!terraces.enabled || diameter < terraces.onsetDiameter) { | |
| 317 | 343267 | return; | |
| 318 | } | ||
| 319 | 27 | const CraterState& state = p.state; | |
| 320 | 54 | const double freshness = | |
| 321 | 27 | state.fresh.depthRatio > 0.0 ? state.current.depthRatio / state.fresh.depthRatio : 1.0; | |
| 322 | 27 | const double strength = | |
| 323 | 27 | Smoothstep((freshness - terraces.minFreshness) / (1.0 - terraces.minFreshness)); | |
| 324 | 27 | if (!(strength > 0.0)) { | |
| 325 | return; | ||
| 326 | } | ||
| 327 | 26 | KilometerPowerLaw floor{0.187, 1.249}; | |
| 328 | 26 | if (const auto* complex = dynamic_cast<const ComplexCraterProfile*>(&profile)) { | |
| 329 | 20 | floor = complex->Shape().floorDiameter; | |
| 330 | } | ||
| 331 | 26 | const double inner = std::clamp(floor.At(diameter) / diameter, 0.2, 0.9); | |
| 332 | 26 | const double radius = 0.5 * diameter; | |
| 333 | 26 | const double drop = | |
| 334 | 26 | profile.Height(radius, diameter, state) - profile.Height(inner * radius, diameter, state); | |
| 335 | 26 | const double meanSlope = drop / ((1.0 - inner) * radius); | |
| 336 | 26 | const double benchSlope = std::tan(terraces.benchSlopeDegrees * kDegree); | |
| 337 | 26 | const double scarpSlope = std::tan(terraces.scarpSlopeDegrees * kDegree) * | |
| 338 | (1.0 - 0.5 * kTerraceScarpRounding) * kTerraceScarpSqueeze; | ||
| 339 | 52 | const double slopeShare = meanSlope > 0.0 ? std::clamp(benchSlope / meanSlope, 0.02, 0.5) : 0.5; | |
| 340 | 52 | const double size = std::clamp((diameter - terraces.onsetDiameter) / | |
| 341 | 26 | (terraces.fullDiameter - terraces.onsetDiameter), | |
| 342 | 26 | 0.0, 1.0); | |
| 343 | 26 | const IdWords id = SplitId(p.crater.id); | |
| 344 | 52 | RandomStream random(terraces.seed.value_or(0u), kCraterTerraceStream, id.low, id.high); | |
| 345 | 26 | const double spread = terraces.maxBenches - terraces.minBenches; | |
| 346 | 26 | const int benches = | |
| 347 | 26 | std::clamp(static_cast<int>(std::lround(terraces.minBenches + spread * size + | |
| 348 | 26 | random.NextDouble() - 0.5)), | |
| 349 | 26 | terraces.minBenches, terraces.maxBenches); | |
| 350 | 52 | const auto harmonics = [&](int first, int count) { | |
| 351 | 26 | std::vector<double> pairs; | |
| 352 | 26 | double total = 0.0; | |
| 353 | 156 | for (int k = first; k < first + count; ++k) { | |
| 354 | 130 | total += 1.0 / k; | |
| 355 | } | ||
| 356 | 156 | for (int k = first; k < first + count; ++k) { | |
| 357 | 130 | pairs.push_back(1.0 / k / total); | |
| 358 | 130 | pairs.push_back(2.0 * kPi * random.NextDouble()); | |
| 359 | } | ||
| 360 | 26 | return pairs; | |
| 361 | ✗ | }; | |
| 362 | 26 | const std::vector<double> arcs = harmonics(2, 5); | |
| 363 | 26 | const double cover = | |
| 364 | 26 | terraces.arcShare >= 1.0 | |
| 365 | 26 | ? 1.0 | |
| 366 | 19 | : terraces.arcShare + std::max(0.0, kFullTerraceCover - terraces.arcShare) * size; | |
| 367 | 26 | double cut = -2.0; | |
| 368 | 26 | if (cover < 1.0) { | |
| 369 | 19 | constexpr std::size_t kSamples = 360; | |
| 370 | 19 | std::vector<double> values(kSamples); | |
| 371 | 6859 | for (std::size_t n = 0; n < kSamples; ++n) { | |
| 372 | 6840 | const double theta = 2.0 * kPi * static_cast<double>(n) / double{kSamples}; | |
| 373 | 6840 | double sum = 0.0; | |
| 374 | 41040 | for (std::size_t k = 0; k < 5; ++k) { | |
| 375 | 34200 | sum += arcs[2 * k] * | |
| 376 | 34200 | std::cos((static_cast<double>(k) + 2.0) * theta + arcs[2 * k + 1]); | |
| 377 | } | ||
| 378 | 6840 | values[n] = sum; | |
| 379 | } | ||
| 380 | 19 | const auto at = | |
| 381 | 19 | values.begin() + static_cast<std::ptrdiff_t>((1.0 - cover) * double{kSamples - 1}); | |
| 382 | 19 | std::nth_element(values.begin(), at, values.end()); | |
| 383 | 19 | cut = *at; | |
| 384 | 19 | } | |
| 385 | 26 | record.terraceCount = static_cast<std::uint32_t>(benches); | |
| 386 | 26 | record.terraceOffset = static_cast<std::uint32_t>(data.size()); | |
| 387 | 338 | for (const double value : | |
| 388 | 182 | {inner, slopeShare, strength, kTerraceScarpRounding, cut, kTerraceArcWidth}) { | |
| 389 | 156 | data.push_back(static_cast<float>(value)); | |
| 390 | } | ||
| 391 | 286 | for (const double value : arcs) { | |
| 392 | 260 | data.push_back(static_cast<float>(value)); | |
| 393 | } | ||
| 394 | 109 | const auto scallopCount = [&random] { | |
| 395 | 83 | return kMinTerraceScallops + | |
| 396 | 83 | std::floor((kMaxTerraceScallops - kMinTerraceScallops + 1.0) * random.NextDouble()); | |
| 397 | 26 | }; | |
| 398 | 26 | const double rimScallop = | |
| 399 | 26 | kTerraceRimScallop * terraces.rimVariation * strength * (0.6 + 0.8 * random.NextDouble()); | |
| 400 | 26 | for (const double value : | |
| 401 | 130 | {rimScallop, scallopCount(), random.NextDouble(), terraces.blockGaps}) { | |
| 402 | 104 | data.push_back(static_cast<float>(value)); | |
| 403 | } | ||
| 404 | 26 | std::vector<double> wall(kTerraceWallSamples + 1); | |
| 405 | 6708 | for (std::size_t n = 0; n < wall.size(); ++n) { | |
| 406 | 6682 | const double t = static_cast<double>(n) / kTerraceWallSamples; | |
| 407 | 6682 | const double height = profile.Height((inner + t * (1.0 - inner)) * radius, diameter, state); | |
| 408 | 6734 | wall[n] = n == 0 ? height : std::max(height, wall[n - 1]); | |
| 409 | } | ||
| 410 | 26 | const double rise = wall.back() - wall.front(); | |
| 411 | 135 | const auto across = [&](double share) { | |
| 412 | 109 | if (!(rise > 0.0)) { | |
| 413 | return share; | ||
| 414 | } | ||
| 415 | 109 | const double target = wall.front() + share * rise; | |
| 416 | 109 | const auto above = std::lower_bound(wall.begin(), wall.end(), target); | |
| 417 | 109 | if (above == wall.begin()) { | |
| 418 | return 0.0; | ||
| 419 | } | ||
| 420 | 83 | if (above == wall.end()) { | |
| 421 | return 1.0; | ||
| 422 | } | ||
| 423 | 83 | const auto n = static_cast<double>(above - wall.begin()); | |
| 424 | 83 | const double gap = *above - *(above - 1); | |
| 425 | 83 | const double part = gap > 0.0 ? (*above - target) / gap : 0.0; | |
| 426 | 83 | return (n - part) / kTerraceWallSamples; | |
| 427 | 26 | }; | |
| 428 | 26 | std::vector<double> heights{0.0}; | |
| 429 | 26 | double total = 0.0; | |
| 430 | 109 | for (int i = 0; i < benches; ++i) { | |
| 431 | 83 | total += 0.5 + random.NextDouble(); | |
| 432 | 83 | heights.push_back(total); | |
| 433 | } | ||
| 434 | 26 | std::vector<double> bases; | |
| 435 | 135 | for (double& height : heights) { | |
| 436 | 109 | height /= total; | |
| 437 | 109 | bases.push_back(across(height)); | |
| 438 | } | ||
| 439 | 26 | bases.back() = 1.0; | |
| 440 | 26 | const double narrowest = kNarrowestTerraceStep / benches; | |
| 441 | 83 | for (std::size_t k = 1; k + 1 < bases.size(); ++k) { | |
| 442 | 57 | bases[k] = std::max(bases[k], bases[k - 1] + narrowest); | |
| 443 | } | ||
| 444 | 83 | for (std::size_t k = bases.size() - 2; k > 0; --k) { | |
| 445 | 57 | bases[k] = std::min(bases[k], bases[k + 1] - narrowest); | |
| 446 | } | ||
| 447 | 26 | const double variation = terraces.rimVariation / (1.0 - inner); | |
| 448 | 83 | for (std::size_t k = 1; k < bases.size() - 1; ++k) { | |
| 449 | 57 | const double room = | |
| 450 | 57 | kTerraceScarpRoom * std::min(bases[k] - bases[k - 1], bases[k + 1] - bases[k]); | |
| 451 | 57 | double scallop = std::min(variation * (0.6 + 0.8 * random.NextDouble()), 0.9 * room); | |
| 452 | 57 | const double scallops = scallopCount(); | |
| 453 | 57 | const double phase = random.NextDouble(); | |
| 454 | 57 | double wobble = std::min(0.5 * variation * random.NextDouble(), 0.4 * room); | |
| 455 | 57 | const double wobblePhase = 2.0 * kPi * random.NextDouble(); | |
| 456 | 57 | double block = terraces.blockOffset * room; | |
| 457 | 57 | const double reach = 2.0 / 3.0 * scallop + wobble + block; | |
| 458 | 57 | if (reach > kTerraceScarpReach * room) { | |
| 459 | 54 | const double fit = kTerraceScarpReach * room / reach; | |
| 460 | 54 | scallop *= fit; | |
| 461 | 54 | wobble *= fit; | |
| 462 | 54 | block *= fit; | |
| 463 | } | ||
| 464 | 57 | const double key = static_cast<double>(random.NextU32() >> 8u); | |
| 465 | 969 | for (const double value : | |
| 466 | 513 | {bases[k], scallop, scallops, phase, wobble, wobblePhase, block, key}) { | |
| 467 | 456 | data.push_back(static_cast<float>(value)); | |
| 468 | } | ||
| 469 | } | ||
| 470 | 164864 | const auto wallAt = [&](double t) { | |
| 471 | 164838 | const double at = std::clamp(t, 0.0, 1.0) * kTerraceWallSamples; | |
| 472 | 164838 | const auto n = std::min(static_cast<std::size_t>(at), wall.size() - 2); | |
| 473 | 164838 | return wall[n] + (at - static_cast<double>(n)) * (wall[n + 1] - wall[n]); | |
| 474 | 26 | }; | |
| 475 | 159386 | const auto stairAt = [&](double within, double level, double scarp) { | |
| 476 | 159360 | const auto rounding = static_cast<float>(kTerraceScarpRounding); | |
| 477 | 159360 | const double lower = scarp * level; | |
| 478 | 159360 | const double upper = scarp * (1.0 - level); | |
| 479 | 159360 | double stair = level; | |
| 480 | 159360 | if (within < lower) { | |
| 481 | 54736 | stair = level * (1.0 - double{KernelTerraceEase( | |
| 482 | 54736 | static_cast<float>(1.0 - within / lower), rounding)}); | |
| 483 | 104624 | } else if (within > 1.0 - upper) { | |
| 484 | 92070 | stair = | |
| 485 | 46035 | level + (1.0 - level) * | |
| 486 | 46035 | double{KernelTerraceEase( | |
| 487 | 46035 | static_cast<float>((within - (1.0 - upper)) / upper), rounding)}; | |
| 488 | } | ||
| 489 | 159360 | return slopeShare * within + (1.0 - slopeShare) * stair; | |
| 490 | 26 | }; | |
| 491 | 2599 | const auto meanHeight = [&](double from, double to, double level, double scarp) { | |
| 492 | 2573 | double weighted = 0.0; | |
| 493 | 2573 | double weights = 0.0; | |
| 494 | 167245 | for (int n = 0; n < kTerraceMeanSamples; ++n) { | |
| 495 | 164672 | const double within = (n + 0.5) / kTerraceMeanSamples; | |
| 496 | 164672 | const double r = inner + (from + within * (to - from)) * (1.0 - inner); | |
| 497 | 164672 | const double shaped = level < 0.0 ? within : stairAt(within, level, scarp); | |
| 498 | 164672 | weighted += r * wallAt(from + shaped * (to - from)); | |
| 499 | 164672 | weights += r; | |
| 500 | } | ||
| 501 | 2573 | return weighted / weights; | |
| 502 | 26 | }; | |
| 503 | 109 | for (std::size_t k = 0; k + 1 < bases.size(); ++k) { | |
| 504 | 83 | const double from = bases[k]; | |
| 505 | 83 | const double to = bases[k + 1]; | |
| 506 | 83 | const double stepSlope = | |
| 507 | 83 | (wallAt(to) - wallAt(from)) / ((to - from) * (1.0 - inner) * radius); | |
| 508 | 166 | const double needed = | |
| 509 | 83 | scarpSlope > benchSlope ? (stepSlope - benchSlope) / (scarpSlope - benchSlope) : 1.0; | |
| 510 | 83 | const double scarp = std::clamp(needed, terraces.scarpShare, kMostTerraceScarpShare); | |
| 511 | 83 | const double kept = | |
| 512 | 83 | 1.0 - Smoothstep((needed - kSteepTerraceStep) / (1.0 - kSteepTerraceStep)); | |
| 513 | 83 | const double target = meanHeight(from, to, -1.0, scarp); | |
| 514 | 83 | const BisectionInterval level = | |
| 515 | 83 | Bisect(kTerraceLevelMargin, 1.0 - kTerraceLevelMargin, kTerraceLevelIterations, 0.0, | |
| 516 | 2573 | [&](double l) { return !(meanHeight(from, to, l, scarp) < target); }); | |
| 517 | 332 | for (const double value : {0.5 * (level.low + level.high), scarp, kept}) { | |
| 518 | 249 | data.push_back(static_cast<float>(value)); | |
| 519 | } | ||
| 520 | } | ||
| 521 | 26 | } | |
| 522 | |||
| 523 | 343485 | void AppendKernelPeaks(const PreparedCrater& p, const CraterPeaks& peaks, | |
| 524 | const CraterProfile& profile, CraterRecord& record, | ||
| 525 | std::vector<float>& data) { | ||
| 526 | 343485 | const auto* complex = dynamic_cast<const ComplexCraterProfile*>(&profile); | |
| 527 | 343485 | if (!peaks.enabled || complex == nullptr || | |
| 528 | 13 | p.crater.diameter < complex->Shape().transitionDiameter) { | |
| 529 | 343473 | return; | |
| 530 | } | ||
| 531 | 12 | const CraterState& state = p.state; | |
| 532 | 12 | const double freshness = | |
| 533 | 12 | state.fresh.depthRatio > 0.0 | |
| 534 | 24 | ? std::clamp(state.current.depthRatio / state.fresh.depthRatio, 0.0, 1.0) | |
| 535 | 12 | : 1.0; | |
| 536 | 12 | const IdWords id = SplitId(p.crater.id); | |
| 537 | 24 | RandomStream random(peaks.seed.value_or(0u), kCraterPeakStream, id.low, id.high); | |
| 538 | 12 | const int massifs = | |
| 539 | 12 | peaks.minMassifs + | |
| 540 | 24 | std::min(peaks.maxMassifs - peaks.minMassifs, | |
| 541 | 12 | static_cast<int>((peaks.maxMassifs - peaks.minMassifs + 1) * random.NextDouble())); | |
| 542 | 12 | record.peakCount = static_cast<std::uint32_t>(massifs); | |
| 543 | 12 | record.peakOffset = static_cast<std::uint32_t>(data.size()); | |
| 544 | 12 | const double key = static_cast<double>(random.NextU32() >> 8u); | |
| 545 | 156 | for (const double value : {peaks.roughness * freshness, peaks.roughnessWavelength, peaks.warp, | |
| 546 | 84 | key, peaks.heightScale, peaks.widthScale}) { | |
| 547 | 72 | data.push_back(static_cast<float>(value)); | |
| 548 | } | ||
| 549 | 12 | const double chain = 2.0 * kPi * random.NextDouble(); | |
| 550 | 12 | const double room = kPeakRoom - peaks.warp; | |
| 551 | 38 | for (int n = 0; n < massifs; ++n) { | |
| 552 | 26 | const bool main = n == 0; | |
| 553 | 26 | const double side = n % 2 == 0 ? kPi : 0.0; | |
| 554 | 18 | const double bearing = | |
| 555 | 18 | main ? 2.0 * kPi * random.NextDouble() | |
| 556 | 14 | : chain + side + peaks.chainSpreadDegrees * kDegree * (random.NextDouble() - 0.5); | |
| 557 | 26 | const double reach = peaks.spreadRadii * (main ? 0.25 * random.NextDouble() | |
| 558 | 14 | : 0.35 + 0.65 * random.NextDouble()); | |
| 559 | 26 | const double aspect = 1.0 + (peaks.elongation - 1.0) * random.NextDouble(); | |
| 560 | 26 | const double stretch = std::sqrt(aspect); | |
| 561 | 26 | const double crowding = massifs == 1 ? 1.0 : std::sqrt(2.0 / massifs); | |
| 562 | 26 | double radius = crowding * (main ? kMainMassifRadius | |
| 563 | 14 | : kLesserMassifRadius * (0.5 + random.NextDouble())); | |
| 564 | 26 | radius = std::max(0.1, std::min(radius, (room - reach) / stretch)); | |
| 565 | 26 | const double heading = | |
| 566 | 26 | main ? 2.0 * kPi * random.NextDouble() : bearing + 0.5 * (random.NextDouble() - 0.5); | |
| 567 | 26 | const double height = main ? 1.0 : 1.0 - peaks.heightVariation * random.NextDouble(); | |
| 568 | 26 | const double exponent = | |
| 569 | 26 | std::clamp((2.0 - 1.5 * peaks.sharpness) * (0.8 + 0.4 * random.NextDouble()), 0.4, 2.5); | |
| 570 | 26 | const double pull = kMaxSummitPull * peaks.lopsidedness * std::sqrt(random.NextDouble()); | |
| 571 | 26 | const double pullBearing = 2.0 * kPi * random.NextDouble(); | |
| 572 | 598 | for (const double value : | |
| 573 | 26 | {reach * std::cos(bearing), reach * std::sin(bearing), radius, stretch, | |
| 574 | 26 | std::cos(heading), std::sin(heading), height, exponent, pull * std::cos(pullBearing), | |
| 575 | 312 | pull * std::sin(pullBearing), 2.0 - peaks.sharpness}) { | |
| 576 | 286 | data.push_back(static_cast<float>(value)); | |
| 577 | } | ||
| 578 | } | ||
| 579 | } | ||
| 580 | |||
| 581 | 239518 | CraterKernelBatch MakeCraterKernelBatch(const CraterShapeKernel& kernel, | |
| 582 | const CraterProfile& profile, | ||
| 583 | const std::vector<PreparedCrater>& prepared, | ||
| 584 | const std::vector<std::size_t>& craters, | ||
| 585 | const CraterApplyOptions& options) { | ||
| 586 | 239518 | CraterKernelBatch batch; | |
| 587 | 239518 | ProfileTables tables(kernel, profile); | |
| 588 | 239510 | tables.AppendAges(batch.data); | |
| 589 | 239528 | batch.records.reserve(craters.size()); | |
| 590 | 583150 | for (const std::size_t k : craters) { | |
| 591 | 343584 | const PreparedCrater& p = prepared[k]; | |
| 592 | 343584 | CheckProfileState(kernel, p.crater.diameter, p.state); | |
| 593 | 343605 | CraterRecord r = BaseRecord(p, options); | |
| 594 | 343605 | SplitCoordinate(p.crater.x, r.x, r.xLow); | |
| 595 | 343605 | SplitCoordinate(p.crater.y, r.y, r.yLow); | |
| 596 | 343605 | AppendKernelOutline(p.outline, r, batch.data); | |
| 597 | 343615 | AppendKernelTerraces(p, options.terraces, profile, r, batch.data); | |
| 598 | 343405 | AppendKernelPeaks(p, options.peaks, profile, r, batch.data); | |
| 599 | 343544 | tables.Append(p, r, batch.data); | |
| 600 | 343632 | batch.records.push_back(r); | |
| 601 | } | ||
| 602 | 239566 | if (batch.data.empty()) { | |
| 603 | 148064 | batch.data.push_back(0.0f); | |
| 604 | } | ||
| 605 | 239565 | return batch; | |
| 606 | 239555 | } | |
| 607 | |||
| 608 | 77468 | std::vector<std::uint32_t> FlattenKernelLists(const std::vector<std::vector<std::uint32_t>>& lists, | |
| 609 | std::vector<std::uint32_t>& heads) { | ||
| 610 | 77468 | std::vector<std::uint32_t> entries; | |
| 611 | 1123581 | for (const std::vector<std::uint32_t>& list : lists) { | |
| 612 | 1046127 | heads.push_back(static_cast<std::uint32_t>(entries.size())); | |
| 613 | 1046193 | heads.push_back(static_cast<std::uint32_t>(list.size())); | |
| 614 | 1046209 | entries.insert(entries.end(), list.begin(), list.end()); | |
| 615 | } | ||
| 616 | 77454 | if (entries.empty()) { | |
| 617 | 15326 | entries.push_back(0); | |
| 618 | } | ||
| 619 | 77461 | return entries; | |
| 620 | } | ||
| 621 | |||
| 622 | } // namespace terraingen | ||
| 623 |