Skip to content

Commit

Permalink
refactor: quantity_point::quantity_from_origin() refactored to `qua…
Browse files Browse the repository at this point in the history
…ntity_point::quantity_ref_from(PO)`

Resolves #479 and relates to #477
  • Loading branch information
mpusz committed Sep 11, 2023
1 parent d801027 commit 493cc3c
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 150 deletions.
4 changes: 2 additions & 2 deletions example/currency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ int main()
quantity_point price_usd = zero + 100 * us_dollar;
quantity_point price_euro = exchange_to<euro>(price_usd);

std::cout << price_usd.quantity_from_origin() << " -> " << price_euro.quantity_from_origin() << "\n";
// std::cout << price_usd.quantity_from_origin() + price_euro.quantity_from_origin() << "\n"; // does not compile
std::cout << price_usd.quantity_ref_from(zero) << " -> " << price_euro.quantity_ref_from(zero) << "\n";
// std::cout << price_usd.quantity_ref_from(zero) + price_euro.quantity_ref_from(zero) << "\n"; // does not compile
}
16 changes: 9 additions & 7 deletions example/include/geographic.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct MP_UNITS_STD_FMT::formatter<geographic::latitude<T>> :
auto format(geographic::latitude<T> lat, FormatContext& ctx)
{
formatter<typename geographic::latitude<T>::quantity_type>::format(
lat > geographic::latitude<T>::zero() ? lat.quantity_from_origin() : -lat.quantity_from_origin(), ctx);
lat > geographic::latitude<T>::zero() ? lat.quantity_ref_from(geographic::equator) : -lat.quantity_ref_from(geographic::equator), ctx);
MP_UNITS_STD_FMT::format_to(ctx.out(), "{}", lat > geographic::latitude<T>::zero() ? " N" : "S");
return ctx.out();
}
Expand All @@ -149,8 +149,10 @@ struct MP_UNITS_STD_FMT::formatter<geographic::longitude<T>> :
template<typename FormatContext>
auto format(geographic::longitude<T> lon, FormatContext& ctx)
{
formatter<typename geographic::longitude<T>::quantity_type>::format(
lon > geographic::longitude<T>::zero() ? lon.quantity_from_origin() : -lon.quantity_from_origin(), ctx);
formatter<typename geographic::longitude<T>::quantity_type>::format(lon > geographic::longitude<T>::zero()
? lon.quantity_ref_from(geographic::prime_meridian)
: -lon.quantity_ref_from(geographic::prime_meridian),
ctx);
MP_UNITS_STD_FMT::format_to(ctx.out(), "{}", lon > geographic::longitude<T>::zero() ? " E" : " W");
return ctx.out();
}
Expand All @@ -174,10 +176,10 @@ distance spherical_distance(position<T> from, position<T> to)

using isq::sin, isq::cos, isq::asin, isq::acos;

const auto& from_lat = from.lat.quantity_from_origin();
const auto& from_lon = from.lon.quantity_from_origin();
const auto& to_lat = to.lat.quantity_from_origin();
const auto& to_lon = to.lon.quantity_from_origin();
const auto& from_lat = from.lat.quantity_ref_from(equator);
const auto& from_lon = from.lon.quantity_ref_from(prime_meridian);
const auto& to_lat = to.lat.quantity_ref_from(equator);
const auto& to_lon = to.lon.quantity_ref_from(prime_meridian);

// https://en.wikipedia.org/wiki/Great-circle_distance#Formulae
if constexpr (sizeof(T) >= 8) {
Expand Down
2 changes: 1 addition & 1 deletion example/kalman_filter/kalman.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ struct MP_UNITS_STD_FMT::formatter<kalman::estimation<Q>> {
if constexpr (mp_units::Quantity<Q>)
return t;
else
return t.quantity_from_origin();
return t.quantity_ref_from(t.point_origin);
}(kalman::get<0>(e.state));

std::string value_buffer;
Expand Down
2 changes: 1 addition & 1 deletion example/kalman_filter/kalman_filter-example_6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ template<QuantityPoint QP, QuantityOf<dimensionless> K>
void print(auto iteration, K gain, QP measured, kalman::estimation<QP> current, kalman::estimation<QP> next)
{
std::cout << MP_UNITS_STD_FMT::format("{:2} | {:7%.4Q} | {:10%.3Q %q} | {:>18.3} | {:>18.3}\n", iteration, gain,
measured.quantity_from_origin(), current, next);
measured.quantity_ref_from(QP::point_origin), current, next);
}

