source: git/src/diffpos.c @ 1b71c05

RELEASE/1.0
Last change on this file since 1b71c05 was 1b71c05, checked in by Olly Betts <olly@…>, 14 years ago

src/: Update FSF address in (C) notices in source files.

git-svn-id: file:///home/survex-svn/survex/branches/1.0@3463 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[0da6e60]1/* diffpos.c */
[3262009]2/* Utility to compare two SURVEX .pos or .3d files */
[2dfd768]3/* Copyright (C) 1994,1996,1998-2003 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
[1b71c05]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
[2dfd768]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
[2dfd768]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
[9f9c05e]76static int old_separator, new_separator, sort_separator;
77
[a20d9b9]78static int
79cmp_pname(const void *a, const void *b)
80{
[9f9c05e]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;
[2dfd768]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{
[2dfd768]101   int v = hash_string(name) & (TREE_SIZE - 1);
[3262009]102   station *stn;
[840b213]103
104   /* Catch duplicate labels - .3d files shouldn't have them, but some do
105    * due to a couple of bugs in some versions of Survex
106    */
[3262009]107   for (stn = htab[v]; stn; stn = stn->next) {
108      if (strcmp(stn->name, name) == 0) return; /* found dup */
109   }
[840b213]110
[3262009]111   stn = osnew(station);
112   stn->name = osstrdup(name);
113   stn->pt = *pt;
114   stn->next = htab[v];
115   htab[v] = stn;
116}
117
118static void
119tree_remove(const char *name, const img_point *pt)
120{
[2dfd768]121   int v = hash_string(name) & (TREE_SIZE - 1);
[3262009]122   station **prev;
123   station *p;
[421b7d2]124
[3262009]125   for (prev = &htab[v]; *prev; prev = &((*prev)->next)) {
126      if (strcmp((*prev)->name, name) == 0) break;
127   }
[421b7d2]128
[3262009]129   if (!*prev) {
[c0635e7]130      added *add = osnew(added);
131      add->name = osstrdup(name);
132      add->next = added_list;
133      added_list = add;
134      c_added++;
[3262009]135      fChanged = fTrue;
136      return;
137   }
[421b7d2]138
[2dfd768]139   if (fabs(pt->x - (*prev)->pt.x) - threshold > TOLERANCE ||
140       fabs(pt->y - (*prev)->pt.y) - threshold > TOLERANCE ||
141       fabs(pt->z - (*prev)->pt.z) - threshold > TOLERANCE) {
[96e1f97]142      printf(msg(/*Moved by (%3.2f,%3.2f,%3.2f): %s*/500),
[3262009]143             pt->x - (*prev)->pt.x,
144             pt->y - (*prev)->pt.y,
145             pt->z - (*prev)->pt.z,
146             name);
[96e1f97]147      putnl();
[3262009]148      fChanged = fTrue;
149   }
[421b7d2]150
[3262009]151   osfree((*prev)->name);
152   p = *prev;
153   *prev = p->next;
154   osfree(p);
155}
156
157static int
158tree_check(void)
159{
[a20d9b9]160   size_t c = 0;
161   char **names;
[3262009]162   size_t i;
[c0635e7]163
164   if (c_added) {
165      names = osmalloc(c_added * ossizeof(char *));
166      for (i = 0; i < c_added; i++) {
167         added *old;
[4c07c51]168         SVX_ASSERT(added_list);
[c0635e7]169         names[i] = added_list->name;
170         old = added_list;
171         added_list = old->next;
172         osfree(old);
173      }
[4c07c51]174      SVX_ASSERT(added_list == NULL);
[9f9c05e]175      sort_separator = new_separator;
[c0635e7]176      qsort(names, c_added, sizeof(char *), cmp_pname);
177      for (i = 0; i < c_added; i++) {
[96e1f97]178         printf(msg(/*Added: %s*/501), names[i]);
179         putnl();
[c0635e7]180         osfree(names[i]);
181      }
182      osfree(names);
183   }
184
[2dfd768]185   for (i = 0; i < TREE_SIZE; i++) {
[3262009]186      station *p;
[a20d9b9]187      for (p = htab[i]; p; p = p->next) c++;
188   }
189   if (c == 0) return fChanged;
190
191   names = osmalloc(c * ossizeof(char *));
192   c = 0;
[2dfd768]193   for (i = 0; i < TREE_SIZE; i++) {
[a20d9b9]194      station *p;
195      for (p = htab[i]; p; p = p->next) names[c++] = p->name;
196   }
[9f9c05e]197   sort_separator = old_separator;
[a20d9b9]198   qsort(names, c, sizeof(char *), cmp_pname);
199   for (i = 0; i < c; i++) {
[96e1f97]200      printf(msg(/*Deleted: %s*/502), names[i]);
201      putnl();
[3262009]202   }
[a20d9b9]203   return fTrue;
[3262009]204}
205
[9f9c05e]206static int
[0da6e60]207parse_file(const char *fnm, const char *survey,
208           void (*tree_func)(const char *, const img_point *))
[840b213]209{
210   img_point pt;
211   int result;
[9f9c05e]212   int separator;
[421b7d2]213
[a2ad284]214   img *pimg = img_open_survey(fnm, survey);
[840b213]215   if (!pimg) fatalerror(img_error(), fnm);
[9f9c05e]216   separator = pimg->separator;
[840b213]217
218   do {
219      result = img_read_item(pimg, &pt);
220      switch (result) {
221       case img_MOVE:
222       case img_LINE:
223         break;
224       case img_LABEL:
225         tree_func(pimg->label, &pt);
226         break;
227       case img_BAD:
228         img_close(pimg);
[b8720f8]229         fatalerror(img_error(), fnm);
[840b213]230      }
231   } while (result != img_STOP);
[421b7d2]232
[840b213]233   img_close(pimg);
[9f9c05e]234   return separator;
[840b213]235}
236
[a420b49]237int
[acc20b1]238main(int argc, char **argv)
[a420b49]239{
[73a8a94]240   char *fnm1, *fnm2;
[421b7d2]241   const char *survey = NULL;
[76bbb7c9]242
[bdfe97f]243   msg_init(argv);
[d06141c]244
[3262009]245   cmdline_set_syntax_message("FILE1 FILE2 [THRESHOLD]",
[421b7d2]246                              "FILE1 and FILE2 can be .pos or .3d files\n"
[acc20b1]247                              "THRESHOLD is the max. ignorable change along "
248                              "any axis in metres (default "
249                              STRING(DFLT_MAX_THRESHOLD)")");
250   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3);
[76bbb7c9]251   while (1) {
252      int opt = cmdline_getopt();
253      if (opt == EOF) break;
254      if (opt == 's') survey = optarg;
[acc20b1]255   }
256   fnm1 = argv[optind++];
257   fnm2 = argv[optind++];
258   if (argv[optind]) {
259      optarg = argv[optind];
260      threshold = cmdline_double_arg();
[73a8a94]261   }
[bfe1242]262
[3262009]263   tree_init();
[bfe1242]264
[9f9c05e]265   old_separator = parse_file(fnm1, survey, tree_insert);
[3262009]266
[9f9c05e]267   new_separator = parse_file(fnm2, survey, tree_remove);
[421b7d2]268
[3262009]269   return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS;
[d1b1380]270}
Note: See TracBrowser for help on using the repository browser.