[d1b1380] | 1 | /* > diffpos.c */ |
---|
[3262009] | 2 | /* Utility to compare two SURVEX .pos or .3d 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 | #include <stdio.h> |
---|
| 25 | #include <stdlib.h> |
---|
| 26 | #include <string.h> |
---|
| 27 | #include <math.h> |
---|
| 28 | |
---|
[2b078c4] | 29 | #include "cavern.h" /* just for REAL_EPSILON */ |
---|
[acc20b1] | 30 | #include "cmdline.h" |
---|
[3262009] | 31 | #include "debug.h" |
---|
| 32 | #include "filelist.h" |
---|
[693388e] | 33 | #include "hash.h" |
---|
[3262009] | 34 | #include "img.h" |
---|
[a20d9b9] | 35 | #include "namecmp.h" |
---|
[acc20b1] | 36 | |
---|
[d1b1380] | 37 | /* macro to just convert argument to a string */ |
---|
| 38 | #define STRING(X) _STRING(X) |
---|
| 39 | #define _STRING(X) #X |
---|
| 40 | |
---|
| 41 | /* very small value for comparing floating point numbers with */ |
---|
[2b078c4] | 42 | #define EPSILON (REAL_EPSILON * 1000) |
---|
[d1b1380] | 43 | |
---|
| 44 | /* default threshold is 1cm */ |
---|
| 45 | #define DFLT_MAX_THRESHOLD 0.01 |
---|
| 46 | |
---|
[3262009] | 47 | static double threshold = DFLT_MAX_THRESHOLD; |
---|
[d1b1380] | 48 | |
---|
[acc20b1] | 49 | static const struct option long_opts[] = { |
---|
| 50 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
---|
| 51 | {"help", no_argument, 0, HLP_HELP}, |
---|
| 52 | {"version", no_argument, 0, HLP_VERSION}, |
---|
| 53 | {0, 0, 0, 0} |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | #define short_opts "" |
---|
| 57 | |
---|
| 58 | static struct help_msg help[] = { |
---|
| 59 | /* <-- */ |
---|
| 60 | {0, 0} |
---|
| 61 | }; |
---|
| 62 | |
---|
[3262009] | 63 | typedef struct station { |
---|
| 64 | struct station *next; |
---|
| 65 | char *name; |
---|
| 66 | img_point pt; |
---|
| 67 | } station; |
---|
| 68 | |
---|
[c0635e7] | 69 | typedef struct added { |
---|
| 70 | struct added *next; |
---|
| 71 | char *name; |
---|
| 72 | } added; |
---|
| 73 | |
---|
[a20d9b9] | 74 | static int |
---|
| 75 | cmp_pname(const void *a, const void *b) |
---|
| 76 | { |
---|
| 77 | return name_cmp(*(const char **)a, *(const char **)b); |
---|
| 78 | } |
---|
| 79 | |
---|
[3262009] | 80 | static station **htab; |
---|
| 81 | static bool fChanged = fFalse; |
---|
| 82 | |
---|
[c0635e7] | 83 | static added *added_list = NULL;; |
---|
| 84 | OSSIZE_T c_added = 0; |
---|
| 85 | |
---|
[3262009] | 86 | static void |
---|
| 87 | tree_init(void) |
---|
| 88 | { |
---|
| 89 | size_t i; |
---|
| 90 | htab = osmalloc(0x2000 * sizeof(int)); |
---|
| 91 | for (i = 0; i < 0x2000; i++) htab[i] = NULL; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | static void |
---|
| 95 | tree_insert(const char *name, const img_point *pt) |
---|
| 96 | { |
---|
| 97 | int v = hash_string(name) & 0x1fff; |
---|
| 98 | station *stn; |
---|
[840b213] | 99 | |
---|
| 100 | /* Catch duplicate labels - .3d files shouldn't have them, but some do |
---|
| 101 | * due to a couple of bugs in some versions of Survex |
---|
| 102 | */ |
---|
[3262009] | 103 | for (stn = htab[v]; stn; stn = stn->next) { |
---|
| 104 | if (strcmp(stn->name, name) == 0) return; /* found dup */ |
---|
| 105 | } |
---|
[840b213] | 106 | |
---|
[3262009] | 107 | stn = osnew(station); |
---|
| 108 | stn->name = osstrdup(name); |
---|
| 109 | stn->pt = *pt; |
---|
| 110 | stn->next = htab[v]; |
---|
| 111 | htab[v] = stn; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | static void |
---|
| 115 | tree_remove(const char *name, const img_point *pt) |
---|
| 116 | { |
---|
| 117 | int v = hash_string(name) & 0x1fff; |
---|
| 118 | station **prev; |
---|
| 119 | station *p; |
---|
| 120 | |
---|
| 121 | for (prev = &htab[v]; *prev; prev = &((*prev)->next)) { |
---|
| 122 | if (strcmp((*prev)->name, name) == 0) break; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | if (!*prev) { |
---|
[c0635e7] | 126 | added *add = osnew(added); |
---|
| 127 | add->name = osstrdup(name); |
---|
| 128 | add->next = added_list; |
---|
| 129 | added_list = add; |
---|
| 130 | c_added++; |
---|
[3262009] | 131 | fChanged = fTrue; |
---|
| 132 | return; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | if (fabs(pt->x - (*prev)->pt.x) - threshold > EPSILON || |
---|
| 136 | fabs(pt->y - (*prev)->pt.y) - threshold > EPSILON || |
---|
| 137 | fabs(pt->z - (*prev)->pt.z) - threshold > EPSILON) { |
---|
| 138 | printf("Moved by (%3.2f,%3.2f,%3.2f): %s\n", |
---|
| 139 | pt->x - (*prev)->pt.x, |
---|
| 140 | pt->y - (*prev)->pt.y, |
---|
| 141 | pt->z - (*prev)->pt.z, |
---|
| 142 | name); |
---|
| 143 | fChanged = fTrue; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | osfree((*prev)->name); |
---|
| 147 | p = *prev; |
---|
| 148 | *prev = p->next; |
---|
| 149 | osfree(p); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | static int |
---|
| 153 | tree_check(void) |
---|
| 154 | { |
---|
[a20d9b9] | 155 | size_t c = 0; |
---|
| 156 | char **names; |
---|
[3262009] | 157 | size_t i; |
---|
[c0635e7] | 158 | |
---|
| 159 | if (c_added) { |
---|
| 160 | names = osmalloc(c_added * ossizeof(char *)); |
---|
| 161 | for (i = 0; i < c_added; i++) { |
---|
| 162 | added *old; |
---|
| 163 | ASSERT(added_list); |
---|
| 164 | names[i] = added_list->name; |
---|
| 165 | old = added_list; |
---|
| 166 | added_list = old->next; |
---|
| 167 | osfree(old); |
---|
| 168 | } |
---|
| 169 | ASSERT(added_list == NULL); |
---|
| 170 | qsort(names, c_added, sizeof(char *), cmp_pname); |
---|
| 171 | for (i = 0; i < c_added; i++) { |
---|
| 172 | printf("Added: %s\n", names[i]); |
---|
| 173 | osfree(names[i]); |
---|
| 174 | } |
---|
| 175 | osfree(names); |
---|
| 176 | } |
---|
| 177 | |
---|
[3262009] | 178 | for (i = 0; i < 0x2000; i++) { |
---|
| 179 | station *p; |
---|
[a20d9b9] | 180 | for (p = htab[i]; p; p = p->next) c++; |
---|
| 181 | } |
---|
| 182 | if (c == 0) return fChanged; |
---|
| 183 | |
---|
| 184 | names = osmalloc(c * ossizeof(char *)); |
---|
| 185 | c = 0; |
---|
| 186 | for (i = 0; i < 0x2000; i++) { |
---|
| 187 | station *p; |
---|
| 188 | for (p = htab[i]; p; p = p->next) names[c++] = p->name; |
---|
| 189 | } |
---|
| 190 | qsort(names, c, sizeof(char *), cmp_pname); |
---|
| 191 | for (i = 0; i < c; i++) { |
---|
[c0635e7] | 192 | printf("Deleted: %s\n", names[i]); |
---|
[3262009] | 193 | } |
---|
[a20d9b9] | 194 | return fTrue; |
---|
[3262009] | 195 | } |
---|
| 196 | |
---|
[840b213] | 197 | static void |
---|
| 198 | parse_3d_file(const char *fnm, |
---|
| 199 | void (*tree_func)(const char *, const img_point *)) |
---|
| 200 | { |
---|
| 201 | img_point pt; |
---|
| 202 | int result; |
---|
| 203 | |
---|
| 204 | img *pimg = img_open(fnm, NULL, NULL); |
---|
| 205 | if (!pimg) fatalerror(img_error(), fnm); |
---|
| 206 | |
---|
| 207 | do { |
---|
| 208 | result = img_read_item(pimg, &pt); |
---|
| 209 | switch (result) { |
---|
| 210 | case img_MOVE: |
---|
| 211 | case img_LINE: |
---|
| 212 | break; |
---|
| 213 | case img_LABEL: |
---|
| 214 | tree_func(pimg->label, &pt); |
---|
| 215 | break; |
---|
| 216 | case img_BAD: |
---|
| 217 | img_close(pimg); |
---|
| 218 | fatalerror(/*Bad 3d image file `%s'*/106, fnm); |
---|
| 219 | } |
---|
| 220 | } while (result != img_STOP); |
---|
| 221 | |
---|
| 222 | img_close(pimg); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | static void |
---|
| 226 | parse_pos_file(const char *fnm, |
---|
| 227 | void (*tree_func)(const char *, const img_point *)) |
---|
| 228 | { |
---|
| 229 | FILE *fh; |
---|
| 230 | img_point pt; |
---|
| 231 | char *buf; |
---|
| 232 | size_t buf_len = 256; |
---|
| 233 | |
---|
| 234 | buf = osmalloc(buf_len); |
---|
| 235 | |
---|
| 236 | fh = fopen(fnm, "rb"); |
---|
| 237 | if (!fh) fatalerror(/*Couldn't open file `%s'*/93, fnm); |
---|
| 238 | |
---|
| 239 | while (1) { |
---|
| 240 | size_t off = 0; |
---|
| 241 | long fp = ftell(fh); |
---|
| 242 | if (fscanf(fh, "(%lf,%lf,%lf ) ", &pt.x, &pt.y, &pt.z) != 3) { |
---|
| 243 | int ch; |
---|
| 244 | fseek(fh, SEEK_SET, fp); |
---|
| 245 | ch = getc(fh); |
---|
| 246 | if (ch == EOF) break; |
---|
| 247 | |
---|
| 248 | printf("%s: Ignoring: ", fnm); |
---|
| 249 | while (ch != '\n' && ch != '\r' && ch != EOF) { |
---|
| 250 | putchar(ch); |
---|
| 251 | ch = getc(fh); |
---|
| 252 | } |
---|
| 253 | putchar('\n'); |
---|
| 254 | |
---|
| 255 | if (feof(fh)) break; |
---|
| 256 | continue; |
---|
| 257 | } |
---|
| 258 | buf[0] = '\0'; |
---|
| 259 | while (!feof(fh)) { |
---|
| 260 | if (!fgets(buf + off, buf_len - off, fh)) { |
---|
| 261 | /* FIXME */ |
---|
| 262 | break; |
---|
| 263 | } |
---|
| 264 | off += strlen(buf + off); |
---|
| 265 | if (off && buf[off - 1] == '\n') { |
---|
| 266 | buf[off - 1] = '\0'; |
---|
| 267 | break; |
---|
| 268 | } |
---|
| 269 | buf_len += buf_len; |
---|
| 270 | buf = osrealloc(buf, buf_len); |
---|
| 271 | } |
---|
| 272 | tree_func(buf, &pt); |
---|
| 273 | } |
---|
| 274 | fclose(fh); |
---|
| 275 | |
---|
| 276 | osfree(buf); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | static void |
---|
| 280 | parse_file(const char *fnm, |
---|
| 281 | void (*tree_func)(const char *, const img_point *)) |
---|
| 282 | { |
---|
| 283 | static const char ext3d[] = EXT_SVX_3D; |
---|
| 284 | size_t len = strlen(fnm); |
---|
| 285 | if (len > sizeof(ext3d) && fnm[len - sizeof(ext3d)] == FNM_SEP_EXT && |
---|
| 286 | strcmp(fnm + len - sizeof(ext3d) + 1, ext3d) == 0) { |
---|
| 287 | parse_3d_file(fnm, tree_func); |
---|
| 288 | } else { |
---|
| 289 | parse_pos_file(fnm, tree_func); |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | |
---|
[a420b49] | 293 | int |
---|
[acc20b1] | 294 | main(int argc, char **argv) |
---|
[a420b49] | 295 | { |
---|
[73a8a94] | 296 | char *fnm1, *fnm2; |
---|
[d06141c] | 297 | msg_init(argv[0]); |
---|
| 298 | |
---|
[3262009] | 299 | cmdline_set_syntax_message("FILE1 FILE2 [THRESHOLD]", |
---|
| 300 | "FILE1 and FILE2 can be .pos or .3d files\n" |
---|
[acc20b1] | 301 | "THRESHOLD is the max. ignorable change along " |
---|
| 302 | "any axis in metres (default " |
---|
| 303 | STRING(DFLT_MAX_THRESHOLD)")"); |
---|
| 304 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3); |
---|
| 305 | while (cmdline_getopt() != EOF) { |
---|
| 306 | /* do nothing */ |
---|
| 307 | } |
---|
| 308 | fnm1 = argv[optind++]; |
---|
| 309 | fnm2 = argv[optind++]; |
---|
| 310 | if (argv[optind]) { |
---|
| 311 | optarg = argv[optind]; |
---|
| 312 | threshold = cmdline_double_arg(); |
---|
[73a8a94] | 313 | } |
---|
[bfe1242] | 314 | |
---|
[3262009] | 315 | tree_init(); |
---|
[bfe1242] | 316 | |
---|
[840b213] | 317 | parse_file(fnm1, tree_insert); |
---|
[3262009] | 318 | |
---|
[840b213] | 319 | parse_file(fnm2, tree_remove); |
---|
| 320 | |
---|
[3262009] | 321 | return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS; |
---|
[d1b1380] | 322 | } |
---|