Add:Core:Added support for utm projection
[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 "item.h"
30 #include "map.h"
31 #include "transform.h"
32 #include "projection.h"
33 #include "point.h"
34
35 #define POST_SHIFT 8
36
37 struct transformation {
38         int yaw;                /* Rotation angle */
39         int pitch;
40         int ddd;
41         int m00,m01,m10,m11;    /* 2d transformation matrix */
42         int xyscale;
43         int m20,m21;            /* additional 3d parameters */
44 #ifdef ENABLE_ROLL
45         int roll;
46         int m02,m12,m22; 
47         int hog;
48         navit_float im02,im12,im20,im21,im22;
49 #endif
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;
54         int screen_dist;
55         int offx,offy,offz;
56         struct coord map_center;        /* Center of source rectangle */
57         enum projection pro;
58         navit_float scale;              /* Scale factor */
59         int scale_shift;
60         int order;
61         int order_base;
62 };
63
64
65
66 static void
67 transform_setup_matrix(struct transformation *t)
68 {
69         navit_float det;
70         navit_float fac;
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);
75 #ifdef ENABLE_ROLL      
76         navit_float rollc=navit_cos(M_PI*t->roll/180);
77         navit_float rolls=navit_sin(M_PI*t->roll/180);
78 #endif
79
80         int scale=t->scale;
81         int order_dir=-1;
82
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);
84         t->scale_shift=0;
85         t->order=t->order_base;
86         if (t->scale >= 1) {
87                 scale=t->scale;
88         } else {
89                 scale=1.0/t->scale;
90                 order_dir=1;
91         }
92         while (scale > 1) {
93                 if (order_dir < 0)
94                         t->scale_shift++;
95                 t->order+=order_dir;
96                 scale >>= 1;
97         }
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);
100         
101 #ifdef ENABLE_ROLL
102         t->m00=rollc*yawc*fac;
103         t->m01=rollc*yaws*fac;
104         t->m02=-rolls*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;
111 #else
112         t->m00=yawc*fac;
113         t->m01=yaws*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;
118 #endif
119         t->offz=0;
120         t->xyscale=1;
121         t->ddd=0;
122         t->offx=t->screen_center.x;
123         t->offy=t->screen_center.y;
124         if (t->pitch) {
125                 t->ddd=1;
126                 t->offz=t->screen_dist;
127                 t->xyscale=t->offz;
128         }
129 #ifdef ENABLE_ROLL
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;
136
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;
146 #else
147         det=((navit_float)t->m00*(navit_float)t->m11-(navit_float)t->m01*(navit_float)t->m10);
148         t->im00=t->m11/det;
149         t->im01=-t->m01/det;
150         t->im10=-t->m10/det;
151         t->im11=t->m00/det;
152 #endif
153 }
154
155 struct transformation *
156 transform_new(void)
157 {
158         struct transformation *this_;
159
160         this_=g_new0(struct transformation, 1);
161         this_->screen_dist=100;
162         this_->order_base=14;
163 #if 0
164         this_->pitch=20;
165 #endif
166 #if 0
167         this_->roll=30;
168         this_->hog=1000;
169 #endif
170         transform_setup_matrix(this_);
171         return this_;
172 }
173
174 #ifdef ENABLE_ROLL
175
176 int
177 transform_get_hog(struct transformation *this_)
178 {
179         return this_->hog;
180 }
181
182 void
183 transform_set_hog(struct transformation *this_, int hog)
184 {
185         this_->hog=hog;
186 }
187
188 #else
189
190 int
191 transform_get_hog(struct transformation *this_)
192 {
193         return 0;
194 }
195
196 void
197 transform_set_hog(struct transformation *this_, int hog)
198 {
199         dbg(0,"not supported\n");
200 }
201
202 #endif
203
204 int
205 transformation_get_order_base(struct transformation *this_)
206 {
207         return this_->order_base;
208 }
209
210 void
211 transform_set_order_base(struct transformation *this_, int order_base)
212 {
213         this_->order_base=order_base;
214 }
215
216
217 struct transformation *
218 transform_dup(struct transformation *t)
219 {
220         struct transformation *ret=g_new0(struct transformation, 1);
221         *ret=*t;
222         return ret;
223 }
224
225 static const navit_float gar2geo_units = 360.0/(1<<24);
226 static const navit_float geo2gar_units = 1/(360.0/(1<<24));
227
228 void
229 transform_to_geo(enum projection pro, struct coord *c, struct coord_geo *g)
230 {
231         int x,y,northern,zone;
232         switch (pro) {
233         case projection_mg:
234                 g->lng=c->x/6371000.0/M_PI*180;
235                 g->lat=navit_atan(exp(c->y/6371000.0))/M_PI*360-90;
236                 break;
237         case projection_garmin:
238                 g->lng=c->x*gar2geo_units;
239                 g->lat=c->y*gar2geo_units;
240                 break;
241         case projection_utm:
242                 x=c->x;
243                 y=c->y;
244                 northern=y >= 0;
245                 if (!northern) {
246                         y+=10000000;
247                 }
248                 zone=(x/1000000);
249                 x=x%1000000;
250                 transform_utm_to_geo(x, y, zone, northern, g);
251                 break;  
252         default:
253                 break;
254         }
255 }
256
257 void
258 transform_from_geo(enum projection pro, struct coord_geo *g, struct coord *c)
259 {
260         switch (pro) {
261         case projection_mg:
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;
264                 break;
265         case projection_garmin:
266                 c->x=g->lng*geo2gar_units;
267                 c->y=g->lat*geo2gar_units;
268                 break;
269         default:
270                 break;
271         }
272 }
273
274 void
275 transform_from_to(struct coord *cfrom, enum projection from, struct coord *cto, enum projection to)
276 {
277         struct coord_geo g;
278         transform_to_geo(from, cfrom, &g);
279         transform_from_geo(to, &g, cto);
280 }
281
282 void
283 transform_geo_to_cart(struct coord_geo *geo, navit_float a, navit_float b, struct coord_geo_cart *cart)
284 {
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);
290 }
291
292 void
293 transform_cart_to_geo(struct coord_geo_cart *cart, navit_float a, navit_float b, struct coord_geo *geo)
294 {
295         navit_float lat,lati,n,ee=1-b*b/(a*a), lng = navit_tan(cart->y/cart->x);
296
297         lat = navit_tan(cart->z / navit_sqrt((cart->x * cart->x) + (cart->y * cart->y)));
298         do
299         {
300                 lati = lat;
301
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));
304         }
305         while (fabs(lat - lati) >= 0.000000000000001);
306
307         geo->lng=lng/M_PI*180;
308         geo->lat=lat/M_PI*180;
309 }
310
311
312 void
313 transform_utm_to_geo(const double UTMEasting, const double UTMNorthing, int ZoneNumber, int NorthernHemisphere, struct coord_geo *geo)
314 {
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
320
321         double Lat, Long;
322         double k0 = 0.99960000000000004;
323         double a = 6378137;
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;
328         double LongOrigin;
329         double mu, phi1, phi1Rad;
330         double x, y;
331         double rad2deg = 180/M_PI;
332
333         x = UTMEasting - 500000.0; //remove 500,000 meter offset for longitude
334         y = UTMNorthing;
335
336         if (!NorthernHemisphere) {
337                 y -= 10000000.0;//remove 10,000,000 meter offset used for southern hemisphere
338         }
339
340         LongOrigin = (ZoneNumber - 1)*6 - 180 + 3;  //+3 puts origin in middle of zone
341
342         eccPrimeSquared = (eccSquared)/(1-eccSquared);
343
344         M = y / k0;
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;
350
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);
355         D = x/(N1*k0);
356
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);
359         Lat = Lat * rad2deg;
360
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;
364
365         geo->lat=Lat;
366         geo->lng=Long;
367 }
368
369 void
370 transform_datum(struct coord_geo *from, enum map_datum from_datum, struct coord_geo *to, enum map_datum to_datum)
371 {
372 }
373
374 int
375 transform(struct transformation *t, enum projection pro, struct coord *c, struct point *p, int count, int unique, int width, int *width_return)
376 {
377         struct coord c1;
378         int xcn, ycn; 
379         struct coord_geo g;
380         int xc, yc, zc=0, xco=0, yco=0, zco=0;
381         int xm,ym,zct;
382         int zlimit=1000;
383         int visible, visibleo=-1;
384         int i,j = 0;
385         dbg(1,"count=%d\n", count);
386         for (i=0; i < count; i++) {
387                 if (pro == t->pro) {
388                         xc=c[i].x;
389                         yc=c[i].y;
390                 } else {
391                         transform_to_geo(pro, &c[i], &g);
392                         transform_from_geo(t->pro, &g, &c1);
393                         xc=c1.x;
394                         yc=c1.y;
395                 }
396                 xm=xc;
397                 ym=yc;
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);
400                 xc-=t->map_center.x;
401                 yc-=t->map_center.y;
402                 xc >>= t->scale_shift;
403                 yc >>= t->scale_shift;
404 #ifdef ENABLE_ROLL              
405                 xcn=xc*t->m00+yc*t->m01+t->hog*t->m02;
406                 ycn=xc*t->m10+yc*t->m11+t->hog*t->m12;
407 #else
408                 xcn=xc*t->m00+yc*t->m01;
409                 ycn=xc*t->m10+yc*t->m11;
410 #endif
411
412                 if (t->ddd) {
413 #ifdef ENABLE_ROLL              
414                         zc=(xc*t->m20+yc*t->m21+t->hog*t->m22);
415 #else
416                         zc=(xc*t->m20+yc*t->m21);
417 #endif
418                         zct=zc;
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);
422                         /* visibility */
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);
427                                 if (zco != zc) {
428                                         xcn=xcn+(long long)(xco-xcn)*(zlimit-zc)/(zco-zc);
429                                         ycn=ycn+(long long)(yco-ycn)*(zlimit-zc)/(zco-zc);
430                                 }
431                                 dbg(1,"result (%d,%d,%d) * %d / %d\n", xcn,ycn,zc,zlimit-zc,zco-zc);
432                                 zc=zlimit;
433                                 xco=xcn;
434                                 yco=ycn;
435                                 zco=zc;
436                                 if (visible)
437                                         i--;
438                                 visibleo=visible;
439                         } else {
440                                 xco=xcn;
441                                 yco=ycn;
442                                 zco=zc;
443                                 visibleo=visible;
444                                 if (! visible)
445                                         continue;
446                         }
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);
450 #if 0
451                         dbg(0,"%d/%d=%d %d/%d=%d\n",xcn,xc,xcn/xc,ycn,yc,ycn/yc);
452 #endif
453 #if 1
454                         xc=(long long)xcn*t->xyscale/zc;
455                         yc=(long long)ycn*t->xyscale/zc;
456 #else
457                         xc=xcn/(1000+zc);
458                         yc=ycn/(1000+zc);
459 #endif
460                         dbg(1,"%d,%d %d\n",xc,yc,zc);
461                 } else {
462                         xc=xcn;
463                         yc=ycn;
464                         xc>>=POST_SHIFT;
465                         yc>>=POST_SHIFT;
466                 }
467                 xc+=t->offx;
468                 yc+=t->offy;
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) {
471                         p[j].x=xc;
472                         p[j].y=yc;
473                         if (width_return) {
474                                 if (t->ddd) 
475                                         width_return[j]=width*(t->offz << POST_SHIFT)/zc;
476                                 else 
477                                         width_return[j]=width;
478                         }
479                         j++;
480                 }
481         }
482         return j;
483 }
484
485 void
486 transform_reverse(struct transformation *t, struct point *p, struct coord *c)
487 {
488         double zc,xc,yc,xcn,ycn,q;
489         double offz=t->offz << POST_SHIFT;
490         xc=p->x - t->offx;
491         yc=p->y - t->offy;
492         if (t->ddd) {
493                 xc/=t->xyscale;
494                 yc/=t->xyscale;
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;
499 #ifdef ENABLE_ROLL      
500                 q=(1-f00-f01-t->im02*t->m20-f10-f11-t->im12*t->m21);
501                 if (q < 0) 
502                         q=0.15;
503                 zc=(offz*((f00+f01+f10+f11))+t->hog*t->m22)/q;
504 #else
505                 q=(1-f00-f01-f10-f11);
506                 if (q < 0) 
507                         q=0.15;
508                 zc=offz*(f00+f01+f10+f11)/q;
509 #endif
510                 xcn=xc*(zc+offz);
511                 ycn=yc*(zc+offz);
512 #ifdef ENABLE_ROLL      
513                 xc=xcn*t->im00+ycn*t->im01+zc*t->im02;
514                 yc=xcn*t->im10+ycn*t->im11+zc*t->im12;
515 #else
516                 xc=xcn*t->im00+ycn*t->im01;
517                 yc=xcn*t->im10+ycn*t->im11;
518 #endif
519
520         } else {
521                 xcn=xc;
522                 ycn=yc;
523                 xc=(xcn*t->im00+ycn*t->im01)*(1 << POST_SHIFT);
524                 yc=(xcn*t->im10+ycn*t->im11)*(1 << POST_SHIFT);
525         }
526         c->x=xc*(1 << t->scale_shift)+t->map_center.x;
527         c->y=yc*(1 << t->scale_shift)+t->map_center.y;
528 }
529
530 enum projection
531 transform_get_projection(struct transformation *this_)
532 {
533         return this_->pro;
534 }
535
536 void
537 transform_set_projection(struct transformation *this_, enum projection pro)
538 {
539         this_->pro=pro;
540 }
541
542 static int
543 min4(int v1,int v2, int v3, int v4)
544 {
545         int res=v1;
546         if (v2 < res)
547                 res=v2;
548         if (v3 < res)
549                 res=v3;
550         if (v4 < res)
551                 res=v4;
552         return res;
553 }
554
555 static int
556 max4(int v1,int v2, int v3, int v4)
557 {
558         int res=v1;
559         if (v2 > res)
560                 res=v2;
561         if (v3 > res)
562                 res=v3;
563         if (v4 > res)
564                 res=v4;
565         return res;
566 }
567
568 struct map_selection *
569 transform_get_selection(struct transformation *this_, enum projection pro, int order)
570 {
571
572         struct map_selection *ret,*curri,*curro;
573         struct coord_geo g;
574         
575         ret=map_selection_dup(this_->map_sel);
576         curri=this_->map_sel;
577         curro=ret;
578         while (curri) {
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);
586                 }
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);
588                 curro->order+=order;
589                 curro->range=item_range_all;
590                 curri=curri->next;
591                 curro=curro->next;
592         }
593         return ret;
594 }
595
596 struct coord *
597 transform_center(struct transformation *this_)
598 {
599         return &this_->map_center;
600 }
601
602 struct coord *
603 transform_get_center(struct transformation *this_)
604 {
605         return &this_->map_center;
606 }
607
608 void
609 transform_set_center(struct transformation *this_, struct coord *c)
610 {
611         this_->map_center=*c;
612 }
613
614
615 void
616 transform_set_yaw(struct transformation *t,int yaw)
617 {
618         t->yaw=yaw;
619         transform_setup_matrix(t);
620 }
621
622 int
623 transform_get_yaw(struct transformation *this_)
624 {
625         return this_->yaw;
626 }
627
628 void
629 transform_set_pitch(struct transformation *this_,int pitch)
630 {
631         this_->pitch=pitch;
632         transform_setup_matrix(this_);
633 }
634 int
635 transform_get_pitch(struct transformation *this_)
636 {
637         return this_->pitch;
638 }
639
640 #ifdef ENABLE_ROLL
641 void
642 transform_set_roll(struct transformation *this_,int roll)
643 {
644         this_->roll=roll;
645         transform_setup_matrix(this_);
646 }
647
648 int
649 transform_get_roll(struct transformation *this_)
650 {
651         return this_->roll;
652 }
653
654 #else
655
656 void
657 transform_set_roll(struct transformation *this_,int roll)
658 {
659         dbg(0,"not supported\n");
660 }
661
662 int
663 transform_get_roll(struct transformation *this_)
664 {
665         return 0;
666 }
667
668 #endif
669
670 void
671 transform_set_distance(struct transformation *this_,int distance)
672 {
673         this_->screen_dist=distance;
674         transform_setup_matrix(this_);
675 }
676
677 int
678 transform_get_distance(struct transformation *this_)
679 {
680         return this_->screen_dist;
681 }
682
683 void
684 transform_set_screen_selection(struct transformation *t, struct map_selection *sel)
685 {
686         map_selection_destroy(t->screen_sel);
687         t->screen_sel=map_selection_dup(sel);
688         if (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);
692         }
693 }
694
695 void
696 transform_set_screen_center(struct transformation *t, struct point *p)
697 {
698         t->screen_center=*p;
699 }
700
701 #if 0
702 void
703 transform_set_size(struct transformation *t, int width, int height)
704 {
705         t->width=width;
706         t->height=height;
707 }
708 #endif
709
710 void
711 transform_get_size(struct transformation *t, int *width, int *height)
712 {
713         struct point_rect *r;
714         if (t->screen_sel) {
715                 r=&t->screen_sel->u.p_rect;
716                 *width=r->rl.x-r->lu.x;
717                 *height=r->rl.y-r->lu.y;
718         }
719 }
720
721 void
722 transform_setup(struct transformation *t, struct pcoord *c, int scale, int yaw)
723 {
724         t->pro=c->pro;
725         t->map_center.x=c->x;
726         t->map_center.y=c->y;
727         t->scale=scale/16.0;
728         transform_set_yaw(t, yaw);
729 }
730
731 #if 0
732
733 void
734 transform_setup_source_rect_limit(struct transformation *t, struct coord *center, int limit)
735 {
736         t->center=*center;
737         t->scale=1;
738         t->angle=0;
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;
743 }
744 #endif
745
746 void
747 transform_setup_source_rect(struct transformation *t)
748 {
749         int i;
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;
754         ms=t->map_sel;
755         while (ms) {
756                 next=ms->next;
757                 g_free(ms);
758                 ms=next;
759         }
760         t->map_sel=NULL;
761         msm_last=&t->map_sel;
762         ms=t->screen_sel;
763         while (ms) {
764                 msm=g_new0(struct map_selection, 1);
765                 *msm=*ms;
766                 pr=&ms->u.p_rect;
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);
778                 }
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);
785                 *msm_last=msm;
786                 msm_last=&msm->next;
787                 ms=ms->next;
788         }
789 }
790
791 long
792 transform_get_scale(struct transformation *t)
793 {
794         return (int)(t->scale*16);
795 }
796
797 void
798 transform_set_scale(struct transformation *t, long scale)
799 {
800         t->scale=scale/16.0;
801         transform_setup_matrix(t);
802 }
803
804
805 int
806 transform_get_order(struct transformation *t)
807 {
808         dbg(1,"order %d\n", t->order);
809         return t->order;
810 }
811
812
813
814 #define TWOPI (M_PI*2)
815 #define GC2RAD(c) ((c) * TWOPI/(1<<24))
816 #define minf(a,b) ((a) < (b) ? (a) : (b))
817
818 static double
819 transform_distance_garmin(struct coord *c1, struct coord *c2)
820 {
821 #ifdef USE_HALVESINE
822         static const int earth_radius = 6371*1000; //m change accordingly
823 // static const int earth_radius  = 3960; //miles
824  
825 //Point 1 cords
826         navit_float lat1  = GC2RAD(c1->y);
827         navit_float long1 = GC2RAD(c1->x);
828
829 //Point 2 cords
830         navit_float lat2  = GC2RAD(c2->y);
831         navit_float long2 = GC2RAD(c2->x);
832
833 //Haversine Formula
834         navit_float dlong = long2-long1;
835         navit_float dlat  = lat2-lat1;
836
837         navit_float sinlat  = navit_sin(dlat/2);
838         navit_float sinlong = navit_sin(dlong/2);
839  
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)));
842 #ifdef AVOID_FLOAT
843         return round(earth_radius*c);
844 #else
845         return earth_radius*c;
846 #endif
847 #else
848 #define GMETER 2.3887499999999999
849         navit_float dx,dy;
850         dx=c1->x-c2->x;
851         dy=c1->y-c2->y;
852         return navit_sqrt(dx*dx+dy*dy)*GMETER;
853 #undef GMETER
854 #endif
855 }
856
857 double
858 transform_scale(int y)
859 {
860         struct coord c;
861         struct coord_geo g;
862         c.x=0;
863         c.y=y;
864         transform_to_geo(projection_mg, &c, &g);
865         return 1/navit_cos(g.lat/180*M_PI);
866 }
867
868 #ifdef AVOID_FLOAT
869 static int
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};
871 #endif
872
873 double
874 transform_distance(enum projection pro, struct coord *c1, struct coord *c2)
875 {
876         if (pro == projection_mg) {
877 #ifndef AVOID_FLOAT 
878         double dx,dy,scale=transform_scale((c1->y+c2->y)/2);
879         dx=c1->x-c2->x;
880         dy=c1->y-c2->y;
881         return sqrt(dx*dx+dy*dy)/scale;
882 #else
883         int dx,dy,f,scale=15539;
884         dx=c1->x-c2->x;
885         dy=c1->y-c2->y;
886         if (dx < 0)
887                 dx=-dx;
888         if (dy < 0)
889                 dy=-dy;
890         while (dx > 20000 || dy > 20000) {
891                 dx/=10;
892                 dy/=10;
893                 scale/=10;
894         }
895         if (! dy)
896                 return dx*10000/scale;
897         if (! dx)
898                 return dy*10000/scale;
899         if (dx > dy) {
900                 f=dx*8/dy-8;
901                 if (f >= 32)
902                         return dx*10000/scale;
903                 return dx*tab_sqrt[f]/scale;
904         } else {
905                 f=dy*8/dx-8;
906                 if (f >= 32)
907                         return dy*10000/scale;
908                 return dy*tab_sqrt[f]/scale;
909         }
910 #endif
911         } else if (pro == projection_garmin) {
912                 return transform_distance_garmin(c1, c2);
913         } else {
914                 dbg(0,"Unknown projection: %d\n", pro);
915                 return 0;
916         }
917 }
918
919 double
920 transform_polyline_length(enum projection pro, struct coord *c, int count)
921 {
922         double ret=0;
923         int i;
924
925         for (i = 0 ; i < count-1 ; i++) 
926                 ret+=transform_distance(pro, &c[i], &c[i+1]);
927         return ret;
928 }
929
930 int
931 transform_distance_sq(struct coord *c1, struct coord *c2)
932 {
933         int dx=c1->x-c2->x;
934         int dy=c1->y-c2->y;
935
936         if (dx > 32767 || dy > 32767 || dx < -32767 || dy < -32767)
937                 return INT_MAX;
938         else
939                 return dx*dx+dy*dy;
940 }
941
942 int
943 transform_distance_sq_pc(struct pcoord *c1, struct pcoord *c2)
944 {
945         struct coord p1,p2;
946         p1.x = c1->x; p1.y = c1->y;
947         p2.x = c2->x; p2.y = c2->y;
948         return transform_distance_sq(&p1, &p2);
949 }
950
951 int
952 transform_distance_line_sq(struct coord *l0, struct coord *l1, struct coord *ref, struct coord *lpnt)
953 {
954         int vx,vy,wx,wy;
955         int c1,c2;
956         int climit=1000000;
957         struct coord l;
958
959         vx=l1->x-l0->x;
960         vy=l1->y-l0->y;
961         wx=ref->x-l0->x;
962         wy=ref->y-l0->y;
963
964         c1=vx*wx+vy*wy;
965         if ( c1 <= 0 ) {
966                 if (lpnt)
967                         *lpnt=*l0;
968                 return transform_distance_sq(l0, ref);
969         }
970         c2=vx*vx+vy*vy;
971         if ( c2 <= c1 ) {
972                 if (lpnt)
973                         *lpnt=*l1;
974                 return transform_distance_sq(l1, ref);
975         }
976         while (c1 > climit || c2 > climit) {
977                 c1/=256;
978                 c2/=256;
979         }
980         l.x=l0->x+vx*c1/c2;
981         l.y=l0->y+vy*c1/c2;
982         if (lpnt)
983                 *lpnt=l;
984         return transform_distance_sq(&l, ref);
985 }
986
987 int
988 transform_distance_polyline_sq(struct coord *c, int count, struct coord *ref, struct coord *lpnt, int *pos)
989 {
990         int i,dist,distn;
991         struct coord lp;
992         if (count < 2)
993                 return INT_MAX;
994         if (pos)
995                 *pos=0;
996         dist=transform_distance_line_sq(&c[0], &c[1], ref, lpnt);
997         for (i=2 ; i < count ; i++) {
998                 distn=transform_distance_line_sq(&c[i-1], &c[i], ref, &lp);
999                 if (distn < dist) {
1000                         dist=distn;
1001                         if (lpnt)
1002                                 *lpnt=lp;
1003                         if (pos)
1004                                 *pos=i-1;
1005                 }
1006         }
1007         return dist;
1008 }
1009
1010
1011 void
1012 transform_print_deg(double deg)
1013 {
1014         printf("%2.0f:%2.0f:%2.4f", floor(deg), fmod(deg*60,60), fmod(deg*3600,60));
1015 }
1016
1017 #ifdef AVOID_FLOAT
1018 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};
1019
1020 static int
1021 atan2_int_lookup(int val)
1022 {
1023         int len=sizeof(tab_atan)/sizeof(int);
1024         int i=len/2;
1025         int p=i-1;
1026         for (;;) {
1027                 i>>=1;
1028                 if (val < tab_atan[p])
1029                         p-=i;
1030                 else
1031                         if (val < tab_atan[p+1])
1032                                 return p+(p>>1);
1033                         else
1034                                 p+=i;
1035         }
1036 }
1037
1038 static int
1039 atan2_int(int dx, int dy)
1040 {
1041         int f,mul=1,add=0,ret;
1042         if (! dx) {
1043                 return dy < 0 ? 180 : 0;
1044         }
1045         if (! dy) {
1046                 return dx < 0 ? -90 : 90;
1047         }
1048         if (dx < 0) {
1049                 dx=-dx;
1050                 mul=-1;
1051         }
1052         if (dy < 0) {
1053                 dy=-dy;
1054                 add=180*mul;
1055                 mul*=-1;
1056         }
1057         while (dx > 20000 || dy > 20000) {
1058                 dx/=10;
1059                 dy/=10;
1060         }
1061         if (dx > dy) {
1062                 ret=90-atan2_int_lookup(dy*10000/dx);
1063         } else {
1064                 ret=atan2_int_lookup(dx*10000/dy);
1065         }
1066         return ret*mul+add;
1067 }
1068 #endif
1069
1070 int
1071 transform_get_angle_delta(struct coord *c1, struct coord *c2, int dir)
1072 {
1073         int dx=c2->x-c1->x;
1074         int dy=c2->y-c1->y;
1075 #ifndef AVOID_FLOAT 
1076         double angle;
1077         angle=atan2(dx,dy);
1078         angle*=180/M_PI;
1079 #else
1080         int angle;
1081         angle=atan2_int(dx,dy);
1082 #endif
1083         if (dir == -1)
1084                 angle=angle-180;
1085         if (angle < 0)
1086                 angle+=360;
1087         return angle;
1088 }
1089
1090 int
1091 transform_within_border(struct transformation *this_, struct point *p, int border)
1092 {
1093         struct map_selection *ms=this_->screen_sel;
1094         while (ms) {
1095                 struct point_rect *r=&ms->u.p_rect;
1096                 if (p->x >= r->lu.x+border && p->x <= r->rl.x-border &&
1097                     p->y >= r->lu.y+border && p->y <= r->rl.y-border)
1098                         return 1;
1099                 ms=ms->next;
1100         }
1101         return 0;
1102 }
1103
1104 int
1105 transform_within_dist_point(struct coord *ref, struct coord *c, int dist)
1106 {
1107         if (c->x-dist > ref->x)
1108                 return 0;
1109         if (c->x+dist < ref->x)
1110                 return 0;
1111         if (c->y-dist > ref->y)
1112                 return 0;
1113         if (c->y+dist < ref->y)
1114                 return 0;
1115         if ((c->x-ref->x)*(c->x-ref->x) + (c->y-ref->y)*(c->y-ref->y) <= dist*dist) 
1116                 return 1;
1117         return 0;
1118 }
1119
1120 int
1121 transform_within_dist_line(struct coord *ref, struct coord *c0, struct coord *c1, int dist)
1122 {
1123         int vx,vy,wx,wy;
1124         int n1,n2;
1125         struct coord lc;
1126
1127         if (c0->x < c1->x) {
1128                 if (c0->x-dist > ref->x)
1129                         return 0;
1130                 if (c1->x+dist < ref->x)
1131                         return 0;
1132         } else {
1133                 if (c1->x-dist > ref->x)
1134                         return 0;
1135                 if (c0->x+dist < ref->x)
1136                         return 0;
1137         }
1138         if (c0->y < c1->y) {
1139                 if (c0->y-dist > ref->y)
1140                         return 0;
1141                 if (c1->y+dist < ref->y)
1142                         return 0;
1143         } else {
1144                 if (c1->y-dist > ref->y)
1145                         return 0;
1146                 if (c0->y+dist < ref->y)
1147                         return 0;
1148         }
1149         vx=c1->x-c0->x;
1150         vy=c1->y-c0->y;
1151         wx=ref->x-c0->x;
1152         wy=ref->y-c0->y;
1153
1154         n1=vx*wx+vy*wy;
1155         if ( n1 <= 0 )
1156                 return transform_within_dist_point(ref, c0, dist);
1157         n2=vx*vx+vy*vy;
1158         if ( n2 <= n1 )
1159                 return transform_within_dist_point(ref, c1, dist);
1160
1161         lc.x=c0->x+vx*n1/n2;
1162         lc.y=c0->y+vy*n1/n2;
1163         return transform_within_dist_point(ref, &lc, dist);
1164 }
1165
1166 int
1167 transform_within_dist_polyline(struct coord *ref, struct coord *c, int count, int close, int dist)
1168 {
1169         int i;
1170         for (i = 0 ; i < count-1 ; i++) {
1171                 if (transform_within_dist_line(ref,c+i,c+i+1,dist)) {
1172                         return 1;
1173                 }
1174         }
1175         if (close)
1176                 return (transform_within_dist_line(ref,c,c+count-1,dist));
1177         return 0;
1178 }
1179
1180 int
1181 transform_within_dist_polygon(struct coord *ref, struct coord *c, int count, int dist)
1182 {
1183         int i, j, ci = 0;
1184         for (i = 0, j = count-1; i < count; j = i++) {
1185                 if ((((c[i].y <= ref->y) && ( ref->y < c[j].y )) ||
1186                 ((c[j].y <= ref->y) && ( ref->y < c[i].y))) &&
1187                 (ref->x < (c[j].x - c[i].x) * (ref->y - c[i].y) / (c[j].y - c[i].y) + c[i].x))
1188                         ci = !ci;
1189         }
1190         if (! ci) {
1191                 if (dist)
1192                         return transform_within_dist_polyline(ref, c, count, dist, 1);
1193                 else
1194                         return 0;
1195         }
1196         return 1;
1197 }
1198
1199 int
1200 transform_within_dist_item(struct coord *ref, enum item_type type, struct coord *c, int count, int dist)
1201 {
1202         if (type < type_line)
1203                 return transform_within_dist_point(ref, c, dist);
1204         if (type < type_area)
1205                 return transform_within_dist_polyline(ref, c, count, 0, dist);
1206         return transform_within_dist_polygon(ref, c, count, dist);
1207 }
1208
1209 void
1210 transform_destroy(struct transformation *t)
1211 {
1212         g_free(t);
1213 }
1214
1215
1216 /*
1217 Note: there are many mathematically equivalent ways to express these formulas. As usual, not all of them are computationally equivalent.
1218
1219 L = latitude in radians (positive north)
1220 Lo = longitude in radians (positive east)
1221 E = easting (meters)
1222 N = northing (meters)
1223
1224 For the sphere
1225
1226 E = r Lo
1227 N = r ln [ tan (pi/4 + L/2) ]
1228
1229 where 
1230
1231 r = radius of the sphere (meters)
1232 ln() is the natural logarithm
1233
1234 For the ellipsoid
1235
1236 E = a Lo
1237 N = a * ln ( tan (pi/4 + L/2) * ( (1 - e * sin (L)) / (1 + e * sin (L))) ** (e/2)  )
1238
1239
1240                                                e
1241                                                -
1242                    pi     L     1 - e sin(L)   2
1243     =  a ln( tan( ---- + ---) (--------------)   )
1244                    4      2     1 + e sin(L) 
1245
1246
1247 where
1248
1249 a = the length of the semi-major axis of the ellipsoid (meters)
1250 e = the first eccentricity of the ellipsoid
1251
1252
1253 */
1254
1255