From 6aada9a3c65602011253e9e33fc3b4597f4b743c Mon Sep 17 00:00:00 2001 From: "Gergely POLONKAI (W00d5t0ck)" Date: Sun, 1 Sep 2013 22:30:51 +0200 Subject: [PATCH] Outsourced house cusp position calculations to SWE-GLib. --- src/astrognome.c | 23 +++++++++-- swe-glib/src/gswe-moment.c | 81 ++++++++++++++++++++++++++++++++++++-- swe-glib/src/gswe-moment.h | 4 ++ swe-glib/src/gswe-types.h | 2 + swe-glib/src/swe-glib.h | 2 + 5 files changed, 106 insertions(+), 6 deletions(-) diff --git a/src/astrognome.c b/src/astrognome.c index 46479aa..4cfe170 100644 --- a/src/astrognome.c +++ b/src/astrognome.c @@ -3,6 +3,7 @@ #include "calculate.h" #include "../swe/src/swephexp.h" +#include "../swe-glib/src/swe-glib.h" #define EPHEDIR "/home/polesz/Projektek/c/astrognome/swe/data" @@ -309,6 +310,17 @@ free_planet_data(gpointer data) g_free(planetData); } +void +print_house_cusp(gpointer data, gpointer user_data) +{ + gdouble *cusp = data; + gint *house = user_data; + + printf("House %2d..: %s (%f)\n", *house, signName[get_sign(*cusp)], *cusp); + + *house += 1; +} + int main(int argc, char *argv[]) { @@ -338,6 +350,8 @@ main(int argc, char *argv[]) signData_t *signData; GList *planetIdList; struct aspect_check_data aspectCheckData; + GsweTimestamp *timestamp; + GsweMoment *moment; #if 1 year = 1983; @@ -395,6 +409,10 @@ main(int argc, char *argv[]) ADD_SIGN(signDataTable, signData, SIGN_PISCES, ELEMENT_WATER, TYPE_MUTABLE); swe_set_ephe_path(EPHEDIR); + gswe_init(EPHEDIR); + + timestamp = gswe_timestamp_new_from_gregorian_full(year, month, day, hour, min, sec, 0, 1.0); + moment = gswe_moment_new_full(timestamp, lon, lat, alt, GSWE_HOUSE_SYSTEM_PLACIDUS); if (set_location_and_time(lon, lat, alt, year, month, day, hour, min, sec, timezone, &te) == 0) { return 1; @@ -406,9 +424,8 @@ main(int argc, char *argv[]) swe_houses(te, lat, lon, 'P', cusps, ascmc); - for (p = 1; p < 13; p++) { - printf("House %2d..: %s (%f)\n", p, signName[get_sign(cusps[p])], cusps[p]); - } + p = 1; + g_list_foreach(gswe_moment_get_house_cusps(moment), print_house_cusp, &p); printf("\nPLANETS AND POINTS\n==================\n\n"); diff --git a/swe-glib/src/gswe-moment.c b/swe-glib/src/gswe-moment.c index b82b6ab..c0ac960 100644 --- a/swe-glib/src/gswe-moment.c +++ b/swe-glib/src/gswe-moment.c @@ -1,11 +1,28 @@ -#include +#include "swe-glib.h" +#include "gswe-types.h" #include "gswe-moment.h" +#include "../../swe/src/swephexp.h" + #define GSWE_MOMENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), GSWE_TYPE_MOMENT, GsweMomentPrivate)) +/** + * GsweMomentPrivate: + * @timestamp: a #GsweTimestmp object representing the current local time at + * the given position specified by @coordinates. Be warned though, + * that the time zone is NOT checked against the coordinates! + * @coordinates: the coordinates of the observers position + * @revision: an internal counter which is incremented whenever the timestamp + * or the coordinates change. Planetary positions are recalculated + * if this number changes + */ struct _GsweMomentPrivate { GsweTimestamp *timestamp; GsweCoordinates coordinates; + GsweHouseSystem house_system; + guint revision; + GList *house_list; + guint house_revision; }; enum { @@ -67,6 +84,9 @@ gswe_moment_init(GsweMoment *moment) moment->priv = GSWE_MOMENT_GET_PRIVATE(moment); moment->priv->timestamp = NULL; + moment->priv->house_list = NULL; + moment->priv->planet_list = NULL; + moment->priv->revision = 1; //moment->priv->a_string = g_strdup("Maman"); } @@ -74,6 +94,8 @@ gswe_moment_init(GsweMoment *moment) static void gswe_moment_timestamp_changed(GsweMoment *moment, gpointer data) { + moment->priv->revision++; + gswe_moment_emit_changed(moment); } static void @@ -91,9 +113,9 @@ gswe_moment_dispose(GObject *gobject) static void gswe_moment_finalize(GObject *gobject) { - //GsweMoment *moment = GSWE_MOMENT(gobject); + GsweMoment *moment = GSWE_MOMENT(gobject); - //g_free(moment->priv->a_string); + g_list_free_full(moment->priv->house_list, g_free); G_OBJECT_CLASS(gswe_moment_parent_class)->finalize(gobject); } @@ -175,3 +197,56 @@ gswe_moment_new(void) return (GsweMoment *)g_object_new(GSWE_TYPE_MOMENT, NULL); } +GsweMoment * +gswe_moment_new_full(GsweTimestamp *timestamp, gdouble longitude, gdouble latitude, gdouble altitude, GsweHouseSystem house_system) +{ + GsweMoment *moment = gswe_moment_new(); + + moment->priv->timestamp = timestamp; + g_object_ref(timestamp); + g_signal_connect(G_OBJECT(timestamp), "changed", G_CALLBACK(gswe_moment_timestamp_changed), NULL); + moment->priv->coordinates.longitude = longitude; + moment->priv->coordinates.latitude = latitude; + moment->priv->coordinates.altitude = altitude; + moment->priv->house_system = house_system; + + return moment; +} + +static void +gswe_moment_calculate_house_positions(GsweMoment *moment) +{ + gdouble cusps[13], + ascmc[10]; + gint i; + GsweHouseSystemInfo *house_system_data; + + if ((house_system_data = g_hash_table_lookup(gswe_house_system_info_table, GINT_TO_POINTER(moment->priv->house_system))) == NULL) { + g_error("Unknown house system!"); + } + + swe_houses(gswe_timestamp_get_julian_day(moment->priv->timestamp), moment->priv->coordinates.latitude, moment->priv->coordinates.longitude, house_system_data->sweph_id, cusps, ascmc); + + g_list_free_full(moment->priv->house_list, g_free); + moment->priv->house_list = NULL; + + for (i = 12; i >= 1; i--) { + gdouble *cusp = g_new0(gdouble, 1); + + *cusp = cusps[i]; + moment->priv->house_list = g_list_prepend(moment->priv->house_list, cusp); + } + + moment->priv->house_revision = moment->priv->revision; +} + +GList * +gswe_moment_get_house_cusps(GsweMoment *moment) +{ + if (moment->priv->house_revision != moment->priv->revision) { + gswe_moment_calculate_house_positions(moment); + } + + return moment->priv->house_list; +} + diff --git a/swe-glib/src/gswe-moment.h b/swe-glib/src/gswe-moment.h index 78c47c4..3574e0a 100644 --- a/swe-glib/src/gswe-moment.h +++ b/swe-glib/src/gswe-moment.h @@ -31,12 +31,14 @@ GQuark gswe_moment_error_quark(void); * GsweCoordinates: * @longitude: longitude part of the coordinates * @latitude: latitude part of the coordinates + * @altitude: altitude relative to sea level * * GsweCoordinates specifies an exact point on Earth's surface */ typedef struct _GsweCoordinates { gdouble longitude; gdouble latitude; + gdouble altitude; } GsweCoordinates; struct _GsweMoment { @@ -64,7 +66,9 @@ GType gswe_moment_get_type(void); /* Method definitions */ GsweMoment *gswe_moment_new(void); +GsweMoment *gswe_moment_new_full(GsweTimestamp *timestamp, gdouble longitude, gdouble latitude, gdouble altitude, GsweHouseSystem house_system); void gswe_moment_set_timestamp(GsweMoment *moment, GsweTimestamp *timestamp); +GList *gswe_moment_get_house_cusps(GsweMoment *moment); #endif /* __GSWE_MOMENT_H__ */ diff --git a/swe-glib/src/gswe-types.h b/swe-glib/src/gswe-types.h index e3a15d7..c2b77d7 100644 --- a/swe-glib/src/gswe-types.h +++ b/swe-glib/src/gswe-types.h @@ -1,6 +1,8 @@ #ifndef __SWE_GLIB_GSWE_PLANETS_H__ #define __SWE_GLIB_GSWE_PLANETS_H__ +#include + typedef enum { GSWE_PLANET_NONE, GSWE_PLANET_SUN, diff --git a/swe-glib/src/swe-glib.h b/swe-glib/src/swe-glib.h index b9f4645..b5c2570 100644 --- a/swe-glib/src/swe-glib.h +++ b/swe-glib/src/swe-glib.h @@ -4,8 +4,10 @@ #include #include "gswe-types.h" #include "gswe-timestamp.h" +#include "gswe-moment.h" extern GHashTable *gswe_planet_info_table; +extern GHashTable *gswe_house_system_info_table; void gswe_init(gchar *sweph_path);