source: git/src/diffpos.c @ fc4ee5b

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernlogstereowalls-datawalls-data-hanging-as-warning
Last change on this file since fc4ee5b was ee1ec59, checked in by Olly Betts <olly@…>, 22 years ago

Merging from 1.0 branch again.

git-svn-id: file:///home/survex-svn/survex/branches/survex-1_1@2063 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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