./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
40 / 40
100.0%
Branches:
46 / 52
88.5%
Functions:
7 / 7
100.0%

src/config/body.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "config/body.h"
4
5 #include "terraingen/mesh/io/icq.h"
6 #include "terraingen/mesh/io/mesh_io.h"
7 #include "terraingen/mesh/shape_model.h"
8 #include "util/overloaded.h"
9
10 #include <map>
11 #include <memory>
12 #include <mutex>
13 #include <system_error>
14 #include <tuple>
15
16 namespace terraingen {
17
18 namespace {
19
20 using config::At;
21 using config::AtFile;
22 using config::Resolve;
23
24 /// The radial shape a shape model spec describes, loaded once per file, modification time, Q and
25 /// unit, so surfaces built from one spec share it and compare equal.
26 /// @throws ConfigError at "body.path" for a file that cannot be read or resampled.
27 18 std::shared_ptr<const RadialShape> LoadShapeModel(const ShapeModelSurfaceSpec& spec,
28 const std::filesystem::path& directory) {
29
1/1
✓ Branch 2 taken 17 times.
36 config::CheckNonEmpty(spec.path, "body.path");
30 17 const std::filesystem::path file = Resolve(spec.path, directory);
31 17 std::error_code error;
32 17 const auto modified = std::filesystem::last_write_time(file, error);
33
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 16 times.
17 if (error) {
34
4/4
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
2 throw ConfigError("body.path", "cannot read " + file.string());
35 }
36 16 using Key = std::tuple<std::string, std::filesystem::file_time_type, std::size_t, double>;
37 16 static std::mutex mutex;
38
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 9 times.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
16 static std::map<Key, std::shared_ptr<const RadialShape>> loaded;
39
3/3
✓ Branch 1 taken 16 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 13 times.
51 const Key key{std::filesystem::absolute(file).string(), modified, spec.q.value_or(0),
40
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 14 times.
18 spec.metersPerUnit.value_or(0.0)};
41
1/1
✓ Branch 1 taken 16 times.
16 const std::lock_guard<std::mutex> lock(mutex);
42
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 10 times.
16 if (const auto found = loaded.find(key); found != loaded.end()) {
43
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
12 return found->second;
44 }
45 20 const auto shape = AtFile("body.path", [&] {
46
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
10 if (IsIcqPath(file)) {
47
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 7 times.
9 const IcqModel model = ReadIcq(file, spec.metersPerUnit.value_or(1000.0));
48
4/5
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
14 return spec.q && *spec.q != model.q ? ShapeModelFromMesh(IcqToMesh(model), *spec.q)
49
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 6 times.
7 : ToRadialShape(model);
50 7 }
51
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 const double scale = spec.metersPerUnit.value_or(1.0);
52
1/1
✓ Branch 2 taken 2 times.
2 const TriangleMesh mesh = Scaled(ReadMesh(file).mesh, {scale, scale, scale});
53
2/3
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
4 return ShapeModelFromMesh(mesh, spec.q.value_or(256));
54
3/3
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
21 });
55
4/6
✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 8 times.
16 return loaded.emplace(key, std::make_shared<const RadialShape>(shape)).first->second;
56 40 }
57
58 } // namespace
59
60 502 Surface BuildSurface(const SurfaceSpec& spec, const std::filesystem::path& directory) {
61
1/1
✓ Branch 2 taken 490 times.
992 config::CheckConfigRanges(spec, "body");
62 976 return std::visit(Overloaded{
63 403 [](const PlaneSurfaceSpec&) { return Surface::Plane(); },
64 63 [](const SphereSurfaceSpec& s) {
65
2/2
✓ Branch 2 taken 63 times.
✓ Branch 5 taken 63 times.
189 return At("body.radius_m", [&] { return Surface::Sphere(s.radius); });
66 },
67 6 [](const EllipsoidSurfaceSpec& s) {
68 12 return At("body.radii_m", [&] {
69 6 return Surface::Ellipsoid(s.radii[0], s.radii[1], s.radii[2]);
70
1/1
✓ Branch 2 taken 6 times.
12 });
71 },
72 18 [&](const ShapeModelSurfaceSpec& s) {
73
1/1
✓ Branch 2 taken 14 times.
32 return Surface::ShapeModel(LoadShapeModel(s, directory));
74 },
75 },
76 490 spec);
77 }
78
79 } // namespace terraingen
80