[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" |
---|
| 33 | #include "img.h" |
---|
[a20d9b9] | 34 | #include "namecmp.h" |
---|
[acc20b1] | 35 | |
---|
[8b0fcbc] | 36 | #ifndef sqrd |
---|
| 37 | # define sqrd(X) ((X)*(X)) |
---|
| 38 | #endif |
---|
[d1b1380] | 39 | |
---|
| 40 | /* macro to just convert argument to a string */ |
---|
| 41 | #define STRING(X) _STRING(X) |
---|
| 42 | #define _STRING(X) #X |
---|
| 43 | |
---|
| 44 | /* very small value for comparing floating point numbers with */ |
---|
[2b078c4] | 45 | #define EPSILON (REAL_EPSILON * 1000) |
---|
[d1b1380] | 46 | |
---|
| 47 | /* default threshold is 1cm */ |
---|
| 48 | #define DFLT_MAX_THRESHOLD 0.01 |
---|
| 49 | |
---|
[3262009] | 50 | static double threshold = DFLT_MAX_THRESHOLD; |
---|
[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 | |
---|
[3262009] | 66 | /* some (preferably prime) number for the hashing function */ |
---|
| 67 | #define HASH_PRIME 29363 |
---|
| 68 | |
---|
| 69 | typedef struct station { |
---|
| 70 | struct station *next; |
---|
| 71 | char *name; |
---|
| 72 | img_point pt; |
---|
| 73 | } station; |
---|
| 74 | |
---|
[c0635e7] | 75 | typedef struct added { |
---|
| 76 | struct added *next; |
---|
| 77 | char *name; |
---|
| 78 | } added; |
---|
| 79 | |
---|
[3262009] | 80 | static int |
---|
| 81 | hash_string(const char *p) |
---|
| 82 | { |
---|
| 83 | int hash; |
---|
| 84 | ASSERT(p != NULL); /* can't hash NULL */ |
---|
| 85 | /* printf("HASH `%s' to ",p); */ |
---|
| 86 | for (hash = 0; *p; p++) |
---|
| 87 | hash = (hash * HASH_PRIME + tolower(*(unsigned char*)p)) & 0x7fff; |
---|
| 88 | /* printf("%d\n",hash); */ |
---|
| 89 | return hash; |
---|
| 90 | } |
---|
| 91 | |
---|
[a20d9b9] | 92 | static int |
---|
| 93 | cmp_pname(const void *a, const void *b) |
---|
| 94 | { |
---|
| 95 | return name_cmp(*(const char **)a, *(const char **)b); |
---|
| 96 | } |
---|
| 97 | |
---|
[3262009] | 98 | static station **htab; |
---|
| 99 | static bool fChanged = fFalse; |
---|
| 100 | |
---|
[c0635e7] | 101 | static added *added_list = NULL;; |
---|
| 102 | OSSIZE_T c_added = 0; |
---|
| 103 | |
---|
[3262009] | 104 | static void |
---|
| 105 | tree_init(void) |
---|
| 106 | { |
---|
| 107 | size_t i; |
---|
| 108 | htab = osmalloc(0x2000 * sizeof(int)); |
---|
| 109 | for (i = 0; i < 0x2000; i++) htab[i] = NULL; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | static void |
---|
| 113 | tree_insert(const char *name, const img_point *pt) |
---|
| 114 | { |
---|
| 115 | int v = hash_string(name) & 0x1fff; |
---|
| 116 | station *stn; |
---|
| 117 | #if 1 |
---|
| 118 | /* need to allow for duplicate labels ... */ |
---|
| 119 | for (stn = htab[v]; stn; stn = stn->next) { |
---|
| 120 | if (strcmp(stn->name, name) == 0) return; /* found dup */ |
---|
| 121 | } |
---|
| 122 | #endif |
---|
| 123 | stn = osnew(station); |
---|
| 124 | stn->name = osstrdup(name); |
---|
| 125 | stn->pt = *pt; |
---|
| 126 | stn->next = htab[v]; |
---|
| 127 | htab[v] = stn; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | static void |
---|
| 131 | tree_remove(const char *name, const img_point *pt) |
---|
| 132 | { |
---|
| 133 | int v = hash_string(name) & 0x1fff; |
---|
| 134 | station **prev; |
---|
| 135 | station *p; |
---|
| 136 | |
---|
| 137 | for (prev = &htab[v]; *prev; prev = &((*prev)->next)) { |
---|
| 138 | if (strcmp((*prev)->name, name) == 0) break; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | if (!*prev) { |
---|
[c0635e7] | 142 | added *add = osnew(added); |
---|
| 143 | add->name = osstrdup(name); |
---|
| 144 | add->next = added_list; |
---|
| 145 | added_list = add; |
---|
| 146 | c_added++; |
---|
[3262009] | 147 | fChanged = fTrue; |
---|
| 148 | return; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | if (fabs(pt->x - (*prev)->pt.x) - threshold > EPSILON || |
---|
| 152 | fabs(pt->y - (*prev)->pt.y) - threshold > EPSILON || |
---|
| 153 | fabs(pt->z - (*prev)->pt.z) - threshold > EPSILON) { |
---|
| 154 | printf("Moved by (%3.2f,%3.2f,%3.2f): %s\n", |
---|
| 155 | pt->x - (*prev)->pt.x, |
---|
| 156 | pt->y - (*prev)->pt.y, |
---|
| 157 | pt->z - (*prev)->pt.z, |
---|
| 158 | name); |
---|
| 159 | fChanged = fTrue; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | osfree((*prev)->name); |
---|
| 163 | p = *prev; |
---|
| 164 | *prev = p->next; |
---|
| 165 | osfree(p); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | static int |
---|
| 169 | tree_check(void) |
---|
| 170 | { |
---|
[a20d9b9] | 171 | size_t c = 0; |
---|
| 172 | char **names; |
---|
[3262009] | 173 | size_t i; |
---|
[c0635e7] | 174 | |
---|
| 175 | if (c_added) { |
---|
| 176 | names = osmalloc(c_added * ossizeof(char *)); |
---|
| 177 | for (i = 0; i < c_added; i++) { |
---|
| 178 | added *old; |
---|
| 179 | ASSERT(added_list); |
---|
| 180 | names[i] = added_list->name; |
---|
| 181 | old = added_list; |
---|
| 182 | added_list = old->next; |
---|
| 183 | osfree(old); |
---|
| 184 | } |
---|
| 185 | ASSERT(added_list == NULL); |
---|
| 186 | qsort(names, c_added, sizeof(char *), cmp_pname); |
---|
| 187 | for (i = 0; i < c_added; i++) { |
---|
| 188 | printf("Added: %s\n", names[i]); |
---|
| 189 | osfree(names[i]); |
---|
| 190 | } |
---|
| 191 | osfree(names); |
---|
| 192 | } |
---|
| 193 | |
---|
[3262009] | 194 | for (i = 0; i < 0x2000; i++) { |
---|
| 195 | station *p; |
---|
[a20d9b9] | 196 | for (p = htab[i]; p; p = p->next) c++; |
---|
| 197 | } |
---|
| 198 | if (c == 0) return fChanged; |
---|
| 199 | |
---|
| 200 | names = osmalloc(c * ossizeof(char *)); |
---|
| 201 | c = 0; |
---|
| 202 | for (i = 0; i < 0x2000; i++) { |
---|
| 203 | station *p; |
---|
| 204 | for (p = htab[i]; p; p = p->next) names[c++] = p->name; |
---|
| 205 | } |
---|
| 206 | qsort(names, c, sizeof(char *), cmp_pname); |
---|
| 207 | for (i = 0; i < c; i++) { |
---|
[c0635e7] | 208 | printf("Deleted: %s\n", names[i]); |
---|
[3262009] | 209 | } |
---|
[a20d9b9] | 210 | return fTrue; |
---|
[3262009] | 211 | } |
---|
| 212 | |
---|
[a420b49] | 213 | int |
---|
[acc20b1] | 214 | main(int argc, char **argv) |
---|
[a420b49] | 215 | { |
---|
[73a8a94] | 216 | char *fnm1, *fnm2; |
---|
[3262009] | 217 | char *buf; |
---|
| 218 | size_t len, buf_len = 256; |
---|
| 219 | const char ext3d[] = EXT_SVX_3D; |
---|
[acc20b1] | 220 | |
---|
[d06141c] | 221 | msg_init(argv[0]); |
---|
| 222 | |
---|
[3262009] | 223 | cmdline_set_syntax_message("FILE1 FILE2 [THRESHOLD]", |
---|
| 224 | "FILE1 and FILE2 can be .pos or .3d files\n" |
---|
[acc20b1] | 225 | "THRESHOLD is the max. ignorable change along " |
---|
| 226 | "any axis in metres (default " |
---|
| 227 | STRING(DFLT_MAX_THRESHOLD)")"); |
---|
| 228 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3); |
---|
| 229 | while (cmdline_getopt() != EOF) { |
---|
| 230 | /* do nothing */ |
---|
| 231 | } |
---|
| 232 | fnm1 = argv[optind++]; |
---|
| 233 | fnm2 = argv[optind++]; |
---|
| 234 | if (argv[optind]) { |
---|
| 235 | optarg = argv[optind]; |
---|
| 236 | threshold = cmdline_double_arg(); |
---|
[73a8a94] | 237 | } |
---|
[bfe1242] | 238 | |
---|
[3262009] | 239 | tree_init(); |
---|
[bfe1242] | 240 | |
---|
[3262009] | 241 | buf = osmalloc(buf_len); |
---|
[d1b1380] | 242 | |
---|
[3262009] | 243 | len = strlen(fnm1); |
---|
| 244 | if (len > sizeof(ext3d) && fnm1[len - sizeof(ext3d)] == FNM_SEP_EXT && |
---|
| 245 | strcmp(fnm1 + len - sizeof(ext3d) + 1, ext3d) == 0) { |
---|
| 246 | img_point pt; |
---|
| 247 | int result; |
---|
| 248 | img *pimg = img_open(fnm1, NULL, NULL); |
---|
| 249 | if (!pimg) fatalerror(img_error(), fnm1); |
---|
[cb3d1e2] | 250 | |
---|
[3262009] | 251 | do { |
---|
| 252 | result = img_read_item(pimg, &pt); |
---|
| 253 | switch (result) { |
---|
| 254 | case img_MOVE: |
---|
| 255 | case img_LINE: |
---|
| 256 | break; |
---|
| 257 | case img_LABEL: |
---|
| 258 | tree_insert(pimg->label, &pt); |
---|
| 259 | break; |
---|
| 260 | case img_BAD: |
---|
| 261 | img_close(pimg); |
---|
[113eba2] | 262 | fatalerror(/*Bad 3d image file `%s'*/106, fnm1); |
---|
[3262009] | 263 | } |
---|
| 264 | } while (result != img_STOP); |
---|
| 265 | |
---|
| 266 | img_close(pimg); |
---|
| 267 | } else { |
---|
| 268 | img_point pt; |
---|
[cb3d1e2] | 269 | |
---|
[3262009] | 270 | FILE *fh = fopen(fnm1, "rb"); |
---|
| 271 | if (!fh) fatalerror(/*Couldn't open file `%s'*/93, fnm1); |
---|
[cb3d1e2] | 272 | |
---|
[3262009] | 273 | while (1) { |
---|
| 274 | size_t off = 0; |
---|
| 275 | if (fscanf(fh, "(%lf,%lf,%lf ) ", &pt.x, &pt.y, &pt.z) != 3) { |
---|
| 276 | int ch; |
---|
| 277 | if (feof(fh)) break; |
---|
[113eba2] | 278 | printf("Skipping first\n"); /* FIXME: print the line */ |
---|
[3262009] | 279 | do { |
---|
| 280 | ch = getc(fh); |
---|
| 281 | } while (ch != EOF && ch != '\n'); |
---|
| 282 | continue; |
---|
| 283 | } |
---|
| 284 | buf[0] = '\0'; |
---|
| 285 | while (!feof(fh)) { |
---|
| 286 | if (!fgets(buf + off, buf_len - off, fh)) { |
---|
| 287 | /* FIXME */ |
---|
| 288 | break; |
---|
| 289 | } |
---|
| 290 | off += strlen(buf + off); |
---|
| 291 | if (off && buf[off - 1] == '\n') { |
---|
| 292 | buf[off - 1] = '\0'; |
---|
| 293 | break; |
---|
| 294 | } |
---|
| 295 | buf_len += buf_len; |
---|
| 296 | buf = osrealloc(buf, buf_len); |
---|
| 297 | } |
---|
| 298 | tree_insert(buf, &pt); |
---|
[73a8a94] | 299 | } |
---|
[cb3d1e2] | 300 | |
---|
[3262009] | 301 | fclose(fh); |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | len = strlen(fnm2); |
---|
| 305 | if (len > sizeof(ext3d) && fnm2[len - sizeof(ext3d)] == FNM_SEP_EXT && |
---|
| 306 | strcmp(fnm2 + len - sizeof(ext3d) + 1, ext3d) == 0) { |
---|
| 307 | img_point pt; |
---|
| 308 | int result; |
---|
| 309 | img *pimg = img_open(fnm2, NULL, NULL); |
---|
| 310 | if (!pimg) fatalerror(img_error(), fnm2); |
---|
| 311 | do { |
---|
| 312 | result = img_read_item(pimg, &pt); |
---|
| 313 | switch (result) { |
---|
| 314 | case img_MOVE: |
---|
| 315 | case img_LINE: |
---|
| 316 | break; |
---|
| 317 | case img_LABEL: |
---|
| 318 | tree_remove(pimg->label, &pt); |
---|
| 319 | break; |
---|
| 320 | case img_BAD: |
---|
| 321 | img_close(pimg); |
---|
[113eba2] | 322 | fatalerror(/*Bad 3d image file `%s'*/106, fnm2); |
---|
[3262009] | 323 | } |
---|
| 324 | } while (result != img_STOP); |
---|
| 325 | |
---|
| 326 | img_close(pimg); |
---|
| 327 | } else { |
---|
| 328 | img_point pt; |
---|
| 329 | |
---|
| 330 | FILE *fh = fopen(fnm2, "rb"); |
---|
| 331 | if (!fh) fatalerror(/*Couldn't open file `%s'*/93, fnm2); |
---|
[cb3d1e2] | 332 | |
---|
[3262009] | 333 | while (1) { |
---|
| 334 | size_t off = 0; |
---|
| 335 | if (fscanf(fh, "(%lf,%lf,%lf ) ", &pt.x, &pt.y, &pt.z) != 3) { |
---|
| 336 | int ch; |
---|
| 337 | if (feof(fh)) break; |
---|
[113eba2] | 338 | printf("Skipping second\n"); /* FIXME: print the line */ |
---|
[3262009] | 339 | do { |
---|
| 340 | ch = getc(fh); |
---|
| 341 | } while (ch != EOF && ch != '\n'); |
---|
| 342 | /* FIXME */ |
---|
| 343 | continue; |
---|
| 344 | } |
---|
| 345 | buf[0] = '\0'; |
---|
| 346 | while (!feof(fh)) { |
---|
| 347 | if (!fgets(buf + off, buf_len - off, fh)) { |
---|
| 348 | /* FIXME */ |
---|
| 349 | break; |
---|
[3f93024] | 350 | } |
---|
[3262009] | 351 | off += strlen(buf + off); |
---|
| 352 | if (off && buf[off - 1] == '\n') { |
---|
| 353 | buf[off - 1] = '\0'; |
---|
| 354 | break; |
---|
[3f93024] | 355 | } |
---|
[3262009] | 356 | buf_len += buf_len; |
---|
| 357 | buf = osrealloc(buf, buf_len); |
---|
[3f93024] | 358 | } |
---|
[3262009] | 359 | tree_remove(buf, &pt); |
---|
[73a8a94] | 360 | } |
---|
[d1b1380] | 361 | |
---|
[3262009] | 362 | fclose(fh); |
---|
[73a8a94] | 363 | } |
---|
[3262009] | 364 | |
---|
| 365 | return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS; |
---|
[d1b1380] | 366 | } |
---|