[libchamplain] Some changes of gdouble to gint and vice versa



commit bd1f3ba178fd715a86a713e1204214a1b121ff32
Author: JiÅ?í Techet <techet gmail com>
Date:   Sun Feb 20 17:21:46 2011 +0100

    Some changes of gdouble to gint and vice versa
    
    In some cases we need better precision even for pixels (when
    we need to perform some further calculations), in other cases
    we need ints to have the contents pixel-aligned.

 champlain/champlain-defines.h      |    8 ++--
 champlain/champlain-map-source.c   |   32 ++++++++++++++------
 champlain/champlain-map-source.h   |    8 ++--
 champlain/champlain-marker-layer.c |    4 +-
 champlain/champlain-path-layer.c   |    2 +-
 champlain/champlain-view.c         |   57 +++++++++++++++++-------------------
 champlain/champlain-view.h         |    2 +-
 7 files changed, 61 insertions(+), 52 deletions(-)
---
diff --git a/champlain/champlain-defines.h b/champlain/champlain-defines.h
index c2e4514..d8ad82f 100644
--- a/champlain/champlain-defines.h
+++ b/champlain/champlain-defines.h
@@ -25,10 +25,10 @@
 
 #define CHAMPLAIN_API __attribute__((visibility ("default")))
 
-#define CHAMPLAIN_MIN_LATITUDE -90
-#define CHAMPLAIN_MAX_LATITUDE 90
-#define CHAMPLAIN_MIN_LONGITUDE -180
-#define CHAMPLAIN_MAX_LONGITUDE 180
+#define CHAMPLAIN_MIN_LATITUDE   -90.0
+#define CHAMPLAIN_MAX_LATITUDE    90.0
+#define CHAMPLAIN_MIN_LONGITUDE -180.0
+#define CHAMPLAIN_MAX_LONGITUDE  180.0
 
 typedef struct _ChamplainView ChamplainView;
 typedef struct _ChamplainViewClass ChamplainViewClass;
diff --git a/champlain/champlain-map-source.c b/champlain/champlain-map-source.c
index 9556b83..a50fa03 100644
--- a/champlain/champlain-map-source.c
+++ b/champlain/champlain-map-source.c
@@ -492,13 +492,15 @@ champlain_map_source_get_projection (ChamplainMapSource *map_source)
  *
  * Since: 0.4
  */
-guint
+gdouble
 champlain_map_source_get_x (ChamplainMapSource *map_source,
     guint zoom_level,
     gdouble longitude)
 {
   g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0);
 
+  longitude = CLAMP (longitude, CHAMPLAIN_MIN_LONGITUDE, CHAMPLAIN_MAX_LONGITUDE);
+
   /* FIXME: support other projections */
   return ((longitude + 180.0) / 360.0 * pow (2.0, zoom_level)) * champlain_map_source_get_tile_size (map_source);
 }
@@ -517,13 +519,15 @@ champlain_map_source_get_x (ChamplainMapSource *map_source,
  *
  * Since: 0.4
  */
-guint
+gdouble
 champlain_map_source_get_y (ChamplainMapSource *map_source,
     guint zoom_level,
     gdouble latitude)
 {
   g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0);
 
+  latitude = CLAMP (latitude, CHAMPLAIN_MIN_LATITUDE, CHAMPLAIN_MAX_LATITUDE);
+  
   /* FIXME: support other projections */
   return ((1.0 - log (tan (latitude * M_PI / 180.0) + 1.0 / cos (latitude * M_PI / 180.0)) / M_PI) / 
           2.0 * pow (2.0, zoom_level)) * champlain_map_source_get_tile_size (map_source);
@@ -546,12 +550,16 @@ champlain_map_source_get_y (ChamplainMapSource *map_source,
 gdouble
 champlain_map_source_get_longitude (ChamplainMapSource *map_source,
     guint zoom_level,
-    guint x)
+    gdouble x)
 {
-  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0);
+  gdouble longitude;
+    
+  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0.0);
   /* FIXME: support other projections */
-  gdouble dx = (float) x / champlain_map_source_get_tile_size (map_source);
-  return dx / pow (2.0, zoom_level) * 360.0 - 180;
+  gdouble dx = (gdouble) x / champlain_map_source_get_tile_size (map_source);
+  longitude =  dx / pow (2.0, zoom_level) * 360.0 - 180.0;
+  
+  return CLAMP (longitude, CHAMPLAIN_MIN_LONGITUDE, CHAMPLAIN_MAX_LONGITUDE);
 }
 
 
@@ -571,13 +579,17 @@ champlain_map_source_get_longitude (ChamplainMapSource *map_source,
 gdouble
 champlain_map_source_get_latitude (ChamplainMapSource *map_source,
     guint zoom_level,
-    guint y)
+    gdouble y)
 {
-  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0);
+  gdouble latitude;
+  
+  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0.0);
   /* FIXME: support other projections */
