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
|
|
|
|
|
|
2021-03-30 13:21:05 +02:00
|
|
|
// IWYU pragma: begin_exports
|
2021-02-26 14:10:36 +01:00
|
|
|
#include "geographic.h"
|
2021-03-30 13:21:05 +02:00
|
|
|
#include <units/isq/si/length.h>
|
2021-03-16 12:03:25 +01:00
|
|
|
#include <units/isq/si/speed.h>
|
2021-03-30 13:21:05 +02:00
|
|
|
#include <units/isq/si/time.h>
|
2021-02-26 14:10:36 +01:00
|
|
|
#include <units/quantity_point_kind.h>
|
2021-03-30 13:21:05 +02:00
|
|
|
// IWYU pragma: end_exports
|
|
|
|
|
|
2021-06-27 18:35:21 -04:00
|
|
|
#include <units/chrono.h>
|
2021-03-30 13:21:05 +02:00
|
|
|
#include <units/format.h>
|
2022-04-02 21:36:42 +02:00
|
|
|
#include <units/math.h> // IWYU pragma: keep
|
2021-03-30 13:21:05 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <array>
|
|
|
|
|
#include <initializer_list>
|
|
|
|
|
#include <iterator>
|
|
|
|
|
#include <ostream>
|
|
|
|
|
#include <ranges>
|
2022-04-02 21:36:42 +02:00
|
|
|
#include <string> // IWYU pragma: keep
|
2021-03-30 13:21:05 +02:00
|
|
|
#include <vector>
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
// An example of a really simplified tactical glide computer
|
|
|
|
|
// Simplifications:
|
|
|
|
|
// - glider 100% clean and with full factory performance (brand new painting)
|
|
|
|
|
// - no influence of the ballast (pilot weight, water, etc) to glider performance
|
|
|
|
|
// - only one point on a glider polar curve
|
|
|
|
|
// - no influence of bank angle (during circling) on a glider performance
|
|
|
|
|
// - no wind
|
|
|
|
|
// - constant thermals strength
|
|
|
|
|
// - thermals exactly where and when we need them ;-)
|
|
|
|
|
// - no airspaces
|
|
|
|
|
// - ground level changes linearly between waypoints
|
|
|
|
|
// - no ground obstacles (i.e. mountains) to pass
|
|
|
|
|
// - flight path exactly on a shortest possible line to destination
|
|
|
|
|
|
|
|
|
|
template<units::QuantityKind QK>
|
2021-09-20 15:01:58 +02:00
|
|
|
struct STD_FMT::formatter<QK> : formatter<typename QK::quantity_type> {
|
2021-02-26 14:10:36 +01:00
|
|
|
template<typename FormatContext>
|
|
|
|
|
auto format(const QK& v, FormatContext& ctx)
|
|
|
|
|
{
|
|
|
|
|
return formatter<typename QK::quantity_type>::format(v.common(), ctx);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace glide_computer {
|
|
|
|
|
|
2021-03-30 13:21:05 +02:00
|
|
|
template<units::QuantityKind QK1, units::QuantityKind QK2>
|
|
|
|
|
constexpr units::Dimensionless auto operator/(const QK1& lhs, const QK2& rhs)
|
2022-04-02 21:36:42 +02:00
|
|
|
requires(!units::QuantityKindRelatedTo<QK1, QK2>) && requires { lhs.common() / rhs.common(); }
|
2021-02-26 14:10:36 +01:00
|
|
|
{
|
|
|
|
|
return lhs.common() / rhs.common();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// kinds
|
|
|
|
|
using horizontal_kind = geographic::horizontal_kind;
|
2021-03-30 13:21:05 +02:00
|
|
|
struct vertical_kind : units::kind<vertical_kind, units::isq::si::dim_length> {};
|
|
|
|
|
struct vertical_point_kind : units::point_kind<vertical_point_kind, vertical_kind> {};
|
2021-05-10 16:45:38 +02:00
|
|
|
struct velocity_kind : units::derived_kind<velocity_kind, units::isq::si::dim_speed, horizontal_kind> {};
|
|
|
|
|
struct rate_of_climb_kind : units::derived_kind<rate_of_climb_kind, units::isq::si::dim_speed, vertical_kind> {};
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
// https://en.wikipedia.org/wiki/Flight_planning#Units_of_measurement
|
|
|
|
|
// length
|
2021-03-30 13:21:05 +02:00
|
|
|
using distance = units::quantity_kind<horizontal_kind, units::isq::si::kilometre>;
|
|
|
|
|
using height = units::quantity_kind<vertical_kind, units::isq::si::metre>;
|
|
|
|
|
using altitude = units::quantity_point_kind<vertical_point_kind, units::isq::si::metre>;
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
// time
|
2021-03-30 13:21:05 +02:00
|
|
|
using duration = units::isq::si::time<units::isq::si::second>;
|
2021-06-27 18:35:21 -04:00
|
|
|
using timestamp = units::quantity_point<units::clock_origin<std::chrono::system_clock>, units::isq::si::second>;
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
// speed
|
2021-03-30 13:21:05 +02:00
|
|
|
using velocity = units::quantity_kind<velocity_kind, units::isq::si::kilometre_per_hour>;
|
|
|
|
|
using rate_of_climb = units::quantity_kind<rate_of_climb_kind, units::isq::si::metre_per_second>;
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
// text output
|
|
|
|
|
template<class CharT, class Traits>
|
|
|
|
|
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const altitude& a)
|
|
|
|
|
{
|
|
|
|
|
return os << a.relative().common() << " AMSL";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace glide_computer
|
|
|
|
|
|
|
|
|
|
template<>
|
2021-09-20 15:01:58 +02:00
|
|
|
struct STD_FMT::formatter<glide_computer::altitude> : formatter<units::isq::si::length<units::isq::si::metre>> {
|
2021-02-26 14:10:36 +01:00
|
|
|
template<typename FormatContext>
|
|
|
|
|
auto format(glide_computer::altitude a, FormatContext& ctx)
|
|
|
|
|
{
|
2021-03-16 12:03:25 +01:00
|
|
|
formatter<units::isq::si::length<units::isq::si::metre>>::format(a.relative().common(), ctx);
|
2021-09-20 15:01:58 +02:00
|
|
|
return STD_FMT::format_to(ctx.out(), " AMSL");
|
2021-02-26 14:10:36 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// definition of glide computer databases and utilities
|
|
|
|
|
namespace glide_computer {
|
|
|
|
|
|
|
|
|
|
struct glider {
|
|
|
|
|
struct polar_point {
|
|
|
|
|
velocity v;
|
|
|
|
|
rate_of_climb climb;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
|
std::array<polar_point, 1> polar;
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-30 13:21:05 +02:00
|
|
|
constexpr units::Dimensionless auto glide_ratio(const glider::polar_point& polar) { return polar.v / -polar.climb; }
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
struct weather {
|
|
|
|
|
height cloud_base;
|
|
|
|
|
rate_of_climb thermal_strength;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct waypoint {
|
|
|
|
|
std::string name;
|
2022-04-21 21:25:54 +02:00
|
|
|
geographic::position<long double> pos;
|
2021-02-26 14:10:36 +01:00
|
|
|
altitude alt;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class task {
|
|
|
|
|
public:
|
|
|
|
|
using waypoints = std::vector<waypoint>;
|
|
|
|
|
|
|
|
|
|
class leg {
|
|
|
|
|
const waypoint* begin_;
|
|
|
|
|
const waypoint* end_;
|
|
|
|
|
distance length_ = geographic::spherical_distance(begin().pos, end().pos);
|
|
|
|
|
public:
|
2022-04-02 21:36:42 +02:00
|
|
|
leg(const waypoint& b, const waypoint& e) noexcept : begin_(&b), end_(&e) {}
|
2021-02-26 14:10:36 +01:00
|
|
|
constexpr const waypoint& begin() const { return *begin_; };
|
|
|
|
|
constexpr const waypoint& end() const { return *end_; }
|
|
|
|
|
constexpr const distance get_length() const { return length_; }
|
|
|
|
|
};
|
|
|
|
|
using legs = std::vector<leg>;
|
|
|
|
|
|
|
|
|
|
template<std::ranges::input_range R>
|
2022-08-03 12:29:49 +02:00
|
|
|
requires std::same_as<std::ranges::range_value_t<R>, waypoint>
|
|
|
|
|
explicit task(const R& r) : waypoints_(std::ranges::begin(r), std::ranges::end(r))
|
2022-04-02 21:36:42 +02:00
|
|
|
{
|
|
|
|
|
}
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
task(std::initializer_list<waypoint> wpts) : waypoints_(wpts) {}
|
|
|
|
|
|
|
|
|
|
const waypoints& get_waypoints() const { return waypoints_; }
|
|
|
|
|
const legs& get_legs() const { return legs_; }
|
|
|
|
|
|
|
|
|
|
const waypoint& get_start() const { return waypoints_.front(); }
|
|
|
|
|
const waypoint& get_finish() const { return waypoints_.back(); }
|
|
|
|
|
|
|
|
|
|
distance get_length() const { return length_; }
|
|
|
|
|
|
2022-04-02 21:36:42 +02:00
|
|
|
distance get_leg_dist_offset(std::size_t leg_index) const
|
|
|
|
|
{
|
|
|
|
|
return leg_index == 0 ? distance{} : leg_total_distances_[leg_index - 1];
|
|
|
|
|
}
|
2021-03-30 13:21:05 +02:00
|
|
|
std::size_t get_leg_index(distance dist) const
|
2021-02-26 14:10:36 +01:00
|
|
|
{
|
2022-04-02 21:36:42 +02:00
|
|
|
return static_cast<std::size_t>(
|
|
|
|
|
std::ranges::distance(leg_total_distances_.cbegin(), std::ranges::lower_bound(leg_total_distances_, dist)));
|
2021-02-26 14:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
waypoints waypoints_;
|
|
|
|
|
legs legs_ = make_legs(waypoints_);
|
|
|
|
|
std::vector<distance> leg_total_distances_ = make_leg_total_distances(legs_);
|
|
|
|
|
distance length_ = leg_total_distances_.back();
|
|
|
|
|
|
|
|
|
|
static legs make_legs(const task::waypoints& wpts);
|
|
|
|
|
static std::vector<distance> make_leg_total_distances(const legs& legs);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct safety {
|
|
|
|
|
height min_agl_height;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct aircraft_tow {
|
|
|
|
|
height height_agl;
|
|
|
|
|
rate_of_climb performance;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct flight_point {
|
|
|
|
|
timestamp ts;
|
|
|
|
|
altitude alt;
|
2021-03-30 13:21:05 +02:00
|
|
|
std::size_t leg_idx = 0;
|
2021-03-12 23:06:11 +01:00
|
|
|
distance dist{};
|
2021-02-26 14:10:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
altitude terrain_level_alt(const task& t, const flight_point& pos);
|
|
|
|
|
|
|
|
|
|
constexpr height agl(altitude glider_alt, altitude terrain_level) { return glider_alt - terrain_level; }
|
|
|
|
|
|
2021-03-30 13:21:05 +02:00
|
|
|
inline units::isq::si::length<units::isq::si::kilometre> length_3d(distance dist, height h)
|
2021-02-26 14:10:36 +01:00
|
|
|
{
|
2022-08-03 12:29:49 +02:00
|
|
|
return hypot(dist.common(), h.common());
|
2021-02-26 14:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
distance glide_distance(const flight_point& pos, const glider& g, const task& t, const safety& s, altitude ground_alt);
|
|
|
|
|
|
2022-04-02 21:36:42 +02:00
|
|
|
void estimate(timestamp start_ts, const glider& g, const weather& w, const task& t, const safety& s,
|
|
|
|
|
const aircraft_tow& at);
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
} // namespace glide_computer
|