src/layers/boulders/boulder_relief.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/raster/cell_loops.h" | ||
| 6 | #include "terraingen/util/bits.h" | ||
| 7 | #include "terraingen/util/parallel.h" | ||
| 8 | #include "util/numerics.h" | ||
| 9 | |||
| 10 | #include <algorithm> | ||
| 11 | #include <array> | ||
| 12 | #include <cmath> | ||
| 13 | #include <cstddef> | ||
| 14 | #include <limits> | ||
| 15 | #include <utility> | ||
| 16 | #include <vector> | ||
| 17 | |||
| 18 | namespace terraingen { | ||
| 19 | |||
| 20 | namespace { | ||
| 21 | |||
| 22 | /// The box the relief of a boulder at (`x`, `y`) reaching `reach` meters is drawn over: on a | ||
| 23 | /// sphere a square in degrees around the box `Surface::Expand` gives. | ||
| 24 | 25334 | Bounds2d ReliefBox(double x, double y, double reach, const Surface& surface) { | |
| 25 | 25334 | if (!surface.IsBody()) { | |
| 26 | 18372 | return {x - reach, y - reach, x + reach, y + reach}; | |
| 27 | } | ||
| 28 | 6962 | const Bounds2d box = surface.Expand({x, y, x, y}, reach); | |
| 29 | 6962 | const double cx = 0.5 * (box.minX + box.maxX); | |
| 30 | 6962 | const double cy = 0.5 * (box.minY + box.maxY); | |
| 31 | 6962 | const double half = std::fmax(0.5 * (box.maxX - box.minX), 0.5 * (box.maxY - box.minY)); | |
| 32 | 6962 | return {cx - half, cy - half, cx + half, cy + half}; | |
| 33 | } | ||
| 34 | |||
| 35 | /// How far a placed shape reaches from its boulder's center `cx`, `cy` along either axis. | ||
| 36 | 8347 | double ShapeReach(const TriangleMesh& placed, double cx, double cy) { | |
| 37 | 8347 | const Box3d box = Bounds(placed); | |
| 38 | 8347 | return std::fmax(std::fmax(std::fabs(box.min.x - cx), std::fabs(box.max.x - cx)), | |
| 39 | 8347 | std::fmax(std::fabs(box.min.y - cy), std::fabs(box.max.y - cy))); | |
| 40 | } | ||
| 41 | |||
| 42 | /// The height (m) of a boulder's dome `distance` meters from its center, averaged over a band | ||
| 43 | /// `width` meters wide so its rim slopes over a sample; 0 `width` samples it at the point. | ||
| 44 | 355408 | double DomeHeight(const Boulder& boulder, double distance, double width) { | |
| 45 | 355408 | const double radius = 0.5 * boulder.diameter; | |
| 46 | 355408 | if (!(width > 0.0)) { | |
| 47 | ✗ | const double r2 = (distance * distance) / (radius * radius); | |
| 48 | ✗ | return r2 < 1.0 ? boulder.ExposedHeight() * std::sqrt(1.0 - r2) : 0.0; | |
| 49 | } | ||
| 50 | 774526 | const auto area = [](double u) { return 0.5 * (u * std::sqrt(1.0 - u * u) + std::asin(u)); }; | |
| 51 | 355408 | const double low = std::clamp((distance - 0.5 * width) / radius, -1.0, 1.0); | |
| 52 | 355408 | const double high = std::clamp((distance + 0.5 * width) / radius, -1.0, 1.0); | |
| 53 | 355408 | if (!(high > low)) { | |
| 54 | return 0.0; | ||
| 55 | } | ||
| 56 | 210625 | return boulder.ExposedHeight() * (area(high) - area(low)) * radius / width; | |
| 57 | } | ||
| 58 | |||
| 59 | /// Where a fillet `top` meters high meets a round dome averaged over `width` meters | ||
| 60 | /// (`DomeHeight`), the same in every direction and no farther out than its radius. | ||
| 61 | 646 | FilletContact RoundDomeContact(const Boulder& boulder, double width, double top) { | |
| 62 | 646 | const double radius = 0.5 * boulder.diameter; | |
| 63 | 646 | const double height = std::fmin(top, DomeHeight(boulder, 0.0, width)); | |
| 64 | 646 | if (!(height > 0.0)) { | |
| 65 | ✗ | return {}; | |
| 66 | } | ||
| 67 | 646 | if (!(width > 0.0)) { | |
| 68 | ✗ | const double share = std::fmin(height / boulder.ExposedHeight(), 1.0); | |
| 69 | ✗ | return {radius * std::sqrt(1.0 - share * share), height}; | |
| 70 | } | ||
| 71 | 646 | if (DomeHeight(boulder, radius, width) >= height) { | |
| 72 | 21 | return {radius, height}; | |
| 73 | } | ||
| 74 | 625 | const BisectionInterval contact = Bisect(0.0, radius, 50, 0.0, [&](double distance) { | |
| 75 | 31250 | return !(DomeHeight(boulder, distance, width) >= height); | |
| 76 | }); | ||
| 77 | 625 | return {contact.low, height}; | |
| 78 | } | ||
| 79 | |||
| 80 | /// Where a fillet `top` meters high meets a dome averaged over `width` meters and cut by the | ||
| 81 | /// boulder's facets along the unit `direction` from its center: the outermost point within its | ||
| 82 | /// radius where the rock stands that high, or as high as it stands along `direction`. | ||
| 83 | 642 | FilletContact FacetedDomeContact(const Boulder& boulder, const Vec2d& direction, double width, | |
| 84 | double top) { | ||
| 85 | 642 | const double outer = 0.5 * boulder.diameter; | |
| 86 | 68052 | const auto rock = [&](double distance) { | |
| 87 | 67410 | return boulder.CutByFacets({distance * direction.x, distance * direction.y}, | |
| 88 | 67410 | DomeHeight(boulder, distance, width)); | |
| 89 | 642 | }; | |
| 90 | 642 | constexpr int kSteps = 64; | |
| 91 | 642 | std::array<double, kSteps + 1> heights{}; | |
| 92 | 642 | double highest = 0.0; | |
| 93 | 42372 | for (std::size_t step = 0; step < heights.size(); ++step) { | |
| 94 | 41730 | heights[step] = rock(outer * static_cast<double>(step) / kSteps); | |
| 95 | 41730 | highest = std::fmax(highest, heights[step]); | |
| 96 | } | ||
| 97 | 642 | const double height = std::fmin(top, highest); | |
| 98 | 642 | if (!(height > 0.0)) { | |
| 99 | ✗ | return {}; | |
| 100 | } | ||
| 101 | int last = 0; | ||
| 102 | 42372 | for (std::size_t step = 0; step < heights.size(); ++step) { | |
| 103 | 41730 | if (heights[step] >= height) { | |
| 104 | 13281 | last = static_cast<int>(step); | |
| 105 | } | ||
| 106 | } | ||
| 107 | 642 | const BisectionInterval contact = | |
| 108 | 642 | Bisect(outer * last / kSteps, outer * std::min(last + 1, kSteps) / kSteps, 40, 0.0, | |
| 109 | 26322 | [&](double distance) { return !(rock(distance) >= height); }); | |
| 110 | 642 | return {contact.low, height}; | |
| 111 | } | ||
| 112 | |||
| 113 | /// How many directions a shape's fillet contacts are found in for a boulder of `diameter` at | ||
| 114 | /// `spacing`: about two per sample around its rim. | ||
| 115 | 5640 | std::size_t FilletDirections(double diameter, double spacing) { | |
| 116 | 5640 | const double around = kPi * diameter / std::fmax(spacing, 1e-9); | |
| 117 | 5640 | return static_cast<std::size_t>(std::clamp(std::ceil(2.0 * around), 16.0, 256.0)); | |
| 118 | } | ||
| 119 | |||
| 120 | /// Where a fillet `top` meters high meets the top surface of `placed` (a shape placed on level | ||
| 121 | /// ground at 0, centered on `center`) in `count` directions at even steps of azimuth from east. | ||
| 122 | 5640 | std::vector<FilletContact> ShapeContacts(const TriangleMesh& placed, const Vec2d& center, | |
| 123 | double top, std::size_t count) { | ||
| 124 | 5640 | struct Piece { | |
| 125 | double inner; | ||
| 126 | double innerHeight; | ||
| 127 | double outer; | ||
| 128 | double outerHeight; | ||
| 129 | }; | ||
| 130 | 5640 | std::vector<FilletContact> contacts(count); | |
| 131 | 5640 | std::vector<Piece> pieces; | |
| 132 | 1447325 | for (std::size_t k = 0; k < count; ++k) { | |
| 133 | 1441685 | const double azimuth = 2.0 * kPi * static_cast<double>(k) / static_cast<double>(count); | |
| 134 | 1441685 | const Vec2d along{std::cos(azimuth), std::sin(azimuth)}; | |
| 135 | 1441685 | pieces.clear(); | |
| 136 | 1441685 | double highest = 0.0; | |
| 137 | 1846560917 | for (const auto& triangle : placed.triangles) { | |
| 138 | double a[3]; | ||
| 139 | double s[3]; | ||
| 140 | double z[3]; | ||
| 141 | ✗ | for (int j = 0; j < 3; ++j) { | |
| 142 | ✗ | const Vec3d& p = placed.positions[triangle[j]]; | |
| 143 | ✗ | a[j] = (p.x - center.x) * along.x + (p.y - center.y) * along.y; | |
| 144 | ✗ | s[j] = (p.y - center.y) * along.x - (p.x - center.x) * along.y; | |
| 145 | ✗ | z[j] = p.z; | |
| 146 | } | ||
| 147 | 1845119232 | double lowA = std::numeric_limits<double>::infinity(); | |
| 148 | 1845119232 | double highA = -lowA; | |
| 149 | 1845119232 | double lowZ = 0.0; | |
| 150 | 1845119232 | double highZ = 0.0; | |
| 151 | 2109239092 | const auto add = [&](double at, double height) { | |
| 152 | 264119860 | if (at < lowA) { | |
| 153 | 200863016 | lowA = at; | |
| 154 | 200863016 | lowZ = height; | |
| 155 | } | ||
| 156 | 264119860 | if (at > highA) { | |
| 157 | 200874274 | highA = at; | |
| 158 | 200874274 | highZ = height; | |
| 159 | } | ||
| 160 | 2109239092 | }; | |
| 161 | ✗ | for (int j = 0; j < 3; ++j) { | |
| 162 | ✗ | const int n = (j + 1) % 3; | |
| 163 | ✗ | if (s[j] == 0.0) { | |
| 164 | 18270000 | add(a[j], z[j]); | |
| 165 | } | ||
| 166 | ✗ | if ((s[j] < 0.0 && s[n] > 0.0) || (s[j] > 0.0 && s[n] < 0.0)) { | |
| 167 | 245849860 | const double t = s[j] / (s[j] - s[n]); | |
| 168 | 245849860 | add(a[j] + t * (a[n] - a[j]), z[j] + t * (z[n] - z[j])); | |
| 169 | } | ||
| 170 | } | ||
| 171 | 1845119232 | if (!(highA >= 0.0)) { | |
| 172 | 1767449457 | continue; | |
| 173 | } | ||
| 174 | 77669775 | if (lowA < 0.0) { | |
| 175 | 2883370 | lowZ = highA > lowA ? lowZ + (highZ - lowZ) * (-lowA) / (highA - lowA) : highZ; | |
| 176 | 2883370 | lowA = 0.0; | |
| 177 | } | ||
| 178 | 77669775 | highest = std::fmax(highest, std::fmax(lowZ, highZ)); | |
| 179 | 77669775 | pieces.push_back({lowA, lowZ, highA, highZ}); | |
| 180 | } | ||
| 181 | 1441685 | const double height = std::fmin(top, highest); | |
| 182 | 1441685 | if (!(height > 0.0)) { | |
| 183 | ✗ | continue; | |
| 184 | } | ||
| 185 | 1441685 | double distance = 0.0; | |
| 186 | 79111460 | for (const Piece& piece : pieces) { | |
| 187 | 77669775 | if (piece.outerHeight >= height) { | |
| 188 | 42585540 | distance = std::fmax(distance, piece.outer); | |
| 189 | 35084235 | } else if (piece.innerHeight >= height) { | |
| 190 | 192458 | const double t = | |
| 191 | 192458 | (piece.innerHeight - height) / (piece.innerHeight - piece.outerHeight); | |
| 192 | 192458 | distance = std::fmax(distance, piece.inner + t * (piece.outer - piece.inner)); | |
| 193 | } | ||
| 194 | } | ||
| 195 | 1441685 | contacts[k] = {distance, height}; | |
| 196 | } | ||
| 197 | 5640 | return contacts; | |
| 198 | 5640 | } | |
| 199 | |||
| 200 | /// Where the fillet meets its boulder in the direction of `offset` (m east and north of its | ||
| 201 | /// center): the one contact of a round dome, those of a shape interpolated in azimuth, or found | ||
| 202 | /// on the dome averaged over `width` meters and cut by its facets. | ||
| 203 | 207964 | FilletContact SampleContact(const Boulder& boulder, const Vec2d& offset, | |
| 204 | const std::vector<FilletContact>& contacts, double width, double top) { | ||
| 205 | 207964 | if (contacts.size() == 1) { | |
| 206 | 103772 | return contacts.front(); | |
| 207 | } | ||
| 208 | 104192 | if (!contacts.empty()) { | |
| 209 | 103550 | double turn = std::atan2(offset.y, offset.x) / (2.0 * kPi); | |
| 210 | 103550 | turn -= std::floor(turn); | |
| 211 | 103550 | const double at = turn * static_cast<double>(contacts.size()); | |
| 212 | 103550 | const std::size_t k = static_cast<std::size_t>(at) % contacts.size(); | |
| 213 | 103550 | const FilletContact& a = contacts[k]; | |
| 214 | 103550 | const FilletContact& b = contacts[(k + 1) % contacts.size()]; | |
| 215 | 103550 | const double t = at - std::floor(at); | |
| 216 | 103550 | return {a.distance + t * (b.distance - a.distance), a.height + t * (b.height - a.height)}; | |
| 217 | } | ||
| 218 | 642 | const double distance = std::hypot(offset.x, offset.y); | |
| 219 | 642 | const Vec2d direction = | |
| 220 | 642 | distance > 0.0 ? Vec2d{offset.x / distance, offset.y / distance} : Vec2d{1.0, 0.0}; | |
| 221 | 642 | return FacetedDomeContact(boulder, direction, width, top); | |
| 222 | } | ||
| 223 | |||
| 224 | } // namespace | ||
| 225 | |||
| 226 | 37087 | Boulder BoulderLayer::InLocalFrame(const Boulder& boulder) const { | |
| 227 | 37087 | Boulder local = boulder; | |
| 228 | 37087 | if (m_surface.IsBody()) { | |
| 229 | 234 | local.x = 0.0; | |
| 230 | 234 | local.y = 0.0; | |
| 231 | } | ||
| 232 | 37087 | return local; | |
| 233 | } | ||
| 234 | |||
| 235 | 11113 | TriangleMesh BoulderLayer::PlacedOnLevel(const Boulder& boulder) const { | |
| 236 | 11113 | const auto shape = Shape(boulder); | |
| 237 | 11113 | const BoulderPlacement placement = | |
| 238 | 11113 | PlaceBoulderModel(InLocalFrame(boulder), *shape, 0.0, Settles(boulder)); | |
| 239 | 11113 | TriangleMesh mesh = PlaceMesh(shape->mesh, placement); | |
| 240 | 11113 | CutFacets(mesh, InLocalFrame(boulder), 0.0); | |
| 241 | 11113 | return mesh; | |
| 242 | 11113 | } | |
| 243 | |||
| 244 | 332573 | float BoulderLayer::AddRelief(float height, float base, double x, double y, const Boulder& boulder, | |
| 245 | const TriangleMesh* placed, double weight, bool rock, double width, | ||
| 246 | const std::vector<FilletContact>& contacts) const { | ||
| 247 | 332573 | const Vec2d offset = m_surface.Offset({boulder.x, boulder.y}, {x, y}); | |
| 248 | 332304 | const double relief = rock ? RockRelief(x, y, boulder, placed, width) : 0.0; | |
| 249 | 335557 | double result = static_cast<double>(height); | |
| 250 | 335557 | if (relief > 0.0) { | |
| 251 | 109785 | result = std::fmax(result, static_cast<double>(base) + weight * relief); | |
| 252 | } | ||
| 253 | 335557 | if (m_options.fillet.heightShare > 0.0) { | |
| 254 | 207960 | const double top = m_options.fillet.heightShare * boulder.ExposedHeight(); | |
| 255 | 207960 | const double fillet = | |
| 256 | 207872 | FilletHeight(boulder, std::hypot(offset.x, offset.y), m_options.fillet, | |
| 257 | 207960 | SampleContact(boulder, offset, contacts, width, top)); | |
| 258 | 207911 | if (fillet > 0.0) { | |
| 259 | 117298 | result = std::fmax(result, static_cast<double>(base) + weight * fillet); | |
| 260 | } | ||
| 261 | } | ||
| 262 | 335508 | return static_cast<float>(result); | |
| 263 | } | ||
| 264 | |||
| 265 | 378017 | double BoulderLayer::RockRelief(double x, double y, const Boulder& boulder, | |
| 266 | const TriangleMesh* placed, double width) const { | ||
| 267 | 378017 | const Vec2d offset = m_surface.Offset({boulder.x, boulder.y}, {x, y}); | |
| 268 | 378339 | if (placed == nullptr) { | |
| 269 | 254196 | return boulder.CutByFacets(offset, | |
| 270 | 510548 | DomeHeight(boulder, std::hypot(offset.x, offset.y), width)); | |
| 271 | } | ||
| 272 | 123223 | const std::optional<double> top = | |
| 273 | 123223 | m_surface.IsBody() ? TopSurface(*placed, offset.x, offset.y) : TopSurface(*placed, x, y); | |
| 274 | 123886 | return top ? std::fmax(*top, 0.0) : 0.0; | |
| 275 | } | ||
| 276 | |||
| 277 | 49074 | double BoulderLayer::RockCover(const Relief& relief, const Vec2d& p) const { | |
| 278 | 49074 | const bool rock = | |
| 279 | 50364 | relief.rock && RockRelief(p.x, p.y, *relief.boulder, relief.Shape(), relief.width) > 0.0; | |
| 280 | 26388 | return rock ? relief.weight : 0.0; | |
| 281 | } | ||
| 282 | |||
| 283 | 3575 | double BoulderLayer::ModelCover(const Boulder& boulder, const Vec2d& p, double spacing) const { | |
| 284 | 3575 | const Vec2d offset = m_surface.Offset({boulder.x, boulder.y}, p); | |
| 285 | 3575 | const bool under = boulder.model && std::hypot(offset.x, offset.y) < 0.5 * boulder.diameter; | |
| 286 | 429 | return under ? ResolutionWeight(boulder.diameter, spacing, m_options.fade) : 0.0; | |
| 287 | } | ||
| 288 | |||
| 289 | 76654 | std::vector<Boulder> BoulderLayer::ReliefBoulders(const Bounds2d& region, double spacing, | |
| 290 | const Ground* ground, unsigned threads) const { | ||
| 291 | 76654 | const double rim = m_options.rendering.relief == BoulderRelief::Dome ? 0.5 * spacing : 0.0; | |
| 292 | 76654 | return BoulderRecords(m_surface.Expand(region, rim), spacing, ground, threads); | |
| 293 | } | ||
| 294 | |||
| 295 | 22421 | double BoulderLayer::MaterialCoverAt(double x, double y, const LayerContext& context) const { | |
| 296 | 22421 | const double spacing = context.spacing; | |
| 297 | 22421 | const std::vector<Boulder> boulders = | |
| 298 | 22421 | ReliefBoulders({x, y, x, y}, spacing, context.ground, context.threads); | |
| 299 | 22421 | double cover = 0.0; | |
| 300 | 24149 | for (const Relief& relief : PrepareRelief(boulders, spacing, false)) { | |
| 301 | 1728 | cover = std::fmax(cover, RockCover(relief, {x, y})); | |
| 302 | 22421 | } | |
| 303 | 25609 | for (const Boulder& boulder : boulders) { | |
| 304 | 3188 | cover = std::fmax(cover, ModelCover(boulder, {x, y}, spacing)); | |
| 305 | } | ||
| 306 | 22421 | return cover; | |
| 307 | 22421 | } | |
| 308 | |||
| 309 | 9 | Raster<double> BoulderLayer::MaterialCoverOver(const RasterGrid& grid, | |
| 310 | const LayerContext& context) const { | ||
| 311 | 9 | const double spacing = context.spacing; | |
| 312 | 9 | const unsigned threads = context.threads; | |
| 313 | 9 | Raster<double> cover(grid); | |
| 314 | 9 | const std::vector<Boulder> boulders = | |
| 315 | 9 | ReliefBoulders(grid.Bounds(), spacing, context.ground, context.threads); | |
| 316 | 9 | std::vector<Relief> reliefs = PrepareRelief(boulders, spacing, false); | |
| 317 | 9 | reliefs.erase(std::remove_if(reliefs.begin(), reliefs.end(), | |
| 318 | [](const Relief& relief) { return !relief.rock; }), | ||
| 319 | 9 | reliefs.end()); | |
| 320 | 9 | ForEachFeatureCell(cover, threads, Boxes(reliefs), m_surface, | |
| 321 | 9 | [&](double& cell, const FeatureCell& at) { | |
| 322 | 47355 | cell = std::fmax(cell, RockCover(reliefs[at.feature], at.center)); | |
| 323 | 47318 | }); | |
| 324 | 9 | std::vector<const Boulder*> models; | |
| 325 | 9 | std::vector<Bounds2d> boxes; | |
| 326 | 4004 | for (const Boulder& boulder : boulders) { | |
| 327 | 3995 | if (boulder.model) { | |
| 328 | 11 | models.push_back(&boulder); | |
| 329 | 22 | boxes.push_back(ReliefBox(boulder.x, boulder.y, 0.5 * boulder.diameter, m_surface)); | |
| 330 | } | ||
| 331 | } | ||
| 332 | 9 | ForEachFeatureCell(cover, threads, boxes, m_surface, [&](double& cell, const FeatureCell& at) { | |
| 333 | 387 | cell = std::fmax(cell, ModelCover(*models[at.feature], at.center, spacing)); | |
| 334 | 387 | }); | |
| 335 | 9 | return cover; | |
| 336 | 9 | } | |
| 337 | |||
| 338 | 11115 | void BoulderLayer::CutFacets(TriangleMesh& placed, const Boulder& local, double base) const { | |
| 339 | 11142 | for (std::uint8_t k = 0; k < local.facetCount; ++k) { | |
| 340 | 27 | const BoulderFacet& facet = local.facets[k]; | |
| 341 | 27 | const Vec3d normal{std::cos(facet.azimuth) * std::sin(facet.angle), | |
| 342 | 27 | std::sin(facet.azimuth) * std::sin(facet.angle), std::cos(facet.angle)}; | |
| 343 | 27 | std::vector<double> distances; | |
| 344 | 27 | distances.reserve(placed.positions.size()); | |
| 345 | 15889 | for (const Vec3d& p : placed.positions) { | |
| 346 | 15862 | distances.push_back((p.z - base - facet.HeightAt({p.x - local.x, p.y - local.y})) * | |
| 347 | 15862 | normal.z); | |
| 348 | } | ||
| 349 | 27 | CutAtPlane(placed, normal, std::move(distances)); | |
| 350 | 27 | } | |
| 351 | 11115 | } | |
| 352 | |||
| 353 | 2 | BoulderShape BoulderLayer::Abraded(const BoulderShape& shape, const BoulderPlacement& placement, | |
| 354 | const Boulder& local, double base) const { | ||
| 355 | 2 | BoulderShape cut = shape; | |
| 356 | 4 | cut.mesh = PlaceMesh(std::move(cut.mesh), placement); | |
| 357 | 2 | CutFacets(cut.mesh, local, base); | |
| 358 | 721 | for (Vec3d& p : cut.mesh.positions) { | |
| 359 | 719 | p = placement.Unapply(p); | |
| 360 | } | ||
| 361 | 2 | return cut; | |
| 362 | } | ||
| 363 | |||
| 364 | 54233 | std::vector<Bounds2d> BoulderLayer::Boxes(const std::vector<Relief>& reliefs) { | |
| 365 | 54233 | std::vector<Bounds2d> boxes; | |
| 366 | 54233 | boxes.reserve(reliefs.size()); | |
| 367 | 77828 | for (const Relief& relief : reliefs) { | |
| 368 | 23595 | boxes.push_back(relief.box); | |
| 369 | } | ||
| 370 | 54233 | return boxes; | |
| 371 | } | ||
| 372 | |||
| 373 | 76654 | std::vector<BoulderLayer::Relief> BoulderLayer::PrepareRelief(const std::vector<Boulder>& boulders, | |
| 374 | double spacing, bool fillets) const { | ||
| 375 | 76654 | const BoulderRelief look = m_options.rendering.relief; | |
| 376 | 76654 | const bool filleted = m_options.fillet.heightShare > 0.0; | |
| 377 | 76654 | std::vector<Relief> reliefs; | |
| 378 | 139445 | for (const Boulder& boulder : boulders) { | |
| 379 | 62791 | const double weight = ResolutionWeight(boulder.diameter, spacing, m_options.fade); | |
| 380 | 62791 | const bool drawn = m_options.rendering.reliefRange.Holds(boulder.diameter, boulder.model); | |
| 381 | 62791 | const bool rock = drawn && look != BoulderRelief::None; | |
| 382 | 62791 | const bool apron = filleted && (drawn || boulder.model); | |
| 383 | 62791 | if (!(rock || apron) || weight <= 0.0) { | |
| 384 | 37468 | continue; | |
| 385 | } | ||
| 386 | 25323 | const double width = look == BoulderRelief::Dome ? spacing : 0.0; | |
| 387 | 25323 | Relief relief{&boulder, weight, rock, width, std::nullopt, {}}; | |
| 388 | 25323 | double reach = 0.5 * (boulder.diameter + width); | |
| 389 | 25323 | if (rock && look == BoulderRelief::Shape) { | |
| 390 | 8347 | relief.placed = PlacedOnLevel(boulder); | |
| 391 | 8347 | const Boulder local = InLocalFrame(boulder); | |
| 392 | 8347 | reach = ShapeReach(*relief.placed, local.x, local.y); | |
| 393 | } | ||
| 394 | 25323 | if (filleted) { | |
| 395 | 7566 | reach = std::fmax(reach, FilletReach(boulder, m_options.fillet)); | |
| 396 | } | ||
| 397 | 25323 | if (filleted && fillets) { | |
| 398 | 6930 | const double top = m_options.fillet.heightShare * boulder.ExposedHeight(); | |
| 399 | 6930 | if (rock && look == BoulderRelief::Dome) { | |
| 400 | 1290 | if (boulder.facetCount == 0) { | |
| 401 | 646 | relief.contacts.push_back(RoundDomeContact(boulder, width, top)); | |
| 402 | } | ||
| 403 | } else { | ||
| 404 | 5640 | std::optional<TriangleMesh> own; | |
| 405 | 5640 | const TriangleMesh* shape = relief.Shape(); | |
| 406 | 2766 | if (shape == nullptr) { | |
| 407 | 2766 | own = PlacedOnLevel(boulder); | |
| 408 | 2766 | shape = &*own; | |
| 409 | } | ||
| 410 | 5640 | const Boulder local = InLocalFrame(boulder); | |
| 411 | ✗ | relief.contacts = ShapeContacts(*shape, {local.x, local.y}, top, | |
| 412 | 11280 | FilletDirections(boulder.diameter, spacing)); | |
| 413 | 5640 | double farthest = 0.5 * boulder.diameter; | |
| 414 | 1447325 | for (const FilletContact& contact : relief.contacts) { | |
| 415 | 1441685 | farthest = std::fmax(farthest, contact.distance); | |
| 416 | } | ||
| 417 | 5640 | reach = std::fmax(reach, farthest + m_options.fillet.widthRatio * boulder.diameter); | |
| 418 | 5640 | } | |
| 419 | } | ||
| 420 | 25323 | relief.box = ReliefBox(boulder.x, boulder.y, reach, m_surface); | |
| 421 | 25323 | reliefs.push_back(std::move(relief)); | |
| 422 | 25323 | } | |
| 423 | 76654 | return reliefs; | |
| 424 | } | ||
| 425 | |||
| 426 | 57122 | void BoulderLayer::ApplySamples(SampleBlock& samples, const LayerContext& context) const { | |
| 427 | 57122 | if (m_options.rendering.relief == BoulderRelief::None && m_options.fillet.heightShare <= 0.0) { | |
| 428 | 2898 | return; | |
| 429 | } | ||
| 430 | 54224 | const double spacing = samples.ResolvedSpacing(context.spacing, m_surface); | |
| 431 | 54224 | const std::vector<Boulder> boulders = | |
| 432 | 54224 | ReliefBoulders(samples.Bounds(), spacing, context.ground, context.threads); | |
| 433 | 54224 | const std::vector<Relief> reliefs = PrepareRelief(boulders, spacing, true); | |
| 434 | 54224 | DrawFeatures(samples, context.threads, Boxes(reliefs), m_surface, | |
| 435 | 54224 | [&](float height, float base, const Vec2d& p, std::size_t n) { | |
| 436 | 332833 | const Relief& relief = reliefs[n]; | |
| 437 | 332833 | return AddRelief(height, base, p.x, p.y, *relief.boulder, relief.Shape(), | |
| 438 | 332833 | relief.weight, relief.rock, relief.width, relief.contacts); | |
| 439 | }); | ||
| 440 | 54224 | } | |
| 441 | |||
| 442 | 67 | std::vector<BoulderModel> BoulderLayer::Models(const std::vector<Boulder>& boulders) const { | |
| 443 | 67 | std::vector<const Boulder*> placed; | |
| 444 | 47775 | for (const Boulder& boulder : boulders) { | |
| 445 | 47708 | if (boulder.model && boulder.ground) { | |
| 446 | 874 | placed.push_back(&boulder); | |
| 447 | } | ||
| 448 | } | ||
| 449 | 67 | std::vector<BoulderModel> models(placed.size()); | |
| 450 | 67 | ParallelFor(placed.size(), 0, [&](std::size_t n) { | |
| 451 | 874 | const Boulder& boulder = *placed[n]; | |
| 452 | 874 | std::shared_ptr<const BoulderShape> shape = Shape(boulder); | |
| 453 | 874 | const Boulder local = InLocalFrame(boulder); | |
| 454 | 874 | const BoulderPlacement placement = | |
| 455 | 874 | PlaceBoulderModel(local, *shape, *boulder.ground, Settles(boulder)); | |
| 456 | 874 | if (boulder.facetCount > 0) { | |
| 457 | 2 | shape = std::make_shared<const BoulderShape>( | |
| 458 | 4 | Abraded(*shape, placement, local, *boulder.ground)); | |
| 459 | } | ||
| 460 | 1748 | models[n] = {boulder, ShapeSeed(boulder, m_options.rendering.shapePrototypes), shape, | |
| 461 | 874 | placement}; | |
| 462 | 2622 | }); | |
| 463 | 67 | return models; | |
| 464 | 67 | } | |
| 465 | |||
| 466 | } // namespace terraingen | ||
| 467 |