source: git/src/extend.c @ ffee37e

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since ffee37e was ffee37e, checked in by Olly Betts <olly@…>, 9 years ago

src/extend.c,src/mainfrm.cc,src/sorterr.c: Fix message references
which I failed to update.

  • Property mode set to 100644
File size: 20.1 KB
Line 
1/* extend.c
2 * Produce an extended elevation
3 * Copyright (C) 1995-2002,2005,2010,2011,2013,2014 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  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_hosted.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_out;
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->fBroken = 0;
132   p->next = headpoint.next;
133   headpoint.next = p;
134   return p;
135}
136
137static void
138add_leg(point *fr, point *to, const char *prefix, int flags)
139{
140   leg *l;
141   fr->order++;
142   to->order++;
143   l = osmalloc(ossizeof(leg));
144   l->fr = fr;
145   l->to = to;
146   if (prefix)
147      l->prefix = find_prefix(prefix);
148   else
149      l->prefix = NULL;
150   l->next = headleg.next;
151   l->dir = 0;
152   l->fDone = 0;
153   l->broken = 0;
154   l->flags = flags;
155   headleg.next = l;
156}
157
158static void
159add_label(point *p, const char *label, int flags)
160{
161   stn *s = osnew(stn);
162   s->label = osstrdup(label);
163   s->flags = flags;
164   s->next = p->stns;
165   p->stns = s;
166}
167
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
181   ch = GETC(fh);
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      }
194      ch = GETC(fh);
195   }
196   if (ch == '\n' || ch == '\r') {
197      int otherone = ch ^ ('\n' ^ '\r');
198      ch = GETC(fh);
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   }
226
227   *le = '\0';
228   return ln;
229}
230
231static void
232parseconfigline(const char *fnm, char *ln)
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);
246      if (*ln == 0)
247         fatalerror_in_file(fnm, lineno, /*Expecting station name*/28);
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;
252               /* TRANSLATORS: for extend: "extend" is starting to produce an extended elevation from station %s */
253               printf(msg(/*Starting from station %s*/512),ln);
254               putnl();
255               goto loopend;
256            }
257         }
258      }
259      /* TRANSLATORS: for extend: the user specified breaking a loop or
260       * changing extend direction at this station, but we didn’t find it in
261       * the 3d file */
262      warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ln);
263   } else if (strcmp(ln, "*eleft")==0) {
264      char *ll = delimword(lc, &lc);
265      if (*ll == 0)
266         fatalerror_in_file(fnm, lineno, /*Expecting station name*/28);
267      ln = delimword(lc, &lc);
268      if (*ln == 0) { /* One argument, look for point to switch at. */
269         for (p = headpoint.next; p != NULL; p = p->next) {
270            for (s = p->stns; s; s = s->next) {
271               if (strcmp(s->label, ll)==0) {
272                  /* TRANSLATORS: for extend: */
273                  printf(msg(/*Extending to the left from station %s*/513), ll);
274                  putnl();
275                  p->dir = ELEFT;
276                  goto loopend;
277               }
278            }
279         }
280         warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll);
281      } else { /* Two arguments look for a specific leg */
282         for (l = headleg.next; l; l=l->next) {
283            point * fr = l->fr;
284            point * to = l->to;
285            if (fr && to) {
286               for (s=fr->stns; s; s=s->next) {
287                  int b = 0;
288                  if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) {
289                     char * lr = (b ? ll : ln);
290                     for (t=to->stns; t; t=t->next) {
291                        if (strcmp(t->label,lr)==0) {
292                           /* TRANSLATORS: for extend: */
293                           printf(msg(/*Extending to the left from leg %s → %s*/515), s->label, t->label);
294                           putnl();
295                           l->dir = ELEFT;
296                           goto loopend;
297                        }
298                     }
299                  }
300               }
301            }
302         }
303         /* TRANSLATORS: for extend: the user specified breaking a loop or
304          * changing extend direction at this leg, but we didn’t find it in the
305          * 3d file */
306         warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln);
307      }
308   } else if (strcmp(ln, "*eright")==0) {
309      char *ll = delimword(lc, &lc);
310      if (*ll == 0)
311         fatalerror_in_file(fnm, lineno, /*Expecting station name*/28);
312      ln = delimword(lc, &lc);
313      if (*ln == 0) { /* One argument, look for point to switch at. */
314         for (p = headpoint.next; p != NULL; p = p->next) {
315            for (s = p->stns; s; s = s->next) {
316               if (strcmp(s->label, ll)==0) {
317                  /* TRANSLATORS: for extend: */
318                  printf(msg(/*Extending to the right from station %s*/514), ll);
319                  putnl();
320                  p->dir = ERIGHT;
321                  goto loopend;
322               }
323            }
324         }
325         warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll);
326      } else { /* Two arguments look for a specific leg */
327         for (l = headleg.next; l; l=l->next) {
328            point * fr = l->fr;
329            point * to = l->to;
330            if (fr && to) {
331               for (s=fr->stns; s; s=s->next) {
332                  int b = 0;
333                  if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) {
334                     char * lr = (b ? ll : ln);
335                     for (t=to->stns; t; t=t->next) {
336                        if (strcmp(t->label,lr)==0) {
337                           /* TRANSLATORS: for extend: */
338                           printf(msg(/*Extending to the right from leg %s → %s*/516), s->label, t->label);
339                           putnl();
340                           l->dir=ERIGHT;
341                           goto loopend;
342                        }
343                     }
344                  }
345               }
346            }
347         }
348         warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln);
349      }
350   } else if (strcmp(ln, "*eswap")==0) {
351      char *ll = delimword(lc, &lc);
352      if (*ll == 0)
353         fatalerror_in_file(fnm, lineno, /*Expecting station name*/28);
354      ln = delimword(lc, &lc);
355      if (*ln == 0) { /* One argument, look for point to switch at. */
356         for (p = headpoint.next; p != NULL; p = p->next) {
357            for (s = p->stns; s; s = s->next) {
358               if (strcmp(s->label, ll)==0) {
359                  /* TRANSLATORS: for extend: */
360                  printf(msg(/*Swapping extend direction from station %s*/519),ll);
361                  putnl();
362                  p->dir = ESWAP;
363                  goto loopend;
364               }
365            }
366         }
367         warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll);
368      } else { /* Two arguments look for a specific leg */
369         for (l = headleg.next; l; l=l->next) {
370            point * fr = l->fr;
371            point * to = l->to;
372            if (fr && to) {
373               for (s=fr->stns; s; s=s->next) {
374                  int b = 0;
375                  if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) {
376                     char * lr = (b ? ll : ln);
377                     for (t=to->stns; t; t=t->next) {
378                        if (strcmp(t->label,lr)==0) {
379                           /* TRANSLATORS: for extend: */
380                           printf(msg(/*Swapping extend direction from leg %s → %s*/520), s->label, t->label);
381                           putnl();
382                           l->dir = ESWAP;
383                           goto loopend;
384                        }
385                     }
386                  }
387               }
388            }
389         }
390         warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln);
391      }
392   } else if (strcmp(ln, "*break")==0) {
393      char *ll = delimword(lc, &lc);
394      if (*ll == 0)
395         fatalerror_in_file(fnm, lineno, /*Expecting station name*/28);
396      ln = delimword(lc, &lc);
397      if (*ln == 0) { /* One argument, look for point to break at. */
398         for (p = headpoint.next; p != NULL; p = p->next) {
399            for (s = p->stns; s; s = s->next) {
400               if (strcmp(s->label, ll)==0) {
401                  /* TRANSLATORS: for extend: */
402                  printf(msg(/*Breaking survey loop at station %s*/517), ll);
403                  putnl();
404                  p->fBroken = 1;
405                  goto loopend;
406               }
407            }
408         }
409         warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll);
410      } else { /* Two arguments look for a specific leg */
411         for (l = headleg.next; l; l=l->next) {
412            point * fr = l->fr;
413            point * to = l->to;
414            if (fr && to) {
415               for (s=fr->stns; s; s=s->next) {
416                  int b = 0;
417                  if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) {
418                     char * lr = (b ? ll : ln);
419                     for (t=to->stns; t; t=t->next) {
420                        if (strcmp(t->label,lr)==0) {
421                           /* TRANSLATORS: for extend: */
422                           printf(msg(/*Breaking survey loop at leg %s → %s*/518), s->label, t->label);
423                           putnl();
424                           l->broken = (b ? BREAK_TO : BREAK_FR);
425                           goto loopend;
426                        }
427                     }
428                  }
429               }
430            }
431         }
432         warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln);
433      }
434   } else {
435      fatalerror_in_file(fnm, lineno, /*Unknown command “%s”*/12, ln);
436   }
437 loopend:
438   ln = delimword(lc, &lc);
439   if (*ln != 0) {
440      fatalerror_in_file(fnm, lineno, /*End of line not blank*/15);
441      /* FIXME: give ln as context? */
442   }
443}
444
445static const struct option long_opts[] = {
446   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
447   {"survey", required_argument, 0, 's'},
448   {"specfile", required_argument, 0, 'p'},
449   {"help", no_argument, 0, HLP_HELP},
450   {"version", no_argument, 0, HLP_VERSION},
451   {0, 0, 0, 0}
452};
453
454#define short_opts "s:p:"
455
456static struct help_msg help[] = {
457/*                              <-- */
458   {HLP_ENCODELONG(0),        /*only load the sub-survey with this prefix*/199, 0},
459   {0, 0, 0}
460};
461
462int
463main(int argc, char **argv)
464{
465   const char *fnm_in, *fnm_out;
466   char *desc;
467   img_point pt;
468   int result;
469   point *fr = NULL, *to;
470   double zMax = -DBL_MAX;
471   point *p;
472   const char *survey = NULL;
473   const char *specfile = NULL;
474   img *pimg;
475   int have_xsect = 0;
476
477   msg_init(argv);
478
479   /* TRANSLATORS: Part of extend --help */
480   cmdline_set_syntax_message(/*INPUT_3D_FILE [OUTPUT_3D_FILE]*/267, 0, NULL);
481   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2);
482   while (1) {
483      int opt = cmdline_getopt();
484      if (opt == EOF) break;
485      if (opt == 's') survey = optarg;
486      if (opt == 'p') specfile = optarg;
487   }
488   fnm_in = argv[optind++];
489   if (argv[optind]) {
490      fnm_out = argv[optind];
491   } else {
492      char * base_in = base_from_fnm(fnm_in);
493      char * base_out = osmalloc(strlen(base_in) + 8);
494      strcpy(base_out, base_in);
495      strcat(base_out, "_extend");
496      fnm_out = add_ext(base_out, EXT_SVX_3D);
497      osfree(base_in);
498      osfree(base_out);
499   }
500
501   /* try to open image file, and check it has correct header */
502   pimg = img_open_survey(fnm_in, survey);
503   if (pimg == NULL) fatalerror(img_error2msg(img_error()), fnm_in);
504
505   putnl();
506   puts(msg(/*Reading in data - please wait…*/105));
507
508   htab = osmalloc(ossizeof(pfx*) * HTAB_SIZE);
509   {
510       int i;
511       for (i = 0; i < HTAB_SIZE; ++i) htab[i] = NULL;
512   }
513
514   do {
515      result = img_read_item(pimg, &pt);
516      switch (result) {
517      case img_MOVE:
518         fr = find_point(&pt);
519         break;
520      case img_LINE:
521         if (!fr) {
522            result = img_BAD;
523            break;
524         }
525         to = find_point(&pt);
526         if (!(pimg->flags & (img_FLAG_SURFACE|img_FLAG_SPLAY)))
527            add_leg(fr, to, pimg->label, pimg->flags);
528         fr = to;
529         break;
530      case img_LABEL:
531         to = find_point(&pt);
532         add_label(to, pimg->label, pimg->flags);
533         break;
534      case img_BAD:
535         (void)img_close(pimg);
536         fatalerror(img_error2msg(img_error()), fnm_in);
537         break;
538      case img_XSECT:
539         have_xsect = 1;
540         break;
541      }
542   } while (result != img_STOP);
543
544   desc = osstrdup(pimg->title);
545
546   if (specfile) {
547      FILE *fs = NULL;
548      char *fnm_used;
549      /* TRANSLATORS: for extend: */
550      printf(msg(/*Applying specfile: “%s”*/521), specfile);
551      putnl();
552      fs = fopenWithPthAndExt("", specfile, NULL, "r", &fnm_used);
553      if (fs == NULL) fatalerror(/*Couldn’t open file “%s”*/24, specfile);
554      while (!feof(fs)) {
555         char *lbuf = getline_alloc(fs, 32);
556         lineno++;
557         if (!lbuf)
558            fatalerror_in_file(fnm_used, lineno, /*Error reading file*/18);
559         parseconfigline(fnm_used, lbuf);
560         osfree(lbuf);
561      }
562      osfree(fnm_used);
563   }
564
565   if (start == NULL) { /* i.e. start wasn't specified in specfile */
566
567      /* start at the highest entrance with some legs attached */
568      for (p = headpoint.next; p != NULL; p = p->next) {
569         if (p->order > 0 && p->p.z > zMax) {
570            const stn *s;
571            for (s = p->stns; s; s = s->next) {
572               if (s->flags & img_SFLAG_ENTRANCE) {
573                  start = p;
574                  zMax = p->p.z;
575                  break;
576               }
577            }
578         }
579      }
580      if (start == NULL) {
581         /* if no entrances with legs, start at the highest 1-node */
582         for (p = headpoint.next; p != NULL; p = p->next) {
583            if (p->order == 1 && p->p.z > zMax) {
584               start = p;
585               zMax = p->p.z;
586            }
587         }
588         /* of course we may have no 1-nodes... */
589         if (start == NULL) {
590            for (p = headpoint.next; p != NULL; p = p->next) {
591               if (p->order != 0 && p->p.z > zMax) {
592                  start = p;
593                  zMax = p->p.z;
594               }
595            }
596            if (start == NULL) {
597               /* There are no legs - just pick the highest station... */
598               for (p = headpoint.next; p != NULL; p = p->next) {
599                  if (p->p.z > zMax) {
600                     start = p;
601                     zMax = p->p.z;
602                  }
603               }
604               if (!start) fatalerror(/*No survey data*/43);
605            }
606         }
607      }
608   }
609
610   /* TRANSLATORS: for extend:
611    * Used to tell the user that a file is being written - %s is the filename
612    */
613   printf(msg(/*Writing %s…*/522), fnm_out);
614   putnl();
615   pimg_out = img_open_write(fnm_out, desc, img_FFLAG_EXTENDED);
616
617   /* Only does single connected component currently. */
618   do_stn(start, 0.0, NULL, ERIGHT, 0);
619
620   if (have_xsect) {
621      img_rewind(pimg);
622      /* Read ahead on pimg before writing pimg_out so we find out if an
623       * img_XSECT_END comes next. */
624      char * label = NULL;
625      int flags = 0;
626      do {
627         result = img_read_item(pimg, &pt);
628         if (result == img_XSECT || result == img_XSECT_END) {
629            if (label) {
630               if (result == img_XSECT_END)
631                  flags |= img_XFLAG_END;
632               img_write_item(pimg_out, img_XSECT, flags, label, 0, 0, 0);
633               osfree(label);
634               label = NULL;
635            }
636         }
637         if (result == img_XSECT) {
638            label = osstrdup(pimg->label);
639            flags = pimg->flags;
640            pimg_out->l = pimg->l;
641            pimg_out->r = pimg->r;
642            pimg_out->u = pimg->u;
643            pimg_out->d = pimg->d;
644         }
645      } while (result != img_STOP);
646   }
647
648   (void)img_close(pimg);
649
650   if (!img_close(pimg_out)) {
651      (void)remove(fnm_out);
652      fatalerror(img_error2msg(img_error()), fnm_out);
653   }
654
655   return EXIT_SUCCESS;
656}
657
658static int adjust_direction(int dir, int by) {
659    if (by == ESWAP)
660        return dir ^ (ELEFT|ERIGHT);
661    if (by)
662        return by;
663    return dir;
664}
665
666static void
667do_stn(point *p, double X, const char *prefix, int dir, int labOnly)
668{
669   leg *l, *lp;
670   double dX;
671   const stn *s;
672   int odir = dir;
673
674   for (s = p->stns; s; s = s->next) {
675      img_write_item(pimg_out, img_LABEL, s->flags, s->label, X, 0, p->p.z);
676   }
677   if (labOnly || p->fBroken) {
678      return;
679   }
680
681   lp = &headleg;
682   for (l = lp->next; l; lp = l, l = lp->next) {
683      dir = odir;
684      if (l->fDone) {
685         /* this case happens if a recursive call causes the next leg to be
686          * removed, leaving our next pointing to a leg which has been dealt
687          * with... */
688         continue;
689      }
690      if (l->prefix != prefix) {
691         continue;
692      }
693      if (l->to == p) {
694         if (l->broken & BREAK_TO) continue;
695         lp->next = l->next;
696         /* adjust direction of extension if necessary */
697         dir = adjust_direction(dir, l->to->dir);
698         dir = adjust_direction(dir, l->dir);
699
700         dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y);
701         if (dir == ELEFT) dX = -dX;
702         img_write_item(pimg_out, img_MOVE, 0, NULL, X + dX, 0, l->fr->p.z);
703         img_write_item(pimg_out, img_LINE, l->flags, l->prefix,
704                        X, 0, l->to->p.z);
705         l->fDone = 1;
706         do_stn(l->fr, X + dX, l->prefix, dir, (l->broken & BREAK_FR));
707         l = lp;
708      } else if (l->fr == p) {
709         if (l->broken & BREAK_FR) continue;
710         lp->next = l->next;
711         /* adjust direction of extension if necessary */
712         dir = adjust_direction(dir, l->fr->dir);
713         dir = adjust_direction(dir, l->dir);
714
715         dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y);
716         if (dir == ELEFT) dX = -dX;
717         img_write_item(pimg_out, img_MOVE, 0, NULL, X, 0, l->fr->p.z);
718         img_write_item(pimg_out, img_LINE, l->flags, l->prefix,
719                        X + dX, 0, l->to->p.z);
720         l->fDone = 1;
721         do_stn(l->to, X + dX, l->prefix, dir, (l->broken & BREAK_TO));
722         l = lp;
723      }
724   }
725   lp = &headleg;
726   for (l = lp->next; l; lp = l, l = lp->next) {
727      dir = odir;
728      if (l->fDone) {
729         /* this case happens iff a recursive call causes the next leg to be
730          * removed, leaving our next pointing to a leg which has been dealt
731          * with... */
732         continue;
733      }
734      if (l->to == p) {
735         if (l->broken & BREAK_TO) continue;
736         lp->next = l->next;
737         /* adjust direction of extension if necessary */
738         dir = adjust_direction(dir, l->to->dir);
739         dir = adjust_direction(dir, l->dir);
740
741         dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y);
742         if (dir == ELEFT) dX = -dX;
743         img_write_item(pimg_out, img_MOVE, 0, NULL, X + dX, 0, l->fr->p.z);
744         img_write_item(pimg_out, img_LINE, l->flags, l->prefix,
745                        X, 0, l->to->p.z);
746         l->fDone = 1;
747         do_stn(l->fr, X + dX, l->prefix, dir, (l->broken & BREAK_FR));
748         l = lp;
749      } else if (l->fr == p) {
750         if (l->broken & BREAK_FR) continue;
751         lp->next = l->next;
752         /* adjust direction of extension if necessary */
753         dir = adjust_direction(dir, l->fr->dir);
754         dir = adjust_direction(dir, l->dir);
755
756         dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y);
757         if (dir == ELEFT) dX = -dX;
758         img_write_item(pimg_out, img_MOVE, 0, NULL, X, 0, l->fr->p.z);
759         img_write_item(pimg_out, img_LINE, l->flags, l->prefix,
760                        X + dX, 0, l->to->p.z);
761         l->fDone = 1;
762         do_stn(l->to, X + dX, l->prefix, dir, (l->broken & BREAK_TO));
763         l = lp;
764      }
765   }
766}
Note: See TracBrowser for help on using the repository browser.