2021-02-26 14:10:36 +01:00
|
|
|
// 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
|
|
|
|
|
|
2022-04-21 21:25:54 +02:00
|
|
|
#include "ranged_representation.h"
|
2021-09-20 15:01:58 +02:00
|
|
|
#include <units/bits/fmt_hacks.h>
|
2022-04-21 21:25:54 +02:00
|
|
|
#include <units/generic/dimensionless.h>
|
2021-03-16 12:03:25 +01:00
|
|
|
#include <units/isq/si/length.h>
|
2021-02-26 14:10:36 +01:00
|
|
|
#include <units/quantity_kind.h>
|
2021-03-30 13:21:05 +02:00
|
|
|
#include <limits>
|
|
|
|
|
#include <ostream>
|
|
|
|
|
|
|
|
|
|
// IWYU pragma: begin_exports
|
2021-02-26 14:10:36 +01:00
|
|
|
#include <compare>
|
2021-03-30 13:21:05 +02:00
|
|
|
// IWYU pragma: end_exports
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
namespace geographic {
|
|
|
|
|
|
2022-04-21 21:25:54 +02:00
|
|
|
// 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, T(-90), T(90)>>;
|
2021-02-26 14:10:36 +01:00
|
|
|
|
2022-04-21 21:25:54 +02:00
|
|
|
template<typename T = double>
|
|
|
|
|
using longitude = units::dimensionless<units::one, ranged_representation<T, T(-180), T(180)>>;
|
2021-02-26 14:10:36 +01:00
|
|
|
|
2022-04-21 21:25:54 +02:00
|
|
|
template<class CharT, class Traits, typename T>
|
|
|
|
|
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const latitude<T>& lat)
|
2021-02-26 14:10:36 +01:00
|
|
|
{
|
2022-04-21 21:25:54 +02:00
|
|
|
if (lat.number() > 0)
|
|
|
|
|
return os << "N" << lat.number();
|
2021-02-26 14:10:36 +01:00
|
|
|
else
|
2022-04-21 21:25:54 +02:00
|
|
|
return os << "S" << -lat.number();
|
2021-02-26 14:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-21 21:25:54 +02:00
|
|
|
template<class CharT, class Traits, typename T>
|
|
|
|
|
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const longitude<T>& lon)
|
2021-02-26 14:10:36 +01:00
|
|
|
{
|
2022-04-21 21:25:54 +02:00
|
|
|
if (lon.number() > 0)
|
|
|
|
|
return os << "E" << lon.number();
|
2021-02-26 14:10:36 +01:00
|
|
|
else
|
2022-04-21 21:25:54 +02:00
|
|
|
return os << "W" << -lon.number();
|
2021-02-26 14:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline namespace literals {
|
|
|
|
|
|
2022-04-21 21:25:54 +02:00
|
|
|
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)));
|
|
|
|
|
}
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
} // namespace literals
|
|
|
|
|
|
|
|
|
|
} // namespace geographic
|
|
|
|
|
|
2022-04-21 21:25:54 +02:00
|
|
|
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); }
|
2021-02-26 14:10:36 +01:00
|
|
|
};
|
|
|
|
|
|
2022-04-21 21:25:54 +02:00
|
|
|
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); }
|
2021-02-26 14:10:36 +01:00
|
|
|
};
|
|
|
|
|
|
2022-04-21 21:25:54 +02:00
|
|
|
template<typename T>
|
|
|
|
|
struct STD_FMT::formatter<geographic::latitude<T>> : formatter<T> {
|
2021-02-26 14:10:36 +01:00
|
|
|
template<typename FormatContext>
|
2022-04-21 21:25:54 +02:00
|
|
|
auto format(geographic::latitude<T> lat, FormatContext& ctx)
|
2021-02-26 14:10:36 +01:00
|
|
|
{
|
2022-04-21 21:25:54 +02:00
|
|
|
using rep = 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);
|
2021-02-26 14:10:36 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-21 21:25:54 +02:00
|
|
|
template<typename T>
|
|
|
|
|
struct STD_FMT::formatter<geographic::longitude<T>> : formatter<T> {
|
2021-02-26 14:10:36 +01:00
|
|
|
template<typename FormatContext>
|
2022-04-21 21:25:54 +02:00
|
|
|
auto format(geographic::longitude<T> lon, FormatContext& ctx)
|
2021-02-26 14:10:36 +01:00
|
|
|
{
|
2022-04-21 21:25:54 +02:00
|
|
|
using rep = 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);
|
2021-02-26 14:10:36 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace geographic {
|
|
|
|
|
|
2021-03-16 12:03:25 +01:00
|
|
|
struct horizontal_kind : units::kind<horizontal_kind, units::isq::si::dim_length> {};
|
|
|
|
|
using distance = units::quantity_kind<horizontal_kind, units::isq::si::kilometre>;
|
2021-02-26 14:10:36 +01:00
|
|
|
|
2022-04-21 21:25:54 +02:00
|
|
|
template<typename T>
|
2021-02-26 14:10:36 +01:00
|
|
|
struct position {
|
2022-04-21 21:25:54 +02:00
|
|
|
latitude<T> lat;
|
|
|
|
|
longitude<T> lon;
|
2021-02-26 14:10:36 +01:00
|
|
|
};
|
|
|
|
|
|
2022-04-21 21:25:54 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
} // namespace geographic
|