Valkyrie 2026
Loading...
Searching...
No Matches
UnitMap.h
1#pragma once
2
3#include "valkyrie/util/eigenunit/UnitVector.h"
4
5namespace valor {
6namespace EigenUnit {
7
17template <typename... UnitTags>
18class UnitMap {
19 public:
21 static constexpr std::size_t Size = sizeof...(UnitTags);
23 using StorageType = Eigen::Matrix<double, Size, 1>;
25 using MapType = Eigen::Map<StorageType>;
27 using ConstMapType = Eigen::Map<const StorageType>;
29 using UnitTuple = std::tuple<UnitTags...>;
30
31 // -----------------------------------------------------------------------
32 // Construction
33 // -----------------------------------------------------------------------
34
39 explicit UnitMap(double* ptr) : map(ptr) {}
40
41 // Non-copyable: copying a map would alias the same buffer under two
42 // objects, which is confusing. Use UnitVector for owned copies.
43 UnitMap(const UnitMap&) = delete;
44 UnitMap& operator=(const UnitMap&) = delete;
45
47 UnitMap(UnitMap&&) = default;
49 UnitMap& operator=(UnitMap&&) = default;
50
51 // -----------------------------------------------------------------------
52 // Read access
53 // -----------------------------------------------------------------------
54
59 template <std::size_t N>
60 requires ValidIndex<N, UnitTags...>
61 constexpr auto Get() const {
62 using T = std::tuple_element_t<N, UnitTuple>;
63 return units::unit_t<T>(map[N]);
64 }
65
67 ConstMapType Raw() const { return ConstMapType(map.data()); }
68
70 MapType Raw() { return map; }
71
72 // -----------------------------------------------------------------------
73 // Write access
74 // -----------------------------------------------------------------------
75
81 template <std::size_t N>
82 requires ValidIndex<N, UnitTags...>
83 void Set(units::unit_t<std::tuple_element_t<N, UnitTuple>> val) {
84 map[N] = val.value();
85 }
86
91 template <typename... OtherTags>
93 requires UnitEquality<UnitTuple, std::tuple<OtherTags...>>
94 {
95 map = vec.Raw();
96 return *this;
97 }
98
99 // -----------------------------------------------------------------------
100 // Conversion
101 // -----------------------------------------------------------------------
102
107 UnitVector<UnitTags...> ToVector() const { return UnitVector<UnitTags...>(StorageType(map)); }
108
112 operator UnitVector<UnitTags...>() const { return ToVector(); }
113
114 // -----------------------------------------------------------------------
115 // Named accessors (mirrors UnitVector)
116 // -----------------------------------------------------------------------
117
119 constexpr auto X() const
120 requires(Size >= 1)
121 {
122 return this->Get<0>();
123 }
124
126 void X(units::unit_t<std::tuple_element_t<Size >= 1 ? 0 : 0, UnitTuple>> val)
127 requires(Size >= 1)
128 {
129 this->Set<0>(val);
130 }
131
133 constexpr auto Y() const
134 requires(Size >= 2)
135 {
136 return this->Get<1>();
137 }
138
140 void Y(units::unit_t<std::tuple_element_t<Size >= 2 ? 1 : 0, UnitTuple>> val)
141 requires(Size >= 2)
142 {
143 this->Set<1>(val);
144 }
145
147 constexpr auto Z() const
148 requires(Size >= 3)
149 {
150 return this->Get<2>();
151 }
152
154 void Z(units::unit_t<std::tuple_element_t<Size >= 3 ? 2 : 0, UnitTuple>> val)
155 requires(Size >= 3)
156 {
157 this->Set<2>(val);
158 }
159
161 constexpr auto W() const
162 requires(Size >= 4)
163 {
164 return this->Get<3>();
165 }
166
168 void W(units::unit_t<std::tuple_element_t<Size >= 4 ? 3 : 0, UnitTuple>> val)
169 requires(Size >= 4)
170 {
171 this->Set<3>(val);
172 }
173
174 // -----------------------------------------------------------------------
175 // Debug
176 // -----------------------------------------------------------------------
177
181 friend std::ostream& operator<<(std::ostream& os, const UnitMap& m) {
182 os << "[";
183 m.print_impl(os, std::make_index_sequence<Size>{});
184 os << "]";
185 return os;
186 }
187
188 private:
189 template <std::size_t... Is>
190 void print_impl(std::ostream& os, std::index_sequence<Is...>) const {
191 ((os << units::unit_t<std::tuple_element_t<Is, UnitTuple>>(map[Is]).value() << (Is == Size - 1 ? "" : ", ")), ...);
192 }
193
194 MapType map;
195};
196
201template <typename... UnitTags>
203 public:
205 static constexpr std::size_t Size = sizeof...(UnitTags);
207 using StorageType = Eigen::Matrix<double, Size, 1>;
209 using ConstMapType = Eigen::Map<const StorageType>;
211 using UnitTuple = std::tuple<UnitTags...>;
212
217 explicit ConstUnitMap(const double* ptr) : map(ptr) {}
218
223 explicit ConstUnitMap(const UnitMap<UnitTags...>& m) : map(m.Raw().data()) {}
224
225 ConstUnitMap(const ConstUnitMap&) = delete;
226 ConstUnitMap& operator=(const ConstUnitMap&) = delete;
231
236 template <std::size_t N>
237 requires ValidIndex<N, UnitTags...>
238 constexpr auto Get() const {
239 using T = std::tuple_element_t<N, UnitTuple>;
240 return units::unit_t<T>(map[N]);
241 }
242
244 ConstMapType Raw() const { return map; }
245
250 UnitVector<UnitTags...> ToVector() const { return UnitVector<UnitTags...>(StorageType(map)); }
251
255 operator UnitVector<UnitTags...>() const { return ToVector(); }
256
258 constexpr auto X() const
259 requires(Size >= 1)
260 {
261 return this->Get<0>();
262 }
263
265 constexpr auto Y() const
266 requires(Size >= 2)
267 {
268 return this->Get<1>();
269 }
270
272 constexpr auto Z() const
273 requires(Size >= 3)
274 {
275 return this->Get<2>();
276 }
277
279 constexpr auto W() const
280 requires(Size >= 4)
281 {
282 return this->Get<3>();
283 }
284
288 friend std::ostream& operator<<(std::ostream& os, const ConstUnitMap& m) {
289 os << "[";
290 m.print_impl(os, std::make_index_sequence<Size>{});
291 os << "]";
292 return os;
293 }
294
295 private:
296 template <std::size_t... Is>
297 void print_impl(std::ostream& os, std::index_sequence<Is...>) const {
298 ((os << units::unit_t<std::tuple_element_t<Is, UnitTuple>>(map[Is]).value() << (Is == Size - 1 ? "" : ", ")), ...);
299 }
300
301 ConstMapType map;
302};
303
304} // namespace EigenUnit
305} // namespace valor
A non-owning, read-only unit-safe view over a const double* buffer.
Definition UnitMap.h:202
constexpr auto W() const
Access the fourth element (w).
Definition UnitMap.h:279
ConstUnitMap & operator=(ConstUnitMap &&)=default
Move assignment.
constexpr auto X() const
Access the first element (x).
Definition UnitMap.h:258
static constexpr std::size_t Size
Definition UnitMap.h:205
UnitVector< UnitTags... > ToVector() const
Produce an owned UnitVector copy of the mapped data.
Definition UnitMap.h:250
ConstUnitMap(const double *ptr)
Construct a read-only view over a raw buffer.
Definition UnitMap.h:217
constexpr auto Z() const
Access the third element (z).
Definition UnitMap.h:272
ConstUnitMap(ConstUnitMap &&)=default
Move constructor.
Eigen::Map< const StorageType > ConstMapType
Definition UnitMap.h:209
constexpr auto Y() const
Access the second element (y).
Definition UnitMap.h:265
ConstMapType Raw() const
Definition UnitMap.h:244
std::tuple< UnitTags... > UnitTuple
Definition UnitMap.h:211
friend std::ostream & operator<<(std::ostream &os, const ConstUnitMap &m)
Debug printing support.
Definition UnitMap.h:288
constexpr auto Get() const
Get the N-th element with its unit.
Definition UnitMap.h:238
ConstUnitMap(const UnitMap< UnitTags... > &m)
Construct from a mutable UnitMap (const widening).
Definition UnitMap.h:223
Eigen::Matrix< double, Size, 1 > StorageType
Definition UnitMap.h:207
A non-owning, unit-safe view over a raw double* buffer.
Definition UnitMap.h:18
constexpr auto Get() const
Get the N-th element with its unit.
Definition UnitMap.h:61
void Z(units::unit_t< std::tuple_element_t< Size >=3 ? 2 :0, UnitTuple > > val)
Set the third element (z).
Definition UnitMap.h:154
Eigen::Matrix< double, Size, 1 > StorageType
Definition UnitMap.h:23
MapType Raw()
Mutable access to the underlying Eigen map.
Definition UnitMap.h:70
std::tuple< UnitTags... > UnitTuple
Definition UnitMap.h:29
void Set(units::unit_t< std::tuple_element_t< N, UnitTuple > > val)
Set the N-th element, writing through to the buffer.
Definition UnitMap.h:83
constexpr auto W() const
Access the fourth element (w).
Definition UnitMap.h:161
void X(units::unit_t< std::tuple_element_t< Size >=1 ? 0 :0, UnitTuple > > val)
Set the first element (x).
Definition UnitMap.h:126
constexpr auto X() const
Access the first element (x).
Definition UnitMap.h:119
UnitVector< UnitTags... > ToVector() const
Produce an owned UnitVector copy of the mapped data.
Definition UnitMap.h:107
constexpr auto Y() const
Access the second element (y).
Definition UnitMap.h:133
ConstMapType Raw() const
Read-only access to the underlying Eigen map.
Definition UnitMap.h:67
Eigen::Map< StorageType > MapType
Definition UnitMap.h:25
UnitMap & operator=(UnitMap &&)=default
Move assignment.
void W(units::unit_t< std::tuple_element_t< Size >=4 ? 3 :0, UnitTuple > > val)
Set the fourth element (w).
Definition UnitMap.h:168
static constexpr std::size_t Size
Definition UnitMap.h:21
friend std::ostream & operator<<(std::ostream &os, const UnitMap &m)
Debug printing support.
Definition UnitMap.h:181
Eigen::Map< const StorageType > ConstMapType
Definition UnitMap.h:27
constexpr auto Z() const
Access the third element (z).
Definition UnitMap.h:147
void Y(units::unit_t< std::tuple_element_t< Size >=2 ? 1 :0, UnitTuple > > val)
Set the second element (y).
Definition UnitMap.h:140
UnitMap(double *ptr)
Construct a mutable view over a raw buffer.
Definition UnitMap.h:39
UnitMap & operator=(const UnitVector< OtherTags... > &vec)
Assign all values from a compatible UnitVector, writing through.
Definition UnitMap.h:92
UnitMap(UnitMap &&)=default
Move constructor.
A vector wrapper that enforces units for each element.
Definition UnitVector.h:22
Concept verifying that two tuple-based unit types match exactly.
Definition Common.h:216
Concept verifying that index N is within the bounds of a pack.
Definition Common.h:192