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

RELEASE/1.2debug-cidebug-ci-sanitiserswalls-data
Last change on this file since 9fcc81a was 736f7df, checked in by Olly Betts <olly@…>, 10 years ago

lib/extract-msgs.pl,lib/survex.pot,src/: Insert "TRANSLATORS"
comments into source code.

  • Property mode set to 100644
File size: 7.3 KB
Line 
1/* diffpos.c */
2/* Utility to compare two SURVEX .pos or .3d files */
3/* Copyright (C) 1994,1996,1998-2003,2010,2011,2013,2014 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_hosted.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*/199, 0},
58   {0, 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      /* TRANSLATORS: for diffpos: */
159      printf(msg(/*Moved by (%3.2f,%3.2f,%3.2f): %s*/500),
160             pt->x - (*found)->pt.x,
161             pt->y - (*found)->pt.y,
162             pt->z - (*found)->pt.z,
163             name);
164      putnl();
165      fChanged = fTrue;
166   }
167
168   osfree((*found)->name);
169   p = *found;
170   *found = p->next;
171   osfree(p);
172}
173
174static int
175tree_check(void)
176{
177   size_t c = 0;
178   char **names;
179   size_t i;
180
181   if (c_added) {
182      names = osmalloc(c_added * ossizeof(char *));
183      for (i = 0; i < c_added; i++) {
184         added *old;
185         SVX_ASSERT(added_list);
186         names[i] = added_list->name;
187         old = added_list;
188         added_list = old->next;
189         osfree(old);
190      }
191      SVX_ASSERT(added_list == NULL);
192      sort_separator = new_separator;
193      qsort(names, c_added, sizeof(char *), cmp_pname);
194      for (i = 0; i < c_added; i++) {
195         /* TRANSLATORS: for diffpos: */
196         printf(msg(/*Added: %s*/501), names[i]);
197         putnl();
198         osfree(names[i]);
199      }
200      osfree(names);
201   }
202
203   for (i = 0; i < TREE_SIZE; i++) {
204      station *p;
205      for (p = htab[i]; p; p = p->next) c++;
206   }
207   if (c == 0) return fChanged;
208
209   names = osmalloc(c * ossizeof(char *));
210   c = 0;
211   for (i = 0; i < TREE_SIZE; i++) {
212      station *p;
213      for (p = htab[i]; p; p = p->next) names[c++] = p->name;
214   }
215   sort_separator = old_separator;
216   qsort(names, c, sizeof(char *), cmp_pname);
217   for (i = 0; i < c; i++) {
218      /* TRANSLATORS: for diffpos: */
219      printf(msg(/*Deleted: %s*/502), names[i]);
220      putnl();
221   }
222   return fTrue;
223}
224
225static int
226parse_file(const char *fnm, const char *survey,
227           void (*tree_func)(const char *, const img_point *))
228{
229   img_point pt;
230   int result;
231   int separator;
232
233   img *pimg = img_open_survey(fnm, survey);
234   if (!pimg) fatalerror(img_error2msg(img_error()), fnm);
235   separator = pimg->separator;
236
237   do {
238      result = img_read_item(pimg, &pt);
239      switch (result) {
240       case img_MOVE:
241       case img_LINE:
242         break;
243       case img_LABEL:
244         tree_func(pimg->label, &pt);
245         break;
246       case img_BAD:
247         img_close(pimg);
248         fatalerror(img_error2msg(img_error()), fnm);
249      }
250   } while (result != img_STOP);
251
252   img_close(pimg);
253   return separator;
254}
255
256int
257main(int argc, char **argv)
258{
259   char *fnm1, *fnm2;
260   const char *survey = NULL;
261
262   msg_init(argv);
263
264   /* TRANSLATORS: Part of diffpos --help */
265   cmdline_set_syntax_message(/*FILE1 FILE2 [THRESHOLD]*/218,
266                              /* TRANSLATORS: Part of diffpos --help */
267                              /*FILE1 and FILE2 can be .pos or .3d files\nTHRESHOLD is the max. ignorable change along any axis in metres (default %s)*/255,
268                              STRING(DFLT_MAX_THRESHOLD));
269   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3);
270   while (1) {
271      int opt = cmdline_getopt();
272      if (opt == EOF) break;
273      if (opt == 's') survey = optarg;
274   }
275   fnm1 = argv[optind++];
276   fnm2 = argv[optind++];
277   if (argv[optind]) {
278      optarg = argv[optind];
279      threshold = cmdline_double_arg();
280   }
281
282   tree_init();
283
284   old_separator = parse_file(fnm1, survey, tree_insert);
285
286   new_separator = parse_file(fnm2, survey, tree_remove);
287
288   return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS;
289}
Note: See TracBrowser for help on using the repository browser.