-  gdouble dy = (float) y / champlain_map_source_get_tile_size (map_source);
+  gdouble dy = (gdouble) y / champlain_map_source_get_tile_size (map_source);
   gdouble n = M_PI - 2.0 * M_PI * dy / pow (2.0, zoom_level);
-  return 180.0 / M_PI * atan (0.5 * (exp (n) - exp (-n)));
+  latitude = 180.0 / M_PI * atan (0.5 * (exp (n) - exp (-n)));
+  
+  return CLAMP (latitude, CHAMPLAIN_MIN_LATITUDE, CHAMPLAIN_MAX_LATITUDE);
 }
 
 
diff --git a/champlain/champlain-map-source.h b/champlain/champlain-map-source.h
index eb69ce0..fa3de84 100644
--- a/champlain/champlain-map-source.h
+++ b/champlain/champlain-map-source.h
@@ -104,18 +104,18 @@ guint champlain_map_source_get_max_zoom_level (ChamplainMapSource *map_source);
 guint champlain_map_source_get_tile_size (ChamplainMapSource *map_source);
 ChamplainMapProjection champlain_map_source_get_projection (ChamplainMapSource *map_source);
 
-guint champlain_map_source_get_x (ChamplainMapSource *map_source,
+gdouble champlain_map_source_get_x (ChamplainMapSource *map_source,
     guint zoom_level,
     gdouble longitude);
-guint champlain_map_source_get_y (ChamplainMapSource *map_source,
+gdouble champlain_map_source_get_y (ChamplainMapSource *map_source,
     guint zoom_level,
     gdouble latitude);
 gdouble champlain_map_source_get_longitude (ChamplainMapSource *map_source,
     guint zoom_level,
-    guint x);
+    gdouble x);
 gdouble champlain_map_source_get_latitude (ChamplainMapSource *map_source,
     guint zoom_level,
-    guint y);
+    gdouble y);
 guint champlain_map_source_get_row_count (ChamplainMapSource *map_source,
     guint zoom_level);
 guint champlain_map_source_get_column_count (ChamplainMapSource *map_source,
diff --git a/champlain/champlain-marker-layer.c b/champlain/champlain-marker-layer.c
index 1fabb0a..3f002f5 100644
--- a/champlain/champlain-marker-layer.c
+++ b/champlain/champlain-marker-layer.c
@@ -380,7 +380,7 @@ static void
 set_marker_position (ChamplainMarkerLayer *layer, ChamplainMarker *marker)
 {
   ChamplainMarkerLayerPrivate *priv = layer->priv;
-  gdouble x, y, origin_x, origin_y;
+  gint x, y, origin_x, origin_y;
   
   /* layer not yet added to the view */
   if (priv->view == NULL)
@@ -928,7 +928,7 @@ get_bounding_box (ChamplainLayer *layer)
   GList *elem;
   ChamplainBoundingBox *bbox;
   GList *markers;
-  double x, y;
+  gdouble x, y;
 
   g_return_val_if_fail (CHAMPLAIN_IS_MARKER_LAYER (layer), NULL);
   
diff --git a/champlain/champlain-path-layer.c b/champlain/champlain-path-layer.c
index b377f44..806f89c 100644
--- a/champlain/champlain-path-layer.c
+++ b/champlain/champlain-path-layer.c
@@ -672,7 +672,7 @@ redraw_path (ChamplainPathLayer *layer)
   gfloat width, height;
   GList *elem;
   ChamplainView *view = priv->view;
-  gdouble x, y;
+  gint x, y;
   guint last_width, last_height;
   
   /* layer not yet added to the view */
diff --git a/champlain/champlain-view.c b/champlain/champlain-view.c
index 600f847..5937fba 100644
--- a/champlain/champlain-view.c
+++ b/champlain/champlain-view.c
@@ -151,18 +151,18 @@ struct _ChamplainViewPrivate
   GTimer *update_viewport_timer;
 
   /* Hack to get smaller x,y coordinates as the clutter limit is G_MAXINT16 */
-  gdouble anchor_x;
-  gdouble anchor_y;
+  gint anchor_x;
+  gint anchor_y;
 
-  gdouble anchor_zoom_level; /* the zoom_level for which the current anchor has
+  guint anchor_zoom_level; /* the zoom_level for which the current anchor has
                                 been computed for */
 
   ClutterActor *kinetic_scroll; /* Contains the viewport */
   ClutterActor *viewport;  /* Contains the map_layer, license and markers */
   ClutterActor *map_layer; /* Contains tiles actors (grouped by zoom level) */
   
-  gdouble viewport_x;
-  gdouble viewport_y;
+  gint viewport_x;
+  gint viewport_y;
   gint viewport_width;
   gint viewport_height;
   
@@ -606,7 +606,7 @@ champlain_view_allocate (ClutterActor *actor,
   ChamplainView *view = CHAMPLAIN_VIEW (actor);
   ChamplainViewPrivate *priv = view->priv;
   ClutterActorBox child_box;
-  guint width, height;
+  gint width, height;
 
   /* Chain up */
   CLUTTER_ACTOR_CLASS (champlain_view_parent_class)->allocate (actor, box, flags);
@@ -1329,7 +1329,7 @@ view_update_anchor (ChamplainView *view,
           if (priv->anchor_y < 0)
             priv->anchor_y = 0;
 
-          DEBUG ("New Anchor (%f, %f) at (%d, %d)", priv->anchor_x, priv->anchor_y, x, y);
+          DEBUG ("New Anchor (%d, %d) at (%d, %d)", priv->anchor_x, priv->anchor_y, x, y);
         }
       else
         {
@@ -1497,8 +1497,9 @@ champlain_view_go_to_with_duration (ChamplainView *view,
   ctx = g_slice_new (GoToContext);
   ctx->from_latitude = priv->latitude;
   ctx->from_longitude = priv->longitude;
-  ctx->to_latitude = latitude;
-  ctx->to_longitude = longitude;
+  ctx->to_latitude = CLAMP (latitude, CHAMPLAIN_MIN_LATITUDE, CHAMPLAIN_MAX_LATITUDE);;
+  ctx->to_longitude = CLAMP (longitude, CHAMPLAIN_MIN_LONGITUDE, CHAMPLAIN_MAX_LONGITUDE);
+
   ctx->view = view;
 
   /* We keep a reference for stop */
@@ -1727,13 +1728,13 @@ gdouble
 champlain_view_x_to_longitude (ChamplainView *view,
     gdouble x)
 {
-  DEBUG_LOG ()
-
-  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), 0.0);
-  
   ChamplainViewPrivate *priv = view->priv;
   gdouble longitude;
 
+  DEBUG_LOG ()
+
+  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), 0.0);  
+  
   longitude = champlain_map_source_get_longitude (priv->map_source,
       priv->zoom_level, 
       x + priv->viewport_x + priv->anchor_x);
@@ -1757,12 +1758,12 @@ gdouble
 champlain_view_y_to_latitude (ChamplainView *view,
     gdouble y)
 {
+  ChamplainViewPrivate *priv = view->priv;
+  gdouble latitude;
+  
   DEBUG_LOG ()
 
   g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), 0.0);
-  
-  ChamplainViewPrivate *priv = view->priv;
-  gdouble latitude;
 
   latitude = champlain_map_source_get_latitude (priv->map_source,
       priv->zoom_level, 
@@ -1787,18 +1788,16 @@ gdouble
 champlain_view_longitude_to_x (ChamplainView *view, 
     gdouble longitude)
 {
+  ChamplainViewPrivate *priv = view->priv;
+  gdouble x;
+  
   DEBUG_LOG ()
 
   g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), 0);
 
-  ChamplainViewPrivate *priv = view->priv;
-  gdouble x;
-
   x = champlain_map_source_get_x (priv->map_source, priv->zoom_level, longitude);
-  x -= priv->anchor_x;
-  x -= priv->viewport_x;
 
-  return x;
+  return x - priv->anchor_x - priv->viewport_x;
 }
 
 
