forked from mpusz/mp-units
Merge branch 'master' into chiphogg/switch-to-mag
This commit is contained in:
@@ -22,6 +22,9 @@
|
||||
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
|
||||
add_library(example_utils INTERFACE)
|
||||
target_include_directories(example_utils INTERFACE include)
|
||||
|
||||
#
|
||||
# add_example(target <depependencies>...)
|
||||
#
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include <units/isq/si/fps/mass.h>
|
||||
#include <units/isq/si/fps/power.h>
|
||||
#include <units/isq/si/fps/speed.h>
|
||||
#include <units/isq/si/international/speed.h>
|
||||
#include <units/isq/si/length.h>
|
||||
#include <units/isq/si/mass.h>
|
||||
#include <units/isq/si/power.h>
|
||||
@@ -68,7 +69,7 @@ void print_details(std::string_view description, const Ship& ship)
|
||||
<< STD_FMT::format("{:20} : {}\n", "beam", fmt_line<si::fps::length::yd<>, si::length::m<>>(ship.beam))
|
||||
<< STD_FMT::format("{:20} : {}\n", "mass", fmt_line<si::fps::mass::lton<>, si::mass::t<>>(ship.mass))
|
||||
<< STD_FMT::format("{:20} : {}\n", "speed",
|
||||
fmt_line<si::fps::speed::knot<>, si::speed::km_per_h<>>(ship.speed))
|
||||
fmt_line<si::international::speed::kn<>, si::speed::km_per_h<>>(ship.speed))
|
||||
<< STD_FMT::format("{:20} : {}\n", "power", fmt_line<si::fps::power::hp<>, si::power::kW<>>(ship.power))
|
||||
<< STD_FMT::format("{:20} : {}\n", "main guns",
|
||||
fmt_line<si::fps::length::in<>, si::length::mm<>>(ship.mainGuns))
|
||||
@@ -101,7 +102,7 @@ int main()
|
||||
auto iowa = Ship{.length{ft<>(860.)},
|
||||
.draft{ft<>(37.) + in<>(2.)},
|
||||
.beam{ft<>(108.) + in<>(2.)},
|
||||
.speed{knot<>(33)},
|
||||
.speed{international::kn<>(33)},
|
||||
.mass{lton<>(57'540)},
|
||||
.mainGuns{in<>(16)},
|
||||
.shellMass{lb<>(2700)},
|
||||
@@ -112,7 +113,7 @@ int main()
|
||||
auto kgv = Ship{.length{ft<>(745.1)},
|
||||
.draft{ft<>(33.) + in<>(7.5)},
|
||||
.beam{ft<>(103.2) + in<>(2.5)},
|
||||
.speed{knot<>(28.3)},
|
||||
.speed{international::kn<>(28.3)},
|
||||
.mass{lton<>(42'245)},
|
||||
.mainGuns{in<>(14)},
|
||||
.shellMass{lb<>(1'590)},
|
||||
|
||||
@@ -32,8 +32,8 @@ using namespace units;
|
||||
|
||||
namespace fps {
|
||||
|
||||
struct foot : named_unit<foot, "ft", no_prefix> {};
|
||||
struct yard : named_scaled_unit<yard, "yd", no_prefix, as_magnitude<3>(), foot> {};
|
||||
struct foot : named_unit<foot, "ft"> {};
|
||||
struct yard : named_scaled_unit<yard, "yd", as_magnitude<3>(), foot> {};
|
||||
|
||||
struct dim_length : base_dimension<"L", foot> {};
|
||||
|
||||
@@ -44,7 +44,7 @@ using length = quantity<dim_length, U, Rep>;
|
||||
|
||||
namespace si {
|
||||
|
||||
struct metre : named_unit<metre, "m", units::isq::si::prefix> {};
|
||||
struct metre : named_unit<metre, "m"> {};
|
||||
struct kilometre : prefixed_unit<kilometre, units::isq::si::kilo, metre> {};
|
||||
|
||||
struct dim_length : base_dimension<"L", metre> {};
|
||||
@@ -54,8 +54,8 @@ using length = quantity<dim_length, U, Rep>;
|
||||
|
||||
namespace fps {
|
||||
|
||||
struct foot : named_scaled_unit<foot, "ft", no_prefix, as_magnitude<ratio(3'048, 1'000, -1)>(), metre> {};
|
||||
struct yard : named_scaled_unit<yard, "yd", no_prefix, as_magnitude<3>(), foot> {};
|
||||
struct foot : named_scaled_unit<foot, "ft", as_magnitude<ratio(3'048, 1'000, -1)>(), metre> {};
|
||||
struct yard : named_scaled_unit<yard, "yd", as_magnitude<3>(), foot> {};
|
||||
|
||||
struct dim_length : base_dimension<"L", foot> {};
|
||||
|
||||
|
||||
@@ -22,6 +22,6 @@
|
||||
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
|
||||
add_library(glide_computer STATIC geographic.cpp include/geographic.h glide_computer.cpp include/glide_computer.h)
|
||||
target_link_libraries(glide_computer PRIVATE mp-units::core-fmt PUBLIC mp-units::si)
|
||||
add_library(glide_computer STATIC include/geographic.h glide_computer.cpp include/glide_computer.h)
|
||||
target_link_libraries(glide_computer PRIVATE mp-units::core-fmt PUBLIC mp-units::si example_utils)
|
||||
target_include_directories(glide_computer PUBLIC include)
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2018 Mateusz Pusz
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include "geographic.h"
|
||||
#include <cmath>
|
||||
#include <numbers>
|
||||
#include <type_traits>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace units::isq::si;
|
||||
inline constexpr length<kilometre> earth_radius(6371);
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace geographic {
|
||||
|
||||
distance spherical_distance(position from, position to)
|
||||
{
|
||||
using rep = std::common_type_t<latitude::value_type, longitude::value_type>;
|
||||
constexpr auto p = std::numbers::pi_v<rep> / 180;
|
||||
const auto lat1 = from.lat.value() * p;
|
||||
const auto lon1 = from.lon.value() * p;
|
||||
const auto lat2 = to.lat.value() * p;
|
||||
const auto lon2 = to.lon.value() * p;
|
||||
|
||||
using std::sin, std::cos, std::asin, std::sqrt;
|
||||
|
||||
// https://en.wikipedia.org/wiki/Great-circle_distance#Formulae
|
||||
if constexpr (sizeof(rep) >= 8) {
|
||||
// spherical law of cosines
|
||||
const auto central_angle = acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1));
|
||||
// const auto central_angle = 2 * asin(sqrt(0.5 - cos(lat2 - lat1) / 2 + cos(lat1) * cos(lat2) * (1 - cos(lon2 -
|
||||
// lon1)) / 2));
|
||||
return distance(earth_radius * central_angle);
|
||||
} else {
|
||||
// the haversine formula
|
||||
const auto sin_lat = sin((lat2 - lat1) / 2);
|
||||
const auto sin_lon = sin((lon2 - lon1) / 2);
|
||||
const auto central_angle = 2 * asin(sqrt(sin_lat * sin_lat + cos(lat1) * cos(lat2) * sin_lon * sin_lon));
|
||||
return distance(earth_radius * central_angle);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace geographic
|
||||
@@ -22,10 +22,14 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ranged_representation.h"
|
||||
#include <units/bits/external/hacks.h>
|
||||
#include <units/bits/fmt_hacks.h>
|
||||
#include <units/generic/dimensionless.h>
|
||||
#include <units/isq/si/length.h>
|
||||
#include <units/quantity_kind.h>
|
||||
#include <limits>
|
||||
#include <numbers>
|
||||
#include <ostream>
|
||||
|
||||
// IWYU pragma: begin_exports
|
||||
@@ -34,88 +38,95 @@
|
||||
|
||||
namespace geographic {
|
||||
|
||||
template<typename Derived, typename Rep>
|
||||
struct coordinate {
|
||||
using value_type = Rep;
|
||||
constexpr explicit coordinate(value_type v) : value_(v) {}
|
||||
constexpr value_type value() const { return value_; }
|
||||
auto operator<=>(const coordinate&) const = default;
|
||||
private:
|
||||
value_type value_;
|
||||
};
|
||||
// TODO Change to `angle` dimension in degree unit when the work on magnitudes is done
|
||||
template<typename T = double>
|
||||
using latitude = units::dimensionless<units::one, ranged_representation<T, -90, 90>>;
|
||||
|
||||
struct latitude : coordinate<latitude, double> {
|
||||
using coordinate::coordinate;
|
||||
};
|
||||
template<typename T = double>
|
||||
using longitude = units::dimensionless<units::one, ranged_representation<T, -180, 180>>;
|
||||
|
||||
struct longitude : coordinate<longitude, double> {
|
||||
using coordinate::coordinate;
|
||||
};
|
||||
|
||||
template<class CharT, class Traits>
|
||||
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const latitude& lat)
|
||||
template<class CharT, class Traits, typename T>
|
||||
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const latitude<T>& lat)
|
||||
{
|
||||
if (lat.value() > 0)
|
||||
return os << "N" << lat.value();
|
||||
if (lat.number() > 0)
|
||||
return os << "N" << lat.number();
|
||||
else
|
||||
return os << "S" << -lat.value();
|
||||
return os << "S" << -lat.number();
|
||||
}
|
||||
|
||||
template<class CharT, class Traits>
|
||||
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const longitude& lon)
|
||||
template<class CharT, class Traits, typename T>
|
||||
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const longitude<T>& lon)
|
||||
{
|
||||
if (lon.value() > 0)
|
||||
return os << "E" << lon.value();
|
||||
if (lon.number() > 0)
|
||||
return os << "E" << lon.number();
|
||||
else
|
||||
return os << "W" << -lon.value();
|
||||
return os << "W" << -lon.number();
|
||||
}
|
||||
|
||||
inline namespace literals {
|
||||
|
||||
constexpr auto operator"" _N(unsigned long long v) { return latitude(static_cast<latitude::value_type>(v)); }
|
||||
constexpr auto operator"" _N(long double v) { return latitude(static_cast<latitude::value_type>(v)); }
|
||||
constexpr auto operator"" _S(unsigned long long v) { return latitude(-static_cast<latitude::value_type>(v)); }
|
||||
constexpr auto operator"" _S(long double v) { return latitude(-static_cast<latitude::value_type>(v)); }
|
||||
constexpr auto operator"" _E(unsigned long long v) { return longitude(static_cast<longitude::value_type>(v)); }
|
||||
constexpr auto operator"" _E(long double v) { return longitude(static_cast<longitude::value_type>(v)); }
|
||||
constexpr auto operator"" _W(unsigned long long v) { return longitude(-static_cast<longitude::value_type>(v)); }
|
||||
constexpr auto operator"" _W(long double v) { return longitude(-static_cast<longitude::value_type>(v)); }
|
||||
constexpr auto operator"" _N(long double v) { return latitude<long double>(latitude<long double>::rep(v)); }
|
||||
constexpr auto operator"" _S(long double v) { return latitude<long double>(latitude<long double>::rep(v)); }
|
||||
constexpr auto operator"" _E(long double v) { return longitude<long double>(longitude<long double>::rep(v)); }
|
||||
constexpr auto operator"" _W(long double v) { return longitude<long double>(longitude<long double>::rep(v)); }
|
||||
constexpr auto operator"" _N(unsigned long long v)
|
||||
{
|
||||
gsl_ExpectsAudit(std::in_range<std::int64_t>(v));
|
||||
return latitude<std::int64_t>(latitude<std::int64_t>::rep(static_cast<std::int64_t>(v)));
|
||||
}
|
||||
constexpr auto operator"" _S(unsigned long long v)
|
||||
{
|
||||
gsl_ExpectsAudit(std::in_range<std::int64_t>(v));
|
||||
return latitude<std::int64_t>(-latitude<std::int64_t>::rep(static_cast<std::int64_t>(v)));
|
||||
}
|
||||
constexpr auto operator"" _E(unsigned long long v)
|
||||
{
|
||||
gsl_ExpectsAudit(std::in_range<std::int64_t>(v));
|
||||
return longitude<std::int64_t>(longitude<std::int64_t>::rep(static_cast<std::int64_t>(v)));
|
||||
}
|
||||
constexpr auto operator"" _W(unsigned long long v)
|
||||
{
|
||||
gsl_ExpectsAudit(std::in_range<std::int64_t>(v));
|
||||
return longitude<std::int64_t>(-longitude<std::int64_t>::rep(static_cast<std::int64_t>(v)));
|
||||
}
|
||||
|
||||
} // namespace literals
|
||||
|
||||
} // namespace geographic
|
||||
|
||||
template<>
|
||||
class std::numeric_limits<geographic::latitude> : public numeric_limits<geographic::latitude::value_type> {
|
||||
static constexpr auto min() noexcept { return geographic::latitude(-90); }
|
||||
static constexpr auto lowest() noexcept { return geographic::latitude(-90); }
|
||||
static constexpr auto max() noexcept { return geographic::latitude(90); }
|
||||
template<typename T>
|
||||
class std::numeric_limits<geographic::latitude<T>> : public numeric_limits<T> {
|
||||
static constexpr auto min() noexcept { return geographic::latitude<T>(-90); }
|
||||
static constexpr auto lowest() noexcept { return geographic::latitude<T>(-90); }
|
||||
static constexpr auto max() noexcept { return geographic::latitude<T>(90); }
|
||||
};
|
||||
|
||||
template<>
|
||||
class std::numeric_limits<geographic::longitude> : public numeric_limits<geographic::longitude::value_type> {
|
||||
static constexpr auto min() noexcept { return geographic::longitude(-180); }
|
||||
static constexpr auto lowest() noexcept { return geographic::longitude(-180); }
|
||||
static constexpr auto max() noexcept { return geographic::longitude(180); }
|
||||
template<typename T>
|
||||
class std::numeric_limits<geographic::longitude<T>> : public numeric_limits<T> {
|
||||
static constexpr auto min() noexcept { return geographic::longitude<T>(-180); }
|
||||
static constexpr auto lowest() noexcept { return geographic::longitude<T>(-180); }
|
||||
static constexpr auto max() noexcept { return geographic::longitude<T>(180); }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct STD_FMT::formatter<geographic::latitude> : formatter<geographic::latitude::value_type> {
|
||||
template<typename T>
|
||||
struct STD_FMT::formatter<geographic::latitude<T>> : formatter<T> {
|
||||
template<typename FormatContext>
|
||||
auto format(geographic::latitude lat, FormatContext& ctx)
|
||||
auto format(geographic::latitude<T> lat, FormatContext& ctx)
|
||||
{
|
||||
STD_FMT::format_to(ctx.out(), "{}", lat.value() > 0 ? 'N' : 'S');
|
||||
return formatter<geographic::latitude::value_type>::format(lat.value() > 0 ? lat.value() : -lat.value(), ctx);
|
||||
using rep = TYPENAME geographic::latitude<T>::rep;
|
||||
STD_FMT::format_to(ctx.out(), "{}", lat > rep{0} ? 'N' : 'S');
|
||||
return formatter<T>::format(lat > rep{0} ? lat.number() : -lat.number(), ctx);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct STD_FMT::formatter<geographic::longitude> : formatter<geographic::longitude::value_type> {
|
||||
template<typename T>
|
||||
struct STD_FMT::formatter<geographic::longitude<T>> : formatter<T> {
|
||||
template<typename FormatContext>
|
||||
auto format(geographic::longitude lon, FormatContext& ctx)
|
||||
auto format(geographic::longitude<T> lon, FormatContext& ctx)
|
||||
{
|
||||
STD_FMT::format_to(ctx.out(), "{}", lon.value() > 0 ? 'E' : 'W');
|
||||
return formatter<geographic::longitude::value_type>::format(lon.value() > 0 ? lon.value() : -lon.value(), ctx);
|
||||
using rep = TYPENAME geographic::longitude<T>::rep;
|
||||
STD_FMT::format_to(ctx.out(), "{}", lon > rep{0} ? 'E' : 'W');
|
||||
return formatter<T>::format(lon > rep{0} ? lon.number() : -lon.number(), ctx);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -124,11 +135,41 @@ namespace geographic {
|
||||
struct horizontal_kind : units::kind<horizontal_kind, units::isq::si::dim_length> {};
|
||||
using distance = units::quantity_kind<horizontal_kind, units::isq::si::kilometre>;
|
||||
|
||||
template<typename T>
|
||||
struct position {
|
||||
latitude lat;
|
||||
longitude lon;
|
||||
latitude<T> lat;
|
||||
longitude<T> lon;
|
||||
};
|
||||
|
||||
distance spherical_distance(position from, position to);
|
||||
template<typename T>
|
||||
distance spherical_distance(position<T> from, position<T> to)
|
||||
{
|
||||
using namespace units::isq::si;
|
||||
constexpr length<kilometre> earth_radius(6371);
|
||||
|
||||
constexpr auto p = std::numbers::pi_v<T> / 180;
|
||||
const auto lat1_rad = from.lat.number() * p;
|
||||
const auto lon1_rad = from.lon.number() * p;
|
||||
const auto lat2_rad = to.lat.number() * p;
|
||||
const auto lon2_rad = to.lon.number() * p;
|
||||
|
||||
using std::sin, std::cos, std::asin, std::acos, std::sqrt;
|
||||
|
||||
// https://en.wikipedia.org/wiki/Great-circle_distance#Formulae
|
||||
if constexpr (sizeof(T) >= 8) {
|
||||
// spherical law of cosines
|
||||
const auto central_angle =
|
||||
acos(sin(lat1_rad) * sin(lat2_rad) + cos(lat1_rad) * cos(lat2_rad) * cos(lon2_rad - lon1_rad));
|
||||
// const auto central_angle = 2 * asin(sqrt(0.5 - cos(lat2_rad - lat1_rad) / 2 + cos(lat1_rad) * cos(lat2_rad) * (1
|
||||
// - cos(lon2_rad - lon1_rad)) / 2));
|
||||
return distance(earth_radius * central_angle);
|
||||
} else {
|
||||
// the haversine formula
|
||||
const auto sin_lat = sin((lat2_rad - lat1_rad) / 2);
|
||||
const auto sin_lon = sin((lon2_rad - lon1_rad) / 2);
|
||||
const auto central_angle = 2 * asin(sqrt(sin_lat * sin_lat + cos(lat1_rad) * cos(lat2_rad) * sin_lon * sin_lon));
|
||||
return distance(earth_radius * central_angle);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace geographic
|
||||
|
||||
@@ -136,7 +136,7 @@ struct weather {
|
||||
|
||||
struct waypoint {
|
||||
std::string name;
|
||||
geographic::position pos;
|
||||
geographic::position<long double> pos;
|
||||
altitude alt;
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2018 Mateusz Pusz
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "validated_type.h"
|
||||
#include <units/bits/external/hacks.h>
|
||||
#include <algorithm>
|
||||
#include <type_traits>
|
||||
|
||||
template<typename T, auto Min, auto Max>
|
||||
inline constexpr auto is_in_range = [](const auto& v) { return std::clamp(v, T{Min}, T{Max}) == v; };
|
||||
|
||||
template<typename T, auto Min, auto Max>
|
||||
using is_in_range_t = decltype(is_in_range<T, Min, Max>);
|
||||
|
||||
template<typename T, auto Min, auto Max>
|
||||
class ranged_representation : public validated_type<T, is_in_range_t<T, Min, Max>> {
|
||||
public:
|
||||
using validated_type<T, is_in_range_t<T, Min, Max>>::validated_type;
|
||||
constexpr ranged_representation() : validated_type<T, is_in_range_t<T, Min, Max>>(T{}) {}
|
||||
|
||||
[[nodiscard]] constexpr ranged_representation operator-() const { return ranged_representation(-this->value()); }
|
||||
};
|
||||
|
||||
template<typename T, auto Min, auto Max>
|
||||
struct std::common_type<std::intmax_t, ranged_representation<T, Min, Max>> :
|
||||
std::type_identity<ranged_representation<std::common_type_t<std::intmax_t, T>, Min, Max>> {};
|
||||
|
||||
template<typename T, auto Min, auto Max>
|
||||
struct std::common_type<ranged_representation<T, Min, Max>, std::intmax_t> :
|
||||
std::type_identity<ranged_representation<std::common_type_t<T, std::intmax_t>, Min, Max>> {};
|
||||
@@ -0,0 +1,81 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2018 Mateusz Pusz
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gsl/gsl-lite.hpp>
|
||||
#include <units/bits/external/hacks.h>
|
||||
#include <utility>
|
||||
|
||||
inline constexpr struct validated_tag {
|
||||
} validated;
|
||||
|
||||
template<std::movable T, std::predicate<T> Validator>
|
||||
class validated_type {
|
||||
T value_;
|
||||
public:
|
||||
using value_type = T;
|
||||
|
||||
static constexpr bool validate(const T& value) { return Validator()(value); }
|
||||
|
||||
constexpr explicit validated_type(const T& value) noexcept(std::is_nothrow_copy_constructible_v<T>)
|
||||
requires std::copyable<T>
|
||||
: value_(value)
|
||||
{
|
||||
gsl_Expects(validate(value_));
|
||||
}
|
||||
|
||||
constexpr explicit validated_type(T&& value) noexcept(std::is_nothrow_move_constructible_v<T>) :
|
||||
value_(std::move(value))
|
||||
{
|
||||
gsl_Expects(validate(value_));
|
||||
}
|
||||
|
||||
constexpr validated_type(const T& value, validated_tag) noexcept(std::is_nothrow_copy_constructible_v<T>)
|
||||
requires std::copyable<T>
|
||||
: value_(value)
|
||||
{
|
||||
}
|
||||
|
||||
constexpr validated_type(T&& value, validated_tag) noexcept(std::is_nothrow_move_constructible_v<T>) :
|
||||
value_(std::move(value))
|
||||
{
|
||||
}
|
||||
|
||||
constexpr explicit(false) operator T() const noexcept(std::is_nothrow_copy_constructible_v<T>)
|
||||
requires std::copyable<T>
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
|
||||
constexpr T& value() & noexcept = delete;
|
||||
constexpr const T& value() const& noexcept { return value_; }
|
||||
constexpr T&& value() && noexcept { return std::move(value_); }
|
||||
constexpr const T&& value() const&& noexcept { return std::move(value_); }
|
||||
|
||||
bool operator==(const validated_type&) const
|
||||
requires std::equality_comparable<T>
|
||||
= default;
|
||||
auto operator<=>(const validated_type&) const
|
||||
requires std::three_way_comparable<T>
|
||||
= default;
|
||||
};
|
||||
@@ -84,7 +84,7 @@ public:
|
||||
uncertainty_type uncertainty;
|
||||
};
|
||||
|
||||
#if UNITS_COMP_CLANG <= 13
|
||||
#if UNITS_COMP_CLANG <= 14
|
||||
|
||||
template<QuantityOrQuantityPoint QQP, units::Quantity U>
|
||||
estimation(state<QQP>, U) -> estimation<QQP>;
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
// TODO Fix when Celsius is properly supported (#232)
|
||||
namespace units::isq::si {
|
||||
|
||||
struct degree_celsius : alias_unit<kelvin, basic_symbol_text{"°C", "deg_C"}, no_prefix> {};
|
||||
struct degree_celsius : alias_unit<kelvin, basic_symbol_text{"°C", "deg_C"}> {};
|
||||
|
||||
namespace thermodynamic_temperature_references {
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
// TODO Fix when Celsius is properly supported (#232)
|
||||
namespace units::isq::si {
|
||||
|
||||
struct degree_celsius : alias_unit<kelvin, basic_symbol_text{"°C", "deg_C"}, no_prefix> {};
|
||||
struct degree_celsius : alias_unit<kelvin, basic_symbol_text{"°C", "deg_C"}> {};
|
||||
|
||||
namespace thermodynamic_temperature_references {
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
// TODO Fix when Celsius is properly supported (#232)
|
||||
namespace units::isq::si {
|
||||
|
||||
struct degree_celsius : alias_unit<kelvin, basic_symbol_text{"°C", "deg_C"}, no_prefix> {};
|
||||
struct degree_celsius : alias_unit<kelvin, basic_symbol_text{"°C", "deg_C"}> {};
|
||||
|
||||
namespace thermodynamic_temperature_references {
|
||||
|
||||
|
||||
@@ -1,113 +0,0 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2018 Mateusz Pusz
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include <units/base_dimension.h>
|
||||
#include <units/isq/si/prefixes.h>
|
||||
#include <units/quantity.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <units/unit.h>
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
|
||||
using namespace units;
|
||||
|
||||
namespace fps {
|
||||
|
||||
struct foot : named_unit<foot, "ft", no_prefix> {};
|
||||
struct yard : named_scaled_unit<yard, "yd", no_prefix, as_magnitude<3>(), foot> {};
|
||||
|
||||
struct dim_length : base_dimension<"L", foot> {};
|
||||
|
||||
template<UnitOf<dim_length> U, Representation Rep = double>
|
||||
using length = quantity<dim_length, U, Rep>;
|
||||
|
||||
} // namespace fps
|
||||
|
||||
namespace si {
|
||||
|
||||
struct metre : named_unit<metre, "m", units::isq::si::prefix> {};
|
||||
struct kilometre : prefixed_unit<kilometre, units::isq::si::kilo, metre> {};
|
||||
|
||||
struct dim_length : base_dimension<"L", metre> {};
|
||||
|
||||
template<UnitOf<dim_length> U, Representation Rep = double>
|
||||
using length = quantity<dim_length, U, Rep>;
|
||||
|
||||
namespace fps {
|
||||
|
||||
struct foot : named_scaled_unit<foot, "ft", no_prefix, as_magnitude<ratio(3'048, 1'000, -1)>(), metre> {};
|
||||
struct yard : named_scaled_unit<yard, "yd", no_prefix, as_magnitude<3>(), foot> {};
|
||||
|
||||
struct dim_length : base_dimension<"L", foot> {};
|
||||
|
||||
template<UnitOf<dim_length> U, Representation Rep = double>
|
||||
using length = quantity<dim_length, U, Rep>;
|
||||
|
||||
} // namespace fps
|
||||
} // namespace si
|
||||
|
||||
void conversions()
|
||||
{
|
||||
// constexpr auto fps_yard = fps::length<fps::yard>(1.);
|
||||
// std::cout << quantity_cast<si::kilometre>(fps_yard) << "\n";
|
||||
|
||||
constexpr auto si_fps_yard = si::fps::length<si::fps::yard>(1.);
|
||||
std::cout << quantity_cast<si::kilometre>(si_fps_yard) << "\n";
|
||||
}
|
||||
|
||||
void unknown_dimensions()
|
||||
{
|
||||
constexpr auto fps_yard = fps::length<fps::yard>(1.);
|
||||
constexpr auto fps_area = quantity_cast<unknown_coherent_unit>(fps_yard * fps_yard);
|
||||
std::cout << fps_yard << "\n";
|
||||
std::cout << fps_area << "\n";
|
||||
|
||||
constexpr auto si_fps_yard = si::fps::length<si::fps::yard>(1.);
|
||||
constexpr auto si_fps_area = quantity_cast<unknown_coherent_unit>(si_fps_yard * si_fps_yard);
|
||||
std::cout << si_fps_yard << "\n";
|
||||
std::cout << si_fps_area << "\n";
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const ratio& r)
|
||||
{
|
||||
return os << "ratio{" << r.num << ", " << r.den << ", " << r.exp << "}";
|
||||
}
|
||||
|
||||
template<Unit U>
|
||||
std::ostream& operator<<(std::ostream& os, const U& u)
|
||||
{
|
||||
using unit_type = std::remove_cvref_t<decltype(u)>;
|
||||
return os << as_ratio(unit_type::mag) << " x " << unit_type::reference::symbol.standard();
|
||||
}
|
||||
|
||||
void what_is_your_ratio()
|
||||
{
|
||||
std::cout << "fps: " << fps::yard() << "\n";
|
||||
std::cout << "si::fps: " << si::fps::yard() << "\n";
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
conversions();
|
||||
unknown_dimensions();
|
||||
what_is_your_ratio();
|
||||
}
|
||||
@@ -26,6 +26,7 @@
|
||||
#include <units/isq/si/fps/mass.h>
|
||||
#include <units/isq/si/fps/power.h>
|
||||
#include <units/isq/si/fps/speed.h>
|
||||
#include <units/isq/si/international/speed.h>
|
||||
#include <units/isq/si/length.h>
|
||||
#include <units/isq/si/mass.h>
|
||||
#include <units/isq/si/power.h>
|
||||
@@ -73,8 +74,9 @@ void print_details(std::string_view description, const Ship& ship)
|
||||
fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.beam))
|
||||
<< STD_FMT::format("{:20} : {}\n", "mass",
|
||||
fmt_line<si::fps::mass<si::fps::long_ton>, si::mass<si::tonne>>(ship.mass))
|
||||
<< STD_FMT::format("{:20} : {}\n", "speed",
|
||||
fmt_line<si::fps::speed<si::fps::knot>, si::speed<si::kilometre_per_hour>>(ship.speed))
|
||||
<< STD_FMT::format(
|
||||
"{:20} : {}\n", "speed",
|
||||
fmt_line<si::speed<si::international::knot>, si::speed<si::kilometre_per_hour>>(ship.speed))
|
||||
<< STD_FMT::format("{:20} : {}\n", "power",
|
||||
fmt_line<si::fps::power<si::fps::horse_power>, si::power<si::kilowatt>>(ship.power))
|
||||
<< STD_FMT::format("{:20} : {}\n", "main guns",
|
||||
@@ -108,7 +110,7 @@ int main()
|
||||
auto iowa = Ship{.length{860._q_ft},
|
||||
.draft{37._q_ft + 2._q_in},
|
||||
.beam{108._q_ft + 2._q_in},
|
||||
.speed{33_q_knot},
|
||||
.speed{si::speed<si::international::knot>{33}},
|
||||
.mass{57'540_q_lton},
|
||||
.mainGuns{16_q_in},
|
||||
.shellMass{2700_q_lb},
|
||||
@@ -119,7 +121,7 @@ int main()
|
||||
auto kgv = Ship{.length{745.1_q_ft},
|
||||
.draft{33._q_ft + 7.5_q_in},
|
||||
.beam{103.2_q_ft + 2.5_q_in},
|
||||
.speed{28.3_q_knot},
|
||||
.speed{si::speed<si::international::knot>{28.3}},
|
||||
.mass{42'245_q_lton},
|
||||
.mainGuns{14_q_in},
|
||||
.shellMass{1'590_q_lb},
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <units/isq/si/fps/speed.h>
|
||||
#include <units/isq/si/fps/time.h>
|
||||
#include <units/isq/si/fps/volume.h>
|
||||
#include <units/isq/si/international/speed.h>
|
||||
#include <units/isq/si/length.h>
|
||||
#include <units/isq/si/mass.h>
|
||||
#include <units/isq/si/power.h>
|
||||
@@ -76,8 +77,9 @@ void print_details(std::string_view description, const Ship& ship)
|
||||
fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.beam))
|
||||
<< STD_FMT::format("{:20} : {}\n", "mass",
|
||||
fmt_line<si::fps::mass<si::fps::long_ton>, si::mass<si::tonne>>(ship.mass))
|
||||
<< STD_FMT::format("{:20} : {}\n", "speed",
|
||||
fmt_line<si::fps::speed<si::fps::knot>, si::speed<si::kilometre_per_hour>>(ship.speed))
|
||||
<< STD_FMT::format(
|
||||
"{:20} : {}\n", "speed",
|
||||
fmt_line<si::speed<si::international::knot>, si::speed<si::kilometre_per_hour>>(ship.speed))
|
||||
<< STD_FMT::format("{:20} : {}\n", "power",
|
||||
fmt_line<si::fps::power<si::fps::horse_power>, si::power<si::kilowatt>>(ship.power))
|
||||
<< STD_FMT::format("{:20} : {}\n", "main guns",
|
||||
@@ -112,7 +114,7 @@ int main()
|
||||
auto iowa = Ship{.length{860. * ft},
|
||||
.draft{37. * ft + 2. * in},
|
||||
.beam{108. * ft + 2. * in},
|
||||
.speed{33 * knot},
|
||||
.speed{33 * units::isq::si::international::references::kn},
|
||||
.mass{57'540 * lton},
|
||||
.mainGuns{16 * in},
|
||||
.shellMass{2700 * lb},
|
||||
@@ -123,7 +125,7 @@ int main()
|
||||
auto kgv = Ship{.length{745.1 * ft},
|
||||
.draft{33. * ft + 7.5 * in},
|
||||
.beam{103.2 * ft + 2.5 * in},
|
||||
.speed{28.3 * knot},
|
||||
.speed{28.3 * units::isq::si::international::references::kn},
|
||||
.mass{42'245 * lton},
|
||||
.mainGuns{14 * in},
|
||||
.shellMass{1'590 * lb},
|
||||
|
||||
Reference in New Issue
Block a user