layers/sample_block.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /// @file | ||
| 2 | /// @brief Positions a layer evaluates and their heights: a raster's cells or scattered points. | ||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include "terraingen/raster/raster.h" | ||
| 6 | #include "terraingen/util/geometry.h" | ||
| 7 | |||
| 8 | #include <cstddef> | ||
| 9 | #include <span> | ||
| 10 | |||
| 11 | namespace terraingen { | ||
| 12 | |||
| 13 | /// Heights to change in place and where they are: every cell of a raster, sampled at its centre, | ||
| 14 | /// or a list of scattered points such as wheel contacts. Layers apply to a block in one call, so a | ||
| 15 | /// single point and a whole grid take the same path and give the same height at the same place. | ||
| 16 | class SampleBlock { | ||
| 17 | public: | ||
| 18 | /// The cells of `heights`, which must outlive the block. | ||
| 19 | explicit SampleBlock(Raster<float>& heights); | ||
| 20 | |||
| 21 | /// The points `positions`, with their heights in step; both must outlive the block. | ||
| 22 | /// @throws std::invalid_argument when the two differ in length. | ||
| 23 | SampleBlock(std::span<const Vec2d> positions, std::span<float> heights); | ||
| 24 | |||
| 25 | /// The number of samples. | ||
| 26 | 1471238212 | std::size_t Size() const { | |
| 27 | 1471238212 | return heights_.size(); | |
| 28 | } | ||
| 29 | |||
| 30 | /// Whether the samples are a raster's cells. | ||
| 31 | 48180161 | bool IsGrid() const { | |
| 32 | 48180161 | return grid_ != nullptr; | |
| 33 | } | ||
| 34 | |||
| 35 | /// The raster of a grid block, or null for points. | ||
| 36 | 48486672 | Raster<float>* Grid() const { | |
| 37 | 48486672 | return grid_; | |
| 38 | } | ||
| 39 | |||
| 40 | /// The position of sample `i`: a cell centre, or a point. | ||
| 41 | Vec2d Position(std::size_t i) const; | ||
| 42 | |||
| 43 | /// The height of sample `i`. | ||
| 44 | 99944096 | float& Height(std::size_t i) const { | |
| 45 | 99944096 | return heights_[i]; | |
| 46 | } | ||
| 47 | |||
| 48 | /// Whether sample `i` holds no data: NaN, or a grid's no-data value. | ||
| 49 | bool IsNoData(std::size_t i) const; | ||
| 50 | |||
| 51 | /// The smallest box holding every sample: a grid's extent, or the points' bounds, which like | ||
| 52 | /// `PointBounds` contain their largest coordinates. | ||
| 53 | Bounds2d Bounds() const; | ||
| 54 | |||
| 55 | private: | ||
| 56 | Raster<float>* grid_ = nullptr; | ||
| 57 | std::span<const Vec2d> positions_; | ||
| 58 | std::span<float> heights_; | ||
| 59 | }; | ||
| 60 | |||
| 61 | } // namespace terraingen | ||
| 62 |