./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
47 / 47
100.0%
Branches:
29 / 38
76.3%
Functions:
6 / 6
100.0%

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
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 if (!IsPositive(shape.transitionDiameter)) {
21
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("The complex crater transition diameter must be positive");
22 }
23
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 57 times.
62 const KilometerPowerLaw peakWidth = shape.peakDiameter ? *shape.peakDiameter : shape.peakVolume;
24
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 60 times.
246 for (const KilometerPowerLaw& law : {shape.floorDiameter, shape.peakHeight, peakWidth}) {
25
2/4
✓ Branch 0 taken 186 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
✗ Branch 3 not taken.
370 if (!(IsPositive(law.coefficient) && std::isfinite(law.exponent))) {
26
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Complex crater power laws need a positive coefficient");
27 }
28 }
29
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
60 if (shape.peakFitMaxDiameter && !IsPositive(*shape.peakFitMaxDiameter)) {
30
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("The peak fit limit must be positive");
31 }
32
4/6
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 59 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 58 times.
✓ Branch 5 taken 1 time.
118 if (!(IsPositive(shape.ejectaExponent) && std::isfinite(shape.reachRadii) &&
33 shape.reachRadii > 1.0)) {
34 1 throw std::invalid_argument(
35
1/1
✓ Branch 2 taken 1 time.
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
2/2
✓ Branch 0 taken 10404 times.
✓ Branch 1 taken 20868 times.
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
2/2
✓ Branch 0 taken 20868 times.
✓ Branch 1 taken 10404 times.
31272 p.peakFromVolume = shape.peakDiameter ? 0u : 1u;
50
1/2
✓ Branch 0 taken 31272 times.
✗ Branch 1 not taken.
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
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 57 times.
58 if (!m_simple) {
59
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A complex crater profile needs a simple one");
60 }
61
1/1
✓ Branch 1 taken 57 times.
57 ValidateComplexCraterShape(m_shape);
62 57 SetKernelComplexShape(m_kernel, m_shape);
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
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
2/2
✓ Branch 0 taken 2882767 times.
✓ Branch 1 taken 3007580 times.
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