src/layers/sand_shadow.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/sand_shadow.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/bedforms_fields.h" | ||
| 6 | #include "terraingen/raster/cell_loops.h" | ||
| 7 | #include "terraingen/util/bits.h" | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <cmath> | ||
| 11 | #include <memory> | ||
| 12 | #include <stdexcept> | ||
| 13 | #include <utility> | ||
| 14 | |||
| 15 | namespace terraingen { | ||
| 16 | |||
| 17 | 23 | void ValidateSandShadowOptions(const SandShadowOptions& options) { | |
| 18 | 23 | CheckRanges(options); | |
| 19 | 15 | ValidateResolutionFade(options.fade); | |
| 20 | 15 | } | |
| 21 | |||
| 22 | 23 | SandShadowLayer::SandShadowLayer(std::shared_ptr<const BoulderLayer> boulders, | |
| 23 | 23 | SandShadowOptions options) | |
| 24 | 23 | : m_boulders(std::move(boulders)), m_options(options) { | |
| 25 | 23 | ValidateSandShadowOptions(m_options); | |
| 26 | 15 | if (!m_boulders) { | |
| 27 | 1 | throw std::invalid_argument("Sand shadows need a boulder layer"); | |
| 28 | } | ||
| 29 | 14 | m_downwind = {std::cos(m_options.windAzimuthDegrees * kDegree), | |
| 30 | 14 | std::sin(m_options.windAzimuthDegrees * kDegree)}; | |
| 31 | 14 | m_flankSlope = std::tan(m_options.flankAngleDegrees * kDegree); | |
| 32 | 23 | } | |
| 33 | |||
| 34 | 3 | LayerPtr SandShadowLayer::Rebound(const LayerReplacements& replaced) const { | |
| 35 | 3 | std::shared_ptr<const BoulderLayer> boulders = Replacement(m_boulders, replaced); | |
| 36 | 3 | if (boulders == m_boulders) { | |
| 37 | 2 | return nullptr; | |
| 38 | } | ||
| 39 | 1 | auto rebound = std::make_shared<SandShadowLayer>(*this); | |
| 40 | 1 | rebound->m_boulders = std::move(boulders); | |
| 41 | 1 | return rebound; | |
| 42 | 4 | } | |
| 43 | |||
| 44 | 62234 | double SandShadowLayer::LongestShadow() const { | |
| 45 | 62234 | return m_boulders->MaxFeatureDiameter() * (0.5 + m_options.lengthRatio); | |
| 46 | } | ||
| 47 | |||
| 48 | 2 | double SandShadowLayer::GroundReach(double spacing) const { | |
| 49 | 2 | return LongestShadow() + m_boulders->GroundReach(spacing); | |
| 50 | } | ||
| 51 | |||
| 52 | 62232 | std::vector<SandShadowLayer::Shadow> SandShadowLayer::ShadowsOver(const Bounds2d& region, | |
| 53 | double spacing, | ||
| 54 | const Ground* ground, | ||
| 55 | const Surface& surface) const { | ||
| 56 | 62232 | const double longest = LongestShadow(); | |
| 57 | 62232 | std::vector<Shadow> shadows; | |
| 58 | 62232 | const BoulderList boulders = | |
| 59 | 62232 | m_boulders->Boulders(surface.Expand(region, longest), spacing, ground); | |
| 60 | 230939 | for (std::size_t i = 0; i < boulders.Size(); ++i) { | |
| 61 | 168707 | const Boulder boulder = boulders.At(i); | |
| 62 | 168707 | if (boulder.diameter < m_options.minDiameter) { | |
| 63 | 1388 | continue; | |
| 64 | } | ||
| 65 | 167320 | const double weight = ResolutionWeight(boulder.diameter, spacing, m_options.fade); | |
| 66 | 167320 | const double cap = boulder.ExposedHeight(); | |
| 67 | 167320 | if (weight <= 0.0 || cap <= 0.0) { | |
| 68 | 1 | continue; | |
| 69 | } | ||
| 70 | 167319 | const double reach = boulder.diameter * (0.5 + m_options.lengthRatio); | |
| 71 | 334638 | shadows.push_back({{boulder.x, boulder.y}, | |
| 72 | boulder.diameter, | ||
| 73 | cap, | ||
| 74 | weight, | ||
| 75 | 334638 | surface.Expand({boulder.x, boulder.y, boulder.x, boulder.y}, reach)}); | |
| 76 | } | ||
| 77 | 62232 | return shadows; | |
| 78 | 62232 | } | |
| 79 | |||
| 80 | 16823 | double SandShadowLayer::ShadowDepth(const Shadow& shadow, double x, double y, | |
| 81 | const Surface& surface) const { | ||
| 82 | 16823 | const Vec2d offset = surface.Offset(shadow.center, {x, y}); | |
| 83 | 16823 | const double along = offset.x * m_downwind.x + offset.y * m_downwind.y - 0.5 * shadow.width; | |
| 84 | 16823 | const double length = m_options.lengthRatio * shadow.width; | |
| 85 | 16823 | if (!(along >= 0.0 && along < length)) { | |
| 86 | return 0.0; | ||
| 87 | } | ||
| 88 | 8821 | const double across = std::fabs(offset.y * m_downwind.x - offset.x * m_downwind.y); | |
| 89 | 8821 | const double taper = 1.0 - along / length; | |
| 90 | 8821 | const double half = 0.5 * shadow.width * taper; | |
| 91 | 8821 | if (across >= half) { | |
| 92 | return 0.0; | ||
| 93 | } | ||
| 94 | 572 | const double crest = std::min(0.5 * shadow.width * m_flankSlope, shadow.cap) * taper; | |
| 95 | 572 | return shadow.weight * crest * (1.0 - across / half); | |
| 96 | } | ||
| 97 | |||
| 98 | 1542 | std::vector<Bounds2d> SandShadowLayer::Boxes(const std::vector<Shadow>& shadows) { | |
| 99 | 1542 | std::vector<Bounds2d> boxes; | |
| 100 | boxes.reserve(shadows.size()); | ||
| 101 | 2879 | for (const Shadow& shadow : shadows) { | |
| 102 | 1337 | boxes.push_back(shadow.box); | |
| 103 | } | ||
| 104 | 1542 | return boxes; | |
| 105 | } | ||
| 106 | |||
| 107 | 60690 | double SandShadowLayer::ThicknessAt(double x, double y, const LayerContext& context) const { | |
| 108 | 60690 | const Surface& surface = context.SurfaceOrPlane(); | |
| 109 | 60690 | double depth = 0.0; | |
| 110 | 60690 | const std::vector<Shadow> shadows = | |
| 111 | 60690 | ShadowsOver({x, y, x, y}, context.spacing, context.ground, surface); | |
| 112 | 226672 | for (const Shadow& shadow : shadows) { | |
| 113 | 165982 | if (surface.Contains(shadow.box, {x, y})) { | |
| 114 | 7995 | depth = std::max(depth, ShadowDepth(shadow, x, y, surface)); | |
| 115 | } | ||
| 116 | } | ||
| 117 | 60690 | return depth; | |
| 118 | 60690 | } | |
| 119 | |||
| 120 | 60674 | double SandShadowLayer::MaterialCoverAt(double x, double y, const LayerContext& context) const { | |
| 121 | 60674 | return Smoothstep(ThicknessAt(x, y, context) / m_options.coverThickness); | |
| 122 | } | ||
| 123 | |||
| 124 | 2 | Raster<double> SandShadowLayer::MaterialCoverOver(const RasterGrid& grid, | |
| 125 | const LayerContext& context) const { | ||
| 126 | 2 | const Surface& surface = context.SurfaceOrPlane(); | |
| 127 | 2 | Raster<double> cover(grid); | |
| 128 | 2 | const std::vector<Shadow> shadows = | |
| 129 | 2 | ShadowsOver(grid.Bounds(), context.spacing, context.ground, surface); | |
| 130 | 4 | ForEachFeatureCell( | |
| 131 | 2 | cover, context.threads, Boxes(shadows), surface, [&](double& cell, const FeatureCell& at) { | |
| 132 | 16598 | cell = | |
| 133 | 8299 | std::max(cell, ShadowDepth(shadows[at.feature], at.center.x, at.center.y, surface)); | |
| 134 | 8299 | }); | |
| 135 | 60674 | for (double& cell : cover.Values()) { | |
| 136 | 60672 | cell = Smoothstep(cell / m_options.coverThickness); | |
| 137 | } | ||
| 138 | 2 | return cover; | |
| 139 | 2 | } | |
| 140 | |||
| 141 | 1540 | void SandShadowLayer::ApplySamples(SampleBlock& samples, const LayerContext& context) const { | |
| 142 | 1540 | const Surface& surface = context.SurfaceOrPlane(); | |
| 143 | 1540 | const double spacing = samples.ResolvedSpacing(context.spacing, surface); | |
| 144 | 1540 | const std::vector<Shadow> shadows = | |
| 145 | 1540 | ShadowsOver(samples.Bounds(), spacing, context.ground, surface); | |
| 146 | 1540 | DrawFeatures(samples, context.threads, Boxes(shadows), surface, | |
| 147 | 1540 | [&](float height, float base, const Vec2d& p, std::size_t n) { | |
| 148 | 800 | const double depth = ShadowDepth(shadows[n], p.x, p.y, surface); | |
| 149 | 800 | return static_cast<float>( | |
| 150 | 800 | std::max(static_cast<double>(height), static_cast<double>(base) + depth)); | |
| 151 | }); | ||
| 152 | 1540 | } | |
| 153 | |||
| 154 | } // namespace terraingen | ||
| 155 |