int main()
Expand Down
2 changes: 1 addition & 1 deletion example/kalman_filter/kalman_filter-example_7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ template<QuantityPoint QP, QuantityOf<dimensionless> K>
void print(auto iteration, K gain, QP measured, kalman::estimation<QP> current, kalman::estimation<QP> next)
{
std::cout << MP_UNITS_STD_FMT::format("{:2} | {:7%.4Q} | {:10%.3Q %q} | {:>18.3} | {:>18.3}\n", iteration, gain,
measured.quantity_from_origin(), current, next);
measured.quantity_ref_from(QP::point_origin), current, next);
}

int main()
Expand Down
2 changes: 1 addition & 1 deletion example/kalman_filter/kalman_filter-example_8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ template<QuantityPoint QP, QuantityOf<dimensionless> K>
void print(auto iteration, K gain, QP measured, kalman::estimation<QP> current, kalman::estimation<QP> next)
{
std::cout << MP_UNITS_STD_FMT::format("{:2} | {:7%.3Q} | {:10%.3Q %q} | {:>16.2} | {:>16.2}\n", iteration, gain,
measured.quantity_from_origin(), current, next);
measured.quantity_ref_from(QP::point_origin), current, next);
}

int main()
Expand Down
8 changes: 4 additions & 4 deletions example/unmanned_aerial_vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ template<earth_gravity_model M>
hae_altitude<M> to_hae(msl_altitude msl, position<long double> pos)
{
const auto geoid_undulation =
isq::height(GeographicLibWhatsMyOffset(pos.lat.quantity_from_origin().numerical_value_in(si::degree),
pos.lon.quantity_from_origin().numerical_value_in(si::degree)) *
isq::height(GeographicLibWhatsMyOffset(pos.lat.quantity_ref_from(equator).numerical_value_in(si::degree),
pos.lon.quantity_ref_from(prime_meridian).numerical_value_in(si::degree)) *
si::metre);
return height_above_ellipsoid<M> + (msl - mean_sea_level - geoid_undulation);
}
Expand All @@ -115,15 +115,15 @@ using hal_altitude = quantity_point<isq::altitude[si::metre], height_above_launc
template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const hal_altitude& a)
{
return os << a.quantity_from_origin() << " HAL";
return os << a.quantity_ref_from(height_above_launch) << " HAL";
}

