Merge branch 'upstream' into maemo
[navit-package] / navit / transform.c
index 44d499e..4708c02 100644 (file)
 #include "point.h"
 
 #define POST_SHIFT 8
-/* #define ENABLE_ROLL */
 
 struct transformation {
        int yaw;                /* Rotation angle */
        int pitch;
        int ddd;
-       int m00,m01,m10,m11;    /* 2d transformation matrix */
-       int xyscale;
-       int m20,m21;            /* additional 3d parameters */
+       int m00,m01,m02;        /* 3d transformation matrix */
+       int m10,m11,m12;        
+       int m20,m21,m22;        
+       int xscale,yscale,wscale;
+       int xscale3d,yscale3d,wscale3d;
 #ifdef ENABLE_ROLL
        int roll;
-       int m02,m12,m22; 
        int hog;
-       double im02,im12,im20,im21,im22;
 #endif
-       double im00,im01,im10,im11;     /* inverse 2d transformation matrix */
+       navit_float im00,im01,im02;     /* inverse 3d transformation matrix */
+       navit_float im10,im11,im12;
+       navit_float im20,im21,im22;
        struct map_selection *map_sel;
        struct map_selection *screen_sel;
        struct point screen_center;
        int screen_dist;
        int offx,offy,offz;
+       int znear,zfar;
        struct coord map_center;        /* Center of source rectangle */
        enum projection pro;
-       double scale;           /* Scale factor */
+       navit_float scale;              /* Scale factor */
        int scale_shift;
        int order;
+       int order_base;
 };
 
+#ifdef ENABLE_ROLL
+#define HOG(t) ((t).hog)
+#else
+#define HOG(t) 0
+#endif
+
+static void
+transform_set_screen_dist(struct transformation *t, int dist)
+{
+       t->screen_dist=dist;
+       t->xscale3d=dist;
+       t->yscale3d=dist;
+       t->wscale3d=dist << POST_SHIFT;
+}
+
 static void
 transform_setup_matrix(struct transformation *t)
 {
-       double det;
-       double fac;
-       double yawc=cos(-M_PI*t->yaw/180);
-       double yaws=sin(-M_PI*t->yaw/180);
-       double pitchc=cos(-M_PI*t->pitch/180);
-       double pitchs=sin(-M_PI*t->pitch/180);
+       navit_float det;
+       navit_float fac;
+       navit_float yawc=navit_cos(-M_PI*t->yaw/180);
+       navit_float yaws=navit_sin(-M_PI*t->yaw/180);
+       navit_float pitchc=navit_cos(-M_PI*t->pitch/180);
+       navit_float pitchs=navit_sin(-M_PI*t->pitch/180);
 #ifdef ENABLE_ROLL     
-       double rollc=cos(M_PI*t->roll/180);
-       double rolls=sin(M_PI*t->roll/180);
+       navit_float rollc=navit_cos(M_PI*t->roll/180);
+       navit_float rolls=navit_sin(M_PI*t->roll/180);
+#else
+       navit_float rollc=1;
+       navit_float rolls=0;
 #endif
 
        int scale=t->scale;
        int order_dir=-1;
 
        dbg(1,"yaw=%d pitch=%d center=0x%x,0x%x\n", t->yaw, t->pitch, t->map_center.x, t->map_center.y);
+       t->znear=1 << POST_SHIFT;
+       t->zfar=300*t->znear;
        t->scale_shift=0;
-       t->order=14;
+       t->order=t->order_base;
        if (t->scale >= 1) {
                scale=t->scale;
        } else {
@@ -96,7 +119,6 @@ transform_setup_matrix(struct transformation *t)
        fac=(1 << POST_SHIFT) * (1 << t->scale_shift) / t->scale;
        dbg(1,"scale_shift=%d order=%d scale=%f fac=%f\n", t->scale_shift, t->order,t->scale,fac);
        
-#ifdef ENABLE_ROLL
         t->m00=rollc*yawc*fac;
         t->m01=rollc*yaws*fac;
        t->m02=-rolls*fac;
@@ -106,31 +128,29 @@ transform_setup_matrix(struct transformation *t)
        t->m20=(pitchc*rolls*yawc+pitchs*yaws)*fac;
        t->m21=(pitchc*rolls*yaws-pitchs*yawc)*fac;
        t->m22=pitchc*rollc*fac;
-#else
-        t->m00=yawc*fac;
-        t->m01=yaws*fac;
-       t->m10=(-pitchc*yaws)*(-fac);
-       t->m11=pitchc*yawc*(-fac);
-       t->m20=pitchs*yaws*fac;
-       t->m21=(-pitchs*yawc)*fac;
-#endif
-       t->offz=0;
-       t->xyscale=1;
-       t->ddd=0;
+
        t->offx=t->screen_center.x;
        t->offy=t->screen_center.y;
        if (t->pitch) {
                t->ddd=1;
                t->offz=t->screen_dist;
-               t->xyscale=t->offz;
+               dbg(1,"near %d far %d\n",t->znear,t->zfar);
+               t->xscale=t->xscale3d;
+               t->yscale=t->yscale3d;
+               t->wscale=t->wscale3d;
+       } else {
+               t->ddd=0;
+               t->offz=0;
+               t->xscale=1;
+               t->yscale=1;
+               t->wscale=1;
        }
-#ifdef ENABLE_ROLL
-       det=(double)t->m00*(double)t->m11*(double)t->m22+
-            (double)t->m01*(double)t->m12*(double)t->m20+
-            (double)t->m02*(double)t->m10*(double)t->m21-
-            (double)t->m02*(double)t->m11*(double)t->m20-
-            (double)t->m01*(double)t->m10*(double)t->m22-
-            (double)t->m00*(double)t->m12*(double)t->m21;
+       det=(navit_float)t->m00*(navit_float)t->m11*(navit_float)t->m22+
+            (navit_float)t->m01*(navit_float)t->m12*(navit_float)t->m20+
+            (navit_float)t->m02*(navit_float)t->m10*(navit_float)t->m21-
+            (navit_float)t->m02*(navit_float)t->m11*(navit_float)t->m20-
+            (navit_float)t->m01*(navit_float)t->m10*(navit_float)t->m22-
+            (navit_float)t->m00*(navit_float)t->m12*(navit_float)t->m21;
 
        t->im00=(t->m11*t->m22-t->m12*t->m21)/det;
        t->im01=(t->m02*t->m21-t->m01*t->m22)/det;
@@ -141,13 +161,6 @@ transform_setup_matrix(struct transformation *t)
        t->im20=(t->m10*t->m21-t->m11*t->m20)/det;
        t->im21=(t->m01*t->m20-t->m00*t->m21)/det;
        t->im22=(t->m00*t->m11-t->m01*t->m10)/det;
-#else
-       det=((double)t->m00*(double)t->m11-(double)t->m01*(double)t->m10);
-       t->im00=t->m11/det;
-       t->im01=-t->m01/det;
-       t->im10=-t->m10/det;
-       t->im11=t->m00/det;
-#endif
 }
 
 struct transformation *
@@ -156,7 +169,8 @@ transform_new(void)
        struct transformation *this_;
 
        this_=g_new0(struct transformation, 1);
-       this_->screen_dist=100;
+       transform_set_screen_dist(this_, 100);
+       this_->order_base=14;
 #if 0
        this_->pitch=20;
 #endif
@@ -168,6 +182,66 @@ transform_new(void)
        return this_;
 }
 
