source: git/src/diffpos.c @ c1eff09

RELEASE/1.0RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereowalls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since c1eff09 was 840b213, checked in by Olly Betts <olly@…>, 23 years ago

Cleaned up diffpos.c by factoring out code to read .3d and .pos files.

git-svn-id: file:///home/survex-svn/survex/trunk@1114 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 7.6 KB
RevLine 
[d1b1380]1/* > diffpos.c */
[3262009]2/* Utility to compare two SURVEX .pos or .3d files */
[759fb47]3/* Copyright (C) 1994,1996,1998,1999,2001 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"
33#include "img.h"
[a20d9b9]34#include "namecmp.h"
[acc20b1]35
[8b0fcbc]36#ifndef sqrd
37# define sqrd(X) ((X)*(X))
38#endif
[d1b1380]39
40/* macro to just convert argument to a string */
41#define STRING(X) _STRING(X)
42#define _STRING(X) #X
43
44/* very small value for comparing floating point numbers with */
[2b078c4]45#define EPSILON (REAL_EPSILON * 1000)
[d1b1380]46
47/* default threshold is 1cm */
48#define DFLT_MAX_THRESHOLD 0.01
49
[3262009]50static double threshold = DFLT_MAX_THRESHOLD;
[d1b1380]51
[acc20b1]52static const struct option long_opts[] = {
53   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
54   {"help", no_argument, 0, HLP_HELP},
55   {"version", no_argument, 0, HLP_VERSION},
56   {0, 0, 0, 0}
57};
58
59#define short_opts ""
60
61static struct help_msg help[] = {
62/*                              <-- */
63   {0, 0}
64};
65
[3262009]66/* some (preferably prime) number for the hashing function */
67#define HASH_PRIME 29363
68
69typedef struct station {
70   struct station *next;
71   char *name;
72   img_point pt;
73} station;
74
[c0635e7]75typedef struct added {
76   struct added *next;
77   char *name;
78} added;
79
[3262009]80static int
81hash_string(const char *p)
82{
83   int hash;
84   ASSERT(p != NULL); /* can't hash NULL */
85   for (hash = 0; *p; p++)
86      hash = (hash * HASH_PRIME + tolower(*(unsigned char*)p)) & 0x7fff;
87   return hash;
88}
89
[a20d9b9]90static int
91cmp_pname(const void *a, const void *b)
92{
93   return name_cmp(*(const char **)a, *(const char **)b);
94}
95
[3262009]96static station **htab;
97static bool fChanged = fFalse;
98
[c0635e7]99static added *added_list = NULL;;
100OSSIZE_T c_added = 0;
101
[3262009]102static void
103tree_init(void)
104{
105   size_t i;
106   htab = osmalloc(0x2000 * sizeof(int));
107   for (i = 0; i < 0x2000; i++) htab[i] = NULL;
108}
109
110static void
111tree_insert(const char *name, const img_point *pt)
112{
113   int v = hash_string(name) & 0x1fff;
114   station *stn;
[840b213]115
116   /* Catch duplicate labels - .3d files shouldn't have them, but some do
117    * due to a couple of bugs in some versions of Survex
118    */
[3262009]119   for (stn = htab[v]; stn; stn = stn->next) {
120      if (strcmp(stn->name, name) == 0) return; /* found dup */
121   }
[840b213]122
[3262009]123   stn = osnew(station);
124   stn->name = osstrdup(name);
125   stn->pt = *pt;
126   stn->next = htab[v];
127   htab[v] = stn;
128}
129
130static void
131tree_remove(const char *name, const img_point *pt)
132{
133   int v = hash_string(name) & 0x1fff;
134   station **prev;
135   station *p;
136   
137   for (prev = &htab[v]; *prev; prev = &((*prev)->next)) {
138      if (strcmp((*prev)->name, name) == 0) break;
139   }
140   
141   if (!*prev) {
[c0635e7]142      added *add = osnew(added);
143      add->name = osstrdup(name);
144      add->next = added_list;
145      added_list = add;
146      c_added++;
[3262009]147      fChanged = fTrue;
148      return;
149   }
150   
151   if (fabs(pt->x - (*prev)->pt.x) - threshold > EPSILON ||
152       fabs(pt->y - (*prev)->pt.y) - threshold > EPSILON ||
153       fabs(pt->z - (*prev)->pt.z) - threshold > EPSILON) {
154      printf("Moved by (%3.2f,%3.2f,%3.2f): %s\n",
155             pt->x - (*prev)->pt.x,
156             pt->y - (*prev)->pt.y,
157             pt->z - (*prev)->pt.z,
158             name);
159      fChanged = fTrue;
160   }
161   
162   osfree((*prev)->name);
163   p = *prev;
164   *prev = p->next;
165   osfree(p);
166}
167
168static int
169tree_check(void)
170{
[a20d9b9]171   size_t c = 0;
172   char **names;
[3262009]173   size_t i;
[c0635e7]174
175   if (c_added) {
176      names = osmalloc(c_added * ossizeof(char *));
177      for (i = 0; i < c_added; i++) {
178         added *old;
179         ASSERT(added_list);
180         names[i] = added_list->name;
181         old = added_list;
182         added_list = old->next;
183         osfree(old);
184      }
185      ASSERT(added_list == NULL);
186      qsort(names, c_added, sizeof(char *), cmp_pname);
187      for (i = 0; i < c_added; i++) {
188         printf("Added: %s\n", names[i]);
189         osfree(names[i]);
190      }
191      osfree(names);
192   }
193
[3262009]194   for (i = 0; i < 0x2000; i++) {
195      station *p;
[a20d9b9]196      for (p = htab[i]; p; p = p->next) c++;
197   }
198   if (c == 0) return fChanged;
199
200   names = osmalloc(c * ossizeof(char *));
201   c = 0;
202   for (i = 0; i < 0x2000; i++) {
203      station *p;
204      for (p = htab[i]; p; p = p->next) names[c++] = p->name;
205   }
206   qsort(names, c, sizeof(char *), cmp_pname);
207   for (i = 0; i < c; i++) {
[c0635e7]208      printf("Deleted: %s\n", names[i]);
[3262009]209   }
[a20d9b9]210   return fTrue;
[3262009]211}
212
[840b213]213static void
214parse_3d_file(const char *fnm,
215              void (*tree_func)(const char *, const img_point *))
216{
217   img_point pt;
218   int result;
219 
220   img *pimg = img_open(fnm, NULL, NULL);
221   if (!pimg) fatalerror(img_error(), fnm);
222
223   do {
224      result = img_read_item(pimg, &pt);
225      switch (result) {
226       case img_MOVE:
227       case img_LINE:
228         break;
229       case img_LABEL:
230         tree_func(pimg->label, &pt);
231         break;
232       case img_BAD:
233         img_close(pimg);
234         fatalerror(/*Bad 3d image file `%s'*/106, fnm);
235      }
236   } while (result != img_STOP);
237     
238   img_close(pimg);
239}
240
241static void
242parse_pos_file(const char *fnm,
243               void (*tree_func)(const char *, const img_point *))
244{
245   FILE *fh;
246   img_point pt;
247   char *buf;
248   size_t buf_len = 256;
249
250   buf = osmalloc(buf_len);
251   
252   fh = fopen(fnm, "rb");
253   if (!fh) fatalerror(/*Couldn't open file `%s'*/93, fnm);
254
255   while (1) {
256      size_t off = 0;
257      long fp = ftell(fh);
258      if (fscanf(fh, "(%lf,%lf,%lf ) ", &pt.x, &pt.y, &pt.z) != 3) {
259         int ch;
260         fseek(fh, SEEK_SET, fp);
261         ch = getc(fh);
262         if (ch == EOF) break;
263
264         printf("%s: Ignoring: ", fnm);
265         while (ch != '\n' && ch != '\r' && ch != EOF) {
266            putchar(ch);
267            ch = getc(fh);
268         }
269         putchar('\n');
270         
271         if (feof(fh)) break;
272         continue;
273      }
274      buf[0] = '\0';
275      while (!feof(fh)) {
276         if (!fgets(buf + off, buf_len - off, fh)) {
277            /* FIXME */
278            break;
279         }
280         off += strlen(buf + off);
281         if (off && buf[off - 1] == '\n') {
282            buf[off - 1] = '\0';
283            break;
284         }
285         buf_len += buf_len;
286         buf = osrealloc(buf, buf_len);
287      }
288      tree_func(buf, &pt);
289   }
290   fclose(fh);
291
292   osfree(buf);
293}
294
295static void
296parse_file(const char *fnm,
297           void (*tree_func)(const char *, const img_point *))
298{
299   static const char ext3d[] = EXT_SVX_3D;
300   size_t len = strlen(fnm);
301   if (len > sizeof(ext3d) && fnm[len - sizeof(ext3d)] == FNM_SEP_EXT &&
302       strcmp(fnm + len - sizeof(ext3d) + 1, ext3d) == 0) {
303      parse_3d_file(fnm, tree_func);
304   } else {
305      parse_pos_file(fnm, tree_func);
306   }
307}
308
[a420b49]309int
[acc20b1]310main(int argc, char **argv)
[a420b49]311{
[73a8a94]312   char *fnm1, *fnm2;
[d06141c]313   msg_init(argv[0]);
314
[3262009]315   cmdline_set_syntax_message("FILE1 FILE2 [THRESHOLD]",
316                              "FILE1 and FILE2 can be .pos or .3d files\n"
[acc20b1]317                              "THRESHOLD is the max. ignorable change along "
318                              "any axis in metres (default "
319                              STRING(DFLT_MAX_THRESHOLD)")");
320   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3);
321   while (cmdline_getopt() != EOF) {
322      /* do nothing */
323   }
324   fnm1 = argv[optind++];
325   fnm2 = argv[optind++];
326   if (argv[optind]) {
327      optarg = argv[optind];
328      threshold = cmdline_double_arg();
[73a8a94]329   }
[bfe1242]330
[3262009]331   tree_init();
[bfe1242]332
[840b213]333   parse_file(fnm1, tree_insert);
[3262009]334
[840b213]335   parse_file(fnm2, tree_remove);
336   
[3262009]337   return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS;
[d1b1380]338}
Note: See TracBrowser for help on using the repository browser.