diff --git a/example/unmanned_aerial_vehicle.cpp b/example/unmanned_aerial_vehicle.cpp index bfcf536d..cfb81092 100644 --- a/example/unmanned_aerial_vehicle.cpp +++ b/example/unmanned_aerial_vehicle.cpp @@ -149,7 +149,7 @@ public: void current(msl_altitude alt) { current_ = alt; } [[nodiscard]] msl_altitude current() const { return current_; } - [[nodiscard]] hal_altitude hal() const { return height_above_launch + (current_ - launch_); } + [[nodiscard]] hal_altitude hal() const { return height_above_launch + current_.quantity_from(launch_); } }; @@ -164,7 +164,7 @@ int main() std::cout << MP_UNITS_STD_FMT::format("hal = {::N[.2f]}\n", uav.hal()); const msl_altitude ground_level = mean_sea_level + 123 * m; - std::cout << MP_UNITS_STD_FMT::format("agl = {::N[.2f]}\n", uav.current() - ground_level); + std::cout << MP_UNITS_STD_FMT::format("agl = {::N[.2f]}\n", uav.current().quantity_from(ground_level)); struct waypoint { std::string name; diff --git a/src/core/include/mp-units/framework/quantity_point.h b/src/core/include/mp-units/framework/quantity_point.h index d148e07a..a121feb7 100644 --- a/src/core/include/mp-units/framework/quantity_point.h +++ b/src/core/include/mp-units/framework/quantity_point.h @@ -256,6 +256,12 @@ public: return *this - PO2{}; } + template QP> + [[nodiscard]] constexpr Quantity auto quantity_from(const QP& qp) const + { + return *this - qp; + } + [[nodiscard]] constexpr Quantity auto quantity_from_zero() const { if constexpr (requires { unit.point_origin; }) { diff --git a/test/static/quantity_point_test.cpp b/test/static/quantity_point_test.cpp index b3ff307f..aa91ab8e 100644 --- a/test/static/quantity_point_test.cpp +++ b/test/static/quantity_point_test.cpp @@ -1439,6 +1439,17 @@ static_assert((ground_level + 42 * m) - (other_ground_level + 42 * m) == -81 * m static_assert((other_ground_level + 42 * m) - (tower_peak + 42 * m) == 39 * m); static_assert((tower_peak + 42 * m) - (other_ground_level + 42 * m) == -39 * m); +static_assert((mean_sea_level + 42 * m).quantity_from(ground_level + 42 * m) == -42 * m); +static_assert((ground_level + 42 * m).quantity_from(mean_sea_level + 42 * m) == 42 * m); +static_assert((tower_peak + 42 * m).quantity_from(ground_level + 42 * m) == 42 * m); +static_assert((ground_level + 42 * m).quantity_from(tower_peak + 42 * m) == -42 * m); +static_assert((tower_peak + 42 * m).quantity_from(mean_sea_level + 42 * m) == 84 * m); +static_assert((mean_sea_level + 42 * m).quantity_from(tower_peak + 42 * m) == -84 * m); +static_assert((other_ground_level + 42 * m).quantity_from(ground_level + 42 * m) == 81 * m); +static_assert((ground_level + 42 * m).quantity_from(other_ground_level + 42 * m) == -81 * m); +static_assert((other_ground_level + 42 * m).quantity_from(tower_peak + 42 * m) == 39 * m); +static_assert((tower_peak + 42 * m).quantity_from(other_ground_level + 42 * m) == -39 * m); + static_assert((mean_sea_level + 42 * m).quantity_from(mean_sea_level) == 42 * m); static_assert((42 * m + mean_sea_level).quantity_from(mean_sea_level) == 42 * m); static_assert((mean_sea_level - 42 * m).quantity_from(mean_sea_level) == -42 * m);