]> git.street.me.uk Git - andy/viking.git/blobdiff - src/vikviewport.c
Add OSRM routing engine
[andy/viking.git] / src / vikviewport.c
index b294e2e118b0b65fd49178088bd4123104edb59f..d21db40dfc87bf7a667256292e0e11089ce4436e 100644 (file)
 /* for ALTI_TO_MPP */
 #include "globals.h"
 
+#define MERCATOR_FACTOR(x) ( (65536.0 / 180 / (x)) * 256.0 )
+
 static gdouble EASTING_OFFSET = 500000.0;
 
 static gint PAD = 10;
 
-static void viewport_class_init ( VikViewportClass *klass );
-static void viewport_init ( VikViewport *vvp );
 static void viewport_finalize ( GObject *gob );
 static void viewport_utm_zone_check ( VikViewport *vvp );
 
@@ -72,9 +72,11 @@ struct _VikViewport {
   GtkDrawingArea drawing_area;
   GdkPixmap *scr_buffer;
   gint width, height;
+  gint width_2, height_2; // Half of the normal width and height
   VikCoord center;
   VikCoordMode coord_mode;
   gdouble xmpp, ympp;
+  gdouble xmfactor, ymfactor;
 
   GdkPixbuf *alpha_pixbuf;
   guint8 alpha_pixbuf_width;
@@ -100,12 +102,6 @@ struct _VikViewport {
   /* subset of coord types. lat lon can be plotted in 2 ways, google or exp. */
   VikViewportDrawMode drawmode;
 
-  /* handy conversion factors which make google plotting extremely fast */
-  gdouble google_calcx_fact;
-  gdouble google_calcy_fact;
-  gdouble google_calcx_rev_fact;
-  gdouble google_calcy_rev_fact;
-
   /* trigger stuff */
   gpointer trigger;
   GdkPixmap *snapshot_buffer;
@@ -131,31 +127,10 @@ viewport_utm_zone_width ( VikViewport *vvp )
     return 0.0;
 }
 
+G_DEFINE_TYPE (VikViewport, vik_viewport, GTK_TYPE_DRAWING_AREA)
 
-GType vik_viewport_get_type (void)
-{
-  static GType vvp_type = 0;
-
-  if (!vvp_type)
-  {
-    static const GTypeInfo vvp_info = 
-    {
-      sizeof (VikViewportClass),
-      NULL, /* base_init */
-      NULL, /* base_finalize */
-      (GClassInitFunc) viewport_class_init,
-      NULL, /* class_finalize */
-      NULL, /* class_data */
-      sizeof (VikViewport),
-      0,
-      (GInstanceInitFunc) viewport_init,
-    };
-    vvp_type = g_type_register_static ( GTK_TYPE_DRAWING_AREA, "VikViewport", &vvp_info, 0 );
-  }
-  return vvp_type;
-}
-
-static void viewport_class_init ( VikViewportClass *klass )
+static void
+vik_viewport_class_init ( VikViewportClass *klass )
 {
   /* Destructor */
   GObjectClass *object_class;
@@ -173,7 +148,8 @@ VikViewport *vik_viewport_new ()
   return vv;
 }
 
-static void viewport_init ( VikViewport *vvp )
+static void
+vik_viewport_init ( VikViewport *vvp )
 {
   viewport_init_ra();
 
@@ -186,6 +162,8 @@ static void viewport_init ( VikViewport *vvp )
   /* TODO: not static */
   vvp->xmpp = 4.0;
   vvp->ympp = 4.0;
+  vvp->xmfactor = MERCATOR_FACTOR (vvp->xmpp);
+  vvp->ymfactor = MERCATOR_FACTOR (vvp->ympp);
   vvp->coord_mode = VIK_COORD_LATLON;
   vvp->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
   vvp->center.mode = VIK_COORD_LATLON;
@@ -213,7 +191,11 @@ static void viewport_init ( VikViewport *vvp )
 
   g_signal_connect (G_OBJECT(vvp), "configure_event", G_CALLBACK(vik_viewport_configure), NULL);
 
+#if GTK_CHECK_VERSION (2,18,0)
+  gtk_widget_set_can_focus ( GTK_WIDGET(vvp), TRUE );
+#else
   GTK_WIDGET_SET_FLAGS(vvp, GTK_CAN_FOCUS); /* allow VVP to have focus -- enabling key events, etc */
+#endif
 }
 
 GdkColor *vik_viewport_get_background_gdkcolor ( VikViewport *vvp )
@@ -292,7 +274,7 @@ GdkGC *vik_viewport_new_gc ( VikViewport *vvp, const gchar *colorname, gint thic
   GdkGC *rv = NULL;
   GdkColor color;
 
-  rv = gdk_gc_new ( GTK_WIDGET(vvp)->window );
+  rv = gdk_gc_new ( gtk_widget_get_window(GTK_WIDGET(vvp)) );
   if ( gdk_color_parse ( colorname, &color ) )
     gdk_gc_set_rgb_fg_color ( rv, &color );
   else
@@ -305,7 +287,7 @@ GdkGC *vik_viewport_new_gc_from_color ( VikViewport *vvp, GdkColor *color, gint
 {
   GdkGC *rv;
 
-  rv = gdk_gc_new ( GTK_WIDGET(vvp)->window );
+  rv = gdk_gc_new ( gtk_widget_get_window(GTK_WIDGET(vvp)) );
   gdk_gc_set_rgb_fg_color ( rv, color );
   gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
   return rv;
@@ -315,14 +297,18 @@ void vik_viewport_configure_manually ( VikViewport *vvp, gint width, guint heigh
 {
   vvp->width = width;
   vvp->height = height;
+
+  vvp->width_2 = vvp->width/2;
+  vvp->height_2 = vvp->height/2;
+
   if ( vvp->scr_buffer )
     g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
-  vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
+  vvp->scr_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
 
   /* TODO trigger: only if this is enabled !!! */
   if ( vvp->snapshot_buffer )
     g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
-  vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
+  vvp->snapshot_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
 }
 
 
@@ -338,16 +324,19 @@ gboolean vik_viewport_configure ( VikViewport *vvp )
   vvp->width = GTK_WIDGET(vvp)->allocation.width;
   vvp->height = GTK_WIDGET(vvp)->allocation.height;
 
+  vvp->width_2 = vvp->width/2;
+  vvp->height_2 = vvp->height/2;
+
   if ( vvp->scr_buffer )
     g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
 
-  vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
+  vvp->scr_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
 
   /* TODO trigger: only if enabled! */
   if ( vvp->snapshot_buffer )
     g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
 
-  vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
+  vvp->snapshot_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
   /* TODO trigger */
 
   /* this is down here so it can get a GC (necessary?) */
@@ -436,7 +425,6 @@ void vik_viewport_draw_scale ( VikViewport *vvp )
     VikCoord left, right;
     gdouble unit, base, diff, old_unit, old_diff, ratio;
     gint odd, len, SCSIZE = 5, HEIGHT=10;
-    PangoFontDescription *pfd;
     PangoLayout *pl;
     gchar s[128];
 
@@ -481,18 +469,18 @@ void vik_viewport_draw_scale ( VikViewport *vvp )
     vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
                         PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
     /* black scale */
-    vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
+    vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
                         PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
-    vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
+    vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
                         PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
-    vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
+    vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
                         PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
     if (odd%2) {
       int i;
       for (i=1; i<5; i++) {
         vik_viewport_draw_line(vvp, vvp->scale_bg_gc, 
                             PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
-        vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
+        vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
                             PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
       }
     } else {
@@ -500,14 +488,12 @@ void vik_viewport_draw_scale ( VikViewport *vvp )
       for (i=1; i<10; i++) {
         vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
                             PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
-        vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
+        vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
                             PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
       }
     }
     pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL); 
-    pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
-    pango_layout_set_font_description (pl, pfd);
-    pango_font_description_free (pfd);
+    pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc);
 
     switch (dist_units) {
     case VIK_UNITS_DISTANCE_KILOMETRES:
@@ -533,7 +519,7 @@ void vik_viewport_draw_scale ( VikViewport *vvp )
       g_critical("Houston, we've had a problem. distance=%d", dist_units);
     }
     pango_layout_set_text(pl, s, -1);
-    vik_viewport_draw_layout(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
+    vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
                           PAD + len + PAD, vvp->height - PAD - 10, pl);
     g_object_unref(pl);
     pl = NULL;
@@ -544,7 +530,6 @@ void vik_viewport_draw_copyright ( VikViewport *vvp )
 {
   g_return_if_fail ( vvp != NULL );
 
-  PangoFontDescription *pfd;
   PangoLayout *pl;
   PangoRectangle ink_rect, logical_rect;
   gchar s[128] = "";
@@ -575,20 +560,17 @@ void vik_viewport_draw_copyright ( VikViewport *vvp )
 
   /* create pango layout */
   pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL); 
-  pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
-  pango_layout_set_font_description (pl, pfd);
-  pango_font_description_free (pfd);
-  pfd = NULL;
+  pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc);
   pango_layout_set_alignment ( pl, PANGO_ALIGN_RIGHT );
 
   /* Set the text */
   pango_layout_set_text(pl, s, -1);
 
   /* Use maximum of half the viewport width */
-  pango_layout_set_width ( pl, ( vvp->width / 2 - PAD ) * PANGO_SCALE );
+  pango_layout_set_width ( pl, ( vvp->width / 2 ) * PANGO_SCALE );
   pango_layout_get_pixel_extents(pl, &ink_rect, &logical_rect);
-  vik_viewport_draw_layout(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
-                          vvp->width / 2, vvp->height - PAD - logical_rect.height, pl);
+  vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
+                          vvp->width / 2, vvp->height - logical_rect.height, pl);
 
   /* Free memory */
   g_object_unref(pl);
@@ -623,7 +605,7 @@ void vik_viewport_draw_centermark ( VikViewport *vvp )
   const int gap = 4;
   int center_x = vvp->width/2;
   int center_y = vvp->height/2;
-  GdkGC * black_gc = GTK_WIDGET(&vvp->drawing_area)->style->black_gc;
+  GdkGC * black_gc = gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc;
 
   /* white back ground */
   vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x - len, center_y, center_x - gap, center_y);
@@ -669,7 +651,7 @@ gboolean vik_viewport_get_draw_highlight ( VikViewport *vvp )
 void vik_viewport_sync ( VikViewport *vvp )
 {
   g_return_if_fail ( vvp != NULL );
-  gdk_draw_drawable(GTK_WIDGET(vvp)->window, GTK_WIDGET(vvp)->style->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, 0, 0, vvp->width, vvp->height);
+  gdk_draw_drawable(gtk_widget_get_window(GTK_WIDGET(vvp)), gtk_widget_get_style(GTK_WIDGET(vvp))->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, 0, 0, vvp->width, vvp->height);
 }
 
 void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off )
