src/layers/sand_fill.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/sand_fill.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/bedforms_fields.h" | ||
| 6 | #include "terraingen/layers/fade.h" | ||
| 7 | #include "terraingen/util/bits.h" | ||
| 8 | |||
| 9 | #include <array> | ||
| 10 | #include <cmath> | ||
| 11 | #include <stdexcept> | ||
| 12 | |||
| 13 | namespace terraingen { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 | constexpr const char* kGroundReader = "A sand fill layer"; | ||
| 18 | |||
| 19 | } // namespace | ||
| 20 | |||
| 21 | 17 | void ValidateSandFillOptions(const SandFillOptions& options) { | |
| 22 | 17 | CheckRanges(options); | |
| 23 | 11 | } | |
| 24 | |||
| 25 | 17 | SandFillLayer::SandFillLayer(SandFillOptions options) : m_options(options) { | |
| 26 | 17 | ValidateSandFillOptions(m_options); | |
| 27 | 33 | for (int ring = 0; ring < 2; ++ring) { | |
| 28 | 22 | const double radius = m_options.reach * (ring == 0 ? 0.5 : 1.0); | |
| 29 | 22 | const double turn = ring == 0 ? kPi / kRingPoints : 0.0; | |
| 30 | 198 | for (int k = 0; k < kRingPoints; ++k) { | |
| 31 | 176 | const double angle = turn + 2.0 * kPi * k / kRingPoints; | |
| 32 | 176 | m_rings[static_cast<std::size_t>(ring * kRingPoints + k)] = {radius * std::cos(angle), | |
| 33 | 176 | radius * std::sin(angle)}; | |
| 34 | } | ||
| 35 | } | ||
| 36 | 11 | } | |
| 37 | |||
| 38 | 59929 | double SandFillLayer::Thickness(double x, double y, double height, | |
| 39 | const LayerContext& context) const { | ||
| 40 | 59929 | if (std::isnan(height) || m_options.fill <= 0.0) { | |
| 41 | return 0.0; | ||
| 42 | } | ||
| 43 | 59928 | std::array<double, 2 * kRingPoints> heights{}; | |
| 44 | 59928 | GroundAround(*context.ground, context.SurfaceOrPlane(), {x, y}, m_rings, context.spacing, | |
| 45 | height, heights); | ||
| 46 | 59971 | double mean = 0.0; | |
| 47 | 1018355 | for (const double there : heights) { | |
| 48 | 958384 | mean += there; | |
| 49 | } | ||
| 50 | 59971 | mean /= 2.0 * kRingPoints; | |
| 51 | 59971 | const double depth = m_options.fill * (mean - height); | |
| 52 | 59971 | if (depth <= 0.0) { | |
| 53 | return 0.0; | ||
| 54 | } | ||
| 55 | 28253 | return m_options.maxThickness * -std::expm1(-depth / m_options.maxThickness); | |
| 56 | } | ||
| 57 | |||
| 58 | 9596 | double SandFillLayer::ThicknessAt(double x, double y, const LayerContext& context) const { | |
| 59 | 9596 | RequireGround(context.ground, kGroundReader); | |
| 60 | 9594 | const float height = context.ground->HeightAt(x, y, context.spacing); | |
| 61 | 9592 | return Thickness(x, y, static_cast<double>(height), context); | |
| 62 | } | ||
| 63 | |||
| 64 | 9591 | double SandFillLayer::MaterialCoverAt(double x, double y, const LayerContext& context) const { | |
| 65 | 9591 | return Smoothstep(ThicknessAt(x, y, context) / m_options.coverThickness); | |
| 66 | } | ||
| 67 | |||
| 68 | 50334 | float SandFillLayer::HeightAt(float height, double x, double y, const LayerContext& context) const { | |
| 69 | 100675 | return static_cast<float>(static_cast<double>(height) + | |
| 70 | 50334 | Thickness(x, y, static_cast<double>(height), context)); | |
| 71 | } | ||
| 72 | |||
| 73 | 13 | void SandFillLayer::CheckContext(const LayerContext& context) const { | |
| 74 | 13 | RequireGround(context.ground, kGroundReader); | |
| 75 | 12 | } | |
| 76 | |||
| 77 | } // namespace terraingen | ||
| 78 |