/[zanavi_public1]/navit/navit/transform.c
ZANavi

Contents of /navit/navit/transform.c

Parent Directory Parent Directory | Revision Log Revision Log


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

   
Visit the ZANavi Wiki