scene/schema/planet.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | /// @file planet.cpp | ||
| 3 | /// @brief The planetary prims' registry entries and extents: terrains from their heights and | ||
| 4 | /// bodies, atmospheres from their bodies and tops. | ||
| 5 | #include "ernie/scene/schema/planet.h" | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <optional> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | namespace ernie { | ||
| 12 | namespace { | ||
| 13 | |||
| 14 | /// @return The lowest and highest of a host array's heights; none on a device or when empty. | ||
| 15 | 2 | std::optional<std::pair<double, double>> HeightRange(const Tensor& heights) { | |
| 16 | 2 | if (heights.Empty() || !heights.OnHost()) { | |
| 17 | ✗ | return std::nullopt; | |
| 18 | } | ||
| 19 | 2 | const std::vector<float> values = CopyFloats(heights, "heights"); | |
| 20 | 2 | const auto [low, high] = std::ranges::minmax_element(values); | |
| 21 | 2 | return std::pair<double, double>(*low, *high); | |
| 22 | 2 | } | |
| 23 | |||
| 24 | /// A plane's footprint: x east from the origin, y south from it, a cell between heights. | ||
| 25 | 1 | Box PlaneFootprint(const Tensor& heights, glm::dvec2 origin, double cell, double low, double high) { | |
| 26 | 1 | const double rows = static_cast<double>(heights.Shape()[0]); | |
| 27 | 1 | const double columns = static_cast<double>(heights.Shape()[1]); | |
| 28 | 1 | return {{origin.x, origin.y - (rows - 1.0) * cell, low}, | |
| 29 | 1 | {origin.x + (columns - 1.0) * cell, origin.y, high}}; | |
| 30 | } | ||
| 31 | |||
| 32 | 2 | void TerrainExtent(const ExtentRows& rows, RowRange range, std::span<Box> out) { | |
| 33 | 2 | const auto heights = rows.Column<Tensor>(Terrain::kHeights); | |
| 34 | 2 | const auto origin = rows.Column<glm::dvec2>(Terrain::kOrigin); | |
| 35 | 2 | const auto cell = rows.Column<double>(Terrain::kCellSize); | |
| 36 | 2 | const auto exaggeration = rows.Column<double>(Terrain::kExaggeration); | |
| 37 | 2 | const auto authored = rows.Column<std::vector<float>>(Boundable::kExtent); | |
| 38 | 2 | const auto bodies = rows.Column<Target>(Terrain::kBody); | |
| 39 | 2 | ComputeRows(range, out, [&](std::uint32_t row) { | |
| 40 | 2 | const std::optional<std::pair<double, double>> span = HeightRange(heights[row]); | |
| 41 | 2 | const double low = span ? span->first * exaggeration[row] : 0.0; | |
| 42 | 2 | const double high = span ? span->second * exaggeration[row] : 0.0; | |
| 43 | 2 | if (const std::optional<Box> body = rows.TargetExtent(bodies[row], row)) { | |
| 44 | 1 | return body->Grown(std::max(high, 0.0)); | |
| 45 | } | ||
| 46 | 1 | return span && heights[row].Rank() == 2 | |
| 47 | 2 | ? PlaneFootprint(heights[row], origin[row], cell[row], low, high) | |
| 48 | 1 | : AuthoredExtent(authored[row]); | |
| 49 | }); | ||
| 50 | 2 | } | |
| 51 | |||
| 52 | 4 | void AtmosphereExtent(const ExtentRows& rows, RowRange range, std::span<Box> out) { | |
| 53 | 4 | const auto top = rows.Column<double>(Atmosphere::kTop); | |
| 54 | 4 | const auto bodies = rows.Column<Target>(Atmosphere::kBody); | |
| 55 | 4 | ComputeRows(range, out, [&](std::uint32_t row) { | |
| 56 | 4 | const std::optional<Box> body = rows.TargetExtent(bodies[row], row); | |
| 57 | 4 | return body ? body->Grown(top[row]) : Box(); | |
| 58 | }); | ||
| 59 | 4 | } | |
| 60 | |||
| 61 | } // namespace | ||
| 62 | |||
| 63 | 3946 | ERNIE_DEFINE_SCHEMA(Terrain, Concrete, ComputedFromOtherPrims(TerrainExtent), | |
| 64 | ERNIE_TERRAIN_ATTRIBUTES) | ||
| 65 | 3364 | ERNIE_DEFINE_SCHEMA(Atmosphere, Concrete, ComputedFromOtherPrims(AtmosphereExtent), | |
| 66 | ERNIE_ATMOSPHERE_ATTRIBUTES) | ||
| 67 | 2485 | ERNIE_DEFINE_SCHEMA(StarField, Concrete, kInheritExtent, ERNIE_STAR_FIELD_ATTRIBUTES) | |
| 68 | 1440 | ERNIE_DEFINE_SCHEMA(GroundPlane, Concrete, kUnbounded, ERNIE_GROUND_PLANE_ATTRIBUTES) | |
| 69 | |||
| 70 | } // namespace ernie | ||
| 71 |