Fix:core:Reduce debug output, use dbg() instead of printf.
[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         ret->map_sel=map_selection_dup(t->map_sel);
252         ret->screen_sel=map_selection_dup(t->screen_sel);
253         return ret;
254 }
255
256 static const navit_float gar2geo_units = 360.0/(1<<24);
257 static const navit_float geo2gar_units = 1/(360.0/(1<<24));
258
259 void
260 transform_to_geo(enum projection pro, struct coord *c, struct coord_geo *g)
261 {
262         int x,y,northern,zone;
263         switch (pro) {
264         case projection_mg:
265                 g->lng=c->x/6371000.0/M_PI*180;
266                 g->lat=navit_atan(exp(c->y/6371000.0))/M_PI*360-90;
267                 break;
268         case projection_garmin:
269                 g->lng=c->x*gar2geo_units;
270                 g->lat=c->y*gar2geo_units;
271                 break;
272         case projection_utm:
273                 x=c->x;
274                 y=c->y;
275                 northern=y >= 0;
276                 if (!northern) {
277                         y+=10000000;
278                 }
279                 zone=(x/1000000);
280                 x=x%1000000;
281                 transform_utm_to_geo(x, y, zone, northern, g);
282                 break;  
283         default:
284                 break;
285         }
286 }
287
288 void
289 transform_from_geo(enum projection pro, struct coord_geo *g, struct coord *c)
290 {
291         switch (pro) {
292         case projection_mg:
293                 c->x=g->lng*6371000.0*M_PI/180;
294                 c->y=log(navit_tan(M_PI_4+g->lat*M_PI/360))*6371000.0;
295                 break;
296         case projection_garmin:
297                 c->x=g->lng*geo2gar_units;
298                 c->y=g->lat*geo2gar_units;
299                 break;
300         default:
301                 break;
302         }
303 }
304
305 void
306 transform_from_to_count(struct coord *cfrom, enum projection from, struct coord *cto, enum projection to, int count)
307 {
308         struct coord_geo g;
309         int i;
310
311         for (i = 0 ; i < count ; i++) {
312                 transform_to_geo(from, cfrom, &g);
313                 transform_from_geo(to, &g, cto);
314                 cfrom++;
315                 cto++;
316         }
317 }
318
319 void
320 transform_from_to(struct coord *cfrom, enum projection from, struct coord *cto, enum projection to)
321 {
322         struct coord_geo g;
323         transform_to_geo(from, cfrom, &g);
324         transform_from_geo(to, &g, cto);
325 }
326
327 void
328 transform_geo_to_cart(struct coord_geo *geo, navit_float a, navit_float b, struct coord_geo_cart *cart)
329 {
330         navit_float n,ee=1-b*b/(a*a);
331         n = a/sqrtf(1-ee*navit_sin(geo->lat)*navit_sin(geo->lat));
332         cart->x=n*navit_cos(geo->lat)*navit_cos(geo->lng);
333         cart->y=n*navit_cos(geo->lat)*navit_sin(geo->lng);
334         cart->z=n*(1-ee)*navit_sin(geo->lat);
335 }
336
337 void
338 transform_cart_to_geo(struct coord_geo_cart *cart, navit_float a, navit_float b, struct coord_geo *geo)
339 {
340         navit_float lat,lati,n,ee=1-b*b/(a*a), lng = navit_tan(cart->y/cart->x);
341
342         lat = navit_tan(cart->z / navit_sqrt((cart->x * cart->x) + (cart->y * cart->y)));
343         do
344         {
345                 lati = lat;
346
347                 n = a / navit_sqrt(1-ee*navit_sin(lat)*navit_sin(lat));
348                 lat = navit_atan((cart->z + ee * n * navit_sin(lat)) / navit_sqrt(cart->x * cart->x + cart->y * cart->y));
349         }
350         while (fabs(lat - lati) >= 0.000000000000001);
351
352         geo->lng=lng/M_PI*180;
353         geo->lat=lat/M_PI*180;
354 }
355
356
357 void
358 transform_utm_to_geo(const double UTMEasting, const double UTMNorthing, int ZoneNumber, int NorthernHemisphere, struct coord_geo *geo)
359 {
360 //converts UTM coords to lat/long.  Equations from USGS Bulletin 1532 
361 //East Longitudes are positive, West longitudes are negative. 
362 //North latitudes are positive, South latitudes are negative
363 //Lat and Long are in decimal degrees. 
364         //Written by Chuck Gantz- chuck.gantz@globalstar.com
365
366         double Lat, Long;
367         double k0 = 0.99960000000000004;
368         double a = 6378137;
369         double eccSquared = 0.0066943799999999998;
370         double eccPrimeSquared;
371         double e1 = (1-sqrt(1-eccSquared))/(1+sqrt(1-eccSquared));
372         double N1, T1, C1, R1, D, M;
373         double LongOrigin;
374         double mu, phi1, phi1Rad;
375         double x, y;
376         double rad2deg = 180/M_PI;
377
378         x = UTMEasting - 500000.0; //remove 500,000 meter offset for longitude
379         y = UTMNorthing;
380
381         if (!NorthernHemisphere) {
382                 y -= 10000000.0;//remove 10,000,000 meter offset used for southern hemisphere
383         }
384
385         LongOrigin = (ZoneNumber - 1)*6 - 180 + 3;  //+3 puts origin in middle of zone
386
387         eccPrimeSquared = (eccSquared)/(1-eccSquared);
388
389         M = y / k0;
390         mu = M/(a*(1-eccSquared/4-3*eccSquared*eccSquared/64-5*eccSquared*eccSquared*eccSquared/256));
391         phi1Rad = mu    + (3*e1/2-27*e1*e1*e1/32)*sin(2*mu) 
392                                 + (21*e1*e1/16-55*e1*e1*e1*e1/32)*sin(4*mu)
393                                 +(151*e1*e1*e1/96)*sin(6*mu);
394         phi1 = phi1Rad*rad2deg;
395
396         N1 = a/sqrt(1-eccSquared*sin(phi1Rad)*sin(phi1Rad));
397         T1 = tan(phi1Rad)*tan(phi1Rad);
398         C1 = eccPrimeSquared*cos(phi1Rad)*cos(phi1Rad);
399         R1 = a*(1-eccSquared)/pow(1-eccSquared*sin(phi1Rad)*sin(phi1Rad), 1.5);
400         D = x/(N1*k0);
401
402         Lat = phi1Rad - (N1*tan(phi1Rad)/R1)*(D*D/2-(5+3*T1+10*C1-4*C1*C1-9*eccPrimeSquared)*D*D*D*D/24
403                                         +(61+90*T1+298*C1+45*T1*T1-252*eccPrimeSquared-3*C1*C1)*D*D*D*D*D*D/720);
404         Lat = Lat * rad2deg;
405
406         Long = (D-(1+2*T1+C1)*D*D*D/6+(5-2*C1+28*T1-3*C1*C1+8*eccPrimeSquared+24*T1*T1)
407                                         *D*D*D*D*D/120)/cos(phi1Rad);
408         Long = LongOrigin + Long * rad2deg;
409
410         geo->lat=Lat;
411         geo->lng=Long;
412 }
413
414 void
415 transform_datum(struct coord_geo *from, enum map_datum from_datum, struct coord_geo *to, enum map_datum to_datum)
416 {
417 }
418
419 int
420 transform(struct transformation *t, enum projection pro, struct coord *c, struct point *p, int count, int mindist, int width, int *width_return)
421 {
422         struct coord c1;
423         int xcn, ycn; 
424         struct coord_geo g;
425         int xc, yc, zc=0, xco=0, yco=0, zco=0;
426         int xm,ym,zct;
427         int zlimit=t->znear;
428         int visible, visibleo=-1;
429         int i,j = 0,k=0;
430         dbg(3,"count=%d\n", count);
431         for (i=0; i < count; i++) {
432                 if (pro == t->pro) {
433                         xc=c[i].x;
434                         yc=c[i].y;
435                 } else {
436                         transform_to_geo(pro, &c[i], &g);
437                         transform_from_geo(t->pro, &g, &c1);
438                         xc=c1.x;
439                         yc=c1.y;
440                 }
441                 xm=xc;
442                 ym=yc;
443                 xc-=t->map_center.x;
444                 yc-=t->map_center.y;
445                 xc >>= t->scale_shift;
446                 yc >>= t->scale_shift;
447                 xm=xc;
448                 ym=yc;
449
450                 xcn=xc*t->m00+yc*t->m01+HOG(*t)*t->m02;
451                 ycn=xc*t->m10+yc*t->m11+HOG(*t)*t->m12;
452
453                 if (t->ddd) {
454                         zc=(xc*t->m20+yc*t->m21+HOG(*t)*t->m22);
455                         zct=zc;
456                         zc+=t->offz << POST_SHIFT;
457                         dbg(1,"zc=%d\n", zc);
458                         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);
459                         /* visibility */
460                         visible=(zc < zlimit ? 0:1);
461                         dbg(1,"visible=%d old %d\n", visible, visibleo);
462                         if (visible != visibleo && visibleo != -1) { 
463                                 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);
464                                 if (zco != zc) {
465                                         xcn=xcn+(long long)(xco-xcn)*(zlimit-zc)/(zco-zc);
466                                         ycn=ycn+(long long)(yco-ycn)*(zlimit-zc)/(zco-zc);
467                                 }
468                                 dbg(1,"result (%d,%d,%d) * %d / %d\n", xcn,ycn,zc,zlimit-zc,zco-zc);
469                                 zc=zlimit;
470                                 xco=xcn;
471                                 yco=ycn;
472                                 zco=zc;
473                                 if (visible)
474                                         i--;
475                                 visibleo=visible;
476                         } else {
477                                 xco=xcn;
478                                 yco=ycn;
479                                 zco=zc;
480                                 visibleo=visible;
481                                 if (! visible)
482                                         continue;
483                         }
484                         dbg(1,"zc=%d\n", zc);
485                         dbg(1,"xcn %d ycn %d\n", xcn, ycn);
486                         dbg(1,"%d,%d %d\n",xc,yc,zc);
487 #if 0
488                         dbg(0,"%d/%d=%d %d/%d=%d\n",xcn,xc,xcn/xc,ycn,yc,ycn/yc);
489 #endif
490 #if 1
491                         xc=(long long)xcn*t->xscale/zc;
492                         yc=(long long)ycn*t->yscale/zc;
493 #else
494                         xc=xcn/(1000+zc);
495                         yc=ycn/(1000+zc);
496 #endif
497 #if 0
498                         dbg(1,"%d,%d %d\n",xc,yc,zc);
499 #endif
500                 } else {
501                         xc=xcn;
502                         yc=ycn;
503                         xc>>=POST_SHIFT;
504                         yc>>=POST_SHIFT;
505                 }
506                 xc+=t->offx;
507                 yc+=t->offy;
508
509                 if (i != 0 && i != count-1 && mindist) {
510                         /* We expect values of mindist to be within 0..5 pixels for nice looking screens. 
511                            That gives difference of about 1 pixel in worst case between abs(dx)+abs(dy) and sqrt(dx*dx+dy*dy).
512                            We expect significantly bigger values when drawing map while scrolling it. But it anyway will be ugly.
513                            Should anybody care about that inaccuracy? */
514                         if ( (abs(xc - p[k].x) + abs(yc - p[k].y)) < mindist &&
515                                 (c[i+1].x != c[0].x || c[i+1].y != c[0].y))
516                                 continue;
517                         k=j;
518                 }               
519
520                 p[j].x=xc;
521                 p[j].y=yc;
522                 if (width_return) {
523                         if (t->ddd) 
524                                 width_return[j]=width*t->wscale/zc;
525                         else 
526                                 width_return[j]=width;
527                 }
528                 j++;
529         }
530         return j;
531 }
532
533 static void
534 transform_apply_inverse_matrix(struct transformation *t, struct coord_geo_cart *in, struct coord_geo_cart *out)
535 {
536         out->x=in->x*t->im00+in->y*t->im01+in->z*t->im02;
537         out->y=in->x*t->im10+in->y*t->im11+in->z*t->im12;
538         out->z=in->x*t->im20+in->y*t->im21+in->z*t->im22;
539 }
540
541 static int
542 transform_zplane_intersection(struct coord_geo_cart *p1, struct coord_geo_cart *p2, navit_float z, struct coord_geo_cart *result)
543 {
544         navit_float dividend=z-p1->z;
545         navit_float divisor=p2->z-p1->z;
546         navit_float q;
547         if (!divisor) {
548                 if (dividend) 
549                         return 0;       /* no intersection */
550                 else
551                         return 3;       /* identical planes */
552         }
553         q=dividend/divisor;
554         result->x=p1->x+q*(p2->x-p1->x);
555         result->y=p1->y+q*(p2->y-p1->y);
556         result->z=z;
557         if (q >= 0 && q <= 1)
558                 return 1;       /* intersection within [p1,p2] */
559         return 2; /* intersection without [p1,p2] */
560 }
561
562 static void
563 transform_screen_to_3d(struct transformation *t, struct point *p, navit_float z, struct coord_geo_cart *cg)
564 {
565         double xc,yc;
566         double offz=t->offz << POST_SHIFT;
567         xc=p->x - t->offx;
568         yc=p->y - t->offy;
569         cg->x=xc*z/t->xscale;
570         cg->y=yc*z/t->yscale;
571         cg->z=z-offz;
572 }
573
574 static int
575 transform_reverse_near_far(struct transformation *t, struct point *p, struct coord *c, int near, int far)
576 {
577         double xc,yc;
578         dbg(1,"%d,%d\n",p->x,p->y);
579         if (t->ddd) {
580                 struct coord_geo_cart nearc,farc,nears,fars,intersection;
581                 transform_screen_to_3d(t, p, near, &nearc);     
582                 transform_screen_to_3d(t, p, far, &farc);
583                 transform_apply_inverse_matrix(t, &nearc, &nears);
584                 transform_apply_inverse_matrix(t, &farc, &fars);
585                 if (transform_zplane_intersection(&nears, &fars, HOG(*t), &intersection) != 1)
586                         return 0;
587                 xc=intersection.x;
588                 yc=intersection.y;
589         } else {
590                 double xcn,ycn;
591                 xcn=p->x - t->offx;
592                 ycn=p->y - t->offy;
593                 xc=(xcn*t->im00+ycn*t->im01)*(1 << POST_SHIFT);
594                 yc=(xcn*t->im10+ycn*t->im11)*(1 << POST_SHIFT);
595         }
596         c->x=xc*(1 << t->scale_shift)+t->map_center.x;
597         c->y=yc*(1 << t->scale_shift)+t->map_center.y;
598         return 1;
599 }
600
601 int
602 transform_reverse(struct transformation *t, struct point *p, struct coord *c)
603 {
604         return transform_reverse_near_far(t, p, c, t->znear, t->zfar);
605 }
606
607 enum projection
608 transform_get_projection(struct transformation *this_)
609 {
610         return this_->pro;
611 }
612
613 void
614 transform_set_projection(struct transformation *this_, enum projection pro)
615 {
616         this_->pro=pro;
617 }
618
619 static int
620 min4(int v1,int v2, int v3, int v4)
621 {
622         int res=v1;
623         if (v2 < res)
624                 res=v2;
625         if (v3 < res)
626                 res=v3;
627         if (v4 < res)
628                 res=v4;
629         return res;
630 }
631
632 static int
633 max4(int v1,int v2, int v3, int v4)
634 {
635         int res=v1;
636         if (v2 > res)
637                 res=v2;
638         if (v3 > res)
639                 res=v3;
640         if (v4 > res)
641                 res=v4;
642         return res;
643 }
644
645 struct map_selection *
646 transform_get_selection(struct transformation *this_, enum projection pro, int order)
647 {
648
649         struct map_selection *ret,*curri,*curro;
650         struct coord_geo g;
651         
652         ret=map_selection_dup(this_->map_sel);
653         curri=this_->map_sel;
654         curro=ret;
655         while (curri) {
656                 if (this_->pro != pro) {
657                         transform_to_geo(this_->pro, &curri->u.c_rect.lu, &g);
658                         transform_from_geo(pro, &g, &curro->u.c_rect.lu);
659                         dbg(1,"%f,%f", g.lat, g.lng);
660                         transform_to_geo(this_->pro, &curri->u.c_rect.rl, &g);
661                         transform_from_geo(pro, &g, &curro->u.c_rect.rl);
662                         dbg(1,": - %f,%f\n", g.lat, g.lng);
663                 }
664                 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);
665                 curro->order+=order;
666 #if 0
667                 curro->u.c_rect.lu.x-=500;
668                 curro->u.c_rect.lu.y+=500;
669                 curro->u.c_rect.rl.x+=500;
670                 curro->u.c_rect.rl.y-=500;
671 #endif
672                 curro->range=item_range_all;
673                 curri=curri->next;
674                 curro=curro->next;
675         }
676         return ret;
677 }
678
679 struct coord *
680 transform_center(struct transformation *this_)
681 {
682         return &this_->map_center;
683 }
684
685 struct coord *
686 transform_get_center(struct transformation *this_)
687 {
688         return &this_->map_center;
689 }
690
691 void
692 transform_set_center(struct transformation *this_, struct coord *c)
693 {
694         this_->map_center=*c;
695 }
696
697
698 void
699 transform_set_yaw(struct transformation *t,int yaw)
700 {
701         t->yaw=yaw;
702         transform_setup_matrix(t);
703 }
704
705 int
706 transform_get_yaw(struct transformation *this_)
707 {
708         return this_->yaw;
709 }
710
711 void
712 transform_set_pitch(struct transformation *this_,int pitch)
713 {
714         this_->pitch=pitch;
715         transform_setup_matrix(this_);
716 }
717 int
718 transform_get_pitch(struct transformation *this_)
719 {
720         return this_->pitch;
721 }
722
723 void
724 transform_set_roll(struct transformation *this_,int roll)
725 {
726 #ifdef ENABLE_ROLL
727         this_->roll=roll;
728         transform_setup_matrix(this_);
729 #else
730         dbg(0,"not supported\n");
731 #endif
732 }
733
734 int
735 transform_get_roll(struct transformation *this_)
736 {
737 #ifdef ENABLE_ROLL
738         return this_->roll;
739 #else
740         return 0;
741 #endif
742 }
743
744 void
745 transform_set_distance(struct transformation *this_,int distance)
746 {
747         transform_set_screen_dist(this_, distance);
748         transform_setup_matrix(this_);
749 }
750
751 int
752 transform_get_distance(struct transformation *this_)
753 {
754         return this_->screen_dist;
755 }
756
757 void
758 transform_set_scales(struct transformation *this_, int xscale, int yscale, int wscale)
759 {
760         this_->xscale3d=xscale;
761         this_->yscale3d=yscale;
762         this_->wscale3d=wscale;
763 }
764
765 void
766 transform_set_screen_selection(struct transformation *t, struct map_selection *sel)
767 {
768         map_selection_destroy(t->screen_sel);
769         t->screen_sel=map_selection_dup(sel);
770         if (sel) {
771                 t->screen_center.x=(sel->u.p_rect.rl.x-sel->u.p_rect.lu.x)/2;
772                 t->screen_center.y=(sel->u.p_rect.rl.y-sel->u.p_rect.lu.y)/2;
773                 transform_setup_matrix(t);
774         }
775 }
776
777 void
778 transform_set_screen_center(struct transformation *t, struct point *p)
779 {
780         t->screen_center=*p;
781 }
782
783 #if 0
784 void
785 transform_set_size(struct transformation *t, int width, int height)
786 {
787         t->width=width;
788         t->height=height;
789 }
790 #endif
791
792 void
793 transform_get_size(struct transformation *t, int *width, int *height)
794 {
795         struct point_rect *r;
796         if (t->screen_sel) {
797                 r=&t->screen_sel->u.p_rect;
798                 *width=r->rl.x-r->lu.x;
799                 *height=r->rl.y-r->lu.y;
800         }
801 }
802
803 void
804 transform_setup(struct transformation *t, struct pcoord *c, int scale, int yaw)
805 {
806         t->pro=c->pro;
807         t->map_center.x=c->x;
808         t->map_center.y=c->y;
809         t->scale=scale/16.0;
810         transform_set_yaw(t, yaw);
811 }
812
813 #if 0
814
815 void
816 transform_setup_source_rect_limit(struct transformation *t, struct coord *center, int limit)
817 {
818         t->center=*center;
819         t->scale=1;
820         t->angle=0;
821         t->r.lu.x=center->x-limit;
822         t->r.rl.x=center->x+limit;
823         t->r.rl.y=center->y-limit;
824         t->r.lu.y=center->y+limit;
825 }
826 #endif
827
828 void
829 transform_setup_source_rect(struct transformation *t)
830 {
831         int i;
832         struct coord screen[4];
833         struct point screen_pnt[4];
834         struct point_rect *pr;
835         struct map_selection *ms,*msm,*next,**msm_last;
836         ms=t->map_sel;
837         while (ms) {
838                 next=ms->next;
839                 g_free(ms);
840                 ms=next;
841         }
842         t->map_sel=NULL;
843         msm_last=&t->map_sel;
844         ms=t->screen_sel;
845         while (ms) {
846                 msm=g_new0(struct map_selection, 1);
847                 *msm=*ms;
848                 pr=&ms->u.p_rect;
849                 screen_pnt[0].x=pr->lu.x;       /* left upper */
850                 screen_pnt[0].y=pr->lu.y;
851                 screen_pnt[1].x=pr->rl.x;       /* right upper */
852                 screen_pnt[1].y=pr->lu.y;
853                 screen_pnt[2].x=pr->rl.x;       /* right lower */
854                 screen_pnt[2].y=pr->rl.y;
855                 screen_pnt[3].x=pr->lu.x;       /* left lower */
856                 screen_pnt[3].y=pr->rl.y;
857                 if (t->ddd) {
858                         struct coord_geo_cart tmp,cg[8];
859                         struct coord c;
860                         int valid=0;
861                         unsigned char edgenodes[]={
862                                 0,1,
863                                 1,2,
864                                 2,3,
865                                 3,0,
866                                 4,5,
867                                 5,6,
868                                 6,7,
869                                 7,4,
870                                 0,4,
871                                 1,5,
872                                 2,6,
873                                 3,7};   
874                         for (i = 0 ; i < 8 ; i++) {
875                                 transform_screen_to_3d(t, &screen_pnt[i%4], (i >= 4 ? t->zfar:t->znear), &tmp);
876                                 transform_apply_inverse_matrix(t, &tmp, &cg[i]);
877                         }
878                         msm->u.c_rect.lu.x=0;
879                         msm->u.c_rect.lu.y=0;
880                         msm->u.c_rect.rl.x=0;
881                         msm->u.c_rect.rl.y=0;
882                         for (i = 0 ; i < 12 ; i++) {
883                                 if (transform_zplane_intersection(&cg[edgenodes[i*2]], &cg[edgenodes[i*2+1]], HOG(*t), &tmp) == 1) {
884                                         c.x=tmp.x*(1 << t->scale_shift)+t->map_center.x;
885                                         c.y=tmp.y*(1 << t->scale_shift)+t->map_center.y;
886                                         dbg(1,"intersection with edge %d at 0x%x,0x%x\n",i,c.x,c.y);
887                                         if (valid)
888                                                 coord_rect_extend(&msm->u.c_rect, &c);
889                                         else {
890                                                 msm->u.c_rect.lu=c;
891                                                 msm->u.c_rect.rl=c;
892                                                 valid=1;
893                                         }
894                                         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);
895                                 }
896                         }
897                 } else {
898                         for (i = 0 ; i < 4 ; i++) {
899                                 transform_reverse(t, &screen_pnt[i], &screen[i]);
900                                 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);
901                         }
902                         msm->u.c_rect.lu.x=min4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
903                         msm->u.c_rect.rl.x=max4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
904                         msm->u.c_rect.rl.y=min4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
905                         msm->u.c_rect.lu.y=max4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
906                 }
907                 dbg(1,"%dx%d\n", msm->u.c_rect.rl.x-msm->u.c_rect.lu.x,
908                                  msm->u.c_rect.lu.y-msm->u.c_rect.rl.y);
909                 *msm_last=msm;
910                 msm_last=&msm->next;
911                 ms=ms->next;
912         }
913 }
914
915 long
916 transform_get_scale(struct transformation *t)
917 {
918         return (int)(t->scale*16);
919 }
920
921 void
922 transform_set_scale(struct transformation *t, long scale)
923 {
924         t->scale=scale/16.0;
925         transform_setup_matrix(t);
926 }
927
928
929 int
930 transform_get_order(struct transformation *t)
931 {
932         dbg(1,"order %d\n", t->order);
933         return t->order;
934 }
935
936
937
938 #define TWOPI (M_PI*2)
939 #define GC2RAD(c) ((c) * TWOPI/(1<<24))
940 #define minf(a,b) ((a) < (b) ? (a) : (b))
941
942 static double
943 transform_distance_garmin(struct coord *c1, struct coord *c2)
944 {
945 #ifdef USE_HALVESINE
946         static const int earth_radius = 6371*1000; //m change accordingly
947 // static const int earth_radius  = 3960; //miles
948  
949 //Point 1 cords
950         navit_float lat1  = GC2RAD(c1->y);
951         navit_float long1 = GC2RAD(c1->x);
952
953 //Point 2 cords
954         navit_float lat2  = GC2RAD(c2->y);
955         navit_float long2 = GC2RAD(c2->x);
956
957 //Haversine Formula
958         navit_float dlong = long2-long1;
959         navit_float dlat  = lat2-lat1;
960
961         navit_float sinlat  = navit_sin(dlat/2);
962         navit_float sinlong = navit_sin(dlong/2);
963  
964         navit_float a=(sinlat*sinlat)+navit_cos(lat1)*navit_cos(lat2)*(sinlong*sinlong);
965         navit_float c=2*navit_asin(minf(1,navit_sqrt(a)));
966 #ifdef AVOID_FLOAT
967         return round(earth_radius*c);
968 #else
969         return earth_radius*c;
970 #endif
971 #else
972 #define GMETER 2.3887499999999999
973         navit_float dx,dy;
974         dx=c1->x-c2->x;
975         dy=c1->y-c2->y;
976         return navit_sqrt(dx*dx+dy*dy)*GMETER;
977 #undef GMETER
978 #endif
979 }
980
981 double
982 transform_scale(int y)
983 {
984         struct coord c;
985         struct coord_geo g;
986         c.x=0;
987         c.y=y;
988         transform_to_geo(projection_mg, &c, &g);
989         return 1/navit_cos(g.lat/180*M_PI);
990 }
991
992 #ifdef AVOID_FLOAT
993 static int
994 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};
995
996 static int tab_int_step = 0x20000;
997 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};
998
999 int transform_int_scale(int y)
1000 {
1001         int i,size = sizeof(tab_int_scale)/sizeof(int);
1002         if (y < 0)
1003                 y=-y;
1004         i=y/tab_int_step;
1005         if (i < size-1) 
1006                 return tab_int_scale[i]+((tab_int_scale[i+1]-tab_int_scale[i])*(y-i*tab_int_step))/tab_int_step;
1007         return tab_int_scale[size-1];
1008 }
1009 #endif
1010
1011 double
1012 transform_distance(enum projection pro, struct coord *c1, struct coord *c2)
1013 {
1014         if (pro == projection_mg) {
1015 #ifndef AVOID_FLOAT 
1016         double dx,dy,scale=transform_scale((c1->y+c2->y)/2);
1017         dx=c1->x-c2->x;
1018         dy=c1->y-c2->y;
1019         return sqrt(dx*dx+dy*dy)/scale;
1020 #else
1021         int dx,dy,f,scale=transform_int_scale((c1->y+c2->y)/2);
1022         dx=c1->x-c2->x;
1023         dy=c1->y-c2->y;
1024         if (dx < 0)
1025                 dx=-dx;
1026         if (dy < 0)
1027                 dy=-dy;
1028         while (dx > 20000 || dy > 20000) {
1029                 dx/=10;
1030                 dy/=10;
1031                 scale/=10;
1032         }
1033         if (! dy)
1034                 return dx*10000/scale;
1035         if (! dx)
1036                 return dy*10000/scale;
1037         if (dx > dy) {
1038                 f=dx*8/dy-8;
1039                 if (f >= 32)
1040                         return dx*10000/scale;
1041                 return dx*tab_sqrt[f]/scale;
1042         } else {
1043                 f=dy*8/dx-8;
1044                 if (f >= 32)
1045                         return dy*10000/scale;
1046                 return dy*tab_sqrt[f]/scale;
1047         }
1048 #endif
1049         } else if (pro == projection_garmin) {
1050                 return transform_distance_garmin(c1, c2);
1051         } else {
1052                 dbg(0,"Unknown projection: %d\n", pro);
1053                 return 0;
1054         }
1055 }
1056
1057 void
1058 transform_project(enum projection pro, struct coord *c, int distance, int angle, struct coord *res)
1059 {
1060         double scale;
1061         switch (pro) {
1062         case projection_mg:
1063                 scale=transform_scale(c->y);
1064                 res->x=c->x+distance*sin(angle*M_PI/180)*scale;
1065                 res->y=c->y+distance*cos(angle*M_PI/180)*scale;
1066                 break;
1067         default:
1068                 dbg(0,"Unsupported projection: %d\n", pro);
1069                 return;
1070         }
1071         
1072 }
1073
1074
1075 double
1076 transform_polyline_length(enum projection pro, struct coord *c, int count)
1077 {
1078         double ret=0;
1079         int i;
1080
1081         for (i = 0 ; i < count-1 ; i++) 
1082                 ret+=transform_distance(pro, &c[i], &c[i+1]);
1083         return ret;
1084 }
1085
1086 int
1087 transform_distance_sq(struct coord *c1, struct coord *c2)
1088 {
1089         int dx=c1->x-c2->x;
1090         int dy=c1->y-c2->y;
1091
1092         if (dx > 32767 || dy > 32767 || dx < -32767 || dy < -32767)
1093                 return INT_MAX;
1094         else
1095                 return dx*dx+dy*dy;
1096 }
1097
1098 navit_float
1099 transform_distance_sq_float(struct coord *c1, struct coord *c2)
1100 {
1101         int dx=c1->x-c2->x;
1102         int dy=c1->y-c2->y;
1103         return (navit_float)dx*dx+dy*dy;
1104 }
1105
1106 int
1107 transform_distance_sq_pc(struct pcoord *c1, struct pcoord *c2)
1108 {
1109         struct coord p1,p2;
1110         p1.x = c1->x; p1.y = c1->y;
1111         p2.x = c2->x; p2.y = c2->y;
1112         return transform_distance_sq(&p1, &p2);
1113 }
1114
1115 int
1116 transform_distance_line_sq(struct coord *l0, struct coord *l1, struct coord *ref, struct coord *lpnt)
1117 {
1118         int vx,vy,wx,wy;
1119         int c1,c2;
1120         int climit=1000000;
1121         struct coord l;
1122
1123         vx=l1->x-l0->x;
1124         vy=l1->y-l0->y;
1125         wx=ref->x-l0->x;
1126         wy=ref->y-l0->y;
1127
1128         c1=vx*wx+vy*wy;
1129         if ( c1 <= 0 ) {
1130                 if (lpnt)
1131                         *lpnt=*l0;
1132                 return transform_distance_sq(l0, ref);
1133         }
1134         c2=vx*vx+vy*vy;
1135         if ( c2 <= c1 ) {
1136                 if (lpnt)
1137                         *lpnt=*l1;
1138                 return transform_distance_sq(l1, ref);
1139         }
1140         while (c1 > climit || c2 > climit) {
1141                 c1/=256;
1142                 c2/=256;
1143         }
1144         l.x=l0->x+vx*c1/c2;
1145         l.y=l0->y+vy*c1/c2;
1146         if (lpnt)
1147                 *lpnt=l;
1148         return transform_distance_sq(&l, ref);
1149 }
1150
1151 navit_float
1152 transform_distance_line_sq_float(struct coord *l0, struct coord *l1, struct coord *ref, struct coord *lpnt)
1153 {
1154         navit_float vx,vy,wx,wy;
1155         navit_float c1,c2;
1156         struct coord l;
1157
1158         vx=l1->x-l0->x;
1159         vy=l1->y-l0->y;
1160         wx=ref->x-l0->x;
1161         wy=ref->y-l0->y;
1162
1163         c1=vx*wx+vy*wy;
1164         if ( c1 <= 0 ) {
1165                 if (lpnt)
1166                         *lpnt=*l0;
1167                 return transform_distance_sq_float(l0, ref);
1168         }
1169         c2=vx*vx+vy*vy;
1170         if ( c2 <= c1 ) {
1171                 if (lpnt)
1172                         *lpnt=*l1;
1173                 return transform_distance_sq_float(l1, ref);
1174         }
1175         l.x=l0->x+vx*c1/c2;
1176         l.y=l0->y+vy*c1/c2;
1177         if (lpnt)
1178                 *lpnt=l;
1179         return transform_distance_sq_float(&l, ref);
1180 }
1181
1182 int
1183 transform_distance_polyline_sq(struct coord *c, int count, struct coord *ref, struct coord *lpnt, int *pos)
1184 {
1185         int i,dist,distn;
1186         struct coord lp;
1187         if (count < 2)
1188                 return INT_MAX;
1189         if (pos)
1190                 *pos=0;
1191         dist=transform_distance_line_sq(&c[0], &c[1], ref, lpnt);
1192         for (i=2 ; i < count ; i++) {
1193                 distn=transform_distance_line_sq(&c[i-1], &c[i], ref, &lp);
1194                 if (distn < dist) {
1195                         dist=distn;
1196                         if (lpnt)
1197                                 *lpnt=lp;
1198                         if (pos)
1199                                 *pos=i-1;
1200                 }
1201         }
1202         return dist;
1203 }
1204
1205 int
1206 transform_douglas_peucker(struct coord *in, int count, int dist_sq, struct coord *out)
1207 {
1208         int ret=0;
1209         int i,d,dmax=0, idx=0;
1210         for (i = 1; i < count-2 ; i++) {
1211                 d=transform_distance_line_sq(&in[0], &in[count-1], &in[i], NULL);
1212                 if (d > dmax) {
1213                         idx=i;
1214                         dmax=d;
1215                 }
1216         }
1217         if (dmax > dist_sq) {
1218                 ret=transform_douglas_peucker(in, idx, dist_sq, out)-1;
1219                 ret+=transform_douglas_peucker(in+idx, count-idx, dist_sq, out+ret);
1220         } else {
1221                 if (count > 0)
1222                         out[ret++]=in[0];
1223                 if (count > 1)
1224                         out[ret++]=in[count-1];
1225         }
1226         return ret;
1227 }
1228
1229 int
1230 transform_douglas_peucker_float(struct coord *in, int count, navit_float dist_sq, struct coord *out)
1231 {
1232         int ret=0;
1233         int i,idx=0;
1234         navit_float d,dmax=0;
1235         for (i = 1; i < count-2 ; i++) {
1236                 d=transform_distance_line_sq_float(&in[0], &in[count-1], &in[i], NULL);
1237                 if (d > dmax) {
1238                         idx=i;
1239                         dmax=d;
1240                 }
1241         }
1242         if (dmax > dist_sq) {
1243                 ret=transform_douglas_peucker_float(in, idx, dist_sq, out)-1;
1244                 ret+=transform_douglas_peucker_float(in+idx, count-idx, dist_sq, out+ret);
1245         } else {
1246                 if (count > 0)
1247                         out[ret++]=in[0];
1248                 if (count > 1)
1249                         out[ret++]=in[count-1];
1250         }
1251         return ret;
1252 }
1253
1254
1255 void
1256 transform_print_deg(double deg)
1257 {
1258         printf("%2.0f:%2.0f:%2.4f", floor(deg), fmod(deg*60,60), fmod(deg*3600,60));
1259 }
1260
1261 #ifdef AVOID_FLOAT
1262 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};
1263
1264 static int
1265 atan2_int_lookup(int val)
1266 {
1267         int len=sizeof(tab_atan)/sizeof(int);
1268         int i=len/2;
1269         int p=i-1;
1270         for (;;) {
1271                 i>>=1;
1272                 if (val < tab_atan[p])
1273                         p-=i;
1274                 else
1275                         if (val < tab_atan[p+1])
1276                                 return p+(p>>1);
1277                         else
1278                                 p+=i;
1279         }
1280 }
1281
1282 static int
1283 atan2_int(int dx, int dy)
1284 {
1285         int mul=1,add=0,ret;
1286         if (! dx) {
1287                 return dy < 0 ? 180 : 0;
1288         }
1289         if (! dy) {
1290                 return dx < 0 ? -90 : 90;
1291         }
1292         if (dx < 0) {
1293                 dx=-dx;
1294                 mul=-1;
1295         }
1296         if (dy < 0) {
1297                 dy=-dy;
1298                 add=180*mul;
1299                 mul*=-1;
1300         }
1301         while (dx > 20000 || dy > 20000) {
1302                 dx/=10;
1303                 dy/=10;
1304         }
1305         if (dx > dy) {
1306                 ret=90-atan2_int_lookup(dy*10000/dx);
1307         } else {
1308                 ret=atan2_int_lookup(dx*10000/dy);
1309         }
1310         return ret*mul+add;
1311 }
1312 #endif
1313
1314 int
1315 transform_get_angle_delta(struct coord *c1, struct coord *c2, int dir)
1316 {
1317         int dx=c2->x-c1->x;
1318         int dy=c2->y-c1->y;
1319 #ifndef AVOID_FLOAT 
1320         double angle;
1321         angle=atan2(dx,dy);
1322         angle*=180/M_PI;
1323 #else
1324         int angle;
1325         angle=atan2_int(dx,dy);
1326 #endif
1327         if (dir == -1)
1328                 angle=angle-180;
1329         if (angle < 0)
1330                 angle+=360;
1331         return angle;
1332 }
1333
1334 int
1335 transform_within_border(struct transformation *this_, struct point *p, int border)
1336 {
1337         struct map_selection *ms=this_->screen_sel;
1338         while (ms) {
1339                 struct point_rect *r=&ms->u.p_rect;
1340                 if (p->x >= r->lu.x+border && p->x <= r->rl.x-border &&
1341                     p->y >= r->lu.y+border && p->y <= r->rl.y-border)
1342                         return 1;
1343                 ms=ms->next;
1344         }
1345         return 0;
1346 }
1347
1348 int
1349 transform_within_dist_point(struct coord *ref, struct coord *c, int dist)
1350 {
1351         if (c->x-dist > ref->x)
1352                 return 0;
1353         if (c->x+dist < ref->x)
1354                 return 0;
1355         if (c->y-dist > ref->y)
1356                 return 0;
1357         if (c->y+dist < ref->y)
1358                 return 0;
1359         if ((c->x-ref->x)*(c->x-ref->x) + (c->y-ref->y)*(c->y-ref->y) <= dist*dist) 
1360                 return 1;
1361         return 0;
1362 }
1363
1364 int
1365 transform_within_dist_line(struct coord *ref, struct coord *c0, struct coord *c1, int dist)
1366 {
1367         int vx,vy,wx,wy;
1368         int n1,n2;
1369         struct coord lc;
1370
1371         if (c0->x < c1->x) {
1372                 if (c0->x-dist > ref->x)
1373                         return 0;
1374                 if (c1->x+dist < ref->x)
1375                         return 0;
1376         } else {
1377                 if (c1->x-dist > ref->x)
1378                         return 0;
1379                 if (c0->x+dist < ref->x)
1380                         return 0;
1381         }
1382         if (c0->y < c1->y) {
1383                 if (c0->y-dist > ref->y)
1384                         return 0;
1385                 if (c1->y+dist < ref->y)
1386                         return 0;
1387         } else {
1388                 if (c1->y-dist > ref->y)
1389                         return 0;
1390                 if (c0->y+dist < ref->y)
1391                         return 0;
1392         }
1393         vx=c1->x-c0->x;
1394         vy=c1->y-c0->y;
1395         wx=ref->x-c0->x;
1396         wy=ref->y-c0->y;
1397
1398         n1=vx*wx+vy*wy;
1399         if ( n1 <= 0 )
1400                 return transform_within_dist_point(ref, c0, dist);
1401         n2=vx*vx+vy*vy;
1402         if ( n2 <= n1 )
1403                 return transform_within_dist_point(ref, c1, dist);
1404
1405         lc.x=c0->x+vx*n1/n2;
1406         lc.y=c0->y+vy*n1/n2;
1407         return transform_within_dist_point(ref, &lc, dist);
1408 }
1409
1410 int
1411 transform_within_dist_polyline(struct coord *ref, struct coord *c, int count, int close, int dist)
1412 {
1413         int i;
1414         for (i = 0 ; i < count-1 ; i++) {
1415                 if (transform_within_dist_line(ref,c+i,c+i+1,dist)) {
1416                         return 1;
1417                 }
1418         }
1419         if (close)
1420                 return (transform_within_dist_line(ref,c,c+count-1,dist));
1421         return 0;
1422 }
1423
1424 int
1425 transform_within_dist_polygon(struct coord *ref, struct coord *c, int count, int dist)
1426 {
1427         int i, j, ci = 0;
1428         for (i = 0, j = count-1; i < count; j = i++) {
1429                 if ((((c[i].y <= ref->y) && ( ref->y < c[j].y )) ||
1430                 ((c[j].y <= ref->y) && ( ref->y < c[i].y))) &&
1431                 (ref->x < (c[j].x - c[i].x) * (ref->y - c[i].y) / (c[j].y - c[i].y) + c[i].x))
1432                         ci = !ci;
1433         }
1434         if (! ci) {
1435                 if (dist)
1436                         return transform_within_dist_polyline(ref, c, count, dist, 1);
1437                 else
1438                         return 0;
1439         }
1440         return 1;
1441 }
1442
1443 int
1444 transform_within_dist_item(struct coord *ref, enum item_type type, struct coord *c, int count, int dist)
1445 {
1446         if (type < type_line)
1447                 return transform_within_dist_point(ref, c, dist);
1448         if (type < type_area)
1449                 return transform_within_dist_polyline(ref, c, count, 0, dist);
1450         return transform_within_dist_polygon(ref, c, count, dist);
1451 }
1452
1453 void
1454 transform_copy(struct transformation *src, struct transformation *dst)
1455 {
1456         memcpy(dst, src, sizeof(*src));
1457 }
1458
1459 void
1460 transform_destroy(struct transformation *t)
1461 {
1462         map_selection_destroy(t->map_sel);
1463         map_selection_destroy(t->screen_sel);
1464         g_free(t);
1465 }
1466
1467
1468 /*
1469 Note: there are many mathematically equivalent ways to express these formulas. As usual, not all of them are computationally equivalent.
1470
1471 L = latitude in radians (positive north)
1472 Lo = longitude in radians (positive east)
1473 E = easting (meters)
1474 N = northing (meters)
1475
1476 For the sphere
1477
1478 E = r Lo
1479 N = r ln [ tan (pi/4 + L/2) ]
1480
1481 where 
1482
1483 r = radius of the sphere (meters)
1484 ln() is the natural logarithm
1485
1486 For the ellipsoid
1487
1488 E = a Lo
1489 N = a * ln ( tan (pi/4 + L/2) * ( (1 - e * sin (L)) / (1 + e * sin (L))) ** (e/2)  )
1490
1491
1492                                                e
1493                                                -
1494                    pi     L     1 - e sin(L)   2
1495     =  a ln( tan( ---- + ---) (--------------)   )
1496                    4      2     1 + e sin(L) 
1497
1498
1499 where
1500
1501 a = the length of the semi-major axis of the ellipsoid (meters)
1502 e = the first eccentricity of the ellipsoid
1503
1504
1505 */
1506
1507