./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
36 / 36
100.0%
Branches:
29 / 29
100.0%
Functions:
7 / 7
100.0%

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
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 14860360 times.
14860365 if (positions.size() != heights.size()) {
18
1/1
✓ Branch 2 taken 5 times.
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
2/2
✓ Branch 0 taken 2378417 times.
✓ Branch 1 taken 57751748 times.
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
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 22 times.
35 if (m_grid == nullptr) {
31 13 return {m_positions.begin(), m_positions.end()};
32 }
33 22 std::vector<Vec2d> positions(Size());
34
2/2
✓ Branch 0 taken 98528 times.
✓ Branch 1 taken 22 times.
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
4/4
✓ Branch 0 taken 301125 times.
✓ Branch 1 taken 6799501 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 301157 times.
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
2/2
✓ Branch 0 taken 482 times.
✓ Branch 1 taken 215505 times.
215987 if (m_grid != nullptr) {
46 482 return m_grid->Grid().Bounds();
47 }
48
2/2
✓ Branch 0 taken 21365 times.
✓ Branch 1 taken 194140 times.
215505 if (m_positions.empty()) {
49 21365 return {};
50 }
51 194140 Bounds2d box = PointBounds(m_positions[0].x, m_positions[0].y);
52
2/2
✓ Branch 0 taken 1698458 times.
✓ Branch 1 taken 194149 times.
1892607 for (const Vec2d& p : m_positions) {
53 1698458 const Bounds2d point = PointBounds(p.x, p.y);
54
2/2
✓ Branch 0 taken 32529 times.
✓ Branch 1 taken 1666035 times.
1698564 box.minX = std::min(box.minX, point.minX);
55
2/2
✓ Branch 0 taken 39155 times.
✓ Branch 1 taken 1659409 times.
1698564 box.minY = std::min(box.minY, point.minY);
56
2/2
✓ Branch 0 taken 131759 times.
✓ Branch 1 taken 1566805 times.
1698564 box.maxX = std::max(box.maxX, point.maxX);
57
2/2
✓ Branch 0 taken 166263 times.
✓ Branch 1 taken 1532301 times.
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
2/2
✓ Branch 0 taken 578 times.
✓ Branch 1 taken 2251416 times.
2251994 return m_grid != nullptr ? terraingen::ResolvedSpacing(m_grid->Grid(), spacing, surface)
64 2251994 : spacing;
65 }
66
67 } // namespace terraingen
68