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.
20 #define _USE_MATH_DEFINES 1
32 #include "transform.h"
33 #include "projection.h"
38 struct transformation {
39 int yaw; /* Rotation angle */
42 int m00,m01,m02; /* 3d transformation matrix */
45 int xscale,yscale,wscale;
46 int xscale3d,yscale3d,wscale3d;
51 navit_float im00,im01,im02; /* inverse 3d transformation matrix */
52 navit_float im10,im11,im12;
53 navit_float im20,im21,im22;
54 struct map_selection *map_sel;
55 struct map_selection *screen_sel;
56 struct point screen_center;
60 struct coord map_center; /* Center of source rectangle */
62 navit_float scale; /* Scale factor */
69 #define HOG(t) ((t).hog)
75 transform_set_screen_dist(struct transformation *t, int dist)
80 t->wscale3d=dist << POST_SHIFT;
84 transform_setup_matrix(struct transformation *t)
88 navit_float yawc=navit_cos(-M_PI*t->yaw/180);
89 navit_float yaws=navit_sin(-M_PI*t->yaw/180);
90 navit_float pitchc=navit_cos(-M_PI*t->pitch/180);
91 navit_float pitchs=navit_sin(-M_PI*t->pitch/180);
93 navit_float rollc=navit_cos(M_PI*t->roll/180);
94 navit_float rolls=navit_sin(M_PI*t->roll/180);
103 dbg(1,"yaw=%d pitch=%d center=0x%x,0x%x\n", t->yaw, t->pitch, t->map_center.x, t->map_center.y);
104 t->znear=1 << POST_SHIFT;
105 t->zfar=300*t->znear;
107 t->order=t->order_base;
120 fac=(1 << POST_SHIFT) * (1 << t->scale_shift) / t->scale;
121 dbg(1,"scale_shift=%d order=%d scale=%f fac=%f\n", t->scale_shift, t->order,t->scale,fac);
123 t->m00=rollc*yawc*fac;
124 t->m01=rollc*yaws*fac;
126 t->m10=(pitchs*rolls*yawc-pitchc*yaws)*(-fac);
127 t->m11=(pitchs*rolls*yaws+pitchc*yawc)*(-fac);
128 t->m12=pitchs*rollc*(-fac);
129 t->m20=(pitchc*rolls*yawc+pitchs*yaws)*fac;
130 t->m21=(pitchc*rolls*yaws-pitchs*yawc)*fac;
131 t->m22=pitchc*rollc*fac;
133 t->offx=t->screen_center.x;
134 t->offy=t->screen_center.y;
137 t->offz=t->screen_dist;
138 dbg(1,"near %d far %d\n",t->znear,t->zfar);
139 t->xscale=t->xscale3d;
140 t->yscale=t->yscale3d;
141 t->wscale=t->wscale3d;
149 det=(navit_float)t->m00*(navit_float)t->m11*(navit_float)t->m22+
150 (navit_float)t->m01*(navit_float)t->m12*(navit_float)t->m20+
151 (navit_float)t->m02*(navit_float)t->m10*(navit_float)t->m21-
152 (navit_float)t->m02*(navit_float)t->m11*(navit_float)t->m20-
153 (navit_float)t->m01*(navit_float)t->m10*(navit_float)t->m22-
154 (navit_float)t->m00*(navit_float)t->m12*(navit_float)t->m21;
156 t->im00=(t->m11*t->m22-t->m12*t->m21)/det;
157 t->im01=(t->m02*t->m21-t->m01*t->m22)/det;
158 t->im02=(t->m01*t->m12-t->m02*t->m11)/det;
159 t->im10=(t->m12*t->m20-t->m10*t->m22)/det;
160 t->im11=(t->m00*t->m22-t->m02*t->m20)/det;
161 t->im12=(t->m02*t->m10-t->m00*t->m12)/det;
162 t->im20=(t->m10*t->m21-t->m11*t->m20)/det;
163 t->im21=(t->m01*t->m20-t->m00*t->m21)/det;
164 t->im22=(t->m00*t->m11-t->m01*t->m10)/det;
167 struct transformation *
170 struct transformation *this_;
172 this_=g_new0(struct transformation, 1);
173 transform_set_screen_dist(this_, 100);
174 this_->order_base=14;
182 transform_setup_matrix(this_);
187 transform_get_hog(struct transformation *this_)
193 transform_set_hog(struct transformation *this_, int hog)
198 dbg(0,"not supported\n");
204 transform_get_attr(struct transformation *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
209 attr->u.num=this_->hog;
220 transform_set_attr(struct transformation *this_, struct attr *attr)
222 switch (attr->type) {
225 this_->hog=attr->u.num;
234 transformation_get_order_base(struct transformation *this_)
236 return this_->order_base;
240 transform_set_order_base(struct transformation *this_, int order_base)
242 this_->order_base=order_base;
246 struct transformation *
247 transform_dup(struct transformation *t)
249 struct transformation *ret=g_new0(struct transformation, 1);
254 static const navit_float gar2geo_units = 360.0/(1<<24);
255 static const navit_float geo2gar_units = 1/(360.0/(1<<24));
258 transform_to_geo(enum projection pro, struct coord *c, struct coord_geo *g)
260 int x,y,northern,zone;
263 g->lng=c->x/6371000.0/M_PI*180;
264 g->lat=navit_atan(exp(c->y/6371000.0))/M_PI*360-90;
266 case projection_garmin:
267 g->lng=c->x*gar2geo_units;
268 g->lat=c->y*gar2geo_units;
279 transform_utm_to_geo(x, y, zone, northern, g);
287 transform_from_geo(enum projection pro, struct coord_geo *g, struct coord *c)
291 c->x=g->lng*6371000.0*M_PI/180;
292 c->y=log(navit_tan(M_PI_4+g->lat*M_PI/360))*6371000.0;
294 case projection_garmin:
295 c->x=g->lng*geo2gar_units;
296 c->y=g->lat*geo2gar_units;
304 transform_from_to_count(struct coord *cfrom, enum projection from, struct coord *cto, enum projection to, int count)
309 for (i = 0 ; i < count ; i++) {
310 transform_to_geo(from, cfrom, &g);
311 transform_from_geo(to, &g, cto);
318 transform_from_to(struct coord *cfrom, enum projection from, struct coord *cto, enum projection to)
321 transform_to_geo(from, cfrom, &g);
322 transform_from_geo(to, &g, cto);
326 transform_geo_to_cart(struct coord_geo *geo, navit_float a, navit_float b, struct coord_geo_cart *cart)
328 navit_float n,ee=1-b*b/(a*a);
329 n = a/sqrtf(1-ee*navit_sin(geo->lat)*navit_sin(geo->lat));
330 cart->x=n*navit_cos(geo->lat)*navit_cos(geo->lng);
331 cart->y=n*navit_cos(geo->lat)*navit_sin(geo->lng);
332 cart->z=n*(1-ee)*navit_sin(geo->lat);
336 transform_cart_to_geo(struct coord_geo_cart *cart, navit_float a, navit_float b, struct coord_geo *geo)
338 navit_float lat,lati,n,ee=1-b*b/(a*a), lng = navit_tan(cart->y/cart->x);
340 lat = navit_tan(cart->z / navit_sqrt((cart->x * cart->x) + (cart->y * cart->y)));
345 n = a / navit_sqrt(1-ee*navit_sin(lat)*navit_sin(lat));
346 lat = navit_atan((cart->z + ee * n * navit_sin(lat)) / navit_sqrt(cart->x * cart->x + cart->y * cart->y));
348 while (fabs(lat - lati) >= 0.000000000000001);
350 geo->lng=lng/M_PI*180;
351 geo->lat=lat/M_PI*180;
356 transform_utm_to_geo(const double UTMEasting, const double UTMNorthing, int ZoneNumber, int NorthernHemisphere, struct coord_geo *geo)
358 //converts UTM coords to lat/long. Equations from USGS Bulletin 1532
359 //East Longitudes are positive, West longitudes are negative.
360 //North latitudes are positive, South latitudes are negative
361 //Lat and Long are in decimal degrees.
362 //Written by Chuck Gantz- chuck.gantz@globalstar.com
365 double k0 = 0.99960000000000004;
367 double eccSquared = 0.0066943799999999998;
368 double eccPrimeSquared;
369 double e1 = (1-sqrt(1-eccSquared))/(1+sqrt(1-eccSquared));
370 double N1, T1, C1, R1, D, M;
372 double mu, phi1, phi1Rad;
374 double rad2deg = 180/M_PI;
376 x = UTMEasting - 500000.0; //remove 500,000 meter offset for longitude
379 if (!NorthernHemisphere) {
380 y -= 10000000.0;//remove 10,000,000 meter offset used for southern hemisphere
383 LongOrigin = (ZoneNumber - 1)*6 - 180 + 3; //+3 puts origin in middle of zone
385 eccPrimeSquared = (eccSquared)/(1-eccSquared);
388 mu = M/(a*(1-eccSquared/4-3*eccSquared*eccSquared/64-5*eccSquared*eccSquared*eccSquared/256));
389 phi1Rad = mu + (3*e1/2-27*e1*e1*e1/32)*sin(2*mu)
390 + (21*e1*e1/16-55*e1*e1*e1*e1/32)*sin(4*mu)
391 +(151*e1*e1*e1/96)*sin(6*mu);
392 phi1 = phi1Rad*rad2deg;
394 N1 = a/sqrt(1-eccSquared*sin(phi1Rad)*sin(phi1Rad));
395 T1 = tan(phi1Rad)*tan(phi1Rad);
396 C1 = eccPrimeSquared*cos(phi1Rad)*cos(phi1Rad);
397 R1 = a*(1-eccSquared)/pow(1-eccSquared*sin(phi1Rad)*sin(phi1Rad), 1.5);
400 Lat = phi1Rad - (N1*tan(phi1Rad)/R1)*(D*D/2-(5+3*T1+10*C1-4*C1*C1-9*eccPrimeSquared)*D*D*D*D/24
401 +(61+90*T1+298*C1+45*T1*T1-252*eccPrimeSquared-3*C1*C1)*D*D*D*D*D*D/720);
404 Long = (D-(1+2*T1+C1)*D*D*D/6+(5-2*C1+28*T1-3*C1*C1+8*eccPrimeSquared+24*T1*T1)
405 *D*D*D*D*D/120)/cos(phi1Rad);
406 Long = LongOrigin + Long * rad2deg;
413 transform_datum(struct coord_geo *from, enum map_datum from_datum, struct coord_geo *to, enum map_datum to_datum)
418 transform(struct transformation *t, enum projection pro, struct coord *c, struct point *p, int count, int mindist, int width, int *width_return)
423 int xc, yc, zc=0, xco=0, yco=0, zco=0;
426 int visible, visibleo=-1;
428 dbg(1,"count=%d\n", count);
429 for (i=0; i < count; i++) {
434 transform_to_geo(pro, &c[i], &g);
435 transform_from_geo(t->pro, &g, &c1);
439 if (i != 0 && i != count-1 && mindist) {
440 if (xc > c[k].x-mindist && xc < c[k].x+mindist && yc > c[k].y-mindist && yc < c[k].y+mindist &&
441 (c[i+1].x != c[0].x || c[i+1].y != c[0].y))
447 // 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);
448 // ret=coord_rect_contains(&t->r, c);
451 xc >>= t->scale_shift;
452 yc >>= t->scale_shift;
456 xcn=xc*t->m00+yc*t->m01+HOG(*t)*t->m02;
457 ycn=xc*t->m10+yc*t->m11+HOG(*t)*t->m12;
460 zc=(xc*t->m20+yc*t->m21+HOG(*t)*t->m22);
462 zc+=t->offz << POST_SHIFT;
463 dbg(1,"zc=%d\n", zc);
464 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);
466 visible=(zc < zlimit ? 0:1);
467 dbg(1,"visible=%d old %d\n", visible, visibleo);
468 if (visible != visibleo && visibleo != -1) {
469 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);
471 xcn=xcn+(long long)(xco-xcn)*(zlimit-zc)/(zco-zc);
472 ycn=ycn+(long long)(yco-ycn)*(zlimit-zc)/(zco-zc);
474 dbg(1,"result (%d,%d,%d) * %d / %d\n", xcn,ycn,zc,zlimit-zc,zco-zc);
490 dbg(1,"zc=%d\n", zc);
491 dbg(1,"xcn %d ycn %d\n", xcn, ycn);
492 dbg(1,"%d,%d %d\n",xc,yc,zc);
494 dbg(0,"%d/%d=%d %d/%d=%d\n",xcn,xc,xcn/xc,ycn,yc,ycn/yc);
497 xc=(long long)xcn*t->xscale/zc;
498 yc=(long long)ycn*t->yscale/zc;
504 dbg(1,"%d,%d %d\n",xc,yc,zc);
518 width_return[j]=width*t->wscale/zc;
520 width_return[j]=width;
528 transform_apply_inverse_matrix(struct transformation *t, struct coord_geo_cart *in, struct coord_geo_cart *out)
530 out->x=in->x*t->im00+in->y*t->im01+in->z*t->im02;
531 out->y=in->x*t->im10+in->y*t->im11+in->z*t->im12;
532 out->z=in->x*t->im20+in->y*t->im21+in->z*t->im22;
536 transform_zplane_intersection(struct coord_geo_cart *p1, struct coord_geo_cart *p2, navit_float z, struct coord_geo_cart *result)
538 navit_float dividend=z-p1->z;
539 navit_float divisor=p2->z-p1->z;
543 return 0; /* no intersection */
545 return 3; /* identical planes */
548 result->x=p1->x+q*(p2->x-p1->x);
549 result->y=p1->y+q*(p2->y-p1->y);
551 if (q >= 0 && q <= 1)
552 return 1; /* intersection within [p1,p2] */
553 return 2; /* intersection without [p1,p2] */
557 transform_screen_to_3d(struct transformation *t, struct point *p, navit_float z, struct coord_geo_cart *cg)
560 double offz=t->offz << POST_SHIFT;
563 cg->x=xc*z/t->xscale;
564 cg->y=yc*z/t->yscale;
569 transform_reverse_near_far(struct transformation *t, struct point *p, struct coord *c, int near, int far)
572 dbg(1,"%d,%d\n",p->x,p->y);
574 struct coord_geo_cart nearc,farc,nears,fars,intersection;
575 transform_screen_to_3d(t, p, near, &nearc);
576 transform_screen_to_3d(t, p, far, &farc);
577 transform_apply_inverse_matrix(t, &nearc, &nears);
578 transform_apply_inverse_matrix(t, &farc, &fars);
579 if (transform_zplane_intersection(&nears, &fars, HOG(*t), &intersection) != 1)
587 xc=(xcn*t->im00+ycn*t->im01)*(1 << POST_SHIFT);
588 yc=(xcn*t->im10+ycn*t->im11)*(1 << POST_SHIFT);
590 c->x=xc*(1 << t->scale_shift)+t->map_center.x;
591 c->y=yc*(1 << t->scale_shift)+t->map_center.y;
596 transform_reverse(struct transformation *t, struct point *p, struct coord *c)
598 return transform_reverse_near_far(t, p, c, t->znear, t->zfar);
602 transform_get_projection(struct transformation *this_)
608 transform_set_projection(struct transformation *this_, enum projection pro)
614 min4(int v1,int v2, int v3, int v4)
627 max4(int v1,int v2, int v3, int v4)
639 struct map_selection *
640 transform_get_selection(struct transformation *this_, enum projection pro, int order)
643 struct map_selection *ret,*curri,*curro;
646 ret=map_selection_dup(this_->map_sel);
647 curri=this_->map_sel;
650 if (this_->pro != pro) {
651 transform_to_geo(this_->pro, &curri->u.c_rect.lu, &g);
652 transform_from_geo(pro, &g, &curro->u.c_rect.lu);
653 dbg(1,"%f,%f", g.lat, g.lng);
654 transform_to_geo(this_->pro, &curri->u.c_rect.rl, &g);
655 transform_from_geo(pro, &g, &curro->u.c_rect.rl);
656 dbg(1,": - %f,%f\n", g.lat, g.lng);
658 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);
661 curro->u.c_rect.lu.x-=500;
662 curro->u.c_rect.lu.y+=500;
663 curro->u.c_rect.rl.x+=500;
664 curro->u.c_rect.rl.y-=500;
666 curro->range=item_range_all;
674 transform_center(struct transformation *this_)
676 return &this_->map_center;
680 transform_get_center(struct transformation *this_)
682 return &this_->map_center;
686 transform_set_center(struct transformation *this_, struct coord *c)
688 this_->map_center=*c;
693 transform_set_yaw(struct transformation *t,int yaw)
696 transform_setup_matrix(t);
700 transform_get_yaw(struct transformation *this_)
706 transform_set_pitch(struct transformation *this_,int pitch)
709 transform_setup_matrix(this_);
712 transform_get_pitch(struct transformation *this_)
718 transform_set_roll(struct transformation *this_,int roll)
722 transform_setup_matrix(this_);
724 dbg(0,"not supported\n");
729 transform_get_roll(struct transformation *this_)
739 transform_set_distance(struct transformation *this_,int distance)
741 transform_set_screen_dist(this_, distance);
742 transform_setup_matrix(this_);
746 transform_get_distance(struct transformation *this_)
748 return this_->screen_dist;
752 transform_set_scales(struct transformation *this_, int xscale, int yscale, int wscale)
754 this_->xscale3d=xscale;
755 this_->yscale3d=yscale;
756 this_->wscale3d=wscale;
760 transform_set_screen_selection(struct transformation *t, struct map_selection *sel)
762 map_selection_destroy(t->screen_sel);
763 t->screen_sel=map_selection_dup(sel);
765 t->screen_center.x=(sel->u.p_rect.rl.x-sel->u.p_rect.lu.x)/2;
766 t->screen_center.y=(sel->u.p_rect.rl.y-sel->u.p_rect.lu.y)/2;
767 transform_setup_matrix(t);
772 transform_set_screen_center(struct transformation *t, struct point *p)
779 transform_set_size(struct transformation *t, int width, int height)
787 transform_get_size(struct transformation *t, int *width, int *height)
789 struct point_rect *r;
791 r=&t->screen_sel->u.p_rect;
792 *width=r->rl.x-r->lu.x;
793 *height=r->rl.y-r->lu.y;
798 transform_setup(struct transformation *t, struct pcoord *c, int scale, int yaw)
801 t->map_center.x=c->x;
802 t->map_center.y=c->y;
804 transform_set_yaw(t, yaw);
810 transform_setup_source_rect_limit(struct transformation *t, struct coord *center, int limit)
815 t->r.lu.x=center->x-limit;
816 t->r.rl.x=center->x+limit;
817 t->r.rl.y=center->y-limit;
818 t->r.lu.y=center->y+limit;
823 transform_setup_source_rect(struct transformation *t)
826 struct coord screen[4];
827 struct point screen_pnt[4];
828 struct point_rect *pr;
829 struct map_selection *ms,*msm,*next,**msm_last;
837 msm_last=&t->map_sel;
840 msm=g_new0(struct map_selection, 1);
843 screen_pnt[0].x=pr->lu.x; /* left upper */
844 screen_pnt[0].y=pr->lu.y;
845 screen_pnt[1].x=pr->rl.x; /* right upper */
846 screen_pnt[1].y=pr->lu.y;
847 screen_pnt[2].x=pr->rl.x; /* right lower */
848 screen_pnt[2].y=pr->rl.y;
849 screen_pnt[3].x=pr->lu.x; /* left lower */
850 screen_pnt[3].y=pr->rl.y;
852 struct coord_geo_cart tmp,cg[8];
855 unsigned char edgenodes[]={
868 for (i = 0 ; i < 8 ; i++) {
869 transform_screen_to_3d(t, &screen_pnt[i%4], (i >= 4 ? t->zfar:t->znear), &tmp);
870 transform_apply_inverse_matrix(t, &tmp, &cg[i]);
872 msm->u.c_rect.lu.x=0;
873 msm->u.c_rect.lu.y=0;
874 msm->u.c_rect.rl.x=0;
875 msm->u.c_rect.rl.y=0;
876 for (i = 0 ; i < 12 ; i++) {
877 if (transform_zplane_intersection(&cg[edgenodes[i*2]], &cg[edgenodes[i*2+1]], HOG(*t), &tmp) == 1) {
878 c.x=tmp.x*(1 << t->scale_shift)+t->map_center.x;
879 c.y=tmp.y*(1 << t->scale_shift)+t->map_center.y;
880 dbg(1,"intersection with edge %d at 0x%x,0x%x\n",i,c.x,c.y);
882 coord_rect_extend(&msm->u.c_rect, &c);
888 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);
892 for (i = 0 ; i < 4 ; i++) {
893 transform_reverse(t, &screen_pnt[i], &screen[i]);
894 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);
896 msm->u.c_rect.lu.x=min4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
897 msm->u.c_rect.rl.x=max4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
898 msm->u.c_rect.rl.y=min4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
899 msm->u.c_rect.lu.y=max4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
901 dbg(1,"%dx%d\n", msm->u.c_rect.rl.x-msm->u.c_rect.lu.x,
902 msm->u.c_rect.lu.y-msm->u.c_rect.rl.y);
910 transform_get_scale(struct transformation *t)
912 return (int)(t->scale*16);
916 transform_set_scale(struct transformation *t, long scale)
919 transform_setup_matrix(t);
924 transform_get_order(struct transformation *t)
926 dbg(1,"order %d\n", t->order);
932 #define TWOPI (M_PI*2)
933 #define GC2RAD(c) ((c) * TWOPI/(1<<24))
934 #define minf(a,b) ((a) < (b) ? (a) : (b))
937 transform_distance_garmin(struct coord *c1, struct coord *c2)
940 static const int earth_radius = 6371*1000; //m change accordingly
941 // static const int earth_radius = 3960; //miles
944 navit_float lat1 = GC2RAD(c1->y);
945 navit_float long1 = GC2RAD(c1->x);
948 navit_float lat2 = GC2RAD(c2->y);
949 navit_float long2 = GC2RAD(c2->x);
952 navit_float dlong = long2-long1;
953 navit_float dlat = lat2-lat1;
955 navit_float sinlat = navit_sin(dlat/2);
956 navit_float sinlong = navit_sin(dlong/2);
958 navit_float a=(sinlat*sinlat)+navit_cos(lat1)*navit_cos(lat2)*(sinlong*sinlong);
959 navit_float c=2*navit_asin(minf(1,navit_sqrt(a)));
961 return round(earth_radius*c);
963 return earth_radius*c;
966 #define GMETER 2.3887499999999999
970 return navit_sqrt(dx*dx+dy*dy)*GMETER;
976 transform_scale(int y)
982 transform_to_geo(projection_mg, &c, &g);
983 return 1/navit_cos(g.lat/180*M_PI);
988 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};
990 static int tab_int_step = 0x20000;
991 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};
993 int transform_int_scale(int y)
995 int i,size = sizeof(tab_int_scale)/sizeof(int);
1000 return tab_int_scale[i]+((tab_int_scale[i+1]-tab_int_scale[i])*(y-i*tab_int_step))/tab_int_step;
1001 return tab_int_scale[size-1];
1006 transform_distance(enum projection pro, struct coord *c1, struct coord *c2)
1008 if (pro == projection_mg) {
1010 double dx,dy,scale=transform_scale((c1->y+c2->y)/2);
1013 return sqrt(dx*dx+dy*dy)/scale;
1015 int dx,dy,f,scale=transform_int_scale((c1->y+c2->y)/2);
1022 while (dx > 20000 || dy > 20000) {
1028 return dx*10000/scale;
1030 return dy*10000/scale;
1034 return dx*10000/scale;
1035 return dx*tab_sqrt[f]/scale;
1039 return dy*10000/scale;
1040 return dy*tab_sqrt[f]/scale;
1043 } else if (pro == projection_garmin) {
1044 return transform_distance_garmin(c1, c2);
1046 dbg(0,"Unknown projection: %d\n", pro);
1052 transform_project(enum projection pro, struct coord *c, int distance, int angle, struct coord *res)
1057 scale=transform_scale(c->y);
1058 res->x=c->x+distance*sin(angle*M_PI/180)*scale;
1059 res->y=c->y+distance*cos(angle*M_PI/180)*scale;
1062 dbg(0,"Unsupported projection: %d\n", pro);
1070 transform_polyline_length(enum projection pro, struct coord *c, int count)
1075 for (i = 0 ; i < count-1 ; i++)
1076 ret+=transform_distance(pro, &c[i], &c[i+1]);
1081 transform_distance_sq(struct coord *c1, struct coord *c2)
1086 if (dx > 32767 || dy > 32767 || dx < -32767 || dy < -32767)
1093 transform_distance_sq_float(struct coord *c1, struct coord *c2)
1097 return (navit_float)dx*dx+dy*dy;
1101 transform_distance_sq_pc(struct pcoord *c1, struct pcoord *c2)
1104 p1.x = c1->x; p1.y = c1->y;
1105 p2.x = c2->x; p2.y = c2->y;
1106 return transform_distance_sq(&p1, &p2);
1110 transform_distance_line_sq(struct coord *l0, struct coord *l1, struct coord *ref, struct coord *lpnt)
1126 return transform_distance_sq(l0, ref);
1132 return transform_distance_sq(l1, ref);
1134 while (c1 > climit || c2 > climit) {
1142 return transform_distance_sq(&l, ref);
1146 transform_distance_line_sq_float(struct coord *l0, struct coord *l1, struct coord *ref, struct coord *lpnt)
1148 navit_float vx,vy,wx,wy;
1161 return transform_distance_sq_float(l0, ref);
1167 return transform_distance_sq_float(l1, ref);
1173 return transform_distance_sq_float(&l, ref);
1177 transform_distance_polyline_sq(struct coord *c, int count, struct coord *ref, struct coord *lpnt, int *pos)
1185 dist=transform_distance_line_sq(&c[0], &c[1], ref, lpnt);
1186 for (i=2 ; i < count ; i++) {
1187 distn=transform_distance_line_sq(&c[i-1], &c[i], ref, &lp);
1200 transform_douglas_peucker(struct coord *in, int count, int dist_sq, struct coord *out)
1203 int i,d,dmax=0, idx=0;
1204 for (i = 1; i < count-2 ; i++) {
1205 d=transform_distance_line_sq(&in[0], &in[count-1], &in[i], NULL);
1211 if (dmax > dist_sq) {
1212 ret=transform_douglas_peucker(in, idx, dist_sq, out)-1;
1213 ret+=transform_douglas_peucker(in+idx, count-idx, dist_sq, out+ret);
1218 out[ret++]=in[count-1];
1224 transform_douglas_peucker_float(struct coord *in, int count, navit_float dist_sq, struct coord *out)
1228 navit_float d,dmax=0;
1229 for (i = 1; i < count-2 ; i++) {
1230 d=transform_distance_line_sq_float(&in[0], &in[count-1], &in[i], NULL);
1236 if (dmax > dist_sq) {
1237 ret=transform_douglas_peucker_float(in, idx, dist_sq, out)-1;
1238 ret+=transform_douglas_peucker_float(in+idx, count-idx, dist_sq, out+ret);
1243 out[ret++]=in[count-1];
1250 transform_print_deg(double deg)
1252 printf("%2.0f:%2.0f:%2.4f", floor(deg), fmod(deg*60,60), fmod(deg*3600,60));
1256 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};
1259 atan2_int_lookup(int val)
1261 int len=sizeof(tab_atan)/sizeof(int);
1266 if (val < tab_atan[p])
1269 if (val < tab_atan[p+1])
1277 atan2_int(int dx, int dy)
1279 int mul=1,add=0,ret;
1281 return dy < 0 ? 180 : 0;
1284 return dx < 0 ? -90 : 90;
1295 while (dx > 20000 || dy > 20000) {
1300 ret=90-atan2_int_lookup(dy*10000/dx);
1302 ret=atan2_int_lookup(dx*10000/dy);
1309 transform_get_angle_delta(struct coord *c1, struct coord *c2, int dir)
1319 angle=atan2_int(dx,dy);
1329 transform_within_border(struct transformation *this_, struct point *p, int border)
1331 struct map_selection *ms=this_->screen_sel;
1333 struct point_rect *r=&ms->u.p_rect;
1334 if (p->x >= r->lu.x+border && p->x <= r->rl.x-border &&
1335 p->y >= r->lu.y+border && p->y <= r->rl.y-border)
1343 transform_within_dist_point(struct coord *ref, struct coord *c, int dist)
1345 if (c->x-dist > ref->x)
1347 if (c->x+dist < ref->x)
1349 if (c->y-dist > ref->y)
1351 if (c->y+dist < ref->y)
1353 if ((c->x-ref->x)*(c->x-ref->x) + (c->y-ref->y)*(c->y-ref->y) <= dist*dist)
1359 transform_within_dist_line(struct coord *ref, struct coord *c0, struct coord *c1, int dist)
1365 if (c0->x < c1->x) {
1366 if (c0->x-dist > ref->x)
1368 if (c1->x+dist < ref->x)
1371 if (c1->x-dist > ref->x)
1373 if (c0->x+dist < ref->x)
1376 if (c0->y < c1->y) {
1377 if (c0->y-dist > ref->y)
1379 if (c1->y+dist < ref->y)
1382 if (c1->y-dist > ref->y)
1384 if (c0->y+dist < ref->y)
1394 return transform_within_dist_point(ref, c0, dist);
1397 return transform_within_dist_point(ref, c1, dist);
1399 lc.x=c0->x+vx*n1/n2;
1400 lc.y=c0->y+vy*n1/n2;
1401 return transform_within_dist_point(ref, &lc, dist);
1405 transform_within_dist_polyline(struct coord *ref, struct coord *c, int count, int close, int dist)
1408 for (i = 0 ; i < count-1 ; i++) {
1409 if (transform_within_dist_line(ref,c+i,c+i+1,dist)) {
1414 return (transform_within_dist_line(ref,c,c+count-1,dist));
1419 transform_within_dist_polygon(struct coord *ref, struct coord *c, int count, int dist)
1422 for (i = 0, j = count-1; i < count; j = i++) {
1423 if ((((c[i].y <= ref->y) && ( ref->y < c[j].y )) ||
1424 ((c[j].y <= ref->y) && ( ref->y < c[i].y))) &&
1425 (ref->x < (c[j].x - c[i].x) * (ref->y - c[i].y) / (c[j].y - c[i].y) + c[i].x))
1430 return transform_within_dist_polyline(ref, c, count, dist, 1);
1438 transform_within_dist_item(struct coord *ref, enum item_type type, struct coord *c, int count, int dist)
1440 if (type < type_line)
1441 return transform_within_dist_point(ref, c, dist);
1442 if (type < type_area)
1443 return transform_within_dist_polyline(ref, c, count, 0, dist);
1444 return transform_within_dist_polygon(ref, c, count, dist);
1448 transform_copy(struct transformation *src, struct transformation *dst)
1450 memcpy(dst, src, sizeof(*src));
1454 transform_destroy(struct transformation *t)
1461 Note: there are many mathematically equivalent ways to express these formulas. As usual, not all of them are computationally equivalent.
1463 L = latitude in radians (positive north)
1464 Lo = longitude in radians (positive east)
1465 E = easting (meters)
1466 N = northing (meters)
1471 N = r ln [ tan (pi/4 + L/2) ]
1475 r = radius of the sphere (meters)
1476 ln() is the natural logarithm
1481 N = a * ln ( tan (pi/4 + L/2) * ( (1 - e * sin (L)) / (1 + e * sin (L))) ** (e/2) )
1487 = a ln( tan( ---- + ---) (--------------) )
1493 a = the length of the semi-major axis of the ellipsoid (meters)
1494 e = the first eccentricity of the ellipsoid