swift
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 
8 SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::aviation, COnGroundInfo)
9 
10 using namespace swift::misc::physical_quantities;
11 
12 namespace 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;
59  case OnGroundDetails::NotSetGroundDetails:
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) { m_onGroundFactor = -1.0; }
77  else if (m_onGroundFactor < 0.001) { m_onGroundFactor = 0.0; }
78  else if (m_onGroundFactor > 0.999) { m_onGroundFactor = 1.0; }
79  }
80 
81  COnGroundInfo::COnGroundInfo(const CLength &cg, const CLength &groundDistance)
82  {
83  m_onGroundDetails = static_cast<int>(OnGroundDetails::OnGroundByElevationAndCG);
84  if (groundDistance.isNull()) { m_onGroundFactor = -1.0; }
85  else if (groundDistance.isNegativeWithEpsilonConsidered()) { m_onGroundFactor = 1.0; }
86  else if (groundDistance.abs() < deltaNearGround()) { m_onGroundFactor = 1.0; }
87  else if (!cg.isNull())
88  {
89  // smaller than percentage from CG
90  const CLength cgFactor(cg * 0.1);
91  if (groundDistance.abs() < cgFactor.abs()) { m_onGroundFactor = 1.0; }
92  }
93  m_onGroundFactor = 0.0;
94  }
95 
96  bool COnGroundInfo::isOnGround() const
97  {
98  SWIFT_VERIFY_X(m_onGroundFactor >= 0.0, Q_FUNC_INFO, "Should only be called with positive groundfactors");
99  if (m_onGroundDetails == OnGroundDetails::OnGroundByInterpolation)
100  {
101  return m_onGroundFactor > m_groundFactorThreshold;
102  }
103  else { return math::CMathUtils::epsilonEqual(m_onGroundFactor, 1.0); }
104  }
105 
107  {
108  if (this->m_onGroundFactor < 0.0) { return OnGroundSituationUnknown; }
109 
110  const bool onGround = isOnGround();
111  return onGround ? OnGround : NotOnGround;
112  }
113 
115  {
116  return static_cast<COnGroundInfo::OnGroundDetails>(m_onGroundDetails);
117  }
118 
120  {
121  static const CLength small(0.5, CLengthUnit::m());
122  return small;
123  }
124 
125  QString COnGroundInfo::convertToQString(bool /*i18n*/) const
126  {
127  return u" | factor: " % QString::number(m_onGroundFactor, 'f', 2) % u" | source: " %
128  onGroundDetailsToString(static_cast<OnGroundDetails>(m_onGroundDetails));
129  }
130 
132  {
133  if (index.isMyself()) { return QVariant::fromValue(*this); }
134 
135  const ColumnIndex i = index.frontCasted<ColumnIndex>();
136  switch (i)
137  {
138  case IndexOnGroundFactor: return QVariant::fromValue(m_onGroundFactor);
139  case IndexOnGroundDetails: return QVariant::fromValue(m_onGroundDetails);
140  default: return CValueObject::propertyByIndex(index);
141  }
142  }
143 
144  void COnGroundInfo::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
145  {
146  if (index.isMyself())
147  {
148  (*this) = variant.value<COnGroundInfo>();
149  return;
150  }
151 
152  const ColumnIndex i = index.frontCasted<ColumnIndex>();
153  switch (i)
154  {
155  case IndexOnGroundFactor: m_onGroundFactor = variant.toDouble(); break;
156  case IndexOnGroundDetails: m_onGroundDetails = variant.toInt(); break;
157  default: CValueObject::setPropertyByIndex(index, variant); break;
158  }
159  }
160 } // 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.
Information about the ground status.
Definition: ongroundinfo.h:19
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.
Definition: ongroundinfo.h:31
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:42
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: mixinindex.h:160
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: mixinindex.h:167
static void registerMetadata()
Register metadata.
Definition: mixinmetatype.h:56
Physical unit length (length)
Definition: length.h:18
bool isNegativeWithEpsilonConsidered() const
Value <= 0 epsilon considered.
#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