Merge branch 'upstream' into maemo
[navit-package] / navit / transform.c
index ce42a26..4708c02 100644 (file)
@@ -38,16 +38,18 @@ struct transformation {
        int yaw;                /* Rotation angle */
        int pitch;
        int ddd;
-       int m00,m01,m10,m11;    /* 2d transformation matrix */
+       int m00,m01,m02;        /* 3d transformation matrix */
+       int m10,m11,m12;        
+       int m20,m21,m22;        
        int xscale,yscale,wscale;
-       int m20,m21;            /* additional 3d parameters */
+       int xscale3d,yscale3d,wscale3d;
 #ifdef ENABLE_ROLL
        int roll;
-       int m02,m12,m22; 
        int hog;
-       navit_float im02,im12,im20,im21,im22;
 #endif
-       navit_float 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;
@@ -62,25 +64,26 @@ struct transformation {
        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)
 {
        navit_float det;
        navit_float fac;
-#if 0 /* FIXME */
-#if 0
-       t->roll=0;
-       t->pitch=90;
-       t->yaw=0;
-#endif
-#if 0
-       t->scale=1;
-#endif
-       t->hog=2;
-       t->ddd=1;
-#endif
        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);
@@ -88,6 +91,9 @@ transform_setup_matrix(struct transformation *t)
 #ifdef ENABLE_ROLL     
        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;
@@ -95,7 +101,7 @@ transform_setup_matrix(struct transformation *t)
 
        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=1000*t->znear;
+       t->zfar=300*t->znear;
        t->scale_shift=0;
        t->order=t->order_base;
        if (t->scale >= 1) {
@@ -113,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;
@@ -123,35 +128,23 @@ 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->xscale=1;
-       t->yscale=1;
-       t->wscale=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->xscale=t->offz;
-               t->yscale=t->offz;
-               t->wscale=t->offz << POST_SHIFT;
+               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;
        }
-#if 0 /* FIXME */
-       t->offz=0;
-       t->xscale=750;
-       t->yscale=620;
-       t->wscale=32 << POST_SHIFT;
-#endif
-#ifdef ENABLE_ROLL
        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-
@@ -168,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=((navit_float)t->m00*(navit_float)t->m11-(navit_float)t->m01*(navit_float)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 *
@@ -183,7 +169,7 @@ 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;
@@ -196,35 +182,52 @@ transform_new(void)
        return this_;
 }
 
-#ifdef ENABLE_ROLL
-
 int
 transform_get_hog(struct transformation *this_)
 {
-       return this_->hog;
+       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_hog(struct transformation *this_)
-{
-       return 0;
 }
 
-void
-transform_set_hog(struct transformation *this_, int hog)
+int
+transform_get_attr(struct transformation *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
 {
-       dbg(0,"not supported\n");
+       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_)
@@ -419,7 +422,8 @@ transform(struct transformation *t, enum projection pro, struct coord *c, struct
                        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) 
+                       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;
                }
@@ -433,20 +437,12 @@ transform(struct transformation *t, enum projection pro, struct coord *c, struct
                yc >>= t->scale_shift;
                xm=xc;
                ym=yc;
-#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
+
+               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);
@@ -516,15 +512,9 @@ transform(struct transformation *t, enum projection pro, struct coord *c, struct
 static void
 transform_apply_inverse_matrix(struct transformation *t, struct coord_geo_cart *in, struct coord_geo_cart *out)
 {
-#ifdef ENABLE_ROLL
        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;
-#else
-       out->x=in->x*t->im00+in->y*t->im01;
-       out->y=in->x*t->im10+in->y*t->im11;
-       out->z=0;
-#endif
 }
 
 static int
@@ -555,7 +545,6 @@ transform_screen_to_3d(struct transformation *t, struct point *p, navit_float z,
        double offz=t->offz << POST_SHIFT;
        xc=p->x - t->offx;
        yc=p->y - t->offy;
-       z+=offz;
        cg->x=xc*z/t->xscale;
        cg->y=yc*z/t->yscale;
        cg->z=z-offz;
@@ -565,7 +554,6 @@ static int
 transform_reverse_near_far(struct transformation *t, struct point *p, struct coord *c, int near, int far)
 {
         double xc,yc;
-       int hog;
        dbg(1,"%d,%d\n",p->x,p->y);
        if (t->ddd) {
                struct coord_geo_cart nearc,farc,nears,fars,intersection;
@@ -573,12 +561,7 @@ transform_reverse_near_far(struct transformation *t, struct point *p, struct coo
                transform_screen_to_3d(t, p, far, &farc);
                transform_apply_inverse_matrix(t, &nearc, &nears);
                transform_apply_inverse_matrix(t, &farc, &fars);
-#ifdef ENABLE_ROLL
-               hog=t->hog;
-#else
-               hog=0;
-#endif
-               if (transform_zplane_intersection(&nears, &fars, hog, &intersection) != 1)
+               if (transform_zplane_intersection(&nears, &fars, HOG(*t), &intersection) != 1)
                        return 0;
                xc=intersection.x;
                yc=intersection.y;
@@ -714,40 +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_);
-}
-
-int
-transform_get_roll(struct transformation *this_)
-{
-       return this_->roll;
-}
-
 #else
-
-void
-transform_set_roll(struct transformation *this_,int roll)
-{
        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_);
 }
 
@@ -758,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);
@@ -853,11 +835,6 @@ transform_setup_source_rect(struct transformation *t)
                        struct coord_geo_cart tmp,cg[8];
                        struct coord c;
                        int valid=0;
-#ifdef ENABLE_ROLL
-                       int hog=t->hog;
-#else
-                       int hog=0;
-#endif
                        char edgenodes[]={
                                0,1,
                                1,2,
@@ -880,10 +857,10 @@ transform_setup_source_rect(struct transformation *t)
                        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, &tmp) == 1) {
+                               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(0,"intersection with edge %d at 0x%x,0x%x\n",i,c.x,c.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 {
@@ -891,7 +868,7 @@ transform_setup_source_rect(struct transformation *t)
                                                msm->u.c_rect.rl=c;
                                                valid=1;
                                        }
-                                       dbg(0,"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);
+                                       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 {