source: git/src/diffpos.c

faster-cavernlog
Last change on this file was b3b0900, checked in by Olly Betts <olly@…>, 3 weeks ago

Clean up header includes

  • Property mode set to 100644
File size: 7.3 KB
RevLine 
[0da6e60]1/* diffpos.c */
[3262009]2/* Utility to compare two SURVEX .pos or .3d files */
[736f7df]3/* Copyright (C) 1994,1996,1998-2003,2010,2011,2013,2014 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
[4c83f84]20#include <config.h>
[d1b1380]21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <math.h>
26
[acc20b1]27#include "cmdline.h"
[3262009]28#include "debug.h"
29#include "filelist.h"
[693388e]30#include "hash.h"
[a405bc1]31#include "img_hosted.h"
[a20d9b9]32#include "namecmp.h"
[b3b0900]33#include "osalloc.h"
[fa42426]34#include "useful.h"
[d1b1380]35
[b4fe9fb]36/* Don't complain if values mismatch by a tiny amount (1e-6m i.e. 0.001mm) */
37#define TOLERANCE 1e-6
[d1b1380]38
39/* default threshold is 1cm */
40#define DFLT_MAX_THRESHOLD 0.01
41
[3262009]42static double threshold = DFLT_MAX_THRESHOLD;
[d1b1380]43
[acc20b1]44static const struct option long_opts[] = {
45   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
[76bbb7c9]46   {"survey", required_argument, 0, 's'},
[acc20b1]47   {"help", no_argument, 0, HLP_HELP},
48   {"version", no_argument, 0, HLP_VERSION},
49   {0, 0, 0, 0}
50};
51
[76bbb7c9]52#define short_opts "s:"
[acc20b1]53
54static struct help_msg help[] = {
55/*                              <-- */
[45af761]56   {HLP_ENCODELONG(0),        /*only load the sub-survey with this prefix*/199, 0},
57   {0, 0, 0}
[acc20b1]58};
59
[b4fe9fb]60/* We use a hashtable with linked list buckets - this is how many hash table
61 * entries we have.  0x2000 with sizeof(void *) == 4 uses 32K. */
62#define TREE_SIZE 0x2000
63
[3262009]64typedef struct station {
65   struct station *next;
66   char *name;
67   img_point pt;
68} station;
69
[c0635e7]70typedef struct added {
71   struct added *next;
72   char *name;
73} added;
74
[ee1ec59]75static int old_separator, new_separator, sort_separator;
76
[a20d9b9]77static int
78cmp_pname(const void *a, const void *b)
79{
[ee1ec59]80   return name_cmp(*(const char **)a, *(const char **)b, sort_separator);
[a20d9b9]81}
82
[3262009]83static station **htab;
[63d4f07]84static bool fChanged = false;
[3262009]85
[b07ae08]86static added *added_list = NULL;
87static OSSIZE_T c_added = 0;
[c0635e7]88
[3262009]89static void
90tree_init(void)
91{
92   size_t i;
[b4fe9fb]93   htab = osmalloc(TREE_SIZE * ossizeof(station *));
94   for (i = 0; i < TREE_SIZE; i++) htab[i] = NULL;
[3262009]95}
96
97static void
98tree_insert(const char *name, const img_point *pt)
99{
[b4fe9fb]100   int v = hash_string(name) & (TREE_SIZE - 1);
[9d20d33]101   station * stn = osnew(station);
[3262009]102   stn->name = osstrdup(name);
103   stn->pt = *pt;
104   stn->next = htab[v];
105   htab[v] = stn;
106}
107
[9d20d33]108static int
109close_enough(const img_point * p1, const img_point * p2)
110{
111    return fabs(p1->x - p2->x) - threshold <= TOLERANCE &&
112           fabs(p1->y - p2->y) - threshold <= TOLERANCE &&
113           fabs(p1->z - p2->z) - threshold <= TOLERANCE;
114}
115
[3262009]116static void
117tree_remove(const char *name, const img_point *pt)
118{
[9d20d33]119   /* We need to handle duplicate labels - normal .3d files shouldn't have them
120    * (though some older ones do due to a couple of bugs in earlier versions of
121    * Survex) but extended .3d files repeat the label where a loop is broken,
122    * and data read from foreign formats might repeat labels.
123    */
[b4fe9fb]124   int v = hash_string(name) & (TREE_SIZE - 1);
[3262009]125   station **prev;
126   station *p;
[9d20d33]127   station **found = NULL;
[63d4f07]128   bool was_close_enough = false;
[421b7d2]129
[3262009]130   for (prev = &htab[v]; *prev; prev = &((*prev)->next)) {
[9d20d33]131      if (strcmp((*prev)->name, name) == 0) {
132         /* Handle stations with the same name.  Stations are inserted at the
133          * start of the linked list, so pick the *last* matching station in
134          * the list as then we match the first stations with the same name in
135          * each file.
136          */
137         if (close_enough(pt, &((*prev)->pt))) {
138            found = prev;
[63d4f07]139            was_close_enough = true;
[9d20d33]140         } else if (!was_close_enough) {
141            found = prev;
142         }
143      }
[3262009]144   }
[421b7d2]145
[9d20d33]146   if (!found) {
[c0635e7]147      added *add = osnew(added);
148      add->name = osstrdup(name);
149      add->next = added_list;
150      added_list = add;
151      c_added++;
[63d4f07]152      fChanged = true;
[3262009]153      return;
154   }
[421b7d2]155
[9d20d33]156   if (!was_close_enough) {
[736f7df]157      /* TRANSLATORS: for diffpos: */
[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();
[63d4f07]164      fChanged = true;
[3262009]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++) {
[736f7df]194         /* TRANSLATORS: for diffpos: */
[96e1f97]195         printf(msg(/*Added: %s*/501), names[i]);
196         putnl();
[c0635e7]197         osfree(names[i]);
198      }
199      osfree(names);
200   }
201
[b4fe9fb]202   for (i = 0; i < TREE_SIZE; i++) {
[3262009]203      station *p;
[a20d9b9]204      for (p = htab[i]; p; p = p->next) c++;
205   }
206   if (c == 0) return fChanged;
207
208   names = osmalloc(c * ossizeof(char *));
209   c = 0;
[b4fe9fb]210   for (i = 0; i < TREE_SIZE; i++) {
[a20d9b9]211      station *p;
212      for (p = htab[i]; p; p = p->next) names[c++] = p->name;
213   }
[ee1ec59]214   sort_separator = old_separator;
[a20d9b9]215   qsort(names, c, sizeof(char *), cmp_pname);
216   for (i = 0; i < c; i++) {
[736f7df]217      /* TRANSLATORS: for diffpos: */
[96e1f97]218      printf(msg(/*Deleted: %s*/502), names[i]);
219      putnl();
[3262009]220   }
[63d4f07]221   return true;
[3262009]222}
223
[ee1ec59]224static int
[0da6e60]225parse_file(const char *fnm, const char *survey,
226           void (*tree_func)(const char *, const img_point *))
[840b213]227{
228   img_point pt;
229   int result;
[ee1ec59]230   int separator;
[421b7d2]231
[a2ad284]232   img *pimg = img_open_survey(fnm, survey);
[a405bc1]233   if (!pimg) fatalerror(img_error2msg(img_error()), fnm);
[ee1ec59]234   separator = pimg->separator;
[840b213]235
236   do {
237      result = img_read_item(pimg, &pt);
238      switch (result) {
239       case img_MOVE:
240       case img_LINE:
241         break;
242       case img_LABEL:
243         tree_func(pimg->label, &pt);
244         break;
245       case img_BAD:
246         img_close(pimg);
[a405bc1]247         fatalerror(img_error2msg(img_error()), fnm);
[840b213]248      }
249   } while (result != img_STOP);
[421b7d2]250
[840b213]251   img_close(pimg);
[ee1ec59]252   return separator;
[840b213]253}
254
[a420b49]255int
[acc20b1]256main(int argc, char **argv)
[a420b49]257{
[73a8a94]258   char *fnm1, *fnm2;
[421b7d2]259   const char *survey = NULL;
[76bbb7c9]260
[bdfe97f]261   msg_init(argv);
[d06141c]262
[736f7df]263   /* TRANSLATORS: Part of diffpos --help */
[d8dbdff]264   cmdline_set_syntax_message(/*FILE1 FILE2 [THRESHOLD]*/218,
[736f7df]265                              /* TRANSLATORS: Part of diffpos --help */
[d8dbdff]266                              /*FILE1 and FILE2 can be .pos or .3d files\nTHRESHOLD is the max. ignorable change along any axis in metres (default %s)*/255,
267                              STRING(DFLT_MAX_THRESHOLD));
[acc20b1]268   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3);
[76bbb7c9]269   while (1) {
270      int opt = cmdline_getopt();
271      if (opt == EOF) break;
272      if (opt == 's') survey = optarg;
[acc20b1]273   }
274   fnm1 = argv[optind++];
275   fnm2 = argv[optind++];
276   if (argv[optind]) {
277      optarg = argv[optind];
278      threshold = cmdline_double_arg();
[73a8a94]279   }
[bfe1242]280
[3262009]281   tree_init();
[bfe1242]282
[ee1ec59]283   old_separator = parse_file(fnm1, survey, tree_insert);
[3262009]284
[ee1ec59]285   new_separator = parse_file(fnm2, survey, tree_remove);
[421b7d2]286
[3262009]287   return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS;
[d1b1380]288}
Note: See TracBrowser for help on using the repository browser.