FancySafeBot 0.0.1
A safe robotics library
Loading...
Searching...
No Matches
fsb_types.h
1
2#ifndef FSB_TYPES_H
3#define FSB_TYPES_H
4
5#include <array>
6#include <cmath>
7#include <cstddef>
8
12#define FSB_TOL (4.5e-15)
13
17#define FSB_CART_SIZE (6U)
18
19namespace fsb
20{
21
25using Real = double;
26
30struct Vec3
31{
33 Real x = 0.0;
35 Real y = 0.0;
37 Real z = 0.0;
38};
39
43struct Mat3Sym
44{
46 Real m00 = 0.0;
48 Real m11 = 0.0;
50 Real m22 = 0.0;
52 Real m01 = 0.0;
54 Real m02 = 0.0;
56 Real m12 = 0.0;
57};
58
62struct Mat3
63{
65 Real m00 = 0.0;
67 Real m10 = 0.0;
69 Real m20 = 0.0;
71 Real m01 = 0.0;
73 Real m11 = 0.0;
75 Real m21 = 0.0;
77 Real m02 = 0.0;
79 Real m12 = 0.0;
81 Real m22 = 0.0;
82};
83
87template<typename T>
88class Span
89{
90public:
91 Span() = default;
92 Span(T* ptr, size_t len) noexcept : m_ptr(ptr), m_len(len) {}
93
94 [[nodiscard]] T* data() noexcept { return m_ptr; }
95 [[nodiscard]] const T* data() const noexcept { return m_ptr; }
96 [[nodiscard]] size_t size() const noexcept { return m_len; }
97 [[nodiscard]] bool empty() const noexcept { return m_len == 0U; }
98
99 T& operator[](size_t ind) noexcept { return *(m_ptr + ind); }
100 const T& operator[](size_t ind) const noexcept { return *(m_ptr + ind); }
101
102 [[nodiscard]] T* begin() noexcept { return m_ptr; }
103 [[nodiscard]] T* end() noexcept { return m_ptr + m_len; }
104 [[nodiscard]] const T* begin() const noexcept { return m_ptr; }
105 [[nodiscard]] const T* end() const noexcept { return m_ptr + m_len; }
106
107 [[nodiscard]] Span<T> active(size_t num, size_t offset = 0U) noexcept
108 {
109 const size_t off_clamped = (offset < m_len) ? offset : m_len;
110 const size_t remaining = m_len - off_clamped;
111 const size_t count = (num < remaining) ? num : remaining;
112 return Span<T>(m_ptr + off_clamped, count);
113 }
114 [[nodiscard]] Span<const T> active(size_t num, size_t offset = 0U) const noexcept
115 {
116 const size_t off_clamped = (offset < m_len) ? offset : m_len;
117 const size_t remaining = m_len - off_clamped;
118 const size_t count = (num < remaining) ? num : remaining;
119 return Span<const T>(m_ptr + off_clamped, count);
120 }
121
122private:
123 T* m_ptr = nullptr;
124 size_t m_len = 0U;
125};
126
127template<size_t Size, typename Type = Real>
128struct Array : public std::array<Type, Size>
129{
130 Span<Type> active(size_t num) noexcept
131 {
132 const size_t num_count = (num < Size) ? num : Size;
133 return Span<Type>(this->data(), num_count);
134 }
135 Span<const Type> active(size_t num) const noexcept
136 {
137 const size_t num_count = (num < Size) ? num : Size;
138 return Span<const Type>(this->data(), num_count);
139 }
140};
141
142} // namespace fsb
143
144#endif
Non-owning view over a contiguous sequence of elements (C++17 substitute for std::span)
Definition fsb_types.h:89
Definition fsb_types.h:129
3x3 symmetric matrix
Definition fsb_types.h:44
Real m00
Element [0, 0] of matrix.
Definition fsb_types.h:46
Real m01
Element [0, 1] and [1, 0] of matrix.
Definition fsb_types.h:52
Real m11
Element [1, 1] of matrix.
Definition fsb_types.h:48
Real m22
Element [2, 2] of matrix.
Definition fsb_types.h:50
Real m12
Element [1, 2] and [2, 1] of matrix.
Definition fsb_types.h:56
Real m02
Element [0, 2] and [2, 0] of matrix.
Definition fsb_types.h:54
3x3 matrix
Definition fsb_types.h:63
Real m02
Element [0, 2] of matrix.
Definition fsb_types.h:77
Real m11
Element [1, 1] of matrix.
Definition fsb_types.h:73
Real m21
Element [2, 1] of matrix.
Definition fsb_types.h:75
Real m12
Element [1, 2] of matrix.
Definition fsb_types.h:79
Real m20
Element [2, 0] of matrix.
Definition fsb_types.h:69
Real m00
Element [0, 0] of matrix.
Definition fsb_types.h:65
Real m01
Element [0, 1] of matrix.
Definition fsb_types.h:71
Real m10
Element [1, 0] of matrix.
Definition fsb_types.h:67
Real m22
Element [2, 2] of matrix.
Definition fsb_types.h:81
3D vector
Definition fsb_types.h:31
Real y
Y component.
Definition fsb_types.h:35
Real z
Z component.
Definition fsb_types.h:37
Real x
X component.
Definition fsb_types.h:33