acalc.h 6.7 KB

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