@@ -1817,18 +1816,16 @@ gdouble
 champlain_view_latitude_to_y (ChamplainView *view, 
     gdouble latitude)
 {
+  ChamplainViewPrivate *priv = view->priv;
+  gdouble y;
+  
   DEBUG_LOG ()
 
   g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), 0);
 
-  ChamplainViewPrivate *priv = view->priv;
-  gdouble y;
-
   y = champlain_map_source_get_y (priv->map_source, priv->zoom_level, latitude);
-  y -= priv->anchor_y;
-  y -= priv->viewport_y;
 
-  return y;
+  return y - priv->anchor_y - priv->viewport_y;
 }
 
 
@@ -1844,8 +1841,8 @@ champlain_view_latitude_to_y (ChamplainView *view,
  */
 void 
 champlain_view_get_viewport_origin (ChamplainView *view, 
-    gdouble *x, 
-    gdouble *y)
+    gint *x, 
+    gint *y)
 {
   DEBUG_LOG ()
 
diff --git a/champlain/champlain-view.h b/champlain/champlain-view.h
index 69f02db..2431d01 100644
--- a/champlain/champlain-view.h
+++ b/champlain/champlain-view.h
@@ -131,7 +131,7 @@ gdouble champlain_view_longitude_to_x (ChamplainView *view,
 gdouble champlain_view_latitude_to_y (ChamplainView *view, 
     gdouble latitude);
 
-void champlain_view_get_viewport_origin (ChamplainView *view, gdouble *x, gdouble *y);
+void champlain_view_get_viewport_origin (ChamplainView *view, gint *x, gint *y);
 
 void champlain_view_bin_layout_add (ChamplainView *view,
   ClutterActor *child,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]