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
25#pragma once
26
27#include "aigenlib.hpp"
28
29#ifndef DIPLA_USE_EIGEN
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
52public:
55 Matrix(dipla::aigenlib::Matrix value) : dipla::aigenlib::Matrix(std::move(value)) {}
57 int rows() const noexcept { return static_cast<int>(dipla::aigenlib::Matrix::rows()); }
59 int cols() const noexcept { return static_cast<int>(dipla::aigenlib::Matrix::cols()); }
61 int size() const noexcept { return static_cast<int>(dipla::aigenlib::Matrix::size()); }
63 bool allFinite() const noexcept { return finite(); }
65 static Matrix Zero(std::size_t rows, std::size_t columns) { return Matrix(rows, columns); }
66};
67
73public:
75 explicit Vector(std::size_t count = 0, double value = 0.0)
76 : dipla::aigenlib::Vector(checked_size(count), value) {}
77
78 Vector(dipla::aigenlib::Vector values) : dipla::aigenlib::Vector(std::move(values)) { checked_size(dipla::aigenlib::Vector::size()); }
80 int size() const noexcept { return static_cast<int>(dipla::aigenlib::Vector::size()); }
82 int rows() const noexcept { return size(); }
84 int cols() const noexcept { return 1; }
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
91 static Vector Zero(std::size_t count) { return Vector(count); }
93 static Vector Constant(std::size_t count, double value) { return Vector(count, value); }
95 Vector eval() const { return *this; }
97 bool allFinite() const {
98 return std::all_of(begin(), end(), [](double value) { return std::isfinite(value); });
99 }
100
101 double sum() const { return std::accumulate(begin(), end(), 0.0); }
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
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
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:
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
130inline Vector operator+(Vector left, const Vector& right) { return left += right; }
131
137public:
140 Point(const dipla::aigenlib::Point& point) : dipla::aigenlib::Point(point) {}
142 static Point Zero() { return Point(0.0, 0.0, 0.0); }
144 void setZero() noexcept { x() = y() = z() = 0.0; }
146 bool allFinite() const noexcept { return finite(); }
148 Point eval() const { return *this; }
149};
150
157
166template <class Number>
167class Matrix4 {
168 static_assert(std::is_floating_point_v<Number>, "Matrix4 needs floating-point scalars");
169public:
171 using Scalar = Number;
173 constexpr int rows() const noexcept { return 4; }
175 constexpr int cols() const noexcept { return 4; }
177 constexpr int size() const noexcept { return 16; }
179 Scalar& operator()(int row, int column) { return values_[offset(row, column)]; }
181 Scalar operator()(int row, int column) const { return values_[offset(row, column)]; }
183 static Matrix4 Zero() { return {}; }
185 static Matrix4 Constant(Scalar value) {
186 Matrix4 result;
187 result.values_.fill(value);
188 return result;
189 }
190
191 static Matrix4 Identity() {
192 Matrix4 result;
193 for (int index = 0; index < 4; ++index) result(index, index) = Scalar(1);
194 return result;
195 }
196
197 bool allFinite() const {
198 return std::all_of(values_.begin(), values_.end(), [](Scalar value) { return std::isfinite(value); });
199 }
200
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
208 Matrix4 result;
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
215 Matrix4 operator*(const Matrix4& right) const {
216 Matrix4 result;
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:
227 std::array<Scalar, 16> values_{};
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
239
240}
241
242namespace aipp::math {
243#if DIPLA_USE_EIGEN
245using MatrixXd = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
247using VectorXd = Eigen::VectorXd;
249using Vector3d = Eigen::Vector3d;
251using Matrix4f = Eigen::Matrix4f;
253using Matrix4d = Eigen::Matrix4d;
255inline constexpr const char* backend = "eigen";
257inline constexpr const char* build_info = "DIPLA_MATH_BACKEND=eigen";
258#else
270inline constexpr const char* backend = "aigenlib";
272inline constexpr const char* build_info = "DIPLA_MATH_BACKEND=aigenlib";
273#endif
274
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
Eigen-compatible scalar type.
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
Definition compat.hpp:45
Matrix4< double > Matrix4d
Eigen-style 4-by-4 double matrix.
Definition compat.hpp:238
Point Vector3d
Eigen-style name for the three-coordinate double point.
Definition compat.hpp:156
Vector VectorXd
Eigen-style name for the bounded dynamic double vector.
Definition compat.hpp:154
Matrix MatrixXd
Eigen-style name for the bounded dynamic double matrix.
Definition compat.hpp:152
Vector operator+(Vector left, const Vector &right)
Add owning vectors.
Definition compat.hpp:130
Matrix4< float > Matrix4f
Eigen-style 4-by-4 float matrix, with actual float storage/arithmetic.
Definition compat.hpp:236
Definition compat.hpp:242
constexpr const char * build_info
Artifact-readable record retained by the executable's –build-info output.
Definition compat.hpp:272
Aigen::Matrix4d Matrix4d
Selected 4-by-4 double matrix.
Definition compat.hpp:268
Aigen::VectorXd VectorXd
Selected dynamic double vector.
Definition compat.hpp:262
VectorXd Vector
Existing planner vector spelling.
Definition compat.hpp:278
Aigen::MatrixXd MatrixXd
Selected dynamic double matrix.
Definition compat.hpp:260
Vector3d Point
Existing planner point spelling.
Definition compat.hpp:280
Aigen::Vector3d Vector3d
Selected three-coordinate double point.
Definition compat.hpp:264
Aigen::Matrix4f Matrix4f
Selected 4-by-4 float matrix.
Definition compat.hpp:266
constexpr const char * backend
Name of the default compiled backend.
Definition compat.hpp:270
MatrixXd Matrix
Existing planner matrix spelling.
Definition compat.hpp:276
std::vector< double > Vector
Owning scalar vector; size validation belongs to the consuming operation.
Definition aigenlib.hpp:37
constexpr std::size_t max_extent
Maximum matrix extent, matching the current dense GP grid ceiling.
Definition aigenlib.hpp:33
Definition aigenlib.cpp:42
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