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"
37 struct transformation {
38 int yaw; /* Rotation angle */
41 int m00,m01,m10,m11; /* 2d transformation matrix */
43 int m20,m21; /* additional 3d parameters */
48 navit_float im02,im12,im20,im21,im22;
50 navit_float im00,im01,im10,im11; /* inverse 2d transformation matrix */
51 struct map_selection *map_sel;
52 struct map_selection *screen_sel;
53 struct point screen_center;
56 struct coord map_center; /* Center of source rectangle */
58 navit_float scale; /* Scale factor */
67 transform_setup_matrix(struct transformation *t)
71 navit_float yawc=navit_cos(-M_PI*t->yaw/180);
72 navit_float yaws=navit_sin(-M_PI*t->yaw/180);
73 navit_float pitchc=navit_cos(-M_PI*t->pitch/180);
74 navit_float pitchs=navit_sin(-M_PI*t->pitch/180);
76 navit_float rollc=navit_cos(M_PI*t->roll/180);
77 navit_float rolls=navit_sin(M_PI*t->roll/180);
83 dbg(1,"yaw=%d pitch=%d center=0x%x,0x%x\n", t->yaw, t->pitch, t->map_center.x, t->map_center.y);
85 t->order=t->order_base;
98 fac=(1 << POST_SHIFT) * (1 << t->scale_shift) / t->scale;
99 dbg(1,"scale_shift=%d order=%d scale=%f fac=%f\n", t->scale_shift, t->order,t->scale,fac);
102 t->m00=rollc*yawc*fac;
103 t->m01=rollc*yaws*fac;
105 t->m10=(pitchs*rolls*yawc-pitchc*yaws)*(-fac);
106 t->m11=(pitchs*rolls*yaws+pitchc*yawc)*(-fac);
107 t->m12=pitchs*rollc*(-fac);
108 t->m20=(pitchc*rolls*yawc+pitchs*yaws)*fac;
109 t->m21=(pitchc*rolls*yaws-pitchs*yawc)*fac;
110 t->m22=pitchc*rollc*fac;
114 t->m10=(-pitchc*yaws)*(-fac);
115 t->m11=pitchc*yawc*(-fac);
116 t->m20=pitchs*yaws*fac;
117 t->m21=(-pitchs*yawc)*fac;
122 t->offx=t->screen_center.x;
123 t->offy=t->screen_center.y;
126 t->offz=t->screen_dist;
130 det=(navit_float)t->m00*(navit_float)t->m11*(navit_float)t->m22+
131 (navit_float)t->m01*(navit_float)t->m12*(navit_float)t->m20+
132 (navit_float)t->m02*(navit_float)t->m10*(navit_float)t->m21-
133 (navit_float)t->m02*(navit_float)t->m11*(navit_float)t->m20-
134 (navit_float)t->m01*(navit_float)t->m10*(navit_float)t->m22-
135 (navit_float)t->m00*(navit_float)t->m12*(navit_float)t->m21;
137 t->im00=(t->m11*t->m22-t->m12*t->m21)/det;
138 t->im01=(t->m02*t->m21-t->m01*t->m22)/det;
139 t->im02=(t->m01*t->m12-t->m02*t->m11)/det;
140 t->im10=(t->m12*t->m20-t->m10*t->m22)/det;
141 t->im11=(t->m00*t->m22-t->m02*t->m20)/det;
142 t->im12=(t->m02*t->m10-t->m00*t->m12)/det;
143 t->im20=(t->m10*t->m21-t->m11*t->m20)/det;
144 t->im21=(t->m01*t->m20-t->m00*t->m21)/det;
145 t->im22=(t->m00*t->m11-t->m01*t->m10)/det;
147 det=((navit_float)t->m00*(navit_float)t->m11-(navit_float)t->m01*(navit_float)t->m10);
155 struct transformation *
158 struct transformation *this_;
160 this_=g_new0(struct transformation, 1);
161 this_->screen_dist=100;
162 this_->order_base=14;
170 transform_setup_matrix(this_);
177 transform_get_hog(struct transformation *this_)
183 transform_set_hog(struct transformation *this_, int hog)
191 transform_get_hog(struct transformation *this_)
197 transform_set_hog(struct transformation *this_, int hog)
199 dbg(0,"not supported\n");
205 transformation_get_order_base(struct transformation *this_)
207 return this_->order_base;
211 transform_set_order_base(struct transformation *this_, int order_base)
213 this_->order_base=order_base;
217 struct transformation *
218 transform_dup(struct transformation *t)
220 struct transformation *ret=g_new0(struct transformation, 1);
225 static const navit_float gar2geo_units = 360.0/(1<<24);
226 static const navit_float geo2gar_units = 1/(360.0/(1<<24));
229 transform_to_geo(enum projection pro, struct coord *c, struct coord_geo *g)
231 int x,y,northern,zone;
234 g->lng=c->x/6371000.0/M_PI*180;
235 g->lat=navit_atan(exp(c->y/6371000.0))/M_PI*360-90;
237 case projection_garmin:
238 g->lng=c->x*gar2geo_units;
239 g->lat=c->y*gar2geo_units;
250 transform_utm_to_geo(x, y, zone, northern, g);
258 transform_from_geo(enum projection pro, struct coord_geo *g, struct coord *c)
262 c->x=g->lng*6371000.0*M_PI/180;
263 c->y=log(navit_tan(M_PI_4+g->lat*M_PI/360))*6371000.0;
265 case projection_garmin:
266 c->x=g->lng*geo2gar_units;
267 c->y=g->lat*geo2gar_units;
275 transform_from_to(struct coord *cfrom, enum projection from, struct coord *cto, enum projection to)
278 transform_to_geo(from, cfrom, &g);
279 transform_from_geo(to, &g, cto);
283 transform_geo_to_cart(struct coord_geo *geo, navit_float a, navit_float b, struct coord_geo_cart *cart)
285 navit_float n,ee=1-b*b/(a*a);
286 n = a/sqrtf(1-ee*navit_sin(geo->lat)*navit_sin(geo->lat));
287 cart->x=n*navit_cos(geo->lat)*navit_cos(geo->lng);
288 cart->y=n*navit_cos(geo->lat)*navit_sin(geo->lng);
289 cart->z=n*(1-ee)*navit_sin(geo->lat);
293 transform_cart_to_geo(struct coord_geo_cart *cart, navit_float a, navit_float b, struct coord_geo *geo)
295 navit_float lat,lati,n,ee=1-b*b/(a*a), lng = navit_tan(cart->y/cart->x);
297 lat = navit_tan(cart->z / navit_sqrt((cart->x * cart->x) + (cart->y * cart->y)));
302 n = a / navit_sqrt(1-ee*navit_sin(lat)*navit_sin(lat));
303 lat = navit_atan((cart->z + ee * n * navit_sin(lat)) / navit_sqrt(cart->x * cart->x + cart->y * cart->y));
305 while (fabs(lat - lati) >= 0.000000000000001);
307 geo->lng=lng/M_PI*180;
308 geo->lat=lat/M_PI*180;
313 transform_utm_to_geo(const double UTMEasting, const double UTMNorthing, int ZoneNumber, int NorthernHemisphere, struct coord_geo *geo)
315 //converts UTM coords to lat/long. Equations from USGS Bulletin 1532
316 //East Longitudes are positive, West longitudes are negative.
317 //North latitudes are positive, South latitudes are negative
318 //Lat and Long are in decimal degrees.
319 //Written by Chuck Gantz- chuck.gantz@globalstar.com
322 double k0 = 0.99960000000000004;
324 double eccSquared = 0.0066943799999999998;
325 double eccPrimeSquared;
326 double e1 = (1-sqrt(1-eccSquared))/(1+sqrt(1-eccSquared));
327 double N1, T1, C1, R1, D, M;
329 double mu, phi1, phi1Rad;
331 double rad2deg = 180/M_PI;
333 x = UTMEasting - 500000.0; //remove 500,000 meter offset for longitude
336 if (!NorthernHemisphere) {
337 y -= 10000000.0;//remove 10,000,000 meter offset used for southern hemisphere
340 LongOrigin = (ZoneNumber - 1)*6 - 180 + 3; //+3 puts origin in middle of zone
342 eccPrimeSquared = (eccSquared)/(1-eccSquared);
345 mu = M/(a*(1-eccSquared/4-3*eccSquared*eccSquared/64-5*eccSquared*eccSquared*eccSquared/256));
346 phi1Rad = mu + (3*e1/2-27*e1*e1*e1/32)*sin(2*mu)
347 + (21*e1*e1/16-55*e1*e1*e1*e1/32)*sin(4*mu)
348 +(151*e1*e1*e1/96)*sin(6*mu);
349 phi1 = phi1Rad*rad2deg;
351 N1 = a/sqrt(1-eccSquared*sin(phi1Rad)*sin(phi1Rad));
352 T1 = tan(phi1Rad)*tan(phi1Rad);
353 C1 = eccPrimeSquared*cos(phi1Rad)*cos(phi1Rad);
354 R1 = a*(1-eccSquared)/pow(1-eccSquared*sin(phi1Rad)*sin(phi1Rad), 1.5);
357 Lat = phi1Rad - (N1*tan(phi1Rad)/R1)*(D*D/2-(5+3*T1+10*C1-4*C1*C1-9*eccPrimeSquared)*D*D*D*D/24
358 +(61+90*T1+298*C1+45*T1*T1-252*eccPrimeSquared-3*C1*C1)*D*D*D*D*D*D/720);
361 Long = (D-(1+2*T1+C1)*D*D*D/6+(5-2*C1+28*T1-3*C1*C1+8*eccPrimeSquared+24*T1*T1)
362 *D*D*D*D*D/120)/cos(phi1Rad);
363 Long = LongOrigin + Long * rad2deg;
370 transform_datum(struct coord_geo *from, enum map_datum from_datum, struct coord_geo *to, enum map_datum to_datum)
375 transform(struct transformation *t, enum projection pro, struct coord *c, struct point *p, int count, int unique, int width, int *width_return)
380 int xc, yc, zc=0, xco=0, yco=0, zco=0;
383 int visible, visibleo=-1;
385 dbg(1,"count=%d\n", count);
386 for (i=0; i < count; i++) {
391 transform_to_geo(pro, &c[i], &g);
392 transform_from_geo(t->pro, &g, &c1);
398 // 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);
399 // ret=coord_rect_contains(&t->r, c);
402 xc >>= t->scale_shift;
403 yc >>= t->scale_shift;
405 xcn=xc*t->m00+yc*t->m01+t->hog*t->m02;
406 ycn=xc*t->m10+yc*t->m11+t->hog*t->m12;
408 xcn=xc*t->m00+yc*t->m01;
409 ycn=xc*t->m10+yc*t->m11;
414 zc=(xc*t->m20+yc*t->m21+t->hog*t->m22);
416 zc=(xc*t->m20+yc*t->m21);
419 zc+=t->offz << POST_SHIFT;
420 dbg(1,"zc=%d\n", zc);
421 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);
423 visible=(zc < zlimit ? 0:1);
424 dbg(1,"visible=%d old %d\n", visible, visibleo);
425 if (visible != visibleo && visibleo != -1) {
426 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);
428 xcn=xcn+(long long)(xco-xcn)*(zlimit-zc)/(zco-zc);
429 ycn=ycn+(long long)(yco-ycn)*(zlimit-zc)/(zco-zc);
431 dbg(1,"result (%d,%d,%d) * %d / %d\n", xcn,ycn,zc,zlimit-zc,zco-zc);
447 dbg(1,"zc=%d\n", zc);
448 dbg(1,"xcn %d ycn %d\n", xcn, ycn);
449 dbg(1,"%d,%d %d\n",xc,yc,zc);
451 dbg(0,"%d/%d=%d %d/%d=%d\n",xcn,xc,xcn/xc,ycn,yc,ycn/yc);
454 xc=(long long)xcn*t->xyscale/zc;
455 yc=(long long)ycn*t->xyscale/zc;
460 dbg(1,"%d,%d %d\n",xc,yc,zc);
469 dbg(1,"xc=%d yc=%d\n", xc, yc);
470 if (j == 0 || !unique || p[j-1].x != xc || p[j-1].y != yc) {
475 width_return[j]=width*(t->offz << POST_SHIFT)/zc;
477 width_return[j]=width;
486 transform_reverse(struct transformation *t, struct point *p, struct coord *c)
488 double zc,xc,yc,xcn,ycn,q;
489 double offz=t->offz << POST_SHIFT;
495 double f00=xc*t->im00*t->m20;
496 double f01=yc*t->im01*t->m20;
497 double f10=xc*t->im10*t->m21;
498 double f11=yc*t->im11*t->m21;
500 q=(1-f00-f01-t->im02*t->m20-f10-f11-t->im12*t->m21);
503 zc=(offz*((f00+f01+f10+f11))+t->hog*t->m22)/q;
505 q=(1-f00-f01-f10-f11);
508 zc=offz*(f00+f01+f10+f11)/q;
513 xc=xcn*t->im00+ycn*t->im01+zc*t->im02;
514 yc=xcn*t->im10+ycn*t->im11+zc*t->im12;
516 xc=xcn*t->im00+ycn*t->im01;
517 yc=xcn*t->im10+ycn*t->im11;
523 xc=(xcn*t->im00+ycn*t->im01)*(1 << POST_SHIFT);
524 yc=(xcn*t->im10+ycn*t->im11)*(1 << POST_SHIFT);
526 c->x=xc*(1 << t->scale_shift)+t->map_center.x;
527 c->y=yc*(1 << t->scale_shift)+t->map_center.y;
531 transform_get_projection(struct transformation *this_)
537 transform_set_projection(struct transformation *this_, enum projection pro)
543 min4(int v1,int v2, int v3, int v4)
556 max4(int v1,int v2, int v3, int v4)
568 struct map_selection *
569 transform_get_selection(struct transformation *this_, enum projection pro, int order)
572 struct map_selection *ret,*curri,*curro;
575 ret=map_selection_dup(this_->map_sel);
576 curri=this_->map_sel;
579 if (this_->pro != pro) {
580 transform_to_geo(this_->pro, &curri->u.c_rect.lu, &g);
581 transform_from_geo(pro, &g, &curro->u.c_rect.lu);
582 dbg(1,"%f,%f", g.lat, g.lng);
583 transform_to_geo(this_->pro, &curri->u.c_rect.rl, &g);
584 transform_from_geo(pro, &g, &curro->u.c_rect.rl);
585 dbg(1,": - %f,%f\n", g.lat, g.lng);
587 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);
589 curro->range=item_range_all;
597 transform_center(struct transformation *this_)
599 return &this_->map_center;
603 transform_get_center(struct transformation *this_)
605 return &this_->map_center;
609 transform_set_center(struct transformation *this_, struct coord *c)
611 this_->map_center=*c;
616 transform_set_yaw(struct transformation *t,int yaw)
619 transform_setup_matrix(t);
623 transform_get_yaw(struct transformation *this_)
629 transform_set_pitch(struct transformation *this_,int pitch)
632 transform_setup_matrix(this_);
635 transform_get_pitch(struct transformation *this_)
642 transform_set_roll(struct transformation *this_,int roll)
645 transform_setup_matrix(this_);
649 transform_get_roll(struct transformation *this_)
657 transform_set_roll(struct transformation *this_,int roll)
659 dbg(0,"not supported\n");
663 transform_get_roll(struct transformation *this_)
671 transform_set_distance(struct transformation *this_,int distance)
673 this_->screen_dist=distance;
674 transform_setup_matrix(this_);
678 transform_get_distance(struct transformation *this_)
680 return this_->screen_dist;
684 transform_set_screen_selection(struct transformation *t, struct map_selection *sel)
686 map_selection_destroy(t->screen_sel);
687 t->screen_sel=map_selection_dup(sel);
689 t->screen_center.x=(sel->u.p_rect.rl.x-sel->u.p_rect.lu.x)/2;
690 t->screen_center.y=(sel->u.p_rect.rl.y-sel->u.p_rect.lu.y)/2;
691 transform_setup_matrix(t);
696 transform_set_screen_center(struct transformation *t, struct point *p)
703 transform_set_size(struct transformation *t, int width, int height)
711 transform_get_size(struct transformation *t, int *width, int *height)
713 struct point_rect *r;
715 r=&t->screen_sel->u.p_rect;
716 *width=r->rl.x-r->lu.x;
717 *height=r->rl.y-r->lu.y;
722 transform_setup(struct transformation *t, struct pcoord *c, int scale, int yaw)
725 t->map_center.x=c->x;
726 t->map_center.y=c->y;
728 transform_set_yaw(t, yaw);
734 transform_setup_source_rect_limit(struct transformation *t, struct coord *center, int limit)
739 t->r.lu.x=center->x-limit;
740 t->r.rl.x=center->x+limit;
741 t->r.rl.y=center->y-limit;
742 t->r.lu.y=center->y+limit;
747 transform_setup_source_rect(struct transformation *t)
750 struct coord screen[4];
751 struct point screen_pnt[4];
752 struct point_rect *pr;
753 struct map_selection *ms,*msm,*next,**msm_last;
761 msm_last=&t->map_sel;
764 msm=g_new0(struct map_selection, 1);
767 screen_pnt[0].x=pr->lu.x;
768 screen_pnt[0].y=pr->lu.y;
769 screen_pnt[1].x=pr->rl.x;
770 screen_pnt[1].y=pr->lu.y;
771 screen_pnt[2].x=pr->lu.x;
772 screen_pnt[2].y=pr->rl.y;
773 screen_pnt[3].x=pr->rl.x;
774 screen_pnt[3].y=pr->rl.y;
775 for (i = 0 ; i < 4 ; i++) {
776 transform_reverse(t, &screen_pnt[i], &screen[i]);
777 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);
779 msm->u.c_rect.lu.x=min4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
780 msm->u.c_rect.rl.x=max4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
781 msm->u.c_rect.rl.y=min4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
782 msm->u.c_rect.lu.y=max4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
783 dbg(1,"%dx%d\n", msm->u.c_rect.rl.x-msm->u.c_rect.lu.x,
784 msm->u.c_rect.lu.y-msm->u.c_rect.rl.y);
792 transform_get_scale(struct transformation *t)
794 return (int)(t->scale*16);
798 transform_set_scale(struct transformation *t, long scale)
801 transform_setup_matrix(t);
806 transform_get_order(struct transformation *t)
808 dbg(1,"order %d\n", t->order);
814 #define TWOPI (M_PI*2)
815 #define GC2RAD(c) ((c) * TWOPI/(1<<24))
816 #define minf(a,b) ((a) < (b) ? (a) : (b))
819 transform_distance_garmin(struct coord *c1, struct coord *c2)
822 static const int earth_radius = 6371*1000; //m change accordingly
823 // static const int earth_radius = 3960; //miles
826 navit_float lat1 = GC2RAD(c1->y);
827 navit_float long1 = GC2RAD(c1->x);
830 navit_float lat2 = GC2RAD(c2->y);
831 navit_float long2 = GC2RAD(c2->x);
834 navit_float dlong = long2-long1;
835 navit_float dlat = lat2-lat1;
837 navit_float sinlat = navit_sin(dlat/2);
838 navit_float sinlong = navit_sin(dlong/2);
840 navit_float a=(sinlat*sinlat)+navit_cos(lat1)*navit_cos(lat2)*(sinlong*sinlong);
841 navit_float c=2*navit_asin(minf(1,navit_sqrt(a)));
843 return round(earth_radius*c);
845 return earth_radius*c;
848 #define GMETER 2.3887499999999999
852 return navit_sqrt(dx*dx+dy*dy)*GMETER;
858 transform_scale(int y)
864 transform_to_geo(projection_mg, &c, &g);
865 return 1/navit_cos(g.lat/180*M_PI);
870 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};
872 static int tab_int_step = 0x20000;
873 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};
875 int transform_int_scale(int y)
877 int a=tab_int_step,i,size = sizeof(tab_int_scale)/sizeof(int);
882 return tab_int_scale[i]+((tab_int_scale[i+1]-tab_int_scale[i])*(y-i*tab_int_step))/tab_int_step;
883 return tab_int_scale[size-1];
888 transform_distance(enum projection pro, struct coord *c1, struct coord *c2)
890 if (pro == projection_mg) {
892 double dx,dy,scale=transform_scale((c1->y+c2->y)/2);
895 return sqrt(dx*dx+dy*dy)/scale;
897 int dx,dy,f,scale=transform_int_scale((c1->y+c2->y)/2);
904 while (dx > 20000 || dy > 20000) {
910 return dx*10000/scale;
912 return dy*10000/scale;
916 return dx*10000/scale;
917 return dx*tab_sqrt[f]/scale;
921 return dy*10000/scale;
922 return dy*tab_sqrt[f]/scale;
925 } else if (pro == projection_garmin) {
926 return transform_distance_garmin(c1, c2);
928 dbg(0,"Unknown projection: %d\n", pro);
934 transform_project(enum projection pro, struct coord *c, int distance, int angle, struct coord *res)
939 scale=transform_scale(c->y);
940 res->x=c->x+distance*sin(angle*M_PI/180)*scale;
941 res->y=c->y+distance*cos(angle*M_PI/180)*scale;
944 dbg(0,"Unsupported projection: %d\n", pro);
952 transform_polyline_length(enum projection pro, struct coord *c, int count)
957 for (i = 0 ; i < count-1 ; i++)
958 ret+=transform_distance(pro, &c[i], &c[i+1]);
963 transform_distance_sq(struct coord *c1, struct coord *c2)
968 if (dx > 32767 || dy > 32767 || dx < -32767 || dy < -32767)
975 transform_distance_sq_pc(struct pcoord *c1, struct pcoord *c2)
978 p1.x = c1->x; p1.y = c1->y;
979 p2.x = c2->x; p2.y = c2->y;
980 return transform_distance_sq(&p1, &p2);
984 transform_distance_line_sq(struct coord *l0, struct coord *l1, struct coord *ref, struct coord *lpnt)
1000 return transform_distance_sq(l0, ref);
1006 return transform_distance_sq(l1, ref);
1008 while (c1 > climit || c2 > climit) {
1016 return transform_distance_sq(&l, ref);
1020 transform_distance_polyline_sq(struct coord *c, int count, struct coord *ref, struct coord *lpnt, int *pos)
1028 dist=transform_distance_line_sq(&c[0], &c[1], ref, lpnt);
1029 for (i=2 ; i < count ; i++) {
1030 distn=transform_distance_line_sq(&c[i-1], &c[i], ref, &lp);
1044 transform_print_deg(double deg)
1046 printf("%2.0f:%2.0f:%2.4f", floor(deg), fmod(deg*60,60), fmod(deg*3600,60));
1050 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};
1053 atan2_int_lookup(int val)
1055 int len=sizeof(tab_atan)/sizeof(int);
1060 if (val < tab_atan[p])
1063 if (val < tab_atan[p+1])
1071 atan2_int(int dx, int dy)
1073 int f,mul=1,add=0,ret;
1075 return dy < 0 ? 180 : 0;
1078 return dx < 0 ? -90 : 90;
1089 while (dx > 20000 || dy > 20000) {
1094 ret=90-atan2_int_lookup(dy*10000/dx);
1096 ret=atan2_int_lookup(dx*10000/dy);
1103 transform_get_angle_delta(struct coord *c1, struct coord *c2, int dir)
1113 angle=atan2_int(dx,dy);
1123 transform_within_border(struct transformation *this_, struct point *p, int border)
1125 struct map_selection *ms=this_->screen_sel;
1127 struct point_rect *r=&ms->u.p_rect;
1128 if (p->x >= r->lu.x+border && p->x <= r->rl.x-border &&
1129 p->y >= r->lu.y+border && p->y <= r->rl.y-border)
1137 transform_within_dist_point(struct coord *ref, struct coord *c, int dist)
1139 if (c->x-dist > ref->x)
1141 if (c->x+dist < ref->x)
1143 if (c->y-dist > ref->y)
1145 if (c->y+dist < ref->y)
1147 if ((c->x-ref->x)*(c->x-ref->x) + (c->y-ref->y)*(c->y-ref->y) <= dist*dist)
1153 transform_within_dist_line(struct coord *ref, struct coord *c0, struct coord *c1, int dist)
1159 if (c0->x < c1->x) {
1160 if (c0->x-dist > ref->x)
1162 if (c1->x+dist < ref->x)
1165 if (c1->x-dist > ref->x)
1167 if (c0->x+dist < ref->x)
1170 if (c0->y < c1->y) {
1171 if (c0->y-dist > ref->y)
1173 if (c1->y+dist < ref->y)
1176 if (c1->y-dist > ref->y)
1178 if (c0->y+dist < ref->y)
1188 return transform_within_dist_point(ref, c0, dist);
1191 return transform_within_dist_point(ref, c1, dist);
1193 lc.x=c0->x+vx*n1/n2;
1194 lc.y=c0->y+vy*n1/n2;
1195 return transform_within_dist_point(ref, &lc, dist);
1199 transform_within_dist_polyline(struct coord *ref, struct coord *c, int count, int close, int dist)
1202 for (i = 0 ; i < count-1 ; i++) {
1203 if (transform_within_dist_line(ref,c+i,c+i+1,dist)) {
1208 return (transform_within_dist_line(ref,c,c+count-1,dist));
1213 transform_within_dist_polygon(struct coord *ref, struct coord *c, int count, int dist)
1216 for (i = 0, j = count-1; i < count; j = i++) {
1217 if ((((c[i].y <= ref->y) && ( ref->y < c[j].y )) ||
1218 ((c[j].y <= ref->y) && ( ref->y < c[i].y))) &&
1219 (ref->x < (c[j].x - c[i].x) * (ref->y - c[i].y) / (c[j].y - c[i].y) + c[i].x))
1224 return transform_within_dist_polyline(ref, c, count, dist, 1);
1232 transform_within_dist_item(struct coord *ref, enum item_type type, struct coord *c, int count, int dist)
1234 if (type < type_line)
1235 return transform_within_dist_point(ref, c, dist);
1236 if (type < type_area)
1237 return transform_within_dist_polyline(ref, c, count, 0, dist);
1238 return transform_within_dist_polygon(ref, c, count, dist);
1242 transform_destroy(struct transformation *t)
1249 Note: there are many mathematically equivalent ways to express these formulas. As usual, not all of them are computationally equivalent.
1251 L = latitude in radians (positive north)
1252 Lo = longitude in radians (positive east)
1253 E = easting (meters)
1254 N = northing (meters)
1259 N = r ln [ tan (pi/4 + L/2) ]
1263 r = radius of the sphere (meters)
1264 ln() is the natural logarithm
1269 N = a * ln ( tan (pi/4 + L/2) * ( (1 - e * sin (L)) / (1 + e * sin (L))) ** (e/2) )
1275 = a ln( tan( ---- + ---) (--------------) )
1281 a = the length of the semi-major axis of the ellipsoid (meters)
1282 e = the first eccentricity of the ellipsoid