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"
34 namespace ACalc {
35 
42 QT_DEPRECATED
43 inline QTime blocktime(QTime tofb, QTime tonb)
44 {
45  QTime blocktime_out(0, 0); // initialise return value at midnight
46 
47  if (tonb > tofb) { // landing same day
48  int blockseconds = tofb.secsTo(tonb);
49  blocktime_out = blocktime_out.addSecs(blockseconds);
50  } else { // landing next day
51  QTime midnight(0, 0);
52  int blockseconds = tofb.secsTo(midnight);
53  blocktime_out = blocktime_out.addSecs(blockseconds);
54  blockseconds = midnight.secsTo(tonb);
55  blocktime_out = blocktime_out.addSecs(blockseconds);
56  }
57  return blocktime_out;
58 }
64 QT_DEPRECATED
65 inline QString minutesToString(QString block_minutes)
66 {
67  int minutes = block_minutes.toInt();
68  QString hour = QString::number(minutes / 60);
69  if (hour.size() < 2) {
70  hour.prepend("0");
71  }
72  QString minute = QString::number(minutes % 60);
73  if (minute.size() < 2) {
74  minute.prepend("0");
75  }
76  QString block_time = hour + ":" + minute;
77  return block_time;
78 };
79 
80 QT_DEPRECATED
81 inline QString minutesToString(int block_minutes)
82 {
83  QString hour = QString::number(block_minutes / 60);
84  if (hour.size() < 2) {
85  hour.prepend("0");
86  }
87  QString minute = QString::number(block_minutes % 60);
88  if (minute.size() < 2) {
89  minute.prepend("0");
90  }
91  QString blocktime = hour + ":" + minute;
92  return blocktime;
93 };
94 
100 QT_DEPRECATED
101 inline int QTimeToMinutes(QTime time)
102 {
103  QString timestring = time.toString("hh:mm");
104  int minutes = (timestring.left(2).toInt()) * 60;
105  minutes += timestring.right(2).toInt();
106  return minutes;
107 }
108 
114 QT_DEPRECATED
115 inline int stringToMinutes(QString timestring)
116 {
117  int minutes = (timestring.left(2).toInt()) * 60;
118  minutes += timestring.right(2).toInt();
119  timestring = QString::number(minutes);
120  return minutes;
121 }
122 
128 inline double radToDeg(double rad)
129 {
130  double deg = rad * (180 / M_PI);
131  return deg;
132 }
133 
139 inline double degToRad(double deg)
140 {
141  double rad = deg * (M_PI / 180);
142  return rad;
143 }
144 
150 inline double radToNauticalMiles(double rad)
151 {
152  double nm = rad * 3440.06479482;
153  return nm;
154 }
155 
164 double greatCircleDistance(double lat1, double lon1, double lat2, double lon2);
165 
173 double greatCircleDistanceBetweenAirports(const QString &dept, const QString &dest);
174 
185 QVector<QVector<double>> intermediatePointsOnGreatCircle(double lat1,
186  double lon1,
187  double lat2,
188  double lon2,
189  int tblk);
206 double solarElevation(QDateTime utc_time_point, double lat, double lon);
207 
218 int calculateNightTime(const QString &dept, const QString &dest, QDateTime departureTime, int tblk, int nightAngle);
219 
220 bool isNight(const QString &icao, QDateTime event_time, int night_angle);
221 
222 QString formatTimeInput(QString user_input);
223 
224 void updateAutoTimes(int acft_id);
225 
226 void updateNightTimes();
227 } // namespace ACalc
228 
229 #endif // ACALC_H
ACalc::intermediatePointsOnGreatCircle
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:136
ACalc::QTimeToMinutes
QT_DEPRECATED int QTimeToMinutes(QTime time)
ACalc::time_to_minutes converts QTime to int minutes.
Definition: acalc.h:101
ACalc::updateAutoTimes
void updateAutoTimes(int acft_id)
ACalc::updateAutoTimes When the details of an aircraft are changed, this function recalculates deduct...
Definition: acalc.cpp:288
ACalc::minutesToString
QT_DEPRECATED QString minutesToString(QString block_minutes)
ACalc::minutes_to_string Converts database time to String Time.
Definition: acalc.h:65
ACalc::radToNauticalMiles
double radToNauticalMiles(double rad)
radToNauticalMiles Convert Radians to nautical miles
Definition: acalc.h:150
ACalc
The ACalc namespace provides various functions for calculations that are performed outside of the dat...
Definition: acalc.h:34
ACalc::blocktime
QT_DEPRECATED QTime blocktime(QTime tofb, QTime tonb)
ACalc::blocktime Calculates Block Time for a given departure and arrival time.
Definition: acalc.h:43
ACalc::formatTimeInput
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:34
ACalc::degToRad
double degToRad(double deg)
degToRad Converts degrees to radians
Definition: acalc.h:139
ACalc::radToDeg
double radToDeg(double rad)
radToDeg Converts radians to degrees
Definition: acalc.h:128
ACalc::greatCircleDistanceBetweenAirports
double greatCircleDistanceBetweenAirports(const QString &dept, const QString &dest)
ACalc::greatCircleDistanceBetweenAirports Calculates Great Circle distance between two coordinates,...
Definition: acalc.cpp:106
ACalc::solarElevation
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:167
ACalc::stringToMinutes
QT_DEPRECATED int stringToMinutes(QString timestring)
ACalc::string_to_minutes Converts String Time to String Number of Minutes.
Definition: acalc.h:115
ACalc::greatCircleDistance
double greatCircleDistance(double lat1, double lon1, double lat2, double lon2)
greatCircleDistance Calculates Great Circle distance between two coordinates, return in Radians.
Definition: acalc.cpp:87
ACalc::updateNightTimes
void updateNightTimes()
ACalc::updateNightTimes updates the night times in the database, used when changing night angle setti...
Definition: acalc.cpp:331
ACalc::calculateNightTime
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:227