Add license files and headers
[navit-package] / navit / transform.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
4  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <assert.h>
21 #include <stdio.h>
22 #include <math.h>
23 #include <limits.h>
24 #include <glib.h>
25 #include <string.h>
26 #include "config.h"
27 #include "coord.h"
28 #include "debug.h"
29 #include "map.h"
30 #include "transform.h"
31 #include "projection.h"
32 #include "point.h"
33
34 struct transformation {
35         long scale;             /* Scale factor */
36         int angle;              /* Rotation angle */
37         double cos_val,sin_val; /* cos and sin of rotation angle */
38         enum projection pro;
39         struct map_selection *map_sel;
40         struct map_selection *screen_sel;
41         struct point screen_center;
42         struct coord map_center;        /* Center of source rectangle */
43 };
44
45 struct transformation *
46 transform_new(void)
47 {
48         struct transformation *this_;
49
50         this_=g_new0(struct transformation, 1);
51
52         return this_;
53 }
54
55 static const double gar2geo_units = 360.0/(1<<24);
56 static const double geo2gar_units = 1/(360.0/(1<<24));
57
58 void
59 transform_to_geo(enum projection pro, struct coord *c, struct coord_geo *g)
60 {
61         switch (pro) {
62         case projection_mg:
63                 g->lng=c->x/6371000.0/M_PI*180;
64                 g->lat=atan(exp(c->y/6371000.0))/M_PI*360-90;
65                 break;
66         case projection_garmin:
67                 g->lng=c->x*gar2geo_units;
68                 g->lat=c->y*gar2geo_units;
69                 break;
70         default:
71                 break;
72         }
73 }
74
75 void
76 transform_from_geo(enum projection pro, struct coord_geo *g, struct coord *c)
77 {
78         switch (pro) {
79         case projection_mg:
80                 c->x=g->lng*6371000.0*M_PI/180;
81                 c->y=log(tan(M_PI_4+g->lat*M_PI/360))*6371000.0;
82                 break;
83         case projection_garmin:
84                 c->x=g->lng*geo2gar_units;
85                 c->y=g->lat*geo2gar_units;
86                 break;
87         default:
88                 break;
89         }
90 }
91
92 void
93 transform_from_to(struct coord *cfrom, enum projection from, struct coord *cto, enum projection to)
94 {
95         struct coord_geo g;
96         transform_to_geo(from, cfrom, &g);
97         transform_from_geo(to, &g, cto);
98 }
99
100 void
101 transform_geo_to_cart(struct coord_geo *geo, double a, double b, struct coord_geo_cart *cart)
102 {
103         double n,ee=1-b*b/(a*a);
104         n = a/sqrt(1-ee*sin(geo->lat)*sin(geo->lat));
105         cart->x=n*cos(geo->lat)*cos(geo->lng);
106         cart->y=n*cos(geo->lat)*sin(geo->lng);
107         cart->z=n*(1-ee)*sin(geo->lat);
108 }
109
110 void
111 transform_cart_to_geo(struct coord_geo_cart *cart, double a, double b, struct coord_geo *geo)
112 {
113         double lat,lati,n,ee=1-b*b/(a*a), lng = atan(cart->y/cart->x);
114
115         lat = atan(cart->z / sqrt((cart->x * cart->x) + (cart->y * cart->y)));
116         do
117         {
118                 lati = lat;
119
120                 n = a / sqrt(1-ee*sin(lat)*sin(lat));
121                 lat = atan((cart->z + ee * n * sin(lat)) / sqrt(cart->x * cart->x + cart->y * cart->y));
122         }
123         while (fabs(lat - lati) >= 0.000000000000001);
124
125         geo->lng=lng/M_PI*180;
126         geo->lat=lat/M_PI*180;
127 }
128
129
130 void
131 transform_datum(struct coord_geo *from, enum map_datum from_datum, struct coord_geo *to, enum map_datum to_datum)
132 {
133 }
134
135 int
136 transform(struct transformation *t, enum projection pro, struct coord *c, struct point *p, int count, int unique)
137 {
138         struct coord c1;
139         int xcn, ycn; 
140         struct coord_geo g;
141 #ifdef AVOID_FLOAT
142         int xc,yc;
143 #else
144         double xc,yc;
145 #endif
146         int i,j = 0;
147         for (i=0; i < count; i++) {
148                 if (pro == t->pro) {
149                         xc=c[i].x;
150                         yc=c[i].y;
151                 } else {
152                         transform_to_geo(pro, &c[i], &g);
153                         transform_from_geo(t->pro, &g, &c1);
154                         xc=c1.x;
155                         yc=c1.y;
156                 }
157 //              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);
158 //              ret=coord_rect_contains(&t->r, c);
159                 xc-=t->map_center.x;
160                 yc-=t->map_center.y;
161                 yc=-yc;
162                 if (t->angle) {
163                         xcn=xc*t->cos_val+yc*t->sin_val;
164                         ycn=-xc*t->sin_val+yc*t->cos_val;
165                         xc=xcn;
166                         yc=ycn;
167                 }
168                 xc=xc*16;
169                 yc=yc*16;
170 #ifndef AVOID_FLOAT
171                 if (t->scale!=1) {
172                         xc=xc/(double)(t->scale);
173                         yc=yc/(double)(t->scale);
174                 }
175 #else
176                 if (t->scale!=1) {
177                         xc=xc/t->scale;
178                         yc=yc/t->scale;
179                 }
180 #endif
181                 xc+=t->screen_center.x;
182                 yc+=t->screen_center.y;
183                 if (xc < -0x8000)
184                         xc=-0x8000;
185                 if (xc > 0x7fff) {
186                         xc=0x7fff;
187                 }
188                 if (yc < -0x8000)
189                         yc=-0x8000;
190                 if (yc > 0x7fff)
191                         yc=0x7fff;
192                 if (j == 0 || !unique || p[j-1].x != xc || p[j-1].y != yc) {
193                         p[j].x=xc;
194                         p[j].y=yc;
195                         j++;
196                 }
197         }
198         return j;
199 }
200
201 void
202 transform_reverse(struct transformation *t, struct point *p, struct coord *c)
203 {
204         int xc,yc;
205         xc=p->x;
206         yc=p->y;
207         xc-=t->screen_center.x;
208         yc-=t->screen_center.y;
209         xc=xc*t->scale/16;
210         yc=-yc*t->scale/16;
211         if (t->angle) {
212                 int xcn, ycn; 
213                 xcn=xc*t->cos_val+yc*t->sin_val;
214                 ycn=-xc*t->sin_val+yc*t->cos_val;
215                 xc=xcn;
216                 yc=ycn;
217         }
218         c->x=t->map_center.x+xc;
219         c->y=t->map_center.y+yc;
220 }
221
222 enum projection
223 transform_get_projection(struct transformation *this_)
224 {
225         return this_->pro;
226 }
227
228 void
229 transform_set_projection(struct transformation *this_, enum projection pro)
230 {
231         this_->pro=pro;
232 }
233
234 static int
235 min4(int v1,int v2, int v3, int v4)
236 {
237         int res=v1;
238         if (v2 < res)
239                 res=v2;
240         if (v3 < res)
241                 res=v3;
242         if (v4 < res)
243                 res=v4;
244         return res;
245 }
246
247 static int
248 max4(int v1,int v2, int v3, int v4)
249 {
250         int res=v1;
251         if (v2 > res)
252                 res=v2;
253         if (v3 > res)
254                 res=v3;
255         if (v4 > res)
256                 res=v4;
257         return res;
258 }
259
260 struct map_selection *
261 transform_get_selection(struct transformation *this_, enum projection pro, int order)
262 {
263
264         struct map_selection *ret,*curri,*curro;
265         struct coord_geo g;
266         int i;
267         
268         ret=map_selection_dup(this_->map_sel);
269         curri=this_->map_sel;
270         curro=ret;
271         while (curri) {
272                 if (this_->pro != pro) {
273                         transform_to_geo(this_->pro, &curri->u.c_rect.lu, &g);
274                         transform_from_geo(pro, &g, &curro->u.c_rect.lu);
275                         dbg(1,"%f,%f", g.lat, g.lng);
276                         transform_to_geo(this_->pro, &curri->u.c_rect.rl, &g);
277                         transform_from_geo(pro, &g, &curro->u.c_rect.rl);
278                         dbg(1,": - %f,%f\n", g.lat, g.lng);
279                 }
280                 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);
281                 for (i = 0 ; i < layer_end ; i++) 
282                         curro->order[i]+=order;
283                 curri=curri->next;
284                 curro=curro->next;
285         }
286         return ret;
287 }
288
289 struct coord *
290 transform_center(struct transformation *this_)
291 {
292         return &this_->map_center;
293 }
294
295 void
296 transform_set_angle(struct transformation *t,int angle)
297 {
298         t->angle=angle;
299         t->cos_val=cos(M_PI*t->angle/180);
300         t->sin_val=sin(M_PI*t->angle/180);
301 }
302
303 int
304 transform_get_angle(struct transformation *this_,int angle)
305 {
306         return this_->angle;
307 }
308
309 void
310 transform_set_screen_selection(struct transformation *t, struct map_selection *sel)
311 {
312         map_selection_destroy(t->screen_sel);
313         t->screen_sel=map_selection_dup(sel);
314         if (sel) {
315                 t->screen_center.x=(sel->u.p_rect.rl.x-sel->u.p_rect.lu.x)/2;
316                 t->screen_center.y=(sel->u.p_rect.rl.y-sel->u.p_rect.lu.y)/2;
317         }
318 }
319
320 #if 0
321 void
322 transform_set_size(struct transformation *t, int width, int height)
323 {
324         t->width=width;
325         t->height=height;
326 }
327 #endif
328
329 void
330 transform_get_size(struct transformation *t, int *width, int *height)
331 {
332         struct point_rect *r;
333         if (t->screen_sel) {
334                 r=&t->screen_sel->u.p_rect;
335                 *width=r->rl.x-r->lu.x;
336                 *height=r->rl.y-r->lu.y;
337         }
338 }
339
340 void
341 transform_setup(struct transformation *t, struct pcoord *c, int scale, int angle)
342 {
343         t->pro=c->pro;
344         t->map_center.x=c->x;
345         t->map_center.y=c->y;
346         t->scale=scale;
347         transform_set_angle(t, angle);
348 }
349
350 #if 0
351
352 void
353 transform_setup_source_rect_limit(struct transformation *t, struct coord *center, int limit)
354 {
355         t->center=*center;
356         t->scale=1;
357         t->angle=0;
358         t->r.lu.x=center->x-limit;
359         t->r.rl.x=center->x+limit;
360         t->r.rl.y=center->y-limit;
361         t->r.lu.y=center->y+limit;
362 }
363 #endif
364
365 void
366 transform_setup_source_rect(struct transformation *t)
367 {
368         int i;
369         struct coord screen[4];
370         struct point screen_pnt[4];
371         struct point_rect *pr;
372         struct map_selection *ms,*msm,*next,**msm_last;
373         ms=t->map_sel;
374         while (ms) {
375                 next=ms->next;
376                 g_free(ms);
377                 ms=next;
378         }
379         t->map_sel=NULL;
380         msm_last=&t->map_sel;
381         ms=t->screen_sel;
382         while (ms) {
383                 msm=g_new0(struct map_selection, 1);
384                 *msm=*ms;
385                 pr=&ms->u.p_rect;
386                 screen_pnt[0].x=pr->lu.x;
387                 screen_pnt[0].y=pr->lu.y;
388                 screen_pnt[1].x=pr->rl.x;
389                 screen_pnt[1].y=pr->lu.y;
390                 screen_pnt[2].x=pr->lu.x;
391                 screen_pnt[2].y=pr->rl.y;
392                 screen_pnt[3].x=pr->rl.x;
393                 screen_pnt[3].y=pr->rl.y;
394                 for (i = 0 ; i < 4 ; i++) {
395                         transform_reverse(t, &screen_pnt[i], &screen[i]);
396                 }
397                 msm->u.c_rect.lu.x=min4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
398                 msm->u.c_rect.rl.x=max4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
399                 msm->u.c_rect.rl.y=min4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
400                 msm->u.c_rect.lu.y=max4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
401                 *msm_last=msm;
402                 msm_last=&msm->next;
403                 ms=ms->next;
404         }
405 }
406
407 long
408 transform_get_scale(struct transformation *t)
409 {
410         return t->scale;
411 }
412
413 void
414 transform_set_scale(struct transformation *t, long scale)
415 {
416         t->scale=scale;
417 }
418
419
420 int
421 transform_get_order(struct transformation *t)
422 {
423         int scale=t->scale;
424         int order=0;
425         while (scale > 1) {
426                 order++;
427                 scale>>=1;
428         }
429         order=18-order;
430         if (order < 0)
431                 order=0;
432         return order;
433 }
434
435
436 void
437 transform_geo_text(struct coord_geo *g, char *buffer)
438 {
439         double lng=g->lng;
440         double lat=g->lat;
441         char lng_c='E';
442         char lat_c='N';
443
444         if (lng < 0) {
445                 lng=-lng;
446                 lng_c='W';
447         }
448         if (lat < 0) {
449                 lat=-lat;
450                 lat_c='S';
451         }
452
453         sprintf(buffer,"%02.0f%07.4f%c %03.0f%07.4f%c", floor(lat), fmod(lat*60,60), lat_c, floor(lng), fmod(lng*60,60), lng_c);
454
455 }
456
457 #define TWOPI (M_PI*2)
458 #define GC2RAD(c) ((c) * TWOPI/(1<<24))
459 #define minf(a,b) ((a) < (b) ? (a) : (b))
460
461 static double
462 transform_distance_garmin(struct coord *c1, struct coord *c2)
463 {
464 #ifdef USE_HALVESINE
465         static const int earth_radius = 6371*1000; //m change accordingly
466 // static const int earth_radius  = 3960; //miles
467  
468 //Point 1 cords
469         float lat1  = GC2RAD(c1->y);
470         float long1 = GC2RAD(c1->x);
471
472 //Point 2 cords
473         float lat2  = GC2RAD(c2->y);
474         float long2 = GC2RAD(c2->x);
475
476 //Haversine Formula
477         float dlong = long2-long1;
478         float dlat  = lat2-lat1;
479
480         float sinlat  = sinf(dlat/2);
481         float sinlong = sinf(dlong/2);
482  
483         float a=(sinlat*sinlat)+cosf(lat1)*cosf(lat2)*(sinlong*sinlong);
484         float c=2*asinf(minf(1,sqrt(a)));
485 #ifdef AVOID_FLOAT
486         return round(earth_radius*c);
487 #else
488         return earth_radius*c;
489 #endif
490 #else
491 #define GMETER 2.3887499999999999
492         double dx,dy;
493         dx=c1->x-c2->x;
494         dy=c1->y-c2->y;
495         return sqrt(dx*dx+dy*dy)*GMETER;
496 #undef GMETER
497 #endif
498 }
499
500 double
501 transform_scale(int y)
502 {
503         struct coord c;
504         struct coord_geo g;
505         c.x=0;
506         c.y=y;
507         transform_to_geo(projection_mg, &c, &g);
508         return 1/cos(g.lat/180*M_PI);
509 }
510
511 #ifdef AVOID_FLOAT
512 static int
513 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};
514 #endif
515
516 double
517 transform_distance(enum projection pro, struct coord *c1, struct coord *c2)
518 {
519         if (pro == projection_mg) {
520 #ifndef AVOID_FLOAT 
521         double dx,dy,scale=transform_scale((c1->y+c2->y)/2);
522         dx=c1->x-c2->x;
523         dy=c1->y-c2->y;
524         return sqrt(dx*dx+dy*dy)/scale;
525 #else
526         int dx,dy,f,scale=15539;
527         dx=c1->x-c2->x;
528         dy=c1->y-c2->y;
529         if (dx < 0)
530                 dx=-dx;
531         if (dy < 0)
532                 dy=-dy;
533         while (dx > 20000 || dy > 20000) {
534                 dx/=10;
535                 dy/=10;
536                 scale/=10;
537         }
538         if (! dy)
539                 return dx*10000/scale;
540         if (! dx)
541                 return dy*10000/scale;
542         if (dx > dy) {
543                 f=dx*8/dy-8;
544                 if (f >= 32)
545                         return dx*10000/scale;
546                 return dx*tab_sqrt[f]/scale;
547         } else {
548                 f=dy*8/dx-8;
549                 if (f >= 32)
550                         return dy*10000/scale;
551                 return dy*tab_sqrt[f]/scale;
552         }
553 #endif
554         } else if (pro == projection_garmin) {
555                 return transform_distance_garmin(c1, c2);
556         } else {
557                 printf("Unknown projection: %d\n", pro);
558                 return 0;
559         }
560 }
561
562 int
563 transform_distance_sq(struct coord *c1, struct coord *c2)
564 {
565         int dx=c1->x-c2->x;
566         int dy=c1->y-c2->y;
567
568         if (dx > 32767 || dy > 32767 || dx < -32767 || dy < -32767)
569                 return INT_MAX;
570         else
571                 return dx*dx+dy*dy;
572 }
573
574 int
575 transform_distance_line_sq(struct coord *l0, struct coord *l1, struct coord *ref, struct coord *lpnt)
576 {
577         int vx,vy,wx,wy;
578         int c1,c2;
579         int climit=1000000;
580         struct coord l;
581
582         vx=l1->x-l0->x;
583         vy=l1->y-l0->y;
584         wx=ref->x-l0->x;
585         wy=ref->y-l0->y;
586
587         c1=vx*wx+vy*wy;
588         if ( c1 <= 0 ) {
589                 if (lpnt)
590                         *lpnt=*l0;
591                 return transform_distance_sq(l0, ref);
592         }
593         c2=vx*vx+vy*vy;
594         if ( c2 <= c1 ) {
595                 if (lpnt)
596                         *lpnt=*l1;
597                 return transform_distance_sq(l1, ref);
598         }
599         while (c1 > climit || c2 > climit) {
600                 c1/=256;
601                 c2/=256;
602         }
603         l.x=l0->x+vx*c1/c2;
604         l.y=l0->y+vy*c1/c2;
605         if (lpnt)
606                 *lpnt=l;
607         return transform_distance_sq(&l, ref);
608 }
609
610 int
611 transform_distance_polyline_sq(struct coord *c, int count, struct coord *ref, struct coord *lpnt, int *pos)
612 {
613         int i,dist,distn;
614         struct coord lp;
615         if (count < 2)
616                 return INT_MAX;
617         if (pos)
618                 *pos=0;
619         dist=transform_distance_line_sq(&c[0], &c[1], ref, lpnt);
620         for (i=2 ; i < count ; i++) {
621                 distn=transform_distance_line_sq(&c[i-1], &c[i], ref, &lp);
622                 if (distn < dist) {
623                         dist=distn;
624                         if (lpnt)
625                                 *lpnt=lp;
626                         if (pos)
627                                 *pos=i-1;
628                 }
629         }
630         return dist;
631 }
632
633
634 void
635 transform_print_deg(double deg)
636 {
637         printf("%2.0f:%2.0f:%2.4f", floor(deg), fmod(deg*60,60), fmod(deg*3600,60));
638 }
639
640 #ifdef AVOID_FLOAT
641 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};
642
643 static int
644 atan2_int_lookup(int val)
645 {
646         int len=sizeof(tab_atan)/sizeof(int);
647         int i=len/2;
648         int p=i-1;
649         for (;;) {
650                 i>>=1;
651                 if (val < tab_atan[p])
652                         p-=i;
653                 else
654                         if (val < tab_atan[p+1])
655                                 return p+(p>>1);
656                         else
657                                 p+=i;
658         }
659 }
660
661 static int
662 atan2_int(int dx, int dy)
663 {
664         int f,mul=1,add=0,ret;
665         if (! dx) {
666                 return dy < 0 ? 180 : 0;
667         }
668         if (! dy) {
669                 return dx < 0 ? -90 : 90;
670         }
671         if (dx < 0) {
672                 dx=-dx;
673                 mul=-1;
674         }
675         if (dy < 0) {
676                 dy=-dy;
677                 add=180*mul;
678                 mul*=-1;
679         }
680         while (dx > 20000 || dy > 20000) {
681                 dx/=10;
682                 dy/=10;
683         }
684         if (dx > dy) {
685                 ret=90-atan2_int_lookup(dy*10000/dx);
686         } else {
687                 ret=atan2_int_lookup(dx*10000/dy);
688         }
689         return ret*mul+add;
690 }
691 #endif
692
693 int
694 transform_get_angle_delta(struct coord *c1, struct coord *c2, int dir)
695 {
696         int dx=c2->x-c1->x;
697         int dy=c2->y-c1->y;
698 #ifndef AVOID_FLOAT 
699         double angle;
700         angle=atan2(dx,dy);
701         angle*=180/M_PI;
702 #else
703         int angle;
704         angle=atan2_int(dx,dy);
705 #endif
706         if (dir == -1)
707                 angle=angle-180;
708         if (angle < 0)
709                 angle+=360;
710         return angle;
711 }
712
713 int
714 transform_within_border(struct transformation *this_, struct point *p, int border)
715 {
716         struct map_selection *ms=this_->screen_sel;
717         while (ms) {
718                 struct point_rect *r=&ms->u.p_rect;
719                 if (p->x >= r->lu.x+border && p->x <= r->rl.x-border &&
720                     p->y >= r->lu.y+border && p->y <= r->rl.y-border)
721                         return 1;
722                 ms=ms->next;
723         }
724         return 0;
725 }
726
727 /*
728 Note: there are many mathematically equivalent ways to express these formulas. As usual, not all of them are computationally equivalent.
729
730 L = latitude in radians (positive north)
731 Lo = longitude in radians (positive east)
732 E = easting (meters)
733 N = northing (meters)
734
735 For the sphere
736
737 E = r Lo
738 N = r ln [ tan (pi/4 + L/2) ]
739
740 where 
741
742 r = radius of the sphere (meters)
743 ln() is the natural logarithm
744
745 For the ellipsoid
746
747 E = a Lo
748 N = a * ln ( tan (pi/4 + L/2) * ( (1 - e * sin (L)) / (1 + e * sin (L))) ** (e/2)  )
749
750
751                                                e
752                                                -
753                    pi     L     1 - e sin(L)   2
754     =  a ln( tan( ---- + ---) (--------------)   )
755                    4      2     1 + e sin(L) 
756
757
758 where
759
760 a = the length of the semi-major axis of the ellipsoid (meters)
761 e = the first eccentricity of the ellipsoid
762
763
764 */
765
766