+int
+transform_get_hog(struct transformation *this_)
+{
+       return HOG(*this_);
+}
+
+void
+transform_set_hog(struct transformation *this_, int hog)
+{
+#ifdef ENABLE_ROLL
+       this_->hog=hog;
+#else
+       dbg(0,"not supported\n");
+#endif
+
+}
+
+int
+transform_get_attr(struct transformation *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
+{
+       switch (type) {
+#ifdef ENABLE_ROLL
+       case attr_hog:
+               attr->u.num=this_->hog;
+               break;
+#endif
+       default:
+               return 0;
+       }
+       attr->type=type;
+       return 1;
+}
+
+int
+transform_set_attr(struct transformation *this_, struct attr *attr)
+{
+       switch (attr->type) {
+#ifdef ENABLE_ROLL
+       case attr_hog:
+               this_->hog=attr->u.num;
+               return 1;
+#endif
+       default:
+               return 0;
+       }
+}
+
+int
+transformation_get_order_base(struct transformation *this_)
+{
+       return this_->order_base;
+}
+
+void
+transform_set_order_base(struct transformation *this_, int order_base)
+{
+       this_->order_base=order_base;
+}
+
+
 struct transformation *
 transform_dup(struct transformation *t)
 {
@@ -176,21 +250,33 @@ transform_dup(struct transformation *t)
        return ret;
 }
 
-static const double gar2geo_units = 360.0/(1<<24);
-static const double geo2gar_units = 1/(360.0/(1<<24));
+static const navit_float gar2geo_units = 360.0/(1<<24);
+static const navit_float geo2gar_units = 1/(360.0/(1<<24));
 
 void
 transform_to_geo(enum projection pro, struct coord *c, struct coord_geo *g)
 {
+       int x,y,northern,zone;
        switch (pro) {
        case projection_mg:
                g->lng=c->x/6371000.0/M_PI*180;
-               g->lat=atan(exp(c->y/6371000.0))/M_PI*360-90;
+               g->lat=navit_atan(exp(c->y/6371000.0))/M_PI*360-90;
                break;
        case projection_garmin:
                g->lng=c->x*gar2geo_units;
                g->lat=c->y*gar2geo_units;
                break;
+       case projection_utm:
+               x=c->x;
+               y=c->y;
+               northern=y >= 0;
+               if (!northern) {
+                       y+=10000000;
+               }
+               zone=(x/1000000);
+               x=x%1000000;
+               transform_utm_to_geo(x, y, zone, northern, g);
+               break;  
        default:
                break;
        }
@@ -202,7 +288,7 @@ transform_from_geo(enum projection pro, struct coord_geo *g, struct coord *c)
        switch (pro) {
        case projection_mg:
                c->x=g->lng*6371000.0*M_PI/180;
-               c->y=log(tan(M_PI_4+g->lat*M_PI/360))*6371000.0;
+               c->y=log(navit_tan(M_PI_4+g->lat*M_PI/360))*6371000.0;
                break;
        case projection_garmin:
                c->x=g->lng*geo2gar_units;
@@ -222,27 +308,27 @@ transform_from_to(struct coord *cfrom, enum projection from, struct coord *cto,
 }
 
 void
-transform_geo_to_cart(struct coord_geo *geo, double a, double b, struct coord_geo_cart *cart)
+transform_geo_to_cart(struct coord_geo *geo, navit_float a, navit_float b, struct coord_geo_cart *cart)
 {
-       double n,ee=1-b*b/(a*a);
-       n = a/sqrt(1-ee*sin(geo->lat)*sin(geo->lat));
-        cart->x=n*cos(geo->lat)*cos(geo->lng);
-        cart->y=n*cos(geo->lat)*sin(geo->lng);
-        cart->z=n*(1-ee)*sin(geo->lat);
+       navit_float n,ee=1-b*b/(a*a);
+       n = a/sqrtf(1-ee*navit_sin(geo->lat)*navit_sin(geo->lat));
+        cart->x=n*navit_cos(geo->lat)*navit_cos(geo->lng);
+        cart->y=n*navit_cos(geo->lat)*navit_sin(geo->lng);
+        cart->z=n*(1-ee)*navit_sin(geo->lat);
 }
 
 void
-transform_cart_to_geo(struct coord_geo_cart *cart, double a, double b, struct coord_geo *geo)
+transform_cart_to_geo(struct coord_geo_cart *cart, navit_float a, navit_float b, struct coord_geo *geo)
 {
-       double lat,lati,n,ee=1-b*b/(a*a), lng = atan(cart->y/cart->x);
+       navit_float lat,lati,n,ee=1-b*b/(a*a), lng = navit_tan(cart->y/cart->x);
 
-        lat = atan(cart->z / sqrt((cart->x * cart->x) + (cart->y * cart->y)));
+        lat = navit_tan(cart->z / navit_sqrt((cart->x * cart->x) + (cart->y * cart->y)));
         do
         {
                 lati = lat;
 
-                n = a / sqrt(1-ee*sin(lat)*sin(lat));
-                lat = atan((cart->z + ee * n * sin(lat)) / sqrt(cart->x * cart->x + cart->y * cart->y));
+                n = a / navit_sqrt(1-ee*navit_sin(lat)*navit_sin(lat));
+                lat = navit_atan((cart->z + ee * n * navit_sin(lat)) / navit_sqrt(cart->x * cart->x + cart->y * cart->y));
         }
         while (fabs(lat - lati) >= 0.000000000000001);
 
@@ -252,21 +338,78 @@ transform_cart_to_geo(struct coord_geo_cart *cart, double a, double b, struct co
 
 
 void
+transform_utm_to_geo(const double UTMEasting, const double UTMNorthing, int ZoneNumber, int NorthernHemisphere, struct coord_geo *geo)
+{
+//converts UTM coords to lat/long.  Equations from USGS Bulletin 1532 
+//East Longitudes are positive, West longitudes are negative. 
+//North latitudes are positive, South latitudes are negative
+//Lat and Long are in decimal degrees. 
+       //Written by Chuck Gantz- chuck.gantz@globalstar.com
+
+       double Lat, Long;
+       double k0 = 0.99960000000000004;
+       double a = 6378137;
+       double eccSquared = 0.0066943799999999998;
+       double eccPrimeSquared;
+       double e1 = (1-sqrt(1-eccSquared))/(1+sqrt(1-eccSquared));
+       double N1, T1, C1, R1, D, M;
+       double LongOrigin;
+       double mu, phi1, phi1Rad;
+       double x, y;
+       double rad2deg = 180/M_PI;
+
+       x = UTMEasting - 500000.0; //remove 500,000 meter offset for longitude
+       y = UTMNorthing;
+
+       if (!NorthernHemisphere) {
+               y -= 10000000.0;//remove 10,000,000 meter offset used for southern hemisphere
+       }
+
+       LongOrigin = (ZoneNumber - 1)*6 - 180 + 3;  //+3 puts origin in middle of zone
+
+       eccPrimeSquared = (eccSquared)/(1-eccSquared);
+
+       M = y / k0;
+       mu = M/(a*(1-eccSquared/4-3*eccSquared*eccSquared/64-5*eccSquared*eccSquared*eccSquared/256));
+       phi1Rad = mu    + (3*e1/2-27*e1*e1*e1/32)*sin(2*mu) 
+                               + (21*e1*e1/16-55*e1*e1*e1*e1/32)*sin(4*mu)
+                               +(151*e1*e1*e1/96)*sin(6*mu);
+       phi1 = phi1Rad*rad2deg;
+
+       N1 = a/sqrt(1-eccSquared*sin(phi1Rad)*sin(phi1Rad));
+       T1 = tan(phi1Rad)*tan(phi1Rad);
+       C1 = eccPrimeSquared*cos(phi1Rad)*cos(phi1Rad);
+       R1 = a*(1-eccSquared)/pow(1-eccSquared*sin(phi1Rad)*sin(phi1Rad), 1.5);
+       D = x/(N1*k0);
+
+       Lat = phi1Rad - (N1*tan(phi1Rad)/R1)*(D*D/2-(5+3*T1+10*C1-4*C1*C1-9*eccPrimeSquared)*D*D*D*D/24
+                                       +(61+90*T1+298*C1+45*T1*T1-252*eccPrimeSquared-3*C1*C1)*D*D*D*D*D*D/720);
+       Lat = Lat * rad2deg;
+
+       Long = (D-(1+2*T1+C1)*D*D*D/6+(5-2*C1+28*T1-3*C1*C1+8*eccPrimeSquared+24*T1*T1)
+                                       *D*D*D*D*D/120)/cos(phi1Rad);
+       Long = LongOrigin + Long * rad2deg;
+
+       geo->lat=Lat;
+       geo->lng=Long;
+}
+
+void
 transform_datum(struct coord_geo *from, enum map_datum from_datum, struct coord_geo *to, enum map_datum to_datum)
 {
 }
 
 int
-transform(struct transformation *t, enum projection pro, struct coord *c, struct point *p, int count, int unique, int width, int *width_return)
+transform(struct transformation *t, enum projection pro, struct coord *c, struct point *p, int count, int mindist, int width, int *width_return)
 {
        struct coord c1;
        int xcn, ycn; 
        struct coord_geo g;
        int xc, yc, zc=0, xco=0, yco=0, zco=0;
        int xm,ym,zct;
-       int zlimit=1000;
+       int zlimit=t->znear;
        int visible, visibleo=-1;
-       int i,j = 0;
+       int i,j = 0,k=0;
        dbg(1,"count=%d\n", count);
        for (i=0; i < count; i++) {
                if (pro == t->pro) {
@@ -278,6 +421,12 @@ transform(struct transformation *t, enum projection pro, struct coord *c, struct
                        xc=c1.x;
                        yc=c1.y;
                }
+               if (i != 0 && i != count-1 && mindist) {
+                       if (xc > c[k].x-mindist && xc < c[k].x+mindist && yc > c[k].y-mindist && yc < c[k].y+mindist &&
+                               (c[i+1].x != c[0].x || c[i+1].y != c[0].y))
+                               continue;
+                       k=i;
+               }
                xm=xc;
                ym=yc;
 //             dbg(2,"0x%x, 0x%x - 0x%x,0x%x contains 0x%x,0x%x\n", t->r.lu.x, t->r.lu.y, t->r.rl.x, t->r.rl.y, c->x, c->y);
@@ -286,20 +435,14 @@ transform(struct transformation *t, enum projection pro, struct coord *c, struct
                yc-=t->map_center.y;
                xc >>= t->scale_shift;
                yc >>= t->scale_shift;
-#ifdef ENABLE_ROLL             
-               xcn=xc*t->m00+yc*t->m01+t->hog*t->m02;
-               ycn=xc*t->m10+yc*t->m11+t->hog*t->m12;
-#else
-               xcn=xc*t->m00+yc*t->m01;
-               ycn=xc*t->m10+yc*t->m11;
-#endif
+               xm=xc;
+               ym=yc;
+
+               xcn=xc*t->m00+yc*t->m01+HOG(*t)*t->m02;
+               ycn=xc*t->m10+yc*t->m11+HOG(*t)*t->m12;
 
                if (t->ddd) {
-#ifdef ENABLE_ROLL             
-                       zc=(xc*t->m20+yc*t->m21+t->hog*t->m22);
-#else
-                       zc=(xc*t->m20+yc*t->m21);
-#endif
+                       zc=(xc*t->m20+yc*t->m21+HOG(*t)*t->m22);
                        zct=zc;
                        zc+=t->offz << POST_SHIFT;
                        dbg(1,"zc=%d\n", zc);
@@ -336,13 +479,15 @@ transform(struct transformation *t, enum projection pro, struct coord *c, struct
                        dbg(0,"%d/%d=%d %d/%d=%d\n",xcn,xc,xcn/xc,ycn,yc,ycn/yc);
 #endif
 #if 1
-                       xc=(long long)xcn*t->xyscale/zc;
-                       yc=(long long)ycn*t->xyscale/zc;
+                       xc=(long long)xcn*t->xscale/zc;
+                       yc=(long long)ycn*t->yscale/zc;
 #else
                        xc=xcn/(1000+zc);
                        yc=ycn/(1000+zc);
 #endif
+#if 0
                        dbg(1,"%d,%d %d\n",xc,yc,zc);
+#endif
                } else {
                        xc=xcn;
                        yc=ycn;
@@ -351,65 +496,91 @@ transform(struct transformation *t, enum projection pro, struct coord *c, struct
                }
                xc+=t->offx;
                yc+=t->offy;
-               dbg(1,"xc=%d yc=%d\n", xc, yc);
-               if (j == 0 || !unique || p[j-1].x != xc || p[j-1].y != yc) {
-                       p[j].x=xc;
-                       p[j].y=yc;
-                       if (width_return) {
-                               if (t->ddd) 
-                                       width_return[j]=width*(t->offz << POST_SHIFT)/zc;
-                               else 
-                                       width_return[j]=width;
-                       }
-                       j++;
+               p[j].x=xc;
+               p[j].y=yc;
+               if (width_return) {
+                       if (t->ddd) 
+                               width_return[j]=width*t->wscale/zc;
+                       else 
+                               width_return[j]=width;
                }
+               j++;
        }
        return j;
 }
 
-void
-transform_reverse(struct transformation *t, struct point *p, struct coord *c)
+static void
+transform_apply_inverse_matrix(struct transformation *t, struct coord_geo_cart *in, struct coord_geo_cart *out)
+{
+       out->x=in->x*t->im00+in->y*t->im01+in->z*t->im02;
+       out->y=in->x*t->im10+in->y*t->im11+in->z*t->im12;
+       out->z=in->x*t->im20+in->y*t->im21+in->z*t->im22;
+}
+
+static int
+transform_zplane_intersection(struct coord_geo_cart *p1, struct coord_geo_cart *p2, navit_float z, struct coord_geo_cart *result)
+{
+       navit_float dividend=z-p1->z;
+       navit_float divisor=p2->z-p1->z;
+       navit_float q;
+       if (!divisor) {
+               if (dividend) 
+                       return 0;       /* no intersection */
+               else
+                       return 3;       /* identical planes */
+       }
+       q=dividend/divisor;
+       result->x=p1->x+q*(p2->x-p1->x);
+       result->y=p1->y+q*(p2->y-p1->y);
+       result->z=z;
+       if (q >= 0 && q <= 1)
+               return 1;       /* intersection within [p1,p2] */
+       return 2; /* intersection without [p1,p2] */
+}
+
+static void
+transform_screen_to_3d(struct transformation *t, struct point *p, navit_float z, struct coord_geo_cart *cg)
 {
-        double zc,xc,yc,xcn,ycn,q;
+       double xc,yc;
        double offz=t->offz << POST_SHIFT;
        xc=p->x - t->offx;
        yc=p->y - t->offy;
-       if (t->ddd) {
-               xc/=t->xyscale;
-               yc/=t->xyscale;
-               double f00=xc*t->im00*t->m20;
-               double f01=yc*t->im01*t->m20;
-               double f10=xc*t->im10*t->m21;
-               double f11=yc*t->im11*t->m21;
-#ifdef ENABLE_ROLL     
-               q=(1-f00-f01-t->im02*t->m20-f10-f11-t->im12*t->m21);
-               if (q < 0) 
-                       q=0.15;
-               zc=(offz*((f00+f01+f10+f11))+t->hog*t->m22)/q;
-#else
-               q=(1-f00-f01-f10-f11);
-                if (q < 0) 
-                       q=0.15;
-               zc=offz*(f00+f01+f10+f11)/q;
-#endif
-               xcn=xc*(zc+offz);
-               ycn=yc*(zc+offz);
-#ifdef ENABLE_ROLL     
-               xc=xcn*t->im00+ycn*t->im01+zc*t->im02;
-               yc=xcn*t->im10+ycn*t->im11+zc*t->im12;
-#else
-               xc=xcn*t->im00+ycn*t->im01;
-               yc=xcn*t->im10+ycn*t->im11;
-#endif
+       cg->x=xc*z/t->xscale;
+       cg->y=yc*z/t->yscale;
+       cg->z=z-offz;
+}
 
+static int
+transform_reverse_near_far(struct transformation *t, struct point *p, struct coord *c, int near, int far)
+{
+        double xc,yc;
+       dbg(1,"%d,%d\n",p->x,p->y);
+       if (t->ddd) {
+               struct coord_geo_cart nearc,farc,nears,fars,intersection;
+               transform_screen_to_3d(t, p, near, &nearc);     
+               transform_screen_to_3d(t, p, far, &farc);
+               transform_apply_inverse_matrix(t, &nearc, &nears);
+               transform_apply_inverse_matrix(t, &farc, &fars);
+               if (transform_zplane_intersection(&nears, &fars, HOG(*t), &intersection) != 1)
+                       return 0;
+               xc=intersection.x;
+               yc=intersection.y;
        } else {
-               xcn=xc;
-               ycn=yc;
+               double xcn,ycn;
+               xcn=p->x - t->offx;
+               ycn=p->y - t->offy;
                xc=(xcn*t->im00+ycn*t->im01)*(1 << POST_SHIFT);
                yc=(xcn*t->im10+ycn*t->im11)*(1 << POST_SHIFT);
        }
        c->x=xc*(1 << t->scale_shift)+t->map_center.x;
        c->y=yc*(1 << t->scale_shift)+t->map_center.y;
+       return 1;
+}
+
+int
+transform_reverse(struct transformation *t, struct point *p, struct coord *c)
+{
+       return transform_reverse_near_far(t, p, c, t->znear, t->zfar);
 }
 
 enum projection
@@ -471,6 +642,10 @@ transform_get_selection(struct transformation *this_, enum projection pro, int o
                }
                dbg(1,"transform rect for %d is %d,%d - %d,%d\n", pro, curro->u.c_rect.lu.x, curro->u.c_rect.lu.y, curro->u.c_rect.rl.x, curro->u.c_rect.rl.y);
                curro->order+=order;
+               curro->u.c_rect.lu.x-=500;
+               curro->u.c_rect.lu.y+=500;
+               curro->u.c_rect.rl.x+=500;
+               curro->u.c_rect.rl.y-=500;
                curro->range=item_range_all;
                curri=curri->next;
                curro=curro->next;
@@ -522,25 +697,31 @@ transform_get_pitch(struct transformation *this_)
        return this_->pitch;
 }
 
-#ifdef ENABLE_ROLL
 void
 transform_set_roll(struct transformation *this_,int roll)
 {
+#ifdef ENABLE_ROLL
        this_->roll=roll;
        transform_setup_matrix(this_);
+#else
+       dbg(0,"not supported\n");
+#endif
 }
 
 int
 transform_get_roll(struct transformation *this_)
 {
+#ifdef ENABLE_ROLL
        return this_->roll;
-}
+#else
+       return 0;
 #endif
+}
 
 void
 transform_set_distance(struct transformation *this_,int distance)
 {
-       this_->screen_dist=distance;
+       transform_set_screen_dist(this_, distance);
        transform_setup_matrix(this_);
 }
 
@@ -551,6 +732,14 @@ transform_get_distance(struct transformation *this_)
 }
 
 void
+transform_set_scales(struct transformation *this_, int xscale, int yscale, int wscale)
+{
+       this_->xscale3d=xscale;
+       this_->yscale3d=yscale;
+       this_->wscale3d=wscale;
+}
+
+void
 transform_set_screen_selection(struct transformation *t, struct map_selection *sel)
 {
        map_selection_destroy(t->screen_sel);
@@ -634,22 +823,64 @@ transform_setup_source_rect(struct transformation *t)
                msm=g_new0(struct map_selection, 1);
                *msm=*ms;
                pr=&ms->u.p_rect;
-               screen_pnt[0].x=pr->lu.x;
+               screen_pnt[0].x=pr->lu.x;       /* left upper */
                screen_pnt[0].y=pr->lu.y;
-               screen_pnt[1].x=pr->rl.x;
+               screen_pnt[1].x=pr->rl.x;       /* right upper */
                screen_pnt[1].y=pr->lu.y;
-               screen_pnt[2].x=pr->lu.x;
+               screen_pnt[2].x=pr->rl.x;       /* right lower */
                screen_pnt[2].y=pr->rl.y;
-               screen_pnt[3].x=pr->rl.x;
+               screen_pnt[3].x=pr->lu.x;       /* left lower */
                screen_pnt[3].y=pr->rl.y;
-               for (i = 0 ; i < 4 ; i++) {
-                       transform_reverse(t, &screen_pnt[i], &screen[i]);
-                       dbg(1,"map(%d) %d,%d=0x%x,0x%x\n", i,screen_pnt[i].x, screen_pnt[i].y, screen[i].x, screen[i].y);
+               if (t->ddd) {
+                       struct coord_geo_cart tmp,cg[8];
+                       struct coord c;
+                       int valid=0;
+                       char edgenodes[]={
+                               0,1,
+                               1,2,
+                               2,3,
+                               3,0,
+                               4,5,
+                               5,6,
+                               6,7,
+                               7,4,
+                               0,4,
+                               1,5,
+                               2,6,
+                               3,7};   
+                       for (i = 0 ; i < 8 ; i++) {
+                               transform_screen_to_3d(t, &screen_pnt[i%4], (i >= 4 ? t->zfar:t->znear), &tmp);
+                               transform_apply_inverse_matrix(t, &tmp, &cg[i]);
+                       }
+                       msm->u.c_rect.lu.x=0;
+                       msm->u.c_rect.lu.y=0;
+                       msm->u.c_rect.rl.x=0;
+                       msm->u.c_rect.rl.y=0;
+                       for (i = 0 ; i < 12 ; i++) {
+                               if (transform_zplane_intersection(&cg[edgenodes[i*2]], &cg[edgenodes[i*2+1]], HOG(*t), &tmp) == 1) {
+                                       c.x=tmp.x*(1 << t->scale_shift)+t->map_center.x;
+                                       c.y=tmp.y*(1 << t->scale_shift)+t->map_center.y;
+                                       dbg(1,"intersection with edge %d at 0x%x,0x%x\n",i,c.x,c.y);
+                                       if (valid)
+                                               coord_rect_extend(&msm->u.c_rect, &c);
+                                       else {
+                                               msm->u.c_rect.lu=c;
+                                               msm->u.c_rect.rl=c;
+                                               valid=1;
+                                       }
+                                       dbg(1,"rect 0x%x,0x%x - 0x%x,0x%x\n",msm->u.c_rect.lu.x,msm->u.c_rect.lu.y,msm->u.c_rect.rl.x,msm->u.c_rect.rl.y);
+                               }
+                       }
+               } else {
+                       for (i = 0 ; i < 4 ; i++) {
+                               transform_reverse(t, &screen_pnt[i], &screen[i]);
+                               dbg(1,"map(%d) %d,%d=0x%x,0x%x\n", i,screen_pnt[i].x, screen_pnt[i].y, screen[i].x, screen[i].y);
+                       }
+                       msm->u.c_rect.lu.x=min4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
+                       msm->u.c_rect.rl.x=max4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
+                       msm->u.c_rect.rl.y=min4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
+                       msm->u.c_rect.lu.y=max4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
                }
-               msm->u.c_rect.lu.x=min4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
-               msm->u.c_rect.rl.x=max4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
-               msm->u.c_rect.rl.y=min4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
-               msm->u.c_rect.lu.y=max4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
                dbg(1,"%dx%d\n", msm->u.c_rect.rl.x-msm->u.c_rect.lu.x,
                                 msm->u.c_rect.lu.y-msm->u.c_rect.rl.y);
                *msm_last=msm;
@@ -675,6 +906,7 @@ transform_set_scale(struct transformation *t, long scale)
 int
 transform_get_order(struct transformation *t)
 {
+       dbg(1,"order %d\n", t->order);
        return t->order;
 }
 
@@ -692,22 +924,22 @@ transform_distance_garmin(struct coord *c1, struct coord *c2)
 // static const int earth_radius  = 3960; //miles
  
 //Point 1 cords
-       float lat1  = GC2RAD(c1->y);
-       float long1 = GC2RAD(c1->x);
+       navit_float lat1  = GC2RAD(c1->y);
+       navit_float long1 = GC2RAD(c1->x);
 
 //Point 2 cords
-       float lat2  = GC2RAD(c2->y);
-       float long2 = GC2RAD(c2->x);
+       navit_float lat2  = GC2RAD(c2->y);
+       navit_float long2 = GC2RAD(c2->x);
 
 //Haversine Formula
-       float dlong = long2-long1;
-       float dlat  = lat2-lat1;
+       navit_float dlong = long2-long1;
+       navit_float dlat  = lat2-lat1;
 
-       float sinlat  = sinf(dlat/2);
-       float sinlong = sinf(dlong/2);
+       navit_float sinlat  = navit_sin(dlat/2);
+       navit_float sinlong = navit_sin(dlong/2);
  
-       float a=(sinlat*sinlat)+cosf(lat1)*cosf(lat2)*(sinlong*sinlong);
-       float c=2*asinf(minf(1,sqrt(a)));
+       navit_float a=(sinlat*sinlat)+navit_cos(lat1)*navit_cos(lat2)*(sinlong*sinlong);
+       navit_float c=2*navit_asin(minf(1,navit_sqrt(a)));
 #ifdef AVOID_FLOAT
        return round(earth_radius*c);
 #else
@@ -715,10 +947,10 @@ transform_distance_garmin(struct coord *c1, struct coord *c2)
 #endif
 #else
 #define GMETER 2.3887499999999999
-       double dx,dy;
+       navit_float dx,dy;
        dx=c1->x-c2->x;
        dy=c1->y-c2->y;
-       return sqrt(dx*dx+dy*dy)*GMETER;
+       return navit_sqrt(dx*dx+dy*dy)*GMETER;
 #undef GMETER
 #endif
 }
@@ -731,12 +963,26 @@ transform_scale(int y)
        c.x=0;
        c.y=y;
        transform_to_geo(projection_mg, &c, &g);
-       return 1/cos(g.lat/180*M_PI);
+       return 1/navit_cos(g.lat/180*M_PI);
 }
 
 #ifdef AVOID_FLOAT
 static int
 tab_sqrt[]={14142,13379,12806,12364,12018,11741,11517,11333,11180,11051,10943,10850,10770,10701,10640,10587,10540,10499,10462,10429,10400,10373,10349,10327,10307,10289,10273,10257,10243,10231,10219,10208};
+
+static int tab_int_step = 0x20000;
+static int tab_int_scale[]={10000,10002,10008,10019,10033,10052,10076,10103,10135,10171,10212,10257,10306,10359,10417,10479,10546,10617,10693,10773,10858,10947,11041,11140,11243,11352,11465,11582,11705,11833,11965,12103,12246,12394,12547,12706,12870,13039,13214,13395,13581,13773,13971,14174,14384,14600,14822,15050,15285,15526,15774,16028,16289,16557,16832,17114,17404,17700,18005,18316,18636,18964,19299,19643,19995,20355,20724,21102,21489,21885,22290,22705,23129,23563,24007,24461,24926,25401,25886,26383,26891};
+
+int transform_int_scale(int y)
+{
+       int a=tab_int_step,i,size = sizeof(tab_int_scale)/sizeof(int);
+       if (y < 0)
+               y=-y;
+       i=y/tab_int_step;
+       if (i < size-1) 
+               return tab_int_scale[i]+((tab_int_scale[i+1]-tab_int_scale[i])*(y-i*tab_int_step))/tab_int_step;
+       return tab_int_scale[size-1];
+}
 #endif
 
 double
@@ -749,7 +995,7 @@ transform_distance(enum projection pro, struct coord *c1, struct coord *c2)
        dy=c1->y-c2->y;
        return sqrt(dx*dx+dy*dy)/scale;
 #else
-       int dx,dy,f,scale=15539;
+       int dx,dy,f,scale=transform_int_scale((c1->y+c2->y)/2);
        dx=c1->x-c2->x;
        dy=c1->y-c2->y;
        if (dx < 0)
@@ -785,6 +1031,24 @@ transform_distance(enum projection pro, struct coord *c1, struct coord *c2)
        }
 }
 
+void
+transform_project(enum projection pro, struct coord *c, int distance, int angle, struct coord *res)
+{
+       double scale;
+       switch (pro) {
+       case projection_mg:
+               scale=transform_scale(c->y);
+               res->x=c->x+distance*sin(angle*M_PI/180)*scale;
+               res->y=c->y+distance*cos(angle*M_PI/180)*scale;
+               break;
+       default:
+               dbg(0,"Unsupported projection: %d\n", pro);
+               return;
+       }
+       
+}
+
+
 double
 transform_polyline_length(enum projection pro, struct coord *c, int count)
 {
@@ -876,6 +1140,30 @@ transform_distance_polyline_sq(struct coord *c, int count, struct coord *ref, st
        return dist;
 }
 
+int
+transform_douglas_peucker(struct coord *in, int count, int dist_sq, struct coord *out)
+{
+       int ret=0;
+       int i,d,dmax=0, idx=0;
+       for (i = 1; i < count-1 ; i++) {
+               d=transform_distance_line_sq(&in[0], &in[count-1], &in[i], NULL);
+               if (d > dmax) {
+                       idx=i;
+                       dmax=d;
+               }
+       }
+       if (dmax > dist_sq) {
+               ret=transform_douglas_peucker(in, idx+1, dist_sq, out)-1;
+               ret+=transform_douglas_peucker(in+idx, count-idx, dist_sq, out+ret);
+       } else {
+               if (count > 0)
+                       out[ret++]=in[0];
+               if (count > 1)
+                       out[ret++]=in[count-1];
+       }
+       return ret;
+}
+
 
 void
 transform_print_deg(double deg)