include/terraingen/layers/exposure.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief `ExposureField`, how far the ground is bare bedrock rather than soil, and `PatchNoise`. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/geometry/surface.h" | ||
| 8 | #include "terraingen/layers/ground.h" | ||
| 9 | #include "terraingen/raster/raster.h" | ||
| 10 | |||
| 11 | #include <array> | ||
| 12 | #include <cstdint> | ||
| 13 | #include <optional> | ||
| 14 | |||
| 15 | namespace terraingen { | ||
| 16 | |||
| 17 | /// Two-octave Perlin patch noise in [-1, 1], about half the ground near either end. | ||
| 18 | /// @param size Typical width of a patch, in meters; positive. | ||
| 19 | /// @param seed Seed of the coarser octave; the finer uses `seed` + 1. | ||
| 20 | double PatchNoise(const Surface& surface, double x, double y, double size, std::uint32_t seed); | ||
| 21 | |||
| 22 | /// The thresholds an `ExposureField` tells soil from bedrock by; a rule of thumb, not a | ||
| 23 | /// measurement. | ||
| 24 | 10 | struct ExposureParams { | |
| 25 | double reach = 12.0; ///< How far around a point its surroundings are read (m). | ||
| 26 | /// Standing this share of the reach above the surroundings or less: soil. | ||
| 27 | double riseSoil = 0.010; | ||
| 28 | double riseBoulder = 0.035; ///< Standing this share of the reach above them or more: bedrock. | ||
| 29 | double slopeSoil = 0.20; ///< Slope (rise over run) at or under which soil holds. | ||
| 30 | double slopeBoulder = 0.40; ///< Slope at or over which none does. | ||
| 31 | /// Share of level ground that is bedrock too, in patches; 0 for none. | ||
| 32 | double floorCover = 0.0; | ||
| 33 | double floorPatch = 60.0; ///< How far across those patches are (m). | ||
| 34 | std::uint32_t seed = 0; ///< Varies the floor patches. | ||
| 35 | |||
| 36 | 1 | bool operator==(const ExposureParams& other) const { | |
| 37 | 1 | return reach == other.reach && riseSoil == other.riseSoil && | |
| 38 | 1 | riseBoulder == other.riseBoulder && slopeSoil == other.slopeSoil && | |
| 39 | 1 | slopeBoulder == other.slopeBoulder && floorCover == other.floorCover && | |
| 40 | 2 | floorPatch == other.floorPatch && seed == other.seed; | |
| 41 | } | ||
| 42 | }; | ||
| 43 | |||
| 44 | /// Checks exposure thresholds. | ||
| 45 | /// @throws std::invalid_argument unless `reach` and `floorPatch` are positive, each boulder | ||
| 46 | /// threshold is over its soil one, and `floorCover` is in [0, 1). | ||
| 47 | void ValidateExposureParams(const ExposureParams& params); | ||
| 48 | |||
| 49 | /// How far the ground at a point is bedrock rather than soil, from its shape alone. | ||
| 50 | /// | ||
| 51 | /// Rises above the mean of eight points a reach around, and steep slopes, grade from soil to | ||
| 52 | /// bedrock by smoothsteps between their thresholds (the larger wins); `floorCover` of level ground | ||
| 53 | /// is bedrock in noise patches. Reads the ground at points, so it agrees on every grid and tile. | ||
| 54 | 13572 | class ExposureField { | |
| 55 | public: | ||
| 56 | /// Makes a field; on a sphere, surroundings are read in each point's local frame. | ||
| 57 | /// @throws std::invalid_argument as `ValidateExposureParams`. | ||
| 58 | explicit ExposureField(const ExposureParams& params = {}, const Surface& surface = {}); | ||
| 59 | |||
| 60 | /// The share of bedrock at world (x, y), in [0, 1] (0 soil, 1 bedrock; 0 without data). | ||
| 61 | /// Neighbors without data count as level with the point. | ||
| 62 | /// @param spacing Sample spacing in world units. | ||
| 63 | double Exposure(const Ground& ground, double x, double y, double spacing) const; | ||
| 64 | |||
| 65 | /// `Exposure` with the surroundings read on `surface` in place of the field's own. | ||
| 66 | /// @param spacing Sample spacing in world units. | ||
| 67 | double Exposure(const Ground& ground, const Surface& surface, double x, double y, | ||
| 68 | double spacing) const; | ||
| 69 | |||
| 70 | /// Fills `raster` with `Exposure` at each cell center. | ||
| 71 | /// @param spacing Sample spacing in world units. | ||
| 72 | /// @param threads Worker threads; 0 for every hardware thread. | ||
| 73 | void Fill(Raster<float>& raster, const Ground& ground, double spacing, | ||
| 74 | unsigned threads = 1) const; | ||
| 75 | |||
| 76 | /// The thresholds the field was made with. | ||
| 77 | 1 | const ExposureParams& Params() const { | |
| 78 | 1 | return m_params; | |
| 79 | } | ||
| 80 | |||
| 81 | private: | ||
| 82 | ExposureParams m_params; | ||
| 83 | Surface m_surface; | ||
| 84 | /// The surroundings read around a point, in meters east and north of it. | ||
| 85 | std::array<Vec2d, 8> m_ring{}; | ||
| 86 | }; | ||
| 87 | |||
| 88 | /// The exposure field a layer may read, at a fixed ground spacing, on the surface it is read on; | ||
| 89 | /// empty when the layer has none. | ||
| 90 | 438 | class LayerExposure { | |
| 91 | public: | ||
| 92 | 430 | LayerExposure() = default; | |
| 93 | |||
| 94 | /// @param spacing Sample spacing in meters the ground is read at. | ||
| 95 | /// @param reader What reads the field, which starts the error message without a ground. | ||
| 96 | /// @throws std::invalid_argument as `ValidateExposureParams`. | ||
| 97 | LayerExposure(const std::optional<ExposureParams>& params, double spacing, const char* reader); | ||
| 98 | |||
| 99 | /// Whether the layer reads an exposure field. | ||
| 100 | 3880986 | explicit operator bool() const { | |
| 101 | 3880986 | return m_field.has_value(); | |
| 102 | } | ||
| 103 | |||
| 104 | /// How far (m) around a point the field reads the ground; 0 when not set. | ||
| 105 | 7 | double Reach() const { | |
| 106 | 7 | return m_field ? m_field->Params().reach : 0.0; | |
| 107 | } | ||
| 108 | |||
| 109 | /// Checks that a layer reading the field was given a ground. | ||
| 110 | /// @throws std::invalid_argument if the field is set and `ground` is null. | ||
| 111 | void Require(const Ground* ground) const; | ||
| 112 | |||
| 113 | /// The field's exposure at world (x, y) on `ground` lying on `surface`; call only when set. | ||
| 114 | /// @throws std::invalid_argument if `ground` is null. | ||
| 115 | double At(double x, double y, const Ground* ground, const Surface& surface) const; | ||
| 116 | |||
| 117 | private: | ||
| 118 | std::optional<ExposureField> m_field; | ||
| 119 | double m_spacing = 1.0; | ||
| 120 | const char* m_reader = ""; | ||
| 121 | }; | ||
| 122 | |||
| 123 | } // namespace terraingen | ||
| 124 |