[d1b1380] | 1 | /* > diffpos.c */ |
---|
[8b0fcbc] | 2 | /* Utility to compare two SURVEX .pos files */ |
---|
[759fb47] | 3 | /* Copyright (C) 1994,1996,1998,1999,2001 Olly Betts |
---|
[846746e] | 4 | * |
---|
[89231c4] | 5 | * This program is free software; you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
[846746e] | 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 |
---|
[89231c4] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
[846746e] | 14 | * |
---|
[89231c4] | 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
[846746e] | 18 | */ |
---|
[d1b1380] | 19 | |
---|
[a420b49] | 20 | #ifdef HAVE_CONFIG_H |
---|
| 21 | # include <config.h> |
---|
| 22 | #endif |
---|
[d1b1380] | 23 | |
---|
| 24 | /* size of line buffer */ |
---|
[0cce7db] | 25 | #define BUFLEN 1024 /* FIXME removed fixed limit */ |
---|
[d1b1380] | 26 | |
---|
| 27 | #include <stdio.h> |
---|
| 28 | #include <stdlib.h> |
---|
| 29 | #include <string.h> |
---|
| 30 | #include <math.h> |
---|
| 31 | |
---|
[2b078c4] | 32 | #include "cavern.h" /* just for REAL_EPSILON */ |
---|
[acc20b1] | 33 | #include "cmdline.h" |
---|
| 34 | |
---|
[8b0fcbc] | 35 | #ifndef sqrd |
---|
| 36 | # define sqrd(X) ((X)*(X)) |
---|
| 37 | #endif |
---|
[d1b1380] | 38 | |
---|
| 39 | /* macro to just convert argument to a string */ |
---|
| 40 | #define STRING(X) _STRING(X) |
---|
| 41 | #define _STRING(X) #X |
---|
| 42 | |
---|
| 43 | /* very small value for comparing floating point numbers with */ |
---|
[2b078c4] | 44 | #define EPSILON (REAL_EPSILON * 1000) |
---|
[d1b1380] | 45 | |
---|
| 46 | /* default threshold is 1cm */ |
---|
| 47 | #define DFLT_MAX_THRESHOLD 0.01 |
---|
| 48 | |
---|
[3f93024] | 49 | static int diff_pos(FILE *fh1, FILE *fh2, double threshold); |
---|
[73a8a94] | 50 | static int read_line(FILE *fh, double *px, double *py, double *pz, char *id); |
---|
[d1b1380] | 51 | |
---|
[acc20b1] | 52 | static const struct option long_opts[] = { |
---|
| 53 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
---|
| 54 | {"help", no_argument, 0, HLP_HELP}, |
---|
| 55 | {"version", no_argument, 0, HLP_VERSION}, |
---|
| 56 | {0, 0, 0, 0} |
---|
| 57 | }; |
---|
| 58 | |
---|
| 59 | #define short_opts "" |
---|
| 60 | |
---|
| 61 | static struct help_msg help[] = { |
---|
| 62 | /* <-- */ |
---|
| 63 | {0, 0} |
---|
| 64 | }; |
---|
| 65 | |
---|
[a420b49] | 66 | int |
---|
[acc20b1] | 67 | main(int argc, char **argv) |
---|
[a420b49] | 68 | { |
---|
[73a8a94] | 69 | double threshold = DFLT_MAX_THRESHOLD; |
---|
| 70 | char *fnm1, *fnm2; |
---|
| 71 | FILE *fh1, *fh2; |
---|
[acc20b1] | 72 | |
---|
[d06141c] | 73 | msg_init(argv[0]); |
---|
| 74 | |
---|
[acc20b1] | 75 | cmdline_set_syntax_message("POS_FILE POS_FILE [THRESHOLD]", |
---|
| 76 | "THRESHOLD is the max. ignorable change along " |
---|
| 77 | "any axis in metres (default " |
---|
| 78 | STRING(DFLT_MAX_THRESHOLD)")"); |
---|
| 79 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3); |
---|
| 80 | while (cmdline_getopt() != EOF) { |
---|
| 81 | /* do nothing */ |
---|
| 82 | } |
---|
| 83 | fnm1 = argv[optind++]; |
---|
| 84 | fnm2 = argv[optind++]; |
---|
| 85 | if (argv[optind]) { |
---|
| 86 | optarg = argv[optind]; |
---|
| 87 | threshold = cmdline_double_arg(); |
---|
[73a8a94] | 88 | } |
---|
[bfe1242] | 89 | |
---|
[73a8a94] | 90 | fh1 = fopen(fnm1, "rb"); |
---|
[bfe1242] | 91 | if (!fh1) fatalerror(/*Couldn't open file `%s'*/93, fnm1); |
---|
| 92 | |
---|
[73a8a94] | 93 | fh2 = fopen(fnm2, "rb"); |
---|
[bfe1242] | 94 | if (!fh2) fatalerror(/*Couldn't open file `%s'*/93, fnm2); |
---|
| 95 | |
---|
[3f93024] | 96 | return diff_pos(fh1, fh2, threshold); |
---|
[d1b1380] | 97 | } |
---|
| 98 | |
---|
[3f93024] | 99 | typedef enum { Eof, Haveline, Needline } state; |
---|
[cb3d1e2] | 100 | |
---|
[3f93024] | 101 | int |
---|
[a420b49] | 102 | diff_pos(FILE *fh1, FILE *fh2, double threshold) |
---|
| 103 | { |
---|
[3f93024] | 104 | state pos1 = Needline, pos2 = Needline; |
---|
| 105 | int result = 0; |
---|
[cb3d1e2] | 106 | |
---|
[3f93024] | 107 | while (1) { |
---|
| 108 | double x1, y1, z1, x2, y2, z2; |
---|
| 109 | char id1[BUFLEN], id2[BUFLEN]; |
---|
[cb3d1e2] | 110 | |
---|
[3f93024] | 111 | if (pos1 == Needline) { |
---|
| 112 | pos1 = Haveline; |
---|
| 113 | if (!read_line(fh1, &x1, &y1, &z1, id1)) pos1 = Eof; |
---|
[73a8a94] | 114 | } |
---|
[cb3d1e2] | 115 | |
---|
[3f93024] | 116 | if (pos2 == Needline) { |
---|
| 117 | pos2 = Haveline; |
---|
| 118 | if (!read_line(fh2, &x2, &y2, &z2, id2)) pos2 = Eof; |
---|
[73a8a94] | 119 | } |
---|
[cb3d1e2] | 120 | |
---|
[3f93024] | 121 | if (pos1 == Eof) { |
---|
| 122 | if (pos2 == Eof) break; |
---|
| 123 | result = 1; |
---|
| 124 | printf("Added: %s (at end of file)\n", id2); |
---|
[cb3d1e2] | 125 | pos2 = Needline; |
---|
[3f93024] | 126 | } else if (pos2 == Eof) { |
---|
| 127 | result = 1; |
---|
| 128 | printf("Deleted: %s (at end of file)\n", id1); |
---|
| 129 | pos1 = Needline; |
---|
[73a8a94] | 130 | } else { |
---|
[3f93024] | 131 | int cmp = strcmp(id1, id2); |
---|
| 132 | if (cmp == 0) { |
---|
| 133 | if (fabs(x1 - x2) - threshold > EPSILON || |
---|
| 134 | fabs(y1 - y2) - threshold > EPSILON || |
---|
| 135 | fabs(z1 - z2) - threshold > EPSILON) { |
---|
| 136 | result = 1; |
---|
| 137 | printf("Moved by (%3.2f,%3.2f,%3.2f): %s\n", |
---|
| 138 | x1 - x2, y1 - y2, z1 - z2, id1); |
---|
| 139 | } |
---|
| 140 | pos1 = pos2 = Needline; |
---|
| 141 | } else { |
---|
| 142 | result = 1; |
---|
| 143 | if (cmp < 0) { |
---|
| 144 | printf("Deleted: %s\n", id1); |
---|
| 145 | pos1 = Needline; |
---|
| 146 | } else { |
---|
| 147 | printf("Added: %s\n", id2); |
---|
| 148 | pos2 = Needline; |
---|
| 149 | } |
---|
| 150 | } |
---|
[73a8a94] | 151 | } |
---|
[d1b1380] | 152 | } |
---|
[3f93024] | 153 | return result; |
---|
[d1b1380] | 154 | } |
---|
| 155 | |
---|
[a420b49] | 156 | static int |
---|
| 157 | read_line(FILE *fh, double *px, double *py, double *pz, char *id) |
---|
| 158 | { |
---|
[73a8a94] | 159 | char buf[BUFLEN]; |
---|
[3f93024] | 160 | while (1) { |
---|
| 161 | if (!fgets(buf, BUFLEN, fh)) return 0; |
---|
| 162 | if (sscanf(buf, "(%lf,%lf,%lf )%s", px, py, pz, id) == 4) break; |
---|
[73a8a94] | 163 | printf("Ignoring line: %s\n", buf); |
---|
| 164 | } |
---|
| 165 | return 1; |
---|
[d1b1380] | 166 | } |
---|