CreOS SDK
The CreOS SDK allows you to interact with Avular robots
 
Loading...
Searching...
No Matches
generic.hpp
1// Copyright (C) 2024 Avular Holding B.V. - All Rights Reserved
2// You may use this code under the terms of the Avular
3// Software End-User License Agreement.
4//
5// You should have received a copy of the Avular
6// Software End-User License Agreement license with
7// this file, or download it from: avular.com/eula
8//
9/*****************************************************************************
10 * Created on: 2024 July 08
11 * Author: Bram Tertoolen
12 *
13 * @file Generic message constructs that are used in multiple messages.
14 ****************************************************************************/
15#pragma once
16
17#include <cassert>
18#include <creos/robot_clock.hpp>
19#include <nlohmann/json.hpp>
20
27namespace creos_messages {
28
32struct Point {
33 double x = 0.0;
34 double y = 0.0;
35 double z = 0.0;
36
40 auto operator<=>(const Point& other) const = default;
41};
42
43NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Point, x, y, z);
44
48struct Vector3d {
49 double x = 0.0;
50 double y = 0.0;
51 double z = 0.0;
52
56 auto operator<=>(const Vector3d& other) const = default;
57};
58
59NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Vector3d, x, y, z);
60
64struct Vector3f {
65 float x = 0.0f;
66 float y = 0.0f;
67 float z = 0.0f;
68
72 auto operator<=>(const Vector3f& other) const = default;
73};
74
75NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Vector3f, x, y, z);
76
81 double x = 0.0;
82 double y = 0.0;
83 double z = 0.0;
84 double w = 1.0;
85
89 auto operator<=>(const Quaterniond& other) const = default;
90};
91
92NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Quaterniond, x, y, z, w);
93
98 float x = 0.0f;
99 float y = 0.0f;
100 float z = 0.0f;
101 float w = 1.0f;
102
106 auto operator<=>(const Quaternionf& other) const = default;
107};
108
109NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Quaternionf, x, y, z, w);
110
114template <typename T, std::size_t n_rows, std::size_t n_cols>
115class Matrix {
116public:
121 Matrix() = default;
122
127 Matrix(const std::array<std::array<T, n_cols>, n_rows>& data) : data_(data) {}
128
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];
137 }
138 }
139 }
140
145 Matrix(std::initializer_list<T> init) {
146 assert(init.size() == n_rows * n_cols);
147 std::size_t i = 0;
148 for (const auto& value : init) {
149 data_[i / n_cols][i % n_cols] = value;
150 ++i;
151 }
152 }
153
158 Matrix(std::initializer_list<std::initializer_list<T>> init) {
159 assert(init.size() == n_rows);
160 std::size_t i = 0;
161 for (const auto& row : init) {
162 assert(row.size() == n_cols);
163 std::copy(row.begin(), row.end(), data_[i].begin());
164 ++i;
165 }
166 }
167
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]);
178 }
179 }
180 }
181
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];
191 }
192 }
193 return array;
194 }
195
199 template <typename U>
200 std::array<U, n_rows * n_cols> transformToArray() const {
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]);
205 }
206 }
207 return array;
208 }
209
215 std::array<T, n_cols>& operator[](std::size_t index) { return data_[index]; }
216
222 const std::array<T, n_cols>& operator[](std::size_t index) const { return data_[index]; }
223
227 auto operator<=>(const Matrix& other) const = default;
228
233 auto begin() const { return data_.begin(); }
234
239 auto end() const { return data_.end(); }
240
241private:
242 std::array<std::array<T, n_cols>, n_rows> data_;
243};
244
245// to_json function for Matrix
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) {
250 j.push_back(row);
251 }
252}
253
254// from_json function for 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>();
260 }
261 }
262}
263
267template <std::size_t n_rows, std::size_t n_cols>
269
273template <std::size_t n_rows, std::size_t n_cols>
275
276} // namespace creos_messages
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.