include/terraingen/mesh/mesh.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Triangle meshes, their measures, normals and rigid transforms. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/geometry/geometry.h" | ||
| 8 | |||
| 9 | #include <array> | ||
| 10 | #include <cstdint> | ||
| 11 | #include <string_view> | ||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | namespace terraingen { | ||
| 15 | |||
| 16 | /// A triangle mesh of shared vertices; triangles are counter-clockwise seen from outside. | ||
| 17 | 66038 | struct TriangleMesh { | |
| 18 | std::vector<Vec3d> positions; ///< Vertex positions. | ||
| 19 | std::vector<std::array<std::uint32_t, 3>> triangles; ///< Indices into `positions`. | ||
| 20 | }; | ||
| 21 | |||
| 22 | /// An axis-aligned box. | ||
| 23 | struct Box3d { | ||
| 24 | Vec3d min; ///< Lowest corner. | ||
| 25 | Vec3d max; ///< Highest corner. | ||
| 26 | |||
| 27 | /// The box's size along each axis, `max` - `min`. | ||
| 28 | 3657 | Vec3d Extents() const { | |
| 29 | 3657 | return max - min; | |
| 30 | } | ||
| 31 | }; | ||
| 32 | |||
| 33 | /// The normal of the counter-clockwise triangle `a`, `b`, `c`, with a length of twice its area. | ||
| 34 | 566913 | inline Vec3d TriangleAreaNormal(const Vec3d& a, const Vec3d& b, const Vec3d& c) { | |
| 35 | 566913 | return Cross(b - a, c - a); | |
| 36 | } | ||
| 37 | |||
| 38 | /// The smallest axis-aligned box around a mesh's vertices. | ||
| 39 | /// @throws std::invalid_argument for a mesh without vertices. | ||
| 40 | Box3d Bounds(const TriangleMesh& mesh); | ||
| 41 | |||
| 42 | /// Checks that every triangle indexes one of the mesh's vertices. | ||
| 43 | /// @throws std::invalid_argument for a triangle indexing past the vertices. | ||
| 44 | void ValidateMesh(const TriangleMesh& mesh); | ||
| 45 | |||
| 46 | /// The unit normal of each vertex: the area-weighted mean of its triangles' normals. | ||
| 47 | /// @throws std::invalid_argument as `ValidateMesh`. | ||
| 48 | std::vector<Vec3d> VertexNormals(const TriangleMesh& mesh); | ||
| 49 | |||
| 50 | /// A mesh with per-vertex normals, its vertices split along creases so fracture faces keep | ||
| 51 | /// hard edges while noisy surfaces stay smooth. | ||
| 52 | 246 | struct ShadedMesh { | |
| 53 | TriangleMesh mesh; ///< The mesh, vertices split along creases. | ||
| 54 | std::vector<Vec3d> normals; ///< One per vertex of `mesh`. | ||
| 55 | }; | ||
| 56 | |||
| 57 | /// A mesh with its vertices split along creases and normals averaged, weighted by area, | ||
| 58 | /// over the triangles that meet within the crease angle. | ||
| 59 | /// @param creaseAngle The largest angle between triangle normals that is smoothed, in radians. | ||
| 60 | /// @throws std::invalid_argument for a negative or non-finite `creaseAngle`, or as | ||
| 61 | /// `ValidateMesh`. | ||
| 62 | ShadedMesh WithCreases(const TriangleMesh& mesh, double creaseAngle); | ||
| 63 | |||
| 64 | /// A rotation followed by a translation. | ||
| 65 | struct RigidTransform { | ||
| 66 | /// Row-major 3 x 3. | ||
| 67 | std::array<double, 9> rotation{1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}; | ||
| 68 | /// Applied after the rotation. | ||
| 69 | Vec3d translation{}; | ||
| 70 | |||
| 71 | /// The point rotated, then translated. | ||
| 72 | Vec3d Apply(const Vec3d& point) const; | ||
| 73 | /// The vector rotated, without the translation. | ||
| 74 | Vec3d Rotate(const Vec3d& vector) const; | ||
| 75 | /// The transform that applies `first`, then this one. | ||
| 76 | RigidTransform After(const RigidTransform& first) const; | ||
| 77 | |||
| 78 | /// A rotation by `angle` radians about x, counter-clockwise looking down the axis. | ||
| 79 | static RigidTransform RotationX(double angle); | ||
| 80 | /// A rotation by `angle` radians about y, counter-clockwise looking down the axis. | ||
| 81 | static RigidTransform RotationY(double angle); | ||
| 82 | /// A rotation by `angle` radians about z, counter-clockwise looking down the axis. | ||
| 83 | static RigidTransform RotationZ(double angle); | ||
| 84 | /// A pure translation by `offset`. | ||
| 85 | static RigidTransform Translation(const Vec3d& offset); | ||
| 86 | }; | ||
| 87 | |||
| 88 | /// Checks that a matrix is a proper rotation: orthonormal to within 1e-9, without a | ||
| 89 | /// reflection. | ||
| 90 | /// @param rotation Row-major 3 x 3. | ||
| 91 | /// @throws std::invalid_argument otherwise. | ||
| 92 | void RequireRotation(const std::array<double, 9>& rotation); | ||
| 93 | |||
| 94 | /// Checks that each per-axis scale factor is finite and not zero. | ||
| 95 | /// @param what What the factors are, as error messages name them, such as "Node scales". | ||
| 96 | /// @throws std::invalid_argument otherwise. | ||
| 97 | void RequireScaleFactors(const Vec3d& scale, std::string_view what); | ||
| 98 | |||
| 99 | /// A mesh with every vertex scaled about the origin per axis; triangles flip when an odd | ||
| 100 | /// number of the factors are negative, so the mesh stays outward-facing. | ||
| 101 | /// @param scale The factor per axis, nonzero and finite. | ||
| 102 | /// @throws std::invalid_argument for a zero or non-finite factor. | ||
| 103 | TriangleMesh Scaled(const TriangleMesh& mesh, const Vec3d& scale); | ||
| 104 | |||
| 105 | /// Cuts `mesh` flat at a plane: triangles crossing it are split along it, sharing the new points | ||
| 106 | /// between neighbors so the mesh stays closed, and points above it are moved down onto it. | ||
| 107 | /// @param normal The plane's unit normal, pointing to the side cut away. | ||
| 108 | /// @param distances Each vertex's signed distance along `normal` from the plane, positive above. | ||
| 109 | void CutAtPlane(TriangleMesh& mesh, const Vec3d& normal, std::vector<double> distances); | ||
| 110 | |||
| 111 | } // namespace terraingen | ||
| 112 |