@@ -677,7 +659,7 @@ void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off )
   gint x, y, wid, hei;
 
   g_return_if_fail ( vvp != NULL );
-  gdk_draw_drawable(GTK_WIDGET(vvp)->window, GTK_WIDGET(vvp)->style->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, x_off, y_off, vvp->width, vvp->height);
+  gdk_draw_drawable(gtk_widget_get_window(GTK_WIDGET(vvp)), gtk_widget_get_style(GTK_WIDGET(vvp))->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, x_off, y_off, vvp->width, vvp->height);
 
   if (x_off >= 0) {
     x = 0;
@@ -700,8 +682,11 @@ void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off )
 void vik_viewport_set_zoom ( VikViewport *vvp, gdouble xympp )
 {
   g_return_if_fail ( vvp != NULL );
-  if ( xympp >= VIK_VIEWPORT_MIN_ZOOM && xympp <= VIK_VIEWPORT_MAX_ZOOM )
+  if ( xympp >= VIK_VIEWPORT_MIN_ZOOM && xympp <= VIK_VIEWPORT_MAX_ZOOM ) {
     vvp->xmpp = vvp->ympp = xympp;
+    // Since xmpp & ympp are the same it doesn't matter which one is used here
+    vvp->xmfactor = vvp->ymfactor = MERCATOR_FACTOR(vvp->xmpp);
+  }
 
   if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
     viewport_utm_zone_check(vvp);
@@ -716,6 +701,9 @@ void vik_viewport_zoom_in ( VikViewport *vvp )
     vvp->xmpp /= 2;
     vvp->ympp /= 2;
 
+    vvp->xmfactor = MERCATOR_FACTOR(vvp->xmpp);
+    vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
+
     viewport_utm_zone_check(vvp);
   }
 }
