src/albedo/blast_zones.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/albedo/blast_zones.h" | ||
| 4 | |||
| 5 | #include "albedo/bands.h" | ||
| 6 | #include "terraingen/albedo/albedo_fields.h" | ||
| 7 | #include "terraingen/geometry/geometry.h" | ||
| 8 | #include "terraingen/random/streams.h" | ||
| 9 | |||
| 10 | #include <cmath> | ||
| 11 | #include <stdexcept> | ||
| 12 | #include <utility> | ||
| 13 | |||
| 14 | namespace terraingen { | ||
| 15 | |||
| 16 | namespace { | ||
| 17 | |||
| 18 | constexpr double kFullRadius = 0.3; | ||
| 19 | |||
| 20 | /// The zone's radial profile at `rho` of its radius: 1 to `kFullRadius`, fading to 0 at 1. | ||
| 21 | 176663 | double Profile(double rho) { | |
| 22 | 176663 | return FadeOut(rho, kFullRadius, 1.0 - kFullRadius); | |
| 23 | } | ||
| 24 | |||
| 25 | /// The semi-axes (long, short) in meters of `zone`. | ||
| 26 | 623544 | std::pair<double, double> SemiAxes(const BlastZone& zone) { | |
| 27 | 623544 | const double minor = std::sqrt(zone.area / (kPi * zone.elongation)); | |
| 28 | 623544 | return {zone.elongation * minor, minor}; | |
| 29 | } | ||
| 30 | |||
| 31 | } // namespace | ||
| 32 | |||
| 33 | 27 | void ValidateBlastZone(const BlastZone& zone) { | |
| 34 | 27 | CheckRanges(zone); | |
| 35 | 10 | } | |
| 36 | |||
| 37 | 270022 | double BlastZoneMeanShare() { | |
| 38 | 6 | static const double share = [] { | |
| 39 | 6 | constexpr int kSteps = 1 << 14; | |
| 40 | 6 | double sum = 0.0; | |
| 41 | 98310 | for (int i = 0; i < kSteps; ++i) { | |
| 42 | 98304 | const double rho = (i + 0.5) / kSteps; | |
| 43 | 98304 | sum += Profile(rho) * 2.0 * rho / kSteps; | |
| 44 | } | ||
| 45 | 6 | return sum; | |
| 46 | 270022 | }(); | |
| 47 | 270022 | return share; | |
| 48 | } | ||
| 49 | |||
| 50 | 16 | BlastZoneAlbedo::BlastZoneAlbedo(std::vector<BlastZone> zones) : m_zones(std::move(zones)) { | |
| 51 | 24 | for (const BlastZone& zone : m_zones) { | |
| 52 | 16 | ValidateBlastZone(zone); | |
| 53 | } | ||
| 54 | 16 | } | |
| 55 | |||
| 56 | 353534 | double BlastZoneAlbedo::Reach(const BlastZone& zone) { | |
| 57 | 353534 | return std::fmax(SemiAxes(zone).first * zone.haloReach, zone.coreRadius); | |
| 58 | } | ||
| 59 | |||
| 60 | 354062 | double BlastZoneAlbedo::Change(const BlastZone& zone, double x, double y, const Surface& surface) { | |
| 61 | 354062 | const Vec2d offset = surface.Offset({zone.x, zone.y}, {x, y}); | |
| 62 | 354064 | const double distance = std::hypot(offset.x, offset.y); | |
| 63 | 354064 | if (distance >= Reach(zone)) { | |
| 64 | return 0.0; | ||
| 65 | } | ||
| 66 | 270018 | const auto [major, minor] = SemiAxes(zone); | |
| 67 | 270018 | const double heading = zone.headingDegrees * kDegree; | |
| 68 | 270018 | const double u = (offset.x * std::cos(heading) + offset.y * std::sin(heading)) / major; | |
| 69 | 270018 | const double v = (offset.y * std::cos(heading) - offset.x * std::sin(heading)) / minor; | |
| 70 | 540037 | const double lobe = EdgeLobe(zone.raggedness, kLobeRing, std::atan2(v, u), 0.0, | |
| 71 | 270018 | zone.seed ^ kBlastZoneStream, 3); | |
| 72 | 270019 | const double rho = std::hypot(u, v) / lobe; | |
| 73 | 270019 | const double share = BlastZoneMeanShare(); | |
| 74 | 270019 | const double peak = (zone.change - zone.haloChange * (1.0 - share)) / share; | |
| 75 | 270019 | const double inner = rho < 1.0 ? Profile(rho) : 0.0; | |
| 76 | 270019 | const double halo = rho <= 1.0 ? 1.0 | |
| 77 | 191660 | : zone.haloReach > 1.0 ? FadeOut(rho, 1.0, zone.haloReach - 1.0) | |
| 78 | 270019 | : 0.0; | |
| 79 | 270019 | double change = peak * inner + (1.0 - inner) * zone.haloChange * halo; | |
| 80 | 270019 | if (zone.coreRadius > 0.0) { | |
| 81 | 241236 | const double core = FadeOut(distance / zone.coreRadius, 0.7, 0.3); | |
| 82 | 241236 | change = zone.coreChange * core + (1.0 - core) * change; | |
| 83 | } | ||
| 84 | return change; | ||
| 85 | } | ||
| 86 | |||
| 87 | 324897 | void BlastZoneAlbedo::ApplyAt(AlbedoSample& value, std::size_t bands, double x, double y, | |
| 88 | const LayerContext& context) const { | ||
| 89 | 324897 | double change = 0.0; | |
| 90 | 649660 | for (const BlastZone& zone : m_zones) { | |
| 91 | 324905 | change += Change(zone, x, y, context.SurfaceOrPlane()); | |
| 92 | } | ||
| 93 | 324755 | ChangeBands(value, bands, change); | |
| 94 | 324877 | } | |
| 95 | |||
| 96 | } // namespace terraingen | ||
| 97 |