source: git/src/extend.c @ ad83822

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

Refactor a little.

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

  • Property mode set to 100644
File size: 18.1 KB
Line 
1/* extend.c
2 * Produce an extended elevation
3 * Copyright (C) 1995-2002 Olly Betts
4 * Copyright (C) 2004,2005 John Pybus
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
24
25#include <float.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include "cmdline.h"
31#include "debug.h"
32#include "filelist.h"
33#include "filename.h"
34#include "hash.h"
35#include "img.h"
36#include "message.h"
37#include "useful.h"
38
39/* To save memory we should probably use the prefix hash for the prefix on
40 * point labels (FIXME) */
41
42typedef struct stn {
43   const char *label;
44   int flags;
45   const struct stn *next;
46} stn;
47
48typedef struct POINT {
49   img_point p;
50   const stn *stns;
51   unsigned int order;
52   char dir;
53   char fDone;
54   char fBroken;
55   struct POINT *next;
56} point;
57
58typedef struct LEG {
59   point *fr, *to;
60   const char *prefix;
61   char dir;
62   char fDone;
63   char broken;
64   int flags;
65   struct LEG *next;
66} leg;
67
68/* Values for leg.broken: */
69#define BREAK_FR    0x01
70#define BREAK_TO    0x02
71
72/* Values for point.dir and leg.dir: */
73#define ELEFT  0x01
74#define ERIGHT 0x02
75#define ESWAP  0x04
76
77static point headpoint = {{0, 0, 0}, NULL, 0, 0, 0, 0, NULL};
78
79static leg headleg = {NULL, NULL, NULL, 0, 0, 0, 0, NULL};
80
81static img *pimg;
82
83static void do_stn(point *, double, const char *, int, int);
84
85typedef struct pfx {
86   const char *label;
87   struct pfx *next;
88} pfx;
89
90static pfx **htab;
91
92#define HTAB_SIZE 0x2000
93
94static const char *
95find_prefix(const char *prefix)
96{
97   pfx *p;
98   int hash;
99
100   SVX_ASSERT(prefix);
101
102   hash = hash_string(prefix) & (HTAB_SIZE - 1);
103   for (p = htab[hash]; p; p = p->next) {
104      if (strcmp(prefix, p->label) == 0) return p->label;
105   }
106
107   p = osnew(pfx);
108   p->label = osstrdup(prefix);
109   p->next = htab[hash];
110   htab[hash] = p;
111
112   return p->label;
113}
114
115static point *
116find_point(const img_point *pt)
117{
118   point *p;
119   for (p = headpoint.next; p != NULL; p = p->next) {
120      if (pt->x == p->p.x && pt->y == p->p.y && pt->z == p->p.z) {
121         return p;
122      }
123   }
124
125   p = osmalloc(ossizeof(point));
126   p->p = *pt;
127   p->stns = NULL;
128   p->order = 0;
129   p->dir = 0;
130   p->fDone = 0;
131   p->next = headpoint.next;
132   headpoint.next = p;
133   return p;
134}
135
136static void
137add_leg(point *fr, point *to, const char *prefix, int flags)
138{
139   leg *l;
140   fr->order++;
141   to->order++;
142   l = osmalloc(ossizeof(leg));
143   l->fr = fr;
144   l->to = to;
145   if (prefix)
146      l->prefix = find_prefix(prefix);
147   else
148      l->prefix = NULL;
149   l->next = headleg.next;
150   l->dir = 0;
151   l->fDone = 0;
152   l->flags = flags;
153   headleg.next = l;
154}
155
156static void
157add_label(point *p, const char *label, int flags)
158{
159   stn *s = osnew(stn);
160   s->label = osstrdup(label);
161   s->flags = flags;
162   s->next = p->stns;
163   p->stns = s;
164}
165
166/* Read in config file */
167
168
169/* lifted from img.c Should be put somewhere common? JPNP*/
170static char *
171getline_alloc(FILE *fh, size_t ilen)
172{
173   int ch;
174   size_t i = 0;
175   size_t len = ilen;
176   char *buf = xosmalloc(len);
177   if (!buf) return NULL;
178
179   ch = getc(fh);
180   while (ch != '\n' && ch != '\r' && ch != EOF) {
181      buf[i++] = ch;
182      if (i == len - 1) {
183         char *p;
184         len += len;
185         p = xosrealloc(buf, len);
186         if (!p) {
187            osfree(buf);
188            return NULL;
189         }
190         buf = p;
191      }
192      ch = getc(fh);
193   }
194   if (ch == '\n' || ch == '\r') {
195      int otherone = ch ^ ('\n' ^ '\r');
196      ch = getc(fh);
197      /* if it's not the other eol character, put it back */
198      if (ch != otherone) ungetc(ch, fh);
199   }
200   buf[i++] = '\0';
201   return buf;
202}
203
204static int lineno = 0;
205static point *start = NULL;
206
207static char*
208delimword(char *ln, char** lr)
209{
210   char *le;
211
212   while (*ln == ' ' || *ln == '\t' || *ln == '\n' || *ln == '\r')
213      ln++;
214
215   le = ln;
216   while (*le != ' ' && *le != '\t' && *le != '\n' && *le != '\r' && *le != ';' && *le != '\0')
217      le++;
218
219   if (*le == '\0' || *le == ';') {
220      *lr = le;
221   } else {
222      *lr = le + 1;
223   }
224   
225   *le = '\0';
226   return ln;
227}
228
229static void
230parseconfigline(char *ln)
231{
232   point *p;
233   const stn *s;
234   const stn *t;
235   leg *l;
236   char *lc = NULL;
237
238   ln = delimword(ln, &lc);
239
240   if (*ln == '\0') return;
241
242   if (strcmp(ln, "*start")==0) {
243      ln = delimword(lc, &lc);
244      if (*ln == 0) fatalerror(/*Command without station name in config, line %i*/602, lineno);
245      for (p = headpoint.next; p != NULL; p = p->next) {
246         for (s = p->stns; s; s = s->next) {
247            if (strcmp(s->label, ln)==0) {
248               start = p;
249               printf(msg(/*Starting from station %s*/604),ln);
250               putnl();
251               goto loopend;
252            }
253         }
254      }
255      warning(/*Failed to find station %s in config, line %i*/603, ln, lineno);
256   } else if (strcmp(ln, "*eleft")==0) {
257      char *ll = delimword(lc, &lc);
258      if (*ll == 0) fatalerror(/*Command without station name in config, line %i*/602,lineno);
259      ln = delimword(lc, &lc);
260      if (*ln == 0) { /* One argument, look for point to switch at. */
261         for (p = headpoint.next; p != NULL; p = p->next) {
262            for (s = p->stns; s; s = s->next) {
263               if (strcmp(s->label, ll)==0) {
264                  printf(msg(/*Plotting to the left from station %s*/605),ll);
265                  putnl();
266                  p->dir = ELEFT;
267                  goto loopend;
268               }
269            }
270         }
271         warning(/*Failed to find station %s in config, line %i*/603, ll, lineno);       
272      } else { /* Two arguments look for a specific leg */
273         for (l = headleg.next; l; l=l->next) {
274            point * fr = l->fr;
275            point * to = l->to;
276            if (fr && to) {
277               for (s=fr->stns; s; s=s->next) {
278                  int b = 0;
279                  if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) {
280                     char * lr = (b ? ll : ln);
281                     for (t=to->stns; t; t=t->next) {
282                        if (strcmp(t->label,lr)==0) {
283                           printf(msg(/*Plotting to the left from leg %s -> %s*/607), s->label, t->label);
284                           putnl();
285                           l->dir = ELEFT;
286                           goto loopend;
287                        }
288                     }
289                  }
290               }
291            }
292         }
293         warning(/*Failed to find leg %s-%s in config, line %i*/606, ll, ln, lineno);
294      }
295   } else if (strcmp(ln, "*eright")==0) {
296      char *ll = delimword(lc, &lc);
297      if (*ll == 0) fatalerror(/*Command without station name in config, line %i*/602,lineno);
298      ln = delimword(lc, &lc);
299      if (*ln == 0) { /* One argument, look for point to switch at. */
300         for (p = headpoint.next; p != NULL; p = p->next) {
301            for (s = p->stns; s; s = s->next) {
302               if (strcmp(s->label, ll)==0) {
303                  printf(msg(/*Plotting to the right from station %s*/608),ll);
304                  putnl();
305                  p->dir = ERIGHT;
306                  goto loopend;
307               }
308            }
309         }
310         warning(/*Failed to find station %s in config, line %i*/603, ll, lineno);     
311      } else { /* Two arguments look for a specific leg */
312         for (l = headleg.next; l; l=l->next) {
313            point * fr = l->fr;
314            point * to = l->to;
315            if (fr && to) {
316               for (s=fr->stns; s; s=s->next) {
317                  int b = 0;
318                  if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) {
319                     char * lr = (b ? ll : ln);
320                     for (t=to->stns; t; t=t->next) {
321                        if (strcmp(t->label,lr)==0) {
322                           printf(msg(/*Plotting to the right from leg %s -> %s*/609), s->label, t->label);
323                           printf("\n");
324                           l->dir=ERIGHT;
325                           goto loopend;
326                        }
327                     }
328                  }
329               }
330            }
331         }
332         warning(/*Failed to find leg %s-%s in config, line %i*/606, ll, ln, lineno);
333      }
334   } else if (strcmp(ln, "*eswap")==0) {
335      char *ll = delimword(lc, &lc);
336      if (*ll == 0) fatalerror(/*Command without station name in config, line %i*/602,lineno);
337      ln = delimword(lc, &lc);
338      if (*ln == 0) { /* One argument, look for point to switch at. */
339         for (p = headpoint.next; p != NULL; p = p->next) {
340            for (s = p->stns; s; s = s->next) {
341               if (strcmp(s->label, ll)==0) {
342                  printf(msg(/*Swapping plot direction from station %s*/615),ll);
343                  putnl();
344                  p->dir = ESWAP;
345                  goto loopend;
346               }
347            }
348         }
349         warning(/*Failed to find station %s in config, line %i*/603, ll, lineno);     
350      } else { /* Two arguments look for a specific leg */
351         for (l = headleg.next; l; l=l->next) {
352            point * fr = l->fr;
353            point * to = l->to;
354            if (fr && to) {
355               for (s=fr->stns; s; s=s->next) {
356                  int b = 0;
357                  if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) {
358                     char * lr = (b ? ll : ln);
359                     for (t=to->stns; t; t=t->next) {
360                        if (strcmp(t->label,lr)==0) {
361                           printf(msg(/*Swapping plot direction from leg %s -> %s*/616), s->label, t->label);
362                           printf("\n");
363                           l->dir = ESWAP;
364                           goto loopend;
365                        }
366                     }
367                  }
368               }
369            }
370         }
371         warning(/*Failed to find leg %s-%s in config, line %i*/606, ll, ln, lineno);
372      }
373   } else if (strcmp(ln, "*break")==0) {
374      char *ll = delimword(lc, &lc);
375      if (*ll == 0) fatalerror(/*Command without station name in config, line %i*/602,lineno);
376      ln = delimword(lc, &lc);
377      if (*ln == 0) { /* One argument, look for point to break at. */
378         for (p = headpoint.next; p != NULL; p = p->next) {
379            for (s = p->stns; s; s = s->next) {
380               if (strcmp(s->label, ll)==0) {
381                  printf(msg(/*Breaking survey at station %s*/610), ll);
382                  putnl();
383                  p->fBroken = 1;
384                  goto loopend;
385               }
386            }
387         }
388         warning(/*Failed to find station %s in config, line %i*/603, ll, lineno); 
389      } else { /* Two arguments look for a specific leg */
390         for (l = headleg.next; l; l=l->next) {
391            point * fr = l->fr;
392            point * to = l->to;
393            if (fr && to) {
394               for (s=fr->stns; s; s=s->next) {
395                  int b = 0;
396                  if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) {
397                     char * lr = (b ? ll : ln);
398                     for (t=to->stns; t; t=t->next) {
399                        if (strcmp(t->label,lr)==0) {
400                           printf(msg(/*Breaking survey at leg %s -> %s*/611), s->label, t->label);
401                           putnl();
402                           l->broken = (b ? BREAK_TO : BREAK_FR);
403                           goto loopend;
404                        }
405                     }
406                  }
407               }
408            }
409         }
410         warning(/*Failed to find leg %s-%s in config, line %i*/606, ll, ln, lineno);
411      }
412   } else {
413      fatalerror(/*Don't understand command `%s' in config, line %i*/600, ln, lineno);
414   }
415 loopend:
416   ln = delimword(lc, &lc);
417   if (*ln != 0) {
418      fatalerror(/*Unexpected content `%s' in config, line %i*/601, ln, lineno);
419   }
420}
421
422static const struct option long_opts[] = {
423   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
424   {"survey", required_argument, 0, 's'},
425   {"specfile", required_argument, 0, 'p'},
426   {"help", no_argument, 0, HLP_HELP},
427   {"version", no_argument, 0, HLP_VERSION},
428   {0, 0, 0, 0}
429};
430
431#define short_opts "s:p:"
432
433static struct help_msg help[] = {
434/*                              <-- */
435   {HLP_ENCODELONG(0),          "only load the sub-survey with this prefix"},
436   {HLP_ENCODELONG(1),          "apply specifications from the named file"},
437   {0, 0}
438};
439
440int
441main(int argc, char **argv)
442{
443   const char *fnm_in, *fnm_out;
444   char *desc;
445   img_point pt;
446   int result;
447   point *fr = NULL, *to;
448   double zMax = -DBL_MAX;
449   point *p;
450   const char *survey = NULL;
451   const char *specfile = NULL;
452
453   msg_init(argv);
454
455   cmdline_set_syntax_message("INPUT_3D_FILE [OUTPUT_3D_FILE]", NULL);
456   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2);
457   while (1) {
458      int opt = cmdline_getopt();
459      if (opt == EOF) break;
460      if (opt == 's') survey = optarg;
461      if (opt == 'p') specfile = optarg;
462   }
463   fnm_in = argv[optind++];
464   if (argv[optind]) {
465      fnm_out = argv[optind];
466   } else {
467      fnm_out = add_ext("extend", EXT_SVX_3D);
468   }
469
470   /* try to open image file, and check it has correct header */
471   pimg = img_open_survey(fnm_in, survey);
472   if (pimg == NULL) fatalerror(img_error(), fnm_in);
473
474   putnl();
475   puts(msg(/*Reading in data - please wait...*/105));
476
477   htab = osmalloc(ossizeof(pfx*) * HTAB_SIZE);
478   {
479       int i;
480       for (i = 0; i < HTAB_SIZE; ++i) htab[i] = NULL;
481   }
482
483   do {
484      result = img_read_item(pimg, &pt);
485      switch (result) {
486      case img_MOVE:
487         fr = find_point(&pt);
488         break;
489      case img_LINE:
490         if (!fr) {
491            result = img_BAD;
492            break;
493         }
494         to = find_point(&pt);
495         if (!(pimg->flags & (img_FLAG_SURFACE|img_FLAG_SPLAY)))
496            add_leg(fr, to, pimg->label, pimg->flags);
497         fr = to;
498         break;
499      case img_LABEL:
500         to = find_point(&pt);
501         add_label(to, pimg->label, pimg->flags);
502         break;
503      case img_BAD:
504         (void)img_close(pimg);
505         fatalerror(img_error(), fnm_in);
506      }
507   } while (result != img_STOP);
508
509   desc = osmalloc(strlen(pimg->title) + 11 + 1);
510   strcpy(desc, pimg->title);
511   strcat(desc, " (extended)");
512
513   (void)img_close(pimg);
514
515   if (specfile) {
516      FILE *fs = NULL;
517      printf(msg(/*Applying specfile: `%s'*/613), specfile);
518      putnl();
519      fs = fopenWithPthAndExt("", specfile, NULL, "r", NULL);
520      if (fs == NULL) fatalerror(/*Unable to open file*/93, specfile);
521      while (!feof(fs)) {
522         char *lbuf = NULL;
523         lbuf = getline_alloc(fs, 32);
524         lineno++;
525         if (!lbuf) {
526            error(/*Error reading line %i from spec file*/612, lineno);
527            osfree(lbuf);
528            break;
529         }
530         parseconfigline(lbuf);
531         osfree(lbuf);
532      }
533   }
534
535   if (start == NULL) { /* i.e. start wasn't specified in specfile */
536   
537      /* start at the highest entrance with some legs attached */
538      for (p = headpoint.next; p != NULL; p = p->next) {
539         if (p->order > 0 && p->p.z > zMax) {
540            const stn *s;
541            for (s = p->stns; s; s = s->next) {
542               if (s->flags & img_SFLAG_ENTRANCE) {
543                  start = p;
544                  zMax = p->p.z;
545                  break;
546               }
547            }
548         }
549      }
550      if (start == NULL) {
551         /* if no entrances with legs, start at the highest 1-node */
552         for (p = headpoint.next; p != NULL; p = p->next) {
553            if (p->order == 1 && p->p.z > zMax) {
554               start = p;
555               zMax = p->p.z;
556            }
557         }
558         /* of course we may have no 1-nodes... */
559         if (start == NULL) {
560            for (p = headpoint.next; p != NULL; p = p->next) {
561               if (p->order != 0 && p->p.z > zMax) {
562                  start = p;
563                  zMax = p->p.z;
564               }
565            }
566            if (start == NULL) {
567               /* There are no legs - just pick the highest station... */
568               for (p = headpoint.next; p != NULL; p = p->next) {
569                  if (p->p.z > zMax) {
570                     start = p;
571                     zMax = p->p.z;
572                  }
573               }
574               if (!start) fatalerror(/*No survey data*/43);
575            }
576         }
577      }
578   }
579
580   printf(msg(/*Writing out .3d file...*/614));
581   putnl();
582   pimg = img_open_write(fnm_out, desc, fTrue);
583
584   /* Only does single connected component currently. */
585   do_stn(start, 0.0, NULL, ERIGHT, 0);
586   if (!img_close(pimg)) {
587      (void)remove(fnm_out);
588      fatalerror(img_error(), fnm_out);
589   }
590
591   return EXIT_SUCCESS;
592}
593
594static int adjust_direction(int dir, int by) {
595    if (by == ESWAP)
596        return dir ^ (ELEFT|ERIGHT);
597    if (by)
598        return by;
599    return dir;
600}
601
602static void
603do_stn(point *p, double X, const char *prefix, int dir, int labOnly)
604{
605   leg *l, *lp;
606   double dX;
607   const stn *s;
608   int odir = dir;
609
610   for (s = p->stns; s; s = s->next) {
611      img_write_item(pimg, img_LABEL, s->flags, s->label, X, 0, p->p.z);
612   }
613   if (labOnly || p->fBroken) {
614      return;
615   }
616
617   lp = &headleg;
618   for (l = lp->next; l; lp = l, l = lp->next) {
619      dir = odir;
620      if (l->fDone) {
621         /* this case happens if a recursive call causes the next leg to be
622          * removed, leaving our next pointing to a leg which has been dealt
623          * with... */
624         continue;
625      }
626      if (l->prefix != prefix) {
627         continue;
628      }
629      if (l->to == p) {
630         if (l->broken & BREAK_TO) continue;
631         lp->next = l->next;
632         /* adjust direction of extension if necessary */
633         dir = adjust_direction(dir, l->to->dir);
634         dir = adjust_direction(dir, l->dir);
635
636         dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y);
637         if (dir == ELEFT) dX = -dX;
638         img_write_item(pimg, img_MOVE, 0, NULL, X + dX, 0, l->fr->p.z);
639         img_write_item(pimg, img_LINE, l->flags, l->prefix,
640                        X, 0, l->to->p.z);
641         l->fDone = 1;
642         do_stn(l->fr, X + dX, l->prefix, dir, (l->broken & BREAK_FR));
643         l = lp;
644      } else if (l->fr == p) {
645         if (l->broken & BREAK_FR) continue;
646         lp->next = l->next;
647         /* adjust direction of extension if necessary */
648         dir = adjust_direction(dir, l->fr->dir);
649         dir = adjust_direction(dir, l->dir);
650
651         dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y);
652         if (dir == ELEFT) dX = -dX;
653         img_write_item(pimg, img_MOVE, 0, NULL, X, 0, l->fr->p.z);
654         img_write_item(pimg, img_LINE, l->flags, l->prefix,
655                        X + dX, 0, l->to->p.z);
656         l->fDone = 1;
657         do_stn(l->to, X + dX, l->prefix, dir, (l->broken & BREAK_TO));
658         l = lp;
659      }
660   }
661   lp = &headleg;
662   for (l = lp->next; l; lp = l, l = lp->next) {
663      dir = odir;
664      if (l->fDone) {
665         /* this case happens iff a recursive call causes the next leg to be
666          * removed, leaving our next pointing to a leg which has been dealt
667          * with... */
668         continue;
669      }
670      if (l->to == p) {
671         if (l->broken & BREAK_TO) continue;
672         lp->next = l->next;
673         /* adjust direction of extension if necessary */
674         dir = adjust_direction(dir, l->to->dir);
675         dir = adjust_direction(dir, l->dir);
676
677         dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y);
678         if (dir == ELEFT) dX = -dX;
679         img_write_item(pimg, img_MOVE, 0, NULL, X + dX, 0, l->fr->p.z);
680         img_write_item(pimg, img_LINE, l->flags, l->prefix,
681                        X, 0, l->to->p.z);
682         l->fDone = 1;
683         do_stn(l->fr, X + dX, l->prefix, dir, (l->broken & BREAK_FR));
684         l = lp;
685      } else if (l->fr == p) {
686         if (l->broken & BREAK_FR) continue;
687         lp->next = l->next;
688         /* adjust direction of extension if necessary */
689         dir = adjust_direction(dir, l->fr->dir);
690         dir = adjust_direction(dir, l->dir);
691
692         dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y);
693         if (dir == ELEFT) dX = -dX;
694         img_write_item(pimg, img_MOVE, 0, NULL, X, 0, l->fr->p.z);
695         img_write_item(pimg, img_LINE, l->flags, l->prefix,
696                        X + dX, 0, l->to->p.z);
697         l->fDone = 1;
698         do_stn(l->to, X + dX, l->prefix, dir, (l->broken & BREAK_TO));
699         l = lp;
700      }
701   }
702}
Note: See TracBrowser for help on using the repository browser.