From f625b27f585a6c7f72dbba28dbcdfd4ae5664a61 Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Fri, 27 Jul 2007 10:49:14 +0300 Subject: [PATCH] Add osm_deinit Start to add place cache so we don't need to query to the database every time we move. Change the distance2line function to take gdouble instead of ints. Change helper to cast int->double --- src/osm-db.c | 55 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/src/osm-db.c b/src/osm-db.c index 7c916f2..1c03dcc 100644 --- a/src/osm-db.c +++ b/src/osm-db.c @@ -30,6 +30,11 @@ gboolean osm_way_get_nodes(osm_way *w); gboolean osm_way_get_name(osm_way *w); gboolean osm_way_get_ref(osm_way *w); +/* Cache hash tables */ +static GHashTable *_place_cache; + +/*****************************************************************************/ + gboolean osm_db_prepare(sqlite3 *db) { @@ -83,11 +88,41 @@ return TRUE; } gboolean -osm_init() +osm_init(void) { +_place_cache=g_hash_table_new(g_direct_hash, g_direct_equal); return TRUE; } +void +osm_deinit(void) +{ +g_hash_table_destroy(_place_cache); +} + +/*****************************************************************************/ + +static osm_place * +osm_place_cache_lookup(guint32 id) +{ +return g_hash_table_lookup(_place_cache, GINT_TO_POINTER(id)); +} + +static void +osm_place_cache_add(guint32 id, osm_place *p) +{ +if (osm_place_cache_lookup(id)==NULL) + g_hash_table_insert(_place_cache, GINT_TO_POINTER(id), p); +} + +static void +osm_place_cache_gc(void) +{ +g_hash_table_foreach_remove(_place_cache, g_free, NULL); +} + +/*****************************************************************************/ + /** * Free way nodes list */ @@ -259,23 +294,25 @@ y=y2-y1; return sqrt((x*x)+(y*y)); } -gboolean distance_point_to_line(gint x, gint y, gint x1, gint y1, gint x2, gint y2, gdouble *d) +gboolean distance_point_to_line(gdouble x, gdouble y, gdouble x1, gdouble y1, gdouble x2, gdouble y2, gdouble *d) { gdouble lm,u,tmp; gdouble ix,iy; -lm=magnitude((gdouble)x1,(gdouble)y1,(gdouble)x2,(gdouble)y2); - -tmp=(gdouble)((x-x1)*(x2-x1))+((y-y1)*(y2-y1)); +lm=magnitude(x1,y1,x2,y2); +if (lm==0.0f) + return FALSE; + +tmp=((x-x1)*(x2-x1))+((y-y1)*(y2-y1)); u=tmp/(lm*lm); if (u<0.0f || u>1.0f) return FALSE; -ix=(gdouble)x1+u*(gdouble)(x2-x1); -iy=(gdouble)y1+u*(gdouble)(y2-y1); +ix=x1+u*(x2-x1); +iy=y1+u*(y2-y1); -*d=magnitude((gdouble)x,(gdouble)y, ix, iy); +*d=magnitude(x,y, ix, iy); return TRUE; } @@ -285,7 +322,7 @@ gboolean osm_way_distance(gint lat, gint lon, osm_way_node *f, osm_way_node *t, if (!f || !t) return FALSE; -return distance_point_to_line(lon, lat, f->lon, f->lat, t->lon, t->lat, d); +return distance_point_to_line((gdouble)lon, (gdouble)lat, (gdouble)f->lon, (gdouble)f->lat, (gdouble)t->lon, (gdouble)t->lat, d); } /** -- 2.39.5