@@ -728,6 +716,9 @@ void vik_viewport_zoom_out ( VikViewport *vvp )
     vvp->xmpp *= 2;
     vvp->ympp *= 2;
 
+    vvp->xmfactor = MERCATOR_FACTOR(vvp->xmpp);
+    vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
+
     viewport_utm_zone_check(vvp);
   }
 }
@@ -753,6 +744,7 @@ void vik_viewport_set_xmpp ( VikViewport *vvp, gdouble xmpp )
 {
   if ( xmpp >= VIK_VIEWPORT_MIN_ZOOM && xmpp <= VIK_VIEWPORT_MAX_ZOOM ) {
     vvp->xmpp = xmpp;
+    vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
     if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
       viewport_utm_zone_check(vvp);
   }
@@ -762,6 +754,7 @@ void vik_viewport_set_ympp ( VikViewport *vvp, gdouble ympp )
 {
   if ( ympp >= VIK_VIEWPORT_MIN_ZOOM && ympp <= VIK_VIEWPORT_MAX_ZOOM ) {
     vvp->ympp = ympp;
+    vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
     if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
       viewport_utm_zone_check(vvp);
   }
@@ -898,34 +891,32 @@ void vik_viewport_screen_to_coord ( VikViewport *vvp, int x, int y, VikCoord *co
 
     utm->zone = vvp->center.utm_zone;
     utm->letter = vvp->center.utm_letter;
-    utm->easting = ( ( x - ( vvp->width / 2) ) * vvp->xmpp ) + vvp->center.east_west;
+    utm->easting = ( ( x - ( vvp->width_2) ) * vvp->xmpp ) + vvp->center.east_west;
     zone_delta = floor( (utm->easting - EASTING_OFFSET ) / vvp->utm_zone_width + 0.5 );
     utm->zone += zone_delta;
     utm->easting -= zone_delta * vvp->utm_zone_width;
-    utm->northing = ( ( ( vvp->height / 2) - y ) * vvp->ympp ) + vvp->center.north_south;
+    utm->northing = ( ( ( vvp->height_2) - y ) * vvp->ympp ) + vvp->center.north_south;
   } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
     coord->mode = VIK_COORD_LATLON;
     if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) {
-      coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width/2));
-      coord->north_south = vvp->center.north_south + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y));
+      coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width_2));
+      coord->north_south = vvp->center.north_south + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height_2 - y));
     } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA )
