[d1b1380] | 1 | /* > diffpos.c */ |
---|
| 2 | |
---|
[73a8a94] | 3 | /* (Originally quick and dirty) program to compare two SURVEX .pos files */ |
---|
| 4 | /* Copyright (C) 1994,1996,1998 Olly Betts */ |
---|
[d1b1380] | 5 | |
---|
| 6 | /* |
---|
| 7 | 1994.04.16 Written |
---|
| 8 | 1994.11.22 Now displays names of stations added/deleted at end of .pos file |
---|
| 9 | Now displays and skips lines which it can't parse (eg headers) |
---|
| 10 | Negative thresholds allowed (to force a "full" compare) |
---|
| 11 | Syntax expanded |
---|
| 12 | 1996.04.04 fixed Borland C warning |
---|
[73a8a94] | 13 | 1996.05.06 fixed STRING() macro to correct Syntax: message |
---|
| 14 | 1998.06.09 fettled layout |
---|
[d1b1380] | 15 | */ |
---|
| 16 | |
---|
| 17 | /* size of line buffer */ |
---|
| 18 | #define BUFLEN 256 |
---|
| 19 | |
---|
| 20 | #include <stdio.h> |
---|
| 21 | #include <stdlib.h> |
---|
| 22 | #include <string.h> |
---|
| 23 | #include <math.h> |
---|
| 24 | |
---|
| 25 | #define sqrd(X) ((X)*(X)) |
---|
| 26 | |
---|
| 27 | /* macro to just convert argument to a string */ |
---|
| 28 | #define STRING(X) _STRING(X) |
---|
| 29 | #define _STRING(X) #X |
---|
| 30 | |
---|
| 31 | /* very small value for comparing floating point numbers with */ |
---|
[73a8a94] | 32 | /* (ought to use a real epsilon value) */ |
---|
[d1b1380] | 33 | #define EPSILON 0.00001 |
---|
| 34 | |
---|
| 35 | /* default threshold is 1cm */ |
---|
| 36 | #define DFLT_MAX_THRESHOLD 0.01 |
---|
| 37 | |
---|
[73a8a94] | 38 | static void diff_pos(FILE *fh1, FILE *fh2, double threshold); |
---|
| 39 | static int read_line(FILE *fh, double *px, double *py, double *pz, char *id); |
---|
[d1b1380] | 40 | |
---|
| 41 | int main( int argc, char *argv[] ) { |
---|
[73a8a94] | 42 | double threshold = DFLT_MAX_THRESHOLD; |
---|
| 43 | char *fnm1, *fnm2; |
---|
| 44 | FILE *fh1, *fh2; |
---|
| 45 | if (argc != 3) { |
---|
| 46 | char *p; |
---|
| 47 | if (argc == 4) |
---|
| 48 | threshold = strtod(argv[3], &p); |
---|
| 49 | if (argc != 4 || *p) { |
---|
| 50 | /* complain if not 4 args, or threshold didn't parse cleanly */ |
---|
| 51 | printf("Syntax: %s <pos file> <pos file> [<threshold>]\n", argv[0]); |
---|
| 52 | printf(" where <threshold> is the max. permitted change along " |
---|
| 53 | "any axis in metres\n" |
---|
| 54 | " (default <threshold> is "STRING(DFLT_MAX_THRESHOLD)"m)\n"); |
---|
| 55 | exit(1); |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | fnm1 = argv[1]; |
---|
| 59 | fnm2 = argv[2]; |
---|
| 60 | fh1 = fopen(fnm1, "rb"); |
---|
| 61 | if (!fh1) { |
---|
| 62 | printf("Can't open file '%s'\n", fnm1); |
---|
| 63 | exit(1); |
---|
| 64 | } |
---|
| 65 | fh2 = fopen(fnm2, "rb"); |
---|
| 66 | if (!fh2) { |
---|
| 67 | printf("Can't open file '%s'\n", fnm2); |
---|
| 68 | exit(1); |
---|
| 69 | } |
---|
| 70 | diff_pos(fh1, fh2, threshold); |
---|
| 71 | return 0; |
---|
[d1b1380] | 72 | } |
---|
| 73 | |
---|
| 74 | static void diff_pos( FILE *fh1, FILE *fh2, double threshold ) { |
---|
[73a8a94] | 75 | double x1, y1, z1, x2, y2, z2; |
---|
| 76 | int cmp; |
---|
| 77 | char id1[BUFLEN], id2[BUFLEN]; |
---|
| 78 | int fRead1 = 1, fRead2 = 1; |
---|
| 79 | while (!feof(fh1) && !feof(fh2)) { |
---|
| 80 | if (fRead1) { |
---|
| 81 | if (!read_line(fh1, &x1, &y1, &z1, id1)) continue; |
---|
| 82 | fRead1 = 0; |
---|
| 83 | } |
---|
| 84 | if (fRead2) { |
---|
| 85 | if (!read_line(fh2, &x2, &y2, &z2, id2)) continue; |
---|
| 86 | fRead2 = 0; |
---|
| 87 | } |
---|
| 88 | cmp = strcmp(id1, id2); |
---|
| 89 | if (cmp == 0) { |
---|
| 90 | if (fabs(x1 - x2) - threshold > EPSILON |
---|
| 91 | || fabs(y1 - y2) - threshold > EPSILON |
---|
| 92 | || fabs(z1 - z2) - threshold > EPSILON) { |
---|
| 93 | printf("Moved by (%3.2f,%3.2f,%3.2f): %s\n", x1 - x2, y1 - y2, |
---|
| 94 | z1 - z2, id1); |
---|
| 95 | } |
---|
| 96 | fRead1 = fRead2 = 1; |
---|
| 97 | } else { |
---|
| 98 | if (cmp < 0) { |
---|
| 99 | printf("Deleted: %s\n", id1); |
---|
| 100 | fRead1 = 1; |
---|
| 101 | } else { |
---|
| 102 | printf("Added: %s\n", id2); |
---|
| 103 | fRead2 = 1; |
---|
| 104 | } |
---|
| 105 | } |
---|
[d1b1380] | 106 | } |
---|
[73a8a94] | 107 | while (!feof(fh1) && read_line(fh1, &x1, &y1, &z1, id1)) |
---|
| 108 | printf("Deleted: %s (at end of file)\n", id1); |
---|
| 109 | while (!feof(fh2) && read_line(fh2, &x2, &y2, &z2, id2)) |
---|
| 110 | printf("Added: %s (at end of file)\n", id2); |
---|
[d1b1380] | 111 | } |
---|
| 112 | |
---|
[73a8a94] | 113 | static int read_line(FILE *fh, double *px, double *py, double *pz, |
---|
| 114 | char *id) { |
---|
| 115 | char buf[BUFLEN]; |
---|
| 116 | if (!fgets(buf, BUFLEN, fh)) return 0; |
---|
| 117 | if (sscanf(buf, "(%lf,%lf,%lf )%s", px, py, pz, id) < 4) { |
---|
| 118 | printf("Ignoring line: %s\n", buf); |
---|
| 119 | return 0; |
---|
| 120 | } |
---|
| 121 | return 1; |
---|
[d1b1380] | 122 | } |
---|