Fix:map_csv:Disable default notification of each deleted item.
[profile/ivi/navit.git] / navit / 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 #define _USE_MATH_DEFINES 1
21 #include <assert.h>
22 #include <stdio.h>
23 #include <math.h>
24 #include <limits.h>
25 #include <glib.h>
26 #include <string.h>
27 #include "config.h"
28 #include "coord.h"
29 #include "debug.h"
30 #include "item.h"
31 #include "map.h"
32 #include "transform.h"
33 #include "projection.h"
34 #include "point.h"
35
36 #define POST_SHIFT 8
37
38 struct transformation {
39         int yaw;                /* Rotation angle */
40         int pitch;
41         int ddd;
42         int m00,m01,m02;        /* 3d transformation matrix */
43         int m10,m11,m12;        
44         int m20,m21,m22;        
45         int xscale,yscale,wscale;
46         int xscale3d,yscale3d,wscale3d;
47 #ifdef ENABLE_ROLL
48         int roll;
49         int hog;
50 #endif
51         navit_float im00,im01,im02;     /* inverse 3d transformation matrix */
52         navit_float im10,im11,im12;
53         navit_float im20,im21,im22;
54         struct map_selection *map_sel;
55         struct map_selection *screen_sel;
56         struct point screen_center;
57         int screen_dist;
58         int offx,offy,offz;
59         int znear,zfar;
60         struct coord map_center;        /* Center of source rectangle */
61         enum projection pro;
62         navit_float scale;              /* Scale factor */
63         int scale_shift;
64         int order;
65         int order_base;
66 };
67
68 #ifdef ENABLE_ROLL
69 #define HOG(t) ((t).hog)
70 #else
71 #define HOG(t) 0
72 #endif
73
74 static void
75 transform_set_screen_dist(struct transformation *t, int dist)
76 {
77         t->screen_dist=dist;
78         t->xscale3d=dist;
79         t->yscale3d=dist;
80         t->wscale3d=dist << POST_SHIFT;
81 }
82
83 static void
84 transform_setup_matrix(struct transformation *t)
85 {
86         navit_float det;
87         navit_float fac;
88         navit_float yawc=navit_cos(-M_PI*t->yaw/180);
89         navit_float yaws=navit_sin(-M_PI*t->yaw/180);
90         navit_float pitchc=navit_cos(-M_PI*t->pitch/180);
91         navit_float pitchs=navit_sin(-M_PI*t->pitch/180);
92 #ifdef ENABLE_ROLL      
93         navit_float rollc=navit_cos(M_PI*t->roll/180);
94         navit_float rolls=navit_sin(M_PI*t->roll/180);
95 #else
96         navit_float rollc=1;
97         navit_float rolls=0;
98 #endif
99
100         int scale=t->scale;
101         int order_dir=-1;
102
103         dbg(1,"yaw=%d pitch=%d center=0x%x,0x%x\n", t->yaw, t->pitch, t->map_center.x, t->map_center.y);
104         t->znear=1 << POST_SHIFT;
105         t->zfar=300*t->znear;
106         t->scale_shift=0;
107         t->order=t->order_base;
108         if (t->scale >= 1) {
109                 scale=t->scale;
110         } else {
111                 scale=1.0/t->scale;
112                 order_dir=1;
113         }
114         while (scale > 1) {
115                 if (order_dir < 0)
116                         t->scale_shift++;
117                 t->order+=order_dir;
118                 scale >>= 1;
119         }
120         fac=(1 << POST_SHIFT) * (1 << t->scale_shift) / t->scale;
121         dbg(1,"scale_shift=%d order=%d scale=%f fac=%f\n", t->scale_shift, t->order,t->scale,fac);
122         
123         t->m00=rollc*yawc*fac;
124         t->m01=rollc*yaws*fac;
125         t->m02=-rolls*fac;
126         t->m10=(pitchs*rolls*yawc-pitchc*yaws)*(-fac);
127         t->m11=(pitchs*rolls*yaws+pitchc*yawc)*(-fac);
128         t->m12=pitchs*rollc*(-fac);
129         t->m20=(pitchc*rolls*yawc+pitchs*yaws)*fac;
130         t->m21=(pitchc*rolls*yaws-pitchs*yawc)*fac;
131         t->m22=pitchc*rollc*fac;
132
133         t->offx=t->screen_center.x;
134         t->offy=t->screen_center.y;
135         if (t->pitch) {
136                 t->ddd=1;
137                 t->offz=t->screen_dist;
138                 dbg(1,"near %d far %d\n",t->znear,t->zfar);
139                 t->xscale=t->xscale3d;
140                 t->yscale=t->yscale3d;
141                 t->wscale=t->wscale3d;
142         } else {
143                 t->ddd=0;
144                 t->offz=0;
145                 t->xscale=1;
146                 t->yscale=1;
147                 t->wscale=1;
148         }
149         det=(navit_float)t->m00*(navit_float)t->m11*(navit_float)t->m22+
150             (navit_float)t->m01*(navit_float)t->m12*(navit_float)t->m20+
151             (navit_float)t->m02*(navit_float)t->m10*(navit_float)t->m21-
152             (navit_float)t->m02*(navit_float)t->m11*(navit_float)t->m20-
153             (navit_float)t->m01*(navit_float)t->m10*(navit_float)t->m22-
154             (navit_float)t->m00*(navit_float)t->m12*(navit_float)t->m21;
155
156         t->im00=(t->m11*t->m22-t->m12*t->m21)/det;
157         t->im01=(t->m02*t->m21-t->m01*t->m22)/det;
158         t->im02=(t->m01*t->m12-t->m02*t->m11)/det;
159         t->im10=(t->m12*t->m20-t->m10*t->m22)/det;
160         t->im11=(t->m00*t->m22-t->m02*t->m20)/det;
161         t->im12=(t->m02*t->m10-t->m00*t->m12)/det;
162         t->im20=(t->m10*t->m21-t->m11*t->m20)/det;
163         t->im21=(t->m01*t->m20-t->m00*t->m21)/det;
164         t->im22=(t->m00*t->m11-t->m01*t->m10)/det;
165 }
166
167 struct transformation *
168 transform_new(void)
169 {
170         struct transformation *this_;
171
172         this_=g_new0(struct transformation, 1);
173         transform_set_screen_dist(this_, 100);
174         this_->order_base=14;
175 #if 0
176         this_->pitch=20;
177 #endif
178 #if 0
179         this_->roll=30;
180         this_->hog=1000;
181 #endif
182         transform_setup_matrix(this_);
183         return this_;
184 }
185
186 int
187 transform_get_hog(struct transformation *this_)
188 {
189         return HOG(*this_);
190 }
191
192 void
193 transform_set_hog(struct transformation *this_, int hog)
194 {
195 #ifdef ENABLE_ROLL
196         this_->hog=hog;
197 #else
198         dbg(0,"not supported\n");
199 #endif
200
201 }
202
203 int
204 transform_get_attr(struct transformation *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
205 {
206         switch (type) {
207 #ifdef ENABLE_ROLL
208         case attr_hog:
209                 attr->u.num=this_->hog;
210                 break;
211 #endif
212         default:
213                 return 0;
214         }
215         attr->type=type;
216         return 1;
217 }
218
219 int
220 transform_set_attr(struct transformation *this_, struct attr *attr)
221 {
222         switch (attr->type) {
223 #ifdef ENABLE_ROLL
224         case attr_hog:
225                 this_->hog=attr->u.num;
226                 return 1;
227 #endif
228         default:
229                 return 0;
230         }
231 }
232
233 int
234 transformation_get_order_base(struct transformation *this_)
235 {
236         return this_->order_base;
237 }
238
239 void
240 transform_set_order_base(struct transformation *this_, int order_base)
241 {
242         this_->order_base=order_base;
243 }
244
245
246 struct transformation *
247 transform_dup(struct transformation *t)
248 {
249         struct transformation *ret=g_new0(struct transformation, 1);
250         *ret=*t;
251         return ret;
252 }
253
254 static const navit_float gar2geo_units = 360.0/(1<<24);
255 static const navit_float geo2gar_units = 1/(360.0/(1<<24));
256
257 void
258 transform_to_geo(enum projection pro, struct coord *c, struct coord_geo *g)
259 {
260         int x,y,northern,zone;
261         switch (pro) {
262         case projection_mg:
263                 g->lng=c->x/6371000.0/M_PI*180;
264                 g->lat=navit_atan(exp(c->y/6371000.0))/M_PI*360-90;
265                 break;
266         case projection_garmin:
267                 g->lng=c->x*gar2geo_units;
268                 g->lat=c->y*gar2geo_units;
269                 break;
270         case projection_utm:
271                 x=c->x;
272                 y=c->y;
273                 northern=y >= 0;
274                 if (!northern) {
275                         y+=10000000;
276                 }
277                 zone=(x/1000000);
278                 x=x%1000000;
279                 transform_utm_to_geo(x, y, zone, northern, g);
280                 break;  
281         default:
282                 break;
283         }
284 }
285
286 void
287 transform_from_geo(enum projection pro, struct coord_geo *g, struct coord *c)
288 {
289         switch (pro) {
290         case projection_mg:
291                 c->x=g->lng*6371000.0*M_PI/180;
292                 c->y=log(navit_tan(M_PI_4+g->lat*M_PI/360))*6371000.0;
293                 break;
294         case projection_garmin:
295                 c->x=g->lng*geo2gar_units;
296                 c->y=g->lat*geo2gar_units;
297                 break;
298         default:
299                 break;
300         }
301 }
302
303 void
304 transform_from_to_count(struct coord *cfrom, enum projection from, struct coord *cto, enum projection to, int count)
305 {
306         struct coord_geo g;
307         int i;
308
309         for (i = 0 ; i < count ; i++) {
310                 transform_to_geo(from, cfrom, &g);
311                 transform_from_geo(to, &g, cto);
312                 cfrom++;
313                 cto++;
314         }
315 }
316
317 void
318 transform_from_to(struct coord *cfrom, enum projection from, struct coord *cto, enum projection to)
319 {
320         struct coord_geo g;
321         transform_to_geo(from, cfrom, &g);
322         transform_from_geo(to, &g, cto);
323 }
324
325 void
326 transform_geo_to_cart(struct coord_geo *geo, navit_float a, navit_float b, struct coord_geo_cart *cart)
327 {
328         navit_float n,ee=1-b*b/(a*a);
329         n = a/sqrtf(1-ee*navit_sin(geo->lat)*navit_sin(geo->lat));
330         cart->x=n*navit_cos(geo->lat)*navit_cos(geo->lng);
331         cart->y=n*navit_cos(geo->lat)*navit_sin(geo->lng);
332         cart->z=n*(1-ee)*navit_sin(geo->lat);
333 }
334
335 void
336 transform_cart_to_geo(struct coord_geo_cart *cart, navit_float a, navit_float b, struct coord_geo *geo)
337 {
338         navit_float lat,lati,n,ee=1-b*b/(a*a), lng = navit_tan(cart->y/cart->x);
339
340         lat = navit_tan(cart->z / navit_sqrt((cart->x * cart->x) + (cart->y * cart->y)));
341         do
342         {
343                 lati = lat;
344
345                 n = a / navit_sqrt(1-ee*navit_sin(lat)*navit_sin(lat));
346                 lat = navit_atan((cart->z + ee * n * navit_sin(lat)) / navit_sqrt(cart->x * cart->x + cart->y * cart->y));
347         }
348         while (fabs(lat - lati) >= 0.000000000000001);
349
350         geo->lng=lng/M_PI*180;
351         geo->lat=lat/M_PI*180;
352 }
353
354
355 void
356 transform_utm_to_geo(const double UTMEasting, const double UTMNorthing, int ZoneNumber, int NorthernHemisphere, struct coord_geo *geo)
357 {
358 //converts UTM coords to lat/long.  Equations from USGS Bulletin 1532 
359 //East Longitudes are positive, West longitudes are negative. 
360 //North latitudes are positive, South latitudes are negative
361 //Lat and Long are in decimal degrees. 
362         //Written by Chuck Gantz- chuck.gantz@globalstar.com
363
364         double Lat, Long;
365         double k0 = 0.99960000000000004;
366         double a = 6378137;
367         double eccSquared = 0.0066943799999999998;
368         double eccPrimeSquared;
369         double e1 = (1-sqrt(1-eccSquared))/(1+sqrt(1-eccSquared));
370         double N1, T1, C1, R1, D, M;
371         double LongOrigin;
372         double mu, phi1, phi1Rad;
373         double x, y;
374         double rad2deg = 180/M_PI;
375
376         x = UTMEasting - 500000.0; //remove 500,000 meter offset for longitude
377         y = UTMNorthing;
378
379         if (!NorthernHemisphere) {
380                 y -= 10000000.0;//remove 10,000,000 meter offset used for southern hemisphere
381         }
382
383         LongOrigin = (ZoneNumber - 1)*6 - 180 + 3;  //+3 puts origin in middle of zone
384
385         eccPrimeSquared = (eccSquared)/(1-eccSquared);
386
387         M = y / k0;
388         mu = M/(a*(1-eccSquared/4-3*eccSquared*eccSquared/64-5*eccSquared*eccSquared*eccSquared/256));
389         phi1Rad = mu    + (3*e1/2-27*e1*e1*e1/32)*sin(2*mu) 
390                                 + (21*e1*e1/16-55*e1*e1*e1*e1/32)*sin(4*mu)
391                                 +(151*e1*e1*e1/96)*sin(6*mu);
392         phi1 = phi1Rad*rad2deg;
393
394         N1 = a/sqrt(1-eccSquared*sin(phi1Rad)*sin(phi1Rad));
395         T1 = tan(phi1Rad)*tan(phi1Rad);
396         C1 = eccPrimeSquared*cos(phi1Rad)*cos(phi1Rad);
397         R1 = a*(1-eccSquared)/pow(1-eccSquared*sin(phi1Rad)*sin(phi1Rad), 1.5);
398         D = x/(N1*k0);
399
400         Lat = phi1Rad - (N1*tan(phi1Rad)/R1)*(D*D/2-(5+3*T1+10*C1-4*C1*C1-9*eccPrimeSquared)*D*D*D*D/24
401                                         +(61+90*T1+298*C1+45*T1*T1-252*eccPrimeSquared-3*C1*C1)*D*D*D*D*D*D/720);
402         Lat = Lat * rad2deg;
403
404         Long = (D-(1+2*T1+C1)*D*D*D/6+(5-2*C1+28*T1-3*C1*C1+8*eccPrimeSquared+24*T1*T1)
405                                         *D*D*D*D*D/120)/cos(phi1Rad);
406         Long = LongOrigin + Long * rad2deg;
407
408         geo->lat=Lat;
409         geo->lng=Long;
410 }
411
412 void
413 transform_datum(struct coord_geo *from, enum map_datum from_datum, struct coord_geo *to, enum map_datum to_datum)
414 {
415 }
416
417 int
418 transform(struct transformation *t, enum projection pro, struct coord *c, struct point *p, int count, int mindist, int width, int *width_return)
419 {
420         struct coord c1;
421         int xcn, ycn; 
422         struct coord_geo g;
423         int xc, yc, zc=0, xco=0, yco=0, zco=0;
424         int xm,ym,zct;
425         int zlimit=t->znear;
426         int visible, visibleo=-1;
427         int i,j = 0,k=0;
428         dbg(1,"count=%d\n", count);
429         for (i=0; i < count; i++) {
430                 if (pro == t->pro) {
431                         xc=c[i].x;
432                         yc=c[i].y;
433                 } else {
434                         transform_to_geo(pro, &c[i], &g);
435                         transform_from_geo(t->pro, &g, &c1);
436                         xc=c1.x;
437                         yc=c1.y;
438                 }
439                 if (i != 0 && i != count-1 && mindist) {
440                         if (xc > c[k].x-mindist && xc < c[k].x+mindist && yc > c[k].y-mindist && yc < c[k].y+mindist &&
441                                 (c[i+1].x != c[0].x || c[i+1].y != c[0].y))
442                                 continue;
443                         k=i;
444                 }
445                 xm=xc;
446                 ym=yc;
447 //              dbg(2,"0x%x, 0x%x - 0x%x,0x%x contains 0x%x,0x%x\n", t->r.lu.x, t->r.lu.y, t->r.rl.x, t->r.rl.y, c->x, c->y);
448 //              ret=coord_rect_contains(&t->r, c);
449                 xc-=t->map_center.x;
450                 yc-=t->map_center.y;
451                 xc >>= t->scale_shift;
452                 yc >>= t->scale_shift;
453                 xm=xc;
454                 ym=yc;
455
456                 xcn=xc*t->m00+yc*t->m01+HOG(*t)*t->m02;
457                 ycn=xc*t->m10+yc*t->m11+HOG(*t)*t->m12;
458
459                 if (t->ddd) {
460                         zc=(xc*t->m20+yc*t->m21+HOG(*t)*t->m22);
461                         zct=zc;
462                         zc+=t->offz << POST_SHIFT;
463                         dbg(1,"zc=%d\n", zc);
464                         dbg(1,"zc(%d)=xc(%d)*m20(%d)+yc(%d)*m21(%d)\n", (xc*t->m20+yc*t->m21), xc, t->m20, yc, t->m21);
465                         /* visibility */
466                         visible=(zc < zlimit ? 0:1);
467                         dbg(1,"visible=%d old %d\n", visible, visibleo);
468                         if (visible != visibleo && visibleo != -1) { 
469                                 dbg(1,"clipping (%d,%d,%d)-(%d,%d,%d) (%d,%d,%d)\n", xcn, ycn, zc, xco, yco, zco, xco-xcn, yco-ycn, zco-zc);
470                                 if (zco != zc) {
471                                         xcn=xcn+(long long)(xco-xcn)*(zlimit-zc)/(zco-zc);
472                                         ycn=ycn+(long long)(yco-ycn)*(zlimit-zc)/(zco-zc);
473                                 }
474                                 dbg(1,"result (%d,%d,%d) * %d / %d\n", xcn,ycn,zc,zlimit-zc,zco-zc);
475                                 zc=zlimit;
476                                 xco=xcn;
477                                 yco=ycn;
478                                 zco=zc;
479                                 if (visible)
480                                         i--;
481                                 visibleo=visible;
482                         } else {
483                                 xco=xcn;
484                                 yco=ycn;
485                                 zco=zc;
486                                 visibleo=visible;
487                                 if (! visible)
488                                         continue;
489                         }
490                         dbg(1,"zc=%d\n", zc);
491                         dbg(1,"xcn %d ycn %d\n", xcn, ycn);
492                         dbg(1,"%d,%d %d\n",xc,yc,zc);
493 #if 0
494                         dbg(0,"%d/%d=%d %d/%d=%d\n",xcn,xc,xcn/xc,ycn,yc,ycn/yc);
495 #endif
496 #if 1
497                         xc=(long long)xcn*t->xscale/zc;
498                         yc=(long long)ycn*t->yscale/zc;
499 #else
500                         xc=xcn/(1000+zc);
501                         yc=ycn/(1000+zc);
502 #endif
503 #if 0
504                         dbg(1,"%d,%d %d\n",xc,yc,zc);
505 #endif
506                 } else {
507                         xc=xcn;
508                         yc=ycn;
509                         xc>>=POST_SHIFT;
510                         yc>>=POST_SHIFT;
511                 }
512                 xc+=t->offx;
513                 yc+=t->offy;
514                 p[j].x=xc;
515                 p[j].y=yc;
516                 if (width_return) {
517                         if (t->ddd) 
518                                 width_return[j]=width*t->wscale/zc;
519                         else 
520                                 width_return[j]=width;
521                 }
522                 j++;
523         }
524         return j;
525 }
526
527 static void
528 transform_apply_inverse_matrix(struct transformation *t, struct coord_geo_cart *in, struct coord_geo_cart *out)
529 {
530         out->x=in->x*t->im00+in->y*t->im01+in->z*t->im02;
531         out->y=in->x*t->im10+in->y*t->im11+in->z*t->im12;
532         out->z=in->x*t->im20+in->y*t->im21+in->z*t->im22;
533 }
534
535 static int
536 transform_zplane_intersection(struct coord_geo_cart *p1, struct coord_geo_cart *p2, navit_float z, struct coord_geo_cart *result)
537 {
538         navit_float dividend=z-p1->z;
539         navit_float divisor=p2->z-p1->z;
540         navit_float q;
541         if (!divisor) {
542                 if (dividend) 
543                         return 0;       /* no intersection */
544                 else
545                         return 3;       /* identical planes */
546         }
547         q=dividend/divisor;
548         result->x=p1->x+q*(p2->x-p1->x);
549         result->y=p1->y+q*(p2->y-p1->y);
550         result->z=z;
551         if (q >= 0 && q <= 1)
552                 return 1;       /* intersection within [p1,p2] */
553         return 2; /* intersection without [p1,p2] */
554 }
555
556 static void
557 transform_screen_to_3d(struct transformation *t, struct point *p, navit_float z, struct coord_geo_cart *cg)
558 {
559         double xc,yc;
560         double offz=t->offz << POST_SHIFT;
561         xc=p->x - t->offx;
562         yc=p->y - t->offy;
563         cg->x=xc*z/t->xscale;
564         cg->y=yc*z/t->yscale;
565         cg->z=z-offz;
566 }
567
568 static int
569 transform_reverse_near_far(struct transformation *t, struct point *p, struct coord *c, int near, int far)
570 {
571         double xc,yc;
572         dbg(1,"%d,%d\n",p->x,p->y);
573         if (t->ddd) {
574                 struct coord_geo_cart nearc,farc,nears,fars,intersection;
575                 transform_screen_to_3d(t, p, near, &nearc);     
576                 transform_screen_to_3d(t, p, far, &farc);
577                 transform_apply_inverse_matrix(t, &nearc, &nears);
578                 transform_apply_inverse_matrix(t, &farc, &fars);
579                 if (transform_zplane_intersection(&nears, &fars, HOG(*t), &intersection) != 1)
580                         return 0;
581                 xc=intersection.x;
582                 yc=intersection.y;
583         } else {
584                 double xcn,ycn;
585                 xcn=p->x - t->offx;
586                 ycn=p->y - t->offy;
587                 xc=(xcn*t->im00+ycn*t->im01)*(1 << POST_SHIFT);
588                 yc=(xcn*t->im10+ycn*t->im11)*(1 << POST_SHIFT);
589         }
590         c->x=xc*(1 << t->scale_shift)+t->map_center.x;
591         c->y=yc*(1 << t->scale_shift)+t->map_center.y;
592         return 1;
593 }
594
595 int
596 transform_reverse(struct transformation *t, struct point *p, struct coord *c)
597 {
598         return transform_reverse_near_far(t, p, c, t->znear, t->zfar);
599 }
600
601 enum projection
602 transform_get_projection(struct transformation *this_)
603 {
604         return this_->pro;
605 }
606
607 void
608 transform_set_projection(struct transformation *this_, enum projection pro)
609 {
610         this_->pro=pro;
611 }
612
613 static int
614 min4(int v1,int v2, int v3, int v4)
615 {
616         int res=v1;
617         if (v2 < res)
618                 res=v2;
619         if (v3 < res)
620                 res=v3;
621         if (v4 < res)
622                 res=v4;
623         return res;
624 }
625
626 static int
627 max4(int v1,int v2, int v3, int v4)
628 {
629         int res=v1;
630         if (v2 > res)
631                 res=v2;
632         if (v3 > res)
633                 res=v3;
634         if (v4 > res)
635                 res=v4;
636         return res;
637 }
638
639 struct map_selection *
640 transform_get_selection(struct transformation *this_, enum projection pro, int order)
641 {
642
643         struct map_selection *ret,*curri,*curro;
644         struct coord_geo g;
645         
646         ret=map_selection_dup(this_->map_sel);
647         curri=this_->map_sel;
648         curro=ret;
649         while (curri) {
650                 if (this_->pro != pro) {
651                         transform_to_geo(this_->pro, &curri->u.c_rect.lu, &g);
652                         transform_from_geo(pro, &g, &curro->u.c_rect.lu);
653                         dbg(1,"%f,%f", g.lat, g.lng);
654                         transform_to_geo(this_->pro, &curri->u.c_rect.rl, &g);
655                         transform_from_geo(pro, &g, &curro->u.c_rect.rl);
656                         dbg(1,": - %f,%f\n", g.lat, g.lng);
657                 }
658                 dbg(1,"transform rect for %d is %d,%d - %d,%d\n", pro, curro->u.c_rect.lu.x, curro->u.c_rect.lu.y, curro->u.c_rect.rl.x, curro->u.c_rect.rl.y);
659                 curro->order+=order;
660 #if 0
661                 curro->u.c_rect.lu.x-=500;
662                 curro->u.c_rect.lu.y+=500;
663                 curro->u.c_rect.rl.x+=500;
664                 curro->u.c_rect.rl.y-=500;
665 #endif
666                 curro->range=item_range_all;
667                 curri=curri->next;
668                 curro=curro->next;
669         }
670         return ret;
671 }
672
673 struct coord *
674 transform_center(struct transformation *this_)
675 {
676         return &this_->map_center;
677 }
678
679 struct coord *
680 transform_get_center(struct transformation *this_)
681 {
682         return &this_->map_center;
683 }
684
685 void
686 transform_set_center(struct transformation *this_, struct coord *c)
687 {
688         this_->map_center=*c;
689 }
690
691
692 void
693 transform_set_yaw(struct transformation *t,int yaw)
694 {
695         t->yaw=yaw;
696         transform_setup_matrix(t);
697 }
698
699 int
700 transform_get_yaw(struct transformation *this_)
701 {
702         return this_->yaw;
703 }
704
705 void
706 transform_set_pitch(struct transformation *this_,int pitch)
707 {
708         this_->pitch=pitch;
709         transform_setup_matrix(this_);
710 }
711 int
712 transform_get_pitch(struct transformation *this_)
713 {
714         return this_->pitch;
715 }
716
717 void
718 transform_set_roll(struct transformation *this_,int roll)
719 {
720 #ifdef ENABLE_ROLL
721         this_->roll=roll;
722         transform_setup_matrix(this_);
723 #else
724         dbg(0,"not supported\n");
725 #endif
726 }
727
728 int
729 transform_get_roll(struct transformation *this_)
730 {
731 #ifdef ENABLE_ROLL
732         return this_->roll;
733 #else
734         return 0;
735 #endif
736 }
737
738 void
739 transform_set_distance(struct transformation *this_,int distance)
740 {
741         transform_set_screen_dist(this_, distance);
742         transform_setup_matrix(this_);
743 }
744
745 int
746 transform_get_distance(struct transformation *this_)
747 {
748         return this_->screen_dist;
749 }
750
751 void
752 transform_set_scales(struct transformation *this_, int xscale, int yscale, int wscale)
753 {
754         this_->xscale3d=xscale;
755         this_->yscale3d=yscale;
756         this_->wscale3d=wscale;
757 }
758
759 void
760 transform_set_screen_selection(struct transformation *t, struct map_selection *sel)
761 {
762         map_selection_destroy(t->screen_sel);
763         t->screen_sel=map_selection_dup(sel);
764         if (sel) {
765                 t->screen_center.x=(sel->u.p_rect.rl.x-sel->u.p_rect.lu.x)/2;
766                 t->screen_center.y=(sel->u.p_rect.rl.y-sel->u.p_rect.lu.y)/2;
767                 transform_setup_matrix(t);
768         }
769 }
770
771 void
772 transform_set_screen_center(struct transformation *t, struct point *p)
773 {
774         t->screen_center=*p;
775 }
776
777 #if 0
778 void
779 transform_set_size(struct transformation *t, int width, int height)
780 {
781         t->width=width;
782         t->height=height;
783 }
784 #endif
785
786 void
787 transform_get_size(struct transformation *t, int *width, int *height)
788 {
789         struct point_rect *r;
790         if (t->screen_sel) {
791                 r=&t->screen_sel->u.p_rect;
792                 *width=r->rl.x-r->lu.x;
793                 *height=r->rl.y-r->lu.y;
794         }
795 }
796
797 void
798 transform_setup(struct transformation *t, struct pcoord *c, int scale, int yaw)
799 {
800         t->pro=c->pro;
801         t->map_center.x=c->x;
802         t->map_center.y=c->y;
803         t->scale=scale/16.0;
804         transform_set_yaw(t, yaw);
805 }
806
807 #if 0
808
809 void
810 transform_setup_source_rect_limit(struct transformation *t, struct coord *center, int limit)
811 {
812         t->center=*center;
813         t->scale=1;
814         t->angle=0;
815         t->r.lu.x=center->x-limit;
816         t->r.rl.x=center->x+limit;
817         t->r.rl.y=center->y-limit;
818         t->r.lu.y=center->y+limit;
819 }
820 #endif
821
822 void
823 transform_setup_source_rect(struct transformation *t)
824 {
825         int i;
826         struct coord screen[4];
827         struct point screen_pnt[4];
828         struct point_rect *pr;
829         struct map_selection *ms,*msm,*next,**msm_last;
830         ms=t->map_sel;
831         while (ms) {
832                 next=ms->next;
833                 g_free(ms);
834                 ms=next;
835         }
836         t->map_sel=NULL;
837         msm_last=&t->map_sel;
838         ms=t->screen_sel;
839         while (ms) {
840                 msm=g_new0(struct map_selection, 1);
841                 *msm=*ms;
842                 pr=&ms->u.p_rect;
843                 screen_pnt[0].x=pr->lu.x;       /* left upper */
844                 screen_pnt[0].y=pr->lu.y;
845                 screen_pnt[1].x=pr->rl.x;       /* right upper */
846                 screen_pnt[1].y=pr->lu.y;
847                 screen_pnt[2].x=pr->rl.x;       /* right lower */
848                 screen_pnt[2].y=pr->rl.y;
849                 screen_pnt[3].x=pr->lu.x;       /* left lower */
850                 screen_pnt[3].y=pr->rl.y;
851                 if (t->ddd) {
852                         struct coord_geo_cart tmp,cg[8];
853                         struct coord c;
854                         int valid=0;
855                         unsigned char edgenodes[]={
856                                 0,1,
857                                 1,2,
858                                 2,3,
859                                 3,0,
860                                 4,5,
861                                 5,6,
862                                 6,7,
863                                 7,4,
864                                 0,4,
865                                 1,5,
866                                 2,6,
867                                 3,7};   
868                         for (i = 0 ; i < 8 ; i++) {
869                                 transform_screen_to_3d(t, &screen_pnt[i%4], (i >= 4 ? t->zfar:t->znear), &tmp);
870                                 transform_apply_inverse_matrix(t, &tmp, &cg[i]);
871                         }
872                         msm->u.c_rect.lu.x=0;
873                         msm->u.c_rect.lu.y=0;
874                         msm->u.c_rect.rl.x=0;
875                         msm->u.c_rect.rl.y=0;
876                         for (i = 0 ; i < 12 ; i++) {
877                                 if (transform_zplane_intersection(&cg[edgenodes[i*2]], &cg[edgenodes[i*2+1]], HOG(*t), &tmp) == 1) {
878                                         c.x=tmp.x*(1 << t->scale_shift)+t->map_center.x;
879                                         c.y=tmp.y*(1 << t->scale_shift)+t->map_center.y;
880                                         dbg(1,"intersection with edge %d at 0x%x,0x%x\n",i,c.x,c.y);
881                                         if (valid)
882                                                 coord_rect_extend(&msm->u.c_rect, &c);
883                                         else {
884                                                 msm->u.c_rect.lu=c;
885                                                 msm->u.c_rect.rl=c;
886                                                 valid=1;
887                                         }
888                                         dbg(1,"rect 0x%x,0x%x - 0x%x,0x%x\n",msm->u.c_rect.lu.x,msm->u.c_rect.lu.y,msm->u.c_rect.rl.x,msm->u.c_rect.rl.y);
889                                 }
890                         }
891                 } else {
892                         for (i = 0 ; i < 4 ; i++) {
893                                 transform_reverse(t, &screen_pnt[i], &screen[i]);
894                                 dbg(1,"map(%d) %d,%d=0x%x,0x%x\n", i,screen_pnt[i].x, screen_pnt[i].y, screen[i].x, screen[i].y);
895                         }
896                         msm->u.c_rect.lu.x=min4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
897                         msm->u.c_rect.rl.x=max4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
898                         msm->u.c_rect.rl.y=min4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
899                         msm->u.c_rect.lu.y=max4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
900                 }
901                 dbg(1,"%dx%d\n", msm->u.c_rect.rl.x-msm->u.c_rect.lu.x,
902                                  msm->u.c_rect.lu.y-msm->u.c_rect.rl.y);
903                 *msm_last=msm;
904                 msm_last=&msm->next;
905                 ms=ms->next;
906         }
907 }
908
909 long
910 transform_get_scale(struct transformation *t)
911 {
912         return (int)(t->scale*16);
913 }
914
915 void
916 transform_set_scale(struct transformation *t, long scale)
917 {
918         t->scale=scale/16.0;
919         transform_setup_matrix(t);
920 }
921
922
923 int
924 transform_get_order(struct transformation *t)
925 {
926         dbg(1,"order %d\n", t->order);
927         return t->order;
928 }
929
930
931
932 #define TWOPI (M_PI*2)
933 #define GC2RAD(c) ((c) * TWOPI/(1<<24))
934 #define minf(a,b) ((a) < (b) ? (a) : (b))
935
936 static double
937 transform_distance_garmin(struct coord *c1, struct coord *c2)
938 {
939 #ifdef USE_HALVESINE
940         static const int earth_radius = 6371*1000; //m change accordingly
941 // static const int earth_radius  = 3960; //miles
942  
943 //Point 1 cords
944         navit_float lat1  = GC2RAD(c1->y);
945         navit_float long1 = GC2RAD(c1->x);
946
947 //Point 2 cords
948         navit_float lat2  = GC2RAD(c2->y);
949         navit_float long2 = GC2RAD(c2->x);
950
951 //Haversine Formula
952         navit_float dlong = long2-long1;
953         navit_float dlat  = lat2-lat1;
954
955         navit_float sinlat  = navit_sin(dlat/2);
956         navit_float sinlong = navit_sin(dlong/2);
957  
958         navit_float a=(sinlat*sinlat)+navit_cos(lat1)*navit_cos(lat2)*(sinlong*sinlong);
959         navit_float c=2*navit_asin(minf(1,navit_sqrt(a)));
960 #ifdef AVOID_FLOAT
961         return round(earth_radius*c);
962 #else
963         return earth_radius*c;
964 #endif
965 #else
966 #define GMETER 2.3887499999999999
967         navit_float dx,dy;
968         dx=c1->x-c2->x;
969         dy=c1->y-c2->y;
970         return navit_sqrt(dx*dx+dy*dy)*GMETER;
971 #undef GMETER
972 #endif
973 }
974
975 double
976 transform_scale(int y)
977 {
978         struct coord c;
979         struct coord_geo g;
980         c.x=0;
981         c.y=y;
982         transform_to_geo(projection_mg, &c, &g);
983         return 1/navit_cos(g.lat/180*M_PI);
984 }
985
986 #ifdef AVOID_FLOAT
987 static int
988 tab_sqrt[]={14142,13379,12806,12364,12018,11741,11517,11333,11180,11051,10943,10850,10770,10701,10640,10587,10540,10499,10462,10429,10400,10373,10349,10327,10307,10289,10273,10257,10243,10231,10219,10208};
989
990 static int tab_int_step = 0x20000;
991 static int tab_int_scale[]={10000,10002,10008,10019,10033,10052,10076,10103,10135,10171,10212,10257,10306,10359,10417,10479,10546,10617,10693,10773,10858,10947,11041,11140,11243,11352,11465,11582,11705,11833,11965,12103,12246,12394,12547,12706,12870,13039,13214,13395,13581,13773,13971,14174,14384,14600,14822,15050,15285,15526,15774,16028,16289,16557,16832,17114,17404,17700,18005,18316,18636,18964,19299,19643,19995,20355,20724,21102,21489,21885,22290,22705,23129,23563,24007,24461,24926,25401,25886,26383,26891};
992
993 int transform_int_scale(int y)
994 {
995         int i,size = sizeof(tab_int_scale)/sizeof(int);
996         if (y < 0)
997                 y=-y;
998         i=y/tab_int_step;
999         if (i < size-1) 
1000                 return tab_int_scale[i]+((tab_int_scale[i+1]-tab_int_scale[i])*(y-i*tab_int_step))/tab_int_step;
1001         return tab_int_scale[size-1];
1002 }
1003 #endif
1004
1005 double
1006 transform_distance(enum projection pro, struct coord *c1, struct coord *c2)
1007 {
1008         if (pro == projection_mg) {
1009 #ifndef AVOID_FLOAT 
1010         double dx,dy,scale=transform_scale((c1->y+c2->y)/2);
1011         dx=c1->x-c2->x;
1012         dy=c1->y-c2->y;
1013         return sqrt(dx*dx+dy*dy)/scale;
1014 #else
1015         int dx,dy,f,scale=transform_int_scale((c1->y+c2->y)/2);
1016         dx=c1->x-c2->x;
1017         dy=c1->y-c2->y;
1018         if (dx < 0)
1019                 dx=-dx;
1020         if (dy < 0)
1021                 dy=-dy;
1022         while (dx > 20000 || dy > 20000) {
1023                 dx/=10;
1024                 dy/=10;
1025                 scale/=10;
1026         }
1027         if (! dy)
1028                 return dx*10000/scale;
1029         if (! dx)
1030                 return dy*10000/scale;
1031         if (dx > dy) {
1032                 f=dx*8/dy-8;
1033                 if (f >= 32)
1034                         return dx*10000/scale;
1035                 return dx*tab_sqrt[f]/scale;
1036         } else {
1037                 f=dy*8/dx-8;
1038                 if (f >= 32)
1039                         return dy*10000/scale;
1040                 return dy*tab_sqrt[f]/scale;
1041         }
1042 #endif
1043         } else if (pro == projection_garmin) {
1044                 return transform_distance_garmin(c1, c2);
1045         } else {
1046                 dbg(0,"Unknown projection: %d\n", pro);
1047                 return 0;
1048         }
1049 }
1050
1051 void
1052 transform_project(enum projection pro, struct coord *c, int distance, int angle, struct coord *res)
1053 {
1054         double scale;
1055         switch (pro) {
1056         case projection_mg:
1057                 scale=transform_scale(c->y);
1058                 res->x=c->x+distance*sin(angle*M_PI/180)*scale;
1059                 res->y=c->y+distance*cos(angle*M_PI/180)*scale;
1060                 break;
1061         default:
1062                 dbg(0,"Unsupported projection: %d\n", pro);
1063                 return;
1064         }
1065         
1066 }
1067
1068
1069 double
1070 transform_polyline_length(enum projection pro, struct coord *c, int count)
1071 {
1072         double ret=0;
1073         int i;
1074
1075         for (i = 0 ; i < count-1 ; i++) 
1076                 ret+=transform_distance(pro, &c[i], &c[i+1]);
1077         return ret;
1078 }
1079
1080 int
1081 transform_distance_sq(struct coord *c1, struct coord *c2)
1082 {
1083         int dx=c1->x-c2->x;
1084         int dy=c1->y-c2->y;
1085
1086         if (dx > 32767 || dy > 32767 || dx < -32767 || dy < -32767)
1087                 return INT_MAX;
1088         else
1089                 return dx*dx+dy*dy;
1090 }
1091
1092 navit_float
1093 transform_distance_sq_float(struct coord *c1, struct coord *c2)
1094 {
1095         int dx=c1->x-c2->x;
1096         int dy=c1->y-c2->y;
1097         return (navit_float)dx*dx+dy*dy;
1098 }
1099
1100 int
1101 transform_distance_sq_pc(struct pcoord *c1, struct pcoord *c2)
1102 {
1103         struct coord p1,p2;
1104         p1.x = c1->x; p1.y = c1->y;
1105         p2.x = c2->x; p2.y = c2->y;
1106         return transform_distance_sq(&p1, &p2);
1107 }
1108
1109 int
1110 transform_distance_line_sq(struct coord *l0, struct coord *l1, struct coord *ref, struct coord *lpnt)
1111 {
1112         int vx,vy,wx,wy;
1113         int c1,c2;
1114         int climit=1000000;
1115         struct coord l;
1116
1117         vx=l1->x-l0->x;
1118         vy=l1->y-l0->y;
1119         wx=ref->x-l0->x;
1120         wy=ref->y-l0->y;
1121
1122         c1=vx*wx+vy*wy;
1123         if ( c1 <= 0 ) {
1124                 if (lpnt)
1125                         *lpnt=*l0;
1126                 return transform_distance_sq(l0, ref);
1127         }
1128         c2=vx*vx+vy*vy;
1129         if ( c2 <= c1 ) {
1130                 if (lpnt)
1131                         *lpnt=*l1;
1132                 return transform_distance_sq(l1, ref);
1133         }
1134         while (c1 > climit || c2 > climit) {
1135                 c1/=256;
1136                 c2/=256;
1137         }
1138         l.x=l0->x+vx*c1/c2;
1139         l.y=l0->y+vy*c1/c2;
1140         if (lpnt)
1141                 *lpnt=l;
1142         return transform_distance_sq(&l, ref);
1143 }
1144
1145 navit_float
1146 transform_distance_line_sq_float(struct coord *l0, struct coord *l1, struct coord *ref, struct coord *lpnt)
1147 {
1148         navit_float vx,vy,wx,wy;
1149         navit_float c1,c2;
1150         struct coord l;
1151
1152         vx=l1->x-l0->x;
1153         vy=l1->y-l0->y;
1154         wx=ref->x-l0->x;
1155         wy=ref->y-l0->y;
1156
1157         c1=vx*wx+vy*wy;
1158         if ( c1 <= 0 ) {
1159                 if (lpnt)
1160                         *lpnt=*l0;
1161                 return transform_distance_sq_float(l0, ref);
1162         }
1163         c2=vx*vx+vy*vy;
1164         if ( c2 <= c1 ) {
1165                 if (lpnt)
1166                         *lpnt=*l1;
1167                 return transform_distance_sq_float(l1, ref);
1168         }
1169         l.x=l0->x+vx*c1/c2;
1170         l.y=l0->y+vy*c1/c2;
1171         if (lpnt)
1172                 *lpnt=l;
1173         return transform_distance_sq_float(&l, ref);
1174 }
1175
1176 int
1177 transform_distance_polyline_sq(struct coord *c, int count, struct coord *ref, struct coord *lpnt, int *pos)
1178 {
1179         int i,dist,distn;
1180         struct coord lp;
1181         if (count < 2)
1182                 return INT_MAX;
1183         if (pos)
1184                 *pos=0;
1185         dist=transform_distance_line_sq(&c[0], &c[1], ref, lpnt);
1186         for (i=2 ; i < count ; i++) {
1187                 distn=transform_distance_line_sq(&c[i-1], &c[i], ref, &lp);
1188                 if (distn < dist) {
1189                         dist=distn;
1190                         if (lpnt)
1191                                 *lpnt=lp;
1192                         if (pos)
1193                                 *pos=i-1;
1194                 }
1195         }
1196         return dist;
1197 }
1198
1199 int
1200 transform_douglas_peucker(struct coord *in, int count, int dist_sq, struct coord *out)
1201 {
1202         int ret=0;
1203         int i,d,dmax=0, idx=0;
1204         for (i = 1; i < count-2 ; i++) {
1205                 d=transform_distance_line_sq(&in[0], &in[count-1], &in[i], NULL);
1206                 if (d > dmax) {
1207                         idx=i;
1208                         dmax=d;
1209                 }
1210         }
1211         if (dmax > dist_sq) {
1212                 ret=transform_douglas_peucker(in, idx, dist_sq, out)-1;
1213                 ret+=transform_douglas_peucker(in+idx, count-idx, dist_sq, out+ret);
1214         } else {
1215                 if (count > 0)
1216                         out[ret++]=in[0];
1217                 if (count > 1)
1218                         out[ret++]=in[count-1];
1219         }
1220         return ret;
1221 }
1222
1223 int
1224 transform_douglas_peucker_float(struct coord *in, int count, navit_float dist_sq, struct coord *out)
1225 {
1226         int ret=0;
1227         int i,idx=0;
1228         navit_float d,dmax=0;
1229         for (i = 1; i < count-2 ; i++) {
1230                 d=transform_distance_line_sq_float(&in[0], &in[count-1], &in[i], NULL);
1231                 if (d > dmax) {
1232                         idx=i;
1233                         dmax=d;
1234                 }
1235         }
1236         if (dmax > dist_sq) {
1237                 ret=transform_douglas_peucker_float(in, idx, dist_sq, out)-1;
1238                 ret+=transform_douglas_peucker_float(in+idx, count-idx, dist_sq, out+ret);
1239         } else {
1240                 if (count > 0)
1241                         out[ret++]=in[0];
1242                 if (count > 1)
1243                         out[ret++]=in[count-1];
1244         }
1245         return ret;
1246 }
1247
1248
1249 void
1250 transform_print_deg(double deg)
1251 {
1252         printf("%2.0f:%2.0f:%2.4f", floor(deg), fmod(deg*60,60), fmod(deg*3600,60));
1253 }
1254
1255 #ifdef AVOID_FLOAT
1256 static int tab_atan[]={0,262,524,787,1051,1317,1584,1853,2126,2401,2679,2962,3249,3541,3839,4142,4452,4770,5095,5430,5774,6128,6494,6873,7265,7673,8098,8541,9004,9490,10000,10538};
1257
1258 static int
1259 atan2_int_lookup(int val)
1260 {
1261         int len=sizeof(tab_atan)/sizeof(int);
1262         int i=len/2;
1263         int p=i-1;
1264         for (;;) {
1265                 i>>=1;
1266                 if (val < tab_atan[p])
1267                         p-=i;
1268                 else
1269                         if (val < tab_atan[p+1])
1270                                 return p+(p>>1);
1271                         else
1272                                 p+=i;
1273         }
1274 }
1275
1276 static int
1277 atan2_int(int dx, int dy)
1278 {
1279         int mul=1,add=0,ret;
1280         if (! dx) {
1281                 return dy < 0 ? 180 : 0;
1282         }
1283         if (! dy) {
1284                 return dx < 0 ? -90 : 90;
1285         }
1286         if (dx < 0) {
1287                 dx=-dx;
1288                 mul=-1;
1289         }
1290         if (dy < 0) {
1291                 dy=-dy;
1292                 add=180*mul;
1293                 mul*=-1;
1294         }
1295         while (dx > 20000 || dy > 20000) {
1296                 dx/=10;
1297                 dy/=10;
1298         }
1299         if (dx > dy) {
1300                 ret=90-atan2_int_lookup(dy*10000/dx);
1301         } else {
1302                 ret=atan2_int_lookup(dx*10000/dy);
1303         }
1304         return ret*mul+add;
1305 }
1306 #endif
1307
1308 int
1309 transform_get_angle_delta(struct coord *c1, struct coord *c2, int dir)
1310 {
1311         int dx=c2->x-c1->x;
1312         int dy=c2->y-c1->y;
1313 #ifndef AVOID_FLOAT 
1314         double angle;
1315         angle=atan2(dx,dy);
1316         angle*=180/M_PI;
1317 #else
1318         int angle;
1319         angle=atan2_int(dx,dy);
1320 #endif
1321         if (dir == -1)
1322                 angle=angle-180;
1323         if (angle < 0)
1324                 angle+=360;
1325         return angle;
1326 }
1327
1328 int
1329 transform_within_border(struct transformation *this_, struct point *p, int border)
1330 {
1331         struct map_selection *ms=this_->screen_sel;
1332         while (ms) {
1333                 struct point_rect *r=&ms->u.p_rect;
1334                 if (p->x >= r->lu.x+border && p->x <= r->rl.x-border &&
1335                     p->y >= r->lu.y+border && p->y <= r->rl.y-border)
1336                         return 1;
1337                 ms=ms->next;
1338         }
1339         return 0;
1340 }
1341
1342 int
1343 transform_within_dist_point(struct coord *ref, struct coord *c, int dist)
1344 {
1345         if (c->x-dist > ref->x)
1346                 return 0;
1347         if (c->x+dist < ref->x)
1348                 return 0;
1349         if (c->y-dist > ref->y)
1350                 return 0;
1351         if (c->y+dist < ref->y)
1352                 return 0;
1353         if ((c->x-ref->x)*(c->x-ref->x) + (c->y-ref->y)*(c->y-ref->y) <= dist*dist) 
1354                 return 1;
1355         return 0;
1356 }
1357
1358 int
1359 transform_within_dist_line(struct coord *ref, struct coord *c0, struct coord *c1, int dist)
1360 {
1361         int vx,vy,wx,wy;
1362         int n1,n2;
1363         struct coord lc;
1364
1365         if (c0->x < c1->x) {
1366                 if (c0->x-dist > ref->x)
1367                         return 0;
1368                 if (c1->x+dist < ref->x)
1369                         return 0;
1370         } else {
1371                 if (c1->x-dist > ref->x)
1372                         return 0;
1373                 if (c0->x+dist < ref->x)
1374                         return 0;
1375         }
1376         if (c0->y < c1->y) {
1377                 if (c0->y-dist > ref->y)
1378                         return 0;
1379                 if (c1->y+dist < ref->y)
1380                         return 0;
1381         } else {
1382                 if (c1->y-dist > ref->y)
1383                         return 0;
1384                 if (c0->y+dist < ref->y)
1385                         return 0;
1386         }
1387         vx=c1->x-c0->x;
1388         vy=c1->y-c0->y;
1389         wx=ref->x-c0->x;
1390         wy=ref->y-c0->y;
1391
1392         n1=vx*wx+vy*wy;
1393         if ( n1 <= 0 )
1394                 return transform_within_dist_point(ref, c0, dist);
1395         n2=vx*vx+vy*vy;
1396         if ( n2 <= n1 )
1397                 return transform_within_dist_point(ref, c1, dist);
1398
1399         lc.x=c0->x+vx*n1/n2;
1400         lc.y=c0->y+vy*n1/n2;
1401         return transform_within_dist_point(ref, &lc, dist);
1402 }
1403
1404 int
1405 transform_within_dist_polyline(struct coord *ref, struct coord *c, int count, int close, int dist)
1406 {
1407         int i;
1408         for (i = 0 ; i < count-1 ; i++) {
1409                 if (transform_within_dist_line(ref,c+i,c+i+1,dist)) {
1410                         return 1;
1411                 }
1412         }
1413         if (close)
1414                 return (transform_within_dist_line(ref,c,c+count-1,dist));
1415         return 0;
1416 }
1417
1418 int
1419 transform_within_dist_polygon(struct coord *ref, struct coord *c, int count, int dist)
1420 {
1421         int i, j, ci = 0;
1422         for (i = 0, j = count-1; i < count; j = i++) {
1423                 if ((((c[i].y <= ref->y) && ( ref->y < c[j].y )) ||
1424                 ((c[j].y <= ref->y) && ( ref->y < c[i].y))) &&
1425                 (ref->x < (c[j].x - c[i].x) * (ref->y - c[i].y) / (c[j].y - c[i].y) + c[i].x))
1426                         ci = !ci;
1427         }
1428         if (! ci) {
1429                 if (dist)
1430                         return transform_within_dist_polyline(ref, c, count, dist, 1);
1431                 else
1432                         return 0;
1433         }
1434         return 1;
1435 }
1436
1437 int
1438 transform_within_dist_item(struct coord *ref, enum item_type type, struct coord *c, int count, int dist)
1439 {
1440         if (type < type_line)
1441                 return transform_within_dist_point(ref, c, dist);
1442         if (type < type_area)
1443                 return transform_within_dist_polyline(ref, c, count, 0, dist);
1444         return transform_within_dist_polygon(ref, c, count, dist);
1445 }
1446
1447 void
1448 transform_copy(struct transformation *src, struct transformation *dst)
1449 {
1450         memcpy(dst, src, sizeof(*src));
1451 }
1452
1453 void
1454 transform_destroy(struct transformation *t)
1455 {
1456         g_free(t);
1457 }
1458
1459
1460 /*
1461 Note: there are many mathematically equivalent ways to express these formulas. As usual, not all of them are computationally equivalent.
1462
1463 L = latitude in radians (positive north)
1464 Lo = longitude in radians (positive east)
1465 E = easting (meters)
1466 N = northing (meters)
1467
1468 For the sphere
1469
1470 E = r Lo
1471 N = r ln [ tan (pi/4 + L/2) ]
1472
1473 where 
1474
1475 r = radius of the sphere (meters)
1476 ln() is the natural logarithm
1477
1478 For the ellipsoid
1479
1480 E = a Lo
1481 N = a * ln ( tan (pi/4 + L/2) * ( (1 - e * sin (L)) / (1 + e * sin (L))) ** (e/2)  )
1482
1483
1484                                                e
1485                                                -
1486                    pi     L     1 - e sin(L)   2
1487     =  a ln( tan( ---- + ---) (--------------)   )
1488                    4      2     1 + e sin(L) 
1489
1490
1491 where
1492
1493 a = the length of the semi-major axis of the ellipsoid (meters)
1494 e = the first eccentricity of the ellipsoid
1495
1496
1497 */
1498
1499