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