-      calcxy_rev(&(coord->east_west), &(coord->north_south), x, y, vvp->center.east_west, vvp->center.north_south, vvp->xmpp * ALTI_TO_MPP, vvp->ympp * ALTI_TO_MPP, vvp->width/2, vvp->height/2);
+      calcxy_rev(&(coord->east_west), &(coord->north_south), x, y, vvp->center.east_west, vvp->center.north_south, vvp->xmpp * ALTI_TO_MPP, vvp->ympp * ALTI_TO_MPP, vvp->width_2, vvp->height_2);
     else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
-      /* FIXMERCATOR */
-      coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width/2));
-      coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) );
-
-#if 0
--->    THIS IS JUNK HERE.
-      *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0;
-
-      (*y - vvp->height/2) / 256 / 65536 * 180 * vvp->ympp = (MERCLAT(center->lat) - MERCLAT(ll->lat);
-      DML((180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) + ML(cl)) = ll
-#endif
+      /* This isn't called with a high frequently so less need to optimize */
+      coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width_2));
+      coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height_2 - y)) );
     }
   }
 }
 
+/*
+ * Since this function is used for every drawn trackpoint - it can get called alot
+ * Thus x & y position factors are calculated once on zoom changes,
+ *  avoiding the need to do it here all the time.
+ * For good measure the half width and height values are also pre calculated too.
+ */
 void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int *x, int *y )
 {
   static VikCoord tmp;
@@ -947,24 +938,22 @@ void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int
       return;
     }
 
