[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) { |
---|
| 140 | printf("Moved by (%3.2f,%3.2f,%3.2f): %s\n", |
---|
| 141 | pt->x - (*prev)->pt.x, |
---|
| 142 | pt->y - (*prev)->pt.y, |
---|
| 143 | pt->z - (*prev)->pt.z, |
---|
| 144 | name); |
---|
| 145 | fChanged = fTrue; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | osfree((*prev)->name); |
---|
| 149 | p = *prev; |
---|
| 150 | *prev = p->next; |
---|
| 151 | osfree(p); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | static int |
---|
| 155 | tree_check(void) |
---|
| 156 | { |
---|
[a20d9b9] | 157 | size_t c = 0; |
---|
| 158 | char **names; |
---|
[3262009] | 159 | size_t i; |
---|
[c0635e7] | 160 | |
---|
| 161 | if (c_added) { |
---|
| 162 | names = osmalloc(c_added * ossizeof(char *)); |
---|
| 163 | for (i = 0; i < c_added; i++) { |
---|
| 164 | added *old; |
---|
| 165 | ASSERT(added_list); |
---|
| 166 | names[i] = added_list->name; |
---|
| 167 | old = added_list; |
---|
| 168 | added_list = old->next; |
---|
| 169 | osfree(old); |
---|
| 170 | } |
---|
| 171 | ASSERT(added_list == NULL); |
---|
| 172 | qsort(names, c_added, sizeof(char *), cmp_pname); |
---|
| 173 | for (i = 0; i < c_added; i++) { |
---|
| 174 | printf("Added: %s\n", names[i]); |
---|
| 175 | osfree(names[i]); |
---|
| 176 | } |
---|
| 177 | osfree(names); |
---|
| 178 | } |
---|
| 179 | |
---|
[3262009] | 180 | for (i = 0; i < 0x2000; i++) { |
---|
| 181 | station *p; |
---|
[a20d9b9] | 182 | for (p = htab[i]; p; p = p->next) c++; |
---|
| 183 | } |
---|
| 184 | if (c == 0) return fChanged; |
---|
| 185 | |
---|
| 186 | names = osmalloc(c * ossizeof(char *)); |
---|
| 187 | c = 0; |
---|
| 188 | for (i = 0; i < 0x2000; i++) { |
---|
| 189 | station *p; |
---|
| 190 | for (p = htab[i]; p; p = p->next) names[c++] = p->name; |
---|
| 191 | } |
---|
| 192 | qsort(names, c, sizeof(char *), cmp_pname); |
---|
| 193 | for (i = 0; i < c; i++) { |
---|
[c0635e7] | 194 | printf("Deleted: %s\n", names[i]); |
---|
[3262009] | 195 | } |
---|
[a20d9b9] | 196 | return fTrue; |
---|
[3262009] | 197 | } |
---|
| 198 | |
---|
[840b213] | 199 | static void |
---|
[76bbb7c9] | 200 | parse_3d_file(const char *fnm, const char *survey, |
---|
[840b213] | 201 | void (*tree_func)(const char *, const img_point *)) |
---|
| 202 | { |
---|
| 203 | img_point pt; |
---|
| 204 | int result; |
---|
| 205 | |
---|
[76bbb7c9] | 206 | img *pimg = img_open_survey(fnm, NULL, NULL, survey); |
---|
[840b213] | 207 | if (!pimg) fatalerror(img_error(), fnm); |
---|
| 208 | |
---|
| 209 | do { |
---|
| 210 | result = img_read_item(pimg, &pt); |
---|
| 211 | switch (result) { |
---|
| 212 | case img_MOVE: |
---|
| 213 | case img_LINE: |
---|
| 214 | break; |
---|
| 215 | case img_LABEL: |
---|
| 216 | tree_func(pimg->label, &pt); |
---|
| 217 | break; |
---|
| 218 | case img_BAD: |
---|
| 219 | img_close(pimg); |
---|
[b8720f8] | 220 | fatalerror(img_error(), fnm); |
---|
[840b213] | 221 | } |
---|
| 222 | } while (result != img_STOP); |
---|
| 223 | |
---|
| 224 | img_close(pimg); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | static void |
---|
[76bbb7c9] | 228 | parse_pos_file(const char *fnm, const char *survey, |
---|
[840b213] | 229 | void (*tree_func)(const char *, const img_point *)) |
---|
| 230 | { |
---|
| 231 | FILE *fh; |
---|
| 232 | img_point pt; |
---|
| 233 | char *buf; |
---|
| 234 | size_t buf_len = 256; |
---|
| 235 | |
---|
[76bbb7c9] | 236 | char *pfx = NULL; |
---|
| 237 | size_t pfx_len = 0; |
---|
| 238 | |
---|
| 239 | if (survey) { |
---|
| 240 | pfx_len = strlen(survey); |
---|
| 241 | if (pfx_len) { |
---|
| 242 | if (survey[pfx_len - 1] == '.') pfx_len--; |
---|
| 243 | if (pfx_len) { |
---|
| 244 | pfx = osmalloc(pfx_len + 2); |
---|
| 245 | memcpy(pfx, survey, pfx_len); |
---|
| 246 | pfx[pfx_len++] = '.'; |
---|
| 247 | pfx[pfx_len] = '\0'; |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | |
---|
[840b213] | 252 | buf = osmalloc(buf_len); |
---|
| 253 | |
---|
| 254 | fh = fopen(fnm, "rb"); |
---|
| 255 | if (!fh) fatalerror(/*Couldn't open file `%s'*/93, fnm); |
---|
| 256 | |
---|
| 257 | while (1) { |
---|
| 258 | size_t off = 0; |
---|
| 259 | long fp = ftell(fh); |
---|
| 260 | if (fscanf(fh, "(%lf,%lf,%lf ) ", &pt.x, &pt.y, &pt.z) != 3) { |
---|
| 261 | int ch; |
---|
| 262 | fseek(fh, SEEK_SET, fp); |
---|
| 263 | ch = getc(fh); |
---|
| 264 | if (ch == EOF) break; |
---|
| 265 | |
---|
| 266 | printf("%s: Ignoring: ", fnm); |
---|
| 267 | while (ch != '\n' && ch != '\r' && ch != EOF) { |
---|
| 268 | putchar(ch); |
---|
| 269 | ch = getc(fh); |
---|
| 270 | } |
---|
| 271 | putchar('\n'); |
---|
| 272 | |
---|
| 273 | if (feof(fh)) break; |
---|
| 274 | continue; |
---|
| 275 | } |
---|
[76bbb7c9] | 276 | |
---|
[840b213] | 277 | buf[0] = '\0'; |
---|
| 278 | while (!feof(fh)) { |
---|
| 279 | if (!fgets(buf + off, buf_len - off, fh)) { |
---|
| 280 | /* FIXME */ |
---|
| 281 | break; |
---|
| 282 | } |
---|
| 283 | off += strlen(buf + off); |
---|
| 284 | if (off && buf[off - 1] == '\n') { |
---|
| 285 | buf[off - 1] = '\0'; |
---|
| 286 | break; |
---|
| 287 | } |
---|
| 288 | buf_len += buf_len; |
---|
| 289 | buf = osrealloc(buf, buf_len); |
---|
| 290 | } |
---|
[76bbb7c9] | 291 | |
---|
| 292 | if (!pfx_len || strncmp(buf, pfx, pfx_len) == 0) { |
---|
| 293 | tree_func(buf + pfx_len, &pt); |
---|
| 294 | } |
---|
[840b213] | 295 | } |
---|
| 296 | fclose(fh); |
---|
| 297 | |
---|
| 298 | osfree(buf); |
---|
[76bbb7c9] | 299 | osfree(pfx); |
---|
[840b213] | 300 | } |
---|
| 301 | |
---|
| 302 | static void |
---|
[76bbb7c9] | 303 | parse_file(const char *fnm, const char *survey, |
---|
[840b213] | 304 | void (*tree_func)(const char *, const img_point *)) |
---|
| 305 | { |
---|
| 306 | static const char ext3d[] = EXT_SVX_3D; |
---|
| 307 | size_t len = strlen(fnm); |
---|
| 308 | if (len > sizeof(ext3d) && fnm[len - sizeof(ext3d)] == FNM_SEP_EXT && |
---|
| 309 | strcmp(fnm + len - sizeof(ext3d) + 1, ext3d) == 0) { |
---|
[76bbb7c9] | 310 | parse_3d_file(fnm, survey, tree_func); |
---|
[840b213] | 311 | } else { |
---|
[76bbb7c9] | 312 | parse_pos_file(fnm, survey, tree_func); |
---|
[840b213] | 313 | } |
---|
| 314 | } |
---|
| 315 | |
---|
[a420b49] | 316 | int |
---|
[acc20b1] | 317 | main(int argc, char **argv) |
---|
[a420b49] | 318 | { |
---|
[73a8a94] | 319 | char *fnm1, *fnm2; |
---|
[76bbb7c9] | 320 | const char *survey = NULL; |
---|
| 321 | |
---|
[d06141c] | 322 | msg_init(argv[0]); |
---|
| 323 | |
---|
[3262009] | 324 | cmdline_set_syntax_message("FILE1 FILE2 [THRESHOLD]", |
---|
| 325 | "FILE1 and FILE2 can be .pos or .3d files\n" |
---|
[acc20b1] | 326 | "THRESHOLD is the max. ignorable change along " |
---|
| 327 | "any axis in metres (default " |
---|
| 328 | STRING(DFLT_MAX_THRESHOLD)")"); |
---|
| 329 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3); |
---|
[76bbb7c9] | 330 | while (1) { |
---|
| 331 | int opt = cmdline_getopt(); |
---|
| 332 | if (opt == EOF) break; |
---|
| 333 | if (opt == 's') survey = optarg; |
---|
[acc20b1] | 334 | } |
---|
| 335 | fnm1 = argv[optind++]; |
---|
| 336 | fnm2 = argv[optind++]; |
---|
| 337 | if (argv[optind]) { |
---|
| 338 | optarg = argv[optind]; |
---|
| 339 | threshold = cmdline_double_arg(); |
---|
[73a8a94] | 340 | } |
---|
[bfe1242] | 341 | |
---|
[3262009] | 342 | tree_init(); |
---|
[bfe1242] | 343 | |
---|
[76bbb7c9] | 344 | parse_file(fnm1, survey, tree_insert); |
---|
[3262009] | 345 | |
---|
[76bbb7c9] | 346 | parse_file(fnm2, survey, tree_remove); |
---|
[840b213] | 347 | |
---|
[3262009] | 348 | return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS; |
---|
[d1b1380] | 349 | } |
---|