src/layers/craters/profiles/complex.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/craters/profiles/complex.h" | ||
| 4 | |||
| 5 | #include "util/checks.h" | ||
| 6 | |||
| 7 | #include <cmath> | ||
| 8 | #include <stdexcept> | ||
| 9 | #include <utility> | ||
| 10 | |||
| 11 | namespace terraingen { | ||
| 12 | |||
| 13 | 502 | double KilometerPowerLaw::At(double diameter, bool volume) const { | |
| 14 | 502 | return static_cast<double>(KilometerLaw(static_cast<float>(coefficient), | |
| 15 | 502 | static_cast<float>(exponent), | |
| 16 | 502 | static_cast<float>(diameter), volume)); | |
| 17 | } | ||
| 18 | |||
| 19 | 63 | void ValidateComplexCraterShape(const ComplexCraterShape& shape) { | |
| 20 | 63 | if (!IsPositive(shape.transitionDiameter)) { | |
| 21 | 1 | throw std::invalid_argument("The complex crater transition diameter must be positive"); | |
| 22 | } | ||
| 23 | 62 | const KilometerPowerLaw peakWidth = shape.peakDiameter ? *shape.peakDiameter : shape.peakVolume; | |
| 24 | 246 | for (const KilometerPowerLaw& law : {shape.floorDiameter, shape.peakHeight, peakWidth}) { | |
| 25 | 370 | if (!(IsPositive(law.coefficient) && std::isfinite(law.exponent))) { | |
| 26 | 2 | throw std::invalid_argument("Complex crater power laws need a positive coefficient"); | |
| 27 | } | ||
| 28 | } | ||
| 29 | 60 | if (shape.peakFitMaxDiameter && !IsPositive(*shape.peakFitMaxDiameter)) { | |
| 30 | 1 | throw std::invalid_argument("The peak fit limit must be positive"); | |
| 31 | } | ||
| 32 | 118 | if (!(IsPositive(shape.ejectaExponent) && std::isfinite(shape.reachRadii) && | |
| 33 | shape.reachRadii > 1.0)) { | ||
| 34 | 1 | throw std::invalid_argument( | |
| 35 | 1 | "Complex craters need a positive ejecta exponent and a reach over 1 radius"); | |
| 36 | } | ||
| 37 | 58 | } | |
| 38 | |||
| 39 | 31272 | void SetKernelComplexShape(CraterProfileKernel& p, const ComplexCraterShape& shape) { | |
| 40 | 31272 | const KilometerPowerLaw width = shape.peakDiameter ? *shape.peakDiameter : shape.peakVolume; | |
| 41 | 31272 | p.complex = 1u; | |
| 42 | 31272 | p.transitionDiameter = static_cast<float>(shape.transitionDiameter); | |
| 43 | 31272 | p.floorCoefficient = static_cast<float>(shape.floorDiameter.coefficient); | |
| 44 | 31272 | p.floorExponent = static_cast<float>(shape.floorDiameter.exponent); | |
| 45 | 31272 | p.peakHeightCoefficient = static_cast<float>(shape.peakHeight.coefficient); | |
| 46 | 31272 | p.peakHeightExponent = static_cast<float>(shape.peakHeight.exponent); | |
| 47 | 31272 | p.peakWidthCoefficient = static_cast<float>(width.coefficient); | |
| 48 | 31272 | p.peakWidthExponent = static_cast<float>(width.exponent); | |
| 49 | 31272 | p.peakFromVolume = shape.peakDiameter ? 0u : 1u; | |
| 50 | 31272 | p.peakFitMaxDiameter = static_cast<float>(shape.peakFitMaxDiameter.value_or(0.0)); | |
| 51 | 31272 | p.ejectaExponent = static_cast<float>(shape.ejectaExponent); | |
| 52 | 31272 | p.complexReachRadii = static_cast<float>(shape.reachRadii); | |
| 53 | 31272 | } | |
| 54 | |||
| 55 | 58 | ComplexCraterProfile::ComplexCraterProfile(std::shared_ptr<const CraterProfile> simple, | |
| 56 | 58 | ComplexCraterShape shape) | |
| 57 | 58 | : m_simple(std::move(simple)), m_shape(shape) { | |
| 58 | 58 | if (!m_simple) { | |
| 59 | 1 | throw std::invalid_argument("A complex crater profile needs a simple one"); | |
| 60 | } | ||
| 61 | 57 | ValidateComplexCraterShape(m_shape); | |
| 62 | 57 | SetKernelComplexShape(m_kernel, m_shape); | |
| 63 | 58 | } | |
| 64 | |||
| 65 | 139293 | double ComplexCraterProfile::ReachRadii() const { | |
| 66 | 139293 | return std::fmax(m_simple->ReachRadii(), m_shape.reachRadii); | |
| 67 | } | ||
| 68 | |||
| 69 | 5890347 | double ComplexCraterProfile::Height(double distance, double diameter, | |
| 70 | const CraterState& state) const { | ||
| 71 | 5890347 | if (!IsComplex(diameter)) { | |
| 72 | 2882767 | return m_simple->Height(distance, diameter, state); | |
| 73 | } | ||
| 74 | 3007580 | return static_cast<double>(ComplexProfileHeight(m_kernel, static_cast<float>(distance), | |
| 75 | static_cast<float>(diameter), | ||
| 76 | 3007580 | KernelStateRecord(diameter, state))); | |
| 77 | } | ||
| 78 | |||
| 79 | } // namespace terraingen | ||
| 80 |