-    *x = ( (utm->easting - center->easting) / vvp->xmpp ) + (vvp->width / 2) -
+    *x = ( (utm->easting - center->easting) / vvp->xmpp ) + (vvp->width_2) -
          (center->zone - utm->zone ) * vvp->utm_zone_width / vvp->xmpp;
-    *y = (vvp->height / 2) - ( (utm->northing - center->northing) / vvp->ympp );
+    *y = (vvp->height_2) - ( (utm->northing - center->northing) / vvp->ympp );
   } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
     struct LatLon *center = (struct LatLon *) &(vvp->center);
     struct LatLon *ll = (struct LatLon *) coord;
     double xx,yy;
     if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) {
-      /* FIXMERCATOR: Optimize */
-      *x = vvp->width/2 + (65536.0 / 180 / vvp->xmpp * (ll->lon - center->lon))*256.0;
-      *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (center->lat - ll->lat))*256.0;
+      *x = vvp->width_2 + ( MERCATOR_FACTOR(vvp->xmpp) * (ll->lon - center->lon) );
+      *y = vvp->height_2 + ( MERCATOR_FACTOR(vvp->ympp) * (center->lat - ll->lat) );
     } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) {
-      calcxy ( &xx, &yy, center->lon, center->lat, ll->lon, ll->lat, vvp->xmpp * ALTI_TO_MPP, vvp->ympp * ALTI_TO_MPP, vvp->width / 2, vvp->height / 2 );
+      calcxy ( &xx, &yy, center->lon, center->lat, ll->lon, ll->lat, vvp->xmpp * ALTI_TO_MPP, vvp->ympp * ALTI_TO_MPP, vvp->width_2, vvp->height_2 );
       *x = xx; *y = yy;
     } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
-      /* FIXMERCATOR: Optimize */
-      *x = vvp->width/2 + (65536.0 / 180 / vvp->xmpp * (ll->lon - center->lon))*256.0;
-      *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0;
+      *x = vvp->width_2 + ( MERCATOR_FACTOR(vvp->xmpp) * (ll->lon - center->lon) );
+      *y = vvp->height_2 + ( MERCATOR_FACTOR(vvp->ympp) * ( MERCLAT(center->lat) - MERCLAT(ll->lat) ) );
     }
   }
 }
@@ -1055,9 +1044,8 @@ void vik_viewport_draw_pixbuf ( VikViewport *vvp, GdkPixbuf *pixbuf, gint src_x,
                               gint dest_x, gint dest_y, gint w, gint h )
 {
   gdk_draw_pixbuf ( vvp->scr_buffer,
-// GTK_WIDGET(vvp)->style->black_gc,
-NULL,
- pixbuf,
+                    NULL,
+                    pixbuf,
                     src_x, src_y, dest_x, dest_y, w, h,
                     GDK_RGB_DITHER_NONE, 0, 0 );
 }
