StelUtils.cpp   StelUtils.cpp 
skipping to change at line 28 skipping to change at line 28
*/ */
#include <cmath> // std::fmod #include <cmath> // std::fmod
#ifdef CYGWIN #ifdef CYGWIN
#include <malloc.h> #include <malloc.h>
#endif #endif
#include "StelUtils.hpp" #include "StelUtils.hpp"
#include "VecMath.hpp" #include "VecMath.hpp"
#include <QtOpenGL>
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
#include <QTextStream> #include <QTextStream>
#include <QFile> #include <QFile>
#include <QDebug> #include <QDebug>
#include <QLocale> #include <QLocale>
#include <QRegExp> #include <QRegExp>
namespace StelUtils namespace StelUtils
{ {
//! Return the full name of stellarium, i.e. "stellarium 0.9.0" //! Return the full name of stellarium, i.e. "stellarium 0.9.0"
QString getApplicationName() QString getApplicationName()
{ {
return QString("Stellarium")+" "+StelUtils::getApplicationVersion(); return QString("Stellarium")+" "+StelUtils::getApplicationVersion();
} }
//! Return the version of stellarium, i.e. "0.9.0" //! Return the version of stellarium, i.e. "0.9.0"
QString getApplicationVersion() QString getApplicationVersion()
{ {
#ifdef SVN_REVISION #ifdef BZR_REVISION
return QString(PACKAGE_VERSION)+" (BZR r"+BZR_REVISION+")";
#elif SVN_REVISION
return QString(PACKAGE_VERSION)+" (SVN r"+SVN_REVISION+")"; return QString(PACKAGE_VERSION)+" (SVN r"+SVN_REVISION+")";
#else #else
return QString(PACKAGE_VERSION); return QString(PACKAGE_VERSION);
#endif #endif
} }
double hmsToRad(unsigned int h, unsigned int m, double s ) double hmsToRad(unsigned int h, unsigned int m, double s )
{ {
return (double)M_PI/24.*h*2.+(double)M_PI/12.*m/60.+s*M_PI/43200.; return (double)M_PI/24.*h*2.+(double)M_PI/12.*m/60.+s*M_PI/43200.;
} }
skipping to change at line 175 skipping to change at line 176
m+=1; m+=1;
} }
if (m==60) if (m==60)
{ {
m=0; m=0;
h+=1; h+=1;
} }
if (h==24 && m==0 && s==0) if (h==24 && m==0 && s==0)
h=0; h=0;
return QString("%1h%2m%3s").arg(h, width).arg(m).arg(s, 0, 'f', prec ision); return QString("%1h%2m%3s").arg(h, width).arg(m,2,10,QLatin1Char('0' )).arg(s, 0, 'f', precision);
} }
/************************************************************************* /*************************************************************************
Convert an angle in radian to a dms formatted string Convert an angle in radian to a dms formatted string
If the minute and second part are null are too small, don't print them If the minute and second part are null are too small, don't print them
*************************************************************************/ *************************************************************************/
QString radToDmsStrAdapt(double angle, bool useD) QString radToDmsStrAdapt(double angle, bool useD)
{ {
QChar degsign('d'); QChar degsign('d');
if (!useD) if (!useD)
skipping to change at line 231 skipping to change at line 232
degsign = 0x00B0; degsign = 0x00B0;
} }
bool sign; bool sign;
unsigned int d,m; unsigned int d,m;
double s; double s;
StelUtils::radToDms(angle+0.005*M_PI/180/(60*60)*(angle<0?-1.:1.), s ign, d, m, s); StelUtils::radToDms(angle+0.005*M_PI/180/(60*60)*(angle<0?-1.:1.), s ign, d, m, s);
QString str; QString str;
QTextStream os(&str); QTextStream os(&str);
os << (sign?'+':'-') << d << degsign; os << (sign?'+':'-') << d << degsign;
int width = 2; os << qSetFieldWidth(2) << qSetPadChar('0') << m << qSetFieldWidth(0
) << '\'';
int width;
if (decimal) if (decimal)
{ {
os << qSetRealNumberPrecision(1); os << qSetRealNumberPrecision(1);
width = 4; width = 4;
} }
else else
{ {
os << qSetRealNumberPrecision(0); os << qSetRealNumberPrecision(0);
width = 2; width = 2;
} }
os << fixed << qSetFieldWidth(width) << qSetPadChar('0') << s << qSe
os << qSetFieldWidth(width) << m << qSetFieldWidth(0) << '\'' tFieldWidth(0) << '\"';
<< fixed << qSetFieldWidth(width) << qSetPadChar('0') << s
<< qSetFieldWidth(0) << '\"';
return str; return str;
} }
// Convert a dms formatted string to an angle in radian
double dmsStrToRad(const QString& s)
{
QRegExp reg("([\\+\\-])(\\d+)d(\\d+)'(\\d+)\"");
if (!reg.exactMatch(s))
return 0;
QStringList list = reg.capturedTexts();
bool sign = (list[1] == "+");
int deg = list[2].toInt();
int min = list[3].toInt();
int sec = list[4].toInt();
return dmsToRad(sign ? deg : -deg, min, sec);
}
// Obtains a Vec3f from a string with the form x,y,z // Obtains a Vec3f from a string with the form x,y,z
Vec3f strToVec3f(const QStringList& s) Vec3f strToVec3f(const QStringList& s)
{ {
if (s.size()<3) if (s.size()<3)
return Vec3f(0.f,0.f,0.f); return Vec3f(0.f,0.f,0.f);
return Vec3f(s[0].toFloat(),s[1].toFloat(),s[2].toFloat()); return Vec3f(s[0].toFloat(),s[1].toFloat(),s[2].toFloat());
} }
Vec3f strToVec3f(const QString& s) Vec3f strToVec3f(const QString& s)
skipping to change at line 411 skipping to change at line 425
QDateTime jdToQDateTime(const double& jd) QDateTime jdToQDateTime(const double& jd)
{ {
int year, month, day; int year, month, day;
getDateFromJulianDay(jd, &year, &month, &day); getDateFromJulianDay(jd, &year, &month, &day);
QDateTime result = QDateTime::fromString(QString("%1.%2.%3").arg(yea r, 4, 10, QLatin1Char('0')).arg(month).arg(day), "yyyy.M.d"); QDateTime result = QDateTime::fromString(QString("%1.%2.%3").arg(yea r, 4, 10, QLatin1Char('0')).arg(month).arg(day), "yyyy.M.d");
result.setTime(jdFractionToQTime(jd)); result.setTime(jdFractionToQTime(jd));
return result; return result;
} }
// based on QDateTime's original handling, but expanded to handle 0.0 and e void getDateFromJulianDay(double jd, int *yy, int *mm, int *dd)
arlier.
void getDateFromJulianDay(double jd, int *year, int *month, int *day)
{ {
int y, m, d; /*
* This algorithm is taken from
* "Numerical Recipes in c, 2nd Ed." (1992), pp. 14-15
* and converted to integer math.
* The electronic version of the book is freely available
* at http://www.nr.com/ , under "Obsolete Versions - Older
* book and code versions.
*/
static const long JD_GREG_CAL = 2299161;
static const int JB_MAX_WITHOUT_OVERFLOW = 107374182;
long julian;
julian = (long)floor(jd + 0.5);
long ta, jalpha, tb, tc, td, te;
if (julian >= JD_GREG_CAL)
{
jalpha = (4*(julian - 1867216) - 1) / 146097;
ta = julian + 1 + jalpha - jalpha / 4;
}
else if (julian < 0)
{
ta = julian + 36525 * (1 - julian / 36525);
}
else
{
ta = julian;
}
// put us in the right calendar day for the time of day. tb = ta + 1524;
double fraction = jd - floor(jd); if (tb <= JB_MAX_WITHOUT_OVERFLOW)
if (fraction >= .5) {
{ tc = (tb*20 - 2442) / 7305;
jd += 1.0;
}
if (jd >= 2299161)
{
// Gregorian calendar starting from October 15, 1582
// This algorithm is from Henry F. Fliegel and Thomas C. Van
Flandern
qulonglong ell, n, i, j;
ell = qulonglong(floor(jd)) + 68569;
n = (4 * ell) / 146097;
ell = ell - (146097 * n + 3) / 4;
i = (4000 * (ell + 1)) / 1461001;
ell = ell - (1461 * i) / 4 + 31;
j = (80 * ell) / 2447;
d = ell - (2447 * j) / 80;
ell = j / 11;
m = j + 2 - (12 * ell);
y = 100 * (n - 49) + i + ell;
} }
else else
{ {
// Julian calendar until October 4, 1582 tc = (long)(((unsigned long long)tb*20 - 2442) / 7305);
// Algorithm from Frequently Asked Questions about Calendars }
by Claus Toendering td = 365 * tc + tc/4;
int julianDay = (int)floor(jd); te = ((tb - td) * 10000)/306001;
julianDay += 32082;
int dd = (4 * julianDay + 3) / 1461; *dd = tb - td - (306001 * te) / 10000;
int ee = julianDay - (1461 * dd) / 4;
int mm = ((5 * ee) + 2) / 153; *mm = te - 1;
d = ee - (153 * mm + 2) / 5 + 1; if (*mm > 12)
m = mm + 3 - 12 * (mm / 10); {
y = dd - 4800 + (mm / 10); *mm -= 12;
} }
*year = y; *yy = tc - 4715;
*month = m; if (*mm > 2)
*day = d; {
--(*yy);
}
if (julian < 0)
{
*yy -= 100 * (1 - julian / 36525);
}
} }
void getTimeFromJulianDay(double julianDay, int *hour, int *minute, int *se cond) void getTimeFromJulianDay(double julianDay, int *hour, int *minute, int *se cond)
{ {
double frac = julianDay - (floor(julianDay)); double frac = julianDay - (floor(julianDay));
int s = (int)floor(frac * 24 * 60 * 60); int s = (int)floor((frac * 24.0 * 60.0 * 60.0) + 0.0001); // add co nstant to fix floating-point truncation error
*hour = ((s / (60 * 60))+12)%24; *hour = ((s / (60 * 60))+12)%24;
*minute = (s/(60))%60; *minute = (s/(60))%60;
*second = s % 60; *second = s % 60;
} }
QString julianDayToISO8601String(double jd) QString julianDayToISO8601String(double jd)
{ {
int year, month, day, hour, minute, second; int year, month, day, hour, minute, second;
getDateFromJulianDay(jd, &year, &month, &day); getDateFromJulianDay(jd, &year, &month, &day);
skipping to change at line 674 skipping to change at line 705
local.setTimeSpec(Qt::UTC); local.setTimeSpec(Qt::UTC);
int shiftInSeconds = universal.secsTo(local); int shiftInSeconds = universal.secsTo(local);
float shiftInHours = shiftInSeconds / 3600.0f; float shiftInHours = shiftInSeconds / 3600.0f;
return shiftInHours; return shiftInHours;
} }
// UTC ! // UTC !
bool getJDFromDate(double* newjd, int y, int m, int d, int h, int min, int s) bool getJDFromDate(double* newjd, int y, int m, int d, int h, int min, int s)
{ {
static const long IGREG2 = 15+31L*(10+12L*1582);
double deltaTime = (h / 24.0) + (min / (24.0*60.0)) + (s / (24.0 * 6 0.0 * 60.0)) - 0.5; double deltaTime = (h / 24.0) + (min / (24.0*60.0)) + (s / (24.0 * 6 0.0 * 60.0)) - 0.5;
QDate test((y <= 0 ? y-1 : y), m, d); QDate test((y <= 0 ? y-1 : y), m, d);
// if QDate will oblige, do so. // if QDate will oblige, do so.
if ( test.isValid() ) if ( test.isValid() )
{ {
double qdjd = (double)test.toJulianDay(); double qdjd = (double)test.toJulianDay();
qdjd += deltaTime; qdjd += deltaTime;
*newjd = qdjd; *newjd = qdjd;
return true; return true;
} else }
else
{ {
double jd = (double)((1461 * (y + 4800 + (m - 14) / 12)) / 4 /*
+ (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 - (3 * ((y + 4900 + (m - 14) * Algorithm taken from "Numerical Recipes in c, 2nd Ed." (1
/ 12) / 100)) / 4 + d - 32075) - 38; 992), pp. 11-12
*/
long ljul;
long jy, jm;
long laa, lbb, lcc, lee;
jy = y;
if (m > 2)
{
jm = m + 1;
}
else
{
--jy;
jm = m + 13;
}
laa = 1461 * jy / 4;
if (jy < 0 && jy % 4)
{
--laa;
}
lbb = 306001 * jm / 10000;
ljul = laa + lbb + d + 1720995L;
if (d + 31L*(m + 12L * y) >= IGREG2)
{
lcc = jy/100;
if (jy < 0 && jy % 100)
{
--lcc;
}
lee = lcc/4;
if (lcc < 0 && lcc % 4)
{
--lee;
}
ljul += 2 - lcc + lee;
}
double jd = (double)ljul;
jd += deltaTime; jd += deltaTime;
*newjd = jd; *newjd = jd;
return true; return true;
} }
return false; return false;
} }
double getJDFromDate_alg2(int y, int m, int d, int h, int min, int s) double getJDFromDate_alg2(int y, int m, int d, int h, int min, int s)
{ {
double extra = (100.0* y) + m - 190002.5; double extra = (100.0* y) + m - 190002.5;
 End of changes. 14 change blocks. 
55 lines changed or deleted 126 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/