layers/layer.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "terraingen/layers/layer.h" | ||
| 2 | |||
| 3 | #include "terraingen/raster/cell_loops.h" | ||
| 4 | #include "terraingen/raster/grid.h" | ||
| 5 | #include "terraingen/util/checks.h" | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <stdexcept> | ||
| 9 | #include <string> | ||
| 10 | |||
| 11 | namespace terraingen { | ||
| 12 | |||
| 13 | 48180162 | void ValidatePointContext(const LayerContext& context) { | |
| 14 | 48180162 | RequirePositive(context.spacing, "Sample spacing"); | |
| 15 | 48180150 | } | |
| 16 | |||
| 17 | 5685736 | void RequireGround(const Ground* ground, std::string_view what) { | |
| 18 | 5685736 | if (ground == nullptr) { | |
| 19 | 36 | throw std::invalid_argument(std::string(what) + " need the ground"); | |
| 20 | } | ||
| 21 | 5685724 | } | |
| 22 | |||
| 23 | 1613 | void Layer::Apply(Raster<float>& heights, const LayerContext& context) const { | |
| 24 | 1613 | SampleBlock samples(heights); | |
| 25 | 1613 | ApplySamples(samples, context); | |
| 26 | 1611 | } | |
| 27 | |||
| 28 | 48180159 | void Layer::Apply(SampleBlock& samples, const LayerContext& context) const { | |
| 29 | 48180159 | if (!samples.IsGrid()) { | |
| 30 | 48180159 | ValidatePointContext(context); | |
| 31 | } | ||
| 32 | 48180149 | ApplySamples(samples, context); | |
| 33 | 48180145 | } | |
| 34 | |||
| 35 | 1542438 | float Layer::ApplyAt(float height, double x, double y, const LayerContext& context) const { | |
| 36 | 1542438 | const Vec2d position{x, y}; | |
| 37 | 1542438 | SampleBlock samples({&position, 1}, {&height, 1}); | |
| 38 | 1542438 | Apply(samples, context); | |
| 39 | 1542425 | return height; | |
| 40 | } | ||
| 41 | |||
| 42 | 6030004 | void FormulaLayer::ApplySamples(SampleBlock& samples, const LayerContext& context) const { | |
| 43 | 6030004 | CheckContext(context); | |
| 44 | 6030001 | if (Raster<float>* grid = samples.Grid()) { | |
| 45 | const double spacing = | ||
| 46 | 70 | std::max(context.spacing, RasterGrid::FromRaster(*grid).Spacing(surface_)); | |
| 47 | 70 | MapCells(*grid, context.threads, [&](float height, const Vec2d& centre) { | |
| 48 | 10411332 | return HeightAt(height, centre.x, centre.y, spacing, context.ground); | |
| 49 | }); | ||
| 50 | 70 | return; | |
| 51 | } | ||
| 52 | 12186966 | for (std::size_t i = 0; i < samples.Size(); ++i) { | |
| 53 | 6157035 | if (!samples.IsNoData(i)) { | |
| 54 | 6157029 | const Vec2d p = samples.Position(i); | |
| 55 | 6157029 | samples.Height(i) = | |
| 56 | 6157029 | HeightAt(samples.Height(i), p.x, p.y, context.spacing, context.ground); | |
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | } // namespace terraingen | ||
| 62 |