acalc.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #ifndef ACALC_H
  2. #define ACALC_H
  3. #include "src/database/db.h"
  4. #include "src/classes/aircraft.h"
  5. #include "src/classes/flight.h"
  6. #include "src/classes/asettings.h"
  7. #include <QDateTime>
  8. #include <cmath>
  9. #include <QDebug>
  10. /*!
  11. * \brief The ACalc namespace provides various functions for calculations that are performed
  12. * outside of the database. This includes tasks like converting different units and formats,
  13. * or functions calculating block time or night time.
  14. */
  15. namespace ACalc {
  16. /*!
  17. * \brief ACalc::blocktime Calculates Block Time for a given departure and arrival time
  18. * \param tofb QTime Time Off Blocks
  19. * \param tonb QTime Time On Blocks
  20. * \return Block Time in minutes
  21. */
  22. inline QTime blocktime(QTime tofb, QTime tonb)
  23. {
  24. QTime blocktime_out(0, 0); // initialise return value at midnight
  25. if (tonb > tofb) { // landing same day
  26. int blockseconds = tofb.secsTo(tonb);
  27. blocktime_out = blocktime_out.addSecs(blockseconds);
  28. } else { // landing next day
  29. QTime midnight(0, 0);
  30. int blockseconds = tofb.secsTo(midnight);
  31. blocktime_out = blocktime_out.addSecs(blockseconds);
  32. blockseconds = midnight.secsTo(tonb);
  33. blocktime_out = blocktime_out.addSecs(blockseconds);
  34. }
  35. return blocktime_out;
  36. }
  37. /*!
  38. * \brief ACalc::minutes_to_string Converts database time to String Time
  39. * \param blockminutes from database
  40. * \return String hh:mm
  41. */
  42. inline QString minutesToString(QString blockminutes)
  43. {
  44. int minutes = blockminutes.toInt();
  45. QString hour = QString::number(minutes / 60);
  46. if (hour.size() < 2) {
  47. hour.prepend("0");
  48. }
  49. QString minute = QString::number(minutes % 60);
  50. if (minute.size() < 2) {
  51. minute.prepend("0");
  52. }
  53. QString blocktime = hour + ":" + minute;
  54. return blocktime;
  55. };
  56. /*!
  57. * \brief ACalc::time_to_minutes converts QTime to int minutes
  58. * \param time QTime
  59. * \return int time as number of minutes
  60. */
  61. inline int QTimeToMinutes(QTime time)
  62. {
  63. QString timestring = time.toString("hh:mm");
  64. int minutes = (timestring.left(2).toInt()) * 60;
  65. minutes += timestring.right(2).toInt();
  66. return minutes;
  67. }
  68. /*!
  69. * \brief ACalc::string_to_minutes Converts String Time to String Number of Minutes
  70. * \param timestring "hh:mm"
  71. * \return String number of minutes
  72. */
  73. inline int stringToMinutes(QString timestring)
  74. {
  75. int minutes = (timestring.left(2).toInt()) * 60;
  76. minutes += timestring.right(2).toInt();
  77. timestring = QString::number(minutes);
  78. return minutes;
  79. }
  80. /*!
  81. * \brief radToDeg Converts radians to degrees
  82. * \param rad
  83. * \return degrees
  84. */
  85. inline double radToDeg(double rad)
  86. {
  87. double deg = rad * (180 / M_PI);
  88. return deg;
  89. }
  90. /*!
  91. * \brief degToRad Converts degrees to radians
  92. * \param deg
  93. * \return radians
  94. */
  95. inline double degToRad(double deg)
  96. {
  97. double rad = deg * (M_PI / 180);
  98. return rad;
  99. }
  100. /*!
  101. * \brief radToNauticalMiles Convert Radians to nautical miles
  102. * \param rad
  103. * \return nautical miles
  104. */
  105. inline double radToNauticalMiles(double rad)
  106. {
  107. double nm = rad * 3440.06479482;
  108. return nm;
  109. }
  110. /*!
  111. * \brief greatCircleDistance Calculates Great Circle distance between two coordinates, return in Radians.
  112. * \param lat1 Location Latitude in degrees -90:90 ;S(-) N(+)
  113. * \param lon1 Location Longitude in degrees -180:180 W(-) E(+)
  114. * \param lat2 Location Latitude in degrees -90:90 ;S(-) N(+)
  115. * \param lon2 Location Longitude in degrees -180:180 W(-) E(+)
  116. * \return
  117. */
  118. double greatCircleDistance(double lat1, double lon1, double lat2, double lon2);
  119. /*!
  120. * \brief ACalc::greatCircleDistanceBetweenAirports Calculates Great
  121. * Circle distance between two coordinates, return in nautical miles.
  122. * \param dept ICAO 4-letter Airport Identifier
  123. * \param dest ICAO 4-letter Airport Identifier
  124. * \return Nautical Miles From Departure to Destination
  125. */
  126. double greatCircleDistanceBetweenAirports(const QString &dept, const QString &dest);
  127. /*!
  128. * \brief Calculates a list of points (lat,lon) along the Great Circle between two points.
  129. * The points are spaced equally, one minute of block time apart.
  130. * \param lat1 Location Latitude in degrees -90:90 ;S(-) N(+)
  131. * \param lon1 Location Longitude in degrees -180:180 W(-) E(+)
  132. * \param lat2 Location Latitude in degrees -90:90 ;S(-) N(+)
  133. * \param lon2 Location Longitude in degrees -180:180 W(-) E(+)
  134. * \param tblk Total Blocktime in minutes
  135. * \return coordinates {lat,lon} along the Great Circle Track
  136. */
  137. QVector<QVector<double>> intermediatePointsOnGreatCircle(double lat1,
  138. double lon1,
  139. double lat2,
  140. double lon2,
  141. int tblk);
  142. /*!
  143. * \brief Calculates solar elevation angle for a given point in time and latitude/longitude coordinates
  144. *
  145. * It is based on the formulas found here: http://stjarnhimlen.se/comp/tutorial.html#5
  146. *
  147. * Credit also goes to Darin C. Koblick for his matlab implementation of various of these
  148. * formulas and to Kevin Godden for porting it to C++.
  149. *
  150. * Darin C. Koblock: https://www.mathworks.com/matlabcentral/profile/authors/1284781
  151. * Kevin Godden: https://www.ridgesolutions.ie/index.php/about-us/
  152. *
  153. * \param utc_time_point - QDateTime (UTC) for which the elevation is Calculated
  154. * \param lat - Location Latitude in degrees -90:90 ;S(-) N(+)
  155. * \param lon - Location Longitude in degrees -180:180 W(-) E(+)
  156. * \return elevation - double of solar elevation in degrees.
  157. */
  158. double solarElevation(QDateTime utc_time_point, double lat, double lon);
  159. /*!
  160. * \brief Calculates which portion of a flight was conducted in night conditions.
  161. * \param dept - ICAO 4-letter code of Departure Airport
  162. * \param dest - ICAO 4-letter Code of Destination Airport
  163. * \param departureTime - QDateTime of Departure (UTC)
  164. * \param tblk - Total block time in minutes
  165. * \param nightAngle - the solar elevation angle where night conditons exist.
  166. * Default -6 (end of civil evening twilight)
  167. * \return Total number of minutes under night flying conditions
  168. */
  169. int calculateNightTime(const QString &dept, const QString &dest, QDateTime departureTime, int tblk, int nightAngle);
  170. bool isNight(const QString &icao, QDateTime event_time, int night_angle);
  171. QString formatTimeInput(QString user_input);
  172. void updateAutoTimes(int acft_id);
  173. void autoTimes(Flight, Aircraft);
  174. void updateNightTimes();
  175. } // namespace ACalc
  176. #endif // ACALC_H