src/layers/boulders/boulder_models.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/boulders/boulder_models.h" | ||
| 4 | |||
| 5 | #include "terraingen/util/bits.h" | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <array> | ||
| 9 | #include <cmath> | ||
| 10 | #include <limits> | ||
| 11 | #include <utility> | ||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | namespace terraingen { | ||
| 15 | |||
| 16 | namespace { | ||
| 17 | |||
| 18 | /// Axis `index` of `frame`: 0 east, 1 north, 2 up. | ||
| 19 | 1098 | const Vec3d& Axis(const LocalFrame& frame, int index) { | |
| 20 | 1098 | return index == 0 ? frame.east : (index == 1 ? frame.north : frame.up); | |
| 21 | } | ||
| 22 | |||
| 23 | /// The rotation turning the shape's axis that `rest` names to the vertical. | ||
| 24 | 17079 | RigidTransform RestRotation(BoulderRest rest) { | |
| 25 | 17079 | switch (rest) { | |
| 26 | 608 | case BoulderRest::Side: | |
| 27 | 608 | return RigidTransform::RotationX(0.5 * kPi); | |
| 28 | 50 | case BoulderRest::End: | |
| 29 | 50 | return RigidTransform::RotationY(-0.5 * kPi); | |
| 30 | case BoulderRest::Face: | ||
| 31 | break; | ||
| 32 | } | ||
| 33 | 16421 | return {}; | |
| 34 | } | ||
| 35 | |||
| 36 | /// The center of mass of a closed mesh of uniform density, from its signed tetrahedra. | ||
| 37 | 8511 | Vec3d MassCenter(const std::vector<Vec3d>& points, | |
| 38 | const std::vector<std::array<std::uint32_t, 3>>& triangles) { | ||
| 39 | 8511 | Vec3d sum{}; | |
| 40 | 8511 | double volume = 0.0; | |
| 41 | 6991325 | for (const auto& t : triangles) { | |
| 42 | 6982814 | const Vec3d& a = points[t[0]]; | |
| 43 | 6982814 | const Vec3d& b = points[t[1]]; | |
| 44 | 6982814 | const Vec3d& c = points[t[2]]; | |
| 45 | 6982814 | const double v = Dot(a, Cross(b, c)); | |
| 46 | 6982814 | sum = sum + (a + b + c) * v; | |
| 47 | 6982814 | volume += v; | |
| 48 | } | ||
| 49 | 8511 | return sum * (1.0 / (4.0 * volume)); | |
| 50 | } | ||
| 51 | |||
| 52 | /// The convex hull of points in the plane, counter-clockwise, without collinear points. | ||
| 53 | 51837 | std::vector<Vec2d> Hull2d(std::vector<Vec2d> points) { | |
| 54 | 51837 | std::sort(points.begin(), points.end(), [](const Vec2d& a, const Vec2d& b) { | |
| 55 | 117215 | return a.x < b.x || (a.x == b.x && a.y < b.y); | |
| 56 | }); | ||
| 57 | 51838 | if (points.size() < 3) { | |
| 58 | 25757 | return points; | |
| 59 | } | ||
| 60 | 78289 | const auto turn = [](const Vec2d& o, const Vec2d& a, const Vec2d& b) { | |
| 61 | 52208 | return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); | |
| 62 | }; | ||
| 63 | 26081 | std::vector<Vec2d> hull(2 * points.size()); | |
| 64 | 26081 | std::size_t k = 0; | |
| 65 | 104340 | for (std::size_t i = 0; i < points.size(); ++i) { | |
| 66 | 91298 | while (k >= 2 && turn(hull[k - 2], hull[k - 1], points[i]) <= 0.0) { | |
| 67 | --k; | ||
| 68 | } | ||
| 69 | 78259 | hull[k++] = points[i]; | |
| 70 | } | ||
| 71 | 78257 | for (std::size_t i = points.size() - 1, lower = k + 1; i-- > 0;) { | |
| 72 | 65233 | while (k >= lower && turn(hull[k - 2], hull[k - 1], points[i]) <= 0.0) { | |
| 73 | --k; | ||
| 74 | } | ||
| 75 | 52176 | hull[k++] = points[i]; | |
| 76 | } | ||
| 77 | 26081 | hull.resize(k - 1); | |
| 78 | 26081 | return hull; | |
| 79 | 26081 | } | |
| 80 | |||
| 81 | /// The point of `hull`'s boundary nearest `p`, and whether `p` lies inside a hull of 3 or more | ||
| 82 | /// points. | ||
| 83 | 51839 | std::pair<Vec2d, bool> NearestOnHull(const std::vector<Vec2d>& hull, const Vec2d& p) { | |
| 84 | 51839 | if (hull.size() == 1) { | |
| 85 | 8495 | return {hull[0], false}; | |
| 86 | } | ||
| 87 | 43344 | bool inside = hull.size() >= 3; | |
| 88 | 43344 | Vec2d best = hull[0]; | |
| 89 | 43344 | double bestDistance = std::numeric_limits<double>::infinity(); | |
| 90 | 156129 | for (std::size_t i = 0; i < hull.size(); ++i) { | |
| 91 | 112785 | const Vec2d& a = hull[i]; | |
| 92 | 112785 | const Vec2d& b = hull[(i + 1) % hull.size()]; | |
| 93 | 112785 | const double ex = b.x - a.x; | |
| 94 | 112785 | const double ey = b.y - a.y; | |
| 95 | 112785 | if (ex * (p.y - a.y) - ey * (p.x - a.x) < 0.0) { | |
| 96 | 38635 | inside = false; | |
| 97 | } | ||
| 98 | 112785 | const double t = | |
| 99 | 112785 | std::clamp(((p.x - a.x) * ex + (p.y - a.y) * ey) / (ex * ex + ey * ey), 0.0, 1.0); | |
| 100 | 112785 | const Vec2d q{a.x + t * ex, a.y + t * ey}; | |
| 101 | 112785 | const double d = std::hypot(p.x - q.x, p.y - q.y); | |
| 102 | 112785 | if (d < bestDistance) { | |
| 103 | 66918 | bestDistance = d; | |
| 104 | 66918 | best = q; | |
| 105 | } | ||
| 106 | } | ||
| 107 | 43344 | return {best, inside}; | |
| 108 | } | ||
| 109 | |||
| 110 | /// One tip of a settling shape: a turn by `angle` radians about the horizontal axis `axis` | ||
| 111 | /// through `origin`, the side `toward` (a horizontal unit vector square to it) going down. | ||
| 112 | struct PivotStep { | ||
| 113 | Vec3d origin; | ||
| 114 | Vec3d axis; | ||
| 115 | Vec3d toward; | ||
| 116 | double cs = 1.0; | ||
| 117 | double sn = 0.0; | ||
| 118 | |||
| 119 | /// Where the step turns `p`. | ||
| 120 | 20134438 | Vec3d Turn(const Vec3d& p) const { | |
| 121 | 20134438 | const Vec3d up{0.0, 0.0, 1.0}; | |
| 122 | 20134438 | const Vec3d v = p - origin; | |
| 123 | 20134438 | const double u = Dot(v, toward); | |
| 124 | 20134438 | const double w = v.z; | |
| 125 | 20134438 | return origin + axis * Dot(v, axis) + toward * (u * cs + w * sn) + up * (w * cs - u * sn); | |
| 126 | } | ||
| 127 | |||
| 128 | /// The step's rotation as a row-major matrix. | ||
| 129 | 43328 | std::array<double, 9> Rotation() const { | |
| 130 | 43328 | std::array<double, 9> m{}; | |
| 131 | 43328 | const std::array<double, 3> av{axis.x, axis.y, axis.z}; | |
| 132 | 43328 | const std::array<double, 3> dv{toward.x, toward.y, toward.z}; | |
| 133 | 43328 | const std::array<double, 3> zv{0.0, 0.0, 1.0}; | |
| 134 | 173312 | for (std::size_t r = 0; r < 3; ++r) { | |
| 135 | 519936 | for (std::size_t k = 0; k < 3; ++k) { | |
| 136 | 389952 | m[3 * r + k] = av[r] * av[k] + cs * (dv[r] * dv[k] + zv[r] * zv[k]) + | |
| 137 | 389952 | sn * (dv[r] * zv[k] - zv[r] * dv[k]); | |
| 138 | } | ||
| 139 | } | ||
| 140 | 43328 | return m; | |
| 141 | } | ||
| 142 | }; | ||
| 143 | |||
| 144 | /// The next tip of `points` resting on the plane through their lowest point toward `center`, | ||
| 145 | /// about the edge or corner of the contact nearest it, until another point touches; none once | ||
| 146 | /// the center stands over the contact, within `tolerance`. | ||
| 147 | 51839 | std::optional<PivotStep> NextPivot(const std::vector<Vec3d>& points, const Vec3d& center, | |
| 148 | double tolerance) { | ||
| 149 | 23697451 | const double low = ZRange(points, [](const Vec3d& p) { return p; }).first; | |
| 150 | 51839 | std::vector<Vec2d> contact; | |
| 151 | 23778359 | for (const Vec3d& p : points) { | |
| 152 | 23726520 | if (p.z <= low + tolerance) { | |
| 153 | 121277 | contact.push_back({p.x, p.y}); | |
| 154 | } | ||
| 155 | } | ||
| 156 | 51839 | const Vec2d c{center.x, center.y}; | |
| 157 | 51839 | const auto [pivot, inside] = NearestOnHull(Hull2d(contact), c); | |
| 158 | 51839 | const double distance = std::hypot(c.x - pivot.x, c.y - pivot.y); | |
| 159 | 51839 | if (inside || distance <= tolerance) { | |
| 160 | 8511 | return std::nullopt; | |
| 161 | } | ||
| 162 | 43328 | PivotStep step; | |
| 163 | 43328 | step.toward = {(c.x - pivot.x) / distance, (c.y - pivot.y) / distance, 0.0}; | |
| 164 | 43328 | step.axis = {-step.toward.y, step.toward.x, 0.0}; | |
| 165 | 43328 | step.origin = {pivot.x, pivot.y, low}; | |
| 166 | 43328 | double rise = 0.0; | |
| 167 | 43328 | double run = 0.0; | |
| 168 | 20226100 | for (const Vec3d& p : points) { | |
| 169 | 20182772 | const double u = Dot(p - step.origin, step.toward); | |
| 170 | 20182772 | if (u > tolerance && (run == 0.0 || (p.z - low) * run < rise * u)) { | |
| 171 | 232786 | rise = p.z - low; | |
| 172 | 232786 | run = u; | |
| 173 | } | ||
| 174 | } | ||
| 175 | 43328 | if (run == 0.0) { | |
| 176 | ✗ | return std::nullopt; | |
| 177 | } | ||
| 178 | 43328 | const double angle = std::atan2(rise, run); | |
| 179 | 43328 | step.cs = std::cos(angle); | |
| 180 | 43328 | step.sn = std::sin(angle); | |
| 181 | 43328 | return step; | |
| 182 | 51839 | } | |
| 183 | |||
| 184 | /// The rotation that tips `points` over on a level plane until they rest: each step pivots about | ||
| 185 | /// the edge or corner of the ground contact nearest the center of mass, toward it, until another | ||
| 186 | /// point touches, and stops once the center of mass stands over the contact. | ||
| 187 | 8511 | std::array<double, 9> SettleRotation(std::vector<Vec3d> points, Vec3d center) { | |
| 188 | 8511 | RigidTransform total; | |
| 189 | 8511 | double extent = 0.0; | |
| 190 | 3521990 | for (const Vec3d& p : points) { | |
| 191 | 3513479 | extent = | |
| 192 | 3513479 | std::fmax(extent, std::fmax(std::fabs(p.x), std::fmax(std::fabs(p.y), std::fabs(p.z)))); | |
| 193 | } | ||
| 194 | 8511 | const double tolerance = 1e-7 * extent; | |
| 195 | 51839 | for (int n = 0; n < 64; ++n) { | |
| 196 | 51839 | const std::optional<PivotStep> step = NextPivot(points, center, tolerance); | |
| 197 | 51838 | if (!step) { | |
| 198 | break; | ||
| 199 | } | ||
| 200 | 20134689 | for (Vec3d& p : points) { | |
| 201 | 20091362 | p = step->Turn(p); | |
| 202 | } | ||
| 203 | 43327 | center = step->Turn(center); | |
| 204 | 43327 | RigidTransform turn; | |
| 205 | 43327 | turn.rotation = step->Rotation(); | |
| 206 | 43327 | total = turn.After(total); | |
| 207 | } | ||
| 208 | 8511 | return total.rotation; | |
| 209 | } | ||
| 210 | |||
| 211 | } // namespace | ||
| 212 | |||
| 213 | 2450 | RigidTransform TiltRotation(double tilt, double azimuth) { | |
| 214 | 2450 | const double ax = -std::sin(azimuth); | |
| 215 | 2450 | const double ay = std::cos(azimuth); | |
| 216 | 2450 | const double c = std::cos(tilt); | |
| 217 | 2450 | const double s = std::sin(tilt); | |
| 218 | 2450 | const double t = 1.0 - c; | |
| 219 | 2450 | RigidTransform turn; | |
| 220 | 2450 | turn.rotation = {c + t * ax * ax, t * ax * ay, s * ay, t * ax * ay, c + t * ay * ay, -s * ax, | |
| 221 | -s * ay, s * ax, c}; | ||
| 222 | 2450 | return turn; | |
| 223 | } | ||
| 224 | |||
| 225 | 311 | BoulderPlacement PlacementAtSite(const BoulderPlacement& placement, const Vec2d& boulder, | |
| 226 | const Vec2d& site, const Surface& surface) { | ||
| 227 | 311 | if (!surface.IsBody()) { | |
| 228 | 250 | return placement; | |
| 229 | } | ||
| 230 | 61 | const LocalFrame from = surface.Frame(boulder); | |
| 231 | 61 | const LocalFrame to = surface.Frame(site); | |
| 232 | 61 | std::array<double, 9> local{}; | |
| 233 | 244 | for (int r = 0; r < 3; ++r) { | |
| 234 | 732 | for (int c = 0; c < 3; ++c) { | |
| 235 | 549 | local[static_cast<std::size_t>(3 * r + c)] = Dot(Axis(to, r), Axis(from, c)); | |
| 236 | } | ||
| 237 | } | ||
| 238 | 61 | RigidTransform toSite; | |
| 239 | 61 | toSite.rotation = local; | |
| 240 | 61 | toSite.translation = to.Components(surface.Position(boulder) - surface.Position(site)); | |
| 241 | 61 | BoulderPlacement moved = placement; | |
| 242 | 61 | moved.pose = toSite.After(placement.pose); | |
| 243 | 61 | return moved; | |
| 244 | } | ||
| 245 | |||
| 246 | 17079 | BoulderPlacement PlaceBoulderModel(const Boulder& boulder, const BoulderShape& shape, | |
| 247 | double groundHeight, bool settle) { | ||
| 248 | 17079 | BoulderPlacement placement; | |
| 249 | 17079 | placement.scale = {boulder.diameter, boulder.diameter, boulder.height / shape.shortAxis}; | |
| 250 | 17079 | placement.pose = RigidTransform::RotationZ(boulder.yaw) | |
| 251 | 17079 | .After(RigidTransform::RotationX(boulder.lean)) | |
| 252 | 17079 | .After(RestRotation(boulder.rest)); | |
| 253 | 17079 | if (settle && !shape.mesh.triangles.empty()) { | |
| 254 | 17022 | const std::vector<Vec3d> points = PlaceMesh(shape.mesh, placement).positions; | |
| 255 | 8511 | RigidTransform rest; | |
| 256 | 8511 | rest.rotation = SettleRotation(points, MassCenter(points, shape.mesh.triangles)); | |
| 257 | 8511 | placement.pose = rest.After(placement.pose); | |
| 258 | 8511 | } | |
| 259 | 17079 | if (boulder.tilt != 0.0) { | |
| 260 | 1292 | placement.pose = TiltRotation(boulder.tilt, boulder.tiltAzimuth).After(placement.pose); | |
| 261 | } | ||
| 262 | 34158 | const auto [low, high] = | |
| 263 | 9021539 | ZRange(shape.mesh.positions, [&](const Vec3d& p) { return placement.Apply(p); }); | |
| 264 | 17079 | placement.pose.translation = {boulder.x, boulder.y, | |
| 265 | 17079 | groundHeight - boulder.burial * (high - low) - low}; | |
| 266 | 17079 | return placement; | |
| 267 | } | ||
| 268 | |||
| 269 | 122790 | std::optional<double> TopSurface(const TriangleMesh& placed, double x, double y) { | |
| 270 | 122790 | std::optional<double> top; | |
| 271 | 53465248 | for (const auto& t : placed.triangles) { | |
| 272 | 53342458 | const Vec3d& a = placed.positions[t[0]]; | |
| 273 | 53342458 | const Vec3d& b = placed.positions[t[1]]; | |
| 274 | 53342458 | const Vec3d& c = placed.positions[t[2]]; | |
| 275 | 53342458 | const double area = (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y); | |
| 276 | 53342458 | if (!(area > 0.0)) { | |
| 277 | 53342458 | continue; | |
| 278 | } | ||
| 279 | 34646017 | const double u = ((b.x - x) * (c.y - y) - (c.x - x) * (b.y - y)) / area; | |
| 280 | 34646017 | const double v = ((c.x - x) * (a.y - y) - (a.x - x) * (c.y - y)) / area; | |
| 281 | 34646017 | const double w = 1.0 - u - v; | |
| 282 | 34646017 | if (u < 0.0 || v < 0.0 || w < 0.0) { | |
| 283 | 34626611 | continue; | |
| 284 | } | ||
| 285 | 19406 | const double z = u * a.z + v * b.z + w * c.z; | |
| 286 | 19406 | if (!top || z > *top) { | |
| 287 | 19218 | top = z; | |
| 288 | } | ||
| 289 | } | ||
| 290 | 122790 | return top; | |
| 291 | } | ||
| 292 | |||
| 293 | 22801 | std::uint64_t ShapeSeed(const Boulder& boulder, std::uint32_t prototypes) { | |
| 294 | 22801 | if (boulder.shapeSeed) { | |
| 295 | 9 | return *boulder.shapeSeed; | |
| 296 | } | ||
| 297 | 22792 | return BoulderShapeSeed(boulder.parent != 0 ? boulder.parent : boulder.id, prototypes); | |
| 298 | } | ||
| 299 | |||
| 300 | 19635 | TriangleMesh PlaceMesh(TriangleMesh mesh, const BoulderPlacement& placement) { | |
| 301 | 9745428 | for (Vec3d& p : mesh.positions) { | |
| 302 | 9725793 | p = placement.Apply(p); | |
| 303 | } | ||
| 304 | 19635 | return mesh; | |
| 305 | } | ||
| 306 | |||
| 307 | } // namespace terraingen | ||
| 308 |