src/pipeline/body_mesh.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/pipeline/body_mesh.h" | ||
| 4 | |||
| 5 | #include "terraingen/geometry/geometry.h" | ||
| 6 | #include "terraingen/util/span.h" | ||
| 7 | #include "util/checks.h" | ||
| 8 | |||
| 9 | #include <cmath> | ||
| 10 | #include <stdexcept> | ||
| 11 | #include <string> | ||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | namespace terraingen { | ||
| 15 | |||
| 16 | namespace { | ||
| 17 | |||
| 18 | /// Checks a whole-body request: a terrain on a body, at least one cell per face edge and a finite | ||
| 19 | /// spacing that is not negative. | ||
| 20 | /// @param what The result, starting the messages, such as "A body mesh". | ||
| 21 | /// @param cells The cells (or ICQ intervals) per face edge, and how they are called. | ||
| 22 | 39 | void CheckBodyRequest(const Terrain& terrain, std::size_t cells, double spacing, | |
| 23 | const std::string& what, const std::string& cellsNeeded) { | ||
| 24 | 39 | if (!terrain.Surface().IsBody()) { | |
| 25 | 6 | throw std::invalid_argument(what + " needs a terrain on a body"); | |
| 26 | } | ||
| 27 | 36 | if (cells == 0) { | |
| 28 | 4 | throw std::invalid_argument(what + " needs " + cellsNeeded); | |
| 29 | } | ||
| 30 | 34 | RequireNonNegative(spacing, "Sample spacing"); | |
| 31 | 32 | } | |
| 32 | |||
| 33 | /// The body-fixed positions along `directions`: the body's radius in each direction plus the | ||
| 34 | /// terrain's height there, on the datum where the terrain has no data. | ||
| 35 | 20 | std::vector<Vec3d> BodyPositions(const Terrain& terrain, Span<const Vec3d> directions, | |
| 36 | double spacing, unsigned threads) { | ||
| 37 | 20 | const Surface& surface = terrain.Surface(); | |
| 38 | 20 | std::vector<Vec2d> points(directions.size()); | |
| 39 | 802796 | for (std::size_t k = 0; k < directions.size(); ++k) { | |
| 40 | 802776 | points[k] = surface.Coordinates(directions[k]); | |
| 41 | } | ||
| 42 | 20 | std::vector<float> heights(points.size()); | |
| 43 | 20 | terrain.HeightsAt(points, heights, spacing, threads); | |
| 44 | 20 | std::vector<Vec3d> positions(directions.size()); | |
| 45 | 802796 | for (std::size_t k = 0; k < directions.size(); ++k) { | |
| 46 | 802776 | const double r = surface.RadiusAt(points[k]) + | |
| 47 | 802776 | (std::isnan(heights[k]) ? 0.0 : static_cast<double>(heights[k])); | |
| 48 | 802776 | positions[k] = directions[k] * r; | |
| 49 | } | ||
| 50 | 20 | return positions; | |
| 51 | 20 | } | |
| 52 | |||
| 53 | } // namespace | ||
| 54 | |||
| 55 | 16 | TriangleMesh BodyMesh(const Terrain& terrain, std::size_t cellsPerFace, double spacing, | |
| 56 | unsigned threads) { | ||
| 57 | 32 | CheckBodyRequest(terrain, cellsPerFace, spacing, "A body mesh", "at least one cell per face"); | |
| 58 | 24 | return IcqToMesh(BodyIcq(terrain, cellsPerFace, spacing, threads)); | |
| 59 | } | ||
| 60 | |||
| 61 | 23 | IcqModel BodyIcq(const Terrain& terrain, std::size_t q, double spacing, unsigned threads) { | |
| 62 | 46 | CheckBodyRequest(terrain, q, spacing, "An ICQ model", "Q > 0"); | |
| 63 | 20 | const double sampled = | |
| 64 | 20 | spacing > 0.0 ? spacing : terrain.Surface().Radius() * kPi / 2.0 / static_cast<double>(q); | |
| 65 | 20 | IcqModel model; | |
| 66 | 20 | model.q = q; | |
| 67 | 20 | std::vector<Vec3d> directions(6 * (q + 1) * (q + 1)); | |
| 68 | 20 | ForEachIcqLabel(q, [&](std::size_t i, std::size_t j, std::size_t face, std::size_t k) { | |
| 69 | 802776 | directions[k] = IcqDirection(q, i, j, face); | |
| 70 | 802776 | }); | |
| 71 | 20 | model.vertices = BodyPositions(terrain, directions, sampled, threads); | |
| 72 | 20 | return model; | |
| 73 | 20 | } | |
| 74 | |||
| 75 | } // namespace terraingen | ||
| 76 |