Enroute Flight Navigation
A navigation app for VFR pilots
Distance.h
1/***************************************************************************
2 * Copyright (C) 2019-2023 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 <QObject>
24#include <QQmlEngine>
25#include <QtMath>
26
27
28namespace Units {
29
35 class Distance {
36 Q_GADGET
37 QML_VALUE_TYPE(distance)
38
39 public:
41 enum DistanceUnit : quint8 {
42 Feet,
43 Meter,
44 Kilometer,
45 StatuteMile,
46 NauticalMile,
47 };
48 Q_ENUM(DistanceUnit)
49
50
56 Q_INVOKABLE static constexpr Units::Distance fromM(double distanceInM)
57 {
58 Distance result;
59 result.m_distanceInM = distanceInM;
60 return result;
61 }
62
69 Q_INVOKABLE static constexpr Units::Distance fromKM(double distanceInKM)
70 {
71 Distance result;
72 result.m_distanceInM = 1000.0*distanceInKM;
73 return result;
74 }
75
82 Q_INVOKABLE static constexpr Units::Distance fromNM(double distanceInNM)
83 {
84 Distance result;
85 result.m_distanceInM = distanceInNM*MetersPerNauticalMile;
86 return result;
87 }
88
95 Q_INVOKABLE static constexpr Units::Distance fromMIL(double distanceInMIL)
96 {
97 Distance result;
98 result.m_distanceInM = distanceInMIL*MetersPerMile;
99 return result;
100 }
101
108 Q_INVOKABLE static constexpr Units::Distance fromFT(double distanceInFT)
109 {
110 Distance result;
111 result.m_distanceInM = distanceInFT * MetersPerFeet;
112 return result;
113 }
114
119 Q_INVOKABLE static constexpr Units::Distance nan()
120 {
121 return {};
122 }
123
131 {
132 m_distanceInM += other.m_distanceInM;
133 return *this;
134 }
135
140 [[nodiscard]] Q_INVOKABLE bool isFinite() const
141 {
142 return std::isfinite(m_distanceInM);
143 }
144
149 [[nodiscard]] Q_INVOKABLE bool isNegative() const
150 {
151 return m_distanceInM < 0.0;
152 }
153
160 [[nodiscard]] Q_INVOKABLE Units::Distance operator+(Units::Distance rhs) const
161 {
162 return fromM(m_distanceInM + rhs.m_distanceInM);
163 }
164
171 [[nodiscard]] Q_INVOKABLE Units::Distance operator-(Units::Distance rhs) const
172 {
173 return fromM(m_distanceInM - rhs.m_distanceInM);
174 }
175
182 [[nodiscard]] Q_INVOKABLE Units::Distance subtract(Units::Distance rhs) const
183 {
184 return fromM(m_distanceInM - rhs.m_distanceInM);
185 }
186
193 [[nodiscard]] Q_INVOKABLE Units::Distance operator*(double factor) const
194 {
195 return fromM(m_distanceInM*factor);
196 }
197
204 [[nodiscard]] Q_INVOKABLE std::partial_ordering operator<=>(const Units::Distance& rhs) const = default;
205
210 [[nodiscard]] Q_INVOKABLE double toFeet() const
211 {
212 return m_distanceInM / MetersPerFeet;
213 }
214
219 [[nodiscard]] Q_INVOKABLE double toKM() const
220 {
221 return m_distanceInM / 1000.;
222 }
223
228 [[nodiscard]] Q_INVOKABLE double toM() const
229 {
230 return m_distanceInM;
231 }
232
237 [[nodiscard]] Q_INVOKABLE double toMIL() const
238 {
239 return m_distanceInM / MetersPerMile;
240 }
241
246 [[nodiscard]] Q_INVOKABLE double toNM() const
247 {
248 return m_distanceInM / MetersPerNauticalMile;
249 }
250
266 [[nodiscard]] Q_INVOKABLE QString toString(Units::Distance::DistanceUnit units, bool roundBigNumbers=false, bool forceSign=false) const;
267
268 private:
269 static constexpr double MetersPerFeet = 0.3048;
270 static constexpr double MetersPerNauticalMile = 1852;
271 static constexpr double MetersPerMile = 1609.344;
272
273 // Speed in meters per second
274 double m_distanceInM{ NAN };
275 };
276} // namespace Units
277
278
280QDataStream& operator<<(QDataStream& stream, const Units::Distance& distance);
281
283QDataStream& operator>>(QDataStream& stream, Units::Distance& distance);
284
285// Declare meta types
286Q_DECLARE_METATYPE(Units::Distance)
Convenience class for distance computations.
Definition Distance.h:35
static Q_INVOKABLE constexpr Units::Distance fromNM(double distanceInNM)
Constructs a distance.
Definition Distance.h:82
static Q_INVOKABLE constexpr Units::Distance fromFT(double distanceInFT)
Constructs a distance.
Definition Distance.h:108
static Q_INVOKABLE constexpr Units::Distance nan()
Constructs an invalid distance.
Definition Distance.h:119
Q_INVOKABLE double toFeet() const
Convert to feet.
Definition Distance.h:210
static Q_INVOKABLE constexpr Units::Distance fromM(double distanceInM)
Constructs a distance.
Definition Distance.h:56
DistanceUnit
Units of measurement for distances.
Definition Distance.h:41
Q_INVOKABLE Units::Distance subtract(Units::Distance rhs) const
Subtraction.
Definition Distance.h:182
Q_INVOKABLE Units::Distance operator+(Units::Distance rhs) const
Addition.
Definition Distance.h:160
Q_INVOKABLE Units::Distance operator*(double factor) const
Scaling.
Definition Distance.h:193
Q_INVOKABLE double toM() const
Convert to meters.
Definition Distance.h:228
static Q_INVOKABLE constexpr Units::Distance fromKM(double distanceInKM)
Constructs a distance.
Definition Distance.h:69
Q_INVOKABLE Units::Distance & operator+=(Units::Distance other)
Add distance to this distance.
Definition Distance.h:130
Q_INVOKABLE QString toString(Units::Distance::DistanceUnit units, bool roundBigNumbers=false, bool forceSign=false) const
Convert to string.
Q_INVOKABLE Units::Distance operator-(Units::Distance rhs) const
Subtraction.
Definition Distance.h:171
Q_INVOKABLE bool isNegative() const
Checks if the distance is negative.
Definition Distance.h:149
Q_INVOKABLE double toKM() const
Convert to meters.
Definition Distance.h:219
Q_INVOKABLE std::partial_ordering operator<=>(const Units::Distance &rhs) const =default
Comparison.
static Q_INVOKABLE constexpr Units::Distance fromMIL(double distanceInMIL)
Constructs a distance.
Definition Distance.h:95
Q_INVOKABLE bool isFinite() const
Checks if the distance is valid.
Definition Distance.h:140
Q_INVOKABLE double toNM() const
Convert to nautical miles.
Definition Distance.h:246
Q_INVOKABLE double toMIL() const
Convert to international miles.
Definition Distance.h:237
Conversion between units used in aviation.
Definition Angle.h:34