Fix:Core:Reduce point densitiy in larger views
[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 mindist, 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,k=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                 if (i != 0 && i != count-1 && mindist) {
397                         if (xc > c[k].x-mindist && xc < c[k].x+mindist && yc > c[k].y-mindist && yc < c[k].y+mindist) 
398                                 continue;
399                         k=i;
400                 }
401                 xm=xc;
402                 ym=yc;
403 //              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);
404 //              ret=coord_rect_contains(&t->r, c);
405                 xc-=t->map_center.x;
406                 yc-=t->map_center.y;
407                 xc >>= t->scale_shift;
408                 yc >>= t->scale_shift;
409 #ifdef ENABLE_ROLL              
410                 xcn=xc*t->m00+yc*t->m01+t->hog*t->m02;
411                 ycn=xc*t->m10+yc*t->m11+t->hog*t->m12;
412 #else
413                 xcn=xc*t->m00+yc*t->m01;
414                 ycn=xc*t->m10+yc*t->m11;
415 #endif
416
417                 if (t->ddd) {
418 #ifdef ENABLE_ROLL              
419                         zc=(xc*t->m20+yc*t->m21+t->hog*t->m22);
420 #else
421                         zc=(xc*t->m20+yc*t->m21);
422 #endif
423                         zct=zc;
424                         zc+=t->offz << POST_SHIFT;
425                         dbg(1,"zc=%d\n", zc);
426                         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);
427                         /* visibility */
428                         visible=(zc < zlimit ? 0:1);
429                         dbg(1,"visible=%d old %d\n", visible, visibleo);
430                         if (visible != visibleo && visibleo != -1) { 
431                                 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);
432                                 if (zco != zc) {
433                                         xcn=xcn+(long long)(xco-xcn)*(zlimit-zc)/(zco-zc);
434                                         ycn=ycn+(long long)(yco-ycn)*(zlimit-zc)/(zco-zc);
435                                 }
436                                 dbg(1,"result (%d,%d,%d) * %d / %d\n", xcn,ycn,zc,zlimit-zc,zco-zc);
437                                 zc=zlimit;
438                                 xco=xcn;
439                                 yco=ycn;
440                                 zco=zc;
441                                 if (visible)
442                                         i--;
443                                 visibleo=visible;
444                         } else {
445                                 xco=xcn;
446                                 yco=ycn;
447                                 zco=zc;
448                                 visibleo=visible;
449                                 if (! visible)
450                                         continue;
451                         }
452                         dbg(1,"zc=%d\n", zc);
453                         dbg(1,"xcn %d ycn %d\n", xcn, ycn);
454                         dbg(1,"%d,%d %d\n",xc,yc,zc);
455 #if 0
456                         dbg(0,"%d/%d=%d %d/%d=%d\n",xcn,xc,xcn/xc,ycn,yc,ycn/yc);
457 #endif
458 #if 1
459                         xc=(long long)xcn*t->xyscale/zc;
460                         yc=(long long)ycn*t->xyscale/zc;
461 #else
462                         xc=xcn/(1000+zc);
463                         yc=ycn/(1000+zc);
464 #endif
465                         dbg(1,"%d,%d %d\n",xc,yc,zc);
466                 } else {
467                         xc=xcn;
468                         yc=ycn;
469                         xc>>=POST_SHIFT;
470                         yc>>=POST_SHIFT;
471                 }
472                 xc+=t->offx;
473                 yc+=t->offy;
474                 dbg(1,"xc=%d yc=%d\n", xc, yc);
475                 p[j].x=xc;
476                 p[j].y=yc;
477                 if (width_return) {
478                         if (t->ddd) 
479                                 width_return[j]=width*(t->offz << POST_SHIFT)/zc;
480                         else 
481                                 width_return[j]=width;
482                 }
483                 j++;
484         }
485         return j;
486 }
487
488 void
489 transform_reverse(struct transformation *t, struct point *p, struct coord *c)
490 {
491         double zc,xc,yc,xcn,ycn,q;
492         double offz=t->offz << POST_SHIFT;
493         xc=p->x - t->offx;
494         yc=p->y - t->offy;
495         if (t->ddd) {
496                 xc/=t->xyscale;
497                 yc/=t->xyscale;
498                 double f00=xc*t->im00*t->m20;
499                 double f01=yc*t->im01*t->m20;
500                 double f10=xc*t->im10*t->m21;
501                 double f11=yc*t->im11*t->m21;
502 #ifdef ENABLE_ROLL      
503                 q=(1-f00-f01-t->im02*t->m20-f10-f11-t->im12*t->m21);
504                 if (q < 0) 
505                         q=0.15;
506                 zc=(offz*((f00+f01+f10+f11))+t->hog*t->m22)/q;
507 #else
508                 q=(1-f00-f01-f10-f11);
509                 if (q < 0) 
510                         q=0.15;
511                 zc=offz*(f00+f01+f10+f11)/q;
512 #endif
513                 xcn=xc*(zc+offz);
514                 ycn=yc*(zc+offz);
515 #ifdef ENABLE_ROLL      
516                 xc=xcn*t->im00+ycn*t->im01+zc*t->im02;
517                 yc=xcn*t->im10+ycn*t->im11+zc*t->im12;
518 #else
519                 xc=xcn*t->im00+ycn*t->im01;
520                 yc=xcn*t->im10+ycn*t->im11;
521 #endif
522
523         } else {
524                 xcn=xc;
525                 ycn=yc;
526                 xc=(xcn*t->im00+ycn*t->im01)*(1 << POST_SHIFT);
527                 yc=(xcn*t->im10+ycn*t->im11)*(1 << POST_SHIFT);
528         }
529         c->x=xc*(1 << t->scale_shift)+t->map_center.x;
530         c->y=yc*(1 << t->scale_shift)+t->map_center.y;
531 }
532
533 enum projection
534 transform_get_projection(struct transformation *this_)
535 {
536         return this_->pro;
537 }
538
539 void
540 transform_set_projection(struct transformation *this_, enum projection pro)
541 {
542         this_->pro=pro;
543 }
544
545 static int
546 min4(int v1,int v2, int v3, int v4)
547 {
548         int res=v1;
549         if (v2 < res)
550                 res=v2;
551         if (v3 < res)
552                 res=v3;
553         if (v4 < res)
554                 res=v4;
555         return res;
556 }
557
558 static int
559 max4(int v1,int v2, int v3, int v4)
560 {
561         int res=v1;
562         if (v2 > res)
563                 res=v2;
564         if (v3 > res)
565                 res=v3;
566         if (v4 > res)
567                 res=v4;
568         return res;
569 }
570
571 struct map_selection *
572 transform_get_selection(struct transformation *this_, enum projection pro, int order)
573 {
574
575         struct map_selection *ret,*curri,*curro;
576         struct coord_geo g;
577         
578         ret=map_selection_dup(this_->map_sel);
579         curri=this_->map_sel;
580         curro=ret;
581         while (curri) {
582                 if (this_->pro != pro) {
583                         transform_to_geo(this_->pro, &curri->u.c_rect.lu, &g);
584                         transform_from_geo(pro, &g, &curro->u.c_rect.lu);
585                         dbg(1,"%f,%f", g.lat, g.lng);
586                         transform_to_geo(this_->pro, &curri->u.c_rect.rl, &g);
587                         transform_from_geo(pro, &g, &curro->u.c_rect.rl);
588                         dbg(1,": - %f,%f\n", g.lat, g.lng);
589                 }
590                 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);
591                 curro->order+=order;
592                 curro->range=item_range_all;
593                 curri=curri->next;
594                 curro=curro->next;
595         }
596         return ret;
597 }
598
599 struct coord *
600 transform_center(struct transformation *this_)
601 {
602         return &this_->map_center;
603 }
604
605 struct coord *
606 transform_get_center(struct transformation *this_)
607 {
608         return &this_->map_center;
609 }
610
611 void
612 transform_set_center(struct transformation *this_, struct coord *c)
613 {
614         this_->map_center=*c;
615 }
616
617
618 void
619 transform_set_yaw(struct transformation *t,int yaw)
620 {
621         t->yaw=yaw;
622         transform_setup_matrix(t);
623 }
624
625 int
626 transform_get_yaw(struct transformation *this_)
627 {
628         return this_->yaw;
629 }
630
631 void
632 transform_set_pitch(struct transformation *this_,int pitch)
633 {
634         this_->pitch=pitch;
635         transform_setup_matrix(this_);
636 }
637 int
638 transform_get_pitch(struct transformation *this_)
639 {
640         return this_->pitch;
641 }
642
643 #ifdef ENABLE_ROLL
644 void
645 transform_set_roll(struct transformation *this_,int roll)
646 {
647         this_->roll=roll;
648         transform_setup_matrix(this_);
649 }
650
651 int
652 transform_get_roll(struct transformation *this_)
653 {
654         return this_->roll;
655 }
656
657 #else
658
659 void
660 transform_set_roll(struct transformation *this_,int roll)
661 {
662         dbg(0,"not supported\n");
663 }
664
665 int
666 transform_get_roll(struct transformation *this_)
667 {
668         return 0;
669 }
670
671 #endif
672
673 void
674 transform_set_distance(struct transformation *this_,int distance)
675 {
676         this_->screen_dist=distance;
677         transform_setup_matrix(this_);
678 }
679
680 int
681 transform_get_distance(struct transformation *this_)
682 {
683         return this_->screen_dist;
684 }
685
686 void
687 transform_set_screen_selection(struct transformation *t, struct map_selection *sel)
688 {
689         map_selection_destroy(t->screen_sel);
690         t->screen_sel=map_selection_dup(sel);
691         if (sel) {
692                 t->screen_center.x=(sel->u.p_rect.rl.x-sel->u.p_rect.lu.x)/2;
693                 t->screen_center.y=(sel->u.p_rect.rl.y-sel->u.p_rect.lu.y)/2;
694                 transform_setup_matrix(t);
695         }
696 }
697
698 void
699 transform_set_screen_center(struct transformation *t, struct point *p)
700 {
701         t->screen_center=*p;
702 }
703
704 #if 0
705 void
706 transform_set_size(struct transformation *t, int width, int height)
707 {
708         t->width=width;
709         t->height=height;
710 }
711 #endif
712
713 void
714 transform_get_size(struct transformation *t, int *width, int *height)
715 {
716         struct point_rect *r;
717         if (t->screen_sel) {
718                 r=&t->screen_sel->u.p_rect;
719                 *width=r->rl.x-r->lu.x;
720                 *height=r->rl.y-r->lu.y;
721         }
722 }
723
724 void
725 transform_setup(struct transformation *t, struct pcoord *c, int scale, int yaw)
726 {
727         t->pro=c->pro;
728         t->map_center.x=c->x;
729         t->map_center.y=c->y;
730         t->scale=scale/16.0;
731         transform_set_yaw(t, yaw);
732 }
733
734 #if 0
735
736 void
737 transform_setup_source_rect_limit(struct transformation *t, struct coord *center, int limit)
738 {
739         t->center=*center;
740         t->scale=1;
741         t->angle=0;
742         t->r.lu.x=center->x-limit;
743         t->r.rl.x=center->x+limit;
744         t->r.rl.y=center->y-limit;
745         t->r.lu.y=center->y+limit;
746 }
747 #endif
748
749 void
750 transform_setup_source_rect(struct transformation *t)
751 {
752         int i;
753         struct coord screen[4];
754         struct point screen_pnt[4];
755         struct point_rect *pr;
756         struct map_selection *ms,*msm,*next,**msm_last;
757         ms=t->map_sel;
758         while (ms) {
759                 next=ms->next;
760                 g_free(ms);
761                 ms=next;
762         }
763         t->map_sel=NULL;
764         msm_last=&t->map_sel;
765         ms=t->screen_sel;
766         while (ms) {
767                 msm=g_new0(struct map_selection, 1);
768                 *msm=*ms;
769                 pr=&ms->u.p_rect;
770                 screen_pnt[0].x=pr->lu.x;
771                 screen_pnt[0].y=pr->lu.y;
772                 screen_pnt[1].x=pr->rl.x;
773                 screen_pnt[1].y=pr->lu.y;
774                 screen_pnt[2].x=pr->lu.x;
775                 screen_pnt[2].y=pr->rl.y;
776                 screen_pnt[3].x=pr->rl.x;
777                 screen_pnt[3].y=pr->rl.y;
778                 for (i = 0 ; i < 4 ; i++) {
779                         transform_reverse(t, &screen_pnt[i], &screen[i]);
780                         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);
781                 }
782                 msm->u.c_rect.lu.x=min4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
783                 msm->u.c_rect.rl.x=max4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
784                 msm->u.c_rect.rl.y=min4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
785                 msm->u.c_rect.lu.y=max4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
786                 dbg(1,"%dx%d\n", msm->u.c_rect.rl.x-msm->u.c_rect.lu.x,
787                                  msm->u.c_rect.lu.y-msm->u.c_rect.rl.y);
788                 *msm_last=msm;
789                 msm_last=&msm->next;
790                 ms=ms->next;
791         }
792 }
793
794 long
795 transform_get_scale(struct transformation *t)
796 {
797         return (int)(t->scale*16);
798 }
799
800 void
801 transform_set_scale(struct transformation *t, long scale)
802 {
803         t->scale=scale/16.0;
804         transform_setup_matrix(t);
805 }
806
807
808 int
809 transform_get_order(struct transformation *t)
810 {
811         dbg(1,"order %d\n", t->order);
812         return t->order;
813 }
814
815
816
817 #define TWOPI (M_PI*2)
818 #define GC2RAD(c) ((c) * TWOPI/(1<<24))
819 #define minf(a,b) ((a) < (b) ? (a) : (b))
820
821 static double
822 transform_distance_garmin(struct coord *c1, struct coord *c2)
823 {
824 #ifdef USE_HALVESINE
825         static const int earth_radius = 6371*1000; //m change accordingly
826 // static const int earth_radius  = 3960; //miles
827  
828 //Point 1 cords
829         navit_float lat1  = GC2RAD(c1->y);
830         navit_float long1 = GC2RAD(c1->x);
831
832 //Point 2 cords
833         navit_float lat2  = GC2RAD(c2->y);
834         navit_float long2 = GC2RAD(c2->x);
835
836 //Haversine Formula
837         navit_float dlong = long2-long1;
838         navit_float dlat  = lat2-lat1;
839
840         navit_float sinlat  = navit_sin(dlat/2);
841         navit_float sinlong = navit_sin(dlong/2);
842  
843         navit_float a=(sinlat*sinlat)+navit_cos(lat1)*navit_cos(lat2)*(sinlong*sinlong);
844         navit_float c=2*navit_asin(minf(1,navit_sqrt(a)));
845 #ifdef AVOID_FLOAT
846         return round(earth_radius*c);
847 #else
848         return earth_radius*c;
849 #endif
850 #else
851 #define GMETER 2.3887499999999999
852         navit_float dx,dy;
853         dx=c1->x-c2->x;
854         dy=c1->y-c2->y;
855         return navit_sqrt(dx*dx+dy*dy)*GMETER;
856 #undef GMETER
857 #endif
858 }
859
860 double
861 transform_scale(int y)
862 {
863         struct coord c;
864         struct coord_geo g;
865         c.x=0;
866         c.y=y;
867         transform_to_geo(projection_mg, &c, &g);
868         return 1/navit_cos(g.lat/180*M_PI);
869 }
870
871 #ifdef AVOID_FLOAT
872 static int
873 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};
874
875 static int tab_int_step = 0x20000;
876 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};
877
878 int transform_int_scale(int y)
879 {
880         int a=tab_int_step,i,size = sizeof(tab_int_scale)/sizeof(int);
881         if (y < 0)
882                 y=-y;
883         i=y/tab_int_step;
884         if (i < size-1) 
885                 return tab_int_scale[i]+((tab_int_scale[i+1]-tab_int_scale[i])*(y-i*tab_int_step))/tab_int_step;
886         return tab_int_scale[size-1];
887 }
888 #endif
889
890 double
891 transform_distance(enum projection pro, struct coord *c1, struct coord *c2)
892 {
893         if (pro == projection_mg) {
894 #ifndef AVOID_FLOAT 
895         double dx,dy,scale=transform_scale((c1->y+c2->y)/2);
896         dx=c1->x-c2->x;
897         dy=c1->y-c2->y;
898         return sqrt(dx*dx+dy*dy)/scale;
899 #else
900         int dx,dy,f,scale=transform_int_scale((c1->y+c2->y)/2);
901         dx=c1->x-c2->x;
902         dy=c1->y-c2->y;
903         if (dx < 0)
904                 dx=-dx;
905         if (dy < 0)
906                 dy=-dy;
907         while (dx > 20000 || dy > 20000) {
908                 dx/=10;
909                 dy/=10;
910                 scale/=10;
911         }
912         if (! dy)
913                 return dx*10000/scale;
914         if (! dx)
915                 return dy*10000/scale;
916         if (dx > dy) {
917                 f=dx*8/dy-8;
918                 if (f >= 32)
919                         return dx*10000/scale;
920                 return dx*tab_sqrt[f]/scale;
921         } else {
922                 f=dy*8/dx-8;
923                 if (f >= 32)
924                         return dy*10000/scale;
925                 return dy*tab_sqrt[f]/scale;
926         }
927 #endif
928         } else if (pro == projection_garmin) {
929                 return transform_distance_garmin(c1, c2);
930         } else {
931                 dbg(0,"Unknown projection: %d\n", pro);
932                 return 0;
933         }
934 }
935
936 void
937 transform_project(enum projection pro, struct coord *c, int distance, int angle, struct coord *res)
938 {
939         double scale;
940         switch (pro) {
941         case projection_mg:
942                 scale=transform_scale(c->y);
943                 res->x=c->x+distance*sin(angle*M_PI/180)*scale;
944                 res->y=c->y+distance*cos(angle*M_PI/180)*scale;
945                 break;
946         default:
947                 dbg(0,"Unsupported projection: %d\n", pro);
948                 return;
949         }
950         
951 }
952
953
954 double
955 transform_polyline_length(enum projection pro, struct coord *c, int count)
956 {
957         double ret=0;
958         int i;
959
960         for (i = 0 ; i < count-1 ; i++) 
961                 ret+=transform_distance(pro, &c[i], &c[i+1]);
962         return ret;
963 }
964
965 int
966 transform_distance_sq(struct coord *c1, struct coord *c2)
967 {
968         int dx=c1->x-c2->x;
969         int dy=c1->y-c2->y;
970
971         if (dx > 32767 || dy > 32767 || dx < -32767 || dy < -32767)
972                 return INT_MAX;
973         else
974                 return dx*dx+dy*dy;
975 }
976
977 int
978 transform_distance_sq_pc(struct pcoord *c1, struct pcoord *c2)
979 {
980         struct coord p1,p2;
981         p1.x = c1->x; p1.y = c1->y;
982         p2.x = c2->x; p2.y = c2->y;
983         return transform_distance_sq(&p1, &p2);
984 }
985
986 int
987 transform_distance_line_sq(struct coord *l0, struct coord *l1, struct coord *ref, struct coord *lpnt)
988 {
989         int vx,vy,wx,wy;
990         int c1,c2;
991         int climit=1000000;
992         struct coord l;
993
994         vx=l1->x-l0->x;
995         vy=l1->y-l0->y;
996         wx=ref->x-l0->x;
997         wy=ref->y-l0->y;
998
999         c1=vx*wx+vy*wy;
1000         if ( c1 <= 0 ) {
1001                 if (lpnt)
1002                         *lpnt=*l0;
1003                 return transform_distance_sq(l0, ref);
1004         }
1005         c2=vx*vx+vy*vy;
1006         if ( c2 <= c1 ) {
1007                 if (lpnt)
1008                         *lpnt=*l1;
1009                 return transform_distance_sq(l1, ref);
1010         }
1011         while (c1 > climit || c2 > climit) {
1012                 c1/=256;
1013                 c2/=256;
1014         }
1015         l.x=l0->x+vx*c1/c2;
1016         l.y=l0->y+vy*c1/c2;
1017         if (lpnt)
1018                 *lpnt=l;
1019         return transform_distance_sq(&l, ref);
1020 }
1021
1022 int
1023 transform_distance_polyline_sq(struct coord *c, int count, struct coord *ref, struct coord *lpnt, int *pos)
1024 {
1025         int i,dist,distn;
1026         struct coord lp;
1027         if (count < 2)
1028                 return INT_MAX;
1029         if (pos)
1030                 *pos=0;
1031         dist=transform_distance_line_sq(&c[0], &c[1], ref, lpnt);
1032         for (i=2 ; i < count ; i++) {
1033                 distn=transform_distance_line_sq(&c[i-1], &c[i], ref, &lp);
1034                 if (distn < dist) {
1035                         dist=distn;
1036                         if (lpnt)
1037                                 *lpnt=lp;
1038                         if (pos)
1039                                 *pos=i-1;
1040                 }
1041         }
1042         return dist;
1043 }
1044
1045
1046 void
1047 transform_print_deg(double deg)
1048 {
1049         printf("%2.0f:%2.0f:%2.4f", floor(deg), fmod(deg*60,60), fmod(deg*3600,60));
1050 }
1051
1052 #ifdef AVOID_FLOAT
1053 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};
1054
1055 static int
1056 atan2_int_lookup(int val)
1057 {
1058         int len=sizeof(tab_atan)/sizeof(int);
1059         int i=len/2;
1060         int p=i-1;
1061         for (;;) {
1062                 i>>=1;
1063                 if (val < tab_atan[p])
1064                         p-=i;
1065                 else
1066                         if (val < tab_atan[p+1])
1067                                 return p+(p>>1);
1068                         else
1069                                 p+=i;
1070         }
1071 }
1072
1073 static int
1074 atan2_int(int dx, int dy)
1075 {
1076         int f,mul=1,add=0,ret;
1077         if (! dx) {
1078                 return dy < 0 ? 180 : 0;
1079         }
1080         if (! dy) {
1081                 return dx < 0 ? -90 : 90;
1082         }
1083         if (dx < 0) {
1084                 dx=-dx;
1085                 mul=-1;
1086         }
1087         if (dy < 0) {
1088                 dy=-dy;
1089                 add=180*mul;
1090                 mul*=-1;
1091         }
1092         while (dx > 20000 || dy > 20000) {
1093                 dx/=10;
1094                 dy/=10;
1095         }
1096         if (dx > dy) {
1097                 ret=90-atan2_int_lookup(dy*10000/dx);
1098         } else {
1099                 ret=atan2_int_lookup(dx*10000/dy);
1100         }
1101         return ret*mul+add;
1102 }
1103 #endif
1104
1105 int
1106 transform_get_angle_delta(struct coord *c1, struct coord *c2, int dir)
1107 {
1108         int dx=c2->x-c1->x;
1109         int dy=c2->y-c1->y;
1110 #ifndef AVOID_FLOAT 
1111         double angle;
1112         angle=atan2(dx,dy);
1113         angle*=180/M_PI;
1114 #else
1115         int angle;
1116         angle=atan2_int(dx,dy);
1117 #endif
1118         if (dir == -1)
1119                 angle=angle-180;
1120         if (angle < 0)
1121                 angle+=360;
1122         return angle;
1123 }
1124
1125 int
1126 transform_within_border(struct transformation *this_, struct point *p, int border)
1127 {
1128         struct map_selection *ms=this_->screen_sel;
1129         while (ms) {
1130                 struct point_rect *r=&ms->u.p_rect;
1131                 if (p->x >= r->lu.x+border && p->x <= r->rl.x-border &&
1132                     p->y >= r->lu.y+border && p->y <= r->rl.y-border)
1133                         return 1;
1134                 ms=ms->next;
1135         }
1136         return 0;
1137 }
1138
1139 int
1140 transform_within_dist_point(struct coord *ref, struct coord *c, int dist)
1141 {
1142         if (c->x-dist > ref->x)
1143                 return 0;
1144         if (c->x+dist < ref->x)
1145                 return 0;
1146         if (c->y-dist > ref->y)
1147                 return 0;
1148         if (c->y+dist < ref->y)
1149                 return 0;
1150         if ((c->x-ref->x)*(c->x-ref->x) + (c->y-ref->y)*(c->y-ref->y) <= dist*dist) 
1151                 return 1;
1152         return 0;
1153 }
1154
1155 int
1156 transform_within_dist_line(struct coord *ref, struct coord *c0, struct coord *c1, int dist)
1157 {
1158         int vx,vy,wx,wy;
1159         int n1,n2;
1160         struct coord lc;
1161
1162         if (c0->x < c1->x) {
1163                 if (c0->x-dist > ref->x)
1164                         return 0;
1165                 if (c1->x+dist < ref->x)
1166                         return 0;
1167         } else {
1168                 if (c1->x-dist > ref->x)
1169                         return 0;
1170                 if (c0->x+dist < ref->x)
1171                         return 0;
1172         }
1173         if (c0->y < c1->y) {
1174                 if (c0->y-dist > ref->y)
1175                         return 0;
1176                 if (c1->y+dist < ref->y)
1177                         return 0;
1178         } else {
1179                 if (c1->y-dist > ref->y)
1180                         return 0;
1181                 if (c0->y+dist < ref->y)
1182                         return 0;
1183         }
1184         vx=c1->x-c0->x;
1185         vy=c1->y-c0->y;
1186         wx=ref->x-c0->x;
1187         wy=ref->y-c0->y;
1188
1189         n1=vx*wx+vy*wy;
1190         if ( n1 <= 0 )
1191                 return transform_within_dist_point(ref, c0, dist);
1192         n2=vx*vx+vy*vy;
1193         if ( n2 <= n1 )
1194                 return transform_within_dist_point(ref, c1, dist);
1195
1196         lc.x=c0->x+vx*n1/n2;
1197         lc.y=c0->y+vy*n1/n2;
1198         return transform_within_dist_point(ref, &lc, dist);
1199 }
1200
1201 int
1202 transform_within_dist_polyline(struct coord *ref, struct coord *c, int count, int close, int dist)
1203 {
1204         int i;
1205         for (i = 0 ; i < count-1 ; i++) {
1206                 if (transform_within_dist_line(ref,c+i,c+i+1,dist)) {
1207                         return 1;
1208                 }
1209         }
1210         if (close)
1211                 return (transform_within_dist_line(ref,c,c+count-1,dist));
1212         return 0;
1213 }
1214
1215 int
1216 transform_within_dist_polygon(struct coord *ref, struct coord *c, int count, int dist)
1217 {
1218         int i, j, ci = 0;
1219         for (i = 0, j = count-1; i < count; j = i++) {
1220                 if ((((c[i].y <= ref->y) && ( ref->y < c[j].y )) ||
1221                 ((c[j].y <= ref->y) && ( ref->y < c[i].y))) &&
1222                 (ref->x < (c[j].x - c[i].x) * (ref->y - c[i].y) / (c[j].y - c[i].y) + c[i].x))
1223                         ci = !ci;
1224         }
1225         if (! ci) {
1226                 if (dist)
1227                         return transform_within_dist_polyline(ref, c, count, dist, 1);
1228                 else
1229                         return 0;
1230         }
1231         return 1;
1232 }
1233
1234 int
1235 transform_within_dist_item(struct coord *ref, enum item_type type, struct coord *c, int count, int dist)
1236 {
1237         if (type < type_line)
1238                 return transform_within_dist_point(ref, c, dist);
1239         if (type < type_area)
1240                 return transform_within_dist_polyline(ref, c, count, 0, dist);
1241         return transform_within_dist_polygon(ref, c, count, dist);
1242 }
1243
1244 void
1245 transform_destroy(struct transformation *t)
1246 {
1247         g_free(t);
1248 }
1249
1250
1251 /*
1252 Note: there are many mathematically equivalent ways to express these formulas. As usual, not all of them are computationally equivalent.
1253
1254 L = latitude in radians (positive north)
1255 Lo = longitude in radians (positive east)
1256 E = easting (meters)
1257 N = northing (meters)
1258
1259 For the sphere
1260
1261 E = r Lo
1262 N = r ln [ tan (pi/4 + L/2) ]
1263
1264 where 
1265
1266 r = radius of the sphere (meters)
1267 ln() is the natural logarithm
1268
1269 For the ellipsoid
1270
1271 E = a Lo
1272 N = a * ln ( tan (pi/4 + L/2) * ( (1 - e * sin (L)) / (1 + e * sin (L))) ** (e/2)  )
1273
1274
1275                                                e
1276                                                -
1277                    pi     L     1 - e sin(L)   2
1278     =  a ln( tan( ---- + ---) (--------------)   )
1279                    4      2     1 + e sin(L) 
1280
1281
1282 where
1283
1284 a = the length of the semi-major axis of the ellipsoid (meters)
1285 e = the first eccentricity of the ellipsoid
1286
1287
1288 */
1289
1290