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 | 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 | 17 | if (error) { | |
| 34 | 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 | 16 | static std::map<Key, std::shared_ptr<const RadialShape>> loaded; | |
| 39 | 51 | const Key key{std::filesystem::absolute(file).string(), modified, spec.q.value_or(0), | |
| 40 | 18 | spec.metersPerUnit.value_or(0.0)}; | |
| 41 | 16 | const std::lock_guard<std::mutex> lock(mutex); | |
| 42 | 16 | if (const auto found = loaded.find(key); found != loaded.end()) { | |
| 43 | 12 | return found->second; | |
| 44 | } | ||
| 45 | 20 | const auto shape = AtFile("body.path", [&] { | |
| 46 | 10 | if (IsIcqPath(file)) { | |
| 47 | 9 | const IcqModel model = ReadIcq(file, spec.metersPerUnit.value_or(1000.0)); | |
| 48 | 14 | return spec.q && *spec.q != model.q ? ShapeModelFromMesh(IcqToMesh(model), *spec.q) | |
| 49 | 7 | : ToRadialShape(model); | |
| 50 | 7 | } | |
| 51 | 2 | const double scale = spec.metersPerUnit.value_or(1.0); | |
| 52 | 2 | const TriangleMesh mesh = Scaled(ReadMesh(file).mesh, {scale, scale, scale}); | |
| 53 | 4 | return ShapeModelFromMesh(mesh, spec.q.value_or(256)); | |
| 54 | 21 | }); | |
| 55 | 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 | 992 | config::CheckConfigRanges(spec, "body"); | |
| 62 | 976 | return std::visit(Overloaded{ | |
| 63 | 403 | [](const PlaneSurfaceSpec&) { return Surface::Plane(); }, | |
| 64 | 63 | [](const SphereSurfaceSpec& s) { | |
| 65 | 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 | 12 | }); | |
| 71 | }, | ||
| 72 | 18 | [&](const ShapeModelSurfaceSpec& s) { | |
| 73 | 32 | return Surface::ShapeModel(LoadShapeModel(s, directory)); | |
| 74 | }, | ||
| 75 | }, | ||
| 76 | 490 | spec); | |
| 77 | } | ||
| 78 | |||
| 79 | } // namespace terraingen | ||
| 80 |