./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
34 / 34
100.0%
Branches:
24 / 24
100.0%
Functions:
4 / 4, 1 excl
100.0%

include/terraingen/layers/craters/shaping/datum.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Crater datums, the surfaces crater interiors are cut to, fitted to the ground.
5 #pragma once
6
7 #include "terraingen/geometry/plane_fit.h"
8 #include "terraingen/layers/craters/profiles/profile.h"
9 #include "terraingen/layers/craters/shaping/apply_options.h"
10 #include "terraingen/layers/craters/shaping/prepared.h"
11 #include "terraingen/layers/ground.h"
12
13 #include <cstddef>
14 #include <functional>
15 #include <optional>
16
17 namespace terraingen {
18
19 /// Heights of the ground at world positions, NaN where there is none; thread-safe.
20 using HeightSampler = std::function<float(double x, double y)>;
21
22 /// Heights of the ground at many world positions at once, into a span of the same size;
23 /// thread-safe.
24 using HeightsSampler = std::function<void(Span<const Vec2d> points, Span<float> heights)>;
25
26 /// Points per side of the lattice crater datums are fitted over.
27 constexpr int kDatumLatticeSide = 7;
28
29 /// Points of the lattice crater datums are fitted over, inside the footprint or not.
30 constexpr std::size_t kDatumLatticePoints =
31 static_cast<std::size_t>(kDatumLatticeSide * kDatumLatticeSide);
32
33 /// The offset (m) from a crater's center of lattice point `index` (0 to `kDatumLatticeSide` - 1)
34 /// along one axis of a fit footprint of `fitRadius` (m).
35 795551 inline double DatumLatticeOffset(int index, double fitRadius) {
36 795495 return fitRadius * ((2.0 * index + 1.0) / kDatumLatticeSide - 1.0);
37 }
38
39 /// Calls `visit(point, dx, dy)` for each lattice point inside a fit footprint of `fitRadius`
40 /// meters, `dx` east and `dy` north of the center, row by row; `point` numbers every lattice
41 /// point, inside or not, row by row.
42 14227 template <typename Visit> void ForEachDatumPoint(double fitRadius, const Visit& visit) {
43
8/8
void terraingen::ForEachDatumPoint<ForEachDatumPoint_VisitsTheLatticeInsideTheFootprintRowByRow_Test::TestBody()::{lambda(unsigned long, double, double)#1}>(double, ForEachDatumPoint_VisitsTheLatticeInsideTheFootprintRowByRow_Test::TestBody()::{lambda(unsigned long, double, double)#1} const&):
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 time.
void terraingen::ForEachDatumPoint<terraingen::(anonymous namespace)::FitFootprint(terraingen::Crater const&, double, terraingen::Surface const&, std::function<float (double, double)> const&)::{lambda(unsigned long, double, double)#1}>(double, terraingen::(anonymous namespace)::FitFootprint(terraingen::Crater const&, double, terraingen::Surface const&, std::function<float (double, double)> const&)::{lambda(unsigned long, double, double)#1} const&):
✓ Branch 0 taken 10430 times.
✓ Branch 1 taken 1490 times.
void terraingen::ForEachDatumPoint<terraingen::(anonymous namespace)::LocateDatumLattice(std::vector<terraingen::PreparedCrater, std::allocator<terraingen::PreparedCrater> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, terraingen::CraterApplyOptions const&, unsigned int)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(unsigned long, double, double)#1}>(double, terraingen::(anonymous namespace)::LocateDatumLattice(std::vector<terraingen::PreparedCrater, std::allocator<terraingen::PreparedCrater> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, terraingen::CraterApplyOptions const&, unsigned int)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(unsigned long, double, double)#1} const&):
✓ Branch 0 taken 44550 times.
✓ Branch 1 taken 6368 times.
void terraingen::ForEachDatumPoint<terraingen::(anonymous namespace)::SolveDatums(std::vector<terraingen::PreparedCrater, std::allocator<terraingen::PreparedCrater> >&, std::vector<unsigned long, std::allocator<unsigned long> > const&, terraingen::Raster<float> const&, std::vector<unsigned char, std::allocator<unsigned char> > const&, terraingen::CraterApplyOptions const&, unsigned int)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(unsigned long, double, double)#1}>(double, terraingen::(anonymous namespace)::SolveDatums(std::vector<terraingen::PreparedCrater, std::allocator<terraingen::PreparedCrater> >&, std::vector<unsigned long, std::allocator<unsigned long> > const&, terraingen::Raster<float> const&, std::vector<unsigned char, std::allocator<unsigned char> > const&, terraingen::CraterApplyOptions const&, unsigned int)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(unsigned long, double, double)#1} const&):
✓ Branch 0 taken 44566 times.
✓ Branch 1 taken 6368 times.
113780 for (int j = 0; j < kDatumLatticeSide; ++j) {
44 99553 const double dy = DatumLatticeOffset(j, fitRadius);
45
8/8
void terraingen::ForEachDatumPoint<ForEachDatumPoint_VisitsTheLatticeInsideTheFootprintRowByRow_Test::TestBody()::{lambda(unsigned long, double, double)#1}>(double, ForEachDatumPoint_VisitsTheLatticeInsideTheFootprintRowByRow_Test::TestBody()::{lambda(unsigned long, double, double)#1} const&):
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 7 times.
void terraingen::ForEachDatumPoint<terraingen::(anonymous namespace)::FitFootprint(terraingen::Crater const&, double, terraingen::Surface const&, std::function<float (double, double)> const&)::{lambda(unsigned long, double, double)#1}>(double, terraingen::(anonymous namespace)::FitFootprint(terraingen::Crater const&, double, terraingen::Surface const&, std::function<float (double, double)> const&)::{lambda(unsigned long, double, double)#1} const&):
✓ Branch 0 taken 73010 times.
✓ Branch 1 taken 10430 times.
void terraingen::ForEachDatumPoint<terraingen::(anonymous namespace)::LocateDatumLattice(std::vector<terraingen::PreparedCrater, std::allocator<terraingen::PreparedCrater> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, terraingen::CraterApplyOptions const&, unsigned int)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(unsigned long, double, double)#1}>(double, terraingen::(anonymous namespace)::LocateDatumLattice(std::vector<terraingen::PreparedCrater, std::allocator<terraingen::PreparedCrater> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, terraingen::CraterApplyOptions const&, unsigned int)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(unsigned long, double, double)#1} const&):
✓ Branch 0 taken 311105 times.
✓ Branch 1 taken 44550 times.
void terraingen::ForEachDatumPoint<terraingen::(anonymous namespace)::SolveDatums(std::vector<terraingen::PreparedCrater, std::allocator<terraingen::PreparedCrater> >&, std::vector<unsigned long, std::allocator<unsigned long> > const&, terraingen::Raster<float> const&, std::vector<unsigned char, std::allocator<unsigned char> > const&, terraingen::CraterApplyOptions const&, unsigned int)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(unsigned long, double, double)#1}>(double, terraingen::(anonymous namespace)::SolveDatums(std::vector<terraingen::PreparedCrater, std::allocator<terraingen::PreparedCrater> >&, std::vector<unsigned long, std::allocator<unsigned long> > const&, terraingen::Raster<float> const&, std::vector<unsigned char, std::allocator<unsigned char> > const&, terraingen::CraterApplyOptions const&, unsigned int)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(unsigned long, double, double)#1} const&):
✓ Branch 0 taken 311760 times.
✓ Branch 1 taken 44566 times.
795477 for (int i = 0; i < kDatumLatticeSide; ++i) {
46 695924 const double dx = DatumLatticeOffset(i, fitRadius);
47
8/8
void terraingen::ForEachDatumPoint<ForEachDatumPoint_VisitsTheLatticeInsideTheFootprintRowByRow_Test::TestBody()::{lambda(unsigned long, double, double)#1}>(double, ForEachDatumPoint_VisitsTheLatticeInsideTheFootprintRowByRow_Test::TestBody()::{lambda(unsigned long, double, double)#1} const&):
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 12 times.
void terraingen::ForEachDatumPoint<terraingen::(anonymous namespace)::FitFootprint(terraingen::Crater const&, double, terraingen::Surface const&, std::function<float (double, double)> const&)::{lambda(unsigned long, double, double)#1}>(double, terraingen::(anonymous namespace)::FitFootprint(terraingen::Crater const&, double, terraingen::Surface const&, std::function<float (double, double)> const&)::{lambda(unsigned long, double, double)#1} const&):
✓ Branch 0 taken 55130 times.
✓ Branch 1 taken 17880 times.
void terraingen::ForEachDatumPoint<terraingen::(anonymous namespace)::LocateDatumLattice(std::vector<terraingen::PreparedCrater, std::allocator<terraingen::PreparedCrater> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, terraingen::CraterApplyOptions const&, unsigned int)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(unsigned long, double, double)#1}>(double, terraingen::(anonymous namespace)::LocateDatumLattice(std::vector<terraingen::PreparedCrater, std::allocator<terraingen::PreparedCrater> > const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, terraingen::CraterApplyOptions const&, unsigned int)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(unsigned long, double, double)#1} const&):
✓ Branch 0 taken 234898 times.
✓ Branch 1 taken 76207 times.
void terraingen::ForEachDatumPoint<terraingen::(anonymous namespace)::SolveDatums(std::vector<terraingen::PreparedCrater, std::allocator<terraingen::PreparedCrater> >&, std::vector<unsigned long, std::allocator<unsigned long> > const&, terraingen::Raster<float> const&, std::vector<unsigned char, std::allocator<unsigned char> > const&, terraingen::CraterApplyOptions const&, unsigned int)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(unsigned long, double, double)#1}>(double, terraingen::(anonymous namespace)::SolveDatums(std::vector<terraingen::PreparedCrater, std::allocator<terraingen::PreparedCrater> >&, std::vector<unsigned long, std::allocator<unsigned long> > const&, terraingen::Raster<float> const&, std::vector<unsigned char, std::allocator<unsigned char> > const&, terraingen::CraterApplyOptions const&, unsigned int)::{lambda(unsigned long)#1}::operator()(unsigned long) const::{lambda(unsigned long, double, double)#1} const&):
✓ Branch 0 taken 235423 times.
✓ Branch 1 taken 76337 times.
695924 if (dx * dx + dy * dy < fitRadius * fitRadius) {
48 525488 visit(static_cast<std::size_t>(j * kDatumLatticeSide + i), dx, dy);
49 }
50 }
51 }
52 14227 }
53
54 /// The datum `fit` gives a crater: its plane for `CraterDatum::Plane`, else flat at its mean;
55 /// nothing when it holds no point.
56 std::optional<Plane> FittedDatum(const PlaneFit& fit, CraterDatum datum);
57
58 /// The datum a crater is cut to, fitted to the ground beneath it; nothing for
59 /// `CraterDatum::None` or where the ground has no data.
60 ///
61 /// `CenterHeight` reads the ground at the center; `MeanHeight` and `Plane` fit it at the points of
62 /// a 7 x 7 lattice inside the `datumFitRadii` footprint.
63 std::optional<Plane> FitCraterDatum(const Crater& crater, const CraterApplyOptions& options,
64 const HeightSampler& ground);
65
66 /// The plane a crater with `Crater::replace` is cut to, fitted to the ground on the ring
67 /// `CraterReplacement` gives; nothing where the ground has no data there.
68 /// @param outlineScale The crater outline's largest stretch (`CraterOutline::MaxScale`).
69 std::optional<Plane> FitReplacementDatum(const Crater& crater, const CraterApplyOptions& options,
70 double reachRadii, double outlineScale,
71 const HeightSampler& ground);
72
73 /// How far (m) beyond a grid replacing craters can change it through their datum fit: their fit
74 /// ring, stretched by the largest outline, plus the profile's reach.
75 /// @param maxDiameter The largest replacing crater's diameter, in meters; 0 for none.
76 double ReplacementMargin(const CraterApplyOptions& options, const CraterProfile& profile,
77 double maxDiameter);
78
79 /// How far (m) beyond a grid craters with a datum can change it through their datum fit: the fit
80 /// footprint plus the profile's reach for the largest crater; 0 without a datum. Craters for a
81 /// grid or point should be generated this far around it.
82 /// @param maxDiameter The largest crater's diameter, in meters.
83 double DatumMargin(const CraterApplyOptions& options, const CraterProfile& profile,
84 double maxDiameter);
85
86 } // namespace terraingen
87