IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 28232


Ignore:
Timestamp:
Jun 4, 2010, 4:13:32 PM (16 years ago)
Author:
Paul Price
Message:

Dealing with the wrap at RA=0=2pi. Using 2*hav(theta) = 1-cos(theta) instead of hav(theta)=sin2(theta/2) which involves more multiplications.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/types/psTree.c

    r28225 r28232  
    430430        switch (dim) {
    431431          case 2: {
    432               // Haversine formula
     432              // Haversine formula, modulo a factor of 1/2
    433433              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);
    435435              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;
    439438          }
    440439          default:
     
    446445
    447446    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);
    454447}
    455448
     
    479472          double decMin = tree->min->F64[index][1], decMax = tree->max->F64[index][1]; // Dec bounds
    480473
    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.
    482477          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);
    494482          }
    495483
    496           double decDist = 0.0;
    497484          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);
    501487          }
     488
    502489          break;
    503490      }
     
    619606        // Using the square of the distance as the distance measure
    620607        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);
    626611      default:
    627612        psAbort("Unrecognised type: %x", tree->type);
Note: See TracChangeset for help on using the changeset viewer.