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