include/terraingen/sources/diamond_square.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief `DiamondSquareUpsampler`, random midpoint displacement (diamond-square) refinement of a | ||
| 5 | /// source's posts, as Martin, Parkes and Dunstan (2014) expand DEMs. | ||
| 6 | #pragma once | ||
| 7 | |||
| 8 | #include "terraingen/raster/raster.h" | ||
| 9 | #include "terraingen/sources/upsampling.h" | ||
| 10 | |||
| 11 | #include <cstddef> | ||
| 12 | #include <cstdint> | ||
| 13 | #include <memory> | ||
| 14 | #include <optional> | ||
| 15 | |||
| 16 | namespace terraingen { | ||
| 17 | |||
| 18 | /// How diamond-square fills new nodes and reads samples between the finest nodes. | ||
| 19 | enum class DiamondSquareInterp { | ||
| 20 | /// Square nodes the mean of their four corners, diamond nodes the mean of their four | ||
| 21 | /// neighbors, samples bilinear between nodes. | ||
| 22 | Bilinear, | ||
| 23 | /// Catmull-Rom along each axis over four nodes, as the VIPER terrain expansion does: square | ||
| 24 | /// nodes from the 4 x 4 coarser nodes around them, diamond nodes the mean of the splines | ||
| 25 | /// along the two axes, samples Catmull-Rom between nodes. Smoother, with fewer lattice | ||
| 26 | /// creases. | ||
| 27 | CatmullRom, | ||
| 28 | }; | ||
| 29 | |||
| 30 | /// The largest `DiamondSquareOptions::fitWindow`, in posts. | ||
| 31 | inline constexpr std::size_t kMaxDiamondSquareFitWindow = 1024; | ||
| 32 | |||
| 33 | /// The fractal a `DiamondSquareUpsampler` refines posts with. | ||
| 34 | 16 | struct DiamondSquareOptions { | |
| 35 | /// Fractal dimension F in [2, 3]; each square or diamond step scales the displacement by | ||
| 36 | /// 2^-((3 - F) / 2). Unset estimates it from the posts (`EstimateMidpointRoughness`). | ||
| 37 | std::optional<double> fractalDimension{}; | ||
| 38 | /// Standard deviation in meters of the displacement at the posts' own last diamond step (Se); | ||
| 39 | /// the first square step of the refinement displaces by Se 2^-((3 - F) / 2). Unset estimates | ||
| 40 | /// it from the posts, window by window (`fitWindow`). | ||
| 41 | std::optional<double> scale{}; | ||
| 42 | /// Side in posts of the overlapping windows an unset `scale` is fitted over, centered every | ||
| 43 | /// half side from post (0, 0), so each region's detail follows its own relief and a crater or | ||
| 44 | /// massif roughens only the windows that hold it; each node's scale is bilinear between the | ||
| 45 | /// fits of the windows around it (`EstimateMidpointScale`). 0 fits one scale to the posts | ||
| 46 | /// `FitTo` reads; else 4 to `kMaxDiamondSquareFitWindow`. | ||
| 47 | std::size_t fitWindow = 32; | ||
| 48 | DiamondSquareInterp interp = DiamondSquareInterp::Bilinear; | ||
| 49 | /// The finest node spacing the posts are refined to, in the posts' units (meters on a plane); | ||
| 50 | /// 0 for no limit. | ||
| 51 | double minPixelSize = 0.0; | ||
| 52 | std::uint32_t seed = 1; ///< Varies the displacements. | ||
| 53 | |||
| 54 | /// Memberwise equality. | ||
| 55 | 6 | bool operator==(const DiamondSquareOptions& other) const { | |
| 56 | 12 | return fractalDimension == other.fractalDimension && scale == other.scale && | |
| 57 | 6 | fitWindow == other.fitWindow && interp == other.interp && | |
| 58 | 3 | minPixelSize == other.minPixelSize && seed == other.seed; | |
| 59 | } | ||
| 60 | 3 | bool operator!=(const DiamondSquareOptions& other) const { | |
| 61 | 3 | return !(*this == other); | |
| 62 | } | ||
| 63 | }; | ||
| 64 | |||
| 65 | /// Checks diamond-square options. | ||
| 66 | /// @throws std::invalid_argument for a `fractalDimension` outside [2, 3], a negative or | ||
| 67 | /// non-finite `scale`, a `fitWindow` of 1 to 3 or above `kMaxDiamondSquareFitWindow`, or a | ||
| 68 | /// negative or non-finite `minPixelSize`. | ||
| 69 | void ValidateDiamondSquareOptions(const DiamondSquareOptions& options); | ||
| 70 | |||
| 71 | /// The displacement scale and fractal dimension a raster's heights show. | ||
| 72 | struct MidpointRoughness { | ||
| 73 | double scale = 0.0; ///< Se in meters. | ||
| 74 | double fractalDimension = 2.5; ///< F in [2, 3]. | ||
| 75 | }; | ||
| 76 | |||
| 77 | /// The displacement scale Se of `heights` on its own cells, robust to a few large features: the | ||
| 78 | /// median absolute difference of each cell and the mean of its four neighbors, times 1.4826 so | ||
| 79 | /// that Gaussian relief gives its RMS; 0 without such a cell. | ||
| 80 | double EstimateMidpointScale(const Raster<float>& heights); | ||
| 81 | |||
| 82 | /// The roughness of `heights` on its own cells, by medians so that a few large features such as | ||
| 83 | /// craters do not set it: the scale is `EstimateMidpointScale`, and the fractal dimension 3 - H | ||
| 84 | /// from the median absolute height differences at lags of 1, 2, 4 and 8 cells; a raster without | ||
| 85 | /// relief gives a scale of 0 and dimension 2.5. | ||
| 86 | /// @throws std::invalid_argument for a raster with no cell whose four neighbors hold data. | ||
| 87 | MidpointRoughness EstimateMidpointRoughness(const Raster<float>& heights); | ||
| 88 | |||
| 89 | /// Random midpoint displacement of a source's posts (Martin, Parkes and Dunstan 2014; Peitgen and | ||
| 90 | /// Saupe 1988): each level halves the node spacing and displaces new nodes by a Gaussian that | ||
| 91 | /// shrinks by 2^-((3 - F) / 2) per step, keyed by node and level so grids, tiles and points agree. | ||
| 92 | /// Without a given scale each region's displacements follow the roughness of the posts around it. | ||
| 93 | 107 | class DiamondSquareUpsampler final : public Upsampler { | |
| 94 | public: | ||
| 95 | /// @throws std::invalid_argument as `ValidateDiamondSquareOptions`. | ||
| 96 | explicit DiamondSquareUpsampler(DiamondSquareOptions options = {}); | ||
| 97 | |||
| 98 | /// Posts either side of a sample its nodes are refined from. | ||
| 99 | int Reach() const override; | ||
| 100 | |||
| 101 | /// A copy with an unset fractal dimension, and an unset scale when `fitWindow` is 0, estimated | ||
| 102 | /// from up to 512 x 512 of the posts held (`EstimateMidpointRoughness`), or 256 x 256 posts | ||
| 103 | /// from post (0, 0) for a lattice without bounds; null when nothing is left to estimate, as | ||
| 104 | /// a scale fitted window by window is read from the posts as they are refined. | ||
| 105 | /// @throws std::invalid_argument as `EstimateMidpointRoughness`. | ||
| 106 | std::shared_ptr<const Upsampler> FitTo(const PostLattice& posts) const override; | ||
| 107 | |||
| 108 | /// A copy whose `DiamondSquareOptions::seed` is `ReseedWord(seed, 0)`, keeping every other | ||
| 109 | /// option, fitted ones included. | ||
| 110 | std::shared_ptr<const Upsampler> Reseeded(std::uint64_t seed) const override; | ||
| 111 | |||
| 112 | /// The levels refined at a sample spacing of `spacing` over posts `postSpacing` apart, both in | ||
| 113 | /// the posts' units: enough to halve the node spacing to `spacing` or finer, at most down to | ||
| 114 | /// `minPixelSize`. | ||
| 115 | int Levels(double postSpacing, double spacing) const; | ||
| 116 | |||
| 117 | /// The options the upsampler was made with. | ||
| 118 | 10 | const DiamondSquareOptions& Options() const { | |
| 119 | 10 | return m_options; | |
| 120 | } | ||
| 121 | |||
| 122 | protected: | ||
| 123 | /// Refines the nodes once for samples close together, a grid tile by tile. | ||
| 124 | /// @throws std::invalid_argument without a fractal dimension (`FitTo`), or without a scale | ||
| 125 | /// and a fit window, or for a rotated lattice. | ||
| 126 | void FillSamples(const PostLattice& posts, SampleBlock& samples, double spacing, | ||
| 127 | unsigned threads) const override; | ||
| 128 | |||
| 129 | private: | ||
| 130 | DiamondSquareOptions m_options; | ||
| 131 | double m_stepFactor = 1.0; | ||
| 132 | }; | ||
| 133 | |||
| 134 | } // namespace terraingen | ||
| 135 |