1 | /* diffpos.c */ |
---|
2 | /* Utility to compare two SURVEX .pos or .3d files */ |
---|
3 | /* Copyright (C) 1994,1996,1998-2003 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 "cmdline.h" |
---|
30 | #include "debug.h" |
---|
31 | #include "filelist.h" |
---|
32 | #include "hash.h" |
---|
33 | #include "img.h" |
---|
34 | #include "namecmp.h" |
---|
35 | #include "useful.h" |
---|
36 | |
---|
37 | /* Don't complain if values mismatch by a tiny amount (1e-6m i.e. 0.001mm) */ |
---|
38 | #define TOLERANCE 1e-6 |
---|
39 | |
---|
40 | /* default threshold is 1cm */ |
---|
41 | #define DFLT_MAX_THRESHOLD 0.01 |
---|
42 | |
---|
43 | static double threshold = DFLT_MAX_THRESHOLD; |
---|
44 | |
---|
45 | static const struct option long_opts[] = { |
---|
46 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
---|
47 | {"survey", required_argument, 0, 's'}, |
---|
48 | {"help", no_argument, 0, HLP_HELP}, |
---|
49 | {"version", no_argument, 0, HLP_VERSION}, |
---|
50 | {0, 0, 0, 0} |
---|
51 | }; |
---|
52 | |
---|
53 | #define short_opts "s:" |
---|
54 | |
---|
55 | static struct help_msg help[] = { |
---|
56 | /* <-- */ |
---|
57 | {HLP_ENCODELONG(0), "only load the sub-survey with this prefix"}, |
---|
58 | {0, 0} |
---|
59 | }; |
---|
60 | |
---|
61 | /* We use a hashtable with linked list buckets - this is how many hash table |
---|
62 | * entries we have. 0x2000 with sizeof(void *) == 4 uses 32K. */ |
---|
63 | #define TREE_SIZE 0x2000 |
---|
64 | |
---|
65 | typedef struct station { |
---|
66 | struct station *next; |
---|
67 | char *name; |
---|
68 | img_point pt; |
---|
69 | } station; |
---|
70 | |
---|
71 | typedef struct added { |
---|
72 | struct added *next; |
---|
73 | char *name; |
---|
74 | } added; |
---|
75 | |
---|
76 | static int old_separator, new_separator, sort_separator; |
---|
77 | |
---|
78 | static int |
---|
79 | cmp_pname(const void *a, const void *b) |
---|
80 | { |
---|
81 | return name_cmp(*(const char **)a, *(const char **)b, sort_separator); |
---|
82 | } |
---|
83 | |
---|
84 | static station **htab; |
---|
85 | static bool fChanged = fFalse; |
---|
86 | |
---|
87 | static added *added_list = NULL; |
---|
88 | static OSSIZE_T c_added = 0; |
---|
89 | |
---|
90 | static void |
---|
91 | tree_init(void) |
---|
92 | { |
---|
93 | size_t i; |
---|
94 | htab = osmalloc(TREE_SIZE * ossizeof(station *)); |
---|
95 | for (i = 0; i < TREE_SIZE; i++) htab[i] = NULL; |
---|
96 | } |
---|
97 | |
---|
98 | static void |
---|
99 | tree_insert(const char *name, const img_point *pt) |
---|
100 | { |
---|
101 | int v = hash_string(name) & (TREE_SIZE - 1); |
---|
102 | station *stn; |
---|
103 | |
---|
104 | /* Catch duplicate labels - .3d files shouldn't have them, but some do |
---|
105 | * due to a couple of bugs in some versions of Survex |
---|
106 | */ |
---|
107 | for (stn = htab[v]; stn; stn = stn->next) { |
---|
108 | if (strcmp(stn->name, name) == 0) return; /* found dup */ |
---|
109 | } |
---|
110 | |
---|
111 | stn = osnew(station); |
---|
112 | stn->name = osstrdup(name); |
---|
113 | stn->pt = *pt; |
---|
114 | stn->next = htab[v]; |
---|
115 | htab[v] = stn; |
---|
116 | } |
---|
117 | |
---|
118 | static void |
---|
119 | tree_remove(const char *name, const img_point *pt) |
---|
120 | { |
---|
121 | int v = hash_string(name) & (TREE_SIZE - 1); |
---|
122 | station **prev; |
---|
123 | station *p; |
---|
124 | |
---|
125 | for (prev = &htab[v]; *prev; prev = &((*prev)->next)) { |
---|
126 | if (strcmp((*prev)->name, name) == 0) break; |
---|
127 | } |
---|
128 | |
---|
129 | if (!*prev) { |
---|
130 | added *add = osnew(added); |
---|
131 | add->name = osstrdup(name); |
---|
132 | add->next = added_list; |
---|
133 | added_list = add; |
---|
134 | c_added++; |
---|
135 | fChanged = fTrue; |
---|
136 | return; |
---|
137 | } |
---|
138 | |
---|
139 | if (fabs(pt->x - (*prev)->pt.x) - threshold > TOLERANCE || |
---|
140 | fabs(pt->y - (*prev)->pt.y) - threshold > TOLERANCE || |
---|
141 | fabs(pt->z - (*prev)->pt.z) - threshold > TOLERANCE) { |
---|
142 | printf(msg(/*Moved by (%3.2f,%3.2f,%3.2f): %s*/500), |
---|
143 | pt->x - (*prev)->pt.x, |
---|
144 | pt->y - (*prev)->pt.y, |
---|
145 | pt->z - (*prev)->pt.z, |
---|
146 | name); |
---|
147 | putnl(); |
---|
148 | fChanged = fTrue; |
---|
149 | } |
---|
150 | |
---|
151 | osfree((*prev)->name); |
---|
152 | p = *prev; |
---|
153 | *prev = p->next; |
---|
154 | osfree(p); |
---|
155 | } |
---|
156 | |
---|
157 | static int |
---|
158 | tree_check(void) |
---|
159 | { |
---|
160 | size_t c = 0; |
---|
161 | char **names; |
---|
162 | size_t i; |
---|
163 | |
---|
164 | if (c_added) { |
---|
165 | names = osmalloc(c_added * ossizeof(char *)); |
---|
166 | for (i = 0; i < c_added; i++) { |
---|
167 | added *old; |
---|
168 | SVX_ASSERT(added_list); |
---|
169 | names[i] = added_list->name; |
---|
170 | old = added_list; |
---|
171 | added_list = old->next; |
---|
172 | osfree(old); |
---|
173 | } |
---|
174 | SVX_ASSERT(added_list == NULL); |
---|
175 | sort_separator = new_separator; |
---|
176 | qsort(names, c_added, sizeof(char *), cmp_pname); |
---|
177 | for (i = 0; i < c_added; i++) { |
---|
178 | printf(msg(/*Added: %s*/501), names[i]); |
---|
179 | putnl(); |
---|
180 | osfree(names[i]); |
---|
181 | } |
---|
182 | osfree(names); |
---|
183 | } |
---|
184 | |
---|
185 | for (i = 0; i < TREE_SIZE; i++) { |
---|
186 | station *p; |
---|
187 | for (p = htab[i]; p; p = p->next) c++; |
---|
188 | } |
---|
189 | if (c == 0) return fChanged; |
---|
190 | |
---|
191 | names = osmalloc(c * ossizeof(char *)); |
---|
192 | c = 0; |
---|
193 | for (i = 0; i < TREE_SIZE; i++) { |
---|
194 | station *p; |
---|
195 | for (p = htab[i]; p; p = p->next) names[c++] = p->name; |
---|
196 | } |
---|
197 | sort_separator = old_separator; |
---|
198 | qsort(names, c, sizeof(char *), cmp_pname); |
---|
199 | for (i = 0; i < c; i++) { |
---|
200 | printf(msg(/*Deleted: %s*/502), names[i]); |
---|
201 | putnl(); |
---|
202 | } |
---|
203 | return fTrue; |
---|
204 | } |
---|
205 | |
---|
206 | static int |
---|
207 | parse_file(const char *fnm, const char *survey, |
---|
208 | void (*tree_func)(const char *, const img_point *)) |
---|
209 | { |
---|
210 | img_point pt; |
---|
211 | int result; |
---|
212 | int separator; |
---|
213 | |
---|
214 | img *pimg = img_open_survey(fnm, survey); |
---|
215 | if (!pimg) fatalerror(img_error(), fnm); |
---|
216 | separator = pimg->separator; |
---|
217 | |
---|
218 | do { |
---|
219 | result = img_read_item(pimg, &pt); |
---|
220 | switch (result) { |
---|
221 | case img_MOVE: |
---|
222 | case img_LINE: |
---|
223 | break; |
---|
224 | case img_LABEL: |
---|
225 | tree_func(pimg->label, &pt); |
---|
226 | break; |
---|
227 | case img_BAD: |
---|
228 | img_close(pimg); |
---|
229 | fatalerror(img_error(), fnm); |
---|
230 | } |
---|
231 | } while (result != img_STOP); |
---|
232 | |
---|
233 | img_close(pimg); |
---|
234 | return separator; |
---|
235 | } |
---|
236 | |
---|
237 | int |
---|
238 | main(int argc, char **argv) |
---|
239 | { |
---|
240 | char *fnm1, *fnm2; |
---|
241 | const char *survey = NULL; |
---|
242 | |
---|
243 | msg_init(argv); |
---|
244 | |
---|
245 | cmdline_set_syntax_message("FILE1 FILE2 [THRESHOLD]", |
---|
246 | "FILE1 and FILE2 can be .pos or .3d files\n" |
---|
247 | "THRESHOLD is the max. ignorable change along " |
---|
248 | "any axis in metres (default " |
---|
249 | STRING(DFLT_MAX_THRESHOLD)")"); |
---|
250 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3); |
---|
251 | while (1) { |
---|
252 | int opt = cmdline_getopt(); |
---|
253 | if (opt == EOF) break; |
---|
254 | if (opt == 's') survey = optarg; |
---|
255 | } |
---|
256 | fnm1 = argv[optind++]; |
---|
257 | fnm2 = argv[optind++]; |
---|
258 | if (argv[optind]) { |
---|
259 | optarg = argv[optind]; |
---|
260 | threshold = cmdline_double_arg(); |
---|
261 | } |
---|
262 | |
---|
263 | tree_init(); |
---|
264 | |
---|
265 | old_separator = parse_file(fnm1, survey, tree_insert); |
---|
266 | |
---|
267 | new_separator = parse_file(fnm2, survey, tree_remove); |
---|
268 | |
---|
269 | return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS; |
---|
270 | } |
---|