template<>
struct MP_UNITS_STD_FMT::formatter<hal_altitude> : formatter<hal_altitude::quantity_type> {
template<typename FormatContext>
auto format(const hal_altitude& a, FormatContext& ctx)
{
formatter<hal_altitude::quantity_type>::format(a.quantity_from_origin(), ctx);
formatter<hal_altitude::quantity_type>::format(a.quantity_ref_from(height_above_launch), ctx);
return MP_UNITS_STD_FMT::format_to(ctx.out(), " HAL");
}
};
Expand Down
65 changes: 42 additions & 23 deletions src/core/include/mp-units/quantity_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class quantity_point {
q_([&] {
if constexpr (is_same_v<std::remove_const_t<decltype(point_origin)>,
std::remove_const_t<decltype(QP::point_origin)>>)
return qp.quantity_from_origin();
return qp.quantity_ref_from(point_origin);
else
return qp - point_origin;
}())
Expand Down Expand Up @@ -146,23 +146,39 @@ class quantity_point {

// data access
#ifdef __cpp_explicit_this_parameter
template<typename Self>
[[nodiscard]] constexpr auto&& quantity_from_origin(this Self&& self) noexcept
template<typename Self, std::same_as<std::remove_const_t<decltype(PO)>> PO2>
[[nodiscard]] constexpr auto&& quantity_ref_from(this Self&& self, PO2) noexcept
{
return std::forward<Self>(self).q_;
}
#else
[[nodiscard]] constexpr quantity_type& quantity_from_origin() & noexcept { return q_; }
[[nodiscard]] constexpr const quantity_type& quantity_from_origin() const& noexcept { return q_; }
[[nodiscard]] constexpr quantity_type&& quantity_from_origin() && noexcept { return std::move(q_); }
[[nodiscard]] constexpr const quantity_type&& quantity_from_origin() const&& noexcept { return std::move(q_); }
template<std::same_as<std::remove_const_t<decltype(PO)>> PO2>
[[nodiscard]] constexpr quantity_type& quantity_ref_from(PO2) & noexcept
{
return q_;
}
template<std::same_as<std::remove_const_t<decltype(PO)>> PO2>
[[nodiscard]] constexpr const quantity_type& quantity_ref_from(PO2) const& noexcept
{
return q_;
}
template<std::same_as<std::remove_const_t<decltype(PO)>> PO2>
[[nodiscard]] constexpr quantity_type&& quantity_ref_from(PO2) && noexcept
{
return std::move(q_);
}
template<std::same_as<std::remove_const_t<decltype(PO)>> PO2>
[[nodiscard]] constexpr const quantity_type&& quantity_ref_from(PO2) const&& noexcept
{
return std::move(q_);
}
#endif

template<Unit U>
requires detail::QuantityConvertibleTo<quantity_type, quantity<::mp_units::reference<quantity_spec, U{}>{}, Rep>>
[[nodiscard]] constexpr quantity_point<::mp_units::reference<quantity_spec, U{}>{}, PO, Rep> in(U) const
{
return make_quantity_point<PO>(quantity_from_origin().in(U{}));
return make_quantity_point<PO>(quantity_ref_from(PO).in(U{}));
}

// member unary operators
Expand Down Expand Up @@ -235,17 +251,17 @@ template<auto R1, auto PO1, typename Rep1, auto R2, typename Rep2>
requires ReferenceOf<std::remove_const_t<decltype(R2)>, PO1.quantity_spec>
[[nodiscard]] constexpr QuantityPoint auto operator+(const quantity_point<R1, PO1, Rep1>& qp,
const quantity<R2, Rep2>& q)
requires requires { qp.quantity_from_origin() + q; }
requires requires { qp.quantity_ref_from(PO1) + q; }
{
return make_quantity_point<PO1>(qp.quantity_from_origin() + q);
return make_quantity_point<PO1>(qp.quantity_ref_from(PO1) + q);
}

template<auto R1, typename Rep1, auto R2, auto PO2, typename Rep2>
// TODO simplify when gcc catches up
requires ReferenceOf<std::remove_const_t<decltype(R1)>, PO2.quantity_spec>
[[nodiscard]] constexpr QuantityPoint auto operator+(const quantity<R1, Rep1>& q,
const quantity_point<R2, PO2, Rep2>& qp)
requires requires { q + qp.quantity_from_origin(); }
requires requires { q + qp.quantity_ref_from(PO2); }
{
return qp + q;
}
Expand All @@ -269,9 +285,9 @@ template<auto R1, auto PO1, typename Rep1, auto R2, typename Rep2>
requires ReferenceOf<std::remove_const_t<decltype(R2)>, PO1.quantity_spec>
[[nodiscard]] constexpr QuantityPoint auto operator-(const quantity_point<R1, PO1, Rep1>& qp,
const quantity<R2, Rep2>& q)
requires requires { qp.quantity_from_origin() - q; }
requires requires { qp.quantity_ref_from(PO1) - q; }
{
return make_quantity_point<PO1>(qp.quantity_from_origin() - q);
return make_quantity_point<PO1>(qp.quantity_ref_from(PO1) - q);
}

template<PointOrigin PO, Quantity Q>
Expand All @@ -285,33 +301,36 @@ template<PointOrigin PO, Quantity Q>
template<QuantityPoint QP1, QuantityPointOf<QP1::absolute_point_origin> QP2>
[[nodiscard]] constexpr Quantity auto operator-(const QP1& lhs, const QP2& rhs)
// TODO consider constraining it for both branches
requires requires { lhs.quantity_from_origin() - rhs.quantity_from_origin(); }
requires requires { lhs.quantity_ref_from(QP1::point_origin) - rhs.quantity_ref_from(QP2::point_origin); }
{
if constexpr (is_same_v<std::remove_const_t<decltype(QP1::point_origin)>,
std::remove_const_t<decltype(QP2::point_origin)>>)
return lhs.quantity_from_origin() - rhs.quantity_from_origin();
return lhs.quantity_ref_from(QP1::point_origin) - rhs.quantity_ref_from(QP2::point_origin);
else
return lhs.quantity_from_origin() - rhs.quantity_from_origin() + (lhs.point_origin - rhs.point_origin);
return lhs.quantity_ref_from(QP1::point_origin) - rhs.quantity_ref_from(QP2::point_origin) +
(lhs.point_origin - rhs.point_origin);
}

template<PointOrigin PO, QuantityPointOf<PO{}> QP>
requires ReferenceOf<std::remove_const_t<decltype(QP::reference)>, PO::quantity_spec>
[[nodiscard]] constexpr Quantity auto operator-(const QP& qp, PO po)
{
if constexpr (is_same_v<std::remove_const_t<decltype(QP::point_origin)>, std::remove_const_t<PO>>)
return qp.quantity_from_origin();
return qp.quantity_ref_from(QP::point_origin);
else if constexpr (detail::is_derived_from_specialization_of_absolute_point_origin<PO>) {
if constexpr (is_same_v<std::remove_const_t<decltype(QP::point_origin)>,
std::remove_const_t<decltype(QP::absolute_point_origin)>>)
return qp.quantity_from_origin();
return qp.quantity_ref_from(QP::point_origin);
else
return qp.quantity_from_origin() + (qp.point_origin - qp.absolute_point_origin);
return qp.quantity_ref_from(QP::point_origin) + (qp.point_origin - qp.absolute_point_origin);
} else {
if constexpr (is_same_v<std::remove_const_t<decltype(QP::point_origin)>,
std::remove_const_t<decltype(po.quantity_point.point_origin)>>)
return qp.quantity_from_origin() - po.quantity_point.quantity_from_origin();
return qp.quantity_ref_from(QP::point_origin) -
po.quantity_point.quantity_ref_from(po.quantity_point.point_origin);
else
return qp.quantity_from_origin() - po.quantity_point.quantity_from_origin() +
return qp.quantity_ref_from(QP::point_origin) -
po.quantity_point.quantity_ref_from(po.quantity_point.point_origin) +
(qp.point_origin - po.quantity_point.point_origin);
}
}
Expand Down Expand Up @@ -344,7 +363,7 @@ template<QuantityPoint QP1, QuantityPointOf<QP1::absolute_point_origin> QP2>
{
if constexpr (is_same_v<std::remove_const_t<decltype(QP1::point_origin)>,
std::remove_const_t<decltype(QP2::point_origin)>>)
return lhs.quantity_from_origin() <=> rhs.quantity_from_origin();
return lhs.quantity_ref_from(QP1::point_origin) <=> rhs.quantity_ref_from(QP2::point_origin);
else
return lhs - lhs.absolute_point_origin <=> rhs - rhs.absolute_point_origin;
}
Expand All @@ -355,7 +374,7 @@ template<QuantityPoint QP1, QuantityPointOf<QP1::absolute_point_origin> QP2>
{
if constexpr (is_same_v<std::remove_const_t<decltype(QP1::point_origin)>,
std::remove_const_t<decltype(QP2::point_origin)>>)
return lhs.quantity_from_origin() == rhs.quantity_from_origin();
return lhs.quantity_ref_from(QP1::point_origin) == rhs.quantity_ref_from(QP2::point_origin);
else
return lhs - lhs.absolute_point_origin == rhs - rhs.absolute_point_origin;
}
Expand Down
Loading

0 comments on commit 493cc3c

Please sign in to comment.