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