21 #include "CaelumPrecompiled.h" 22 #include "Astronomy.h" 26 const LongReal Astronomy::PI = 3.1415926535897932384626433832795029L;
30 LongReal Astronomy::radToDeg (LongReal value)
32 return value * 180 / PI;
35 LongReal Astronomy::degToRad (LongReal value)
37 return value * PI / 180;
40 LongReal Astronomy::sinDeg (LongReal x) {
41 return std::sin (degToRad (x));
44 LongReal Astronomy::cosDeg (LongReal x) {
45 return std::cos (degToRad (x));
48 LongReal Astronomy::atan2Deg (LongReal y, LongReal x) {
49 return radToDeg(std::atan2 (y, x));
52 LongReal Astronomy::normalizeDegrees (LongReal value)
54 value = fmod (value, 360);
55 if (value < LongReal (0)) {
56 value += LongReal (360);
62 LongReal lon, LongReal lat,
63 LongReal &rasc, LongReal &decl)
65 double ecl = Astronomy::degToRad(23.439281);
67 double x = cos(lon) * cos(lat);
68 double y = cos(ecl) * sin(lon) * cos(lat) - sin(ecl) * sin(lat);
69 double z = sin(ecl) * sin(lon) * cos(lat) + cos(ecl) * sin(lat);
71 double r = sqrt(x * x + y * y);
76 void Astronomy::convertRectangularToSpherical (
77 LongReal x, LongReal y, LongReal z,
78 LongReal &rasc, LongReal &decl, LongReal &dist)
80 dist = sqrt (x * x + y * y + z * z);
81 rasc = atan2Deg (y, x);
82 decl = atan2Deg (z, sqrt (x * x + y * y));
85 void Astronomy::convertSphericalToRectangular (
86 LongReal rasc, LongReal decl, LongReal dist,
87 LongReal &x, LongReal &y, LongReal &z)
89 x = dist * cosDeg (rasc) * cosDeg (decl);
90 y = dist * sinDeg (rasc) * cosDeg (decl);
91 z = dist * sinDeg (decl);
96 LongReal longitude, LongReal latitude,
97 LongReal rasc, LongReal decl,
98 LongReal &azimuth, LongReal &altitude)
100 LongReal d = jday - 2451543.5;
101 LongReal w = LongReal (282.9404 + 4.70935E-5 * d);
102 LongReal M = LongReal (356.0470 + 0.9856002585 * d);
106 LongReal UT = LongReal(fmod(d, 1) * 360);
107 LongReal hourAngle = longitude + L + LongReal (180) + UT - rasc;
109 LongReal x = cosDeg (hourAngle) * cosDeg (decl);
110 LongReal y = sinDeg (hourAngle) * cosDeg (decl);
111 LongReal z = sinDeg (decl);
113 LongReal xhor = x * sinDeg (latitude) - z * cosDeg (latitude);
115 LongReal zhor = x * cosDeg (latitude) + z * sinDeg (latitude);
117 azimuth = atan2Deg (yhor, xhor) + LongReal (180);
118 altitude = atan2Deg (zhor, sqrt (xhor * xhor + yhor * yhor));
123 LongReal longitude, LongReal latitude,
124 LongReal &azimuth, LongReal &altitude)
128 LongReal d = jday - 2451543.5;
132 LongReal w = LongReal (282.9404 + 4.70935E-5 * d);
134 LongReal e = 0.016709 - 1.151E-9 * d;
136 LongReal M = LongReal(356.0470 + 0.9856002585 * d);
141 LongReal E = M + radToDeg(e * sinDeg (M) * (1 + e * cosDeg (M)));
144 LongReal xv = cosDeg (E) - e;
145 LongReal yv = sinDeg (E) * sqrt (1 - e * e);
147 LongReal lon = atan2Deg (yv, xv) + w;
150 LongReal lambda = degToRad(lon);
151 LongReal beta = degToRad(lat);
154 rasc = radToDeg(rasc);
155 decl = radToDeg(decl);
159 jday, longitude, latitude, rasc, decl, azimuth, altitude);
164 Ogre::Degree longitude, Ogre::Degree latitude,
165 Ogre::Degree &azimuth, Ogre::Degree &altitude)
169 azimuth = Ogre::Degree(az);
170 altitude = Ogre::Degree(al);
175 LongReal &lon, LongReal &lat)
178 double T = (jday - 2451545.0L) / 36525.0L;
179 double lprim = 3.8104L + 8399.7091L * T;
180 double mprim = 2.3554L + 8328.6911L * T;
181 double m = 6.2300L + 648.3019L * T;
182 double d = 5.1985L + 7771.3772L * T;
183 double f = 1.6280L + 8433.4663L * T;
185 + 0.1098L * sin(mprim)
186 + 0.0222L * sin(2.0L * d - mprim)
187 + 0.0115L * sin(2.0L * d)
188 + 0.0037L * sin(2.0L * mprim)
190 - 0.0020L * sin(2.0L * f)
191 + 0.0010L * sin(2.0L * d - 2.0L * mprim)
192 + 0.0010L * sin(2.0L * d - m - mprim)
193 + 0.0009L * sin(2.0L * d + mprim)
194 + 0.0008L * sin(2.0L * d - m)
195 + 0.0007L * sin(mprim - m)
197 - 0.0005L * sin(m + mprim);
200 + 0.0049L * sin(mprim + f)
201 + 0.0048L * sin(mprim - f)
202 + 0.0030L * sin(2.0L * d - f)
203 + 0.0010L * sin(2.0L * d + f - mprim)
204 + 0.0008 * sin(2.0L * d - f - mprim)
205 + 0.0006L * sin(2.0L * d + f);
208 void Astronomy::getHorizontalMoonPosition (
210 LongReal longitude, LongReal latitude,
211 LongReal &azimuth, LongReal &altitude)
214 LongReal lonecl, latecl;
222 rasc = radToDeg(rasc);
223 decl = radToDeg(decl);
227 jday, longitude, latitude, rasc, decl, azimuth, altitude);
230 void Astronomy::getHorizontalMoonPosition (
232 Ogre::Degree longitude, Ogre::Degree latitude,
233 Ogre::Degree &azimuth, Ogre::Degree &altitude)
236 getHorizontalMoonPosition(jday, longitude.valueDegrees (), latitude.valueDegrees (), az, al);
237 azimuth = Ogre::Degree(az);
238 altitude = Ogre::Degree(al);
241 void Astronomy::getHorizontalNorthEclipticPolePosition (
243 Ogre::Degree longitude, Ogre::Degree latitude,
244 Ogre::Degree &azimuth, Ogre::Degree &altitude)
247 LongReal rasc = 270.0;
248 LongReal decl = 90.0 - 23.439281;
253 jday, longitude.valueDegrees (), latitude.valueDegrees (), rasc, decl, az, al);
254 azimuth = Ogre::Degree(az);
255 altitude = Ogre::Degree(al);
259 int year,
int month,
int day)
264 int a = (14 - month) / 12;
265 int y = year + 4800 - a;
266 int m = month + 12 * a - 3;
267 return day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045;
271 int year,
int month,
int day,
272 int hour,
int minute, LongReal second)
278 LongReal jd = jdn + (hour - 12) / 24.0 + minute / 1440.0 + second / 86400.0;
284 int year,
int month,
int day,
285 LongReal secondsFromMidnight)
288 LongReal jd = jdn + secondsFromMidnight / 86400.0 - 0.5;
293 int julianDay,
int &year,
int &month,
int &day)
301 int c = (dg / 36524 + 1) * 3 / 4;
302 int dc = dg - c * 36524;
305 int a = (db / 365 + 1) * 3 / 4;
306 int da = db - a * 365;
307 int y = g * 400 + c * 100 + b * 4 + a;
308 int m = (da * 5 + 308) / 153 - 2;
309 int d = da - (m + 4) * 153 / 5 + 122;
310 year = y - 4800 + (m + 2) / 12;
311 month = (m + 2) % 12 + 1;
316 LongReal julianDay,
int &year,
int &month,
int &day,
317 int &hour,
int &minute, LongReal &second)
323 julianDay += (LongReal)0.5;
324 int ijd =
static_cast<int>(floor(julianDay));
328 LongReal s = (julianDay - (LongReal)ijd);
330 hour =
static_cast<int>(floor(s / 3600));
332 minute =
static_cast<int>(floor(s / 60));
339 LongReal julianDay,
int &year,
int &month,
int &day)
347 #if (OGRE_PLATFORM == OGRE_PLATFORM_WIN32) && (OGRE_COMPILER == OGRE_COMPILER_MSVC) 350 int oldMode = ::_controlfp (0, 0);
351 ::_controlfp (_PC_64, _MCW_PC);
357 ::_controlfp (oldMode, _MCW_PC);
369 assert(oldMode == 0xC0FFEE);
Dummy class to increase floting point precission in a block This class will raise precission in the c...
static const LongReal J2000
January 1, 2000, noon.
static void restoreFloatingPointMode(int oldMode)
Restore old floating point precission.
static void getGregorianDateTimeFromJulianDay(LongReal julianDay, int &year, int &month, int &day, int &hour, int &minute, LongReal &second)
Get gregorian date time from floating point julian day.
static void convertEclipticToEquatorialRad(LongReal lon, LongReal lat, LongReal &rasc, LongReal &decl)
Convert from ecliptic to ecuatorial spherical coordinates, in radians.
static LongReal getJulianDayFromGregorianDateTime(int year, int month, int day, int hour, int minute, LongReal second)
Get astronomical julian day from normal gregorian calendar.
static void getHorizontalSunPosition(LongReal jday, LongReal longitude, LongReal latitude, LongReal &azimuth, LongReal &altitude)
Get the sun's position in the sky in, relative to the horizon.
static void convertEquatorialToHorizontal(LongReal jday, LongReal longitude, LongReal latitude, LongReal rasc, LongReal decl, LongReal &azimuth, LongReal &altitude)
Convert from equatorial to horizontal coordinates.
static void getEclipticMoonPositionRad(LongReal jday, LongReal &lon, LongReal &lat)
Gets the moon position at a specific time in ecliptic coordinates.
static int enterHighPrecissionFloatingPointMode()
Enter high-precission floating-point mode.
static void getGregorianDateFromJulianDay(int julianDay, int &year, int &month, int &day)
Get gregorian date from integer julian day.
static int getJulianDayFromGregorianDate(int year, int month, int day)
Get astronomical julian day from normal gregorian calendar.