include/terraingen/layers/transforms.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Height transforms as layers: scale and offset, clamp, and a function of your own. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/layers/layer.h" | ||
| 8 | |||
| 9 | #include <functional> | ||
| 10 | #include <optional> | ||
| 11 | |||
| 12 | namespace terraingen { | ||
| 13 | |||
| 14 | /// Scales and shifts the heights so far: height * `scale` + `offset`. | ||
| 15 | 33 | class ScaleLayer final : public FormulaLayer { | |
| 16 | public: | ||
| 17 | /// @param scale Factor on the heights (vertical exaggeration). | ||
| 18 | /// @param offset Added after scaling, in meters. | ||
| 19 | /// @throws std::invalid_argument unless `scale` and `offset` are finite. | ||
| 20 | ScaleLayer(double scale, double offset = 0.0); | ||
| 21 | |||
| 22 | /// The factor on the heights. | ||
| 23 | 1 | double Scale() const { | |
| 24 | 1 | return m_scale; | |
| 25 | } | ||
| 26 | /// The shift after scaling, in meters. | ||
| 27 | 1 | double Offset() const { | |
| 28 | 1 | return m_offset; | |
| 29 | } | ||
| 30 | |||
| 31 | protected: | ||
| 32 | float HeightAt(float height, double x, double y, const LayerContext& context) const override; | ||
| 33 | |||
| 34 | private: | ||
| 35 | double m_scale; | ||
| 36 | double m_offset; | ||
| 37 | }; | ||
| 38 | |||
| 39 | /// Clamps the heights so far to [`min`, `max`], either bound optional. | ||
| 40 | 11 | class ClampLayer final : public FormulaLayer { | |
| 41 | public: | ||
| 42 | /// @param min The lowest height kept, in meters; none for no floor. | ||
| 43 | /// @param max The highest height kept, in meters; none for no cap. | ||
| 44 | /// @throws std::invalid_argument for a bound that is not finite or `min` above `max`. | ||
| 45 | ClampLayer(std::optional<double> min, std::optional<double> max); | ||
| 46 | |||
| 47 | /// The lowest height kept, in meters; none for no floor. | ||
| 48 | 1 | std::optional<double> Min() const { | |
| 49 | 1 | return m_min; | |
| 50 | } | ||
| 51 | /// The highest height kept, in meters; none for no cap. | ||
| 52 | 1 | std::optional<double> Max() const { | |
| 53 | 1 | return m_max; | |
| 54 | } | ||
| 55 | |||
| 56 | protected: | ||
| 57 | float HeightAt(float height, double x, double y, const LayerContext& context) const override; | ||
| 58 | |||
| 59 | private: | ||
| 60 | std::optional<double> m_min; | ||
| 61 | std::optional<double> m_max; | ||
| 62 | }; | ||
| 63 | |||
| 64 | /// The new height at (`x`, `y`) sampled every `spacing` meters given the `height` so far; must be | ||
| 65 | /// deterministic and thread-safe. | ||
| 66 | using HeightFunction = std::function<double(double x, double y, double spacing, double height)>; | ||
| 67 | |||
| 68 | /// A layer from a function of position, sample spacing and the height so far. | ||
| 69 | 5 | class FunctionLayer final : public FormulaLayer { | |
| 70 | public: | ||
| 71 | /// @throws std::invalid_argument for an empty `function`. | ||
| 72 | explicit FunctionLayer(HeightFunction function); | ||
| 73 | |||
| 74 | protected: | ||
| 75 | float HeightAt(float height, double x, double y, const LayerContext& context) const override; | ||
| 76 | |||
| 77 | private: | ||
| 78 | HeightFunction m_function; | ||
| 79 | }; | ||
| 80 | |||
| 81 | } // namespace terraingen | ||
| 82 |