src/layers/boulders/boulder_fragments.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/boulders/boulder_fragments.h" | ||
| 4 | |||
| 5 | #include "terraingen/random/stream.h" | ||
| 6 | #include "terraingen/util/bits.h" | ||
| 7 | |||
| 8 | #include <algorithm> | ||
| 9 | #include <cmath> | ||
| 10 | #include <stdexcept> | ||
| 11 | |||
| 12 | namespace terraingen { | ||
| 13 | |||
| 14 | namespace { | ||
| 15 | |||
| 16 | /// The seed points of `pieces` pieces of a shape whose box is `box`: around the middle, a third | ||
| 17 | /// of the way out, a little above or below it so the cracks tilt. | ||
| 18 | 143 | std::vector<Vec3d> SeedPoints(const Box3d& box, int pieces, RandomStream& random) { | |
| 19 | 143 | const Vec3d center = (box.min + box.max) * 0.5; | |
| 20 | 143 | const Vec3d half = (box.max - box.min) * 0.5; | |
| 21 | 143 | const double turn = 2.0 * kPi / pieces; | |
| 22 | 143 | const double start = turn * random.NextDouble(); | |
| 23 | 143 | std::vector<Vec3d> seeds; | |
| 24 | 624 | for (int k = 0; k < pieces; ++k) { | |
| 25 | 481 | const double angle = start + turn * (k + 0.3 * (random.NextDouble() - 0.5)); | |
| 26 | 481 | const double out = 0.35 + 0.15 * random.NextDouble(); | |
| 27 | 481 | const double rise = 0.2 * (random.NextDouble() - 0.5); | |
| 28 | 481 | seeds.push_back(center + Vec3d{out * half.x * std::cos(angle), | |
| 29 | 481 | out * half.y * std::sin(angle), rise * half.z}); | |
| 30 | } | ||
| 31 | 143 | return seeds; | |
| 32 | } | ||
| 33 | |||
| 34 | /// The seed points of `pieces` slabs of a shape whose box is `box`: spread along one horizontal | ||
| 35 | /// line through the middle, a little off it and above or below it, so the cracks between them run | ||
| 36 | /// roughly parallel right through the shape. | ||
| 37 | 247 | std::vector<Vec3d> SlabSeedPoints(const Box3d& box, int pieces, RandomStream& random) { | |
| 38 | 247 | const Vec3d center = (box.min + box.max) * 0.5; | |
| 39 | 247 | const Vec3d half = (box.max - box.min) * 0.5; | |
| 40 | 247 | const double angle = kPi * random.NextDouble(); | |
| 41 | 247 | const Vec3d along{std::cos(angle), std::sin(angle), 0.0}; | |
| 42 | 247 | const Vec3d across{-along.y, along.x, 0.0}; | |
| 43 | 247 | const double reach = half.x * std::fabs(along.x) + half.y * std::fabs(along.y); | |
| 44 | 247 | const double side = half.x * std::fabs(across.x) + half.y * std::fabs(across.y); | |
| 45 | 247 | const double step = 2.0 * reach / pieces; | |
| 46 | 247 | std::vector<Vec3d> seeds; | |
| 47 | 1108 | for (int k = 0; k < pieces; ++k) { | |
| 48 | 861 | const double at = (k - 0.5 * (pieces - 1) + 0.4 * (random.NextDouble() - 0.5)) * step; | |
| 49 | 861 | const double off = 0.3 * side * (random.NextDouble() - 0.5); | |
| 50 | 861 | const double rise = 0.3 * half.z * (random.NextDouble() - 0.5); | |
| 51 | 861 | seeds.push_back(center + along * at + across * off + Vec3d{0.0, 0.0, rise}); | |
| 52 | } | ||
| 53 | 247 | return seeds; | |
| 54 | } | ||
| 55 | |||
| 56 | /// `mesh` cut to the cell of seed `k` among `seeds`: every vertex beyond a plane halfway to | ||
| 57 | /// another seed is moved along its ray from seed `k` onto that plane. | ||
| 58 | 1342 | TriangleMesh CutToCell(const TriangleMesh& mesh, const std::vector<Vec3d>& seeds, std::size_t k) { | |
| 59 | 1342 | TriangleMesh piece = mesh; | |
| 60 | 6592 | for (std::size_t j = 0; j < seeds.size(); ++j) { | |
| 61 | 5250 | if (j == k) { | |
| 62 | 1342 | continue; | |
| 63 | } | ||
| 64 | 3908 | const Vec3d between = seeds[j] - seeds[k]; | |
| 65 | 3908 | const double length = Length(between); | |
| 66 | 3908 | const Vec3d normal = between * (1.0 / length); | |
| 67 | 3908 | const double plane = 0.5 * length; | |
| 68 | 1992140 | for (Vec3d& p : piece.positions) { | |
| 69 | 1988232 | const Vec3d ray = p - seeds[k]; | |
| 70 | 1988232 | const double along = Dot(normal, ray); | |
| 71 | 1988232 | if (along > plane) { | |
| 72 | 732791 | p = seeds[k] + ray * (plane / along); | |
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | 1342 | return piece; | |
| 77 | } | ||
| 78 | |||
| 79 | } // namespace | ||
| 80 | |||
| 81 | 393 | std::vector<BoulderFragment> FragmentBoulderShape(const BoulderShape& parent, int pieces, | |
| 82 | std::uint64_t seed, BoulderCracks cracks) { | ||
| 83 | 393 | if (pieces < 2 || pieces > 12) { | |
| 84 | 3 | throw std::invalid_argument("A boulder breaks into 2 to 12 pieces"); | |
| 85 | } | ||
| 86 | 390 | const IdWords words = SplitId(seed); | |
| 87 | 390 | RandomStream random(words.low, kBoulderFragmentStream, words.high); | |
| 88 | 390 | const Box3d box = Bounds(parent.mesh); | |
| 89 | 390 | const std::vector<Vec3d> seeds = cracks == BoulderCracks::Slabs | |
| 90 | 390 | ? SlabSeedPoints(box, pieces, random) | |
| 91 | 390 | : SeedPoints(box, pieces, random); | |
| 92 | 390 | std::vector<BoulderFragment> fragments; | |
| 93 | 1732 | for (std::size_t k = 0; k < seeds.size(); ++k) { | |
| 94 | 1342 | const TriangleMesh cut = CutToCell(parent.mesh, seeds, k); | |
| 95 | 1342 | const Box3d bounds = Bounds(cut); | |
| 96 | 1342 | BoulderFragment fragment; | |
| 97 | 1342 | fragment.shape = UnitBoulderShape(cut, bounds); | |
| 98 | 1342 | fragment.shape.style = parent.style; | |
| 99 | 1342 | fragment.shape.material = parent.material; | |
| 100 | 1342 | fragment.center = (bounds.min + bounds.max) * 0.5; | |
| 101 | 1342 | fragment.scale = std::fmax(bounds.max.x - bounds.min.x, bounds.max.y - bounds.min.y); | |
| 102 | 1342 | fragments.push_back(std::move(fragment)); | |
| 103 | 2684 | } | |
| 104 | 390 | return fragments; | |
| 105 | 390 | } | |
| 106 | |||
| 107 | } // namespace terraingen | ||
| 108 |