[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; */ |
---|
[76bbb7c9] | 51 | {"survey", required_argument, 0, 's'}, |
---|
[acc20b1] | 52 | {"help", no_argument, 0, HLP_HELP}, |
---|
| 53 | {"version", no_argument, 0, HLP_VERSION}, |
---|
| 54 | {0, 0, 0, 0} |
---|
| 55 | }; |
---|
| 56 | |
---|
[76bbb7c9] | 57 | #define short_opts "s:" |
---|
[acc20b1] | 58 | |
---|
| 59 | static struct help_msg help[] = { |
---|
| 60 | /* <-- */ |
---|
[76bbb7c9] | 61 | {HLP_ENCODELONG(0), "Only load the sub-survey with this prefix"}, |
---|
[acc20b1] | 62 | {0, 0} |
---|
| 63 | }; |
---|
| 64 | |
---|
[3262009] | 65 | typedef struct station { |
---|
| 66 | struct station *next; |
---|
| 67 | char *name; |
---|
| 68 | img_point pt; |
---|
| 69 | } station; |
---|
| 70 | |
---|
[c0635e7] | 71 | typedef struct added { |
---|
| 72 | struct added *next; |
---|
| 73 | char *name; |
---|
| 74 | } added; |
---|
| 75 | |
---|
[a20d9b9] | 76 | static int |
---|
| 77 | cmp_pname(const void *a, const void *b) |
---|
| 78 | { |
---|
| 79 | return name_cmp(*(const char **)a, *(const char **)b); |
---|
| 80 | } |
---|
| 81 | |
---|
[3262009] | 82 | static station **htab; |
---|
| 83 | static bool fChanged = fFalse; |
---|
| 84 | |
---|
[b07ae08] | 85 | static added *added_list = NULL; |
---|
| 86 | static OSSIZE_T c_added = 0; |
---|
[c0635e7] | 87 | |
---|
[3262009] | 88 | static void |
---|
| 89 | tree_init(void) |
---|
| 90 | { |
---|
| 91 | size_t i; |
---|
| 92 | htab = osmalloc(0x2000 * sizeof(int)); |
---|
| 93 | for (i = 0; i < 0x2000; i++) htab[i] = NULL; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | static void |
---|
| 97 | tree_insert(const char *name, const img_point *pt) |
---|
| 98 | { |
---|
| 99 | int v = hash_string(name) & 0x1fff; |
---|
| 100 | station *stn; |
---|
[840b213] | 101 | |
---|
| 102 | /* Catch duplicate labels - .3d files shouldn't have them, but some do |
---|
| 103 | * due to a couple of bugs in some versions of Survex |
---|
| 104 | */ |
---|
[3262009] | 105 | for (stn = htab[v]; stn; stn = stn->next) { |
---|
| 106 | if (strcmp(stn->name, name) == 0) return; /* found dup */ |
---|
| 107 | } |
---|
[840b213] | 108 | |
---|
[3262009] | 109 | stn = osnew(station); |
---|
| 110 | stn->name = osstrdup(name); |
---|
| 111 | stn->pt = *pt; |
---|
| 112 | stn->next = htab[v]; |
---|
| 113 | htab[v] = stn; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | static void |
---|
| 117 | tree_remove(const char *name, const img_point *pt) |
---|
| 118 | { |
---|
| 119 | int v = hash_string(name) & 0x1fff; |
---|
| 120 | station **prev; |
---|
| 121 | station *p; |
---|
| 122 | |
---|
| 123 | for (prev = &htab[v]; *prev; prev = &((*prev)->next)) { |
---|
| 124 | if (strcmp((*prev)->name, name) == 0) break; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | if (!*prev) { |
---|
[c0635e7] | 128 | added *add = osnew(added); |
---|
| 129 | add->name = osstrdup(name); |
---|
| 130 | add->next = added_list; |
---|
| 131 | added_list = add; |
---|
| 132 | c_added++; |
---|
[3262009] | 133 | fChanged = fTrue; |
---|
| 134 | return; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | if (fabs(pt->x - (*prev)->pt.x) - threshold > EPSILON || |
---|
| 138 | fabs(pt->y - (*prev)->pt.y) - threshold > EPSILON || |
---|
| 139 | fabs(pt->z - (*prev)->pt.z) - threshold > EPSILON) { |
---|
[96e1f97] | 140 | printf(msg(/*Moved by (%3.2f,%3.2f,%3.2f): %s*/500), |
---|
[3262009] | 141 | pt->x - (*prev)->pt.x, |
---|
| 142 | pt->y - (*prev)->pt.y, |
---|
| 143 | pt->z - (*prev)->pt.z, |
---|
| 144 | name); |
---|
[96e1f97] | 145 | putnl(); |
---|
[3262009] | 146 | fChanged = fTrue; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | osfree((*prev)->name); |
---|
| 150 | p = *prev; |
---|
| 151 | *prev = p->next; |
---|
| 152 | osfree(p); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | static int |
---|
| 156 | tree_check(void) |
---|
| 157 | { |
---|
[a20d9b9] | 158 | size_t c = 0; |
---|
| 159 | char **names; |
---|
[3262009] | 160 | size_t i; |
---|
[c0635e7] | 161 | |
---|
| 162 | if (c_added) { |
---|
| 163 | names = osmalloc(c_added * ossizeof(char *)); |
---|
| 164 | for (i = 0; i < c_added; i++) { |
---|
| 165 | added *old; |
---|
| 166 | ASSERT(added_list); |
---|
| 167 | names[i] = added_list->name; |
---|
| 168 | old = added_list; |
---|
| 169 | added_list = old->next; |
---|
| 170 | osfree(old); |
---|
| 171 | } |
---|
| 172 | ASSERT(added_list == NULL); |
---|
| 173 | qsort(names, c_added, sizeof(char *), cmp_pname); |
---|
| 174 | for (i = 0; i < c_added; i++) { |
---|
[96e1f97] | 175 | printf(msg(/*Added: %s*/501), names[i]); |
---|
| 176 | putnl(); |
---|
[c0635e7] | 177 | osfree(names[i]); |
---|
| 178 | } |
---|
| 179 | osfree(names); |
---|
| 180 | } |
---|
| 181 | |
---|
[3262009] | 182 | for (i = 0; i < 0x2000; i++) { |
---|
| 183 | station *p; |
---|
[a20d9b9] | 184 | for (p = htab[i]; p; p = p->next) c++; |
---|
| 185 | } |
---|
| 186 | if (c == 0) return fChanged; |
---|
| 187 | |
---|
| 188 | names = osmalloc(c * ossizeof(char *)); |
---|
| 189 | c = 0; |
---|
| 190 | for (i = 0; i < 0x2000; i++) { |
---|
| 191 | station *p; |
---|
| 192 | for (p = htab[i]; p; p = p->next) names[c++] = p->name; |
---|
| 193 | } |
---|
| 194 | qsort(names, c, sizeof(char *), cmp_pname); |
---|
| 195 | for (i = 0; i < c; i++) { |
---|
[96e1f97] | 196 | printf(msg(/*Deleted: %s*/502), names[i]); |
---|
| 197 | putnl(); |
---|
[3262009] | 198 | } |
---|
[a20d9b9] | 199 | return fTrue; |
---|
[3262009] | 200 | } |
---|
| 201 | |
---|
[840b213] | 202 | static void |
---|
[76bbb7c9] | 203 | parse_3d_file(const char *fnm, const char *survey, |
---|
[840b213] | 204 | void (*tree_func)(const char *, const img_point *)) |
---|
| 205 | { |
---|
| 206 | img_point pt; |
---|
| 207 | int result; |
---|
| 208 | |
---|
[76bbb7c9] | 209 | img *pimg = img_open_survey(fnm, NULL, NULL, survey); |
---|
[840b213] | 210 | if (!pimg) fatalerror(img_error(), fnm); |
---|
| 211 | |
---|
| 212 | do { |
---|
| 213 | result = img_read_item(pimg, &pt); |
---|
| 214 | switch (result) { |
---|
| 215 | case img_MOVE: |
---|
| 216 | case img_LINE: |
---|
| 217 | break; |
---|
| 218 | case img_LABEL: |
---|
| 219 | tree_func(pimg->label, &pt); |
---|
| 220 | break; |
---|
| 221 | case img_BAD: |
---|
| 222 | img_close(pimg); |
---|
[b8720f8] | 223 | fatalerror(img_error(), fnm); |
---|
[840b213] | 224 | } |
---|
| 225 | } while (result != img_STOP); |
---|
| 226 | |
---|
| 227 | img_close(pimg); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | static void |
---|
[76bbb7c9] | 231 | parse_pos_file(const char *fnm, const char *survey, |
---|
[840b213] | 232 | void (*tree_func)(const char *, const img_point *)) |
---|
| 233 | { |
---|
| 234 | FILE *fh; |
---|
| 235 | img_point pt; |
---|
| 236 | char *buf; |
---|
| 237 | size_t buf_len = 256; |
---|
| 238 | |
---|
[76bbb7c9] | 239 | char *pfx = NULL; |
---|
| 240 | size_t pfx_len = 0; |
---|
| 241 | |
---|
| 242 | if (survey) { |
---|
| 243 | pfx_len = strlen(survey); |
---|
| 244 | if (pfx_len) { |
---|
| 245 | if (survey[pfx_len - 1] == '.') pfx_len--; |
---|
| 246 | if (pfx_len) { |
---|
| 247 | pfx = osmalloc(pfx_len + 2); |
---|
| 248 | memcpy(pfx, survey, pfx_len); |
---|
| 249 | pfx[pfx_len++] = '.'; |
---|
| 250 | pfx[pfx_len] = '\0'; |
---|
| 251 | } |
---|
| 252 | } |
---|
| 253 | } |
---|
| 254 | |
---|
[840b213] | 255 | buf = osmalloc(buf_len); |
---|
| 256 | |
---|
| 257 | fh = fopen(fnm, "rb"); |
---|
| 258 | if (!fh) fatalerror(/*Couldn't open file `%s'*/93, fnm); |
---|
| 259 | |
---|
| 260 | while (1) { |
---|
| 261 | size_t off = 0; |
---|
| 262 | long fp = ftell(fh); |
---|
[b65db17] | 263 | if (fp == -1) break; |
---|
[840b213] | 264 | if (fscanf(fh, "(%lf,%lf,%lf ) ", &pt.x, &pt.y, &pt.z) != 3) { |
---|
| 265 | int ch; |
---|
[b65db17] | 266 | if (fseek(fh, fp, SEEK_SET) == -1) break; |
---|
[840b213] | 267 | ch = getc(fh); |
---|
| 268 | if (ch == EOF) break; |
---|
| 269 | |
---|
[96e1f97] | 270 | printf("%s: %s ", fnm, msg(/*Ignoring:*/503)); |
---|
[840b213] | 271 | while (ch != '\n' && ch != '\r' && ch != EOF) { |
---|
| 272 | putchar(ch); |
---|
| 273 | ch = getc(fh); |
---|
| 274 | } |
---|
| 275 | putchar('\n'); |
---|
| 276 | |
---|
| 277 | if (feof(fh)) break; |
---|
| 278 | continue; |
---|
| 279 | } |
---|
[76bbb7c9] | 280 | |
---|
[b65db17] | 281 | if (ferror(fh)) |
---|
| 282 | fatalerror_in_file(fnm, 0, /*Error reading file*/18); |
---|
| 283 | |
---|
[840b213] | 284 | buf[0] = '\0'; |
---|
| 285 | while (!feof(fh)) { |
---|
[b65db17] | 286 | if (!fgets(buf + off, buf_len - off, fh)) |
---|
| 287 | fatalerror_in_file(fnm, 0, /*Error reading file*/18); |
---|
| 288 | |
---|
[840b213] | 289 | off += strlen(buf + off); |
---|
| 290 | if (off && buf[off - 1] == '\n') { |
---|
| 291 | buf[off - 1] = '\0'; |
---|
| 292 | break; |
---|
| 293 | } |
---|
| 294 | buf_len += buf_len; |
---|
| 295 | buf = osrealloc(buf, buf_len); |
---|
| 296 | } |
---|
[76bbb7c9] | 297 | |
---|
| 298 | if (!pfx_len || strncmp(buf, pfx, pfx_len) == 0) { |
---|
| 299 | tree_func(buf + pfx_len, &pt); |
---|
| 300 | } |
---|
[840b213] | 301 | } |
---|
[1da3ccc] | 302 | (void)fclose(fh); |
---|
[840b213] | 303 | |
---|
| 304 | osfree(buf); |
---|
[76bbb7c9] | 305 | osfree(pfx); |
---|
[840b213] | 306 | } |
---|
| 307 | |
---|
| 308 | static void |
---|
[76bbb7c9] | 309 | parse_file(const char *fnm, const char *survey, |
---|
[840b213] | 310 | void (*tree_func)(const char *, const img_point *)) |
---|
| 311 | { |
---|
| 312 | static const char ext3d[] = EXT_SVX_3D; |
---|
| 313 | size_t len = strlen(fnm); |
---|
| 314 | if (len > sizeof(ext3d) && fnm[len - sizeof(ext3d)] == FNM_SEP_EXT && |
---|
| 315 | strcmp(fnm + len - sizeof(ext3d) + 1, ext3d) == 0) { |
---|
[76bbb7c9] | 316 | parse_3d_file(fnm, survey, tree_func); |
---|
[840b213] | 317 | } else { |
---|
[76bbb7c9] | 318 | parse_pos_file(fnm, survey, tree_func); |
---|
[840b213] | 319 | } |
---|
| 320 | } |
---|
| 321 | |
---|
[a420b49] | 322 | int |
---|
[acc20b1] | 323 | main(int argc, char **argv) |
---|
[a420b49] | 324 | { |
---|
[73a8a94] | 325 | char *fnm1, *fnm2; |
---|
[76bbb7c9] | 326 | const char *survey = NULL; |
---|
| 327 | |
---|
[d06141c] | 328 | msg_init(argv[0]); |
---|
| 329 | |
---|
[3262009] | 330 | cmdline_set_syntax_message("FILE1 FILE2 [THRESHOLD]", |
---|
| 331 | "FILE1 and FILE2 can be .pos or .3d files\n" |
---|
[acc20b1] | 332 | "THRESHOLD is the max. ignorable change along " |
---|
| 333 | "any axis in metres (default " |
---|
| 334 | STRING(DFLT_MAX_THRESHOLD)")"); |
---|
| 335 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3); |
---|
[76bbb7c9] | 336 | while (1) { |
---|
| 337 | int opt = cmdline_getopt(); |
---|
| 338 | if (opt == EOF) break; |
---|
| 339 | if (opt == 's') survey = optarg; |
---|
[acc20b1] | 340 | } |
---|
| 341 | fnm1 = argv[optind++]; |
---|
| 342 | fnm2 = argv[optind++]; |
---|
| 343 | if (argv[optind]) { |
---|
| 344 | optarg = argv[optind]; |
---|
| 345 | threshold = cmdline_double_arg(); |
---|
[73a8a94] | 346 | } |
---|
[bfe1242] | 347 | |
---|
[3262009] | 348 | tree_init(); |
---|
[bfe1242] | 349 | |
---|
[76bbb7c9] | 350 | parse_file(fnm1, survey, tree_insert); |
---|
[3262009] | 351 | |
---|
[76bbb7c9] | 352 | parse_file(fnm2, survey, tree_remove); |
---|
[840b213] | 353 | |
---|
[3262009] | 354 | return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS; |
---|
[d1b1380] | 355 | } |
---|