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 | static int |
---|
76 | hash_string(const char *p) |
---|
77 | { |
---|
78 | int hash; |
---|
79 | ASSERT(p != NULL); /* can't hash NULL */ |
---|
80 | /* printf("HASH `%s' to ",p); */ |
---|
81 | for (hash = 0; *p; p++) |
---|
82 | hash = (hash * HASH_PRIME + tolower(*(unsigned char*)p)) & 0x7fff; |
---|
83 | /* printf("%d\n",hash); */ |
---|
84 | return hash; |
---|
85 | } |
---|
86 | |
---|
87 | static int |
---|
88 | cmp_pname(const void *a, const void *b) |
---|
89 | { |
---|
90 | return name_cmp(*(const char **)a, *(const char **)b); |
---|
91 | } |
---|
92 | |
---|
93 | static station **htab; |
---|
94 | static bool fChanged = fFalse; |
---|
95 | |
---|
96 | static void |
---|
97 | tree_init(void) |
---|
98 | { |
---|
99 | size_t i; |
---|
100 | htab = osmalloc(0x2000 * sizeof(int)); |
---|
101 | for (i = 0; i < 0x2000; i++) htab[i] = NULL; |
---|
102 | } |
---|
103 | |
---|
104 | static void |
---|
105 | tree_insert(const char *name, const img_point *pt) |
---|
106 | { |
---|
107 | int v = hash_string(name) & 0x1fff; |
---|
108 | station *stn; |
---|
109 | #if 1 |
---|
110 | /* need to allow for duplicate labels ... */ |
---|
111 | for (stn = htab[v]; stn; stn = stn->next) { |
---|
112 | if (strcmp(stn->name, name) == 0) return; /* found dup */ |
---|
113 | } |
---|
114 | #endif |
---|
115 | stn = osnew(station); |
---|
116 | stn->name = osstrdup(name); |
---|
117 | stn->pt = *pt; |
---|
118 | stn->next = htab[v]; |
---|
119 | htab[v] = stn; |
---|
120 | } |
---|
121 | |
---|
122 | static void |
---|
123 | tree_remove(const char *name, const img_point *pt) |
---|
124 | { |
---|
125 | int v = hash_string(name) & 0x1fff; |
---|
126 | station **prev; |
---|
127 | station *p; |
---|
128 | |
---|
129 | for (prev = &htab[v]; *prev; prev = &((*prev)->next)) { |
---|
130 | if (strcmp((*prev)->name, name) == 0) break; |
---|
131 | } |
---|
132 | |
---|
133 | if (!*prev) { |
---|
134 | printf("Added: %s\n", name); |
---|
135 | fChanged = fTrue; |
---|
136 | return; |
---|
137 | } |
---|
138 | |
---|
139 | if (fabs(pt->x - (*prev)->pt.x) - threshold > EPSILON || |
---|
140 | fabs(pt->y - (*prev)->pt.y) - threshold > EPSILON || |
---|
141 | fabs(pt->z - (*prev)->pt.z) - threshold > EPSILON) { |
---|
142 | printf("Moved by (%3.2f,%3.2f,%3.2f): %s\n", |
---|
143 | pt->x - (*prev)->pt.x, |
---|
144 | pt->y - (*prev)->pt.y, |
---|
145 | pt->z - (*prev)->pt.z, |
---|
146 | name); |
---|
147 | fChanged = fTrue; |
---|
148 | } |
---|
149 | |
---|
150 | osfree((*prev)->name); |
---|
151 | p = *prev; |
---|
152 | *prev = p->next; |
---|
153 | osfree(p); |
---|
154 | } |
---|
155 | |
---|
156 | static int |
---|
157 | tree_check(void) |
---|
158 | { |
---|
159 | size_t c = 0; |
---|
160 | char **names; |
---|
161 | size_t i; |
---|
162 | for (i = 0; i < 0x2000; i++) { |
---|
163 | station *p; |
---|
164 | for (p = htab[i]; p; p = p->next) c++; |
---|
165 | } |
---|
166 | if (c == 0) return fChanged; |
---|
167 | |
---|
168 | names = osmalloc(c * ossizeof(char *)); |
---|
169 | c = 0; |
---|
170 | for (i = 0; i < 0x2000; i++) { |
---|
171 | station *p; |
---|
172 | for (p = htab[i]; p; p = p->next) names[c++] = p->name; |
---|
173 | } |
---|
174 | qsort(names, c, sizeof(char *), cmp_pname); |
---|
175 | for (i = 0; i < c; i++) { |
---|
176 | printf("Deleted: %s\n", names[c]); |
---|
177 | } |
---|
178 | return fTrue; |
---|
179 | } |
---|
180 | |
---|
181 | int |
---|
182 | main(int argc, char **argv) |
---|
183 | { |
---|
184 | char *fnm1, *fnm2; |
---|
185 | char *buf; |
---|
186 | size_t len, buf_len = 256; |
---|
187 | const char ext3d[] = EXT_SVX_3D; |
---|
188 | |
---|
189 | msg_init(argv[0]); |
---|
190 | |
---|
191 | cmdline_set_syntax_message("FILE1 FILE2 [THRESHOLD]", |
---|
192 | "FILE1 and FILE2 can be .pos or .3d files\n" |
---|
193 | "THRESHOLD is the max. ignorable change along " |
---|
194 | "any axis in metres (default " |
---|
195 | STRING(DFLT_MAX_THRESHOLD)")"); |
---|
196 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3); |
---|
197 | while (cmdline_getopt() != EOF) { |
---|
198 | /* do nothing */ |
---|
199 | } |
---|
200 | fnm1 = argv[optind++]; |
---|
201 | fnm2 = argv[optind++]; |
---|
202 | if (argv[optind]) { |
---|
203 | optarg = argv[optind]; |
---|
204 | threshold = cmdline_double_arg(); |
---|
205 | } |
---|
206 | |
---|
207 | tree_init(); |
---|
208 | |
---|
209 | buf = osmalloc(buf_len); |
---|
210 | |
---|
211 | len = strlen(fnm1); |
---|
212 | if (len > sizeof(ext3d) && fnm1[len - sizeof(ext3d)] == FNM_SEP_EXT && |
---|
213 | strcmp(fnm1 + len - sizeof(ext3d) + 1, ext3d) == 0) { |
---|
214 | img_point pt; |
---|
215 | int result; |
---|
216 | img *pimg = img_open(fnm1, NULL, NULL); |
---|
217 | if (!pimg) fatalerror(img_error(), fnm1); |
---|
218 | |
---|
219 | do { |
---|
220 | result = img_read_item(pimg, &pt); |
---|
221 | switch (result) { |
---|
222 | case img_MOVE: |
---|
223 | case img_LINE: |
---|
224 | break; |
---|
225 | case img_LABEL: |
---|
226 | tree_insert(pimg->label, &pt); |
---|
227 | break; |
---|
228 | case img_BAD: |
---|
229 | img_close(pimg); |
---|
230 | exit(1); |
---|
231 | } |
---|
232 | } while (result != img_STOP); |
---|
233 | |
---|
234 | img_close(pimg); |
---|
235 | } else { |
---|
236 | img_point pt; |
---|
237 | |
---|
238 | FILE *fh = fopen(fnm1, "rb"); |
---|
239 | if (!fh) fatalerror(/*Couldn't open file `%s'*/93, fnm1); |
---|
240 | |
---|
241 | while (1) { |
---|
242 | size_t off = 0; |
---|
243 | if (fscanf(fh, "(%lf,%lf,%lf ) ", &pt.x, &pt.y, &pt.z) != 3) { |
---|
244 | int ch; |
---|
245 | if (feof(fh)) break; |
---|
246 | printf("Skipping first\n"); |
---|
247 | do { |
---|
248 | ch = getc(fh); |
---|
249 | } while (ch != EOF && ch != '\n'); |
---|
250 | /* FIXME */ |
---|
251 | continue; |
---|
252 | } |
---|
253 | buf[0] = '\0'; |
---|
254 | while (!feof(fh)) { |
---|
255 | if (!fgets(buf + off, buf_len - off, fh)) { |
---|
256 | /* FIXME */ |
---|
257 | break; |
---|
258 | } |
---|
259 | off += strlen(buf + off); |
---|
260 | if (off && buf[off - 1] == '\n') { |
---|
261 | buf[off - 1] = '\0'; |
---|
262 | break; |
---|
263 | } |
---|
264 | buf_len += buf_len; |
---|
265 | buf = osrealloc(buf, buf_len); |
---|
266 | } |
---|
267 | tree_insert(buf, &pt); |
---|
268 | } |
---|
269 | |
---|
270 | fclose(fh); |
---|
271 | } |
---|
272 | |
---|
273 | len = strlen(fnm2); |
---|
274 | if (len > sizeof(ext3d) && fnm2[len - sizeof(ext3d)] == FNM_SEP_EXT && |
---|
275 | strcmp(fnm2 + len - sizeof(ext3d) + 1, ext3d) == 0) { |
---|
276 | img_point pt; |
---|
277 | int result; |
---|
278 | img *pimg = img_open(fnm2, NULL, NULL); |
---|
279 | if (!pimg) fatalerror(img_error(), fnm2); |
---|
280 | do { |
---|
281 | result = img_read_item(pimg, &pt); |
---|
282 | switch (result) { |
---|
283 | case img_MOVE: |
---|
284 | case img_LINE: |
---|
285 | break; |
---|
286 | case img_LABEL: |
---|
287 | tree_remove(pimg->label, &pt); |
---|
288 | break; |
---|
289 | case img_BAD: |
---|
290 | img_close(pimg); |
---|
291 | exit(1); |
---|
292 | } |
---|
293 | } while (result != img_STOP); |
---|
294 | |
---|
295 | img_close(pimg); |
---|
296 | } else { |
---|
297 | img_point pt; |
---|
298 | |
---|
299 | FILE *fh = fopen(fnm2, "rb"); |
---|
300 | if (!fh) fatalerror(/*Couldn't open file `%s'*/93, fnm2); |
---|
301 | |
---|
302 | while (1) { |
---|
303 | size_t off = 0; |
---|
304 | if (fscanf(fh, "(%lf,%lf,%lf ) ", &pt.x, &pt.y, &pt.z) != 3) { |
---|
305 | int ch; |
---|
306 | if (feof(fh)) break; |
---|
307 | printf("Skipping second\n"); |
---|
308 | do { |
---|
309 | ch = getc(fh); |
---|
310 | } while (ch != EOF && ch != '\n'); |
---|
311 | /* FIXME */ |
---|
312 | continue; |
---|
313 | } |
---|
314 | buf[0] = '\0'; |
---|
315 | while (!feof(fh)) { |
---|
316 | if (!fgets(buf + off, buf_len - off, fh)) { |
---|
317 | /* FIXME */ |
---|
318 | break; |
---|
319 | } |
---|
320 | off += strlen(buf + off); |
---|
321 | if (off && buf[off - 1] == '\n') { |
---|
322 | buf[off - 1] = '\0'; |
---|
323 | break; |
---|
324 | } |
---|
325 | buf_len += buf_len; |
---|
326 | buf = osrealloc(buf, buf_len); |
---|
327 | } |
---|
328 | tree_remove(buf, &pt); |
---|
329 | } |
---|
330 | |
---|
331 | fclose(fh); |
---|
332 | } |
---|
333 | |
---|
334 | return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS; |
---|
335 | } |
---|