-// moonpos.cxx (basically, this is a slightly modified version of the 'sunpos.cxx' file, adapted from XEarth)
-
+// moonpos.cxx (basically, this is a slightly modified version of the
+// 'sunpos.cxx' file, adapted from XEarth)
+//
// kirk johnson
// july 1993
//
# include <config.h>
#endif
-#include "Include/compiler.h"
-#ifdef FG_HAVE_STD_INCLUDES
+#include <simgear/compiler.h>
+
+#ifdef SG_HAVE_STD_INCLUDES
# include <cmath>
# include <cstdio>
# include <ctime>
# include <time.h>
#endif
-
-//#include <Astro/orbits.hxx>
-#include <Astro/solarsystem.hxx>
-#include <Debug/logstream.hxx>
-#include <Include/fg_constants.h>
-#include <Main/views.hxx>
-#include <Math/fg_geodesy.hxx>
-#include <Math/mat3.h>
-#include <Math/point3d.hxx>
-#include <Math/polar3d.hxx>
-#include <Math/vector.hxx>
+#include <simgear/constants.h>
+#include <simgear/debug/logstream.hxx>
+#include <simgear/ephemeris/ephemeris.hxx>
+#include <simgear/math/point3d.hxx>
+#include <simgear/math/polar3d.hxx>
+#include <simgear/math/sg_geodesy.hxx>
+#include <simgear/math/vector.hxx>
+#include <simgear/timing/sg_time.hxx>
+
+#include <Main/globals.hxx>
+#include <Main/viewer.hxx>
#include <Scenery/scenery.hxx>
+#include <Time/light.hxx>
-#include "fg_time.hxx"
#include "moonpos.hxx"
-extern SolarSystem *solarSystem;
-
#undef E
/*
* assuming the apparent orbit of the moon about the earth is circular,
* the rate at which the orbit progresses is given by RadsPerDay --
- * FG_2PI radians per orbit divided by 365.242191 days per year:
+ * SG_2PI radians per orbit divided by 365.242191 days per year:
*/
-#define RadsPerDay (FG_2PI/365.242191)
+#define RadsPerDay (SG_2PI/365.242191)
/*
* details of moon's apparent orbit at epoch 1990.0 (after
* Eccentricity (eccentricity of orbit) 0.016713
*/
-#define Epsilon_g (279.403303*(FG_2PI/360))
-#define OmegaBar_g (282.768422*(FG_2PI/360))
+#define Epsilon_g (279.403303*(SGD_2PI/360))
+#define OmegaBar_g (282.768422*(SGD_2PI/360))
#define Eccentricity (0.016713)
/*
* 1990.0 (computed as 23.440592 degrees according to the method given
* in duffett-smith, section 27)
*/
-#define MeanObliquity (23.440592*(FG_2PI/360))
+#define MeanObliquity (23.440592*(SGD_2PI/360))
/* static double solve_keplers_equation(double); */
/* static double moon_ecliptic_longitude(time_t); */
D = DaysSinceEpoch(ssue);
N = RadsPerDay * D;
- N = fmod(N, FG_2PI);
- if (N < 0) N += FG_2PI;
+ N = fmod(N, SG_2PI);
+ if (N < 0) N += SG_2PI;
M_moon = N + Epsilon_g - OmegaBar_g;
- if (M_moon < 0) M_moon += FG_2PI;
+ if (M_moon < 0) M_moon += SG_2PI;
E = solve_keplers_equation(M_moon);
v = 2 * atan(sqrt((1+Eccentricity)/(1-Eccentricity)) * tan(E/2));
/* lazy test to ensure gregorian calendar */
if (y < 1583) {
- FG_LOG( FG_EVENT, FG_ALERT,
+ SG_LOG( SG_EVENT, SG_ALERT,
"WHOOPS! Julian dates only valid for 1582 oct 15 or later" );
}
* every ten minutes. (Comment added by Durk Talsma).
************************************************************************/
- ecliptic_to_equatorial( SolarSystem::theSolarSystem->getMoon()->getLon(),
+ ecliptic_to_equatorial( globals->get_ephem()->get_moon()->getLon(),
0.0, &alpha, &delta );
- tmp = alpha - (FG_2PI/24)*GST(ssue);
- if (tmp < -FG_PI) {
- do tmp += FG_2PI;
- while (tmp < -FG_PI);
- } else if (tmp > FG_PI) {
- do tmp -= FG_2PI;
- while (tmp < -FG_PI);
+ tmp = alpha - (SGD_2PI/24)*GST(ssue);
+ if (tmp < -SGD_PI) {
+ do tmp += SGD_2PI;
+ while (tmp < -SGD_PI);
+ } else if (tmp > SGD_PI) {
+ do tmp -= SGD_2PI;
+ while (tmp < -SGD_PI);
}
*lon = tmp;
/* lambda = moon_ecliptic_longitude(ssue); */
/* ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta); */
//ecliptic_to_equatorial (solarPosition.lonMoon, 0.0, &alpha, &delta);
- ecliptic_to_equatorial( SolarSystem::theSolarSystem->getMoon()->getLon(),
- SolarSystem::theSolarSystem->getMoon()->getLat(),
+ ecliptic_to_equatorial( globals->get_ephem()->get_moon()->getLon(),
+ globals->get_ephem()->get_moon()->getLat(),
&alpha, &delta );
-// tmp = alpha - (FG_2PI/24)*GST(ssue);
- tmp = alpha - (FG_2PI/24)*gst;
- if (tmp < -FG_PI) {
- do tmp += FG_2PI;
- while (tmp < -FG_PI);
- } else if (tmp > FG_PI) {
- do tmp -= FG_2PI;
- while (tmp < -FG_PI);
+// tmp = alpha - (SG_2PI/24)*GST(ssue);
+ tmp = alpha - (SGD_2PI/24)*gst;
+ if (tmp < -SGD_PI) {
+ do tmp += SGD_2PI;
+ while (tmp < -SGD_PI);
+ } else if (tmp > SGD_PI) {
+ do tmp -= SGD_2PI;
+ while (tmp < -SGD_PI);
}
*lon = tmp;
// update the cur_time_params structure with the current moon position
void fgUpdateMoonPos( void ) {
- fgLIGHT *l;
- FGTime *t;
- FGView *v;
- MAT3vec nup, nmoon, v0, surface_to_moon;
- Point3D p, rel_moonpos;
+ sgVec3 nup, nmoon;
+ Point3D rel_moonpos;
double dot, east_dot;
double moon_gd_lat, sl_radius;
- double ntmp;
- l = &cur_light_params;
- t = FGTime::cur_time_params;
- v = ¤t_view;
+ // vector in cartesian coordinates from current position to the
+ // postion on the earth's surface the moon is directly over
+ sgVec3 to_moon;
+
+ // surface direction to go to head towards moon
+ sgVec3 surface_to_moon;
- FG_LOG( FG_EVENT, FG_INFO, " Updating Moon position" );
+ FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
+ SGTime *t = globals->get_time_params();
+ FGViewer *v = globals->get_current_view();
- // (not sure why there was two)
- // fgMoonPosition(t->cur_time, &l->moon_lon, &moon_gd_lat);
- fgMoonPositionGST(t->getGst(), &l->moon_lon, &moon_gd_lat);
+ SG_LOG( SG_EVENT, SG_INFO, " Updating Moon position" );
- fgGeodToGeoc(moon_gd_lat, 0.0, &sl_radius, &l->moon_gc_lat);
+ double moon_l;
+ fgMoonPositionGST(t->getGst(), &moon_l, &moon_gd_lat);
+ l->set_moon_lon(moon_l);
- p = Point3D( l->moon_lon, l->moon_gc_lat, sl_radius );
- l->fg_moonpos = fgPolarToCart3d(p);
+ sgGeodToGeoc(moon_gd_lat, 0.0, &sl_radius, &moon_l);
+ l->set_moon_gc_lat(moon_l);
- FG_LOG( FG_EVENT, FG_INFO, " t->cur_time = " << t->get_cur_time() );
- FG_LOG( FG_EVENT, FG_INFO,
+ Point3D p = Point3D( l->get_moon_lon(), l->get_moon_gc_lat(), sl_radius );
+ l->set_moonpos( sgPolarToCart3d(p) );
+
+ SG_LOG( SG_EVENT, SG_INFO, " t->cur_time = " << t->get_cur_time() );
+ SG_LOG( SG_EVENT, SG_INFO,
" Moon Geodetic lat = " << moon_gd_lat
- << " Geocentric lat = " << l->moon_gc_lat );
+ << " Geocentric lat = " << l->get_moon_gc_lat() );
- // I think this will work better for generating the moon light vector
- l->moon_vec[0] = l->fg_moonpos.x();
- l->moon_vec[1] = l->fg_moonpos.y();
- l->moon_vec[2] = l->fg_moonpos.z();
- MAT3_NORMALIZE_VEC(l->moon_vec, ntmp);
- MAT3_SCALE_VEC(l->moon_vec_inv, l->moon_vec, -1.0);
+ // update the sun light vector
+ sgSetVec4( l->moon_vec(), l->get_moonpos().x(),
+ l->get_moonpos().y(), l->get_moonpos().z(), 0.0 );
+ sgNormalizeVec4( l->moon_vec() );
+ sgCopyVec4( l->moon_vec_inv(), l->moon_vec() );
+ sgNegateVec4( l->moon_vec_inv() );
// make sure these are directional light sources only
- l->moon_vec[3] = 0.0;
- l->moon_vec_inv[3] = 0.0;
-
- // printf(" l->moon_vec = %.2f %.2f %.2f\n", l->moon_vec[0], l->moon_vec[1],
- // l->moon_vec[2]);
+ l->moon_vec()[3] = l->moon_vec_inv()[3] = 0.0;
+ // cout << " l->moon_vec = " << l->moon_vec[0] << "," << l->moon_vec[1]
+ // << ","<< l->moon_vec[2] << endl;
// calculate the moon's relative angle to local up
- MAT3_COPY_VEC(nup, v->get_local_up());
- nmoon[0] = l->fg_moonpos.x();
- nmoon[1] = l->fg_moonpos.y();
- nmoon[2] = l->fg_moonpos.z();
- MAT3_NORMALIZE_VEC(nup, ntmp);
- MAT3_NORMALIZE_VEC(nmoon, ntmp);
-
- l->moon_angle = acos(MAT3_DOT_PRODUCT(nup, nmoon));
- // printf(" MOON ANGLE relative to current location = %.3f rads.\n",
- // l->moon_angle);
+ sgCopyVec3( nup, v->get_world_up() );
+ sgSetVec3( nmoon, l->get_moonpos().x(),
+ l->get_moonpos().y(), l->get_moonpos().z() );
+ sgNormalizeVec3(nup);
+ sgNormalizeVec3(nmoon);
+ // cout << "nup = " << nup[0] << "," << nup[1] << ","
+ // << nup[2] << endl;
+ // cout << "nmoon = " << nmoon[0] << "," << nmoon[1] << ","
+ // << nmoon[2] << endl;
+
+ l->set_moon_angle( acos( sgScalarProductVec3( nup, nmoon ) ) );
+ SG_LOG( SG_EVENT, SG_INFO, "moon angle relative to current location = "
+ << l->get_moon_angle() );
// calculate vector to moon's position on the earth's surface
- rel_moonpos = l->fg_moonpos - (v->get_view_pos() + scenery.center);
- v->set_to_moon( rel_moonpos.x(), rel_moonpos.y(), rel_moonpos.z() );
+ Point3D vp( v->get_view_pos()[0],
+ v->get_view_pos()[1],
+ v->get_view_pos()[2] );
+ rel_moonpos = l->get_moonpos()-(vp + globals->get_scenery()->get_center());
+ sgSetVec3( to_moon, rel_moonpos.x(), rel_moonpos.y(), rel_moonpos.z() );
// printf( "Vector to moon = %.2f %.2f %.2f\n",
- // v->to_moon[0], v->to_moon[1], v->to_moon[2]);
-
- // make a vector to the current view position
- Point3D view_pos = v->get_view_pos();
- MAT3_SET_VEC(v0, view_pos.x(), view_pos.y(), view_pos.z());
+ // to_moon[0], to_moon[1], to_moon[2]);
// Given a vector from the view position to the point on the
// earth's surface the moon is directly over, map into onto the
// local plane representing "horizontal".
- map_vec_onto_cur_surface_plane( v->get_local_up(), v0, v->get_to_moon(),
- surface_to_moon );
- MAT3_NORMALIZE_VEC(surface_to_moon, ntmp);
- v->set_surface_to_moon( surface_to_moon[0], surface_to_moon[1],
- surface_to_moon[2] );
- // printf("Surface direction to moon is %.2f %.2f %.2f\n",
- // v->surface_to_moon[0], v->surface_to_moon[1], v->surface_to_moon[2]);
- // printf("Should be close to zero = %.2f\n",
- // MAT3_DOT_PRODUCT(v->local_up, v->surface_to_moon));
+
+ sgmap_vec_onto_cur_surface_plane( v->get_world_up(), v->get_view_pos(),
+ to_moon, surface_to_moon );
+ sgNormalizeVec3(surface_to_moon);
+ // cout << "(sg) Surface direction to moon is "
+ // << surface_to_moon[0] << ","
+ // << surface_to_moon[1] << ","
+ // << surface_to_moon[2] << endl;
+ // cout << "Should be close to zero = "
+ // << sgScalarProductVec3(nup, surface_to_moon) << endl;
// calculate the angle between v->surface_to_moon and
// v->surface_east. We do this so we can sort out the acos()
// ambiguity. I wish I could think of a more efficient way ... :-(
- east_dot = MAT3_DOT_PRODUCT( surface_to_moon, v->get_surface_east() );
- // printf(" East dot product = %.2f\n", east_dot);
+ east_dot = sgScalarProductVec3( surface_to_moon, v->get_surface_east() );
+ // cout << " East dot product = " << east_dot << endl;
// calculate the angle between v->surface_to_moon and
// v->surface_south. this is how much we have to rotate the sky
// for it to align with the moon
- dot = MAT3_DOT_PRODUCT( surface_to_moon, v->get_surface_south() );
- // printf(" Dot product = %.2f\n", dot);
+ dot = sgScalarProductVec3( surface_to_moon, v->get_surface_south() );
+ // cout << " Dot product = " << dot << endl;
+
if ( east_dot >= 0 ) {
- l->moon_rotation = acos(dot);
+ l->set_moon_rotation( acos(dot) );
} else {
- l->moon_rotation = -acos(dot);
+ l->set_moon_rotation( -acos(dot) );
}
- // printf(" Sky needs to rotate = %.3f rads = %.1f degrees.\n",
- // angle, angle * RAD_TO_DEG); */
+ // cout << " Sky needs to rotate = " << angle << " rads = "
+ // << angle * SGD_RADIANS_TO_DEGREES << " degrees." << endl;
}