Changeset 28232
- Timestamp:
- Jun 4, 2010, 4:13:32 PM (16 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/types/psTree.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/types/psTree.c
r28225 r28232 430 430 switch (dim) { 431 431 case 2: { 432 // Haversine formula 432 // Haversine formula, modulo a factor of 1/2 433 433 double dphi = coords->data.F64[1] - tree->data->F64[index][1]; 434 double sindphi = sin(dphi / 2.0);434 double haverPhi = 1.0 - cos(dphi); 435 435 double dlambda = coords->data.F64[0] - tree->data->F64[index][0]; 436 double sindlambda = sin(dlambda / 2.0); 437 return PS_SQR(sindphi) + 438 cos(coords->data.F64[1]) * cos(tree->data->F64[index][1]) * PS_SQR(sindlambda); 436 double haverLambda = 1.0 - cos(dlambda); 437 return haverPhi + cos(coords->data.F64[1]) * cos(tree->data->F64[index][1]) * haverLambda; 439 438 } 440 439 default: … … 446 445 447 446 return NAN; 448 }449 450 static inline double sin2diff(double coord1, double coord2)451 {452 double sinDiff = sin(coord1 - coord2);453 return PS_SQR(sinDiff);454 447 } 455 448 … … 479 472 double decMin = tree->min->F64[index][1], decMax = tree->max->F64[index][1]; // Dec bounds 480 473 481 double raDist = 0.0; 474 // Haversine formula, modulo a factor of 1/2 475 // This seems to deal with the wrap at RA=0=2pi, probably ascending the tree and descending on the 476 // other side of the wrap. 482 477 if (ra < raMin || ra > raMax) { 483 double ra1 = sin2diff(ra, raMin); 484 double ra2 = sin2diff(ra + 2.0 * M_PI, raMin); 485 raDist = PS_MIN(ra1, ra2); 486 double ra3 = sin2diff(ra, raMax); 487 if (ra3 < raDist) { 488 raDist = ra3; 489 } 490 double ra4 = sin2diff(ra + 2.0 * M_PI, raMax); 491 if (ra4 < raDist) { 492 raDist = ra4; 493 } 478 double ra1 = cos(ra - raMin), ra2 = cos(ra - raMax); // Options for RA distance 479 double raDist = PS_MAX(ra1, ra2); // Will give the smallest distance 480 double dec0 = (fabs(dec - decMin) < fabs(dec - decMax)) ? decMin : decMax; // Closest Dec limit 481 distance += cos(dec0) * cos(dec) * (1.0 - raDist); 494 482 } 495 483 496 double decDist = 0.0;497 484 if (dec < decMin || dec > decMax) { 498 double dec1 = sin2diff(dec, decMin); 499 double dec2 = sin2diff(dec, decMax); 500 decDist = PS_MIN(dec1, dec2); 485 double dec1 = cos(dec - decMin), dec2 = cos(dec - decMax); // Options for Dec distance 486 distance += 1.0 - PS_MAX(dec1, dec2); 501 487 } 488 502 489 break; 503 490 } … … 619 606 // Using the square of the distance as the distance measure 620 607 return PS_SQR(radius); 621 case PS_TREE_SPHERICAL: { 622 // Using a rearrangement of the Haversine formula 623 double sindist = sin(radius / 2.0); 624 return PS_SQR(sindist); 625 } 608 case PS_TREE_SPHERICAL: 609 // Using Haversine formula, modulo a factor of 1/2 610 return 1.0 - cos(radius); 626 611 default: 627 612 psAbort("Unrecognised type: %x", tree->type);
Note:
See TracChangeset
for help on using the changeset viewer.
