Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
compat.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @file compat.hpp
20 * @brief Eigen-style Aigen values and build-selected planner aliases.
21 * @details Aigen always uses aigenlib and can coexist with Eigen in one translation
22 * unit. The planner aliases follow DIPLA_USE_EIGEN consistently across units.
23 * Supports a small value API, not Eigen's general expression templates.
24 */
25#pragma once
26
27#include "aigenlib.hpp"
28
29#ifndef DIPLA_USE_EIGEN
30/** Select Eigen when nonzero; the default planner backend is Aigen/aigenlib. */
31#define DIPLA_USE_EIGEN 0
32#endif
33
34#if DIPLA_USE_EIGEN
35#include <Eigen/Dense>
36#endif
37#include <algorithm>
38#include <array>
39#include <cmath>
40#include <numeric>
41#include <stdexcept>
42#include <type_traits>
43#include <utility>
44
45namespace Aigen {
46
47/**
48 * @brief aigenlib storage with the planner's signed dimensions and finite predicate.
49 * @details Public inheritance permits zero-copy calls into aigenlib kernels.
50 */
52public:
54 /** @brief Accept an owning kernel result. @param value Matrix storage. */
55 Matrix(dipla::aigenlib::Matrix value) : dipla::aigenlib::Matrix(std::move(value)) {}
56 /** @brief Signed row count. @return Bounded rows. */
57 int rows() const noexcept { return static_cast<int>(dipla::aigenlib::Matrix::rows()); }
58 /** @brief Signed column count. @return Bounded columns. */
59 int cols() const noexcept { return static_cast<int>(dipla::aigenlib::Matrix::cols()); }
60 /** @brief Signed scalar count. @return Bounded scalar count. */
61 int size() const noexcept { return static_cast<int>(dipla::aigenlib::Matrix::size()); }
62 /** @brief Test every scalar. @return Whether finite. */
63 bool allFinite() const noexcept { return finite(); }
64 /** @brief Allocate zero storage. @param rows Rows. @param columns Columns. @return Zero matrix. */
65 static Matrix Zero(std::size_t rows, std::size_t columns) { return Matrix(rows, columns); }
66};
67
68/**
69 * @brief Bounded owning vector with the planner's small Eigen-style value API.
70 * @details No expression objects or implicit unbounded allocations are exposed.
71 */
73public:
74 /** @brief Allocate initialized scalars. @param count Length. @param value Initial scalar. */
75 explicit Vector(std::size_t count = 0, double value = 0.0)
76 : dipla::aigenlib::Vector(checked_size(count), value) {}
77 /** @brief Accept owning kernel output. @param values Scalars. */
78 Vector(dipla::aigenlib::Vector values) : dipla::aigenlib::Vector(std::move(values)) { checked_size(dipla::aigenlib::Vector::size()); }
79 /** @brief Signed vector length. @return Bounded length. */
80 int size() const noexcept { return static_cast<int>(dipla::aigenlib::Vector::size()); }
81 /** @brief Matrix-compatible row count. @return Length. */
82 int rows() const noexcept { return size(); }
83 /** @brief Matrix-compatible column count. @return One. */
84 int cols() const noexcept { return 1; }
85 /** @brief Checked matrix-style access. @param row Row. @param column Must be zero. @return Scalar. */
86 double operator()(std::size_t row, std::size_t column) const {
87 if (column != 0) throw std::out_of_range("Vector column must be zero");
88 return at(row);
89 }
90 /** @brief Allocate zeros. @param count Length. @return Owning vector. */
91 static Vector Zero(std::size_t count) { return Vector(count); }
92 /** @brief Allocate a constant. @param count Length. @param value Scalar. @return Owning vector. */
93 static Vector Constant(std::size_t count, double value) { return Vector(count, value); }
94 /** @brief Produce a value, never an expression. @return Owning copy. */
95 Vector eval() const { return *this; }
96 /** @brief Validate every scalar. @return Whether finite. */
97 bool allFinite() const {
98 return std::all_of(begin(), end(), [](double value) { return std::isfinite(value); });
99 }
100 /** @brief Sum all entries. @return Sum. */
101 double sum() const { return std::accumulate(begin(), end(), 0.0); }
102 /** @brief Stable Euclidean length. @return Norm. */
103 double norm() const {
104 double result = 0.0;
105 for (const double value : *this) result = std::hypot(result, value);
106 return result;
107 }
108 /** @brief Add a same-sized vector. @param other Addend. @return This vector. */
109 Vector& operator+=(const Vector& other) {
110 if (size() != other.size()) throw std::invalid_argument("Vector dimensions differ");
111 for (int index = 0; index < size(); ++index) (*this)[index] += other[index];
112 return *this;
113 }
114 /** @brief Relative norm comparison. @param other Vector. @param tolerance Relative limit. @return Whether approximately equal. */
115 bool isApprox(const Vector& other, double tolerance) const {
116 if (size() != other.size() || !allFinite() || !other.allFinite() || !(tolerance >= 0.0)) return false;
117 double error = 0.0;
118 for (int index = 0; index < size(); ++index) error = std::hypot(error, (*this)[index] - other[index]);
119 return error <= tolerance * std::min(norm(), other.norm());
120 }
121private:
122 /** @brief Validate before allocating. @param count Requested length. @return Checked length. */
123 static std::size_t checked_size(std::size_t count) {
124 if (count > dipla::aigenlib::max_extent) throw std::length_error("Planner vector exceeds grid bound");
125 return count;
126 }
127};
128
129/** @brief Add owning vectors. @param left First. @param right Second. @return Sum. */
130inline Vector operator+(Vector left, const Vector& right) { return left += right; }
131
132/**
133 * @brief aigenlib point with familiar initialization and finite-check spelling.
134 * @details Arithmetic uses the underlying value operations without heap storage.
135 */
137public:
139 /** @brief Accept the result of value arithmetic. @param point Coordinates. */
141 /** @brief Construct the origin. @return Zero coordinates. */
142 static Point Zero() { return Point(0.0, 0.0, 0.0); }
143 /** @brief Reset every coordinate to zero. */
144 void setZero() noexcept { x() = y() = z() = 0.0; }
145 /** @brief Validate coordinates. @return Whether finite. */
146 bool allFinite() const noexcept { return finite(); }
147 /** @brief Return a value, never an expression. @return This point. */
148 Point eval() const { return *this; }
149};
150
151/** Eigen-style name for the bounded dynamic double matrix. */
153/** Eigen-style name for the bounded dynamic double vector. */
155/** Eigen-style name for the three-coordinate double point. */
157
158/**
159 * @brief Owning, zero-initialized 4-by-4 column-major value matrix.
160 * @details Storage is sixteen scalars on the stack; products return owning
161 * values and support aliasing assignments without expression-lifetime issues.
162 * This subset provides initialization, access, transpose, trace and products,
163 * not decompositions or a general Eigen expression API.
164 * @tparam Number Floating-point scalar type.
165 */
166template <class Number>
167class Matrix4 {
168 static_assert(std::is_floating_point_v<Number>, "Matrix4 needs floating-point scalars");
169public:
170 /** Eigen-compatible scalar type. */
171 using Scalar = Number;
172 /** @brief Row count. @return Four. */
173 constexpr int rows() const noexcept { return 4; }
174 /** @brief Column count. @return Four. */
175 constexpr int cols() const noexcept { return 4; }
176 /** @brief Scalar count. @return Sixteen. */
177 constexpr int size() const noexcept { return 16; }
178 /** @brief Checked mutable element. @param row Row. @param column Column. @return Scalar reference. */
179 Scalar& operator()(int row, int column) { return values_[offset(row, column)]; }
180 /** @brief Checked element. @param row Row. @param column Column. @return Scalar. */
181 Scalar operator()(int row, int column) const { return values_[offset(row, column)]; }
182 /** @brief Allocate a zero value. @return Zero matrix. */
183 static Matrix4 Zero() { return {}; }
184 /** @brief Construct a constant value. @param value Scalar. @return Constant matrix. */
185 static Matrix4 Constant(Scalar value) {
187 result.values_.fill(value);
188 return result;
189 }
190 /** @brief Construct the identity. @return Identity matrix. */
191 static Matrix4 Identity() {
193 for (int index = 0; index < 4; ++index) result(index, index) = Scalar(1);
194 return result;
195 }
196 /** @brief Validate all entries. @return Whether finite. */
197 bool allFinite() const {
198 return std::all_of(values_.begin(), values_.end(), [](Scalar value) { return std::isfinite(value); });
199 }
200 /** @brief Sum the diagonal. @return Trace. */
201 Scalar trace() const {
202 Scalar result = 0;
203 for (int index = 0; index < 4; ++index) result += (*this)(index, index);
204 return result;
205 }
206 /** @brief Return an owning transpose. @return Transposed matrix. */
209 for (int row = 0; row < 4; ++row) {
210 for (int column = 0; column < 4; ++column) result(row, column) = (*this)(column, row);
211 }
212 return result;
213 }
214 /** @brief Multiply two owning values. @param right Right operand. @return Product. */
215 Matrix4 operator*(const Matrix4& right) const {
217 for (int column = 0; column < 4; ++column) {
218 for (int inner = 0; inner < 4; ++inner) {
219 const Scalar factor = right(inner, column);
220 for (int row = 0; row < 4; ++row) result(row, column) += (*this)(row, inner) * factor;
221 }
222 }
223 return result;
224 }
225private:
226 /** Zero-initialized, fixed-size column-major storage. */
227 std::array<Scalar, 16> values_{};
228 /** @brief Validate an element address. @param row Row. @param column Column. @return Flat offset. */
229 static std::size_t offset(int row, int column) {
230 if (row < 0 || row >= 4 || column < 0 || column >= 4) throw std::out_of_range("Matrix4 index");
231 return static_cast<std::size_t>(column * 4 + row);
232 }
233};
234
235/** Eigen-style 4-by-4 float matrix, with actual float storage/arithmetic. */
237/** Eigen-style 4-by-4 double matrix. */
239
240}
241
242namespace aipp::math {
243#if DIPLA_USE_EIGEN
244/** Row-major Eigen matrix selected explicitly at build time. */
245using MatrixXd = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
246/** Eigen vector selected explicitly at build time. */
247using VectorXd = Eigen::VectorXd;
248/** Eigen three-coordinate point selected explicitly at build time. */
249using Vector3d = Eigen::Vector3d;
250/** Selected 4-by-4 float matrix. */
251using Matrix4f = Eigen::Matrix4f;
252/** Selected 4-by-4 double matrix. */
253using Matrix4d = Eigen::Matrix4d;
254/** Name of the compiled backend, independent of runtime configuration. */
255inline constexpr const char* backend = "eigen";
256/** Artifact-readable record retained by the executable's --build-info output. */
257inline constexpr const char* build_info = "DIPLA_MATH_BACKEND=eigen";
258#else
259/** Selected dynamic double matrix. */
261/** Selected dynamic double vector. */
263/** Selected three-coordinate double point. */
265/** Selected 4-by-4 float matrix. */
267/** Selected 4-by-4 double matrix. */
269/** Name of the default compiled backend. */
270inline constexpr const char* backend = "aigenlib";
271/** Artifact-readable record retained by the executable's --build-info output. */
272inline constexpr const char* build_info = "DIPLA_MATH_BACKEND=aigenlib";
273#endif
274
275/** Existing planner matrix spelling. */
277/** Existing planner vector spelling. */
279/** Existing planner point spelling. */
281
282}
Bounded, owning numerical types and Gaussian-process operations.
Owning, zero-initialized 4-by-4 column-major value matrix.
Definition compat.hpp:167
constexpr int cols() const noexcept
Column count.
Definition compat.hpp:175
bool allFinite() const
Validate all entries.
Definition compat.hpp:197
Scalar & operator()(int row, int column)
Checked mutable element.
Definition compat.hpp:179
static Matrix4 Constant(Scalar value)
Construct a constant value.
Definition compat.hpp:185
std::array< Scalar, 16 > values_
Definition compat.hpp:227
constexpr int size() const noexcept
Scalar count.
Definition compat.hpp:177
Scalar operator()(int row, int column) const
Checked element.
Definition compat.hpp:181
Matrix4 operator*(const Matrix4 &right) const
Multiply two owning values.
Definition compat.hpp:215
static Matrix4 Identity()
Construct the identity.
Definition compat.hpp:191
Number Scalar
Definition compat.hpp:171
static std::size_t offset(int row, int column)
Validate an element address.
Definition compat.hpp:229
constexpr int rows() const noexcept
Row count.
Definition compat.hpp:173
Scalar trace() const
Sum the diagonal.
Definition compat.hpp:201
static Matrix4 Zero()
Allocate a zero value.
Definition compat.hpp:183
Matrix4 transpose() const
Return an owning transpose.
Definition compat.hpp:207
aigenlib storage with the planner's signed dimensions and finite predicate.
Definition compat.hpp:51
int size() const noexcept
Signed scalar count.
Definition compat.hpp:61
int cols() const noexcept
Signed column count.
Definition compat.hpp:59
static Matrix Zero(std::size_t rows, std::size_t columns)
Allocate zero storage.
Definition compat.hpp:65
bool allFinite() const noexcept
Test every scalar.
Definition compat.hpp:63
Matrix()=default
Construct an empty matrix.
int rows() const noexcept
Signed row count.
Definition compat.hpp:57
Matrix(dipla::aigenlib::Matrix value)
Accept an owning kernel result.
Definition compat.hpp:55
aigenlib point with familiar initialization and finite-check spelling.
Definition compat.hpp:136
bool allFinite() const noexcept
Validate coordinates.
Definition compat.hpp:146
void setZero() noexcept
Reset every coordinate to zero.
Definition compat.hpp:144
static Point Zero()
Construct the origin.
Definition compat.hpp:142
Point(const dipla::aigenlib::Point &point)
Accept the result of value arithmetic.
Definition compat.hpp:140
Point eval() const
Return a value, never an expression.
Definition compat.hpp:148
Bounded owning vector with the planner's small Eigen-style value API.
Definition compat.hpp:72
bool allFinite() const
Validate every scalar.
Definition compat.hpp:97
Vector(dipla::aigenlib::Vector values)
Accept owning kernel output.
Definition compat.hpp:78
Vector eval() const
Produce a value, never an expression.
Definition compat.hpp:95
bool isApprox(const Vector &other, double tolerance) const
Relative norm comparison.
Definition compat.hpp:115
int size() const noexcept
Signed vector length.
Definition compat.hpp:80
static Vector Constant(std::size_t count, double value)
Allocate a constant.
Definition compat.hpp:93
double sum() const
Sum all entries.
Definition compat.hpp:101
int rows() const noexcept
Matrix-compatible row count.
Definition compat.hpp:82
int cols() const noexcept
Matrix-compatible column count.
Definition compat.hpp:84
static std::size_t checked_size(std::size_t count)
Validate before allocating.
Definition compat.hpp:123
Vector(std::size_t count=0, double value=0.0)
Allocate initialized scalars.
Definition compat.hpp:75
static Vector Zero(std::size_t count)
Allocate zeros.
Definition compat.hpp:91
double norm() const
Stable Euclidean length.
Definition compat.hpp:103
Vector & operator+=(const Vector &other)
Add a same-sized vector.
Definition compat.hpp:109
double operator()(std::size_t row, std::size_t column) const
Checked matrix-style access.
Definition compat.hpp:86
Owning, contiguous, row-major double matrix.
Definition aigenlib.hpp:95
std::size_t cols() const noexcept
Column count.
Definition aigenlib.cpp:241
std::size_t size() const noexcept
Allocated scalar count.
Definition aigenlib.cpp:242
Matrix()=default
Construct an empty matrix.
bool finite() const noexcept
Validate all stored scalars.
Definition aigenlib.cpp:259
std::size_t rows() const noexcept
Row count.
Definition aigenlib.cpp:240
uint8_t row[LWIR_MAX_WIDTH *3U]
Definition lwir_image.c:63
struct jpeg_error_mgr error
Definition lwir_image.c:60
Matrix4< double > Matrix4d
Definition compat.hpp:238
Point Vector3d
Definition compat.hpp:156
Vector VectorXd
Definition compat.hpp:154
Matrix MatrixXd
Definition compat.hpp:152
Vector operator+(Vector left, const Vector &right)
Add owning vectors.
Definition compat.hpp:130
Matrix4< float > Matrix4f
Definition compat.hpp:236
constexpr const char * build_info
Definition compat.hpp:272
Aigen::Matrix4d Matrix4d
Definition compat.hpp:268
Aigen::VectorXd VectorXd
Definition compat.hpp:262
VectorXd Vector
Definition compat.hpp:278
Aigen::MatrixXd MatrixXd
Definition compat.hpp:260
Vector3d Point
Definition compat.hpp:280
Aigen::Vector3d Vector3d
Definition compat.hpp:264
Aigen::Matrix4f Matrix4f
Definition compat.hpp:266
constexpr const char * backend
Definition compat.hpp:270
MatrixXd Matrix
Definition compat.hpp:276
std::vector< double > Vector
Definition aigenlib.hpp:37
constexpr std::size_t max_extent
Definition aigenlib.hpp:33
static size_t count
Definition pose_log.c:79
Three-dimensional value type for route arithmetic.
Definition aigenlib.hpp:43
double & y() noexcept
Access north.
Definition aigenlib.cpp:190
bool finite() const noexcept
Check every coordinate.
Definition aigenlib.cpp:197
Point(double east=0.0, double north=0.0, double altitude=0.0)
Construct a point.
Definition aigenlib.cpp:187
double & x() noexcept
Access east.
Definition aigenlib.cpp:188
double & z() noexcept
Access altitude.
Definition aigenlib.cpp:192
One timestamped geographic position for a replay annotation.
struct usher_control_result result
Definition usher.c:377