layers/transforms.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "terraingen/layers/transforms.h" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <stdexcept> | ||
| 6 | #include <utility> | ||
| 7 | |||
| 8 | namespace terraingen { | ||
| 9 | |||
| 10 | 27 | ScaleLayer::ScaleLayer(double scale, double offset) : scale_(scale), offset_(offset) { | |
| 11 | 27 | if (!(std::isfinite(scale) && std::isfinite(offset))) { | |
| 12 | 2 | throw std::invalid_argument("Scale and offset must be finite"); | |
| 13 | } | ||
| 14 | 27 | } | |
| 15 | |||
| 16 | 699150 | float ScaleLayer::HeightAt(float height, double /*x*/, double /*y*/, double /*spacing*/, | |
| 17 | const Ground* /*ground*/) const { | ||
| 18 | 699150 | return static_cast<float>(static_cast<double>(height) * scale_ + offset_); | |
| 19 | } | ||
| 20 | |||
| 21 | 20 | ClampLayer::ClampLayer(std::optional<double> min, std::optional<double> max) | |
| 22 | 20 | : min_(min), max_(max) { | |
| 23 | 20 | if ((min && !std::isfinite(*min)) || (max && !std::isfinite(*max))) { | |
| 24 | 2 | throw std::invalid_argument("Clamp bounds must be finite"); | |
| 25 | } | ||
| 26 | 18 | if (min && max && *min > *max) { | |
| 27 | 3 | throw std::invalid_argument("Clamp min must not exceed max"); | |
| 28 | } | ||
| 29 | 20 | } | |
| 30 | |||
| 31 | 277114 | float ClampLayer::HeightAt(float height, double /*x*/, double /*y*/, double /*spacing*/, | |
| 32 | const Ground* /*ground*/) const { | ||
| 33 | 277114 | double value = height; | |
| 34 | 277114 | if (min_) { | |
| 35 | 263220 | value = std::max(value, *min_); | |
| 36 | } | ||
| 37 | 277114 | if (max_) { | |
| 38 | 277112 | value = std::min(value, *max_); | |
| 39 | } | ||
| 40 | 277114 | return static_cast<float>(value); | |
| 41 | } | ||
| 42 | |||
| 43 | 5 | FunctionLayer::FunctionLayer(HeightFunction function, Surface surface) | |
| 44 | 5 | : FormulaLayer(surface), function_(std::move(function)) { | |
| 45 | 5 | if (!function_) { | |
| 46 | 1 | throw std::invalid_argument("A function layer needs a function"); | |
| 47 | } | ||
| 48 | 6 | } | |
| 49 | |||
| 50 | 127 | float FunctionLayer::HeightAt(float height, double x, double y, double spacing, | |
| 51 | const Ground* /*ground*/) const { | ||
| 52 | 127 | return static_cast<float>(function_(x, y, spacing, height)); | |
| 53 | } | ||
| 54 | |||
| 55 | } // namespace terraingen | ||
| 56 |