| 1 | /* diffpos.c */
|
|---|
| 2 | /* Utility to compare two SURVEX .pos or .3d files */
|
|---|
| 3 | /* Copyright (C) 1994,1996,1998-2002 Olly Betts
|
|---|
| 4 | *
|
|---|
| 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.
|
|---|
| 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
|
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | * GNU General Public License for more details.
|
|---|
| 14 | *
|
|---|
| 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
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | #ifdef HAVE_CONFIG_H
|
|---|
| 21 | # include <config.h>
|
|---|
| 22 | #endif
|
|---|
| 23 |
|
|---|
| 24 | #include <stdio.h>
|
|---|
| 25 | #include <stdlib.h>
|
|---|
| 26 | #include <string.h>
|
|---|
| 27 | #include <math.h>
|
|---|
| 28 |
|
|---|
| 29 | #include "cavern.h" /* just for REAL_EPSILON */
|
|---|
| 30 | #include "cmdline.h"
|
|---|
| 31 | #include "debug.h"
|
|---|
| 32 | #include "filelist.h"
|
|---|
| 33 | #include "hash.h"
|
|---|
| 34 | #include "img.h"
|
|---|
| 35 | #include "namecmp.h"
|
|---|
| 36 | #include "useful.h"
|
|---|
| 37 |
|
|---|
| 38 | /* very small value for comparing floating point numbers with */
|
|---|
| 39 | #define EPSILON (REAL_EPSILON * 1000)
|
|---|
| 40 |
|
|---|
| 41 | /* default threshold is 1cm */
|
|---|
| 42 | #define DFLT_MAX_THRESHOLD 0.01
|
|---|
| 43 |
|
|---|
| 44 | static double threshold = DFLT_MAX_THRESHOLD;
|
|---|
| 45 |
|
|---|
| 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; */
|
|---|
| 48 | {"survey", required_argument, 0, 's'},
|
|---|
| 49 | {"help", no_argument, 0, HLP_HELP},
|
|---|
| 50 | {"version", no_argument, 0, HLP_VERSION},
|
|---|
| 51 | {0, 0, 0, 0}
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | #define short_opts "s:"
|
|---|
| 55 |
|
|---|
| 56 | static struct help_msg help[] = {
|
|---|
| 57 | /* <-- */
|
|---|
| 58 | {HLP_ENCODELONG(0), "only load the sub-survey with this prefix"},
|
|---|
| 59 | {0, 0}
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| 62 | typedef struct station {
|
|---|
| 63 | struct station *next;
|
|---|
| 64 | char *name;
|
|---|
| 65 | img_point pt;
|
|---|
| 66 | } station;
|
|---|
| 67 |
|
|---|
| 68 | typedef struct added {
|
|---|
| 69 | struct added *next;
|
|---|
| 70 | char *name;
|
|---|
| 71 | } added;
|
|---|
| 72 |
|
|---|
| 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 |
|
|---|
| 79 | static station **htab;
|
|---|
| 80 | static bool fChanged = fFalse;
|
|---|
| 81 |
|
|---|
| 82 | static added *added_list = NULL;
|
|---|
| 83 | static OSSIZE_T c_added = 0;
|
|---|
| 84 |
|
|---|
| 85 | static void
|
|---|
| 86 | tree_init(void)
|
|---|
| 87 | {
|
|---|
| 88 | size_t i;
|
|---|
| 89 | htab = osmalloc(0x2000 * ossizeof(station *));
|
|---|
| 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;
|
|---|
| 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 | */
|
|---|
| 102 | for (stn = htab[v]; stn; stn = stn->next) {
|
|---|
| 103 | if (strcmp(stn->name, name) == 0) return; /* found dup */
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 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;
|
|---|
| 119 |
|
|---|
| 120 | for (prev = &htab[v]; *prev; prev = &((*prev)->next)) {
|
|---|
| 121 | if (strcmp((*prev)->name, name) == 0) break;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | if (!*prev) {
|
|---|
| 125 | added *add = osnew(added);
|
|---|
| 126 | add->name = osstrdup(name);
|
|---|
| 127 | add->next = added_list;
|
|---|
| 128 | added_list = add;
|
|---|
| 129 | c_added++;
|
|---|
| 130 | fChanged = fTrue;
|
|---|
| 131 | return;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 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) {
|
|---|
| 137 | printf(msg(/*Moved by (%3.2f,%3.2f,%3.2f): %s*/500),
|
|---|
| 138 | pt->x - (*prev)->pt.x,
|
|---|
| 139 | pt->y - (*prev)->pt.y,
|
|---|
| 140 | pt->z - (*prev)->pt.z,
|
|---|
| 141 | name);
|
|---|
| 142 | putnl();
|
|---|
| 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 | {
|
|---|
| 155 | size_t c = 0;
|
|---|
| 156 | char **names;
|
|---|
| 157 | size_t i;
|
|---|
| 158 |
|
|---|
| 159 | if (c_added) {
|
|---|
| 160 | names = osmalloc(c_added * ossizeof(char *));
|
|---|
| 161 | for (i = 0; i < c_added; i++) {
|
|---|
| 162 | added *old;
|
|---|
| 163 | SVX_ASSERT(added_list);
|
|---|
| 164 | names[i] = added_list->name;
|
|---|
| 165 | old = added_list;
|
|---|
| 166 | added_list = old->next;
|
|---|
| 167 | osfree(old);
|
|---|
| 168 | }
|
|---|
| 169 | SVX_ASSERT(added_list == NULL);
|
|---|
| 170 | qsort(names, c_added, sizeof(char *), cmp_pname);
|
|---|
| 171 | for (i = 0; i < c_added; i++) {
|
|---|
| 172 | printf(msg(/*Added: %s*/501), names[i]);
|
|---|
| 173 | putnl();
|
|---|
| 174 | osfree(names[i]);
|
|---|
| 175 | }
|
|---|
| 176 | osfree(names);
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | for (i = 0; i < 0x2000; i++) {
|
|---|
| 180 | station *p;
|
|---|
| 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++) {
|
|---|
| 193 | printf(msg(/*Deleted: %s*/502), names[i]);
|
|---|
| 194 | putnl();
|
|---|
| 195 | }
|
|---|
| 196 | return fTrue;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | static void
|
|---|
| 200 | parse_file(const char *fnm, const char *survey,
|
|---|
| 201 | void (*tree_func)(const char *, const img_point *))
|
|---|
| 202 | {
|
|---|
| 203 | img_point pt;
|
|---|
| 204 | int result;
|
|---|
| 205 |
|
|---|
| 206 | img *pimg = img_open_survey(fnm, survey);
|
|---|
| 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);
|
|---|
| 220 | fatalerror(img_error(), fnm);
|
|---|
| 221 | }
|
|---|
| 222 | } while (result != img_STOP);
|
|---|
| 223 |
|
|---|
| 224 | img_close(pimg);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | int
|
|---|
| 228 | main(int argc, char **argv)
|
|---|
| 229 | {
|
|---|
| 230 | char *fnm1, *fnm2;
|
|---|
| 231 | const char *survey = NULL;
|
|---|
| 232 |
|
|---|
| 233 | msg_init(argv);
|
|---|
| 234 |
|
|---|
| 235 | cmdline_set_syntax_message("FILE1 FILE2 [THRESHOLD]",
|
|---|
| 236 | "FILE1 and FILE2 can be .pos or .3d files\n"
|
|---|
| 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);
|
|---|
| 241 | while (1) {
|
|---|
| 242 | int opt = cmdline_getopt();
|
|---|
| 243 | if (opt == EOF) break;
|
|---|
| 244 | if (opt == 's') survey = optarg;
|
|---|
| 245 | }
|
|---|
| 246 | fnm1 = argv[optind++];
|
|---|
| 247 | fnm2 = argv[optind++];
|
|---|
| 248 | if (argv[optind]) {
|
|---|
| 249 | optarg = argv[optind];
|
|---|
| 250 | threshold = cmdline_double_arg();
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | tree_init();
|
|---|
| 254 |
|
|---|
| 255 | parse_file(fnm1, survey, tree_insert);
|
|---|
| 256 |
|
|---|
| 257 | parse_file(fnm2, survey, tree_remove);
|
|---|
| 258 |
|
|---|
| 259 | return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS;
|
|---|
| 260 | }
|
|---|