wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Loading...
Searching...
No Matches
Unit.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <ratio>
4#include <type_traits>
5
6#include <mp-units/framework.h>
7#include <mp-units/systems/si.h>
8
9#include "wuzp/core/Unit.hpp"
10#include "wuzp/core/traits.hpp"
11
12namespace wuzp_mp_units {
13namespace internal {
14
15template <typename... RatiosT_>
16struct RatioSum {
17 using type = std::ratio<0>;
18};
19template <typename RatioT_, typename... RatiosT_>
20struct RatioSum<RatioT_, RatiosT_...> {
21 using type = std::ratio_add<RatioT_, typename RatioSum<RatiosT_...>::type>;
22};
23
24using Gram = std::remove_cvref_t<decltype(mp_units::si::gram)>;
25
27template <typename UnitT_>
28struct GramExponent {
29 using type = std::ratio<0>;
30};
31template <>
32struct GramExponent<Gram> {
33 using type = std::ratio<1>;
34};
35template <int NumeratorV_, int... DenominatorV_>
36struct GramExponent<mp_units::power<Gram, NumeratorV_, DenominatorV_...>> {
37 using type = std::ratio<NumeratorV_, DenominatorV_...>;
38};
39template <typename FactorT_, typename... FactorsT_>
40struct GramExponent<mp_units::per<FactorT_, FactorsT_...>> {
41 using type =
42 std::ratio_subtract<std::ratio<0>,
43 typename RatioSum<typename GramExponent<FactorT_>::type, typename GramExponent<FactorsT_>::type...>::type>;
44};
45template <typename... FactorsT_>
46struct GramExponent<mp_units::derived_unit<FactorsT_...>> {
47 using type = typename RatioSum<typename GramExponent<FactorsT_>::type...>::type;
48};
49
51template <typename ReferenceT_>
52consteval auto coherentUnit(ReferenceT_ reference) {
53 using GramExponentT = typename GramExponent<ReferenceT_>::type;
54 // mp_units::pow only takes positive exponents for named units, so negative ones divide (std::ratio keeps den positive).
55 constexpr auto num = GramExponentT::num > 0 ? GramExponentT::num : -GramExponentT::num;
56 if constexpr (GramExponentT::num > 0) {
57 return mp_units::pow<num, GramExponentT::den>(mp_units::si::kilogram) *
58 (reference / mp_units::pow<num, GramExponentT::den>(mp_units::si::gram));
59 } else if constexpr (GramExponentT::num < 0) {
60 return reference * mp_units::pow<num, GramExponentT::den>(mp_units::si::gram) /
61 mp_units::pow<num, GramExponentT::den>(mp_units::si::kilogram);
62 } else {
63 return reference;
64 }
65}
66
79template <typename UnitT_>
80struct Normalize {
81 private:
82 static constexpr auto canonical = mp_units::get_canonical_unit(UnitT_{});
83 static constexpr auto coherent = coherentUnit(canonical.reference_unit);
84 static_assert(mp_units::get_canonical_unit(coherent).mag == canonical.mag,
85 "Only coherent SI units are supported, e.g. m, kg and rad but not km, g or deg: wuzp does not convert units.");
86
87 public:
88 using type = std::remove_cvref_t<decltype(coherent)>;
89};
90
91} // namespace internal
92
93// The type of an mp-units unit object, e.g. UnitType<mp_units::si::metre / mp_units::si::second> or
94// UnitType<mp_units::si::newton>, normalized to the coherent SI unit built from the base units (see internal::Normalize), so
95// that equal units have the same type. Units that are not coherent SI units, e.g. km, are rejected.
96template <auto UnitV_>
97using UnitType = typename internal::Normalize<std::remove_cvref_t<decltype(UnitV_)>>::type;
98
99// Angles are dimensionless in every units backend (see "Angles are dimensionless" in DESIGN.md), so they are the unit one
100// rather than mp-units' radian, and angular velocity is 1 / s.
101using Acceleration = wuzp::Unit<UnitType<mp_units::si::metre / mp_units::square(mp_units::si::second)>>;
103using AngularAcceleration = wuzp::Unit<UnitType<mp_units::one / mp_units::square(mp_units::si::second)>>;
104using AngularVelocity = wuzp::Unit<UnitType<mp_units::one / mp_units::si::second>>;
105using Area = wuzp::Unit<UnitType<mp_units::square(mp_units::si::metre)>>;
107using Energy =
108 wuzp::Unit<UnitType<mp_units::si::kilogram * mp_units::square(mp_units::si::metre) / mp_units::square(mp_units::si::second)>>;
109using Force = wuzp::Unit<UnitType<mp_units::si::kilogram * mp_units::si::metre / mp_units::square(mp_units::si::second)>>;
110using Frequency = wuzp::Unit<UnitType<mp_units::one / mp_units::si::second>>;
114// Torque is N m, the unit of energy, so that the cross product of a length and a force is a torque.
115using Torque =
116 wuzp::Unit<UnitType<mp_units::si::kilogram * mp_units::square(mp_units::si::metre) / mp_units::square(mp_units::si::second)>>;
117using Velocity = wuzp::Unit<UnitType<mp_units::si::metre / mp_units::si::second>>;
118using Volume = wuzp::Unit<UnitType<mp_units::cubic(mp_units::si::metre)>>;
119
120} // namespace wuzp_mp_units
121
122namespace wuzp {
123
124// The results of unit arithmetic are normalized like the units themselves, see wuzp_mp_units::UnitType.
125template <mp_units::Unit UnitLhsT_, mp_units::Unit UnitRhsT_>
126struct Product<Unit<UnitLhsT_>, Unit<UnitRhsT_>> {
127 using type = Unit<wuzp_mp_units::UnitType<UnitLhsT_{} * UnitRhsT_{}>>;
128};
129
130template <mp_units::Unit UnitLhsT_, mp_units::Unit UnitRhsT_>
131struct Division<Unit<UnitLhsT_>, Unit<UnitRhsT_>> {
132 using type = Unit<wuzp_mp_units::UnitType<UnitLhsT_{} / UnitRhsT_{}>>;
133};
134
135template <mp_units::Unit UnitT_, typename ExponentT_>
139
140template <mp_units::Unit UnitT_>
141struct Identity<Unit<UnitT_>> {
143};
144
145template <mp_units::Unit UnitT_>
146struct Inverse<Unit<UnitT_>> {
147 using type = Unit<wuzp_mp_units::UnitType<mp_units::one / UnitT_{}>>;
148};
149
150template <typename ValueT_>
151struct AccessUnits<AccessImplementation<ValueT_>> {
167};
168
169} // namespace wuzp
wuzp::Unit< UnitType< mp_units::cubic(mp_units::si::metre)> > Volume
Definition Unit.hpp:118
wuzp::Unit< UnitType< mp_units::si::kilogram *mp_units::square(mp_units::si::metre)/mp_units::square(mp_units::si::second)> > Torque
Definition Unit.hpp:115
typename internal::Normalize< std::remove_cvref_t< decltype(UnitV_)> >::type UnitType
Definition Unit.hpp:97
wuzp::Unit< UnitType< mp_units::si::kilogram *mp_units::si::metre/mp_units::square(mp_units::si::second)> > Force
Definition Unit.hpp:109
wuzp::Unit< UnitType< mp_units::si::metre/mp_units::si::second > > Velocity
Definition Unit.hpp:117
wuzp::Unit< UnitType< mp_units::si::metre > > Length
Definition Unit.hpp:111
wuzp::Unit< UnitType< mp_units::si::kilogram *mp_units::square(mp_units::si::metre)/mp_units::square(mp_units::si::second)> > Energy
Definition Unit.hpp:107
wuzp::Unit< UnitType< mp_units::one/mp_units::si::second > > AngularVelocity
Definition Unit.hpp:104
wuzp::Unit< UnitType< mp_units::one > > Angle
Definition Unit.hpp:102
wuzp::Unit< UnitType< mp_units::one > > Dimensionless
Definition Unit.hpp:106
wuzp::Unit< UnitType< mp_units::si::second > > Time
Definition Unit.hpp:113
wuzp::Unit< UnitType< mp_units::one/mp_units::si::second > > Frequency
Definition Unit.hpp:110
wuzp::Unit< UnitType< mp_units::one/mp_units::square(mp_units::si::second)> > AngularAcceleration
Definition Unit.hpp:103
wuzp::Unit< UnitType< mp_units::square(mp_units::si::metre)> > Area
Definition Unit.hpp:105
wuzp::Unit< UnitType< mp_units::si::kilogram > > Mass
Definition Unit.hpp:112
wuzp::Unit< UnitType< mp_units::si::metre/mp_units::square(mp_units::si::second)> > Acceleration
Definition Unit.hpp:101
wuzp_units::AngularAcceleration AngularAcceleration
Definition Unit.hpp:70
wuzp_units::AngularVelocity AngularVelocity
Definition Unit.hpp:71
Unit< wuzp_mp_units::UnitType< UnitLhsT_{}/UnitRhsT_{}> > type
Definition Unit.hpp:132
wuzp_mp_units::Dimensionless type
Definition Unit.hpp:142
Unit< wuzp_mp_units::UnitType< mp_units::one/UnitT_{}> > type
Definition Unit.hpp:147
The inverse: the matrix inverse of a matrix, like Eigen's MatrixBase::inverse(), and the reciprocal o...
Definition traits.hpp:188
Unit< wuzp_mp_units::UnitType< mp_units::pow< ExponentT_::Dividend, ExponentT_::Divisor >(UnitT_{})> > type
Definition Unit.hpp:137
Unit< wuzp_mp_units::UnitType< UnitLhsT_{} *UnitRhsT_{}> > type
Definition Unit.hpp:127