source: git/src/extend.c @ e02a6a6

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-datawalls-data-hanging-as-warning
Last change on this file since e02a6a6 was e02a6a6, checked in by Olly Betts <olly@…>, 14 years ago

configure.in,src/: Define GETC() in config.h and use it everywhere.
Similarly define PUTC() to use putc_unlocked() where available and
use that everywhere too.

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

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