Enroute Flight Navigation
A navigation app for VFR pilots
Speed.h
1/***************************************************************************
2 * Copyright (C) 2019-2021 by Stefan Kebekus *
3 * stefan.kebekus@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#pragma once
22
23#include <QDataStream>
24#include <QObject>
25#include <QQmlEngine>
26#include <QtMath>
27
28#include "units/Distance.h"
29#include "units/Timespan.h"
30
31
32namespace Units {
33
39 class Speed {
40 Q_GADGET
41 QML_VALUE_TYPE(speed)
42
43 public:
50 Q_INVOKABLE static constexpr Units::Speed fromFPM(double speedInFPM)
51 {
52 Speed result;
53 result._speedInMPS = speedInFPM/FPM_per_MPS;
54 return result;
55 }
56
63 Q_INVOKABLE static constexpr Units::Speed fromMPS(double speedInMPS)
64 {
65 Speed result;
66 result._speedInMPS = speedInMPS;
67 return result;
68 }
69
70
77 Q_INVOKABLE static constexpr Units::Speed fromMPH(double speedInMPH)
78 {
79 Speed result;
80 result._speedInMPS = speedInMPH*MPS_per_MPH;
81 return result;
82 }
83
90 Q_INVOKABLE static constexpr Units::Speed fromKN(double speedInKT)
91 {
92 Speed result;
93 result._speedInMPS = speedInKT / KN_per_MPS;
94 return result;
95 }
96
103 Q_INVOKABLE static constexpr Units::Speed fromKMH(double speedInKMH)
104 {
105 Speed result;
106 result._speedInMPS = speedInKMH / KMH_per_MPS;
107 return result;
108 }
109
114 [[nodiscard]] Q_INVOKABLE bool isFinite() const
115 {
116 return std::isfinite(_speedInMPS);
117 }
118
123 [[nodiscard]] Q_INVOKABLE bool isNegative() const
124 {
125 return _speedInMPS < 0.0;
126 }
127
134 Q_INVOKABLE double operator/(Units::Speed rhs) const
135 {
136 if (qFuzzyIsNull(rhs._speedInMPS)) {
137 return qQNaN();
138}
139 return _speedInMPS / rhs._speedInMPS;
140 }
141
148 Q_INVOKABLE Units::Speed operator+(Units::Speed rhs) const
149 {
150 return Units::Speed::fromMPS(_speedInMPS + rhs._speedInMPS);
151 }
152
159 Q_INVOKABLE Units::Speed operator-(Units::Speed rhs) const
160 {
161 return Units::Speed::fromMPS(_speedInMPS - rhs._speedInMPS);
162 }
163
170 [[nodiscard]] Q_INVOKABLE std::partial_ordering operator<=>(const Units::Speed& rhs) const = default;
171
176 [[nodiscard]] Q_INVOKABLE double toFPM() const
177 {
178 return _speedInMPS * FPM_per_MPS;
179 }
180
185 [[nodiscard]] Q_INVOKABLE double toMPS() const
186 {
187 return _speedInMPS;
188 }
189
194 [[nodiscard]] Q_INVOKABLE double toMPH() const
195 {
196 return _speedInMPS/MPS_per_MPH;
197 }
198
203 [[nodiscard]] Q_INVOKABLE double toKN() const
204 {
205 return _speedInMPS * KN_per_MPS;
206 }
207
212 [[nodiscard]] Q_INVOKABLE double toKMH() const
213 {
214 return _speedInMPS * KMH_per_MPS;
215 }
216
218 static constexpr double FPM_per_MPS = 196.85039370079;
219
221 static constexpr double MPS_per_MPH = 0.44704;
222
224 static constexpr double KMH_per_MPS = 3.6;
225
227 static constexpr double KN_per_MPS = 1.943844;
228
229
230
232 static constexpr double KMH_per_KT = KMH_per_MPS / KN_per_MPS;
233
234 private:
235 // Speed in meters per second
236 double _speedInMPS{ NAN };
237 };
238
239} // namespace Units
240
241
242//
243// Operations
244//
245
254inline Units::Speed operator*(double lhs, Units::Speed rhs)
255{
256 return Units::Speed::fromMPS(lhs*rhs.toMPS());
257}
258
267inline Units::Distance operator*(Units::Timespan lhs, Units::Speed rhs)
268{
269 return Units::Distance::fromM(lhs.toS() * rhs.toMPS());
270}
271
280inline Units::Distance operator*(Units::Speed lhs, Units::Timespan rhs)
281{
282 return Units::Distance::fromM(rhs.toS() * lhs.toMPS());
283}
284
293QDataStream & operator<<(QDataStream &out, Units::Speed speed);
294
303auto operator>>(QDataStream &in, Units::Speed &speed) -> QDataStream &;
304
305
306// Declare meta types
307Q_DECLARE_METATYPE(Units::Speed)
Convenience class for distance computations.
Definition Distance.h:35
static Q_INVOKABLE constexpr Units::Distance fromM(double distanceInM)
Constructs a distance.
Definition Distance.h:56
Convenience class for speed computations.
Definition Speed.h:39
static constexpr double MPS_per_MPH
Unitless constant: one mile per hour / meters per second.
Definition Speed.h:221
static Q_INVOKABLE constexpr Units::Speed fromMPS(double speedInMPS)
Constructs a speed.
Definition Speed.h:63
static constexpr double KMH_per_KT
Unitless constant: one km/h / knot.
Definition Speed.h:232
static constexpr double KN_per_MPS
Unitless constant: one knot / meters per second.
Definition Speed.h:227
static constexpr double FPM_per_MPS
Unitless constant: one feet per minute / meters per second.
Definition Speed.h:218
Q_INVOKABLE bool isFinite() const
Checks if the speed is valid.
Definition Speed.h:114
Q_INVOKABLE double toFPM() const
Convert to feet per minute.
Definition Speed.h:176
static Q_INVOKABLE constexpr Units::Speed fromKMH(double speedInKMH)
Constructs a speed.
Definition Speed.h:103
static Q_INVOKABLE constexpr Units::Speed fromMPH(double speedInMPH)
Constructs a speed.
Definition Speed.h:77
Q_INVOKABLE std::partial_ordering operator<=>(const Units::Speed &rhs) const =default
Comparison.
static constexpr double KMH_per_MPS
Unitless constant: one km/h / meters per second.
Definition Speed.h:224
Q_INVOKABLE double toMPS() const
Convert to meters per second.
Definition Speed.h:185
Q_INVOKABLE double toKN() const
Convert to knots.
Definition Speed.h:203
static Q_INVOKABLE constexpr Units::Speed fromFPM(double speedInFPM)
Constructs a speed.
Definition Speed.h:50
Q_INVOKABLE Units::Speed operator-(Units::Speed rhs) const
Difference of two speeds.
Definition Speed.h:159
Q_INVOKABLE bool isNegative() const
Checks if the speed is negative.
Definition Speed.h:123
static Q_INVOKABLE constexpr Units::Speed fromKN(double speedInKT)
Constructs a speed.
Definition Speed.h:90
Q_INVOKABLE double toKMH() const
Convert to km/h.
Definition Speed.h:212
Q_INVOKABLE Units::Speed operator+(Units::Speed rhs) const
Adds two speeds.
Definition Speed.h:148
Q_INVOKABLE double operator/(Units::Speed rhs) const
Divides two speeds.
Definition Speed.h:134
Q_INVOKABLE double toMPH() const
Convert to meters per second.
Definition Speed.h:194
Convenience class for time computations.
Definition Timespan.h:35
Q_INVOKABLE double toS() const
Convert time to seconds.
Definition Timespan.h:119
Conversion between units used in aviation.
Definition Angle.h:34