@@ -1101,14 +1089,14 @@ static gboolean calcxy_rev(double *lg, double *lt, gint x, gint y, double zero_l
   lon =
     zero_long -
     px / (Ra *
-         cos (lat * DEG2RAD));
+         cos (DEG2RAD(lat)));
 
-  dif = lat * (1 - (cos ((fabs (lon - zero_long)) * DEG2RAD)));
+  dif = lat * (1 - (cos (DEG2RAD(fabs (lon - zero_long)))));
   lat = lat - dif / 1.5;
   lon =
     zero_long -
     px / (Ra *
-              cos (lat * DEG2RAD));
+              cos (DEG2RAD(lat)));
 
   *lt = lat;
   *lg = lon;
@@ -1128,9 +1116,9 @@ static gboolean calcxy(double *x, double *y, double lg, double lt, double zero_l
 //    lt *= rad2deg;
     Ra = Radius[90+(gint)lt];
     *x = Ra *
-         cos (lt*DEG2RAD) * (lg - zero_long);
+         cos (DEG2RAD(lt)) * (lg - zero_long);
     *y = Ra * (lt - zero_lat);
-    dif = Ra * RAD2DEG * (1 - (cos ((DEG2RAD * (lg - zero_long)))));
+    dif = Ra * RAD2DEG(1 - (cos ((DEG2RAD(lg - zero_long)))));
     *y = *y + dif / 1.85;
     *x = *x / pixelfact_x;
     *y = *y / pixelfact_y;
@@ -1148,7 +1136,7 @@ static void viewport_init_ra()
   {
     gint i;
     for ( i = -90; i <= 90; i++)
-      Radius[i+90] = calcR ( (double)i ) * DEG2RAD;
+      Radius[i+90] = calcR ( DEG2RAD((double)i) );
     done_before = TRUE;
   }
 }
@@ -1172,7 +1160,7 @@ double calcR (double lat)
      * = 0.081082 Eccentricity
      */
 
-    lat = lat * DEG2RAD;
+    lat = DEG2RAD(lat);
     sc = sin (lat);
     x = a * (1.0 - e2);
     z = 1.0 - e2 * sc * sc;
@@ -1340,3 +1328,46 @@ void vik_viewport_add_logo ( VikViewport *vp, const GdkPixbuf *logo )
     }
   }
 }
+
+/**
+ * vik_viewport_compute_bearing:
+ * @vp: self object
+ * @x1: screen coord
+ * @y1: screen coord
+ * @x2: screen coord
+ * @y2: screen coord
+ * @angle: bearing in Radian (output)
+ * @baseangle: UTM base angle in Radian (output)
+ * 
+ * Compute bearing.
+ */
+void vik_viewport_compute_bearing ( VikViewport *vp, gint x1, gint y1, gint x2, gint y2, gdouble *angle, gdouble *baseangle )
+{
+  gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
+  gdouble dx = (x2-x1)/len*10;
+  gdouble dy = (y2-y1)/len*10;
+
+  *angle = atan2(dy, dx) + M_PI_2;
+
+  if ( vik_viewport_get_drawmode ( vp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
+    VikCoord test;
+    struct LatLon ll;
+    struct UTM u;
+    gint tx, ty;
+
+    vik_viewport_screen_to_coord ( vp, x1, y1, &test );
+    vik_coord_to_latlon ( &test, &ll );
+    ll.lat += vik_viewport_get_ympp ( vp ) * vik_viewport_get_height ( vp ) / 11000.0; // about 11km per degree latitude
+    a_coords_latlon_to_utm ( &ll, &u );
+    vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
+    vik_viewport_coord_to_screen ( vp, &test, &tx, &ty );
+
+    *baseangle = M_PI - atan2(tx-x1, ty-y1);
+    *angle -= *baseangle;
+  }
+
+  if (*angle < 0)
+    *angle += 2*M_PI;
+  if (*angle > 2*M_PI)
+    *angle -= 2*M_PI;
+}