[0da6e60] | 1 | /* diffpos.c */ |
---|
[3262009] | 2 | /* Utility to compare two SURVEX .pos or .3d files */ |
---|
[fa42426] | 3 | /* Copyright (C) 1994,1996,1998-2002 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" |
---|
[fa42426] | 36 | #include "useful.h" |
---|
[d1b1380] | 37 | |
---|
| 38 | /* very small value for comparing floating point numbers with */ |
---|
[2b078c4] | 39 | #define EPSILON (REAL_EPSILON * 1000) |
---|
[d1b1380] | 40 | |
---|
| 41 | /* default threshold is 1cm */ |
---|
| 42 | #define DFLT_MAX_THRESHOLD 0.01 |
---|
| 43 | |
---|
[3262009] | 44 | static double threshold = DFLT_MAX_THRESHOLD; |
---|
[d1b1380] | 45 | |
---|
[acc20b1] | 46 | static const struct option long_opts[] = { |
---|
| 47 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
---|
[76bbb7c9] | 48 | {"survey", required_argument, 0, 's'}, |
---|
[acc20b1] | 49 | {"help", no_argument, 0, HLP_HELP}, |
---|
| 50 | {"version", no_argument, 0, HLP_VERSION}, |
---|
| 51 | {0, 0, 0, 0} |
---|
| 52 | }; |
---|
| 53 | |
---|
[76bbb7c9] | 54 | #define short_opts "s:" |
---|
[acc20b1] | 55 | |
---|
| 56 | static struct help_msg help[] = { |
---|
| 57 | /* <-- */ |
---|
[e4138b5] | 58 | {HLP_ENCODELONG(0), "only load the sub-survey with this prefix"}, |
---|
[acc20b1] | 59 | {0, 0} |
---|
| 60 | }; |
---|
| 61 | |
---|
[3262009] | 62 | typedef struct station { |
---|
| 63 | struct station *next; |
---|
| 64 | char *name; |
---|
| 65 | img_point pt; |
---|
| 66 | } station; |
---|
| 67 | |
---|
[c0635e7] | 68 | typedef struct added { |
---|
| 69 | struct added *next; |
---|
| 70 | char *name; |
---|
| 71 | } added; |
---|
| 72 | |
---|
[a20d9b9] | 73 | static int |
---|
| 74 | cmp_pname(const void *a, const void *b) |
---|
| 75 | { |
---|
| 76 | return name_cmp(*(const char **)a, *(const char **)b); |
---|
| 77 | } |
---|
| 78 | |
---|
[3262009] | 79 | static station **htab; |
---|
| 80 | static bool fChanged = fFalse; |
---|
| 81 | |
---|
[b07ae08] | 82 | static added *added_list = NULL; |
---|
| 83 | static OSSIZE_T c_added = 0; |
---|
[c0635e7] | 84 | |
---|
[3262009] | 85 | static void |
---|
| 86 | tree_init(void) |
---|
| 87 | { |
---|
| 88 | size_t i; |
---|
| 89 | htab = osmalloc(0x2000 * sizeof(int)); |
---|
| 90 | for (i = 0; i < 0x2000; i++) htab[i] = NULL; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | static void |
---|
| 94 | tree_insert(const char *name, const img_point *pt) |
---|
| 95 | { |
---|
| 96 | int v = hash_string(name) & 0x1fff; |
---|
| 97 | station *stn; |
---|
[840b213] | 98 | |
---|
| 99 | /* Catch duplicate labels - .3d files shouldn't have them, but some do |
---|
| 100 | * due to a couple of bugs in some versions of Survex |
---|
| 101 | */ |
---|
[3262009] | 102 | for (stn = htab[v]; stn; stn = stn->next) { |
---|
| 103 | if (strcmp(stn->name, name) == 0) return; /* found dup */ |
---|
| 104 | } |
---|
[840b213] | 105 | |
---|
[3262009] | 106 | stn = osnew(station); |
---|
| 107 | stn->name = osstrdup(name); |
---|
| 108 | stn->pt = *pt; |
---|
| 109 | stn->next = htab[v]; |
---|
| 110 | htab[v] = stn; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | static void |
---|
| 114 | tree_remove(const char *name, const img_point *pt) |
---|
| 115 | { |
---|
| 116 | int v = hash_string(name) & 0x1fff; |
---|
| 117 | station **prev; |
---|
| 118 | station *p; |
---|
[421b7d2] | 119 | |
---|
[3262009] | 120 | for (prev = &htab[v]; *prev; prev = &((*prev)->next)) { |
---|
| 121 | if (strcmp((*prev)->name, name) == 0) break; |
---|
| 122 | } |
---|
[421b7d2] | 123 | |
---|
[3262009] | 124 | if (!*prev) { |
---|
[c0635e7] | 125 | added *add = osnew(added); |
---|
| 126 | add->name = osstrdup(name); |
---|
| 127 | add->next = added_list; |
---|
| 128 | added_list = add; |
---|
| 129 | c_added++; |
---|
[3262009] | 130 | fChanged = fTrue; |
---|
| 131 | return; |
---|
| 132 | } |
---|
[421b7d2] | 133 | |
---|
[3262009] | 134 | if (fabs(pt->x - (*prev)->pt.x) - threshold > EPSILON || |
---|
| 135 | fabs(pt->y - (*prev)->pt.y) - threshold > EPSILON || |
---|
| 136 | fabs(pt->z - (*prev)->pt.z) - threshold > EPSILON) { |
---|
[96e1f97] | 137 | printf(msg(/*Moved by (%3.2f,%3.2f,%3.2f): %s*/500), |
---|
[3262009] | 138 | pt->x - (*prev)->pt.x, |
---|
| 139 | pt->y - (*prev)->pt.y, |
---|
| 140 | pt->z - (*prev)->pt.z, |
---|
| 141 | name); |
---|
[96e1f97] | 142 | putnl(); |
---|
[3262009] | 143 | fChanged = fTrue; |
---|
| 144 | } |
---|
[421b7d2] | 145 | |
---|
[3262009] | 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++) { |
---|
[96e1f97] | 172 | printf(msg(/*Added: %s*/501), names[i]); |
---|
| 173 | putnl(); |
---|
[c0635e7] | 174 | osfree(names[i]); |
---|
| 175 | } |
---|
| 176 | osfree(names); |
---|
| 177 | } |
---|
| 178 | |
---|
[3262009] | 179 | for (i = 0; i < 0x2000; i++) { |
---|
| 180 | station *p; |
---|
[a20d9b9] | 181 | for (p = htab[i]; p; p = p->next) c++; |
---|
| 182 | } |
---|
| 183 | if (c == 0) return fChanged; |
---|
| 184 | |
---|
| 185 | names = osmalloc(c * ossizeof(char *)); |
---|
| 186 | c = 0; |
---|
| 187 | for (i = 0; i < 0x2000; i++) { |
---|
| 188 | station *p; |
---|
| 189 | for (p = htab[i]; p; p = p->next) names[c++] = p->name; |
---|
| 190 | } |
---|
| 191 | qsort(names, c, sizeof(char *), cmp_pname); |
---|
| 192 | for (i = 0; i < c; i++) { |
---|
[96e1f97] | 193 | printf(msg(/*Deleted: %s*/502), names[i]); |
---|
| 194 | putnl(); |
---|
[3262009] | 195 | } |
---|
[a20d9b9] | 196 | return fTrue; |
---|
[3262009] | 197 | } |
---|
| 198 | |
---|
[840b213] | 199 | static void |
---|
[0da6e60] | 200 | parse_file(const char *fnm, const char *survey, |
---|
| 201 | void (*tree_func)(const char *, const img_point *)) |
---|
[840b213] | 202 | { |
---|
| 203 | img_point pt; |
---|
| 204 | int result; |
---|
[421b7d2] | 205 | |
---|
[a2ad284] | 206 | img *pimg = img_open_survey(fnm, 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); |
---|
[421b7d2] | 223 | |
---|
[840b213] | 224 | img_close(pimg); |
---|
| 225 | } |
---|
| 226 | |
---|
[a420b49] | 227 | int |
---|
[acc20b1] | 228 | main(int argc, char **argv) |
---|
[a420b49] | 229 | { |
---|
[73a8a94] | 230 | char *fnm1, *fnm2; |
---|
[421b7d2] | 231 | const char *survey = NULL; |
---|
[76bbb7c9] | 232 | |
---|
[bdfe97f] | 233 | msg_init(argv); |
---|
[d06141c] | 234 | |
---|
[3262009] | 235 | cmdline_set_syntax_message("FILE1 FILE2 [THRESHOLD]", |
---|
[421b7d2] | 236 | "FILE1 and FILE2 can be .pos or .3d files\n" |
---|
[acc20b1] | 237 | "THRESHOLD is the max. ignorable change along " |
---|
| 238 | "any axis in metres (default " |
---|
| 239 | STRING(DFLT_MAX_THRESHOLD)")"); |
---|
| 240 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3); |
---|
[76bbb7c9] | 241 | while (1) { |
---|
| 242 | int opt = cmdline_getopt(); |
---|
| 243 | if (opt == EOF) break; |
---|
| 244 | if (opt == 's') survey = optarg; |
---|
[acc20b1] | 245 | } |
---|
| 246 | fnm1 = argv[optind++]; |
---|
| 247 | fnm2 = argv[optind++]; |
---|
| 248 | if (argv[optind]) { |
---|
| 249 | optarg = argv[optind]; |
---|
| 250 | threshold = cmdline_double_arg(); |
---|
[73a8a94] | 251 | } |
---|
[bfe1242] | 252 | |
---|
[3262009] | 253 | tree_init(); |
---|
[bfe1242] | 254 | |
---|
[76bbb7c9] | 255 | parse_file(fnm1, survey, tree_insert); |
---|
[3262009] | 256 | |
---|
[76bbb7c9] | 257 | parse_file(fnm2, survey, tree_remove); |
---|
[421b7d2] | 258 | |
---|
[3262009] | 259 | return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS; |
---|
[d1b1380] | 260 | } |
---|