openPilotLog
calc.h
1 /*
2  *openPilotLog - A FOSS Pilot Logbook Application
3  *Copyright (C) 2020-2022 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 CALC_H
19 #define CALC_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/time.h"
34 namespace OPL::Calc {
35 
41 inline double radToDeg(double rad)
42 {
43  double deg = rad * (180 / M_PI);
44  return deg;
45 }
46 
52 inline double degToRad(double deg)
53 {
54  double rad = deg * (M_PI / 180);
55  return rad;
56 }
57 
63 inline double radToNauticalMiles(double rad)
64 {
65  double nm = rad * 3440.06479482;
66  return nm;
67 }
68 
77 double greatCircleDistance(double lat1, double lon1, double lat2, double lon2);
78 
86 double greatCircleDistanceBetweenAirports(const QString &dept, const QString &dest);
87 
98 QVector<QVector<double>> intermediatePointsOnGreatCircle(double lat1,
99  double lon1,
100  double lat2,
101  double lon2,
102  int tblk);
119 double solarElevation(QDateTime utc_time_point, double lat, double lon);
120 
131 int calculateNightTime(const QString &dept, const QString &dest, QDateTime departureTime, int tblk, int nightAngle);
132 
133 bool isNight(const QString &icao, QDateTime event_time, int night_angle);
134 
135 QString formatTimeInput(QString user_input);
136 
137 void updateAutoTimes(int acft_id);
138 
139 void updateNightTimes();
140 
145  NightTimeValues() = delete;
146  NightTimeValues(const QString& dept, const QString& dest, const QDateTime& departure_time, int block_minutes, int night_angle)
147  {
148  nightMinutes = calculateNightTime(dept, dest, departure_time, block_minutes, night_angle);
149 
150  nightTime = OPL::Time::qTimefromMinutes(nightMinutes);
151  totalTime = OPL::Time::qTimefromMinutes(block_minutes);
152 
153  if (nightMinutes == 0) { // all day
154  takeOffNight = false;
155  landingNight = false;
156  }
157  else if (nightMinutes == block_minutes) { // all night
158  takeOffNight = true;
159  landingNight = true;
160  } else {
161  if(isNight(dept, departure_time, night_angle))
162  takeOffNight = true;
163  else
164  takeOffNight = false;
165  if(isNight(dest, departure_time.addSecs(block_minutes * 60), night_angle))
166  landingNight = true;
167  else
168  landingNight = false;
169  }
170 
171  };
172  NightTimeValues(bool to_night, bool ldg_night, int night_minutes, QTime night_time, QTime total_time)
173  : takeOffNight(to_night), landingNight(ldg_night), nightMinutes(night_minutes), nightTime(night_time), totalTime(total_time){};
174  bool takeOffNight;
175  bool landingNight;
176  int nightMinutes;
177  QTime nightTime;
178  QTime totalTime;
179 
180  inline bool isAllDay() {return (!takeOffNight && !landingNight);}
181  inline bool isAllNight() {return ( takeOffNight && landingNight);}
182  inline bool isDayToNight() {return (!takeOffNight && landingNight);}
183  inline bool isNightToDay() {return ( takeOffNight && !landingNight);}
184 };
185 
186 
187 } // namespace OPL::Calc
188 
189 #endif // CALC_H
The ACalc namespace provides various functions for calculations that are performed outside of the dat...
Definition: calc.h:34
void updateAutoTimes(int acft_id)
OPL::Calc::updateAutoTimes When the details of an aircraft are changed, this function recalculates de...
Definition: calc.cpp:301
double greatCircleDistanceBetweenAirports(const QString &dept, const QString &dest)
Opl::Calc::greatCircleDistanceBetweenAirports Calculates Great Circle distance between two coordinate...
Definition: calc.cpp:103
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: calc.cpp:226
double degToRad(double deg)
degToRad Converts degrees to radians
Definition: calc.h:52
void updateNightTimes()
OPL::Calc::updateNightTimes updates the night times in the database, used when changing night angle s...
Definition: calc.cpp:344
QString formatTimeInput(QString user_input)
OPL::Calc::formatTimeInput verifies user input and formats to hh:mm if the output is not a valid time...
Definition: calc.cpp:31
double radToNauticalMiles(double rad)
radToNauticalMiles Convert Radians to nautical miles
Definition: calc.h:63
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: calc.cpp:133
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: calc.cpp:167
double radToDeg(double rad)
radToDeg Converts radians to degrees
Definition: calc.h:41
double greatCircleDistance(double lat1, double lon1, double lat2, double lon2)
greatCircleDistance Calculates Great Circle distance between two coordinates, return in Radians.
Definition: calc.cpp:84
The NightTimeValues struct encapsulates values relating to night time that are needed by the NewFligh...
Definition: calc.h:144