include/terraingen/mesh/mesh_build.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief `BuildMesh`, a grid of heights as a triangle mesh, and `FillMesh` into caller buffers. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/geometry/geometry.h" | ||
| 8 | #include "terraingen/geometry/surface.h" | ||
| 9 | #include "terraingen/mesh/mesh.h" | ||
| 10 | #include "terraingen/raster/raster.h" | ||
| 11 | #include "terraingen/util/span.h" | ||
| 12 | |||
| 13 | #include <cstddef> | ||
| 14 | #include <cstdint> | ||
| 15 | #include <vector> | ||
| 16 | |||
| 17 | namespace terraingen { | ||
| 18 | |||
| 19 | /// What a `BuildMesh` leaves out and hangs below its edges. | ||
| 20 | 102 | struct BuildMeshOptions { | |
| 21 | /// Boxes in surface coordinates whose squares are left out: a square when all four of its | ||
| 22 | /// cell centers lie in one, edges included. | ||
| 23 | std::vector<Bounds2d> cutouts{}; | ||
| 24 | /// Depth in meters of a skirt hung below every boundary edge, around cutouts too; 0 for none. | ||
| 25 | double skirt = 0.0; | ||
| 26 | }; | ||
| 27 | |||
| 28 | /// The number of vertices and triangles of a mesh. | ||
| 29 | 81 | struct MeshSize { | |
| 30 | std::size_t vertices = 0; ///< Vertices, skirt vertices included. | ||
| 31 | std::size_t triangles = 0; ///< Triangles, skirt triangles included. | ||
| 32 | |||
| 33 | /// Memberwise equality. | ||
| 34 | 12 | bool operator==(const MeshSize& other) const { | |
| 35 | 12 | return vertices == other.vertices && triangles == other.triangles; | |
| 36 | } | ||
| 37 | /// Memberwise inequality. | ||
| 38 | 1 | bool operator!=(const MeshSize& other) const { | |
| 39 | 2 | return !(*this == other); | |
| 40 | } | ||
| 41 | }; | ||
| 42 | |||
| 43 | /// A grid of heights as a mesh: a vertex at every cell center with data, and two triangles facing | ||
| 44 | /// up for every square of four such vertices outside the cutouts, with a skirt facing out below | ||
| 45 | /// each boundary edge. Positions are in the frame boulder models are written in: world | ||
| 46 | /// coordinates and heights on a plane; on a sphere, meters east, north and up of `site` on the | ||
| 47 | /// datum sphere (as `PlacementAtSite`), so terrain and boulders line up. Skirts hang down the | ||
| 48 | /// frame's up axis. Made with `GetMeshSize` and `FillMesh`. | ||
| 49 | /// @param site Surface coordinates of the site; unused on a plane. | ||
| 50 | /// @throws std::invalid_argument as `FillMesh`. | ||
| 51 | TriangleMesh BuildMesh(const Raster<float>& heights, const Surface& surface = {}, | ||
| 52 | const Vec2d& site = {}, const BuildMeshOptions& options = {}); | ||
| 53 | |||
| 54 | /// The number of vertices and triangles `BuildMesh` makes of `heights`: cells with data, squares | ||
| 55 | /// outside the cutouts and the skirt. | ||
| 56 | /// @throws std::invalid_argument for a grid with fewer than four cells with data in a square | ||
| 57 | /// outside the cutouts, or a negative or non-finite skirt. | ||
| 58 | MeshSize GetMeshSize(const Raster<float>& heights, const BuildMeshOptions& options = {}); | ||
| 59 | |||
| 60 | /// Writes the mesh `BuildMesh` makes of `heights` into caller buffers, bit for bit: `positions` | ||
| 61 | /// as x, y, z triples and `triangles` as vertex index triples, in `BuildMesh`'s order. The layout | ||
| 62 | /// matches an array of Chrono's ChVector3d and ChVector3i, or NumPy arrays of shape (N, 3) float64 | ||
| 63 | /// and (M, 3) int32, so callers resize those to `GetMeshSize` and pass their data with no copy. | ||
| 64 | /// @param positions 3 * `GetMeshSize(heights, options).vertices` doubles, in meters. | ||
| 65 | /// @param triangles 3 * `GetMeshSize(heights, options).triangles` indices into the vertices. | ||
| 66 | /// @param site Surface coordinates of the site; unused on a plane. | ||
| 67 | /// @throws std::invalid_argument as `GetMeshSize`, for a buffer of another size, or for a mesh of | ||
| 68 | /// more than INT32_MAX vertices. | ||
| 69 | void FillMesh(const Raster<float>& heights, Span<double> positions, Span<std::int32_t> triangles, | ||
| 70 | const Surface& surface = {}, const Vec2d& site = {}, | ||
| 71 | const BuildMeshOptions& options = {}); | ||
| 72 | |||
| 73 | } // namespace terraingen | ||
| 74 |