include/terraingen/layers/ground.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief The terrain beneath a layer, readable at any point. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/geometry/geometry.h" | ||
| 8 | #include "terraingen/geometry/surface.h" | ||
| 9 | #include "terraingen/sources/sample_block.h" | ||
| 10 | #include "terraingen/util/span.h" | ||
| 11 | |||
| 12 | #include <array> | ||
| 13 | #include <cstddef> | ||
| 14 | #include <optional> | ||
| 15 | |||
| 16 | namespace terraingen { | ||
| 17 | |||
| 18 | struct LayerContext; | ||
| 19 | |||
| 20 | /// The terrain beneath a layer (its source and the layers before it), readable anywhere. | ||
| 21 | /// | ||
| 22 | /// Layers that depend on the shape of the ground, such as craters cut to a datum, read it here | ||
| 23 | /// rather than from cells they changed, so they agree on every grid, tile and point. Like a | ||
| 24 | /// `Source`, a ground evaluates one method, `FillSamples`, which `HeightAt` and `HeightsAt` wrap. | ||
| 25 | /// A `Terrain` is a ground, and so is every prefix of one (`Terrain::Beneath`). | ||
| 26 | /// Implementations must be deterministic and thread-safe. | ||
| 27 | 2997443 | class Ground { | |
| 28 | public: | ||
| 29 | 2998906 | virtual ~Ground() = default; | |
| 30 | |||
| 31 | /// The ground's height at world (x, y), or NaN where there is no data. | ||
| 32 | /// @param spacing Sample spacing in meters. | ||
| 33 | float HeightAt(double x, double y, double spacing) const; | ||
| 34 | |||
| 35 | /// `HeightAt` at each of `points`, into `heights` of the same size, in one `FillSamples` on | ||
| 36 | /// `threads` threads (0 for every hardware thread); the heights do not depend on it. | ||
| 37 | /// @param spacing Sample spacing in meters. | ||
| 38 | /// @throws std::invalid_argument for spans of different sizes. | ||
| 39 | void HeightsAt(Span<const Vec2d> points, Span<float> heights, double spacing, | ||
| 40 | unsigned threads = 0) const; | ||
| 41 | |||
| 42 | /// The context a layer reads this ground in, at a sample spacing of `spacing` meters on | ||
| 43 | /// `threads` threads: on this ground, with the surface and source of a terrain. | ||
| 44 | virtual LayerContext Context(double spacing, unsigned threads) const; | ||
| 45 | |||
| 46 | protected: | ||
| 47 | /// Sets every sample of `samples` to the ground's height there at a sample spacing of | ||
| 48 | /// `spacing` meters, NaN where there is no data; a point gets the bits of a grid cell | ||
| 49 | /// centered there, on `threads` threads (0 for every hardware thread). | ||
| 50 | virtual void FillSamples(SampleBlock& samples, double spacing, unsigned threads) const = 0; | ||
| 51 | }; | ||
| 52 | |||
| 53 | /// The four points `step` meters east, west, north and south of `center` on `surface`, in that | ||
| 54 | /// order, which `GradientFromHeights` reads a gradient from. | ||
| 55 | std::array<Vec2d, 4> GradientPoints(const Surface& surface, const Vec2d& center, double step); | ||
| 56 | |||
| 57 | /// The gradient (east, north, rise over run) by central differences of `heights` read at | ||
| 58 | /// `GradientPoints` `step` meters out; nothing where any difference is not finite. | ||
| 59 | std::optional<Vec2d> GradientFromHeights(Span<const float> heights, double step); | ||
| 60 | |||
| 61 | /// The gradient (east, north, rise over run) of `ground` at `center` by central differences | ||
| 62 | /// `step` meters out (`GradientPoints`); nothing where the ground has no data there. | ||
| 63 | /// @param spacing Sample spacing in meters the ground is read at. | ||
| 64 | std::optional<Vec2d> GroundGradient(const Ground& ground, const Surface& surface, | ||
| 65 | const Vec2d& center, double step, double spacing); | ||
| 66 | |||
| 67 | /// The heights of `ground` at `offsets` (meters east and north in the local frame of `center`), | ||
| 68 | /// into `heights` of the same size, with `fallback` where the ground has no data. | ||
| 69 | /// @param spacing Sample spacing in meters the ground is read at. | ||
| 70 | void GroundAround(const Ground& ground, const Surface& surface, const Vec2d& center, | ||
| 71 | Span<const Vec2d> offsets, double spacing, double fallback, Span<double> heights); | ||
| 72 | |||
| 73 | } // namespace terraingen | ||
| 74 |