include/terraingen/layers/boulders/boulder_models.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Boulders as 3D models: placing unit shapes on the ground, and their meshes. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/geometry/surface.h" | ||
| 8 | #include "terraingen/layers/boulders/boulder.h" | ||
| 9 | #include "terraingen/layers/boulders/boulder_shape.h" | ||
| 10 | #include "terraingen/mesh/mesh.h" | ||
| 11 | |||
| 12 | #include <array> | ||
| 13 | #include <cmath> | ||
| 14 | #include <cstdint> | ||
| 15 | #include <limits> | ||
| 16 | #include <memory> | ||
| 17 | #include <optional> | ||
| 18 | #include <utility> | ||
| 19 | #include <vector> | ||
| 20 | |||
| 21 | namespace terraingen { | ||
| 22 | |||
| 23 | /// How a unit `BoulderShape` becomes a boulder in the world: scaled per axis by `scale`, | ||
| 24 | /// then moved by `pose`. | ||
| 25 | struct BoulderPlacement { | ||
| 26 | Vec3d scale{1.0, 1.0, 1.0}; ///< Scale along the shape's x, y and z, in meters. | ||
| 27 | RigidTransform pose{}; ///< Rotation and translation after scaling. | ||
| 28 | |||
| 29 | /// Where point `p` of the unit shape lands in the world. | ||
| 30 | 26814960 | Vec3d Apply(const Vec3d& p) const { | |
| 31 | 26814960 | return pose.Apply({p.x * scale.x, p.y * scale.y, p.z * scale.z}); | |
| 32 | } | ||
| 33 | |||
| 34 | /// The point of the unit shape that lands at world point `p`. | ||
| 35 | 720 | Vec3d Unapply(const Vec3d& p) const { | |
| 36 | 720 | const std::array<double, 9>& r = pose.rotation; | |
| 37 | 720 | const Vec3d d = p - pose.translation; | |
| 38 | 720 | return {(r[0] * d.x + r[3] * d.y + r[6] * d.z) / scale.x, | |
| 39 | 720 | (r[1] * d.x + r[4] * d.y + r[7] * d.z) / scale.y, | |
| 40 | 720 | (r[2] * d.x + r[5] * d.y + r[8] * d.z) / scale.z}; | |
| 41 | } | ||
| 42 | }; | ||
| 43 | |||
| 44 | /// `mesh`, a unit shape's mesh, with every point moved by `placement`. | ||
| 45 | TriangleMesh PlaceMesh(TriangleMesh mesh, const BoulderPlacement& placement); | ||
| 46 | |||
| 47 | /// The lowest and highest z of `points` once mapped by `map`. | ||
| 48 | template <typename Map> | ||
| 49 | 85039 | std::pair<double, double> ZRange(const std::vector<Vec3d>& points, const Map& map) { | |
| 50 | 85039 | double low = std::numeric_limits<double>::infinity(); | |
| 51 | 85039 | double high = -low; | |
| 52 | 40771960 | for (const Vec3d& p : points) { | |
| 53 | 40686921 | const double z = map(p).z; | |
| 54 | 40686921 | low = std::fmin(low, z); | |
| 55 | 40686921 | high = std::fmax(high, z); | |
| 56 | } | ||
| 57 | 85039 | return {low, high}; | |
| 58 | } | ||
| 59 | |||
| 60 | /// A boulder placed as a 3D model of its own. | ||
| 61 | 4527 | struct BoulderModel { | |
| 62 | Boulder boulder; ///< Its record. | ||
| 63 | std::uint64_t shapeSeed = 0; ///< The seed of its shape, or of its parent's for a fragment. | ||
| 64 | std::shared_ptr<const BoulderShape> shape; ///< Its unit shape. | ||
| 65 | BoulderPlacement placement; ///< The shape moved onto the ground beneath it. | ||
| 66 | }; | ||
| 67 | |||
| 68 | /// The height at world (x, y) of the highest upward-facing triangle of `placed` (a mesh in world | ||
| 69 | /// coordinates), or nothing outside its footprint. | ||
| 70 | std::optional<double> TopSurface(const TriangleMesh& placed, double x, double y); | ||
| 71 | |||
| 72 | /// A boulder's placement moved from its own local frame into the local frame of a site. | ||
| 73 | /// | ||
| 74 | /// On a sphere the site frame is east, north and up at `site` on the datum sphere, keeping a scene | ||
| 75 | /// about a site in meter-scale coordinates; on a plane `placement` is returned unchanged. | ||
| 76 | /// @param placement In the boulder's local frame (east, north, up above datum). | ||
| 77 | /// @param boulder Surface coordinates of the boulder. | ||
| 78 | /// @param site Surface coordinates of the site. | ||
| 79 | BoulderPlacement PlacementAtSite(const BoulderPlacement& placement, const Vec2d& boulder, | ||
| 80 | const Vec2d& site, const Surface& surface); | ||
| 81 | |||
| 82 | /// The rotation tipping the vertical by `tilt` radians toward `azimuth` (radians counter-clockwise | ||
| 83 | /// from east), about the horizontal axis square to that direction. | ||
| 84 | RigidTransform TiltRotation(double tilt, double azimuth); | ||
| 85 | |||
| 86 | /// The placement of a shape as a boulder standing on the ground. | ||
| 87 | /// | ||
| 88 | /// The long axis is scaled to the diameter (the intermediate in proportion) and the short axis to | ||
| 89 | /// the height; then stood on the axis `rest` names, leaned by `lean` about the long axis, turned by | ||
| 90 | /// `yaw`, tipped by `tilt` toward `tiltAzimuth`, and set at the boulder's position with `burial` | ||
| 91 | /// of its vertical extent below `groundHeight`. With `settle`, before the tilt it is tipped over | ||
| 92 | /// on level ground, from that pose, until it rests stably with its center of mass above the face | ||
| 93 | /// or edges it stands on, so no shape is left balanced on a corner. | ||
| 94 | /// @param groundHeight Height of the ground under its center, in meters. | ||
| 95 | BoulderPlacement PlaceBoulderModel(const Boulder& boulder, const BoulderShape& shape, | ||
| 96 | double groundHeight, bool settle = true); | ||
| 97 | |||
| 98 | /// The seed of `boulder`'s shape, or of its parent's whole shape for a fragment: its `shapeSeed` | ||
| 99 | /// when set, else `BoulderShapeSeed` of its ID, or its parent's. | ||
| 100 | /// @param prototypes Number of shared shapes, or 0 for a shape per boulder. | ||
| 101 | std::uint64_t ShapeSeed(const Boulder& boulder, std::uint32_t prototypes = 0); | ||
| 102 | |||
| 103 | } // namespace terraingen | ||
| 104 |