src/layers/ground.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/ground.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/layer.h" | ||
| 6 | |||
| 7 | #include "sources/lattice.h" | ||
| 8 | |||
| 9 | #include <cmath> | ||
| 10 | |||
| 11 | namespace terraingen { | ||
| 12 | |||
| 13 | 7120099 | float Ground::HeightAt(double x, double y, double spacing) const { | |
| 14 | 7120099 | return detail::PointHeight(x, y, | |
| 15 | 14249617 | [&](SampleBlock& samples) { FillSamples(samples, spacing, 1); }); | |
| 16 | } | ||
| 17 | |||
| 18 | 389765 | void Ground::HeightsAt(Span<const Vec2d> points, Span<float> heights, double spacing, | |
| 19 | unsigned threads) const { | ||
| 20 | 389765 | SampleBlock samples(points, heights); | |
| 21 | 389763 | FillSamples(samples, spacing, threads); | |
| 22 | 389762 | } | |
| 23 | |||
| 24 | 2510658 | LayerContext Ground::Context(double spacing, unsigned threads) const { | |
| 25 | 2510658 | LayerContext context; | |
| 26 | 2510658 | context.spacing = spacing; | |
| 27 | 2510658 | context.threads = threads; | |
| 28 | 2510658 | context.ground = this; | |
| 29 | 2510658 | return context; | |
| 30 | } | ||
| 31 | |||
| 32 | 440315 | std::array<Vec2d, 4> GradientPoints(const Surface& surface, const Vec2d& center, double step) { | |
| 33 | 440315 | return {surface.Locate(center, {step, 0.0}), surface.Locate(center, {-step, 0.0}), | |
| 34 | 440315 | surface.Locate(center, {0.0, step}), surface.Locate(center, {0.0, -step})}; | |
| 35 | } | ||
| 36 | |||
| 37 | 378198 | std::optional<Vec2d> GradientFromHeights(Span<const float> heights, double step) { | |
| 38 | 378198 | const double east = | |
| 39 | 378198 | (static_cast<double>(heights[0]) - static_cast<double>(heights[1])) / (2.0 * step); | |
| 40 | 378198 | const double north = | |
| 41 | 378198 | (static_cast<double>(heights[2]) - static_cast<double>(heights[3])) / (2.0 * step); | |
| 42 | 378198 | if (!std::isfinite(east) || !std::isfinite(north)) { | |
| 43 | 221 | return std::nullopt; | |
| 44 | } | ||
| 45 | 377977 | return Vec2d{east, north}; | |
| 46 | } | ||
| 47 | |||
| 48 | 362446 | std::optional<Vec2d> GroundGradient(const Ground& ground, const Surface& surface, | |
| 49 | const Vec2d& center, double step, double spacing) { | ||
| 50 | 362446 | const std::array<Vec2d, 4> points = GradientPoints(surface, center, step); | |
| 51 | 362446 | std::array<float, 4> heights{}; | |
| 52 | 362446 | ground.HeightsAt(points, heights, spacing); | |
| 53 | 362446 | return GradientFromHeights(heights, step); | |
| 54 | } | ||
| 55 | |||
| 56 | 444876 | void GroundAround(const Ground& ground, const Surface& surface, const Vec2d& center, | |
| 57 | Span<const Vec2d> offsets, double spacing, double fallback, | ||
| 58 | Span<double> heights) { | ||
| 59 | 4463784 | for (std::size_t k = 0; k < offsets.size(); ++k) { | |
| 60 | 4018867 | const Vec2d at = surface.Locate(center, offsets[k]); | |
| 61 | 4018737 | const float there = ground.HeightAt(at.x, at.y, spacing); | |
| 62 | 4018965 | heights[k] = std::isnan(there) ? fallback : static_cast<double>(there); | |
| 63 | } | ||
| 64 | 444917 | } | |
| 65 | |||
| 66 | } // namespace terraingen | ||
| 67 |