include/terraingen/geometry/plane_fit.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief `Plane` and `PlaneFit`, a least-squares plane through points added one at a time. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | namespace terraingen { | ||
| 8 | |||
| 9 | /// The plane h = `a` + `b` dx + `c` dy, dx and dy in meters east and north of a reference point. | ||
| 10 | struct Plane { | ||
| 11 | double a = 0.0; ///< Height at the reference point (m). | ||
| 12 | double b = 0.0; ///< Slope eastward (m/m). | ||
| 13 | double c = 0.0; ///< Slope northward (m/m). | ||
| 14 | |||
| 15 | /// Memberwise equality. | ||
| 16 | 4 | bool operator==(const Plane& other) const { | |
| 17 | 4 | return a == other.a && b == other.b && c == other.c; | |
| 18 | } | ||
| 19 | bool operator!=(const Plane& other) const { | ||
| 20 | return !(*this == other); | ||
| 21 | } | ||
| 22 | }; | ||
| 23 | |||
| 24 | /// A least-squares plane h = a + b dx + c dy through points added one at a time. | ||
| 25 | class PlaneFit { | ||
| 26 | public: | ||
| 27 | /// Adds the point `dx` east and `dy` north (m) at height `h` (m). | ||
| 28 | void Add(double dx, double dy, double h); | ||
| 29 | |||
| 30 | /// Whether no point was added. | ||
| 31 | 7920 | bool Empty() const { | |
| 32 | 7920 | return m_n == 0.0; | |
| 33 | } | ||
| 34 | |||
| 35 | /// The mean height of the points; call only when not `Empty`. | ||
| 36 | 2407 | double Mean() const { | |
| 37 | 2407 | return m_sh / m_n; | |
| 38 | } | ||
| 39 | |||
| 40 | /// The least-squares plane by Cramer's rule, or the flat plane at the mean for fewer than | ||
| 41 | /// three points or a degenerate layout; call only when not `Empty`. | ||
| 42 | Plane Solve() const; | ||
| 43 | |||
| 44 | private: | ||
| 45 | double m_n = 0.0; | ||
| 46 | double m_sx = 0.0; | ||
| 47 | double m_sy = 0.0; | ||
| 48 | double m_sxx = 0.0; | ||
| 49 | double m_sxy = 0.0; | ||
| 50 | double m_syy = 0.0; | ||
| 51 | double m_sh = 0.0; | ||
| 52 | double m_sxh = 0.0; | ||
| 53 | double m_syh = 0.0; | ||
| 54 | }; | ||
| 55 | |||
| 56 | } // namespace terraingen | ||
| 57 |