1 | /* > diffpos.c */ |
---|
2 | /* Utility to compare two SURVEX .pos or .3d files */ |
---|
3 | /* Copyright (C) 1994,1996,1998,1999,2001 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 "img.h" |
---|
34 | #include "namecmp.h" |
---|
35 | |
---|
36 | #ifndef sqrd |
---|
37 | # define sqrd(X) ((X)*(X)) |
---|
38 | #endif |
---|
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 */ |
---|
45 | #define EPSILON (REAL_EPSILON * 1000) |
---|
46 | |
---|
47 | /* default threshold is 1cm */ |
---|
48 | #define DFLT_MAX_THRESHOLD 0.01 |
---|
49 | |
---|
50 | static double threshold = DFLT_MAX_THRESHOLD; |
---|
51 | |
---|
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 | |
---|
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 | |
---|
75 | typedef struct added { |
---|
76 | struct added *next; |
---|
77 | char *name; |
---|
78 | } added; |
---|
79 | |
---|
80 | static int |
---|
81 | hash_string(const char *p) |
---|
82 | { |
---|
83 | int hash; |
---|
84 | ASSERT(p != NULL); /* can't hash NULL */ |
---|
85 | for (hash = 0; *p; p++) |
---|
86 | hash = (hash * HASH_PRIME + tolower(*(unsigned char*)p)) & 0x7fff; |
---|
87 | return hash; |
---|
88 | } |
---|
89 | |
---|
90 | static int |
---|
91 | cmp_pname(const void *a, const void *b) |
---|
92 | { |
---|
93 | return name_cmp(*(const char **)a, *(const char **)b); |
---|
94 | } |
---|
95 | |
---|
96 | static station **htab; |
---|
97 | static bool fChanged = fFalse; |
---|
98 | |
---|
99 | static added *added_list = NULL;; |
---|
100 | OSSIZE_T c_added = 0; |
---|
101 | |
---|
102 | static void |
---|
103 | tree_init(void) |
---|
104 | { |
---|
105 | size_t i; |
---|
106 | htab = osmalloc(0x2000 * sizeof(int)); |
---|
107 | for (i = 0; i < 0x2000; i++) htab[i] = NULL; |
---|
108 | } |
---|
109 | |
---|
110 | static void |
---|
111 | tree_insert(const char *name, const img_point *pt) |
---|
112 | { |
---|
113 | int v = hash_string(name) & 0x1fff; |
---|
114 | station *stn; |
---|
115 | |
---|
116 | /* Catch duplicate labels - .3d files shouldn't have them, but some do |
---|
117 | * due to a couple of bugs in some versions of Survex |
---|
118 | */ |
---|
119 | for (stn = htab[v]; stn; stn = stn->next) { |
---|
120 | if (strcmp(stn->name, name) == 0) return; /* found dup */ |
---|
121 | } |
---|
122 | |
---|
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) { |
---|
142 | added *add = osnew(added); |
---|
143 | add->name = osstrdup(name); |
---|
144 | add->next = added_list; |
---|
145 | added_list = add; |
---|
146 | c_added++; |
---|
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 | { |
---|
171 | size_t c = 0; |
---|
172 | char **names; |
---|
173 | size_t i; |
---|
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 | |
---|
194 | for (i = 0; i < 0x2000; i++) { |
---|
195 | station *p; |
---|
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++) { |
---|
208 | printf("Deleted: %s\n", names[i]); |
---|
209 | } |
---|
210 | return fTrue; |
---|
211 | } |
---|
212 | |
---|
213 | static void |
---|
214 | parse_3d_file(const char *fnm, |
---|
215 | void (*tree_func)(const char *, const img_point *)) |
---|
216 | { |
---|
217 | img_point pt; |
---|
218 | int result; |
---|
219 | |
---|
220 | img *pimg = img_open(fnm, NULL, NULL); |
---|
221 | if (!pimg) fatalerror(img_error(), fnm); |
---|
222 | |
---|
223 | do { |
---|
224 | result = img_read_item(pimg, &pt); |
---|
225 | switch (result) { |
---|
226 | case img_MOVE: |
---|
227 | case img_LINE: |
---|
228 | break; |
---|
229 | case img_LABEL: |
---|
230 | tree_func(pimg->label, &pt); |
---|
231 | break; |
---|
232 | case img_BAD: |
---|
233 | img_close(pimg); |
---|
234 | fatalerror(/*Bad 3d image file `%s'*/106, fnm); |
---|
235 | } |
---|
236 | } while (result != img_STOP); |
---|
237 | |
---|
238 | img_close(pimg); |
---|
239 | } |
---|
240 | |
---|
241 | static void |
---|
242 | parse_pos_file(const char *fnm, |
---|
243 | void (*tree_func)(const char *, const img_point *)) |
---|
244 | { |
---|
245 | FILE *fh; |
---|
246 | img_point pt; |
---|
247 | char *buf; |
---|
248 | size_t buf_len = 256; |
---|
249 | |
---|
250 | buf = osmalloc(buf_len); |
---|
251 | |
---|
252 | fh = fopen(fnm, "rb"); |
---|
253 | if (!fh) fatalerror(/*Couldn't open file `%s'*/93, fnm); |
---|
254 | |
---|
255 | while (1) { |
---|
256 | size_t off = 0; |
---|
257 | long fp = ftell(fh); |
---|
258 | if (fscanf(fh, "(%lf,%lf,%lf ) ", &pt.x, &pt.y, &pt.z) != 3) { |
---|
259 | int ch; |
---|
260 | fseek(fh, SEEK_SET, fp); |
---|
261 | ch = getc(fh); |
---|
262 | if (ch == EOF) break; |
---|
263 | |
---|
264 | printf("%s: Ignoring: ", fnm); |
---|
265 | while (ch != '\n' && ch != '\r' && ch != EOF) { |
---|
266 | putchar(ch); |
---|
267 | ch = getc(fh); |
---|
268 | } |
---|
269 | putchar('\n'); |
---|
270 | |
---|
271 | if (feof(fh)) break; |
---|
272 | continue; |
---|
273 | } |
---|
274 | buf[0] = '\0'; |
---|
275 | while (!feof(fh)) { |
---|
276 | if (!fgets(buf + off, buf_len - off, fh)) { |
---|
277 | /* FIXME */ |
---|
278 | break; |
---|
279 | } |
---|
280 | off += strlen(buf + off); |
---|
281 | if (off && buf[off - 1] == '\n') { |
---|
282 | buf[off - 1] = '\0'; |
---|
283 | break; |
---|
284 | } |
---|
285 | buf_len += buf_len; |
---|
286 | buf = osrealloc(buf, buf_len); |
---|
287 | } |
---|
288 | tree_func(buf, &pt); |
---|
289 | } |
---|
290 | fclose(fh); |
---|
291 | |
---|
292 | osfree(buf); |
---|
293 | } |
---|
294 | |
---|
295 | static void |
---|
296 | parse_file(const char *fnm, |
---|
297 | void (*tree_func)(const char *, const img_point *)) |
---|
298 | { |
---|
299 | static const char ext3d[] = EXT_SVX_3D; |
---|
300 | size_t len = strlen(fnm); |
---|
301 | if (len > sizeof(ext3d) && fnm[len - sizeof(ext3d)] == FNM_SEP_EXT && |
---|
302 | strcmp(fnm + len - sizeof(ext3d) + 1, ext3d) == 0) { |
---|
303 | parse_3d_file(fnm, tree_func); |
---|
304 | } else { |
---|
305 | parse_pos_file(fnm, tree_func); |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | int |
---|
310 | main(int argc, char **argv) |
---|
311 | { |
---|
312 | char *fnm1, *fnm2; |
---|
313 | msg_init(argv[0]); |
---|
314 | |
---|
315 | cmdline_set_syntax_message("FILE1 FILE2 [THRESHOLD]", |
---|
316 | "FILE1 and FILE2 can be .pos or .3d files\n" |
---|
317 | "THRESHOLD is the max. ignorable change along " |
---|
318 | "any axis in metres (default " |
---|
319 | STRING(DFLT_MAX_THRESHOLD)")"); |
---|
320 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3); |
---|
321 | while (cmdline_getopt() != EOF) { |
---|
322 | /* do nothing */ |
---|
323 | } |
---|
324 | fnm1 = argv[optind++]; |
---|
325 | fnm2 = argv[optind++]; |
---|
326 | if (argv[optind]) { |
---|
327 | optarg = argv[optind]; |
---|
328 | threshold = cmdline_double_arg(); |
---|
329 | } |
---|
330 | |
---|
331 | tree_init(); |
---|
332 | |
---|
333 | parse_file(fnm1, tree_insert); |
---|
334 | |
---|
335 | parse_file(fnm2, tree_remove); |
---|
336 | |
---|
337 | return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS; |
---|
338 | } |
---|