Changeset 28223
- Timestamp:
- Jun 4, 2010, 2:20:19 PM (16 years ago)
- File:
-
- 1 edited
-
trunk/psLib/test/types/tap_psTree.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/test/types/tap_psTree.c
r25256 r28223 3 3 #include "pstap.h" 4 4 5 #define NUM 1000000 // Number of points5 #define NUM 1000000 // Number of points 6 6 #define SPHERICAL_DISTANCE 3.0 // Distance of interest for spherical test 7 #define SEED 0 // Random seed 8 9 static double distance(double ra1, double dec1, double ra2, double dec2) 10 { 11 #if 0 12 // Traditional formula 13 return acos(sin(dec1) * sin(dec2) + cos(dec1) * cos(dec2) * cos(ra1 - ra2)); 14 #else 15 // Haversine formula: used in psTree 16 double dphi = dec1 - dec2; 17 double sindphi = sin(dphi/2.0); 18 double dlambda = ra1 - ra2; 19 double sindlambda = sin(dlambda/2.0); 20 return 2.0 * asin(sqrt(PS_SQR(sindphi) + cos(dec1) * cos(dec2) * PS_SQR(sindlambda))); 21 #endif 22 } 7 23 8 24 int main(int argc, char *argv[]) … … 54 70 ok(closest->data.F64[0] == x->data.F64[bestIndex] && 55 71 closest->data.F64[1] == y->data.F64[bestIndex], 56 "corre st coords: %lf,%lf(%lf) vs %lf,%lf(%lf)",72 "correct coords: %lf,%lf(%lf) vs %lf,%lf(%lf)", 57 73 closest->data.F64[0], closest->data.F64[1], 58 74 sqrt(PS_SQR(closest->data.F64[0]) + PS_SQR(closest->data.F64[1])), … … 77 93 psVector *dec = psVectorAlloc(NUM, PS_TYPE_F64); 78 94 79 psRandom *rng = psRandomAllocSpecific(PS_RANDOM_TAUS, 0);95 psRandom *rng = psRandomAllocSpecific(PS_RANDOM_TAUS, SEED); 80 96 for (int i = 0; i < NUM; i++) { 81 97 // Using http://mathworld.wolfram.com/SpherePointPicking.html 82 ra->data.F64[i] = psRandomUniform(rng) ;98 ra->data.F64[i] = psRandomUniform(rng) * 2.0 * M_PI; 83 99 dec->data.F64[i] = acos(2.0 * psRandomUniform(rng) - 1.0) - M_PI_2; 84 100 } … … 92 108 93 109 psVector *coords = psVectorAlloc(2, PS_TYPE_F64); 94 coords->data.F64[0] = psRandomUniform(rng); 110 #if 0 111 coords->data.F64[0] = psRandomUniform(rng) * 2.0 * M_PI; 95 112 coords->data.F64[1] = acos(2.0 * psRandomUniform(rng) - 1.0) - M_PI_2; 113 #else 114 psVectorInit(coords, 0); 115 #endif 96 116 97 117 psVector *indices = psTreeAllWithin(tree, coords, DEG_TO_RAD(SPHERICAL_DISTANCE)); … … 102 122 ok(psVectorSortInPlace(indices), "sorted indices"); 103 123 124 #if 0 125 for (long i = 0; i < indices->n; i++) { 126 long index = indices->data.S64[i]; 127 double dist = distance(coords->data.F64[0], coords->data.F64[1], 128 ra->data.F64[index], dec->data.F64[index]); 129 diag("%ld (%lf,%lf) is in the list (%lf vs %lf)", 130 index, ra->data.F64[index], dec->data.F64[index], RAD_TO_DEG(dist), SPHERICAL_DISTANCE); 131 } 132 #endif 133 104 134 bool allgood = true; // All points in the appropriate place? 105 135 double bestDistance = INFINITY; // Distance to best point 106 136 long bestIndex = -1; // Index of best point 137 long bad = 0; // Number bad 107 138 for (long i = 0, j = 0; i < NUM; i++) { 108 #if 1 109 // Traditional formula 110 double dist = acos(sin(coords->data.F64[1]) * sin(dec->data.F64[i]) + 111 cos(coords->data.F64[1]) * cos(dec->data.F64[i]) * 112 cos(coords->data.F64[0] - ra->data.F64[i])); 113 #else 114 // Haversine formula: used in psTree 115 double dphi = coords->data.F64[1] - dec->data.F64[i]; 116 double sindphi = sin(dphi/2.0); 117 double dlambda = coords->data.F64[0] - ra->data.F64[i]; 118 double sindlambda = sin(dlambda/2.0); 119 double dist = 2.0 * asin(sqrt(PS_SQR(sindphi) + 120 cos(coords->data.F64[1]) * cos(dec->data.F64[i]) * 121 PS_SQR(sindlambda))); 122 #endif 123 if (dist < bestDistance) { 139 double dist = distance(coords->data.F64[0], coords->data.F64[1], 140 ra->data.F64[i], dec->data.F64[i]); 141 if (dist < bestDistance) { 124 142 bestDistance = dist; 125 143 bestIndex = i; 126 144 } 127 if ( i == indices->data.S64[j]) {145 if (j < indices->n && i == indices->data.S64[j]) { 128 146 j++; 129 147 if (dist > DEG_TO_RAD(SPHERICAL_DISTANCE)) { 130 diag("%ld is in the list, but shouldn't be (%lf vs %lf)",131 i, RAD_TO_DEG(dist), SPHERICAL_DISTANCE);148 diag("%ld (%lf,%lf) is in the list, but shouldn't be (%lf vs %lf)", 149 i, ra->data.F64[i], dec->data.F64[i], RAD_TO_DEG(dist), SPHERICAL_DISTANCE); 132 150 allgood = false; 151 bad++; 152 } else { 153 #if 0 154 diag("%ld (%lf,%lf) correctly identified in the list (%lf vs %lf)", 155 i, ra->data.F64[i], dec->data.F64[i], RAD_TO_DEG(dist), SPHERICAL_DISTANCE); 156 #endif 133 157 } 134 158 } else if (dist <= DEG_TO_RAD(SPHERICAL_DISTANCE)) { 135 diag("%ld is not in the list, but should be (%lf vs %lf)",136 i, RAD_TO_DEG(dist), SPHERICAL_DISTANCE);159 diag("%ld (%lf,%lf) is not in the list, but should be (%lf vs %lf)", 160 i, ra->data.F64[i], dec->data.F64[i], RAD_TO_DEG(dist), SPHERICAL_DISTANCE); 137 161 allgood = false; 162 bad++; 138 163 } 139 164 } 140 ok(allgood, "list is ac urate");165 ok(allgood, "list is accurate: %ld bad", bad); 141 166 ok(bestIndex == closeIndex, "correct point: %ld vs %ld", closeIndex, bestIndex); 142 167
Note:
See TracChangeset
for help on using the changeset viewer.
