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