8#include "valkyrie/util/eigenunit/Common.h"
21template <
typename... UnitTags>
25 static constexpr std::size_t
Size =
sizeof...(UnitTags);
64 ((data[i++] = args.value()), ...);
84 StorageType d = Eigen::Map<const StorageType>(arr.data());
95 StorageType d = Eigen::Map<const StorageType>(s.data());
116 template <std::
size_t N>
118 constexpr auto Get()
const {
119 using T = std::tuple_element_t<N, UnitTuple>;
120 return units::unit_t<T>(data[N]);
128 template <std::
size_t N>
130 void Set(units::unit_t<std::tuple_element_t<N, UnitTuple>> val) {
131 data[N] = val.value();
140 template <
typename T = std::tuple_element_t<0, UnitTuple>>
144 return units::unit_t<T>(data.norm());
155 return data.normalized();
165 template <
typename... OtherTags>
166 requires(
sizeof...(UnitTags) ==
sizeof...(OtherTags)) &&
AllSameUnits<MultiplyResult<UnitTags, OtherTags>...>
169 using T1 = std::tuple_element_t<0, UnitTuple>;
170 using T2 = std::tuple_element_t<0, std::tuple<OtherTags...>>;
171 using ResultUnit = MultiplyResult<T1, T2>;
172 return units::unit_t<ResultUnit>(data.dot(other.
Raw()));
180 template <std::
size_t N>
183 return slice_impl<0>(std::make_index_sequence<N>{});
191 template <std::
size_t N>
194 return slice_impl<
Size - N>(std::make_index_sequence<N>{});
203 template <std::
size_t Start, std::
size_t N>
204 requires ValidSize<Start + N, UnitTags...>
206 return slice_impl<Start>(std::make_index_sequence<N>{});
210 template <
typename... OtherTags>
219 template <
typename... OtherTags>
231 template <
typename... OtherTags>
240 template <
typename... OtherTags>
254 template <
typename ScalarTag>
256 constexpr auto operator*(
const units::unit_t<ScalarTag>& scalar)
const {
258 return ResultType(data * scalar.value());
267 template <
typename ScalarTag>
269 constexpr auto operator/(
const units::unit_t<ScalarTag>& scalar)
const {
271 return ResultType(data / scalar.value());
302 vec.print_impl(os, std::make_index_sequence<Size>{});
308 template <
typename... OtherTags>
312 return data == other.
Raw();
321 template <
typename... OtherTags>
333 template <
typename... OtherTags>
366 return UnitVector(data.CWiseMax(lo.Raw()).CWiseMin(hi.Raw()));
376 units::unit_t<std::tuple_element_t<0, UnitTuple>> hi)
const
379 return UnitVector(data.cwiseMax(lo.value()).cwiseMin(hi.value()));
389 units::unit_t<std::tuple_element_t<0, UnitTuple>> tol = units::unit_t<std::tuple_element_t<0, UnitTuple>>{1e-9})
const
392 return data.isApprox(other.
Raw(), tol.value());
404 return UnitVector(StorageType::Constant(val.value()));
408 template <
typename T = std::tuple_element_t<0, UnitTuple>>
412 return units::unit_t<T>(data.sum());
416 template <
typename T = std::tuple_element_t<0, UnitTuple>>
420 return units::unit_t<T>(data.mean());
424 constexpr auto X() const
431 void X(units::unit_t<std::tuple_element_t<Size >= 1 ? 0 : 0,
UnitTuple>> val)
438 constexpr auto Y() const
445 void Y(units::unit_t<std::tuple_element_t<Size >= 2 ? 1 : 0,
UnitTuple>> val)
452 constexpr auto Z() const
459 void Z(units::unit_t<std::tuple_element_t<Size >= 3 ? 2 : 0,
UnitTuple>> val)
466 constexpr auto W() const
473 void W(units::unit_t<std::tuple_element_t<Size >= 4 ? 3 : 0,
UnitTuple>> val)
493 typename Mat::StorageType d;
494 d << 0, -vec.data[2], vec.data[1], vec.data[2], 0, -vec.data[0], -vec.data[1], vec.data[0], 0;
495 return Mat(std::move(d));
499 template <
typename ScalarTag>
504 return UnitVector<UnitTags...>(data.array() + scalar.value());
508 template <
typename ScalarTag>
513 return UnitVector<UnitTags...>(data.array() - scalar.value());
517 template <
typename ScalarTag>
521 data.array() += scalar.value();
526 template <
typename ScalarTag>
530 data.array() -= scalar.value();
538 template <
int P,
typename T = std::tuple_element_t<0, UnitTuple>>
542 return units::unit_t<T>(data.template lpNorm<P>());
546 template <
typename T = std::tuple_element_t<0, UnitTuple>>
550 return units::unit_t<T>(data.stableNorm());
553 constexpr auto Begin() {
return data.begin(); }
555 constexpr auto End() {
return data.end(); }
557 constexpr auto Begin()
const {
return data.begin(); }
559 constexpr auto End()
const {
return data.end(); }
562 template <std::size_t Offset, std::size_t... Is>
563 auto slice_impl(std::index_sequence<Is...>)
const {
567 template <std::size_t... Is>
568 void print_impl(std::ostream& os, std::index_sequence<Is...>)
const {
569 ((os << units::unit_t<std::tuple_element_t<Is, UnitTuple>>(data[Is]).value() << (Is ==
Size - 1 ?
"" :
", ")), ...);
580template <
typename T, std::
size_t N>
586using UnitQuaternion = VectorN<units::dimensionless::scalar, 4>;
591template <
typename... T>
592 requires(
requires {
typename T::unit_type; } && ...)
598template <std::
size_t I, typename... UnitTags>
599constexpr auto Get(const
UnitVector<UnitTags...>& vec) {
600 return vec.template Get<I>();
612template <
typename... TagsA,
typename... TagsB>
617 using T1 = std::tuple_element_t<0, std::tuple<TagsA...>>;
618 using T2 = std::tuple_element_t<0, std::tuple<TagsB...>>;
619 using ResultUnit = MultiplyResult<T1, T2>;
632template <
typename ScalarTag,
typename... UnitTags>
645template <
typename... UnitTags>
654template <
typename ScalarTag,
typename... UnitTags>
666template <
typename ScalarTag,
typename... UnitTags>
671 return UnitVector<UnitTags...>(scalar.value() - vec.Raw().array());
674template <
typename Derived,
typename Base>
675struct NamedPhysicalVector : Base {
678 NamedPhysicalVector(
const Base& b) : Base(b.Raw()) {}
687template <
typename... UnitTags>
688struct std::tuple_size<valor::EigenUnit::UnitVector<UnitTags...>> : std::integral_constant<std::size_t, sizeof...(UnitTags)> {};
693template <std::size_t N,
typename... UnitTags>
694struct std::tuple_element<N, valor::EigenUnit::UnitVector<UnitTags...>> {
A matrix wrapper representing a linear map between two UnitVectors.
Definition UnitMatrix.h:23
A vector wrapper that enforces units for each element.
Definition UnitVector.h:22
auto Norm() const
Computes the L2 norm (magnitude). Requires all elements to have the same unit tag.
Definition UnitVector.h:143
constexpr UnitVector operator-() const
Unary negation.
Definition UnitVector.h:228
auto Mean() const
Computes the mean of all elements. Only valid if all units are the same.
Definition UnitVector.h:419
auto Sum() const
Computes the sum of all elements. Only valid if all units are the same.
Definition UnitVector.h:411
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:62
constexpr UnitVector operator-(const units::unit_t< ScalarTag > &scalar) const
Vector - scalar subtraction (broadcasting).
Definition UnitVector.h:510
constexpr auto Get() const
Get the value of the N-th element with its unit.
Definition UnitVector.h:118
constexpr UnitVector & operator+=(const units::unit_t< ScalarTag > &scalar)
In-place vector + scalar addition (broadcasting).
Definition UnitVector.h:518
auto AsDiagonal() const
Converts the vector to a diagonal matrix.
Definition UnitMatrix.h:615
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:388
constexpr UnitVector(const UnitVector &)=default
Copy constructor.
constexpr UnitVector()
Default constructor. Initializes all elements to zero.
Definition UnitVector.h:34
constexpr auto W() const
Access the fourth element (w).
Definition UnitVector.h:466
constexpr UnitVector & operator=(const UnitVector &)=default
Copy assignment.
constexpr auto Head() const
Extract the first N elements as a new UnitVector.
Definition UnitVector.h:182
constexpr UnitVector(StorageType &&d)
Construct from a raw Eigen matrix (move).
Definition UnitVector.h:46
constexpr auto CWiseQuotient(const UnitVector< OtherTags... > &other) const
Component-wise quotient with another UnitVector.
Definition UnitVector.h:335
constexpr auto operator*(const units::unit_t< ScalarTag > &scalar) const
Multiplication by a scalar unit.
Definition UnitVector.h:256
void W(units::unit_t< std::tuple_element_t< Size >=4 ? 3 :0, UnitTuple > > val)
Set the fourth element (w).
Definition UnitVector.h:473
constexpr UnitVector & operator+=(const UnitVector< OtherTags... > &other)
In-place vector addition.
Definition UnitVector.h:232
constexpr UnitVector & operator-=(const units::unit_t< ScalarTag > &scalar)
In-place vector - scalar subtraction (broadcasting).
Definition UnitVector.h:527
auto StableNorm() const
Computes the stable norm.
Definition UnitVector.h:549
constexpr UnitVector(UnitVector &&)=default
Move constructor.
static constexpr std::size_t Size
Definition UnitVector.h:25
constexpr UnitVector operator*(double scalar) const
Raw scaling (multiplication by double).
Definition UnitVector.h:280
constexpr auto Slice() const
Extract N elements after the Start as a new UnitVector.
Definition UnitVector.h:205
constexpr auto Tail() const
Extract the last N elements as a new UnitVector.
Definition UnitVector.h:193
auto Normalized() const
Returns a normalized (unit-length) vector. Result is dimensionless. Only valid if all elements have t...
Definition UnitVector.h:152
constexpr UnitVector(const StorageType &d)
Construct from a raw Eigen matrix.
Definition UnitVector.h:40
constexpr UnitVector CWiseAbs() const
Component-wise absolute value.
Definition UnitVector.h:341
constexpr UnitVector operator-(const UnitVector< OtherTags... > &other) const
Vector subtraction.
Definition UnitVector.h:221
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:168
constexpr UnitVector & operator-=(const UnitVector< OtherTags... > &other)
In-place vector subtraction.
Definition UnitVector.h:241
bool operator==(const UnitVector< OtherTags... > &other) const
Equality operator.
Definition UnitVector.h:309
void Z(units::unit_t< std::tuple_element_t< Size >=3 ? 2 :0, UnitTuple > > val)
Set the third element (z).
Definition UnitVector.h:459
auto LPNorm() const
Computes the Lp norm.
Definition UnitVector.h:541
friend std::ostream & operator<<(std::ostream &os, const UnitVector &vec)
Debug printing support.
Definition UnitVector.h:300
constexpr UnitVector CWiseMax(const UnitVector &other) const
Component-wise maximum.
Definition UnitVector.h:353
void X(units::unit_t< std::tuple_element_t< Size >=1 ? 0 :0, UnitTuple > > val)
Set the first element (x).
Definition UnitVector.h:431
constexpr auto Y() const
Access the second element (y).
Definition UnitVector.h:438
constexpr StorageType & Raw()
Get a reference to the underlying raw Eigen data.
Definition UnitVector.h:109
static constexpr UnitVector Zero()
Create a zero-initialized vector.
Definition UnitVector.h:71
constexpr const StorageType & Raw() const
Get a constant reference to the underlying raw Eigen data.
Definition UnitVector.h:103
constexpr UnitVector operator/(double scalar) const
Raw scaling (division by double).
Definition UnitVector.h:290
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:83
constexpr UnitVector CWiseMin(const UnitVector &other) const
Component-wise minimum.
Definition UnitVector.h:347
void Set(units::unit_t< std::tuple_element_t< N, UnitTuple > > val)
Set the value of the N-th element.
Definition UnitVector.h:130
Eigen::Matrix< double, Size, 1 > StorageType
Definition UnitVector.h:27
constexpr UnitVector operator+(const units::unit_t< ScalarTag > &scalar) const
Vector + scalar addition (broadcasting).
Definition UnitVector.h:501
constexpr auto operator/(const units::unit_t< ScalarTag > &scalar) const
Division by a scalar unit.
Definition UnitVector.h:269
constexpr UnitVector operator+(const UnitVector< OtherTags... > &other) const
Vector addition.
Definition UnitVector.h:212
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:375
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:489
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:94
constexpr auto X() const
Access the first element (x).
Definition UnitVector.h:424
std::tuple< UnitTags... > UnitTuple
Definition UnitVector.h:29
constexpr UnitVector Clamp(const UnitVector &lo, const UnitVector &hi) const
Clamp each element between lo and hi vectors.
Definition UnitVector.h:363
constexpr auto CWiseProduct(const UnitVector< OtherTags... > &other) const
Component-wise product with another UnitVector.
Definition UnitVector.h:323
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:401
constexpr auto Z() const
Access the third element (z).
Definition UnitVector.h:452
void Y(units::unit_t< std::tuple_element_t< Size >=2 ? 1 :0, UnitTuple > > val)
Set the second element (y).
Definition UnitVector.h:445
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