acalc.h 6.6 KB

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