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@…>, 14 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
RevLine 
[0da6e60]1/* diffpos.c */
[3262009]2/* Utility to compare two SURVEX .pos or .3d files */
[9d20d33]3/* Copyright (C) 1994,1996,1998-2003,2010 Olly Betts
[846746e]4 *
[89231c4]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.
[846746e]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
[89231c4]12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
[846746e]14 *
[89231c4]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
[ecbc6c18]17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
[846746e]18 */
[d1b1380]19
[a420b49]20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
[d1b1380]23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <math.h>
28
[acc20b1]29#include "cmdline.h"
[3262009]30#include "debug.h"
31#include "filelist.h"
[693388e]32#include "hash.h"
[3262009]33#include "img.h"
[a20d9b9]34#include "namecmp.h"
[fa42426]35#include "useful.h"
[d1b1380]36
[b4fe9fb]37/* Don't complain if values mismatch by a tiny amount (1e-6m i.e. 0.001mm) */
38#define TOLERANCE 1e-6
[d1b1380]39
40/* default threshold is 1cm */
41#define DFLT_MAX_THRESHOLD 0.01
42
[3262009]43static double threshold = DFLT_MAX_THRESHOLD;
[d1b1380]44
[acc20b1]45static const struct option long_opts[] = {
46   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
[76bbb7c9]47   {"survey", required_argument, 0, 's'},
[acc20b1]48   {"help", no_argument, 0, HLP_HELP},
49   {"version", no_argument, 0, HLP_VERSION},
50   {0, 0, 0, 0}
51};
52
[76bbb7c9]53#define short_opts "s:"
[acc20b1]54
55static struct help_msg help[] = {
56/*                              <-- */
[e4138b5]57   {HLP_ENCODELONG(0),          "only load the sub-survey with this prefix"},
[acc20b1]58   {0, 0}
59};
60
[b4fe9fb]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
[3262009]65typedef struct station {
66   struct station *next;
67   char *name;
68   img_point pt;
69} station;
70
[c0635e7]71typedef struct added {
72   struct added *next;
73   char *name;
74} added;
75
[ee1ec59]76static int old_separator, new_separator, sort_separator;
77
[a20d9b9]78static int
79cmp_pname(const void *a, const void *b)
80{
[ee1ec59]81   return name_cmp(*(const char **)a, *(const char **)b, sort_separator);
[a20d9b9]82}
83
[3262009]84static station **htab;
85static bool fChanged = fFalse;
86
[b07ae08]87static added *added_list = NULL;
88static OSSIZE_T c_added = 0;
[c0635e7]89
[3262009]90static void
91tree_init(void)
92{
93   size_t i;
[b4fe9fb]94   htab = osmalloc(TREE_SIZE * ossizeof(station *));
95   for (i = 0; i < TREE_SIZE; i++) htab[i] = NULL;
[3262009]96}
97
98static void
99tree_insert(const char *name, const img_point *pt)
100{
[b4fe9fb]101   int v = hash_string(name) & (TREE_SIZE - 1);
[9d20d33]102   station * stn = osnew(station);
[3262009]103   stn->name = osstrdup(name);
104   stn->pt = *pt;
105   stn->next = htab[v];
106   htab[v] = stn;
107}
108
[9d20d33]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
[3262009]117static void
118tree_remove(const char *name, const img_point *pt)
119{
[9d20d33]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    */
[b4fe9fb]125   int v = hash_string(name) & (TREE_SIZE - 1);
[3262009]126   station **prev;
127   station *p;
[9d20d33]128   station **found = NULL;
129   bool was_close_enough = fFalse;
[421b7d2]130
[3262009]131   for (prev = &htab[v]; *prev; prev = &((*prev)->next)) {
[9d20d33]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      }
[3262009]145   }
[421b7d2]146
[9d20d33]147   if (!found) {
[c0635e7]148      added *add = osnew(added);
149      add->name = osstrdup(name);
150      add->next = added_list;
151      added_list = add;
152      c_added++;
[3262009]153      fChanged = fTrue;
154      return;
155   }
[421b7d2]156
[9d20d33]157   if (!was_close_enough) {
[96e1f97]158      printf(msg(/*Moved by (%3.2f,%3.2f,%3.2f): %s*/500),
[9d20d33]159             pt->x - (*found)->pt.x,
160             pt->y - (*found)->pt.y,
161             pt->z - (*found)->pt.z,
[3262009]162             name);
[96e1f97]163      putnl();
[3262009]164      fChanged = fTrue;
165   }
[421b7d2]166
[9d20d33]167   osfree((*found)->name);
168   p = *found;
169   *found = p->next;
[3262009]170   osfree(p);
171}
172
173static int
174tree_check(void)
175{
[a20d9b9]176   size_t c = 0;
177   char **names;
[3262009]178   size_t i;
[c0635e7]179
180   if (c_added) {
181      names = osmalloc(c_added * ossizeof(char *));
182      for (i = 0; i < c_added; i++) {
183         added *old;
[4c07c51]184         SVX_ASSERT(added_list);
[c0635e7]185         names[i] = added_list->name;
186         old = added_list;
187         added_list = old->next;
188         osfree(old);
189      }
[4c07c51]190      SVX_ASSERT(added_list == NULL);
[ee1ec59]191      sort_separator = new_separator;
[c0635e7]192      qsort(names, c_added, sizeof(char *), cmp_pname);
193      for (i = 0; i < c_added; i++) {
[96e1f97]194         printf(msg(/*Added: %s*/501), names[i]);
195         putnl();
[c0635e7]196         osfree(names[i]);
197      }
198      osfree(names);
199   }
200
[b4fe9fb]201   for (i = 0; i < TREE_SIZE; i++) {
[3262009]202      station *p;
[a20d9b9]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;
[b4fe9fb]209   for (i = 0; i < TREE_SIZE; i++) {
[a20d9b9]210      station *p;
211      for (p = htab[i]; p; p = p->next) names[c++] = p->name;
212   }
[ee1ec59]213   sort_separator = old_separator;
[a20d9b9]214   qsort(names, c, sizeof(char *), cmp_pname);
215   for (i = 0; i < c; i++) {
[96e1f97]216      printf(msg(/*Deleted: %s*/502), names[i]);
217      putnl();
[3262009]218   }
[a20d9b9]219   return fTrue;
[3262009]220}
221
[ee1ec59]222static int
[0da6e60]223parse_file(const char *fnm, const char *survey,
224           void (*tree_func)(const char *, const img_point *))
[840b213]225{
226   img_point pt;
227   int result;
[ee1ec59]228   int separator;
[421b7d2]229
[a2ad284]230   img *pimg = img_open_survey(fnm, survey);
[840b213]231   if (!pimg) fatalerror(img_error(), fnm);
[ee1ec59]232   separator = pimg->separator;
[840b213]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);
[b8720f8]245         fatalerror(img_error(), fnm);
[840b213]246      }
247   } while (result != img_STOP);
[421b7d2]248
[840b213]249   img_close(pimg);
[ee1ec59]250   return separator;
[840b213]251}
252
[a420b49]253int
[acc20b1]254main(int argc, char **argv)
[a420b49]255{
[73a8a94]256   char *fnm1, *fnm2;
[421b7d2]257   const char *survey = NULL;
[76bbb7c9]258
[bdfe97f]259   msg_init(argv);
[d06141c]260
[3262009]261   cmdline_set_syntax_message("FILE1 FILE2 [THRESHOLD]",
[421b7d2]262                              "FILE1 and FILE2 can be .pos or .3d files\n"
[acc20b1]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);
[76bbb7c9]267   while (1) {
268      int opt = cmdline_getopt();
269      if (opt == EOF) break;
270      if (opt == 's') survey = optarg;
[acc20b1]271   }
272   fnm1 = argv[optind++];
273   fnm2 = argv[optind++];
274   if (argv[optind]) {
275      optarg = argv[optind];
276      threshold = cmdline_double_arg();
[73a8a94]277   }
[bfe1242]278
[3262009]279   tree_init();
[bfe1242]280
[ee1ec59]281   old_separator = parse_file(fnm1, survey, tree_insert);
[3262009]282
[ee1ec59]283   new_separator = parse_file(fnm2, survey, tree_remove);
[421b7d2]284
[3262009]285   return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS;
[d1b1380]286}
Note: See TracBrowser for help on using the repository browser.