src/sources/sample_block.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/sources/sample_block.h" | ||
| 4 | |||
| 5 | #include "terraingen/raster/grid.h" | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <cmath> | ||
| 9 | #include <stdexcept> | ||
| 10 | |||
| 11 | namespace terraingen { | ||
| 12 | |||
| 13 | 2315 | SampleBlock::SampleBlock(Raster<float>& heights) : m_grid(&heights), m_heights(heights.Values()) {} | |
| 14 | |||
| 15 | 14860365 | SampleBlock::SampleBlock(Span<const Vec2d> positions, Span<float> heights) | |
| 16 | 14860365 | : m_positions(positions), m_heights(heights) { | |
| 17 | 14860365 | if (positions.size() != heights.size()) { | |
| 18 | 5 | throw std::invalid_argument("A sample block needs one height per position"); | |
| 19 | } | ||
| 20 | 14860360 | } | |
| 21 | |||
| 22 | 60130165 | Vec2d SampleBlock::Position(std::size_t i) const { | |
| 23 | 60130165 | if (m_grid != nullptr) { | |
| 24 | 2378417 | return m_grid->CellCenter(i % m_grid->Width(), i / m_grid->Width()); | |
| 25 | } | ||
| 26 | 57751748 | return m_positions[i]; | |
| 27 | } | ||
| 28 | |||
| 29 | 35 | std::vector<Vec2d> SampleBlock::Positions() const { | |
| 30 | 35 | if (m_grid == nullptr) { | |
| 31 | 13 | return {m_positions.begin(), m_positions.end()}; | |
| 32 | } | ||
| 33 | 22 | std::vector<Vec2d> positions(Size()); | |
| 34 | 98550 | for (std::size_t i = 0; i < positions.size(); ++i) { | |
| 35 | 98528 | positions[i] = Position(i); | |
| 36 | } | ||
| 37 | 22 | return positions; | |
| 38 | 22 | } | |
| 39 | |||
| 40 | 7100626 | bool SampleBlock::IsNoData(std::size_t i) const { | |
| 41 | 7100628 | return m_grid != nullptr ? m_grid->IsNoData(m_heights[i]) : std::isnan(m_heights[i]); | |
| 42 | } | ||
| 43 | |||
| 44 | 215987 | Bounds2d SampleBlock::Bounds() const { | |
| 45 | 215987 | if (m_grid != nullptr) { | |
| 46 | 482 | return m_grid->Grid().Bounds(); | |
| 47 | } | ||
| 48 | 215505 | if (m_positions.empty()) { | |
| 49 | 21365 | return {}; | |
| 50 | } | ||
| 51 | 194140 | Bounds2d box = PointBounds(m_positions[0].x, m_positions[0].y); | |
| 52 | 1892607 | for (const Vec2d& p : m_positions) { | |
| 53 | 1698458 | const Bounds2d point = PointBounds(p.x, p.y); | |
| 54 | 1698564 | box.minX = std::min(box.minX, point.minX); | |
| 55 | 1698564 | box.minY = std::min(box.minY, point.minY); | |
| 56 | 1698564 | box.maxX = std::max(box.maxX, point.maxX); | |
| 57 | 1864827 | box.maxY = std::max(box.maxY, point.maxY); | |
| 58 | } | ||
| 59 | 194149 | return box; | |
| 60 | } | ||
| 61 | |||
| 62 | 2251994 | double SampleBlock::ResolvedSpacing(double spacing, const Surface& surface) const { | |
| 63 | 2251994 | return m_grid != nullptr ? terraingen::ResolvedSpacing(m_grid->Grid(), spacing, surface) | |
| 64 | 2251994 | : spacing; | |
| 65 | } | ||
| 66 | |||
| 67 | } // namespace terraingen | ||
| 68 |