layers/noise_relief.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "terraingen/layers/noise_relief.h" | ||
| 2 | |||
| 3 | #include "terraingen/layers/kernel.h" | ||
| 4 | #include "terraingen/raster/cell_loops.h" | ||
| 5 | |||
| 6 | #include <cstddef> | ||
| 7 | #include <utility> | ||
| 8 | |||
| 9 | namespace terraingen { | ||
| 10 | |||
| 11 | 293 | NoiseRelief::NoiseRelief(SyntheticTerrainParams params, Vec2d origin, Surface surface) | |
| 12 | 293 | : params_(std::move(params)), origin_(origin), surface_(surface) { | |
| 13 | 293 | ValidateSyntheticTerrain(params_); | |
| 14 | 291 | kernel_ = MakeNoiseReliefKernel(params_.layers); | |
| 15 | 297 | } | |
| 16 | |||
| 17 | 41773244 | void NoiseRelief::ApplySamples(SampleBlock& samples, const LayerContext& context) const { | |
| 18 | 41773244 | if (!surface_.IsBody()) { | |
| 19 | 41772527 | ApplyKernel(samples, context, kernel_, origin_); | |
| 20 | 41772633 | return; | |
| 21 | } | ||
| 22 | 994802 | const auto at = [&](float height, const Vec2d& p) { | |
| 23 | 994802 | return height + NoiseLayersAt(params_.layers, origin_, surface_, p.x, p.y); | |
| 24 | 717 | }; | |
| 25 | 717 | if (Raster<float>* grid = samples.Grid()) { | |
| 26 | 106 | MapCells(*grid, context.threads, at); | |
| 27 | 106 | return; | |
| 28 | } | ||
| 29 | 129463 | for (std::size_t i = 0; i < samples.Size(); ++i) { | |
| 30 | 128852 | if (!samples.IsNoData(i)) { | |
| 31 | 128852 | samples.Height(i) = at(samples.Height(i), samples.Position(i)); | |
| 32 | } | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | 6455684 | float NoiseLayersAt(const std::vector<NoiseLayer>& layers, const Vec2d& origin, | |
| 37 | const Surface& surface, double x, double y) { | ||
| 38 | 6455684 | if (surface.IsBody()) { | |
| 39 | 1126911 | const Vec3d p = surface.Position({x, y}); | |
| 40 | 1126911 | return EvaluateNoiseLayers3(p.x, p.y, p.z, layers.data(), layers.size()); | |
| 41 | } | ||
| 42 | 5328773 | return EvaluateNoiseLayers(static_cast<float>(x - origin.x), static_cast<float>(y - origin.y), | |
| 43 | 5328773 | layers.data(), layers.size()); | |
| 44 | } | ||
| 45 | |||
| 46 | } // namespace terraingen | ||
| 47 |