swift
Loading...
Searching...
No Matches
ongroundinfo.cpp
1// SPDX-FileCopyrightText: Copyright (C) swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include "misc/verify.h"
7
8SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::aviation, COnGroundInfo)
9
10using namespace swift::misc::physical_quantities;
11
12namespace swift::misc::aviation
13{
14
15 COnGroundInfo::COnGroundInfo(IsOnGround onGround, OnGroundDetails details)
16 : m_onGroundDetails(static_cast<int>(details))
17 {
18 switch (onGround)
19 {
20 case IsOnGround::OnGroundSituationUnknown: m_onGroundFactor = -1.0; break;
21 case IsOnGround::OnGround: m_onGroundFactor = 1.0; break;
22 case IsOnGround::NotOnGround: m_onGroundFactor = 0.0; break;
23 }
24 }
25
27 {
28 static const QString notog("not on ground");
29 static const QString og("on ground");
30 static const QString unknown("unknown");
31
32 switch (onGround)
33 {
34 case IsOnGround::NotOnGround: return notog;
35 case IsOnGround::OnGround: return og;
36 case IsOnGround::OnGroundSituationUnknown:
37 default: return unknown;
38 }
39 }
40
42 {
43 static const QString elvCg("elevation/CG");
44 static const QString interpolation("interpolation");
45 static const QString guess("guessing");
46 static const QString unknown("unknown");
47 static const QString outOwnAircraft("own aircraft");
48 static const QString inNetwork("from network");
49 static const QString inFromParts("from parts");
50
51 switch (reliability)
52 {
53 case OnGroundDetails::OnGroundByElevationAndCG: return elvCg;
54 case OnGroundDetails::OnGroundByGuessing: return guess;
55 case OnGroundDetails::OnGroundByInterpolation: return interpolation;
56 case OnGroundDetails::OutOnGroundOwnAircraft: return outOwnAircraft;
57 case OnGroundDetails::InFromNetwork: return inNetwork;
58 case OnGroundDetails::InFromParts: return inFromParts;
60 default: return unknown;
61 }
62 }
63
65 {
67 qRegisterMetaType<IsOnGround>();
68 qRegisterMetaType<OnGroundDetails>();
69 }
70
71 COnGroundInfo::COnGroundInfo(double interpolatedGndFactor)
72 : m_onGroundDetails(static_cast<int>(OnGroundDetails::OnGroundByInterpolation)),
73 m_onGroundFactor(interpolatedGndFactor)
74 {
75 // Clip small ground factor values
76 if (m_onGroundFactor < -0.1)
77 {
78 m_onGroundFactor = -1.0;
79 }
80 else if (m_onGroundFactor < 0.001)
81 {
82 m_onGroundFactor = 0.0;
83 }
84 else if (m_onGroundFactor > 0.999)
85 {
86 m_onGroundFactor = 1.0;
87 }
88 }
89
90 COnGroundInfo::COnGroundInfo(const CLength &cg, const CLength &groundDistance)
91 {
92 m_onGroundDetails = static_cast<int>(OnGroundDetails::OnGroundByElevationAndCG);
93 if (groundDistance.isNull())
94 {
95 m_onGroundFactor = -1.0;
96 }
97 else if (groundDistance.isNegativeWithEpsilonConsidered())
98 {
99 m_onGroundFactor = 1.0;
100 }
101 else if (groundDistance.abs() < deltaNearGround())
102 {
103 m_onGroundFactor = 1.0;
104 }
105 else if (!cg.isNull())
106 {
107 // smaller than percentage from CG
108 const CLength cgFactor(cg * 0.1);
109 if (groundDistance.abs() < cgFactor.abs())
110 {
111 m_onGroundFactor = 1.0;
112 }
113 }
114 m_onGroundFactor = 0.0;
115 }
116
117 bool COnGroundInfo::isOnGround() const
118 {
119 SWIFT_VERIFY_X(m_onGroundFactor >= 0.0, Q_FUNC_INFO, "Should only be called with positive groundfactors");
120 if (m_onGroundDetails == OnGroundDetails::OnGroundByInterpolation)
121 {
122 return m_onGroundFactor > m_groundFactorThreshold;
123 }
124 else
125 {
126 return math::CMathUtils::epsilonEqual(m_onGroundFactor, 1.0);
127 }
128 }
129
131 {
132 if (this->m_onGroundFactor < 0.0)
133 {
134 return OnGroundSituationUnknown;
135 }
136
137 const bool onGround = isOnGround();
138 return onGround ? OnGround : NotOnGround;
139 }
140
142 {
143 return static_cast<COnGroundInfo::OnGroundDetails>(m_onGroundDetails);
144 }
145
147 {
148 static const CLength small(0.5, CLengthUnit::m());
149 return small;
150 }
151
153 {
154 return u" | factor: " % QString::number(m_onGroundFactor, 'f', 2) % u" | source: " %
155 onGroundDetailsToString(static_cast<OnGroundDetails>(m_onGroundDetails));
156 }
157
159 {
160 if (index.isMyself())
161 {
162 return QVariant::fromValue(*this);
163 }
164
165 const auto i = index.frontCasted<ColumnIndex>();
166 switch (i)
167 {
168 case IndexOnGroundFactor: return QVariant::fromValue(m_onGroundFactor);
169 case IndexOnGroundDetails: return QVariant::fromValue(m_onGroundDetails);
170 default: return CValueObject::propertyByIndex(index);
171 }
172 }
173
175 {
176 if (index.isMyself())
177 {
178 (*this) = variant.value<COnGroundInfo>();
179 return;
180 }
181
182 const auto i = index.frontCasted<ColumnIndex>();
183 switch (i)
184 {
185 case IndexOnGroundFactor: m_onGroundFactor = variant.toDouble(); break;
186 case IndexOnGroundDetails: m_onGroundDetails = variant.toInt(); break;
187 default: CValueObject::setPropertyByIndex(index, variant); break;
188 }
189 }
190} // namespace swift::misc::aviation
Non-owning reference to a CPropertyIndex with a subset of its features.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
static void registerMetadata()
Register metadata.
static const QString & isOnGroundToString(IsOnGround onGround)
Enum to string.
OnGroundDetails
Reliability of on ground information.
@ InFromParts
set from aircraft parts
@ InFromNetwork
received from network
@ OutOnGroundOwnAircraft
sending on ground
@ OnGroundByInterpolation
strongest for remote aircraft
@ NotSetGroundDetails
not set or unknown if correct (e.g.
QString convertToQString(bool i18n=false) const
Cast as QString.
IsOnGround getOnGround() const
Is on ground?
OnGroundDetails getGroundDetails() const
Get ground details.
static const physical_quantities::CLength & deltaNearGround()
Delta distance, near to ground.
static const QString & onGroundDetailsToString(OnGroundDetails reliability)
Enum to string.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
static bool epsilonEqual(float v1, float v2, float epsilon=1E-06f)
Epsilon safe equal.
Definition mathutils.cpp:51
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition mixinindex.h:167
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition mixinindex.h:180
static void registerMetadata()
Register metadata.
Physical unit length (length).
Definition length.h:18
static CLengthUnit m()
Meter m.
Definition units.h:140
bool isNegativeWithEpsilonConsidered() const
Value <= 0 epsilon considered.
QString number(double n, char format, int precision)
QVariant fromValue(T &&value)
double toDouble(bool *ok) const const
int toInt(bool *ok) const const
T value() const &const
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition valueobject.h:67
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.
Definition verify.h:26