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