18#include <creos/robot_clock.hpp>
19#include <nlohmann/json.hpp>
43NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
Point, x, y, z);
59NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
Vector3d, x, y, z);
75NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
Vector3f, x, y, z);
92NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
Quaterniond, x, y, z, w);
109NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
Quaternionf, x, y, z, w);
114template <
typename T, std::
size_t n_rows, std::
size_t n_cols>
127 Matrix(
const std::array<std::array<T, n_cols>, n_rows>& data) : data_(data) {}
133 Matrix(
const std::array<T, n_rows * n_cols>& array) {
134 for (std::size_t i = 0; i < n_rows; ++i) {
135 for (std::size_t j = 0; j < n_cols; ++j) {
136 data_[i][j] = array[i * n_cols + j];
146 assert(init.size() == n_rows * n_cols);
148 for (
const auto& value : init) {
149 data_[i / n_cols][i % n_cols] = value;
158 Matrix(std::initializer_list<std::initializer_list<T>> init) {
159 assert(init.size() == n_rows);
161 for (
const auto& row : init) {
162 assert(row.size() == n_cols);
163 std::copy(row.begin(), row.end(), data_[i].begin());
173 template <
typename U>
174 Matrix(
const std::array<U, n_rows * n_cols>& array) {
175 for (std::size_t i = 0; i < n_rows; ++i) {
176 for (std::size_t j = 0; j < n_cols; ++j) {
177 data_[i][j] =
static_cast<T
>(array[i * n_cols + j]);
186 std::array<T, n_rows * n_cols>
toArray()
const {
187 std::array<T, n_rows * n_cols> array;
188 for (std::size_t i = 0; i < n_rows; ++i) {
189 for (std::size_t j = 0; j < n_cols; ++j) {
190 array[i * n_cols + j] = data_[i][j];
199 template <
typename U>
201 std::array<U, n_rows * n_cols> array;
202 for (std::size_t i = 0; i < n_rows; ++i) {
203 for (std::size_t j = 0; j < n_cols; ++j) {
204 array[i * n_cols + j] =
static_cast<U
>(data_[i][j]);
215 std::array<T, n_cols>&
operator[](std::size_t index) {
return data_[index]; }
222 const std::array<T, n_cols>&
operator[](std::size_t index)
const {
return data_[index]; }
233 auto begin()
const {
return data_.begin(); }
239 auto end()
const {
return data_.end(); }
242 std::array<std::array<T, n_cols>, n_rows> data_;
246template <
typename T, std::
size_t n_rows, std::
size_t n_cols>
247void to_json(nlohmann::json& j,
const Matrix<T, n_rows, n_cols>& matrix) {
248 j = nlohmann::json::array();
249 for (
const auto& row : matrix) {
255template <
typename T, std::
size_t n_rows, std::
size_t n_cols>
256void from_json(
const nlohmann::json& j, Matrix<T, n_rows, n_cols>& matrix) {
257 for (std::size_t i = 0; i < n_rows; ++i) {
258 for (std::size_t k = 0; k < n_cols; ++k) {
259 matrix[i][k] = j[i][k].get<T>();
267template <std::
size_t n_rows, std::
size_t n_cols>
273template <std::
size_t n_rows, std::
size_t n_cols>
Matrix template for a 2D array of type T.
Definition generic.hpp:115
const std::array< T, n_cols > & operator[](std::size_t index) const
Access the elements of the matrix.
Definition generic.hpp:222
Matrix()=default
Default constructor.
std::array< T, n_rows *n_cols > toArray() const
Convert the matrix to a 1D array.
Definition generic.hpp:186
auto begin() const
Returns a constant iterator to the beginning of the matrix.
Definition generic.hpp:233
Matrix(std::initializer_list< std::initializer_list< T > > init)
Constructor from initializer list of initializer lists.
Definition generic.hpp:158
auto end() const
Returns a constant iterator to the end of the matrix.
Definition generic.hpp:239
Matrix(const std::array< std::array< T, n_cols >, n_rows > &data)
Constructor from 2D array.
Definition generic.hpp:127
std::array< U, n_rows *n_cols > transformToArray() const
Transform the matrix to another type of array.
Definition generic.hpp:200
Matrix(std::initializer_list< T > init)
Constructor from initializer list.
Definition generic.hpp:145
Matrix(const std::array< T, n_rows *n_cols > &array)
Constructor from 1D array.
Definition generic.hpp:133
auto operator<=>(const Matrix &other) const =default
Compare two Matrix messages.
Matrix(const std::array< U, n_rows *n_cols > &array)
Transform constructor from another type of array.
Definition generic.hpp:174
std::array< T, n_cols > & operator[](std::size_t index)
Access the elements of the matrix.
Definition generic.hpp:215
The messages that are used in the communication between the agent and the client.
Definition accel.hpp:22
Point message containing a x, y and z of type double.
Definition generic.hpp:32
auto operator<=>(const Point &other) const =default
Compare two Point messages.
Quaternion message containing the x, y, z and w components as doubles.
Definition generic.hpp:80
auto operator<=>(const Quaterniond &other) const =default
Compare two Quaterniond messages.
Quaternion message containing the x, y, z and w components as floats.
Definition generic.hpp:97
auto operator<=>(const Quaternionf &other) const =default
Compare two Quaternionf messages.
Vector3d message containing a x, y and z of type double.
Definition generic.hpp:48
auto operator<=>(const Vector3d &other) const =default
Compare two Vector3d messages.
Vector3f message containing a x, y and z of type float.
Definition generic.hpp:64
auto operator<=>(const Vector3f &other) const =default
Compare two Vector3f messages.