1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
|
/*
* DIS/x : An implementation of the IEEE 1278.1 protocol
*
* Copyright (C) 1996,1998, Riley Rainey (rrainey@ix.netcom.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of either:
*
* a) the GNU Library General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your
* option) any later version. A description of the terms and conditions
* of the GLPL may be found in the "COPYING.LIB" file.
*
* b) the "Artistic License" which comes with this Kit. Information
* about this license may be found in the "Artistic" file.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License or the Artistic License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Information describing how to contact the author can be found in the
* README file.
*/
#include <dis/dis.h>
#include <math.h>
#include <stdio.h>
#include <ctype.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifndef M_PI_2
#define M_PI_2 1.57079632679489661923
#endif
/*
* In the DIS 2.0 coordinate system:
*
* positive Z axis is North;
* positive X axis points to 0N, 0E;
* positive Y axis points to 0N 90E.
*
* So, North latitudes are positive; East longitudes are positive.
*
* The world is considered a perfect ellipsoid based on the WGS84
* standard -- no correction is made to take into account height differences
* between the ellpsoid and the geoid.
*
* "The Surveying Handbook", edited by Brinker and Minnick contains a decent
* discussion of the technical issues required to understand what's
* going on in this code.
*/
/*
* Shift location d meters on a given geodetic course (radians)
*/
void
DISUpdateWorldCoordinates(WorldCoordinates * p,
double cos_course, double sin_course, double d_meters)
{
double n1, n2, m1;
double sin_lat, sin_lat_sqr, tan_lat, sin_course_sqr;
double delta_latitude, delta_longitude, d_sqr, cos_lat;
double B, C, /* D, */ E, h, sin_newlat;
/* Increase our height to the height above the reference ellipsoid */
double wgs84_a = WGS84_MAJOR + p->z;
sin_lat = sin(p->latitude);
sin_lat_sqr = sin_lat * sin_lat;
cos_lat = cos(p->latitude);
tan_lat = sin_lat / cos_lat;
sin_course_sqr = sin_course * sin_course;
d_sqr = d_meters * d_meters;
n1 = wgs84_a / sqrt(1.0 - WGS84_ECC_SQR * sin_lat_sqr);
m1 = (wgs84_a * (1.0 - WGS84_ECC_SQR)) /
pow(1.0 - WGS84_ECC_SQR * sin_lat_sqr, 1.5);
B = 1.0 / m1;
h = d_meters * B * cos_course;
C = tan_lat / (2.0 * m1 * n1);
#ifdef notdef
D = (3.0 * WGS84_ECC_SQR * sin_lat * cos_lat) /
(2.0 * (1.0 - WGS84_ECC_SQR * sin_lat_sqr));
#endif
E = (1.0 + 3.0 * tan_lat * tan_lat) *
(1.0 - WGS84_ECC_SQR * sin_lat_sqr) / (6.0 * wgs84_a * wgs84_a);
delta_latitude = d_meters * B * cos_course -
d_sqr * C * sin_course_sqr -
h * d_sqr * E * sin_course_sqr;
p->latitude += delta_latitude;
if (p->latitude > M_PI_2) {
p->latitude -= M_PI_2;
}
else if (p->latitude < -M_PI_2) {
p->latitude += M_PI_2;
}
sin_newlat = sin(p->latitude);
n2 = wgs84_a / sqrt(1.0 - WGS84_ECC_SQR * sin_newlat * sin_newlat);
delta_longitude = (d_meters * sin_course) / (n2 * cos(p->latitude));
p->longitude += delta_longitude;
if (p->longitude > M_PI) {
p->longitude -= M_PI;
}
else if (p->longitude < -M_PI) {
p->longitude += M_PI;
}
}
/*
* Shift location d_meters meters on a given geodetic course (radians)
* returns new outbound heading correct for the new location in delta_course_rad
*/
void
DISUpdateWorldCoordinatesEx(WorldCoordinates * p,
double cos_course, double sin_course, double d_meters,
double * delta_course_rad )
{
double n1, n2, m1;
double sin_lat, sin_lat_sqr, tan_lat, sin_course_sqr;
double delta_latitude, delta_longitude, d_sqr, cos_lat;
double B, C, /* D, */ E, h, sin_newlat;
double old_latitude, phi_m, sin_phi_m, cos_phi_m;
/* arc-seconds per rad */
const double rho = 206264.8062470964;
/* Increase our height to the height above the reference ellipsoid */
double wgs84_a = WGS84_MAJOR + p->z;
sin_lat = sin(p->latitude);
sin_lat_sqr = sin_lat * sin_lat;
cos_lat = cos(p->latitude);
tan_lat = sin_lat / cos_lat;
sin_course_sqr = sin_course * sin_course;
d_sqr = d_meters * d_meters;
n1 = wgs84_a / sqrt(1.0 - WGS84_ECC_SQR * sin_lat_sqr);
m1 = (wgs84_a * (1.0 - WGS84_ECC_SQR)) /
pow(1.0 - WGS84_ECC_SQR * sin_lat_sqr, 1.5);
B = 1.0 / m1;
h = d_meters * B * cos_course;
C = tan_lat / (2.0 * m1 * n1);
#ifdef notdef
D = (3.0 * WGS84_ECC_SQR * sin_lat * cos_lat) /
(2.0 * (1.0 - WGS84_ECC_SQR * sin_lat_sqr));
#endif
E = (1.0 + 3.0 * tan_lat * tan_lat) *
(1.0 - WGS84_ECC_SQR * sin_lat_sqr) / (6.0 * wgs84_a * wgs84_a);
delta_latitude = d_meters * B * cos_course -
d_sqr * C * sin_course_sqr -
h * d_sqr * E * sin_course_sqr;
old_latitude = p->latitude;
p->latitude += delta_latitude;
if (p->latitude > M_PI_2) {
p->latitude -= M_PI_2;
}
else if (p->latitude < -M_PI_2) {
p->latitude += M_PI_2;
}
phi_m = old_latitude + delta_latitude / 2.0;
sin_phi_m = sin(phi_m);
cos_phi_m = cos(phi_m);
sin_newlat = sin(p->latitude);
n2 = wgs84_a / sqrt(1.0 - WGS84_ECC_SQR * sin_newlat * sin_newlat);
delta_longitude = (d_meters * sin_course) / (n2 * cos(p->latitude));
*delta_course_rad = delta_longitude * sin_phi_m / cos(delta_latitude / 2.0) +
delta_longitude * (sin_phi_m * cos_phi_m * cos_phi_m) / rho;
p->longitude += delta_longitude;
if (p->longitude > M_PI) {
p->longitude -= M_PI;
}
else if (p->longitude < -M_PI) {
p->longitude += M_PI;
}
}
/*
* Convert cartesian geocentric coordinates into WGS84 geodetic lat/lon/z
*/
void
DISGeocentricToWorldCoordinates(dis_world_coordinates * loc,
WorldCoordinates * p)
{
double a_sqr = WGS84_MAJOR * WGS84_MAJOR, b_sqr = WGS84_MINOR * WGS84_MINOR;
double w, x, x_sqr, z, delta_x, cos_x;
double f, f_prime, w0, z0;
w = sqrt(loc->x * loc->x + loc->y * loc->y);
z = loc->z;
/*
* x is the sine of the parametric latitude. Use the sine of the geocentric
* latitude as the initial guess.
*/
if (w == 0.0 && z == 0.0) {
p->latitude = 0.0;
p->longitude = 0.0;
p->z = 0.0;
return;
}
x = z / sqrt(w * w + z * z);
/*
* Compute x with accuracy that will yield a lat/lon accuracy of
* about 0.0001 arc-seconds (~ 0.10 foot).
*/
for (delta_x = 1.0; fabs(delta_x) > 4.8E-10;) {
x_sqr = x * x;
cos_x = sqrt(1.0 - x_sqr);
f = 2.0 * (WGS84_MAJOR * x * w - a_sqr * x * cos_x - WGS84_MINOR * cos_x * z +
b_sqr * cos_x * x);
f_prime = 2.0 * (a_sqr + 2.0 * (a_sqr * x_sqr) - WGS84_MAJOR * w * x_sqr +
b_sqr - 2.0 * b_sqr * x_sqr + WGS84_MINOR * x * z);
delta_x = f / f_prime;
x -= delta_x;
}
z0 = WGS84_MINOR * x;
w0 = WGS84_MAJOR * sqrt(1.0 - x * x);
p->z = sqrt((z - z0) * (z - z0) + (w - w0) * (w - w0));
p->latitude = atan(z0 / (w0 * (1.0 - WGS84_ECC_SQR)));
p->longitude = atan2(loc->y, loc->x);
}
/*
* Convert WGS84 geodetic lat/lon/z into cartesian geocentric coordinates
*/
void
DISWorldCoordinatesToGeocentric(WorldCoordinates * w,
dis_world_coordinates * p)
{
double N, N1;
double cos_latitude, sin_latitude;
sin_latitude = sin(w->latitude);
cos_latitude = cos(w->latitude);
/*
* N is the length of the normal line segment from the surface to the
* spin axis.
*/
N = WGS84_MAJOR / sqrt(1.0 - (WGS84_ECC_SQR * sin_latitude * sin_latitude));
/*
* N1 lengthens the normal line to account for height above the surface
*/
N1 = N + w->z;
p->x = N1 * cos_latitude * cos(w->longitude);
p->y = N1 * cos_latitude * sin(w->longitude);
p->z = (((WGS84_MINOR * WGS84_MINOR) / (WGS84_MAJOR * WGS84_MAJOR)) * N + w->z) * sin_latitude;
}
char *
DISLatitudeToString(char *s, double la, LatLongDisplayFormat mode)
{
int d, m;
double dla, dmin, dsec;
double round_dms = 1.0 / (36000.0 * 2.0);
double round_dm = 1.0 / (600.0 * 2.0);
char *ns;
round_dms = round_dm = 0.0;
switch (mode) {
case LLM_DMS:
ns = (la >= 0.0) ? "N" : "S";
dla = RADtoDEG(fabs(la)) + round_dms;
d = (int) dla;
dmin = (dla - (double) d) * 60.0;
m = (int) dmin;
dsec = (dmin - (double) m) * 60.0;
sprintf(s, "%d %d %.1f %s", d, m, dsec, ns);
break;
case LLM_DM:
ns = (la >= 0.0) ? "N" : "S";
dla = RADtoDEG(fabs(la)) + round_dm;
d = (int) dla;
dmin = (dla - (double) d) * 60.0;
sprintf(s, "%d %.1f %s", d, dmin, ns);
break;
case LLM_D:
ns = (la >= 0.0) ? "N" : "S";
dla = RADtoDEG(fabs(la)) + 0.05;
sprintf(s, "%.1f %s", dla, ns);
break;
case LLM_SIGNED_D:
sprintf(s, "%.1f", RADtoDEG(la));
break;
}
return s;
}
char *
DISLongitudeToString(char *s, double lo, LatLongDisplayFormat mode)
{
int d, m;
double dlo, dmin, dsec;
double round_dms = 1.0 / (36000.0 * 2.0);
double round_dm = 1.0 / (600.0 * 2.0);
char *ew;
round_dms = round_dm = 0.0;
switch (mode) {
case LLM_DMS:
ew = (lo >= 0.0) ? "E" : "W";
dlo = RADtoDEG(fabs(lo)) + round_dms;
d = (int) dlo;
dmin = (dlo - (double) d) * 60.0;
m = (int) dmin;
dsec = (dmin - (double) m) * 60.0;
sprintf(s, "%d %d %.1f %s", d, m, dsec, ew);
break;
case LLM_DM:
ew = (lo >= 0.0) ? "E" : "W";
dlo = RADtoDEG(fabs(lo)) + round_dm;
d = (int) dlo;
dmin = (dlo - (double) d) * 60.0;
sprintf(s, "%d %.1f %s", d, dmin, ew);
break;
case LLM_D:
ew = (lo >= 0.0) ? "E" : "W";
dlo = RADtoDEG(fabs(lo)) + 0.05;
sprintf(s, "%.1f %s", dlo, ew);
break;
case LLM_SIGNED_D:
sprintf(s, "%.1f", RADtoDEG(lo));
break;
}
return s;
}
#define STATE_INITIAL 0
#define STATE_WORD 1
#define STATE_INTEGER 2
#define STATE_FLOAT 3
typedef enum {
EndOfFile,
TOKEN_FLOAT,
TOKEN_LONG,
TOKEN_DASH,
TOKEN_NORTH,
TOKEN_SOUTH,
TOKEN_EAST,
TOKEN_WEST
} token_id;
typedef union {
double double_value;
long long_value;
} lex_val;
static lex_val lex_value;
struct lex_record {
char *s;
FILE *f;
int lookahead_valid;
int lookahead;
int stack_top;
lex_val value_stack[16];
};
static int
input(struct lex_record *p)
{
int val;
if (p->lookahead_valid) {
p->lookahead_valid = 0;
val = p->lookahead;
}
else if (p->s) {
val = *(p->s)++;
}
else {
val = fgetc(p->f);
}
return val;
}
#define push_value(p, type, val) \
p->value_stack[p->stack_top++].type = val
#define pop_value(p, type) (p->value_stack[--p->stack_top].type)
#define unput(p, c) { p->lookahead = c; p->lookahead_valid = 1; }
#define InitializeLexRecord(p) { p->lookahead_valid = 0; }
static char token[256];
static int token_length = 0;
static token_id
NextTokenx(struct lex_record *p)
{
register int c, state = STATE_INITIAL;
#ifndef HAVE_STDLIB_H
#ifndef linux
extern double atof(const char *);
#endif
#endif
token_length = 0;
while ((c = input(p)) != EOF) {
switch (state) {
case STATE_INITIAL:
if (isspace(c)) {
continue;
}
else if (isdigit(c)) {
token[token_length++] = c;
state = STATE_INTEGER;
}
else if (c == '.') {
token[token_length++] = c;
state = STATE_FLOAT;
}
else {
token[0] = c;
token[1] = '\0';
#ifdef DEBUG
printf("other %s\n", token);
#endif
switch (c) {
case '-':
return TOKEN_DASH;
case 'n':
case 'N':
return TOKEN_NORTH;
case 'e':
case 'E':
return TOKEN_EAST;
case 's':
case 'S':
return TOKEN_SOUTH;
case 'w':
case 'W':
return TOKEN_WEST;
/*
* invalid character
*/
default:
return EndOfFile;
}
}
break;
case STATE_INTEGER:
case STATE_FLOAT:
if (isspace(c) ||
c == '-' ||
toupper(c) == 'N' ||
toupper(c) == 'S' ||
toupper(c) == 'W' ||
toupper(c) == 'E') {
token[token_length] = '\0';
unput(p, c);
if (state == STATE_INTEGER) {
lex_value.long_value = atoi(token);
return TOKEN_LONG;
}
else {
lex_value.double_value = atof(token);
return TOKEN_FLOAT;
}
}
else {
if (c == '.') {
state = STATE_FLOAT;
}
token[token_length++] = c;
}
break;
default:
token[token_length++] = c;
break;
}
}
return EndOfFile;
}
static token_id
NextToken(struct lex_record *p)
{
token_id t;
t = NextTokenx(p);
#ifdef DEBUG
printf("token %s\n", token);
#endif
return t;
}
static int
ParseLatitude(struct lex_record *p)
{
double x = 0.0;
double divider = 1.0;
int int_valid = 1;
token_id t;
t = NextToken(p);
for (;;) {
switch (t) {
case TOKEN_NORTH:
lex_value.double_value = x;
return 0;
case TOKEN_SOUTH:
lex_value.double_value = -x;
return 0;
case TOKEN_LONG:
if (int_valid) {
x += lex_value.long_value / divider;
divider *= 60.0;
t = NextToken(p);
if (t == TOKEN_DASH) {
t = NextToken(p);
}
}
else {
return -1;
}
break;
case TOKEN_FLOAT:
int_valid = 0;
x += lex_value.double_value / divider;
divider *= 60.0;
t = NextToken(p);
if (t == TOKEN_DASH) {
t = NextToken(p);
}
break;
default:
return -1;
}
}
}
static int
ParseLongitude(struct lex_record *p)
{
double x = 0.0;
double divider = 1.0;
int t, int_valid = 1;
t = NextToken(p);
for (;;) {
switch (t) {
case TOKEN_EAST:
lex_value.double_value = x;
return 0;
case TOKEN_WEST:
lex_value.double_value = -x;
return 0;
case TOKEN_LONG:
if (int_valid) {
x += lex_value.long_value / divider;
divider *= 60.0;
t = NextToken(p);
if (t == TOKEN_DASH) {
t = NextToken(p);
}
}
else {
return -1;
}
break;
case TOKEN_FLOAT:
int_valid = 0;
x += lex_value.double_value / divider;
divider *= 60.0;
t = NextToken(p);
if (t == TOKEN_DASH) {
t = NextToken(p);
}
break;
default:
return -1;
}
}
}
char *
DISStringToLatLong(char *s, WorldCoordinates * w)
{
struct lex_record p;
p.s = s;
p.lookahead_valid = 0;
if (ParseLatitude(&p) != 0) {
return 0;
}
w->latitude = DEGtoRAD(lex_value.double_value);
if (ParseLongitude(&p) != 0) {
return 0;
}
w->longitude = DEGtoRAD(lex_value.double_value);
w->z = 0.0;
return p.s;
}
|