./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
28 / 28
100.0%
Branches:
8 / 8
100.0%
Functions:
3 / 3
100.0%

src/geometry/plane_fit.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/geometry/plane_fit.h"
4
5 #include <array>
6 #include <cmath>
7 #include <cstddef>
8
9 namespace terraingen {
10
11 namespace {
12
13 using Matrix3 = std::array<std::array<double, 3>, 3>;
14
15 19399 double Determinant(const Matrix3& m) {
16 19399 return m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1]) -
17 19399 m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0]) +
18 19399 m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]);
19 }
20
21 } // namespace
22
23 2306377 void PlaneFit::Add(double dx, double dy, double h) {
24 2306377 m_n += 1.0;
25 2306377 m_sx += dx;
26 2306377 m_sy += dy;
27 2306377 m_sxx += dx * dx;
28 2306377 m_sxy += dx * dy;
29 2306377 m_syy += dy * dy;
30 2306377 m_sh += h;
31 2306377 m_sxh += dx * h;
32 2306377 m_syh += dy * h;
33 2306377 }
34
35 4948 Plane PlaneFit::Solve() const {
36 4948 const Matrix3 normal{{{m_n, m_sx, m_sy}, {m_sx, m_sxx, m_sxy}, {m_sy, m_sxy, m_syy}}};
37 4948 const double det = Determinant(normal);
38
4/4
✓ Branch 0 taken 4929 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 112 times.
✓ Branch 3 taken 4817 times.
4948 if (m_n < 3.0 || !(std::fabs(det) > 1e-9 * m_n * m_sxx * m_syy)) {
39 131 return {m_sh / m_n, 0.0, 0.0};
40 }
41 4817 const std::array<double, 3> rhs{m_sh, m_sxh, m_syh};
42 4817 std::array<double, 3> solution{};
43
2/2
✓ Branch 0 taken 14451 times.
✓ Branch 1 taken 4817 times.
19268 for (std::size_t column = 0; column < 3; ++column) {
44 14451 Matrix3 replaced = normal;
45
2/2
✓ Branch 0 taken 43353 times.
✓ Branch 1 taken 14451 times.
57804 for (std::size_t row = 0; row < 3; ++row) {
46 43353 replaced[row][column] = rhs[row];
47 }
48 14451 solution[column] = Determinant(replaced) / det;
49 }
50 4817 return {solution[0], solution[1], solution[2]};
51 }
52
53 } // namespace terraingen
54