openPilotLog
acalc.h
1 /*
2  *openPilotLog - A FOSS Pilot Logbook Application
3  *Copyright (C) 2020-2021 Felix Turowsky
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, see <https://www.gnu.org/licenses/>.
17  */
18 #ifndef ACALC_H
19 #define ACALC_H
20 
21 #ifdef _MSC_VER
22 #define _USE_MATH_DEFINES
23 #endif
24 #include <cmath>
25 #include <QDateTime>
26 #include <QDebug>
27 #include "src/functions/alog.h"
28 #include "src/functions/atime.h"
35 namespace ACalc {
36 
43 QT_DEPRECATED
44 inline QTime blocktime(QTime tofb, QTime tonb)
45 {
46  QTime blocktime_out(0, 0); // initialise return value at midnight
47 
48  if (tonb > tofb) { // landing same day
49  int blockseconds = tofb.secsTo(tonb);
50  blocktime_out = blocktime_out.addSecs(blockseconds);
51  } else { // landing next day
52  QTime midnight(0, 0);
53  int blockseconds = tofb.secsTo(midnight);
54  blocktime_out = blocktime_out.addSecs(blockseconds);
55  blockseconds = midnight.secsTo(tonb);
56  blocktime_out = blocktime_out.addSecs(blockseconds);
57  }
58  return blocktime_out;
59 }
65 QT_DEPRECATED
66 inline QString minutesToString(QString block_minutes)
67 {
68  int minutes = block_minutes.toInt();
69  QString hour = QString::number(minutes / 60);
70  if (hour.size() < 2) {
71  hour.prepend("0");
72  }
73  QString minute = QString::number(minutes % 60);
74  if (minute.size() < 2) {
75  minute.prepend("0");
76  }
77  QString block_time = hour + ":" + minute;
78  return block_time;
79 };
80 
81 QT_DEPRECATED
82 inline QString minutesToString(int block_minutes)
83 {
84  QString hour = QString::number(block_minutes / 60);
85  if (hour.size() < 2) {
86  hour.prepend("0");
87  }
88  QString minute = QString::number(block_minutes % 60);
89  if (minute.size() < 2) {
90  minute.prepend("0");
91  }
92  QString blocktime = hour + ":" + minute;
93  return blocktime;
94 };
95 
101 QT_DEPRECATED
102 inline int QTimeToMinutes(QTime time)
103 {
104  QString timestring = time.toString("hh:mm");
105  int minutes = (timestring.left(2).toInt()) * 60;
106  minutes += timestring.right(2).toInt();
107  return minutes;
108 }
109 
115 QT_DEPRECATED
116 inline int stringToMinutes(QString timestring)
117 {
118  int minutes = (timestring.left(2).toInt()) * 60;
119  minutes += timestring.right(2).toInt();
120  timestring = QString::number(minutes);
121  return minutes;
122 }
123 
129 inline double radToDeg(double rad)
130 {
131  double deg = rad * (180 / M_PI);
132  return deg;
133 }
134 
140 inline double degToRad(double deg)
141 {
142  double rad = deg * (M_PI / 180);
143  return rad;
144 }
145 
151 inline double radToNauticalMiles(double rad)
152 {
153  double nm = rad * 3440.06479482;
154  return nm;
155 }
156 
165 double greatCircleDistance(double lat1, double lon1, double lat2, double lon2);
166 
174 double greatCircleDistanceBetweenAirports(const QString &dept, const QString &dest);
175 
186 QVector<QVector<double>> intermediatePointsOnGreatCircle(double lat1,
187  double lon1,
188  double lat2,
189  double lon2,
190  int tblk);
207 double solarElevation(QDateTime utc_time_point, double lat, double lon);
208 
219 int calculateNightTime(const QString &dept, const QString &dest, QDateTime departureTime, int tblk, int nightAngle);
220 
221 bool isNight(const QString &icao, QDateTime event_time, int night_angle);
222 
223 QString formatTimeInput(QString user_input);
224 
225 void updateAutoTimes(int acft_id);
226 
227 void updateNightTimes();
228 
233  NightTimeValues() = delete;
234  NightTimeValues(const QString& dept, const QString& dest, const QDateTime& departure_time, int block_minutes, int night_angle)
235  {
236  nightMinutes = calculateNightTime(dept, dest, departure_time, block_minutes, night_angle);
237 
238  nightTime = ATime::qTimefromMinutes(nightMinutes);
239  totalTime = ATime::qTimefromMinutes(block_minutes);
240 
241  if (nightMinutes == 0) { // all day
242  takeOffNight = false;
243  landingNight = false;
244  }
245  else if (nightMinutes == block_minutes) { // all night
246  takeOffNight = true;
247  landingNight = true;
248  } else {
249  if(isNight(dept, departure_time, night_angle))
250  takeOffNight = true;
251  else
252  takeOffNight = false;
253  if(isNight(dest, departure_time.addSecs(block_minutes * 60), night_angle))
254  landingNight = true;
255  else
256  landingNight = false;
257  }
258 
259  };
260  NightTimeValues(bool to_night, bool ldg_night, int night_minutes, QTime night_time, QTime total_time)
261  : takeOffNight(to_night), landingNight(ldg_night), nightMinutes(night_minutes), nightTime(night_time), totalTime(total_time){};
262  bool takeOffNight;
263  bool landingNight;
264  int nightMinutes;
265  QTime nightTime;
266  QTime totalTime;
267 
268  inline bool isAllDay() {return (!takeOffNight && !landingNight);}
269  inline bool isAllNight() {return ( takeOffNight && landingNight);}
270  inline bool isDayToNight() {return (!takeOffNight && landingNight);}
271  inline bool isNightToDay() {return ( takeOffNight && !landingNight);}
272 };
273 
274 
275 } // namespace ACalc
276 
277 #endif // ACALC_H
The ACalc namespace provides various functions for calculations that are performed outside of the dat...
Definition: acalc.h:35
double radToDeg(double rad)
radToDeg Converts radians to degrees
Definition: acalc.h:129
double greatCircleDistanceBetweenAirports(const QString &dept, const QString &dest)
ACalc::greatCircleDistanceBetweenAirports Calculates Great Circle distance between two coordinates,...
Definition: acalc.cpp:107
double radToNauticalMiles(double rad)
radToNauticalMiles Convert Radians to nautical miles
Definition: acalc.h:151
void updateAutoTimes(int acft_id)
ACalc::updateAutoTimes When the details of an aircraft are changed, this function recalculates deduct...
Definition: acalc.cpp:305
QVector< QVector< double > > intermediatePointsOnGreatCircle(double lat1, double lon1, double lat2, double lon2, int tblk)
Calculates a list of points (lat,lon) along the Great Circle between two points. The points are space...
Definition: acalc.cpp:137
QT_DEPRECATED int stringToMinutes(QString timestring)
ACalc::string_to_minutes Converts String Time to String Number of Minutes.
Definition: acalc.h:116
int calculateNightTime(const QString &dept, const QString &dest, QDateTime departureTime, int tblk, int nightAngle)
Calculates which portion of a flight was conducted in night conditions.
Definition: acalc.cpp:230
double greatCircleDistance(double lat1, double lon1, double lat2, double lon2)
greatCircleDistance Calculates Great Circle distance between two coordinates, return in Radians.
Definition: acalc.cpp:88
QT_DEPRECATED QTime blocktime(QTime tofb, QTime tonb)
ACalc::blocktime Calculates Block Time for a given departure and arrival time.
Definition: acalc.h:44
QT_DEPRECATED int QTimeToMinutes(QTime time)
ACalc::time_to_minutes converts QTime to int minutes.
Definition: acalc.h:102
QT_DEPRECATED QString minutesToString(QString block_minutes)
ACalc::minutes_to_string Converts database time to String Time.
Definition: acalc.h:66
double degToRad(double deg)
degToRad Converts degrees to radians
Definition: acalc.h:140
double solarElevation(QDateTime utc_time_point, double lat, double lon)
Calculates solar elevation angle for a given point in time and latitude/longitude coordinates.
Definition: acalc.cpp:171
QString formatTimeInput(QString user_input)
ACalc::formatTimeInput verifies user input and formats to hh:mm if the output is not a valid time,...
Definition: acalc.cpp:35
void updateNightTimes()
ACalc::updateNightTimes updates the night times in the database, used when changing night angle setti...
Definition: acalc.cpp:348
The NightTimeValues struct encapsulates values relating to night time that are needed by the NewFligh...
Definition: acalc.h:232