layers/sample_block.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "terraingen/layers/sample_block.h" | ||
| 2 | |||
| 3 | #include "terraingen/raster/grid.h" | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cmath> | ||
| 7 | #include <stdexcept> | ||
| 8 | |||
| 9 | namespace terraingen { | ||
| 10 | |||
| 11 | 1614 | SampleBlock::SampleBlock(Raster<float>& heights) : grid_(&heights), heights_(heights.Values()) {} | |
| 12 | |||
| 13 | 48180160 | SampleBlock::SampleBlock(std::span<const Vec2d> positions, std::span<float> heights) | |
| 14 | 48180160 | : positions_(positions), heights_(heights) { | |
| 15 | 48180160 | if (positions.size() != heights.size()) { | |
| 16 | 1 | throw std::invalid_argument("A sample block needs one height per position"); | |
| 17 | } | ||
| 18 | 48180159 | } | |
| 19 | |||
| 20 | 1417592999 | Vec2d SampleBlock::Position(std::size_t i) const { | |
| 21 | 1417592999 | if (grid_ != nullptr) { | |
| 22 | 136453 | return grid_->CellCenter(i % grid_->Width(), i / grid_->Width()); | |
| 23 | } | ||
| 24 | 1417456546 | return positions_[i]; | |
| 25 | } | ||
| 26 | |||
| 27 | 50379121 | bool SampleBlock::IsNoData(std::size_t i) const { | |
| 28 | 50379121 | return grid_ != nullptr ? grid_->IsNoData(heights_[i]) : std::isnan(heights_[i]); | |
| 29 | } | ||
| 30 | |||
| 31 | 306458 | Bounds2d SampleBlock::Bounds() const { | |
| 32 | 306458 | if (grid_ != nullptr) { | |
| 33 | 522 | return RasterGrid::FromRaster(*grid_).Bounds(); | |
| 34 | } | ||
| 35 | 305936 | if (positions_.empty()) { | |
| 36 | 20360 | return {}; | |
| 37 | } | ||
| 38 | 285576 | Bounds2d box = PointBounds(positions_[0].x, positions_[0].y); | |
| 39 | 1748041 | for (const Vec2d& p : positions_) { | |
| 40 | 1462465 | const Bounds2d point = PointBounds(p.x, p.y); | |
| 41 | 1462465 | box.minX = std::min(box.minX, point.minX); | |
| 42 | 1462465 | box.minY = std::min(box.minY, point.minY); | |
| 43 | 1462465 | box.maxX = std::max(box.maxX, point.maxX); | |
| 44 | 1462465 | box.maxY = std::max(box.maxY, point.maxY); | |
| 45 | } | ||
| 46 | 285576 | return box; | |
| 47 | } | ||
| 48 | |||
| 49 | } // namespace terraingen | ||
| 50 |