src/layers/boulders/boulder_pose.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/boulders/boulder_layer.h" | ||
| 4 | |||
| 5 | #include "terraingen/noise/hash.h" | ||
| 6 | #include "terraingen/random/stream.h" | ||
| 7 | #include "terraingen/util/bits.h" | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <cmath> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | namespace terraingen { | ||
| 14 | |||
| 15 | 31101 | const ManualBoulder* BoulderLayer::Fixed(std::uint64_t id) const { | |
| 16 | 31101 | const auto found = m_fixed.find(id); | |
| 17 | 49224 | return found != m_fixed.end() ? &found->second : nullptr; | |
| 18 | } | ||
| 19 | |||
| 20 | 16948 | bool BoulderLayer::Settles(const Boulder& boulder) const { | |
| 21 | 16948 | if (!m_options.pose.settle) { | |
| 22 | return false; | ||
| 23 | } | ||
| 24 | 16948 | const ManualBoulder* fixed = boulder.parent == 0 ? Fixed(boulder.id) : nullptr; | |
| 25 | 16505 | return fixed == nullptr || !fixed->FixesOrientation(); | |
| 26 | } | ||
| 27 | |||
| 28 | 485622 | double BoulderLayer::Wear(double diameter, const BoulderAbundance& abundance, double density, | |
| 29 | std::uint64_t id) const { | ||
| 30 | 485622 | RandomStream random = AttributeStream(id, BoulderDraw::Wear); | |
| 31 | 485622 | const double thrown = random.NextDouble(); | |
| 32 | 485622 | if (abundance.ejecta > 0.0 && density > 0.0) { | |
| 33 | 13211 | const double fromCrater = abundance.ejecta * m_ejectaSizes->Incremental(diameter) / density; | |
| 34 | 13211 | if (thrown < fromCrater) { | |
| 35 | 10818 | return abundance.ejectaWear; | |
| 36 | } | ||
| 37 | } | ||
| 38 | 474804 | return random.NextDouble(); | |
| 39 | } | ||
| 40 | |||
| 41 | 485622 | Boulder BoulderLayer::DrawPose(const ThinnedCandidate& thinned, double spacing, | |
| 42 | const ManualBoulder* fixed) const { | ||
| 43 | 485622 | const Crater& candidate = thinned.candidate; | |
| 44 | 485622 | RandomStream random = AttributeStream(candidate.id, BoulderDraw::Pose); | |
| 45 | 485622 | const BoulderPose& pose = m_options.pose; | |
| 46 | 485622 | const double ratio = | |
| 47 | 485622 | ValueRange{pose.minHeightRatio, pose.maxHeightRatio}.Lerp(random.NextDouble()); | |
| 48 | 485622 | double wear = Wear(candidate.diameter, thinned.abundance, thinned.density, candidate.id); | |
| 49 | 485622 | if (fixed != nullptr && fixed->wear) { | |
| 50 | 9590 | wear = *fixed->wear; | |
| 51 | } | ||
| 52 | 485622 | double share = random.NextDouble(); | |
| 53 | 485622 | if (pose.burialByWear > 0.0) { | |
| 54 | 1491 | share = (1.0 - pose.burialByWear) * share + pose.burialByWear * wear; | |
| 55 | } | ||
| 56 | 485622 | const double burial = ValueRange{pose.minBurial, pose.maxBurial}.Lerp(share); | |
| 57 | 485622 | const double yaw = 2.0 * kPi * random.NextDouble(); | |
| 58 | 485622 | const double lean = (pose.maxLeanDegrees * kDegree) * random.NextDouble(); | |
| 59 | 485622 | Boulder boulder{ | |
| 60 | 485622 | candidate.x, candidate.y, candidate.diameter, ratio * candidate.diameter, burial, | |
| 61 | 485622 | yaw, lean, candidate.id}; | |
| 62 | 485622 | boulder.wear = wear; | |
| 63 | 485622 | boulder.model = m_options.rendering.modelThreshold.IsModel(boulder.diameter, spacing); | |
| 64 | 485622 | if (fixed != nullptr) { | |
| 65 | 9616 | boulder.height = fixed->height.value_or(boulder.height); | |
| 66 | 9616 | boulder.burial = fixed->burial.value_or(boulder.burial); | |
| 67 | 9616 | if (fixed->yawDegrees) { | |
| 68 | 9591 | boulder.yaw = *fixed->yawDegrees * kDegree; | |
| 69 | } | ||
| 70 | 9616 | if (fixed->leanDegrees) { | |
| 71 | 9590 | boulder.lean = *fixed->leanDegrees * kDegree; | |
| 72 | } | ||
| 73 | 9616 | boulder.shapeSeed = fixed->shapeSeed; | |
| 74 | 19200 | boulder.model = fixed->model.value_or(boulder.model); | |
| 75 | } | ||
| 76 | 485622 | return boulder; | |
| 77 | } | ||
| 78 | |||
| 79 | 485622 | std::optional<Boulder> BoulderLayer::PlaceCandidate(const ThinnedCandidate& thinned, double spacing, | |
| 80 | const Ground* ground) const { | ||
| 81 | 485622 | const ManualBoulder* fixed = thinned.candidate.manual ? Fixed(thinned.candidate.id) : nullptr; | |
| 82 | 485622 | Boulder boulder = DrawPose(thinned, spacing, fixed); | |
| 83 | 485622 | const BoulderPose& pose = m_options.pose; | |
| 84 | 547018 | if (pose.maxSlopeDegrees && ground != nullptr && !thinned.candidate.manual && | |
| 85 | 61396 | !Roll(boulder, *ground)) { | |
| 86 | 1641 | return std::nullopt; | |
| 87 | } | ||
| 88 | 483981 | ReadGround(boulder, ground); | |
| 89 | 465918 | if (pose.sideShare > 0.0 || pose.endShare > 0.0 || (pose.maxTiltDegrees * kDegree) > 0.0 || | |
| 90 | 949917 | pose.followSlope || (fixed != nullptr && fixed->FixesOrientation())) { | |
| 91 | 28712 | Settle(boulder, ground, fixed); | |
| 92 | } | ||
| 93 | 483981 | if (m_options.ventifacts.share > 0.0) { | |
| 94 | 3612 | Abrade(boulder); | |
| 95 | } | ||
| 96 | 483981 | return boulder; | |
| 97 | } | ||
| 98 | |||
| 99 | 483981 | void BoulderLayer::ReadGround(Boulder& boulder, const Ground* ground) const { | |
| 100 | 483981 | boulder.ground.reset(); | |
| 101 | 483981 | if (ground != nullptr) { | |
| 102 | 323719 | const double height = | |
| 103 | 323719 | static_cast<double>(ground->HeightAt(boulder.x, boulder.y, m_options.groundSpacing)); | |
| 104 | 323719 | if (!std::isnan(height)) { | |
| 105 | 323715 | boulder.ground = height; | |
| 106 | } | ||
| 107 | } | ||
| 108 | 483981 | } | |
| 109 | |||
| 110 | 3612 | void BoulderLayer::Abrade(Boulder& boulder) const { | |
| 111 | 3612 | const BoulderVentifacts& ventifacts = m_options.ventifacts; | |
| 112 | 3612 | RandomStream random = AttributeStream(boulder.id, BoulderDraw::Abrade); | |
| 113 | 3612 | if (random.NextDouble() >= ventifacts.share) { | |
| 114 | 1709 | return; | |
| 115 | } | ||
| 116 | 1903 | const double reach = 0.5 * boulder.diameter * (1.0 - boulder.wear); | |
| 117 | 4413 | const auto facet = [&](double wind, double pick) { | |
| 118 | 2510 | return BoulderFacet{wind + kPi, | |
| 119 | 2510 | ValueRange{(ventifacts.minAngleDegrees * kDegree), | |
| 120 | 607 | (ventifacts.maxAngleDegrees * kDegree)} | |
| 121 | 2510 | .Lerp(pick), | |
| 122 | reach}; | ||
| 123 | 1903 | }; | |
| 124 | 1903 | boulder.facets[0] = facet((ventifacts.windAzimuthDegrees * kDegree), random.NextDouble()); | |
| 125 | 1903 | boulder.facetCount = 1; | |
| 126 | 1903 | const double keel = random.NextDouble(); | |
| 127 | 1903 | const double second = random.NextDouble(); | |
| 128 | 1903 | if (ventifacts.secondWindAzimuthDegrees && keel < ventifacts.keelShare) { | |
| 129 | 607 | boulder.facets[1] = facet((*ventifacts.secondWindAzimuthDegrees * kDegree), second); | |
| 130 | 607 | boulder.facetCount = 2; | |
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | 61396 | bool BoulderLayer::Roll(Boulder& boulder, const Ground& ground) const { | |
| 135 | 61396 | constexpr double kMaxSteps = 128.0; | |
| 136 | 61396 | const BoulderPose& pose = m_options.pose; | |
| 137 | 61396 | const double limit = std::tan((*pose.maxSlopeDegrees * kDegree)); | |
| 138 | 61396 | const double reach = pose.rollDistance.value_or(0.0); | |
| 139 | 61396 | const double step = std::fmax(boulder.diameter, reach / kMaxSteps); | |
| 140 | 61396 | double rolled = 0.0; | |
| 141 | 661180 | for (;;) { | |
| 142 | 361288 | const std::optional<Vec2d> g = Gradient(boulder, ground); | |
| 143 | 361288 | const double gradient = g ? std::hypot(g->x, g->y) : 0.0; | |
| 144 | 361288 | if (!(gradient > limit)) { | |
| 145 | 61396 | return true; | |
| 146 | } | ||
| 147 | 301533 | if (rolled + step > reach) { | |
| 148 | return false; | ||
| 149 | } | ||
| 150 | 299892 | const Vec2d to = m_surface.Locate({boulder.x, boulder.y}, | |
| 151 | 299892 | {-g->x / gradient * step, -g->y / gradient * step}); | |
| 152 | 299892 | boulder.x = to.x; | |
| 153 | 299892 | boulder.y = to.y; | |
| 154 | 299892 | rolled += step; | |
| 155 | 299892 | } | |
| 156 | } | ||
| 157 | |||
| 158 | 362444 | std::optional<Vec2d> BoulderLayer::Gradient(const Boulder& boulder, const Ground& ground) const { | |
| 159 | 724888 | return GroundGradient(ground, m_surface, {boulder.x, boulder.y}, 0.5 * boulder.diameter, | |
| 160 | 362444 | m_options.groundSpacing); | |
| 161 | } | ||
| 162 | |||
| 163 | 28712 | Vec3d BoulderLayer::UpAxis(RandomStream& random, const ManualBoulder* fixedTilt) const { | |
| 164 | 28712 | const BoulderPose& pose = m_options.pose; | |
| 165 | 28712 | Vec3d up{0.0, 0.0, 1.0}; | |
| 166 | 28712 | double cosine = 1.0; | |
| 167 | 28712 | double azimuth = 0.0; | |
| 168 | 28712 | if ((pose.maxTiltDegrees * kDegree) > 0.0) { | |
| 169 | 17975 | cosine = 1.0 - random.NextDouble() * (1.0 - std::cos(pose.maxTiltDegrees * kDegree)); | |
| 170 | 17975 | const double sine = std::sqrt(std::fmax(0.0, 1.0 - cosine * cosine)); | |
| 171 | 17975 | azimuth = 2.0 * kPi * random.NextDouble(); | |
| 172 | 17975 | up = {sine * std::cos(azimuth), sine * std::sin(azimuth), cosine}; | |
| 173 | } | ||
| 174 | 28712 | if (fixedTilt == nullptr) { | |
| 175 | 19122 | return up; | |
| 176 | } | ||
| 177 | 9590 | if (!((pose.maxTiltDegrees * kDegree) > 0.0)) { | |
| 178 | 9585 | azimuth = 2.0 * kPi * random.NextDouble(); | |
| 179 | } | ||
| 180 | 9590 | const double tilt = | |
| 181 | 9590 | fixedTilt->tiltDegrees ? *fixedTilt->tiltDegrees * kDegree : std::acos(cosine); | |
| 182 | 9590 | const double toward = | |
| 183 | 9590 | fixedTilt->tiltAzimuthDegrees ? *fixedTilt->tiltAzimuthDegrees * kDegree : azimuth; | |
| 184 | 9590 | return {std::sin(tilt) * std::cos(toward), std::sin(tilt) * std::sin(toward), std::cos(tilt)}; | |
| 185 | } | ||
| 186 | |||
| 187 | 28712 | void BoulderLayer::Settle(Boulder& boulder, const Ground* ground, | |
| 188 | const ManualBoulder* fixed) const { | ||
| 189 | 28712 | const BoulderPose& pose = m_options.pose; | |
| 190 | 28712 | RandomStream random = AttributeStream(boulder.id, BoulderDraw::Settle); | |
| 191 | 28712 | const double pick = random.NextDouble(); | |
| 192 | 28712 | boulder.rest = pick < pose.endShare ? BoulderRest::End | |
| 193 | 26904 | : pick < pose.endShare + pose.sideShare ? BoulderRest::Side | |
| 194 | : BoulderRest::Face; | ||
| 195 | 28712 | if (fixed != nullptr && fixed->rest) { | |
| 196 | 9 | boulder.rest = *fixed->rest; | |
| 197 | } | ||
| 198 | 28712 | const bool fixesTilt = fixed != nullptr && (fixed->tiltDegrees.has_value() || | |
| 199 | 10 | fixed->tiltAzimuthDegrees.has_value()); | |
| 200 | 28712 | Vec3d up = UpAxis(random, fixesTilt ? fixed : nullptr); | |
| 201 | 28712 | bool sloped = false; | |
| 202 | 28712 | if (pose.followSlope && ground != nullptr) { | |
| 203 | 1156 | if (const std::optional<Vec2d> g = Gradient(boulder, *ground)) { | |
| 204 | 1156 | const double slope = std::atan(std::hypot(g->x, g->y)); | |
| 205 | 1156 | up = TiltRotation(slope, std::atan2(-g->y, -g->x)).Apply(up); | |
| 206 | 1156 | sloped = true; | |
| 207 | } | ||
| 208 | } | ||
| 209 | 28712 | boulder.tilt = std::acos(std::clamp(up.z, -1.0, 1.0)); | |
| 210 | 28712 | boulder.tiltAzimuth = std::atan2(up.y, up.x); | |
| 211 | 28712 | if (fixesTilt && !sloped) { | |
| 212 | 9589 | if (fixed->tiltDegrees) { | |
| 213 | 9589 | boulder.tilt = *fixed->tiltDegrees * kDegree; | |
| 214 | } | ||
| 215 | 9589 | if (fixed->tiltAzimuthDegrees) { | |
| 216 | 6 | boulder.tiltAzimuth = *fixed->tiltAzimuthDegrees * kDegree; | |
| 217 | } | ||
| 218 | } | ||
| 219 | 28712 | } | |
| 220 | |||
| 221 | 3616 | void BoulderLayer::ReadGrounds(std::vector<Boulder>& boulders, | |
| 222 | const std::vector<std::size_t>& indices, const Ground* ground, | ||
| 223 | unsigned threads) const { | ||
| 224 | 3616 | std::vector<Vec2d> points; | |
| 225 | 3616 | points.reserve(indices.size()); | |
| 226 | 16669 | for (const std::size_t n : indices) { | |
| 227 | 13053 | boulders[n].ground.reset(); | |
| 228 | 13053 | points.push_back({boulders[n].x, boulders[n].y}); | |
| 229 | } | ||
| 230 | 3616 | if (ground == nullptr || points.empty()) { | |
| 231 | 3615 | return; | |
| 232 | } | ||
| 233 | 1 | std::vector<float> heights(points.size()); | |
| 234 | 1 | ground->HeightsAt(points, heights, m_options.groundSpacing, threads); | |
| 235 | 508 | for (std::size_t k = 0; k < indices.size(); ++k) { | |
| 236 | 507 | if (!std::isnan(heights[k])) { | |
| 237 | 1014 | boulders[indices[k]].ground = heights[k]; | |
| 238 | } | ||
| 239 | } | ||
| 240 | 3616 | } | |
| 241 | |||
| 242 | 4961 | BoulderLayer::BreakStage BoulderLayer::Stage(const Boulder& parent) const { | |
| 243 | 4961 | const BoulderFragmentation& fragmentation = *m_options.fragmentation; | |
| 244 | 4961 | const bool rubble = parent.wear >= fragmentation.rubbleWear; | |
| 245 | 4961 | BreakStage stage; | |
| 246 | 4071 | stage.opening = rubble || fragmentation.rubbleWear <= fragmentation.splitWear | |
| 247 | 4961 | ? 1.0 | |
| 248 | 4071 | : (parent.wear - fragmentation.splitWear) / | |
| 249 | 4071 | (fragmentation.rubbleWear - fragmentation.splitWear); | |
| 250 | 4961 | stage.scatter = !rubble ? 0.0 | |
| 251 | 890 | : fragmentation.rubbleWear < 1.0 ? (parent.wear - fragmentation.rubbleWear) / | |
| 252 | 890 | (1.0 - fragmentation.rubbleWear) | |
| 253 | : 1.0; | ||
| 254 | 4961 | return stage; | |
| 255 | } | ||
| 256 | |||
| 257 | 16120 | Boulder BoulderLayer::PlacePiece(const Boulder& parent, const BoulderFragment& fragment, | |
| 258 | std::size_t k, const PieceFrame& frame, double push, | ||
| 259 | double twist) const { | ||
| 260 | 16120 | const BoulderFragmentation& fragmentation = *m_options.fragmentation; | |
| 261 | 16120 | const BoulderPlacement& placement = frame.placement; | |
| 262 | 16120 | const double scatter = frame.stage.scatter; | |
| 263 | 16120 | const auto [low, high] = ZRange(fragment.shape.mesh.positions, [&](const Vec3d& p) { | |
| 264 | 7985008 | return placement.Apply(fragment.center + p * fragment.scale); | |
| 265 | }); | ||
| 266 | 16120 | const Vec3d center = placement.Apply(fragment.center); | |
| 267 | 16120 | const double out = std::hypot(center.x, center.y); | |
| 268 | 16120 | const double angle = out > 0.0 ? std::atan2(center.y, center.x) : 2.0 * kPi * push; | |
| 269 | 16120 | const double gap = (frame.stage.opening * fragmentation.gapRatio + | |
| 270 | 16120 | scatter * fragmentation.spreadRatio * (0.5 + 0.5 * push)) * | |
| 271 | 16120 | parent.diameter; | |
| 272 | 16120 | Boulder piece = parent; | |
| 273 | 16120 | piece.facetCount = 0; | |
| 274 | 16120 | const IdWords id = SplitId(parent.id); | |
| 275 | 16120 | const UVec3 key = | |
| 276 | 16120 | Pcg3dHash({id.low, id.high, kBoulderPieceStream + static_cast<std::uint32_t>(k)}); | |
| 277 | 16120 | piece.id = JoinWords(key.x, key.y); | |
| 278 | 16120 | piece.parent = parent.id; | |
| 279 | 16120 | piece.piece = static_cast<std::uint32_t>(k); | |
| 280 | 16120 | piece.diameter = parent.diameter * fragment.scale; | |
| 281 | 16120 | piece.height = parent.height * fragment.shape.shortAxis * fragment.scale / frame.shortAxis; | |
| 282 | 16120 | piece.yaw = parent.yaw + scatter * kPi * (2.0 * twist - 1.0); | |
| 283 | 16120 | piece.lean = parent.lean * (1.0 - scatter); | |
| 284 | 16120 | piece.tilt = parent.tilt * (1.0 - scatter); | |
| 285 | 16120 | const double sunk = std::clamp(-low / (high - low), 0.0, 0.95); | |
| 286 | 16120 | piece.burial = std::fmax(sunk, sunk + scatter * (m_options.pose.maxBurial - sunk)); | |
| 287 | 32240 | const Vec2d at = m_surface.Locate( | |
| 288 | 16120 | {parent.x, parent.y}, {center.x + gap * std::cos(angle), center.y + gap * std::sin(angle)}); | |
| 289 | 16120 | piece.x = at.x; | |
| 290 | 16120 | piece.y = at.y; | |
| 291 | 16120 | return piece; | |
| 292 | } | ||
| 293 | |||
| 294 | 5423 | std::vector<Boulder> BoulderLayer::Break(const Boulder& parent, double spacing, | |
| 295 | double smallest) const { | ||
| 296 | 5423 | if (!m_options.fragmentation) { | |
| 297 | ✗ | return {parent}; | |
| 298 | } | ||
| 299 | 5423 | const BoulderFragmentation& fragmentation = *m_options.fragmentation; | |
| 300 | 10385 | if (parent.diameter < fragmentation.minDiameter || parent.wear < fragmentation.splitWear || | |
| 301 | 4962 | Fixed(parent.id) != nullptr) { | |
| 302 | 462 | return {parent}; | |
| 303 | } | ||
| 304 | 4961 | const auto shape = Shape(parent); | |
| 305 | 4961 | const auto fragments = CachedFragments(ShapeSeed(parent, m_options.rendering.shapePrototypes)); | |
| 306 | 4961 | Boulder origin = parent; | |
| 307 | 4961 | origin.x = 0.0; | |
| 308 | 4961 | origin.y = 0.0; | |
| 309 | 4961 | const PieceFrame frame{PlaceBoulderModel(origin, *shape, 0.0, Settles(parent)), Stage(parent), | |
| 310 | 4961 | shape->shortAxis}; | |
| 311 | 4961 | const IdWords id = SplitId(parent.id); | |
| 312 | 4961 | RandomStream random(m_options.seed, kBoulderPieceStream, id.low, id.high); | |
| 313 | 4961 | std::vector<Boulder> pieces; | |
| 314 | 21081 | for (std::size_t k = 0; k < fragments->size(); ++k) { | |
| 315 | 16120 | const double push = random.NextDouble(); | |
| 316 | 16120 | const double twist = random.NextDouble(); | |
| 317 | 16120 | Boulder piece = PlacePiece(parent, (*fragments)[k], k, frame, push, twist); | |
| 318 | 16120 | if (piece.diameter < smallest) { | |
| 319 | 1237 | continue; | |
| 320 | } | ||
| 321 | 14883 | piece.model = m_options.rendering.modelThreshold.IsModel(piece.diameter, spacing); | |
| 322 | 14883 | pieces.push_back(piece); | |
| 323 | } | ||
| 324 | 4961 | return pieces; | |
| 325 | 14883 | } | |
| 326 | |||
| 327 | } // namespace terraingen | ||
| 328 |