20#include <nlohmann/json.hpp>
21using json = nlohmann::json;
23#include <fmt/ostream.h>
47template<
typename _Scalar,
int _Rows,
int _Cols>
48void to_json(
json& j,
const Matrix<_Scalar, _Rows, _Cols>& matrix)
50 for (
int r = 0; r < matrix.rows(); r++)
52 for (
int c = 0; c < matrix.cols(); c++)
54 if (std::isnan(matrix(r, c))) { j[std::to_string(r)][std::to_string(c)] =
"NaN"; }
55 else { j[std::to_string(r)][std::to_string(c)] = matrix(r, c); }
66template<
typename _Scalar,
int _Rows,
int _Cols>
67void from_json(
const json& j, Matrix<_Scalar, _Rows, _Cols>& matrix)
69 if constexpr (_Rows == -1 || _Cols == -1)
73 for (
int r = 0; j.contains(std::to_string(r)); r++)
75 rows = std::max(r, rows);
76 for (
int c = 0; j.at(std::to_string(r)).contains(std::to_string(c)); c++)
78 cols = std::max(c, cols);
81 matrix = Eigen::Matrix<_Scalar, _Rows, _Cols>::Zero(rows + 1, cols + 1);
84 for (
int r = 0; j.contains(std::to_string(r)); r++)
86 for (
int c = 0; j.at(std::to_string(r)).contains(std::to_string(c)); c++)
88 if (j.at(std::to_string(r)).at(std::to_string(c)).is_string())
90 auto str = j.at(std::to_string(r)).at(std::to_string(c)).get<std::string>();
91 if (str ==
"NaN") { matrix(r, c) = std::nan(
""); }
92 else {
LOG_WARN(
"Reading matrix value failed at position ({}, {}). Value is an unknown string '{}'.", r, c, str); }
94 else if (j.at(std::to_string(r)).at(std::to_string(c)).is_number())
96 j.at(std::to_string(r)).at(std::to_string(c)).get_to(matrix(r, c));
100 LOG_WARN(
"Reading matrix value failed at position ({}, {}). Value has the type '{}' which cannot be converted into a floating point number.", r, c, j.at(std::to_string(r)).at(std::to_string(c)).type_name());
115template<
typename Derived>
116void removeRows(Eigen::DenseBase<Derived>& matrix,
size_t index,
size_t length)
118 INS_ASSERT_USER_ERROR(
static_cast<size_t>(matrix.rows()) >= index + length,
"Tried to remove rows which do not exist");
120 std::vector<int> indicesToKeep;
121 indicesToKeep.reserve(
static_cast<size_t>(matrix.rows()) - length);
122 for (
int i = 0; i < static_cast<int>(index); i++) { indicesToKeep.push_back(i); }
123 for (
int i =
static_cast<int>(index + length); i < matrix.rows(); i++) { indicesToKeep.push_back(i); }
125 matrix = matrix(indicesToKeep, Eigen::all).eval();
131template<
typename Derived>
132void removeRows(Eigen::DenseBase<Derived>& matrix,
const std::vector<int>& rowIndices)
134 std::vector<int> rowIndicesToKeep;
135 rowIndicesToKeep.reserve(
static_cast<size_t>(matrix.rows()) - rowIndices.size());
136 for (
int i = 0; i < matrix.rows(); i++)
138 if (std::find(rowIndices.begin(), rowIndices.end(), i) == rowIndices.end())
140 rowIndicesToKeep.push_back(i);
144 matrix = matrix(rowIndicesToKeep, Eigen::all).eval();
151template<
typename Derived>
152void removeCols(Eigen::DenseBase<Derived>& matrix,
size_t index,
size_t length)
154 INS_ASSERT_USER_ERROR(
static_cast<size_t>(matrix.cols()) >= index + length,
"Tried to remove cols which do not exist");
156 std::vector<int> indicesToKeep;
157 indicesToKeep.reserve(
static_cast<size_t>(matrix.cols()) - length);
158 for (
int i = 0; i < static_cast<int>(index); i++) { indicesToKeep.push_back(i); }
159 for (
int i =
static_cast<int>(index + length); i < matrix.cols(); i++) { indicesToKeep.push_back(i); }
161 matrix = matrix(Eigen::all, indicesToKeep).eval();
167template<
typename Derived>
168void removeCols(Eigen::DenseBase<Derived>& matrix,
const std::vector<int>& colIndices)
170 std::vector<int> colIndicesToKeep;
171 colIndicesToKeep.reserve(
static_cast<size_t>(matrix.cols()) - colIndices.size());
172 for (
int i = 0; i < matrix.cols(); i++)
174 if (std::find(colIndices.begin(), colIndices.end(), i) == colIndices.end())
176 colIndicesToKeep.push_back(i);
180 matrix = matrix(Eigen::all, colIndicesToKeep).eval();
189template<
typename Derived>
190void removeRowsAndCols(Eigen::DenseBase<Derived>& matrix,
size_t row,
size_t rows,
size_t col,
size_t cols)
192 INS_ASSERT_USER_ERROR(
static_cast<size_t>(matrix.rows()) >= row + rows,
"Tried to remove rows which do not exist");
193 INS_ASSERT_USER_ERROR(
static_cast<size_t>(matrix.cols()) >= col + cols,
"Tried to remove cols which do not exist");
195 std::vector<int> rowsToKeep;
196 rowsToKeep.reserve(
static_cast<size_t>(matrix.rows()) - rows);
197 for (
int i = 0; i < static_cast<int>(row); i++) { rowsToKeep.push_back(i); }
198 for (
int i =
static_cast<int>(row + rows); i < matrix.rows(); i++) { rowsToKeep.push_back(i); }
200 std::vector<int> colsToKeep;
201 colsToKeep.reserve(
static_cast<size_t>(matrix.cols()) - cols);
202 for (
int i = 0; i < static_cast<int>(col); i++) { colsToKeep.push_back(i); }
203 for (
int i =
static_cast<int>(col + cols); i < matrix.cols(); i++) { colsToKeep.push_back(i); }
205 matrix = matrix(rowsToKeep, colsToKeep).eval();
212template<
typename Derived>
213void removeRowsAndCols(Eigen::DenseBase<Derived>& matrix,
const std::vector<int>& rowIndices,
const std::vector<int>& colIndices)
215 std::vector<int> rowIndicesToKeep;
216 rowIndicesToKeep.reserve(
static_cast<size_t>(matrix.rows()) - rowIndices.size());
217 for (
int i = 0; i < matrix.rows(); i++)
219 if (std::find(rowIndices.begin(), rowIndices.end(), i) == rowIndices.end())
221 rowIndicesToKeep.push_back(i);
225 std::vector<int> colIndicesToKeep;
226 colIndicesToKeep.reserve(
static_cast<size_t>(matrix.cols()) - colIndices.size());
227 for (
int i = 0; i < matrix.cols(); i++)
229 if (std::find(colIndices.begin(), colIndices.end(), i) == colIndices.end())
231 colIndicesToKeep.push_back(i);
235 matrix = matrix(rowIndicesToKeep, colIndicesToKeep).eval();
240#ifndef DOXYGEN_IGNORE
245 requires std::is_base_of_v<Eigen::DenseBase<T>, T>
246struct fmt::formatter<T> : ostream_formatter
255struct fmt::formatter<Eigen::Quaternionf> : ostream_formatter
258struct fmt::formatter<Eigen::Quaterniond> : ostream_formatter
261struct fmt::formatter<Eigen::
Quaternionld> : ostream_formatter
#define INS_ASSERT_USER_ERROR(_EXP, _MSG)
Assert function with message.
Definition Assert.h:21
Quaternion< long double > Quaternionld
Long double Eigen::Quaternion.
Definition Eigen.hpp:37
Matrix< long double, 3, 3 > Matrix3ld
Long double 3x3 Eigen::Matrix.
Definition Eigen.hpp:34
void removeRows(Eigen::DenseBase< Derived > &matrix, size_t index, size_t length)
Removes rows from a matrix or vector.
Definition Eigen.hpp:116
Matrix< long double, 3, 1 > Vector3ld
Long double 3x1 Eigen::Vector.
Definition Eigen.hpp:31
Matrix< long double, 4, 4 > Matrix4ld
Long double 4x4 Eigen::Matrix.
Definition Eigen.hpp:35
Array< long double, 3, 1 > Array3ld
Long double 3x1 Eigen::Array.
Definition Eigen.hpp:29
void removeCols(Eigen::DenseBase< Derived > &matrix, size_t index, size_t length)
Removes columns from a matrix or vector.
Definition Eigen.hpp:152
Matrix< long double, 4, 1 > Vector4ld
Long double 3x1 Eigen::Vector.
Definition Eigen.hpp:32
AngleAxis< long double > AngleAxisld
Long double Eigen::AngleAxis.
Definition Eigen.hpp:39
void removeRowsAndCols(Eigen::DenseBase< Derived > &matrix, size_t row, size_t rows, size_t col, size_t cols)
Removes rows and columns from a matrix or vector.
Definition Eigen.hpp:190
nlohmann::json json
json namespace
Definition FlowManager.hpp:21
Utility class for logging to console and file.
#define LOG_WARN
Error occurred, but a fallback option exists and program continues to work normally.
Definition Logger.hpp:71