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