7#include <valkyrie/util/eigenunit/Common.h>
20template <
typename... UnitTags>
24 static constexpr std::size_t
Size =
sizeof...(UnitTags);
63 ((data[i++] = args.value()), ...);
83 StorageType d = Eigen::Map<const StorageType>(arr.data());
94 StorageType d = Eigen::Map<const StorageType>(s.data());
115 template <std::
size_t N>
117 constexpr auto Get()
const {
118 using T = std::tuple_element_t<N, UnitTuple>;
119 return units::unit_t<T>(data[N]);
127 template <std::
size_t N>
129 void Set(units::unit_t<std::tuple_element_t<N, UnitTuple>> val) {
130 data[N] = val.value();
139 template <
typename T = std::tuple_element_t<0, UnitTuple>>
143 return units::unit_t<T>(data.norm());
154 return data.normalized();
164 template <
typename... OtherTags>
165 requires(
sizeof...(UnitTags) ==
sizeof...(OtherTags)) &&
AllSameUnits<MultiplyResult<UnitTags, OtherTags>...>
168 using T1 = std::tuple_element_t<0, UnitTuple>;
169 using T2 = std::tuple_element_t<0, std::tuple<OtherTags...>>;
170 using ResultUnit = MultiplyResult<T1, T2>;
171 return units::unit_t<ResultUnit>(data.dot(other.
Raw()));
179 template <std::
size_t N>
182 return slice_impl<0>(std::make_index_sequence<N>{});
190 template <std::
size_t N>
193 return slice_impl<
Size - N>(std::make_index_sequence<N>{});
202 template <std::
size_t Start, std::
size_t N>
203 requires ValidSize<Start + N, UnitTags...>
205 return slice_impl<Start>(std::make_index_sequence<N>{});
209 template <
typename... OtherTags>
218 template <
typename... OtherTags>
230 template <
typename... OtherTags>
239 template <
typename... OtherTags>
253 template <
typename ScalarTag>
255 constexpr auto operator*(
const units::unit_t<ScalarTag>& scalar)
const {
257 return ResultType(data * scalar.value());
266 template <
typename ScalarTag>
268 constexpr auto operator/(
const units::unit_t<ScalarTag>& scalar)
const {
270 return ResultType(data / scalar.value());
301 vec.print_impl(os, std::make_index_sequence<Size>{});
307 template <
typename... OtherTags>
311 return data == other.
Raw();
320 template <
typename... OtherTags>
332 template <
typename... OtherTags>
365 return UnitVector(data.CWiseMax(lo.Raw()).CWiseMin(hi.Raw()));
375 units::unit_t<std::tuple_element_t<0, UnitTuple>> hi)
const
378 return UnitVector(data.cwiseMax(lo.value()).cwiseMin(hi.value()));
388 units::unit_t<std::tuple_element_t<0, UnitTuple>> tol = units::unit_t<std::tuple_element_t<0, UnitTuple>>{1e-9})
const
391 return data.isApprox(other.
Raw(), tol.value());
403 return UnitVector(StorageType::Constant(val.value()));
407 template <
typename T = std::tuple_element_t<0, UnitTuple>>
411 return units::unit_t<T>(data.sum());
415 template <
typename T = std::tuple_element_t<0, UnitTuple>>
419 return units::unit_t<T>(data.mean());
423 constexpr auto X() const
430 void X(units::unit_t<std::tuple_element_t<Size >= 1 ? 0 : 0,
UnitTuple>> val)
437 constexpr auto Y() const
444 void Y(units::unit_t<std::tuple_element_t<Size >= 2 ? 1 : 0,
UnitTuple>> val)
451 constexpr auto Z() const
458 void Z(units::unit_t<std::tuple_element_t<Size >= 3 ? 2 : 0,
UnitTuple>> val)
465 constexpr auto W() const
472 void W(units::unit_t<std::tuple_element_t<Size >= 4 ? 3 : 0,
UnitTuple>> val)
492 typename Mat::StorageType d;
493 d << 0, -vec.data[2], vec.data[1], vec.data[2], 0, -vec.data[0], -vec.data[1], vec.data[0], 0;
494 return Mat(std::move(d));
498 template <
typename ScalarTag>
503 return UnitVector<UnitTags...>(data.array() + scalar.value());
507 template <
typename ScalarTag>
512 return UnitVector<UnitTags...>(data.array() - scalar.value());
516 template <
typename ScalarTag>
520 data.array() += scalar.value();
525 template <
typename ScalarTag>
529 data.array() -= scalar.value();
537 template <
int P,
typename T = std::tuple_element_t<0, UnitTuple>>
541 return units::unit_t<T>(data.template lpNorm<P>());
545 template <
typename T = std::tuple_element_t<0, UnitTuple>>
549 return units::unit_t<T>(data.stableNorm());
552 constexpr auto Begin() {
return data.begin(); }
554 constexpr auto End() {
return data.end(); }
556 constexpr auto Begin()
const {
return data.begin(); }
558 constexpr auto End()
const {
return data.end(); }
561 template <std::size_t Offset, std::size_t... Is>
562 auto slice_impl(std::index_sequence<Is...>)
const {
566 template <std::size_t... Is>
567 void print_impl(std::ostream& os, std::index_sequence<Is...>)
const {
568 ((os << units::unit_t<std::tuple_element_t<Is, UnitTuple>>(data[Is]).value() << (Is ==
Size - 1 ?
"" :
", ")), ...);
579template <
typename T, std::
size_t N>
585using UnitQuaternion = VectorN<units::dimensionless::scalar, 4>;
590template <
typename... T>
591 requires(
requires {
typename T::unit_type; } && ...)
597template <std::
size_t I, typename... UnitTags>
598constexpr auto Get(const
UnitVector<UnitTags...>& vec) {
599 return vec.template Get<I>();
611template <
typename... TagsA,
typename... TagsB>
616 using T1 = std::tuple_element_t<0, std::tuple<TagsA...>>;
617 using T2 = std::tuple_element_t<0, std::tuple<TagsB...>>;
618 using ResultUnit = MultiplyResult<T1, T2>;
631template <
typename ScalarTag,
typename... UnitTags>
644template <
typename... UnitTags>
653template <
typename ScalarTag,
typename... UnitTags>
665template <
typename ScalarTag,
typename... UnitTags>
670 return UnitVector<UnitTags...>(scalar.value() - vec.Raw().array());
673template <
typename Derived,
typename Base>
674struct NamedPhysicalVector : Base {
677 NamedPhysicalVector(
const Base& b) : Base(b.Raw()) {}
686template <
typename... UnitTags>
687struct std::tuple_size<valor::EigenUnit::UnitVector<UnitTags...>> : std::integral_constant<std::size_t, sizeof...(UnitTags)> {};
692template <std::size_t N,
typename... UnitTags>
693struct std::tuple_element<N, valor::EigenUnit::UnitVector<UnitTags...>> {
A matrix wrapper representing a linear map between two UnitVectors.
Definition UnitMatrix.h:22
A vector wrapper that enforces units for each element.
Definition UnitVector.h:21
auto Norm() const
Computes the L2 norm (magnitude). Requires all elements to have the same unit tag.
Definition UnitVector.h:142
constexpr UnitVector operator-() const
Unary negation.
Definition UnitVector.h:227
auto Mean() const
Computes the mean of all elements. Only valid if all units are the same.
Definition UnitVector.h:418
auto Sum() const
Computes the sum of all elements. Only valid if all units are the same.
Definition UnitVector.h:410
constexpr UnitVector(units::unit_t< UnitTags >... args)
Construct from individual unit values. Values are automatically converted to the units specified by U...
Definition UnitVector.h:61
constexpr UnitVector operator-(const units::unit_t< ScalarTag > &scalar) const
Vector - scalar subtraction (broadcasting).
Definition UnitVector.h:509
constexpr auto Get() const
Get the value of the N-th element with its unit.
Definition UnitVector.h:117
constexpr UnitVector & operator+=(const units::unit_t< ScalarTag > &scalar)
In-place vector + scalar addition (broadcasting).
Definition UnitVector.h:517
auto AsDiagonal() const
Converts the vector to a diagonal matrix.
Definition UnitMatrix.h:614
bool IsApprox(const UnitVector &other, units::unit_t< std::tuple_element_t< 0, UnitTuple > > tol=units::unit_t< std::tuple_element_t< 0, UnitTuple > >{1e-9}) const
Check if two vectors are approximately equal.
Definition UnitVector.h:387
constexpr UnitVector(const UnitVector &)=default
Copy constructor.
constexpr UnitVector()
Default constructor. Initializes all elements to zero.
Definition UnitVector.h:33
constexpr auto W() const
Access the fourth element (w).
Definition UnitVector.h:465
constexpr UnitVector & operator=(const UnitVector &)=default
Copy assignment.
constexpr auto Head() const
Extract the first N elements as a new UnitVector.
Definition UnitVector.h:181
constexpr UnitVector(StorageType &&d)
Construct from a raw Eigen matrix (move).
Definition UnitVector.h:45
constexpr auto CWiseQuotient(const UnitVector< OtherTags... > &other) const
Component-wise quotient with another UnitVector.
Definition UnitVector.h:334
constexpr auto operator*(const units::unit_t< ScalarTag > &scalar) const
Multiplication by a scalar unit.
Definition UnitVector.h:255
void W(units::unit_t< std::tuple_element_t< Size >=4 ? 3 :0, UnitTuple > > val)
Set the fourth element (w).
Definition UnitVector.h:472
constexpr UnitVector & operator+=(const UnitVector< OtherTags... > &other)
In-place vector addition.
Definition UnitVector.h:231
constexpr UnitVector & operator-=(const units::unit_t< ScalarTag > &scalar)
In-place vector - scalar subtraction (broadcasting).
Definition UnitVector.h:526
auto StableNorm() const
Computes the stable norm.
Definition UnitVector.h:548
constexpr UnitVector(UnitVector &&)=default
Move constructor.
static constexpr std::size_t Size
Definition UnitVector.h:24
constexpr UnitVector operator*(double scalar) const
Raw scaling (multiplication by double).
Definition UnitVector.h:279
constexpr auto Slice() const
Extract N elements after the Start as a new UnitVector.
Definition UnitVector.h:204
constexpr auto Tail() const
Extract the last N elements as a new UnitVector.
Definition UnitVector.h:192
auto Normalized() const
Returns a normalized (unit-length) vector. Result is dimensionless. Only valid if all elements have t...
Definition UnitVector.h:151
constexpr UnitVector(const StorageType &d)
Construct from a raw Eigen matrix.
Definition UnitVector.h:39
constexpr UnitVector CWiseAbs() const
Component-wise absolute value.
Definition UnitVector.h:340
constexpr UnitVector operator-(const UnitVector< OtherTags... > &other) const
Vector subtraction.
Definition UnitVector.h:220
auto Dot(const UnitVector< OtherTags... > &other) const
Dot product with another UnitVector. Resulting unit is the product of the first tags of both vectors.
Definition UnitVector.h:167
constexpr UnitVector & operator-=(const UnitVector< OtherTags... > &other)
In-place vector subtraction.
Definition UnitVector.h:240
bool operator==(const UnitVector< OtherTags... > &other) const
Equality operator.
Definition UnitVector.h:308
void Z(units::unit_t< std::tuple_element_t< Size >=3 ? 2 :0, UnitTuple > > val)
Set the third element (z).
Definition UnitVector.h:458
auto LPNorm() const
Computes the Lp norm.
Definition UnitVector.h:540
friend std::ostream & operator<<(std::ostream &os, const UnitVector &vec)
Debug printing support.
Definition UnitVector.h:299
constexpr UnitVector CWiseMax(const UnitVector &other) const
Component-wise maximum.
Definition UnitVector.h:352
void X(units::unit_t< std::tuple_element_t< Size >=1 ? 0 :0, UnitTuple > > val)
Set the first element (x).
Definition UnitVector.h:430
constexpr auto Y() const
Access the second element (y).
Definition UnitVector.h:437
constexpr StorageType & Raw()
Get a reference to the underlying raw Eigen data.
Definition UnitVector.h:108
static constexpr UnitVector Zero()
Create a zero-initialized vector.
Definition UnitVector.h:70
constexpr const StorageType & Raw() const
Get a constant reference to the underlying raw Eigen data.
Definition UnitVector.h:102
constexpr UnitVector operator/(double scalar) const
Raw scaling (division by double).
Definition UnitVector.h:289
static UnitVector FromArray(const std::array< double, Size > &arr)
Construct a UnitVector from a std::array of doubles. Values are assumed to be in the target units.
Definition UnitVector.h:82
constexpr UnitVector CWiseMin(const UnitVector &other) const
Component-wise minimum.
Definition UnitVector.h:346
void Set(units::unit_t< std::tuple_element_t< N, UnitTuple > > val)
Set the value of the N-th element.
Definition UnitVector.h:129
Eigen::Matrix< double, Size, 1 > StorageType
Definition UnitVector.h:26
constexpr UnitVector operator+(const units::unit_t< ScalarTag > &scalar) const
Vector + scalar addition (broadcasting).
Definition UnitVector.h:500
constexpr auto operator/(const units::unit_t< ScalarTag > &scalar) const
Division by a scalar unit.
Definition UnitVector.h:268
constexpr UnitVector operator+(const UnitVector< OtherTags... > &other) const
Vector addition.
Definition UnitVector.h:211
constexpr UnitVector Clamp(units::unit_t< std::tuple_element_t< 0, UnitTuple > > lo, units::unit_t< std::tuple_element_t< 0, UnitTuple > > hi) const
Clamp each element between lo and hi unit values.
Definition UnitVector.h:374
constexpr UnitVector & operator=(UnitVector &&)=default
Move assignment.
static auto Skew(const UnitVector< UnitTags... > &vec)
Converts a 3D vector to a skew-symmetric matrix.
Definition UnitVector.h:488
static UnitVector FromSpan(const std::span< double, Size > &s)
Construct a UnitVector from a std::span of doubles. Values are assumed to be in the target units.
Definition UnitVector.h:93
constexpr auto X() const
Access the first element (x).
Definition UnitVector.h:423
std::tuple< UnitTags... > UnitTuple
Definition UnitVector.h:28
constexpr UnitVector Clamp(const UnitVector &lo, const UnitVector &hi) const
Clamp each element between lo and hi vectors.
Definition UnitVector.h:362
constexpr auto CWiseProduct(const UnitVector< OtherTags... > &other) const
Component-wise product with another UnitVector.
Definition UnitVector.h:322
static UnitVector Constant(units::unit_t< typename std::tuple_element_t< 0, UnitTuple > > val)
Create a vector where all elements are set to a constant value. Only valid if all units are the same.
Definition UnitVector.h:400
constexpr auto Z() const
Access the third element (z).
Definition UnitVector.h:451
void Y(units::unit_t< std::tuple_element_t< Size >=2 ? 1 :0, UnitTuple > > val)
Set the second element (y).
Definition UnitVector.h:444
Concept verifying that all units in a pack are identical.
Definition Common.h:204
Concept verifying that a pack contains exactly three elements.
Definition Common.h:210
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
Concept verifying that size N is within the bounds of a pack.
Definition Common.h:198
Template specializations for std::tuple_size and std::tuple_element to support UnitVector.
Definition Formatters.h:8
Builder for N-dimensional UnitVectors with uniform units.
Definition Common.h:126