source: git/src/diffpos.c @ 9d20d33

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since 9d20d33 was 9d20d33, checked in by Olly Betts <olly@…>, 13 years ago

src/diffpos.c: Handle files with duplicate labels in better - extend
generates duplicate labels when it breaks a loop.

git-svn-id: file:///home/survex-svn/survex/trunk@3555 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 7.1 KB
Line 
1/* diffpos.c */
2/* Utility to compare two SURVEX .pos or .3d files */
3/* Copyright (C) 1994,1996,1998-2003,2010 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  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
43static double threshold = DFLT_MAX_THRESHOLD;
44
45static 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
55static 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
65typedef struct station {
66   struct station *next;
67   char *name;
68   img_point pt;
69} station;
70
71typedef struct added {
72   struct added *next;
73   char *name;
74} added;
75
76static int old_separator, new_separator, sort_separator;
77
78static int
79cmp_pname(const void *a, const void *b)
80{
81   return name_cmp(*(const char **)a, *(const char **)b, sort_separator);
82}
83
84static station **htab;
85static bool fChanged = fFalse;
86
87static added *added_list = NULL;
88static OSSIZE_T c_added = 0;
89
90static void
91tree_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
98static void
99tree_insert(const char *name, const img_point *pt)
100{
101   int v = hash_string(name) & (TREE_SIZE - 1);
102   station * stn = osnew(station);
103   stn->name = osstrdup(name);
104   stn->pt = *pt;
105   stn->next = htab[v];
106   htab[v] = stn;
107}
108
109static int
110close_enough(const img_point * p1, const img_point * p2)
111{
112    return fabs(p1->x - p2->x) - threshold <= TOLERANCE &&
113           fabs(p1->y - p2->y) - threshold <= TOLERANCE &&
114           fabs(p1->z - p2->z) - threshold <= TOLERANCE;
115}
116
117static void
118tree_remove(const char *name, const img_point *pt)
119{
120   /* We need to handle duplicate labels - normal .3d files shouldn't have them
121    * (though some older ones do due to a couple of bugs in earlier versions of
122    * Survex) but extended .3d files repeat the label where a loop is broken,
123    * and data read from foreign formats might repeat labels.
124    */
125   int v = hash_string(name) & (TREE_SIZE - 1);
126   station **prev;
127   station *p;
128   station **found = NULL;
129   bool was_close_enough = fFalse;
130
131   for (prev = &htab[v]; *prev; prev = &((*prev)->next)) {
132      if (strcmp((*prev)->name, name) == 0) {
133         /* Handle stations with the same name.  Stations are inserted at the
134          * start of the linked list, so pick the *last* matching station in
135          * the list as then we match the first stations with the same name in
136          * each file.
137          */
138         if (close_enough(pt, &((*prev)->pt))) {
139            found = prev;
140            was_close_enough = fTrue;
141         } else if (!was_close_enough) {
142            found = prev;
143         }
144      }
145   }
146
147   if (!found) {
148      added *add = osnew(added);
149      add->name = osstrdup(name);
150      add->next = added_list;
151      added_list = add;
152      c_added++;
153      fChanged = fTrue;
154      return;
155   }
156
157   if (!was_close_enough) {
158      printf(msg(/*Moved by (%3.2f,%3.2f,%3.2f): %s*/500),
159             pt->x - (*found)->pt.x,
160             pt->y - (*found)->pt.y,
161             pt->z - (*found)->pt.z,
162             name);
163      putnl();
164      fChanged = fTrue;
165   }
166
167   osfree((*found)->name);
168   p = *found;
169   *found = p->next;
170   osfree(p);
171}
172
173static int
174tree_check(void)
175{
176   size_t c = 0;
177   char **names;
178   size_t i;
179
180   if (c_added) {
181      names = osmalloc(c_added * ossizeof(char *));
182      for (i = 0; i < c_added; i++) {
183         added *old;
184         SVX_ASSERT(added_list);
185         names[i] = added_list->name;
186         old = added_list;
187         added_list = old->next;
188         osfree(old);
189      }
190      SVX_ASSERT(added_list == NULL);
191      sort_separator = new_separator;
192      qsort(names, c_added, sizeof(char *), cmp_pname);
193      for (i = 0; i < c_added; i++) {
194         printf(msg(/*Added: %s*/501), names[i]);
195         putnl();
196         osfree(names[i]);
197      }
198      osfree(names);
199   }
200
201   for (i = 0; i < TREE_SIZE; i++) {
202      station *p;
203      for (p = htab[i]; p; p = p->next) c++;
204   }
205   if (c == 0) return fChanged;
206
207   names = osmalloc(c * ossizeof(char *));
208   c = 0;
209   for (i = 0; i < TREE_SIZE; i++) {
210      station *p;
211      for (p = htab[i]; p; p = p->next) names[c++] = p->name;
212   }
213   sort_separator = old_separator;
214   qsort(names, c, sizeof(char *), cmp_pname);
215   for (i = 0; i < c; i++) {
216      printf(msg(/*Deleted: %s*/502), names[i]);
217      putnl();
218   }
219   return fTrue;
220}
221
222static int
223parse_file(const char *fnm, const char *survey,
224           void (*tree_func)(const char *, const img_point *))
225{
226   img_point pt;
227   int result;
228   int separator;
229
230   img *pimg = img_open_survey(fnm, survey);
231   if (!pimg) fatalerror(img_error(), fnm);
232   separator = pimg->separator;
233
234   do {
235      result = img_read_item(pimg, &pt);
236      switch (result) {
237       case img_MOVE:
238       case img_LINE:
239         break;
240       case img_LABEL:
241         tree_func(pimg->label, &pt);
242         break;
243       case img_BAD:
244         img_close(pimg);
245         fatalerror(img_error(), fnm);
246      }
247   } while (result != img_STOP);
248
249   img_close(pimg);
250   return separator;
251}
252
253int
254main(int argc, char **argv)
255{
256   char *fnm1, *fnm2;
257   const char *survey = NULL;
258
259   msg_init(argv);
260
261   cmdline_set_syntax_message("FILE1 FILE2 [THRESHOLD]",
262                              "FILE1 and FILE2 can be .pos or .3d files\n"
263                              "THRESHOLD is the max. ignorable change along "
264                              "any axis in metres (default "
265                              STRING(DFLT_MAX_THRESHOLD)")");
266   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3);
267   while (1) {
268      int opt = cmdline_getopt();
269      if (opt == EOF) break;
270      if (opt == 's') survey = optarg;
271   }
272   fnm1 = argv[optind++];
273   fnm2 = argv[optind++];
274   if (argv[optind]) {
275      optarg = argv[optind];
276      threshold = cmdline_double_arg();
277   }
278
279   tree_init();
280
281   old_separator = parse_file(fnm1, survey, tree_insert);
282
283   new_separator = parse_file(fnm2, survey, tree_remove);
284
285   return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS;
286}
Note: See TracBrowser for help on using the repository browser.