src/albedo/crater_albedo.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/albedo/crater_albedo.h" | ||
| 4 | |||
| 5 | #include "albedo/bands.h" | ||
| 6 | #include "albedo/zone_shadows.h" | ||
| 7 | #include "terraingen/geometry/geometry.h" | ||
| 8 | #include "terraingen/layers/craters/secondaries.h" | ||
| 9 | #include "terraingen/layers/craters/shaping/irregularity.h" | ||
| 10 | #include "terraingen/noise/hash.h" | ||
| 11 | #include "terraingen/noise/value.h" | ||
| 12 | #include "terraingen/random/stream.h" | ||
| 13 | #include "terraingen/raster/grid.h" | ||
| 14 | #include "terraingen/util/log.h" | ||
| 15 | #include "util/checks.h" | ||
| 16 | |||
| 17 | #include <algorithm> | ||
| 18 | #include <cmath> | ||
| 19 | #include <limits> | ||
| 20 | #include <optional> | ||
| 21 | #include <stdexcept> | ||
| 22 | #include <utility> | ||
| 23 | |||
| 24 | namespace terraingen { | ||
| 25 | |||
| 26 | namespace { | ||
| 27 | |||
| 28 | constexpr std::size_t kBlockRows = 32; | ||
| 29 | |||
| 30 | /// How far `crater`'s ejecta edge reaches at `angle` radians around it, over its mean reach. | ||
| 31 | 2043214 | double LobeScale(double angle, std::uint32_t key, const CraterAlbedoTexture& texture) { | |
| 32 | 2043214 | if (texture.lobes == 0.0) { | |
| 33 | return 1.0; | ||
| 34 | } | ||
| 35 | 2712 | const double ring = static_cast<double>(texture.lobeCount) / (2.0 * kPi); | |
| 36 | 2712 | const double n = RingNoise(ring, angle, 0.0, key ^ kEjectaLobeStream, 2); | |
| 37 | 5424 | return 1.0 + texture.lobes * std::clamp(n, -1.0, 1.0); | |
| 38 | } | ||
| 39 | |||
| 40 | /// The share of the excess the filaments leave at `radii` radii and `angle` radians around a | ||
| 41 | /// crater: 1 along a filament, down to 1 - `streaks` between them. | ||
| 42 | 140487 | double StreakShare(double radii, double angle, std::uint32_t key, | |
| 43 | const CraterAlbedoTexture& texture) { | ||
| 44 | 140487 | if (texture.streaks == 0.0) { | |
| 45 | return 1.0; | ||
| 46 | } | ||
| 47 | 3956 | const double ring = kRadian / texture.streakWidthDegrees; | |
| 48 | 3956 | const double n = RingNoise(ring, angle, radii / texture.streakLengthRadii, key, 3); | |
| 49 | 3956 | const double filament = Smoothstep(0.5 + 1.5 * n); | |
| 50 | 3956 | return 1.0 - texture.streaks * (1.0 - filament); | |
| 51 | } | ||
| 52 | |||
| 53 | /// One reflectance zone of a crater, drawn. | ||
| 54 | struct DrawnZone { | ||
| 55 | double change = 0.0; | ||
| 56 | double strength = 0.0; | ||
| 57 | double resolution = 0.0; | ||
| 58 | double reach = 0.0; | ||
| 59 | double raggedness = 0.0; | ||
| 60 | double ring = 0.0; | ||
| 61 | double feather = 1.0; | ||
| 62 | bool streaked = false; | ||
| 63 | bool distal = false; | ||
| 64 | std::uint32_t key = 0; | ||
| 65 | |||
| 66 | /// How much of the change the zone gives at `distance` meters and `angle` radians from the | ||
| 67 | /// crater's center, from 1 inside to 0 beyond its ragged edge, whose fingers bend and break | ||
| 68 | /// with distance. | ||
| 69 | 3869281 | double Cover(double distance, double angle) const { | |
| 70 | 3869281 | if (distance >= reach) { | |
| 71 | return 0.0; | ||
| 72 | } | ||
| 73 | 1033229 | const double finger = EdgeLobe(raggedness, ring, angle, 3.0 * distance / reach, key, 4); | |
| 74 | 1033164 | return SoftEdge(distance / (reach * finger), feather); | |
| 75 | } | ||
| 76 | }; | ||
| 77 | |||
| 78 | /// `zone` drawn from the next draws of `random` around a crater `diameter` meters across with | ||
| 79 | /// `life` of its zone life left; nothing when the crater lacks it or it has faded. | ||
| 80 | 24220 | std::optional<DrawnZone> DrawZone(const ReflectanceZone& zone, RandomStream& random, | |
| 81 | double diameter, double life, double fadeExponent, | ||
| 82 | double resolution) { | ||
| 83 | 24220 | const double present = random.NextDouble(); | |
| 84 | 24220 | const double c1 = random.NextDouble(); | |
| 85 | 24220 | const double c2 = random.NextDouble(); | |
| 86 | 24220 | const double s1 = random.NextDouble(); | |
| 87 | 24220 | const double s2 = random.NextDouble(); | |
| 88 | 24220 | DrawnZone drawn; | |
| 89 | 24220 | drawn.key = random.NextU32(); | |
| 90 | 24220 | if (present >= zone.share || life <= 0.0) { | |
| 91 | 6918 | return std::nullopt; | |
| 92 | } | ||
| 93 | 17302 | drawn.change = zone.change.Sample(c1, c2); | |
| 94 | 17302 | drawn.strength = std::pow(life, fadeExponent); | |
| 95 | 17302 | drawn.resolution = resolution; | |
| 96 | 17302 | drawn.reach = zone.coefficient * std::pow(diameter, zone.exponent) * zone.scale.Sample(s1, s2); | |
| 97 | 17302 | drawn.raggedness = zone.raggedness; | |
| 98 | 17302 | drawn.ring = kRadian / zone.fingerWidthDegrees; | |
| 99 | 17302 | drawn.feather = zone.feather; | |
| 100 | 17302 | return drawn; | |
| 101 | } | ||
| 102 | |||
| 103 | } // namespace | ||
| 104 | |||
| 105 | /// One ray, in the frame of its crater. | ||
| 106 | struct CraterAlbedo::Ray { | ||
| 107 | double cos = 1.0; | ||
| 108 | double sin = 0.0; | ||
| 109 | double length = 0.0; | ||
| 110 | double sinHalfWidth = 0.0; | ||
| 111 | std::uint32_t seed = 0; | ||
| 112 | |||
| 113 | /// The ray's excess at `along` meters from the crater's center along it and `lateral` across, | ||
| 114 | /// for a crater of `radius` meters. | ||
| 115 | 45804853 | double ExcessAt(double along, double lateral, double radius, const CraterRays& rays) const { | |
| 116 | 45804853 | if (along <= radius || along >= length) { | |
| 117 | return 0.0; | ||
| 118 | } | ||
| 119 | 8991944 | const double half = std::fmin(along, 2.0 * radius) * sinHalfWidth; | |
| 120 | 8991944 | if (lateral >= half) { | |
| 121 | return 0.0; | ||
| 122 | } | ||
| 123 | 69222 | const double q = 1.0 - (lateral / half) * (lateral / half); | |
| 124 | 69222 | double strength = rays.excess * q * q * (length - along) / (length - radius); | |
| 125 | 69222 | if (rays.patchiness > 0.0) { | |
| 126 | 66992 | const double wavelength = rays.patchRadii * radius; | |
| 127 | 66992 | const double u = along / wavelength; | |
| 128 | 66992 | const double iu = std::floor(u); | |
| 129 | 66992 | const float noise = ValueNoise2(static_cast<std::int32_t>(iu), 0, | |
| 130 | 66992 | static_cast<float>(u - iu), 0.5f, seed); | |
| 131 | 66992 | strength *= 1.0 - rays.patchiness * (0.5 + 0.5 * static_cast<double>(noise)); | |
| 132 | } | ||
| 133 | return strength; | ||
| 134 | } | ||
| 135 | }; | ||
| 136 | |||
| 137 | /// A crater's bright material, ready to evaluate at points. | ||
| 138 | struct CraterAlbedo::Prepared { | ||
| 139 | Crater crater; | ||
| 140 | CraterOutline outline; | ||
| 141 | KernelOutline profile; ///< `outline` as the crater kernel reads it. | ||
| 142 | double weight = 0.0; | ||
| 143 | double reach = 0.0; | ||
| 144 | std::vector<Ray> rays; | ||
| 145 | std::vector<DrawnZone> zones; | ||
| 146 | std::shared_ptr<const detail::ShadowProfile> shadow; | ||
| 147 | |||
| 148 | /// The relative reflectance change of the zones at (`x`, `y`), inner zones covering outer as | ||
| 149 | /// long as they last, the bright proximal zone broken by `texture`'s filaments. | ||
| 150 | 1592143 | double Change(const CraterAlbedoTexture& texture, const Surface& surface, double x, | |
| 151 | double y) const { | ||
| 152 | 1592143 | if (zones.empty()) { | |
| 153 | return 0.0; | ||
| 154 | } | ||
| 155 | 1289226 | const Vec2d offset = surface.Offset({crater.x, crater.y}, {x, y}); | |
| 156 | 1289050 | const double distance = std::hypot(offset.x, offset.y); | |
| 157 | 1289050 | const double angle = std::atan2(offset.y, offset.x); | |
| 158 | 1289050 | const double shade = shadow ? shadow->Shade(distance, angle) : 0.0; | |
| 159 | 1289060 | ChangeOverlay overlay; | |
| 160 | 5157711 | for (const DrawnZone& zone : zones) { | |
| 161 | 3868643 | const double cover = zone.Cover(distance, angle) * (zone.distal ? 1.0 - shade : 1.0); | |
| 162 | 3868651 | if (cover == 0.0) { | |
| 163 | 3334024 | continue; | |
| 164 | } | ||
| 165 | 534627 | const double streak = zone.streaked | |
| 166 | 534627 | ? StreakShare(2.0 * distance / crater.diameter, angle, | |
| 167 | 1934 | CraterKey(crater.id, texture.seed), texture) | |
| 168 | 534627 | : 1.0; | |
| 169 | 534627 | overlay.Add(cover * zone.strength, zone.resolution * streak * zone.change); | |
| 170 | } | ||
| 171 | return overlay.Change(); | ||
| 172 | } | ||
| 173 | |||
| 174 | /// The excess optical maturity at (`x`, `y`). | ||
| 175 | 2057352 | double Excess(const CraterAlbedoOptions& o, const Surface& surface, double x, double y) const { | |
| 176 | 2057352 | if (weight == 0.0) { | |
| 177 | return 0.0; | ||
| 178 | } | ||
| 179 | 2057476 | const double radii = profile.ProfileDistance(surface.Offset({crater.x, crater.y}, {x, y})) / | |
| 180 | 2057268 | (0.5 * crater.diameter); | |
| 181 | 2057268 | const CraterAlbedoTexture& texture = o.texture; | |
| 182 | 2057268 | const bool textured = texture.streaks > 0.0 || texture.lobes > 0.0; | |
| 183 | 2057268 | const Vec2d offset = surface.Offset({crater.x, crater.y}, {x, y}); | |
| 184 | 2057509 | const double angle = textured ? std::atan2(offset.y, offset.x) : 0.0; | |
| 185 | 3317 | const std::uint32_t key = textured ? CraterKey(crater.id, texture.seed) : 0u; | |
| 186 | 2057509 | if (radii < 1.0) { | |
| 187 | 14443 | return InteriorExcess(o, radii, angle, key); | |
| 188 | } | ||
| 189 | 2043066 | const double excess = OutsideExcess(o, offset, radii, angle, key); | |
| 190 | 2044898 | if (excess == 0.0) { | |
| 191 | return 0.0; | ||
| 192 | } | ||
| 193 | 124255 | const double streak = StreakShare(radii, angle, key, texture); | |
| 194 | 124289 | if (texture.continuousRadii > 0.0) { | |
| 195 | 2667 | const double ramp = SmoothRamp(radii, 1.0, texture.continuousRadii); | |
| 196 | 2667 | return weight * excess * (1.0 - (1.0 - streak) * ramp); | |
| 197 | } | ||
| 198 | 121622 | return weight * excess * streak; | |
| 199 | } | ||
| 200 | |||
| 201 | /// The weighted excess inside the rim, `radii` radii and `angle` radians from the center, | ||
| 202 | /// its filaments keyed by `key`. | ||
| 203 | 14442 | double InteriorExcess(const CraterAlbedoOptions& o, double radii, double angle, | |
| 204 | std::uint32_t key) const { | ||
| 205 | 14442 | const CraterAlbedoTexture& texture = o.texture; | |
| 206 | 14442 | double interior = o.interiorExcess; | |
| 207 | 14442 | const double wall = SmoothRamp(radii, 0.3, 0.6); | |
| 208 | 14442 | if (texture.floorShare < 1.0) { | |
| 209 | 569 | interior *= texture.floorShare + (1.0 - texture.floorShare) * wall; | |
| 210 | } | ||
| 211 | 14442 | const double streak = StreakShare(radii, angle, key, texture); | |
| 212 | 14443 | return weight * interior * (1.0 - (1.0 - streak) * wall); | |
| 213 | } | ||
| 214 | |||
| 215 | /// The unweighted excess of the ejecta and rays beyond the rim at `offset` meters from the | ||
| 216 | /// center, `radii` radii and `angle` radians, before the filaments. | ||
| 217 | 2043214 | double OutsideExcess(const CraterAlbedoOptions& o, const Vec2d& offset, double radii, | |
| 218 | double angle, std::uint32_t key) const { | ||
| 219 | 2043214 | double excess = 0.0; | |
| 220 | 2043214 | const double lobed = radii / LobeScale(angle, key, o.texture); | |
| 221 | 2043265 | if (lobed < o.ejectaReachRadii) { | |
| 222 | 100739 | const double edge = std::pow(o.ejectaReachRadii, -o.ejectaExponent); | |
| 223 | 100739 | excess = o.ejectaExcess * (std::pow(std::fmax(lobed, 1.0), -o.ejectaExponent) - edge) / | |
| 224 | 100739 | (1.0 - edge); | |
| 225 | } | ||
| 226 | 2043265 | const double radius = 0.5 * crater.diameter; | |
| 227 | 47937087 | for (const Ray& ray : rays) { | |
| 228 | 45893822 | const double along = offset.x * ray.cos + offset.y * ray.sin; | |
| 229 | 45893822 | const double lateral = std::fabs(offset.y * ray.cos - offset.x * ray.sin); | |
| 230 | 45893822 | excess += ray.ExcessAt(along, lateral, radius, o.rays); | |
| 231 | } | ||
| 232 | 2043265 | return excess; | |
| 233 | } | ||
| 234 | }; | ||
| 235 | |||
| 236 | 402 | CraterAlbedo::CraterAlbedo(std::shared_ptr<const CraterLayer> craters, CraterAlbedoOptions options) | |
| 237 | 402 | : m_craters(std::move(craters)), m_options(options) { | |
| 238 | 402 | if (!m_craters) { | |
| 239 | 1 | throw std::invalid_argument("Crater albedo needs a crater layer"); | |
| 240 | } | ||
| 241 | 401 | ValidateCraterAlbedoOptions(m_options); | |
| 242 | 422 | } | |
| 243 | |||
| 244 | 519498 | bool CraterAlbedo::Eligible(const Crater& crater, RandomStream& random) const { | |
| 245 | 519498 | return m_options.selection.Includes(crater) && random.NextDouble() < m_options.share; | |
| 246 | } | ||
| 247 | |||
| 248 | 30012962 | CraterAlbedo::Ray CraterAlbedo::DrawRay(RandomStream& random, double median, | |
| 249 | const CraterImpact& impact, | ||
| 250 | const RayDirection* direction) const { | ||
| 251 | 30012962 | const CraterRays& rays = m_options.rays; | |
| 252 | 30012962 | const ObliqueImpacts& oblique = rays.oblique; | |
| 253 | 30012962 | const double asymmetry = (impact.speedRatio - 1.0) / (impact.speedRatio + 1.0); | |
| 254 | 30012962 | const double zone = oblique.zoneHalfWidthDegrees * kDegree; | |
| 255 | 30012962 | const double drawn = 2.0 * kPi * random.NextDouble(); | |
| 256 | 30012962 | const double azimuth = direction != nullptr ? direction->azimuth : drawn; | |
| 257 | 30012962 | const double l1 = random.NextDouble(); | |
| 258 | 30012962 | const double l2 = random.NextDouble(); | |
| 259 | 30012962 | const double w1 = random.NextDouble(); | |
| 260 | 30012962 | const double w2 = random.NextDouble(); | |
| 261 | 30012962 | Ray ray; | |
| 262 | 30012962 | ray.cos = std::cos(azimuth); | |
| 263 | 30012962 | ray.sin = std::sin(azimuth); | |
| 264 | 30012962 | ray.length = median * std::exp(rays.lengthSpread.Sample(l1, l2)); | |
| 265 | 30012962 | const double fromDownrange = std::remainder(azimuth - impact.heading, 2.0 * kPi); | |
| 266 | 30012962 | if (asymmetry > 0.0) { | |
| 267 | 15439666 | const double speed = 1.0 + asymmetry * std::cos(fromDownrange); | |
| 268 | 15439666 | ray.length *= speed * speed / (1.0 + 0.5 * asymmetry * asymmetry); | |
| 269 | } | ||
| 270 | 30012962 | const bool uprange = std::fabs(fromDownrange) > kPi - zone; | |
| 271 | 30012962 | const bool downrange = std::fabs(fromDownrange) < zone; | |
| 272 | 30012962 | if ((oblique.enabled && impact.angleDegrees < oblique.forbiddenDegrees && uprange) || | |
| 273 | 28243629 | (oblique.enabled && impact.angleDegrees < oblique.butterflyDegrees && downrange)) { | |
| 274 | 1950097 | ray.length = 0.0; | |
| 275 | } | ||
| 276 | 60025924 | ray.sinHalfWidth = direction != nullptr | |
| 277 | 30012962 | ? direction->sinHalfWidth | |
| 278 | 30007138 | : std::sin(0.5 * rays.widthDegrees.Sample(w1, w2) * kDegree); | |
| 279 | 30012962 | ray.seed = random.NextU32(); | |
| 280 | 30012962 | return ray; | |
| 281 | } | ||
| 282 | |||
| 283 | 479202 | void CraterAlbedo::AddRays(Prepared& prepared, RandomStream& random) const { | |
| 284 | 479202 | const Crater& crater = prepared.crater; | |
| 285 | 479202 | const double c1 = random.NextDouble(); | |
| 286 | 479202 | const double c2 = random.NextDouble(); | |
| 287 | 479202 | const std::vector<RayDirection> directions = m_options.rays.layout | |
| 288 | 479202 | ? RayDirections(crater, *m_options.rays.layout) | |
| 289 | 479202 | : std::vector<RayDirection>{}; | |
| 290 | 479202 | const auto count = m_options.rays.layout | |
| 291 | 479202 | ? static_cast<int>(directions.size()) | |
| 292 | 478474 | : static_cast<int>(std::lround(m_options.rays.count.Sample(c1, c2))); | |
| 293 | 479202 | const double median = m_options.rays.MedianLength(crater.diameter); | |
| 294 | 479202 | const CraterImpact impact = Impact(crater); | |
| 295 | 30492164 | for (int i = 0; i < count; ++i) { | |
| 296 | 30012962 | const Ray ray = | |
| 297 | 30018786 | DrawRay(random, median, impact, | |
| 298 | 30012962 | m_options.rays.layout ? &directions[static_cast<std::size_t>(i)] : nullptr); | |
| 299 | 30012962 | if (ray.length > 0.5 * crater.diameter) { | |
| 300 | 28041087 | prepared.reach = std::fmax(prepared.reach, ray.length); | |
| 301 | 28041087 | prepared.rays.push_back(ray); | |
| 302 | } | ||
| 303 | } | ||
| 304 | 479202 | } | |
| 305 | |||
| 306 | 6055 | void CraterAlbedo::AddZones(Prepared& prepared, double left, double resolution) const { | |
| 307 | 6055 | const CraterReflectanceZones& z = m_options.zones; | |
| 308 | 6055 | const Crater& crater = prepared.crater; | |
| 309 | 6055 | const IdWords id = SplitId(crater.id); | |
| 310 | 6055 | RandomStream random(z.seed, kAlbedoZoneStream, id.low, id.high); | |
| 311 | 6055 | const bool immature = z.excavationRatio * crater.diameter > z.matureDepth; | |
| 312 | 6055 | std::vector<DrawnZone> distal; | |
| 313 | 30275 | for (const ReflectanceZone* zone : z.All()) { | |
| 314 | 24220 | const double life = zone == &z.proximalHigh && !immature ? 0.0 | |
| 315 | 21222 | : zone->retentionMyr | |
| 316 | 21222 | ? std::fmin(left, LifeLeft(crater.ageGyr, *zone->retentionMyr)) | |
| 317 | 24220 | : left; | |
| 318 | 24220 | std::optional<DrawnZone> drawn = | |
| 319 | 24220 | DrawZone(*zone, random, crater.diameter, life, m_options.fadeExponent, resolution); | |
| 320 | 24220 | if (!drawn) { | |
| 321 | 6918 | continue; | |
| 322 | } | ||
| 323 | 17302 | drawn->streaked = zone == &z.proximalHigh; | |
| 324 | 26381 | drawn->distal = zone == &z.distalHigh || zone == &z.distalLow; | |
| 325 | 17302 | prepared.reach = std::fmax(prepared.reach, drawn->reach); | |
| 326 | 17302 | (drawn->distal ? distal : prepared.zones).push_back(*drawn); | |
| 327 | } | ||
| 328 | 6055 | std::sort(distal.begin(), distal.end(), | |
| 329 | 5356 | [](const DrawnZone& a, const DrawnZone& b) { return a.reach < b.reach; }); | |
| 330 | 6055 | prepared.zones.insert(prepared.zones.end(), distal.begin(), distal.end()); | |
| 331 | 6055 | } | |
| 332 | |||
| 333 | 519498 | CraterAlbedo::Prepared CraterAlbedo::Prepare(const Crater& crater, double spacing) const { | |
| 334 | 519498 | Prepared prepared; | |
| 335 | 519498 | prepared.crater = crater; | |
| 336 | 519498 | const CraterAlbedoOptions& o = m_options; | |
| 337 | 519498 | const IdWords id = SplitId(crater.id); | |
| 338 | 519498 | RandomStream random(o.seed, kCraterAlbedoStream, id.low, id.high); | |
| 339 | 519498 | if (!Eligible(crater, random)) { | |
| 340 | return prepared; | ||
| 341 | } | ||
| 342 | 519492 | const double left = LifeLeft(crater.ageGyr, o.retention.Myr(crater.diameter)); | |
| 343 | 519492 | const double resolution = ResolutionWeight(crater.diameter, spacing, o.fade); | |
| 344 | 519492 | if (left <= 0.0 || resolution == 0.0) { | |
| 345 | return prepared; | ||
| 346 | } | ||
| 347 | 486922 | prepared.weight = std::pow(left, o.fadeExponent) * resolution; | |
| 348 | 486922 | prepared.outline = IrregularOutline(crater, m_craters->Options().apply.irregularity); | |
| 349 | 486922 | prepared.profile = KernelOutline(prepared.outline, crater.diameter); | |
| 350 | 486922 | prepared.reach = o.ejectaReachRadii * (0.5 * crater.diameter) * prepared.outline.MaxScale() * | |
| 351 | 486922 | (1.0 + o.texture.lobes); | |
| 352 | 486922 | if (o.rays.enabled) { | |
| 353 | 479202 | AddRays(prepared, random); | |
| 354 | } | ||
| 355 | 486922 | if (o.zones.enabled && crater.diameter <= o.zones.maxDiameter) { | |
| 356 | 6055 | AddZones(prepared, left, resolution); | |
| 357 | } | ||
| 358 | return prepared; | ||
| 359 | } | ||
| 360 | |||
| 361 | 471314 | double CraterAlbedo::Excess(const Crater& crater, double x, double y, double spacing) const { | |
| 362 | 471314 | return Prepare(crater, spacing).Excess(m_options, m_craters->Options().apply.surface, x, y); | |
| 363 | } | ||
| 364 | |||
| 365 | 4324 | double CraterAlbedo::ReflectanceChange(const Crater& crater, double x, double y, double spacing, | |
| 366 | const Ground* ground) const { | ||
| 367 | 4324 | Prepared prepared = Prepare(crater, spacing); | |
| 368 | 4324 | if (ground != nullptr && m_options.zones.shadows.enabled) { | |
| 369 | 11 | AddShadows(prepared, *ground, PointBounds(x, y)); | |
| 370 | } | ||
| 371 | 8648 | return prepared.Change(m_options.texture, m_craters->Options().apply.surface, x, y); | |
| 372 | 4324 | } | |
| 373 | |||
| 374 | 79 | void CraterAlbedo::AddShadows(Prepared& prepared, const Ground& ground, | |
| 375 | const Bounds2d& region) const { | ||
| 376 | 79 | const ZoneShadows& s = m_options.zones.shadows; | |
| 377 | 79 | double reach = 0.0; | |
| 378 | 384 | for (const DrawnZone& zone : prepared.zones) { | |
| 379 | 305 | reach = zone.distal ? std::fmax(reach, zone.reach) : reach; | |
| 380 | } | ||
| 381 | 79 | const Crater& crater = prepared.crater; | |
| 382 | 79 | const double radius = 0.5 * crater.diameter; | |
| 383 | 79 | if (reach <= radius) { | |
| 384 | 4 | return; | |
| 385 | } | ||
| 386 | 75 | const Vec2d center{crater.x, crater.y}; | |
| 387 | 75 | const Surface& surface = m_craters->Options().apply.surface; | |
| 388 | 75 | const detail::RayWindow window = | |
| 389 | 75 | detail::RaysOver(center, region, surface, static_cast<std::size_t>(s.azimuths)); | |
| 390 | 75 | auto profile = std::make_shared<detail::ShadowProfile>( | |
| 391 | 150 | detail::MakeShadowProfile(s, radius, reach, window)); | |
| 392 | 75 | const std::vector<Vec2d> points = profile->Points(center, surface); | |
| 393 | 75 | std::vector<float> heights(points.size()); | |
| 394 | 75 | ground.HeightsAt(points, heights, profile->step); | |
| 395 | 75 | profile->TraceJets(heights, std::tan(s.jetAngleDegrees * kDegree) * profile->step); | |
| 396 | 75 | prepared.shadow = std::move(profile); | |
| 397 | 75 | } | |
| 398 | |||
| 399 | 499504 | CraterImpact CraterAlbedo::Impact(const Crater& crater) const { | |
| 400 | 499504 | const ObliqueImpacts& oblique = m_options.rays.oblique; | |
| 401 | 499504 | CraterImpact impact; | |
| 402 | 499504 | if (!oblique.enabled) { | |
| 403 | return impact; | ||
| 404 | } | ||
| 405 | 496613 | const IdWords id = SplitId(crater.id); | |
| 406 | 496613 | RandomStream random(m_options.seed, kObliqueRayStream, id.low, id.high); | |
| 407 | 496613 | impact.angleDegrees = std::asin(std::sqrt(random.NextDouble())) * kRadian; | |
| 408 | 496613 | const double h = random.NextDouble(); | |
| 409 | 496613 | impact.heading = oblique.headingDegrees ? *oblique.headingDegrees * kDegree : 2.0 * kPi * h; | |
| 410 | 496613 | const double r1 = random.NextDouble(); | |
| 411 | 496613 | const double r2 = random.NextDouble(); | |
| 412 | 496613 | if (impact.angleDegrees < oblique.asymmetricDegrees) { | |
| 413 | 255390 | impact.speedRatio = oblique.downrangeSpeedRatio.Sample(r1, r2); | |
| 414 | } | ||
| 415 | return impact; | ||
| 416 | } | ||
| 417 | |||
| 418 | 10 | double CraterAlbedo::Reach(const Crater& crater) const { | |
| 419 | 10 | return Prepare(crater, 0.0).reach; | |
| 420 | } | ||
| 421 | |||
| 422 | 379 | double CraterAlbedo::GatherMargin() const { | |
| 423 | 379 | const double largest = m_craters->MaxFeatureDiameter(); | |
| 424 | 379 | const double rayReach = m_options.rays.enabled | |
| 425 | 411 | ? m_options.rays.MedianLength(largest) * | |
| 426 | 32 | std::exp(m_options.rays.lengthSpread.Highest()) * | |
| 427 | 32 | m_options.rays.oblique.MaxStretch() | |
| 428 | 379 | : 0.0; | |
| 429 | 758 | return std::fmax(rayReach, 0.5 * largest * m_options.ejectaReachRadii * | |
| 430 | 379 | MaxOutlineScale(m_craters->Options().apply.irregularity) * | |
| 431 | 379 | (1.0 + m_options.texture.lobes)); | |
| 432 | } | ||
| 433 | |||
| 434 | 1387 | void CraterAlbedo::TakePrepared(const CraterList& list, const Bounds2d& region, double spacing, | |
| 435 | std::vector<Prepared>& prepared) const { | ||
| 436 | 1387 | const Surface& surface = m_craters->Options().apply.surface; | |
| 437 | 45237 | for (std::size_t i = 0; i < list.Size(); ++i) { | |
| 438 | 43850 | Prepared one = Prepare(list.At(i), spacing); | |
| 439 | 43850 | if (one.weight == 0.0) { | |
| 440 | 32573 | continue; | |
| 441 | } | ||
| 442 | 11277 | const Crater& c = one.crater; | |
| 443 | 11277 | if (surface.Contains(surface.Expand(region, one.reach), {c.x, c.y})) { | |
| 444 | 1435 | prepared.push_back(std::move(one)); | |
| 445 | } | ||
| 446 | 43850 | } | |
| 447 | 1387 | } | |
| 448 | |||
| 449 | namespace { | ||
| 450 | |||
| 451 | /// Sorts `prepared` by crater ID and drops repeats. | ||
| 452 | 379 | template <typename Prepared> void SortUniqueById(std::vector<Prepared>& prepared) { | |
| 453 | 379 | std::sort(prepared.begin(), prepared.end(), | |
| 454 | 2740 | [](const Prepared& a, const Prepared& b) { return a.crater.id < b.crater.id; }); | |
| 455 | 379 | prepared.erase(std::unique(prepared.begin(), prepared.end(), | |
| 456 | 1066 | [](const Prepared& a, const Prepared& b) { | |
| 457 | 1066 | return a.crater.id == b.crater.id; | |
| 458 | }), | ||
| 459 | 379 | prepared.end()); | |
| 460 | 379 | } | |
| 461 | |||
| 462 | /// The craters of `prepared` whose reach meets the rows of `raster` from `row0` to `row1`. | ||
| 463 | template <typename Prepared> | ||
| 464 | 143 | std::vector<const Prepared*> NearRows(const std::vector<Prepared>& prepared, | |
| 465 | const Raster<float>& raster, std::size_t row0, | ||
| 466 | std::size_t row1, const Surface& surface) { | ||
| 467 | 143 | const double ya = raster.CellCenter(0, row0).y; | |
| 468 | 143 | const double yb = raster.CellCenter(0, row1 - 1).y; | |
| 469 | 143 | std::vector<const Prepared*> near; | |
| 470 | 652 | for (const Prepared& p : prepared) { | |
| 471 | 509 | const double reachY = p.reach / surface.UnitLength(); | |
| 472 | 509 | if (p.crater.y + reachY >= std::fmin(ya, yb) && p.crater.y - reachY <= std::fmax(ya, yb)) { | |
| 473 | 209 | near.push_back(&p); | |
| 474 | } | ||
| 475 | } | ||
| 476 | 143 | return near; | |
| 477 | } | ||
| 478 | |||
| 479 | /// Moves `value` to the reflectance at the background maturity plus `excess`, capped, then | ||
| 480 | /// changes it by the relative reflectance `change`, never below 0. | ||
| 481 | 1744804 | void Brighten(AlbedoSample& value, std::size_t bands, double excess, double change, | |
| 482 | const CraterAlbedoOptions& o) { | ||
| 483 | 1744804 | if (excess == 0.0 && change == 0.0) { | |
| 484 | return; | ||
| 485 | } | ||
| 486 | 647737 | const double factor = std::fmax(0.0, 1.0 + change); | |
| 487 | 647737 | if (excess == 0.0) { | |
| 488 | 523827 | ScaleBands(value, bands, factor); | |
| 489 | 523827 | return; | |
| 490 | } | ||
| 491 | 123910 | const double maturity = std::fmin(o.backgroundMaturity + excess, o.maxMaturity); | |
| 492 | 123910 | MatureBands(value, bands, o.backgroundMaturity, maturity, o.originReflectance, factor); | |
| 493 | } | ||
| 494 | |||
| 495 | } // namespace | ||
| 496 | |||
| 497 | 379 | std::vector<CraterAlbedo::Prepared> CraterAlbedo::Gather(const Bounds2d& region, double spacing, | |
| 498 | const Ground* ground) const { | ||
| 499 | 379 | const double margin = GatherMargin(); | |
| 500 | 379 | const Surface& surface = m_craters->Options().apply.surface; | |
| 501 | 379 | std::vector<Prepared> prepared; | |
| 502 | 379 | TakePrepared(m_craters->Craters(surface.Expand(region, margin), spacing), region, spacing, | |
| 503 | prepared); | ||
| 504 | 379 | const CraterReflectanceZones& zones = m_options.zones; | |
| 505 | 379 | if (zones.enabled) { | |
| 506 | 334 | for (double high = std::fmin(m_craters->MaxFeatureDiameter(), zones.maxDiameter); | |
| 507 | 1342 | zones.MaxReach(high) > margin; high *= 0.5) { | |
| 508 | 1008 | TakePrepared(m_craters->Craters(surface.Expand(region, zones.MaxReach(high)), spacing, | |
| 509 | 0.5 * high, high), | ||
| 510 | region, spacing, prepared); | ||
| 511 | } | ||
| 512 | } | ||
| 513 | 379 | SortUniqueById(prepared); | |
| 514 | 379 | if (ground != nullptr && zones.enabled && zones.shadows.enabled) { | |
| 515 | 68 | std::size_t shadowed = 0; | |
| 516 | 136 | for (Prepared& p : prepared) { | |
| 517 | 68 | AddShadows(p, *ground, region); | |
| 518 | 70 | shadowed += p.shadow ? 1u : 0u; | |
| 519 | } | ||
| 520 | 68 | LogDebug("albedo", "Sampled the ground for the zone shadows of ", shadowed, " craters"); | |
| 521 | } | ||
| 522 | 379 | return prepared; | |
| 523 | } | ||
| 524 | |||
| 525 | 350 | void CraterAlbedo::ApplyAt(AlbedoSample& value, std::size_t bands, double x, double y, | |
| 526 | const LayerContext& context) const { | ||
| 527 | 350 | const Surface& surface = m_craters->Options().apply.surface; | |
| 528 | 350 | double excess = 0.0; | |
| 529 | 350 | double change = 0.0; | |
| 530 | 761 | for (const Prepared& p : Gather(PointBounds(x, y), context.spacing, context.ground)) { | |
| 531 | 411 | excess += p.Excess(m_options, surface, x, y); | |
| 532 | 411 | change += p.Change(m_options.texture, surface, x, y); | |
| 533 | 350 | } | |
| 534 | 350 | Brighten(value, bands, excess, change, m_options); | |
| 535 | 350 | } | |
| 536 | |||
| 537 | 30 | void CraterAlbedo::Apply(std::vector<Raster<float>>& bands, const LayerContext& context) const { | |
| 538 | 30 | if (bands.empty()) { | |
| 539 | 1 | return; | |
| 540 | } | ||
| 541 | 29 | const Surface& surface = m_craters->Options().apply.surface; | |
| 542 | 29 | const Raster<float>& first = bands.front(); | |
| 543 | 29 | const std::vector<Prepared> prepared = | |
| 544 | 29 | Gather(first.Grid().Bounds(), context.spacing, context.ground); | |
| 545 | 29 | const std::size_t count = bands.size(); | |
| 546 | 29 | ForEachAlbedoBlock(bands, context.threads, kBlockRows, [&](std::size_t row0, std::size_t row1) { | |
| 547 | 143 | std::vector<const Prepared*> near = NearRows(prepared, first, row0, row1, surface); | |
| 548 | 143 | return AlbedoCellVisitor([this, near = std::move(near), &surface, | |
| 549 | 286 | count](AlbedoSample& value, double x, double y) { | |
| 550 | 1739851 | double excess = 0.0; | |
| 551 | 1739851 | double change = 0.0; | |
| 552 | 3895855 | for (const Prepared* p : near) { | |
| 553 | 2155643 | if (!surface.IsBody() && std::fabs(x - p->crater.x) > p->reach) { | |
| 554 | 573898 | continue; | |
| 555 | } | ||
| 556 | 1581745 | excess += p->Excess(m_options, surface, x, y); | |
| 557 | 1587548 | change += p->Change(m_options.texture, surface, x, y); | |
| 558 | } | ||
| 559 | 1740212 | Brighten(value, count, excess, change, m_options); | |
| 560 | 286 | }); | |
| 561 | 143 | }); | |
| 562 | 29 | } | |
| 563 | |||
| 564 | } // namespace terraingen | ||
| 565 |