Stellarium  0.16.1
StelUtils.hpp
1 /*
2  * Stellarium
3  * Copyright (C) 2002 Fabien Chereau
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  */
19 
20 #ifndef _STELUTILS_HPP_
21 #define _STELUTILS_HPP_
22 
23 #include <cmath>
24 #include "VecMath.hpp"
25 
26 #include <QVariantMap>
27 #include <QDateTime>
28 #include <QString>
29 
30 // astronomical unit (km)
31 #define AU 149597870.691
32 #define AU_KM (1.0/149597870.691)
33 // Parsec (km)
34 #define PARSEC 30.857e12
35 // speed of light (km/sec)
36 #define SPEED_OF_LIGHT 299792.458
37 
38 #define stelpow10f(x) std::exp((x) * 2.3025850930f)
39 
41 namespace StelUtils
42 {
44  QString getApplicationName();
45 
47  QString getApplicationVersion();
48 
50  QString getOperatingSystemInfo();
51 
53  QString getUserAgentString();
54 
60  double hmsToRad(const unsigned int h, const unsigned int m, const double s);
61 
67  double dmsToRad(const int d, const unsigned int m, const double s);
68 
74  void radToHms(double rad, unsigned int& h, unsigned int& m, double& s);
75 
82  void radToDms(double rad, bool& sign, unsigned int& d, unsigned int& m, double& s);
83 
88  void radToDecDeg(double rad, bool& sign, double& deg);
89 
95  QString radToDecDegStr(const double angle, const int precision = 4, const bool useD=false, const bool useC=false);
96 
100  QString radToHmsStrAdapt(const double angle);
101 
105  QString radToHmsStr(const double angle, const bool decimal=false);
106 
111  QString radToDmsStrAdapt(const double angle, const bool useD=false);
112 
117  QString radToDmsStr(const double angle, const bool decimal=false, const bool useD=false);
118 
125  void decDegToDms(double angle, bool& sign, unsigned int& d, unsigned int& m, double& s);
126 
129  QString decDegToDmsStr(const double angle);
130 
133  double dmsStrToRad(const QString& s);
134 
135  //TODO these Vector-String converters should be removed, and missing functionality added to
136  //the VecMath classes and QVariant-based conversion
137 
139  Vec2f strToVec2f(const QStringList& s);
141  Vec2f strToVec2f(const QString& s);
146  Vec3f strToVec3f(const QStringList& s);
148  Vec3f strToVec3f(const QString& s);
150  Vec4d strToVec4d(const QStringList& s);
152  Vec4d strToVec4d(const QString& s);
153 
154  // Converts a Vec2f to a string in the same format that can be read by strToVec2f
155  QString vec2fToStr(const Vec2f& v);
157  QString vec3fToStr(const Vec3f& v);
159  QString vec4dToStr(const Vec4d& v);
160 
164  QString vec3fToHtmlColor(const Vec3f& v);
165 
168  Vec3f htmlColorToVec3f(const QString& c);
169 
174  void spheToRect(const double lng, const double lat, Vec3d& v);
175 
180  void spheToRect(const float lng, const float lat, Vec3f& v);
181 
186  void rectToSphe(double *lng, double *lat, const Vec3d& v);
187 
192  void rectToSphe(float *lng, float *lat, const Vec3d& v);
193 
198  void rectToSphe(float *lng, float *lat, const Vec3f& v);
199 
201  void equToEcl(const double raRad, const double decRad, const double eclRad, double *lambdaRad, double *betaRad);
202 
204  void eclToEqu(const double lambdaRad, const double betaRad, const double eclRad, double *raRad, double *decRad);
205 
226  double getDecAngle(const QString& str);
227 
229  bool isPowerOfTwo(const int value);
230 
232  int getBiggerPowerOfTwo(int value);
233 
235  double asinh(const double z);
236 
238  int imod(const int a, const int b);
239 
241  // New Qt based General Calendar Functions.
243  void getDateFromJulianDay(const double julianDay, int *year, int *month, int *day);
244 
246  void getTimeFromJulianDay(const double julianDay, int *hour, int *minute, int *second, int *millis=Q_NULLPTR);
247 
250  bool getDateTimeFromISO8601String(const QString& iso8601Date, int* y, int* m, int* d, int* h, int* min, float* s);
251 
254  QString julianDayToISO8601String(const double jd, bool addMS = false);
255 
258  double getJulianDayFromISO8601String(const QString& iso8601Date, bool* ok);
259 
264  QString localeDateString(const int year, const int month, const int day, const int dayOfWeek, const QString &fmt);
265 
269  QString localeDateString(const int year, const int month, const int day, const int dayOfWeek);
270 
273  double getJDFromSystem();
274 
278  double getJDFromBesselianEpoch(const float epoch);
279 
283  double qTimeToJDFraction(const QTime& time);
284 
286  QTime jdFractionToQTime(const double jd);
287 
291  double qDateTimeToJd(const QDateTime& dateTime);
292 
296  QDateTime jdToQDateTime(const double& jd);
297 
309  bool getJDFromDate(double* newjd, const int y, const int m, const int d, const int h, const int min, const int s);
310 
311  int numberOfDaysInMonthInYear(const int month, const int year);
313  bool isLeapYear(const int year);
316  int dayInYear(const int year, const int month, const int day);
318  double yearFraction(const int year, const int month, const double day);
319 
320  bool changeDateTimeForRollover(int oy, int om, int od, int oh, int omin, int os,
321  int* ry, int* rm, int* rd, int* rh, int* rmin, int* rs);
322 
324  void debugQVariantMap(const QVariant& m, const QString& indent="", const QString& key="");
325 
326 
329  inline float fastAcos(const float x)
330  {
331  return static_cast<float>(M_PI_2) - (x + x*x*x * (1.f/6.f + x*x * (3.f/40.f + 5.f/112.f * x*x)) );
332  }
333 
335  inline float fastExp(const float x)
336  {
337  return (x>=0)?
338  (1.f + x*(1.f+ x/2.f*(1.f+ x/3.f*(1.f+x/4.f*(1.f+x/5.f))))):
339  1.f / (1.f -x*(1.f -x/2.f*(1.f- x/3.f*(1.f-x/4.f*(1.f-x/5.f)))));
340  }
341 
344  inline Vec3f getNightColor(const Vec3f& dayColor)
345  {
346  float max = 0.0;
347  for(int i=0; i<3; i++)
348  {
349  max = dayColor[i] > max ? dayColor[i] : max;
350  }
351  return Vec3f(max, 0, 0);
352  }
353 
355  double calculateSiderealPeriod(const double SemiMajorAxis);
356 
358  QString hoursToHmsStr(const double hours);
359 
367  long double secondsSinceStart();
368 
371  double getDeltaTwithoutCorrection(const double jDay);
372 
379  double getDeltaTByEspenakMeeus(const double jDay);
380 
389  double getDeltaTBySchoch(const double jDay);
390 
400  double getDeltaTByClemence(const double jDay);
401 
410  double getDeltaTByIAU(const double jDay);
411 
421  double getDeltaTByAstronomicalEphemeris(const double jDay);
422 
427  double getDeltaTByTuckermanGoldstine(const double jDay);
428 
440  double getDeltaTByMullerStephenson(const double jDay);
441 
451  double getDeltaTByStephenson1978(const double jDay);
452 
459  double getDeltaTByStephenson1997(const double jDay);
460 
471  double getDeltaTBySchmadelZech1979(const double jDay);
472 
477  double getDeltaTByMorrisonStephenson1982(const double jDay);
478 
487  double getDeltaTByStephensonMorrison1984(const double jDay);
488 
497  double getDeltaTByStephensonMorrison1995(const double jDay);
498 
503  double getDeltaTByStephensonHoulden(const double jDay);
504 
510  double getDeltaTByEspenak(const double jDay);
511 
520  double getDeltaTByBorkowski(const double jDay);
521 
531  double getDeltaTBySchmadelZech1988(const double jDay);
532 
537  double getDeltaTByChaprontTouze(const double jDay);
538 
543  double getDeltaTByJPLHorizons(const double jDay);
544 
557  double getDeltaTByMorrisonStephenson2004(const double jDay);
558 
564  double getDeltaTByReijs(const double jDay);
565 
570  double getDeltaTByChaprontMeeus(const double jDay);
571 
580  double getDeltaTByMeeusSimons(const double jDay);
581 
588  double getDeltaTByMontenbruckPfleger(const double jDay);
589 
596  double getDeltaTByReingoldDershowitz(const double jDay);
597 
606  double getDeltaTByBanjevic(const double jDay);
607 
620  double getDeltaTByIslamSadiqQureshi(const double jDay);
621 
629  double getDeltaTByKhalidSultanaZaidi(const double jDay);
630 
638  double getDeltaTByStephensonMorrisonHohenkerk2016(const double jDay);
639 
649  double getMoonSecularAcceleration(const double jDay, const double ndot, const bool useDE43x);
650 
654  double getDeltaTStandardError(const double jDay);
655 
663  double getMoonFluctuation(const double jDay);
664 
666  template <typename T> int sign(T val)
667  {
668  return (T(0) < val) - (val < T(0));
669  }
670 
675  float *ComputeCosSinTheta(const int slices);
676 
681  float *ComputeCosSinRho(const int segments);
682 
690  float* ComputeCosSinRhoZone(const float dRho, const int segments, const float minAngle);
691 
697  double getDecYear(const int year, const int month, const int day);
698 
703  int compareVersions(const QString v1, const QString v2);
704 
706  QByteArray uncompress(const QByteArray& data);
707 
713  QByteArray uncompress(QIODevice &device, qint64 maxBytes=-1);
714 
715 #ifdef _MSC_BUILD
716  inline double trunc(double x)
717  {
718  return (x < 0 ? std::ceil(x) : std::floor(x));
719  }
720 #else
721  inline double trunc(double x) { return ::trunc(x); }
722 #endif
723 }
724 
725 #endif // _STELUTILS_HPP_
double getDeltaTByMontenbruckPfleger(const double jDay)
Get Delta-T estimation for a given date.
void getTimeFromJulianDay(const double julianDay, int *hour, int *minute, int *second, int *millis=Q_NULLPTR)
Make from julianDay an hour, minute, second.
QString decDegToDmsStr(const double angle)
Convert an angle in decimal degrees to a dms formatted string.
bool getDateTimeFromISO8601String(const QString &iso8601Date, int *y, int *m, int *d, int *h, int *min, float *s)
Parse an ISO8601 date string.
bool isPowerOfTwo(const int value)
Check if a number is a power of 2.
QTime jdFractionToQTime(const double jd)
Convert a fraction of a Julian Day to a QTime.
QString julianDayToISO8601String(const double jd, bool addMS=false)
Format the given Julian Day in (UTC) ISO8601 date string.
QString hoursToHmsStr(const double hours)
Convert decimal hours to hours, minutes, seconds.
double getDeltaTByMorrisonStephenson2004(const double jDay)
Get Delta-T estimation for a given date.
double getDeltaTByIslamSadiqQureshi(const double jDay)
Get Delta-T estimation for a given date.
QString vec4dToStr(const Vec4d &v)
Converts a Vec4d to a string in the same format that can be read by strToVec4d.
void equToEcl(const double raRad, const double decRad, const double eclRad, double *lambdaRad, double *betaRad)
Coordinate Transformation from equatorial to ecliptical.
double getJDFromSystem()
Get the current Julian Date from system time.
double getDeltaTByJPLHorizons(const double jDay)
Get Delta-T estimation for a given date.
void rectToSphe(double *lng, double *lat, const Vec3d &v)
Convert from spherical coordinates to Rectangular direction.
double getDeltaTByReijs(const double jDay)
Get Delta-T estimation for a given date.
Vec3f strToVec3f(const QStringList &s)
Obtains a Vec3f from a string.
QString getApplicationName()
Return the full name of stellarium, i.e. "stellarium 0.9.0".
double dmsStrToRad(const QString &s)
Convert a dms formatted string to an angle in radian.
void spheToRect(const double lng, const double lat, Vec3d &v)
Convert from spherical coordinates to Rectangular direction.
double getDeltaTByStephensonMorrisonHohenkerk2016(const double jDay)
Get Delta-T estimation for a given date.
double hmsToRad(const unsigned int h, const unsigned int m, const double s)
Convert an angle in hms format to radian.
int dayInYear(const int year, const int month, const int day)
Find day number for date in year.
double getDeltaTByEspenak(const double jDay)
Get Delta-T estimation for a given date.
QString getOperatingSystemInfo()
Return the name and the version of operating system, i.e. "Mac OS X 10.7".
float * ComputeCosSinRho(const int segments)
Compute cosines and sines around a half-circle which is split in "segments" parts.
float * ComputeCosSinTheta(const int slices)
Compute cosines and sines around a circle which is split in "segments" parts.
Vec2f strToVec2f(const QStringList &s)
Reads a Vec2f from a string list.
QString radToHmsStrAdapt(const double angle)
Convert an angle in radian to a hms formatted string.
QString vec3fToHtmlColor(const Vec3f &v)
Converts a Vec3f to HTML color notation.
double getDeltaTByBanjevic(const double jDay)
Get Delta-T estimation for a given date.
double getDecYear(const int year, const int month, const int day)
Compute date in decimal year format.
double getDeltaTByChaprontTouze(const double jDay)
Get Delta-T estimation for a given date.
QString localeDateString(const int year, const int month, const int day, const int dayOfWeek, const QString &fmt)
Format the date and day-of-week per the format in fmt (see QDateTime::toString()).
QDateTime jdToQDateTime(const double &jd)
Convert a julian day to a QDateTime.
Vec3f getNightColor(const Vec3f &dayColor)
Get a night mode version of a color.
Definition: StelUtils.hpp:344
double getMoonFluctuation(const double jDay)
Get value of the Moon fluctuation Source: The Rotation of the Earth, and the Secular Accelerations of...
void radToDecDeg(double rad, bool &sign, double &deg)
Convert an angle in radian to decimal degree.
double qDateTimeToJd(const QDateTime &dateTime)
Convert a QT QDateTime class to julian day.
void radToHms(double rad, unsigned int &h, unsigned int &m, double &s)
Convert an angle in radian to hms format.
double getDeltaTByMorrisonStephenson1982(const double jDay)
Get Delta-T estimation for a given date.
double getDeltaTByTuckermanGoldstine(const double jDay)
Get Delta-T estimation for a given date.
void decDegToDms(double angle, bool &sign, unsigned int &d, unsigned int &m, double &s)
Convert an angle in decimal degree to +-dms format.
Vec4d strToVec4d(const QStringList &s)
Like StelUtils::strToVec3f, but with 4 components and with double precision.
double getDeltaTwithoutCorrection(const double jDay)
Get Delta-T estimation for a given date.
double getDeltaTByBorkowski(const double jDay)
Get Delta-T estimation for a given date.
double getDeltaTBySchmadelZech1988(const double jDay)
Get Delta-T estimation for a given date.
QString radToDecDegStr(const double angle, const int precision=4, const bool useD=false, const bool useC=false)
Convert an angle in radian to a decimal degree string.
QString getApplicationVersion()
Return the version of stellarium, i.e. "0.9.0".
A templatized 2d vector compatible with openGL.
Definition: VecMath.hpp:32
double getDeltaTByChaprontMeeus(const double jDay)
Get Delta-T estimation for a given date.
float fastAcos(const float x)
Compute acos(x) The taylor serie is not accurate around x=1 and x=-1.
Definition: StelUtils.hpp:329
double getDeltaTBySchoch(const double jDay)
Get Delta-T estimation for a given date.
double getDeltaTByStephensonMorrison1984(const double jDay)
Get Delta-T estimation for a given date.
QString radToDmsStr(const double angle, const bool decimal=false, const bool useD=false)
Convert an angle in radian to a dms formatted string.
double getJDFromBesselianEpoch(const float epoch)
Get the Julian Day Number (JD) from Besselian epoch.
double getDeltaTBySchmadelZech1979(const double jDay)
Get Delta-T estimation for a given date.
double getDeltaTByKhalidSultanaZaidi(const double jDay)
Get Delta-T estimation for a given date.
double getDeltaTByIAU(const double jDay)
Get Delta-T estimation for a given date.
double getDeltaTByEspenakMeeus(const double jDay)
Get Delta-T estimation for a given date.
void radToDms(double rad, bool &sign, unsigned int &d, unsigned int &m, double &s)
Convert an angle in radian to +-dms format.
double dmsToRad(const int d, const unsigned int m, const double s)
Convert an angle in +-dms format to radian.
bool isLeapYear(const int year)
double calculateSiderealPeriod(const double SemiMajorAxis)
Calculate and return sidereal period in days from semi-major axis (in AU)
double getDeltaTByStephenson1997(const double jDay)
Get Delta-T estimation for a given date.
double getDeltaTByStephenson1978(const double jDay)
Get Delta-T estimation for a given date.
QString radToDmsStrAdapt(const double angle, const bool useD=false)
Convert an angle in radian to a dms formatted string.
double getDeltaTByAstronomicalEphemeris(const double jDay)
Get Delta-T estimation for a given date.
double getDeltaTByClemence(const double jDay)
Get Delta-T estimation for a given date.
double getDeltaTByMeeusSimons(const double jDay)
Get Delta-T estimation for a given date.
contains general purpose utility functions.
double getDecAngle(const QString &str)
Convert a string longitude, latitude, RA or Declination angle to radians.
int getBiggerPowerOfTwo(int value)
Return the first power of two bigger than the given value.
QString vec3fToStr(const Vec3f &v)
Converts a Vec3f to a string in the same format that can be read by strToVec3f.
void eclToEqu(const double lambdaRad, const double betaRad, const double eclRad, double *raRad, double *decRad)
Coordinate Transformation from ecliptical to equatorial.
long double secondsSinceStart()
Get the number of seconds since program start.
double getDeltaTByMullerStephenson(const double jDay)
Get Delta-T estimation for a given date.
double getDeltaTByStephensonMorrison1995(const double jDay)
Get Delta-T estimation for a given date.
float * ComputeCosSinRhoZone(const float dRho, const int segments, const float minAngle)
Compute cosines and sines around part of a circle (from top to bottom) which is split in "segments" p...
QString radToHmsStr(const double angle, const bool decimal=false)
Convert an angle in radian to a hms formatted string.
double getJulianDayFromISO8601String(const QString &iso8601Date, bool *ok)
Return the Julian Date matching the ISO8601 date string.
QByteArray uncompress(const QByteArray &data)
Uncompress gzip or zlib compressed data.
QString getUserAgentString()
Return the user agent name, i.e. "Stellarium/0.15.0 (Linux)".
int sign(T val)
Sign function from http://stackoverflow.com/questions/1903954/is-there-a-standard-sign-function-signu...
Definition: StelUtils.hpp:666
int imod(const int a, const int b)
Integer modulo where the result is always positive.
double qTimeToJDFraction(const QTime &time)
Convert a time of day to the fraction of a Julian Day.
int compareVersions(const QString v1, const QString v2)
Comparison two string versions and return a result in range -1,0,1.
double yearFraction(const int year, const int month, const double day)
Return a fractional year like YYYY.ddddd. For negative years, the year number is decreased. E.g. -500.5 occurs in -501.
bool getJDFromDate(double *newjd, const int y, const int m, const int d, const int h, const int min, const int s)
Compute Julian day number from calendar date.
void getDateFromJulianDay(const double julianDay, int *year, int *month, int *day)
Make from julianDay a year, month, day for the Julian Date julianDay represents.
double getDeltaTStandardError(const double jDay)
Get the standard error (sigma) for the value of DeltaT.
double getDeltaTByReingoldDershowitz(const double jDay)
Get Delta-T estimation for a given date.
double getDeltaTByStephensonHoulden(const double jDay)
Get Delta-T estimation for a given date.
double getMoonSecularAcceleration(const double jDay, const double ndot, const bool useDE43x)
Get Secular Acceleration estimation for a given year.
float fastExp(const float x)
Compute exp(x) for small exponents x.
Definition: StelUtils.hpp:335
Vec3f htmlColorToVec3f(const QString &c)
Converts a color in HTML notation to a Vec3f.
double asinh(const double z)
Return the inverse sinus hyperbolic of z.
void debugQVariantMap(const QVariant &m, const QString &indent="", const QString &key="")
Output a QVariantMap to qDebug(). Formats like a tree where there are nested objects.