2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
31 #include "transform.h"
32 #include "projection.h"
36 /* #define ENABLE_ROLL */
38 struct transformation {
39 int yaw; /* Rotation angle */
42 int m00,m01,m10,m11; /* 2d transformation matrix */
44 int m20,m21; /* additional 3d parameters */
49 double im02,im12,im20,im21,im22;
51 double im00,im01,im10,im11; /* inverse 2d transformation matrix */
52 struct map_selection *map_sel;
53 struct map_selection *screen_sel;
54 struct point screen_center;
57 struct coord map_center; /* Center of source rectangle */
59 double scale; /* Scale factor */
65 transform_setup_matrix(struct transformation *t)
69 double yawc=cos(-M_PI*t->yaw/180);
70 double yaws=sin(-M_PI*t->yaw/180);
71 double pitchc=cos(-M_PI*t->pitch/180);
72 double pitchs=sin(-M_PI*t->pitch/180);
74 double rollc=cos(M_PI*t->roll/180);
75 double rolls=sin(M_PI*t->roll/180);
81 dbg(1,"yaw=%d pitch=%d center=0x%x,0x%x\n", t->yaw, t->pitch, t->map_center.x, t->map_center.y);
96 fac=(1 << POST_SHIFT) * (1 << t->scale_shift) / t->scale;
97 dbg(1,"scale_shift=%d order=%d scale=%f fac=%f\n", t->scale_shift, t->order,t->scale,fac);
100 t->m00=rollc*yawc*fac;
101 t->m01=rollc*yaws*fac;
103 t->m10=(pitchs*rolls*yawc-pitchc*yaws)*(-fac);
104 t->m11=(pitchs*rolls*yaws+pitchc*yawc)*(-fac);
105 t->m12=pitchs*rollc*(-fac);
106 t->m20=(pitchc*rolls*yawc+pitchs*yaws)*fac;
107 t->m21=(pitchc*rolls*yaws-pitchs*yawc)*fac;
108 t->m22=pitchc*rollc*fac;
112 t->m10=(-pitchc*yaws)*(-fac);
113 t->m11=pitchc*yawc*(-fac);
114 t->m20=pitchs*yaws*fac;
115 t->m21=(-pitchs*yawc)*fac;
120 t->offx=t->screen_center.x;
121 t->offy=t->screen_center.y;
124 t->offz=t->screen_dist;
128 det=(double)t->m00*(double)t->m11*(double)t->m22+
129 (double)t->m01*(double)t->m12*(double)t->m20+
130 (double)t->m02*(double)t->m10*(double)t->m21-
131 (double)t->m02*(double)t->m11*(double)t->m20-
132 (double)t->m01*(double)t->m10*(double)t->m22-
133 (double)t->m00*(double)t->m12*(double)t->m21;
135 t->im00=(t->m11*t->m22-t->m12*t->m21)/det;
136 t->im01=(t->m02*t->m21-t->m01*t->m22)/det;
137 t->im02=(t->m01*t->m12-t->m02*t->m11)/det;
138 t->im10=(t->m12*t->m20-t->m10*t->m22)/det;
139 t->im11=(t->m00*t->m22-t->m02*t->m20)/det;
140 t->im12=(t->m02*t->m10-t->m00*t->m12)/det;
141 t->im20=(t->m10*t->m21-t->m11*t->m20)/det;
142 t->im21=(t->m01*t->m20-t->m00*t->m21)/det;
143 t->im22=(t->m00*t->m11-t->m01*t->m10)/det;
145 det=((double)t->m00*(double)t->m11-(double)t->m01*(double)t->m10);
153 struct transformation *
156 struct transformation *this_;
158 this_=g_new0(struct transformation, 1);
159 this_->screen_dist=100;
167 transform_setup_matrix(this_);
171 struct transformation *
172 transform_dup(struct transformation *t)
174 struct transformation *ret=g_new0(struct transformation, 1);
179 static const double gar2geo_units = 360.0/(1<<24);
180 static const double geo2gar_units = 1/(360.0/(1<<24));
183 transform_to_geo(enum projection pro, struct coord *c, struct coord_geo *g)
187 g->lng=c->x/6371000.0/M_PI*180;
188 g->lat=atan(exp(c->y/6371000.0))/M_PI*360-90;
190 case projection_garmin:
191 g->lng=c->x*gar2geo_units;
192 g->lat=c->y*gar2geo_units;
200 transform_from_geo(enum projection pro, struct coord_geo *g, struct coord *c)
204 c->x=g->lng*6371000.0*M_PI/180;
205 c->y=log(tan(M_PI_4+g->lat*M_PI/360))*6371000.0;
207 case projection_garmin:
208 c->x=g->lng*geo2gar_units;
209 c->y=g->lat*geo2gar_units;
217 transform_from_to(struct coord *cfrom, enum projection from, struct coord *cto, enum projection to)
220 transform_to_geo(from, cfrom, &g);
221 transform_from_geo(to, &g, cto);
225 transform_geo_to_cart(struct coord_geo *geo, double a, double b, struct coord_geo_cart *cart)
227 double n,ee=1-b*b/(a*a);
228 n = a/sqrt(1-ee*sin(geo->lat)*sin(geo->lat));
229 cart->x=n*cos(geo->lat)*cos(geo->lng);
230 cart->y=n*cos(geo->lat)*sin(geo->lng);
231 cart->z=n*(1-ee)*sin(geo->lat);
235 transform_cart_to_geo(struct coord_geo_cart *cart, double a, double b, struct coord_geo *geo)
237 double lat,lati,n,ee=1-b*b/(a*a), lng = atan(cart->y/cart->x);
239 lat = atan(cart->z / sqrt((cart->x * cart->x) + (cart->y * cart->y)));
244 n = a / sqrt(1-ee*sin(lat)*sin(lat));
245 lat = atan((cart->z + ee * n * sin(lat)) / sqrt(cart->x * cart->x + cart->y * cart->y));
247 while (fabs(lat - lati) >= 0.000000000000001);
249 geo->lng=lng/M_PI*180;
250 geo->lat=lat/M_PI*180;
255 transform_datum(struct coord_geo *from, enum map_datum from_datum, struct coord_geo *to, enum map_datum to_datum)
260 transform(struct transformation *t, enum projection pro, struct coord *c, struct point *p, int count, int unique, int width, int *width_return)
265 int xc, yc, zc=0, xco=0, yco=0, zco=0;
268 int visible, visibleo=-1;
270 dbg(1,"count=%d\n", count);
271 for (i=0; i < count; i++) {
276 transform_to_geo(pro, &c[i], &g);
277 transform_from_geo(t->pro, &g, &c1);
283 // 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);
284 // ret=coord_rect_contains(&t->r, c);
287 xc >>= t->scale_shift;
288 yc >>= t->scale_shift;
290 xcn=xc*t->m00+yc*t->m01+t->hog*t->m02;
291 ycn=xc*t->m10+yc*t->m11+t->hog*t->m12;
293 xcn=xc*t->m00+yc*t->m01;
294 ycn=xc*t->m10+yc*t->m11;
299 zc=(xc*t->m20+yc*t->m21+t->hog*t->m22);
301 zc=(xc*t->m20+yc*t->m21);
304 zc+=t->offz << POST_SHIFT;
305 dbg(1,"zc=%d\n", zc);
306 dbg(1,"zc(%d)=xc(%d)*m20(%d)+yc(%d)*m21(%d)\n", (xc*t->m20+yc*t->m21), xc, t->m20, yc, t->m21);
308 visible=(zc < zlimit ? 0:1);
309 dbg(1,"visible=%d old %d\n", visible, visibleo);
310 if (visible != visibleo && visibleo != -1) {
311 dbg(1,"clipping (%d,%d,%d)-(%d,%d,%d) (%d,%d,%d)\n", xcn, ycn, zc, xco, yco, zco, xco-xcn, yco-ycn, zco-zc);
313 xcn=xcn+(long long)(xco-xcn)*(zlimit-zc)/(zco-zc);
314 ycn=ycn+(long long)(yco-ycn)*(zlimit-zc)/(zco-zc);
316 dbg(1,"result (%d,%d,%d) * %d / %d\n", xcn,ycn,zc,zlimit-zc,zco-zc);
332 dbg(1,"zc=%d\n", zc);
333 dbg(1,"xcn %d ycn %d\n", xcn, ycn);
334 dbg(1,"%d,%d %d\n",xc,yc,zc);
336 dbg(0,"%d/%d=%d %d/%d=%d\n",xcn,xc,xcn/xc,ycn,yc,ycn/yc);
339 xc=(long long)xcn*t->xyscale/zc;
340 yc=(long long)ycn*t->xyscale/zc;
345 dbg(1,"%d,%d %d\n",xc,yc,zc);
354 dbg(1,"xc=%d yc=%d\n", xc, yc);
355 if (j == 0 || !unique || p[j-1].x != xc || p[j-1].y != yc) {
360 width_return[j]=width*(t->offz << POST_SHIFT)/zc;
362 width_return[j]=width;
371 transform_reverse(struct transformation *t, struct point *p, struct coord *c)
373 double zc,xc,yc,xcn,ycn,q;
374 double offz=t->offz << POST_SHIFT;
380 double f00=xc*t->im00*t->m20;
381 double f01=yc*t->im01*t->m20;
382 double f10=xc*t->im10*t->m21;
383 double f11=yc*t->im11*t->m21;
385 q=(1-f00-f01-t->im02*t->m20-f10-f11-t->im12*t->m21);
388 zc=(offz*((f00+f01+f10+f11))+t->hog*t->m22)/q;
390 q=(1-f00-f01-f10-f11);
393 zc=offz*(f00+f01+f10+f11)/q;
398 xc=xcn*t->im00+ycn*t->im01+zc*t->im02;
399 yc=xcn*t->im10+ycn*t->im11+zc*t->im12;
401 xc=xcn*t->im00+ycn*t->im01;
402 yc=xcn*t->im10+ycn*t->im11;
408 xc=(xcn*t->im00+ycn*t->im01)*(1 << POST_SHIFT);
409 yc=(xcn*t->im10+ycn*t->im11)*(1 << POST_SHIFT);
411 c->x=xc*(1 << t->scale_shift)+t->map_center.x;
412 c->y=yc*(1 << t->scale_shift)+t->map_center.y;
416 transform_get_projection(struct transformation *this_)
422 transform_set_projection(struct transformation *this_, enum projection pro)
428 min4(int v1,int v2, int v3, int v4)
441 max4(int v1,int v2, int v3, int v4)
453 struct map_selection *
454 transform_get_selection(struct transformation *this_, enum projection pro, int order)
457 struct map_selection *ret,*curri,*curro;
460 ret=map_selection_dup(this_->map_sel);
461 curri=this_->map_sel;
464 if (this_->pro != pro) {
465 transform_to_geo(this_->pro, &curri->u.c_rect.lu, &g);
466 transform_from_geo(pro, &g, &curro->u.c_rect.lu);
467 dbg(1,"%f,%f", g.lat, g.lng);
468 transform_to_geo(this_->pro, &curri->u.c_rect.rl, &g);
469 transform_from_geo(pro, &g, &curro->u.c_rect.rl);
470 dbg(1,": - %f,%f\n", g.lat, g.lng);
472 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);
474 curro->range=item_range_all;
482 transform_center(struct transformation *this_)
484 return &this_->map_center;
488 transform_get_center(struct transformation *this_)
490 return &this_->map_center;
494 transform_set_center(struct transformation *this_, struct coord *c)
496 this_->map_center=*c;
501 transform_set_yaw(struct transformation *t,int yaw)
504 transform_setup_matrix(t);
508 transform_get_yaw(struct transformation *this_)
514 transform_set_pitch(struct transformation *this_,int pitch)
517 transform_setup_matrix(this_);
520 transform_get_pitch(struct transformation *this_)
527 transform_set_roll(struct transformation *this_,int roll)
530 transform_setup_matrix(this_);
534 transform_get_roll(struct transformation *this_)
541 transform_set_distance(struct transformation *this_,int distance)
543 this_->screen_dist=distance;
544 transform_setup_matrix(this_);
548 transform_get_distance(struct transformation *this_)
550 return this_->screen_dist;
554 transform_set_screen_selection(struct transformation *t, struct map_selection *sel)
556 map_selection_destroy(t->screen_sel);
557 t->screen_sel=map_selection_dup(sel);
559 t->screen_center.x=(sel->u.p_rect.rl.x-sel->u.p_rect.lu.x)/2;
560 t->screen_center.y=(sel->u.p_rect.rl.y-sel->u.p_rect.lu.y)/2;
561 transform_setup_matrix(t);
566 transform_set_screen_center(struct transformation *t, struct point *p)
573 transform_set_size(struct transformation *t, int width, int height)
581 transform_get_size(struct transformation *t, int *width, int *height)
583 struct point_rect *r;
585 r=&t->screen_sel->u.p_rect;
586 *width=r->rl.x-r->lu.x;
587 *height=r->rl.y-r->lu.y;
592 transform_setup(struct transformation *t, struct pcoord *c, int scale, int yaw)
595 t->map_center.x=c->x;
596 t->map_center.y=c->y;
598 transform_set_yaw(t, yaw);
604 transform_setup_source_rect_limit(struct transformation *t, struct coord *center, int limit)
609 t->r.lu.x=center->x-limit;
610 t->r.rl.x=center->x+limit;
611 t->r.rl.y=center->y-limit;
612 t->r.lu.y=center->y+limit;
617 transform_setup_source_rect(struct transformation *t)
620 struct coord screen[4];
621 struct point screen_pnt[4];
622 struct point_rect *pr;
623 struct map_selection *ms,*msm,*next,**msm_last;
631 msm_last=&t->map_sel;
634 msm=g_new0(struct map_selection, 1);
637 screen_pnt[0].x=pr->lu.x;
638 screen_pnt[0].y=pr->lu.y;
639 screen_pnt[1].x=pr->rl.x;
640 screen_pnt[1].y=pr->lu.y;
641 screen_pnt[2].x=pr->lu.x;
642 screen_pnt[2].y=pr->rl.y;
643 screen_pnt[3].x=pr->rl.x;
644 screen_pnt[3].y=pr->rl.y;
645 for (i = 0 ; i < 4 ; i++) {
646 transform_reverse(t, &screen_pnt[i], &screen[i]);
647 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);
649 msm->u.c_rect.lu.x=min4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
650 msm->u.c_rect.rl.x=max4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
651 msm->u.c_rect.rl.y=min4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
652 msm->u.c_rect.lu.y=max4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
653 dbg(1,"%dx%d\n", msm->u.c_rect.rl.x-msm->u.c_rect.lu.x,
654 msm->u.c_rect.lu.y-msm->u.c_rect.rl.y);
662 transform_get_scale(struct transformation *t)
664 return (int)(t->scale*16);
668 transform_set_scale(struct transformation *t, long scale)
671 transform_setup_matrix(t);
676 transform_get_order(struct transformation *t)
683 #define TWOPI (M_PI*2)
684 #define GC2RAD(c) ((c) * TWOPI/(1<<24))
685 #define minf(a,b) ((a) < (b) ? (a) : (b))
688 transform_distance_garmin(struct coord *c1, struct coord *c2)
691 static const int earth_radius = 6371*1000; //m change accordingly
692 // static const int earth_radius = 3960; //miles
695 float lat1 = GC2RAD(c1->y);
696 float long1 = GC2RAD(c1->x);
699 float lat2 = GC2RAD(c2->y);
700 float long2 = GC2RAD(c2->x);
703 float dlong = long2-long1;
704 float dlat = lat2-lat1;
706 float sinlat = sinf(dlat/2);
707 float sinlong = sinf(dlong/2);
709 float a=(sinlat*sinlat)+cosf(lat1)*cosf(lat2)*(sinlong*sinlong);
710 float c=2*asinf(minf(1,sqrt(a)));
712 return round(earth_radius*c);
714 return earth_radius*c;
717 #define GMETER 2.3887499999999999
721 return sqrt(dx*dx+dy*dy)*GMETER;
727 transform_scale(int y)
733 transform_to_geo(projection_mg, &c, &g);
734 return 1/cos(g.lat/180*M_PI);
739 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};
743 transform_distance(enum projection pro, struct coord *c1, struct coord *c2)
745 if (pro == projection_mg) {
747 double dx,dy,scale=transform_scale((c1->y+c2->y)/2);
750 return sqrt(dx*dx+dy*dy)/scale;
752 int dx,dy,f,scale=15539;
759 while (dx > 20000 || dy > 20000) {
765 return dx*10000/scale;
767 return dy*10000/scale;
771 return dx*10000/scale;
772 return dx*tab_sqrt[f]/scale;
776 return dy*10000/scale;
777 return dy*tab_sqrt[f]/scale;
780 } else if (pro == projection_garmin) {
781 return transform_distance_garmin(c1, c2);
783 dbg(0,"Unknown projection: %d\n", pro);
789 transform_polyline_length(enum projection pro, struct coord *c, int count)
794 for (i = 0 ; i < count-1 ; i++)
795 ret+=transform_distance(pro, &c[i], &c[i+1]);
800 transform_distance_sq(struct coord *c1, struct coord *c2)
805 if (dx > 32767 || dy > 32767 || dx < -32767 || dy < -32767)
812 transform_distance_sq_pc(struct pcoord *c1, struct pcoord *c2)
815 p1.x = c1->x; p1.y = c1->y;
816 p2.x = c2->x; p2.y = c2->y;
817 return transform_distance_sq(&p1, &p2);
821 transform_distance_line_sq(struct coord *l0, struct coord *l1, struct coord *ref, struct coord *lpnt)
837 return transform_distance_sq(l0, ref);
843 return transform_distance_sq(l1, ref);
845 while (c1 > climit || c2 > climit) {
853 return transform_distance_sq(&l, ref);
857 transform_distance_polyline_sq(struct coord *c, int count, struct coord *ref, struct coord *lpnt, int *pos)
865 dist=transform_distance_line_sq(&c[0], &c[1], ref, lpnt);
866 for (i=2 ; i < count ; i++) {
867 distn=transform_distance_line_sq(&c[i-1], &c[i], ref, &lp);
881 transform_print_deg(double deg)
883 printf("%2.0f:%2.0f:%2.4f", floor(deg), fmod(deg*60,60), fmod(deg*3600,60));
887 static int tab_atan[]={0,262,524,787,1051,1317,1584,1853,2126,2401,2679,2962,3249,3541,3839,4142,4452,4770,5095,5430,5774,6128,6494,6873,7265,7673,8098,8541,9004,9490,10000,10538};
890 atan2_int_lookup(int val)
892 int len=sizeof(tab_atan)/sizeof(int);
897 if (val < tab_atan[p])
900 if (val < tab_atan[p+1])
908 atan2_int(int dx, int dy)
910 int f,mul=1,add=0,ret;
912 return dy < 0 ? 180 : 0;
915 return dx < 0 ? -90 : 90;
926 while (dx > 20000 || dy > 20000) {
931 ret=90-atan2_int_lookup(dy*10000/dx);
933 ret=atan2_int_lookup(dx*10000/dy);
940 transform_get_angle_delta(struct coord *c1, struct coord *c2, int dir)
950 angle=atan2_int(dx,dy);
960 transform_within_border(struct transformation *this_, struct point *p, int border)
962 struct map_selection *ms=this_->screen_sel;
964 struct point_rect *r=&ms->u.p_rect;
965 if (p->x >= r->lu.x+border && p->x <= r->rl.x-border &&
966 p->y >= r->lu.y+border && p->y <= r->rl.y-border)
974 transform_within_dist_point(struct coord *ref, struct coord *c, int dist)
976 if (c->x-dist > ref->x)
978 if (c->x+dist < ref->x)
980 if (c->y-dist > ref->y)
982 if (c->y+dist < ref->y)
984 if ((c->x-ref->x)*(c->x-ref->x) + (c->y-ref->y)*(c->y-ref->y) <= dist*dist)
990 transform_within_dist_line(struct coord *ref, struct coord *c0, struct coord *c1, int dist)
997 if (c0->x-dist > ref->x)
999 if (c1->x+dist < ref->x)
1002 if (c1->x-dist > ref->x)
1004 if (c0->x+dist < ref->x)
1007 if (c0->y < c1->y) {
1008 if (c0->y-dist > ref->y)
1010 if (c1->y+dist < ref->y)
1013 if (c1->y-dist > ref->y)
1015 if (c0->y+dist < ref->y)
1025 return transform_within_dist_point(ref, c0, dist);
1028 return transform_within_dist_point(ref, c1, dist);
1030 lc.x=c0->x+vx*n1/n2;
1031 lc.y=c0->y+vy*n1/n2;
1032 return transform_within_dist_point(ref, &lc, dist);
1036 transform_within_dist_polyline(struct coord *ref, struct coord *c, int count, int close, int dist)
1039 for (i = 0 ; i < count-1 ; i++) {
1040 if (transform_within_dist_line(ref,c+i,c+i+1,dist)) {
1045 return (transform_within_dist_line(ref,c,c+count-1,dist));
1050 transform_within_dist_polygon(struct coord *ref, struct coord *c, int count, int dist)
1053 for (i = 0, j = count-1; i < count; j = i++) {
1054 if ((((c[i].y <= ref->y) && ( ref->y < c[j].y )) ||
1055 ((c[j].y <= ref->y) && ( ref->y < c[i].y))) &&
1056 (ref->x < (c[j].x - c[i].x) * (ref->y - c[i].y) / (c[j].y - c[i].y) + c[i].x))
1061 return transform_within_dist_polyline(ref, c, count, dist, 1);
1069 transform_within_dist_item(struct coord *ref, enum item_type type, struct coord *c, int count, int dist)
1071 if (type < type_line)
1072 return transform_within_dist_point(ref, c, dist);
1073 if (type < type_area)
1074 return transform_within_dist_polyline(ref, c, count, 0, dist);
1075 return transform_within_dist_polygon(ref, c, count, dist);
1079 transform_destroy(struct transformation *t)
1086 Note: there are many mathematically equivalent ways to express these formulas. As usual, not all of them are computationally equivalent.
1088 L = latitude in radians (positive north)
1089 Lo = longitude in radians (positive east)
1090 E = easting (meters)
1091 N = northing (meters)
1096 N = r ln [ tan (pi/4 + L/2) ]
1100 r = radius of the sphere (meters)
1101 ln() is the natural logarithm
1106 N = a * ln ( tan (pi/4 + L/2) * ( (1 - e * sin (L)) / (1 + e * sin (L))) ** (e/2) )
1112 = a ln( tan( ---- + ---) (--------------) )
1118 a = the length of the semi-major axis of the ellipsoid (meters)
1119 e = the first eccentricity of the ellipsoid