source: git/src/extend.c @ c5b16cb

warn-only-for-hanging-survey
Last change on this file since c5b16cb was b3b0900, checked in by Olly Betts <olly@…>, 7 months ago

Clean up header includes

  • Property mode set to 100644
File size: 22.1 KB
Line 
1/* extend.c
2 * Produce an extended elevation
3 * Copyright (C) 1995-2002,2005,2010,2011,2013,2014,2016,2017 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#include <config.h>
22
23#include <float.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include "cmdline.h"
29#include "debug.h"
30#include "filelist.h"
31#include "filename.h"
32#include "hash.h"
33#include "img_hosted.h"
34#include "message.h"
35#include "osalloc.h"
36#include "useful.h"
37
38/* To save memory we should probably use the prefix hash for the prefix on
39 * point labels (FIXME) */
40
41typedef struct stn {
42   const char *label;
43   int flags;
44   const struct stn *next;
45} stn;
46
47typedef struct splay {
48   struct POINT *pt;
49   struct splay *next;
50} splay;
51
52typedef struct POINT {
53   img_point p;
54   double X;
55   const stn *stns;
56   unsigned int order;
57   char dir;
58   char fDone;
59   char fBroken;
60   splay *splays;
61   struct POINT *next;
62} point;
63
64typedef struct LEG {
65   point *fr, *to;
66   const char *prefix;
67   char dir;
68   char fDone;
69   char broken;
70   int flags;
71   struct LEG *next;
72} leg;
73
74/* Values for leg.broken: */
75#define BREAK_FR    0x01
76#define BREAK_TO    0x02
77
78/* Values for point.dir and leg.dir: */
79#define ELEFT  0x01
80#define ERIGHT 0x02
81#define ESWAP  0x04
82
83static point headpoint = {{0, 0, 0}, 0, NULL, 0, 0, 0, 0, NULL, NULL};
84
85static leg headleg = {NULL, NULL, NULL, 0, 0, 0, 0, NULL};
86
87static img *pimg_out;
88
89static int show_breaks = 0;
90
91static void do_stn(point *, double, const char *, int, int, double, double);
92
93typedef struct pfx {
94   const char *label;
95   struct pfx *next;
96} pfx;
97
98static pfx **htab;
99
100#define HTAB_SIZE 0x2000
101
102static const char *
103find_prefix(const char *prefix)
104{
105   pfx *p;
106   int hash;
107
108   SVX_ASSERT(prefix);
109
110   hash = hash_string(prefix) & (HTAB_SIZE - 1);
111   for (p = htab[hash]; p; p = p->next) {
112      if (strcmp(prefix, p->label) == 0) return p->label;
113   }
114
115   p = osnew(pfx);
116   p->label = osstrdup(prefix);
117   p->next = htab[hash];
118   htab[hash] = p;
119
120   return p->label;
121}
122
123static point *
124find_point(const img_point *pt)
125{
126   point *p;
127   for (p = headpoint.next; p != NULL; p = p->next) {
128      if (pt->x == p->p.x && pt->y == p->p.y && pt->z == p->p.z) {
129         return p;
130      }
131   }
132
133   p = osmalloc(ossizeof(point));
134   p->p = *pt;
135   p->X = HUGE_VAL;
136   p->stns = NULL;
137   p->order = 0;
138   p->dir = 0;
139   p->fDone = 0;
140   p->fBroken = 0;
141   p->splays = NULL;
142   p->next = headpoint.next;
143   headpoint.next = p;
144   return p;
145}
146
147static void
148add_leg(point *fr, point *to, const char *prefix, int flags)
149{
150   leg *l;
151   fr->order++;
152   to->order++;
153   l = osmalloc(ossizeof(leg));
154   l->fr = fr;
155   l->to = to;
156   if (prefix)
157      l->prefix = find_prefix(prefix);
158   else
159      l->prefix = NULL;
160   l->next = headleg.next;
161   l->dir = 0;
162   l->fDone = 0;
163   l->broken = 0;
164   l->flags = flags;
165   headleg.next = l;
166}
167
168static void
169add_label(point *p, const char *label, int flags)
170{
171   stn *s = osnew(stn);
172   s->label = osstrdup(label);
173   s->flags = flags;
174   s->next = p->stns;
175   p->stns = s;
176}
177
178/* Read in config file */
179
180
181/* lifted from img.c Should be put somewhere common? JPNP*/
182static char *
183getline_alloc(FILE *fh, size_t ilen)
184{
185   int ch;
186   size_t i = 0;
187   size_t len = ilen;
188   char *buf = xosmalloc(len);
189   if (!buf) return NULL;
190
191   ch = GETC(fh);
192   while (ch != '\n' && ch != '\r' && ch != EOF) {
193      buf[i++] = ch;
194      if (i == len - 1) {
195         char *p;
196         len += len;
197         p = xosrealloc(buf, len);
198         if (!p) {
199            osfree(buf);
200            return NULL;
201         }
202         buf = p;
203      }
204      ch = GETC(fh);
205   }
206   if (ch == '\n' || ch == '\r') {
207      int otherone = ch ^ ('\n' ^ '\r');
208      ch = GETC(fh);
209      /* if it's not the other eol character, put it back */
210      if (ch != otherone) ungetc(ch, fh);
211   }
212   buf[i++] = '\0';
213   return buf;
214}
215
216static int lineno = 0;
217static point *start = NULL;
218
219static char*
220delimword(char *ln, char** lr)
221{
222   char *le;
223
224   while (*ln == ' ' || *ln == '\t' || *ln == '\n' || *ln == '\r')
225      ln++;
226
227   le = ln;
228   while (*le != ' ' && *le != '\t' && *le != '\n' && *le != '\r' && *le != ';' && *le != '\0')
229      le++;
230
231   if (*le == '\0' || *le == ';') {
232      *lr = le;
233   } else {
234      *lr = le + 1;
235   }
236
237   *le = '\0';
238   return ln;
239}
240
241static void
242parseconfigline(const char *fnm, char *ln)
243{
244   point *p;
245   const stn *s;
246   const stn *t;
247   leg *l;
248   char *lc = NULL;
249
250   ln = delimword(ln, &lc);
251
252   if (*ln == '\0') return;
253
254   if (strcmp(ln, "*start")==0) {
255      ln = delimword(lc, &lc);
256      if (*ln == 0)
257         /* TRANSLATORS: Here "station" is a survey station, not a train station. */
258         fatalerror_in_file(fnm, lineno, /*Expecting station name*/28);
259      for (p = headpoint.next; p != NULL; p = p->next) {
260         for (s = p->stns; s; s = s->next) {
261            if (strcmp(s->label, ln)==0) {
262               start = p;
263               /* TRANSLATORS: for extend: "extend" is starting to produce an extended elevation from station %s */
264               printf(msg(/*Starting from station %s*/512),ln);
265               putnl();
266               goto loopend;
267            }
268         }
269      }
270      /* TRANSLATORS: for extend: the user specified breaking a loop or
271       * changing extend direction at this station, but we didn’t find it in
272       * the 3d file */
273      warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ln);
274   } else if (strcmp(ln, "*eleft")==0) {
275      char *ll = delimword(lc, &lc);
276      if (*ll == 0)
277         fatalerror_in_file(fnm, lineno, /*Expecting station name*/28);
278      ln = delimword(lc, &lc);
279      if (*ln == 0) {
280         /* One argument - look for station to switch at. */
281         for (p = headpoint.next; p != NULL; p = p->next) {
282            for (s = p->stns; s; s = s->next) {
283               if (strcmp(s->label, ll)==0) {
284                  /* TRANSLATORS: for extend: */
285                  printf(msg(/*Extending to the left from station %s*/513), ll);
286                  putnl();
287                  p->dir = ELEFT;
288                  goto loopend;
289               }
290            }
291         }
292         warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll);
293      } else {
294         /* Two arguments - look for a specified leg. */
295         for (l = headleg.next; l; l=l->next) {
296            point * fr = l->fr;
297            point * to = l->to;
298            if (fr && to) {
299               for (s=fr->stns; s; s=s->next) {
300                  int b = 0;
301                  if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) {
302                     char * lr = (b ? ll : ln);
303                     for (t=to->stns; t; t=t->next) {
304                        if (strcmp(t->label,lr)==0) {
305                           /* TRANSLATORS: for extend: */
306                           printf(msg(/*Extending to the left from leg %s → %s*/515), s->label, t->label);
307                           putnl();
308                           l->dir = ELEFT;
309                           goto loopend;
310                        }
311                     }
312                  }
313               }
314            }
315         }
316         /* TRANSLATORS: for extend: the user specified breaking a loop or
317          * changing extend direction at this leg, but we didn’t find it in the
318          * 3d file */
319         warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln);
320      }
321   } else if (strcmp(ln, "*eright")==0) {
322      char *ll = delimword(lc, &lc);
323      if (*ll == 0)
324         fatalerror_in_file(fnm, lineno, /*Expecting station name*/28);
325      ln = delimword(lc, &lc);
326      if (*ln == 0) {
327         /* One argument - look for station to switch at. */
328         for (p = headpoint.next; p != NULL; p = p->next) {
329            for (s = p->stns; s; s = s->next) {
330               if (strcmp(s->label, ll)==0) {
331                  /* TRANSLATORS: for extend: */
332                  printf(msg(/*Extending to the right from station %s*/514), ll);
333                  putnl();
334                  p->dir = ERIGHT;
335                  goto loopend;
336               }
337            }
338         }
339         warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll);
340      } else {
341         /* Two arguments - look for a specified leg. */
342         for (l = headleg.next; l; l=l->next) {
343            point * fr = l->fr;
344            point * to = l->to;
345            if (fr && to) {
346               for (s=fr->stns; s; s=s->next) {
347                  int b = 0;
348                  if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) {
349                     char * lr = (b ? ll : ln);
350                     for (t=to->stns; t; t=t->next) {
351                        if (strcmp(t->label,lr)==0) {
352                           /* TRANSLATORS: for extend: */
353                           printf(msg(/*Extending to the right from leg %s → %s*/516), s->label, t->label);
354                           putnl();
355                           l->dir=ERIGHT;
356                           goto loopend;
357                        }
358                     }
359                  }
360               }
361            }
362         }
363         warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln);
364      }
365   } else if (strcmp(ln, "*eswap")==0) {
366      char *ll = delimword(lc, &lc);
367      if (*ll == 0)
368         fatalerror_in_file(fnm, lineno, /*Expecting station name*/28);
369      ln = delimword(lc, &lc);
370      if (*ln == 0) {
371         /* One argument - look for station to switch at. */
372         for (p = headpoint.next; p != NULL; p = p->next) {
373            for (s = p->stns; s; s = s->next) {
374               if (strcmp(s->label, ll)==0) {
375                  /* TRANSLATORS: for extend: */
376                  printf(msg(/*Swapping extend direction from station %s*/519),ll);
377                  putnl();
378                  p->dir = ESWAP;
379                  goto loopend;
380               }
381            }
382         }
383         warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll);
384      } else {
385         /* Two arguments - look for a specified leg. */
386         for (l = headleg.next; l; l=l->next) {
387            point * fr = l->fr;
388            point * to = l->to;
389            if (fr && to) {
390               for (s=fr->stns; s; s=s->next) {
391                  int b = 0;
392                  if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) {
393                     char * lr = (b ? ll : ln);
394                     for (t=to->stns; t; t=t->next) {
395                        if (strcmp(t->label,lr)==0) {
396                           /* TRANSLATORS: for extend: */
397                           printf(msg(/*Swapping extend direction from leg %s → %s*/520), s->label, t->label);
398                           putnl();
399                           l->dir = ESWAP;
400                           goto loopend;
401                        }
402                     }
403                  }
404               }
405            }
406         }
407         warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln);
408      }
409   } else if (strcmp(ln, "*break")==0) {
410      char *ll = delimword(lc, &lc);
411      if (*ll == 0)
412         fatalerror_in_file(fnm, lineno, /*Expecting station name*/28);
413      ln = delimword(lc, &lc);
414      if (*ln == 0) {
415         /* One argument - look for specified station to break at. */
416         for (p = headpoint.next; p != NULL; p = p->next) {
417            for (s = p->stns; s; s = s->next) {
418               if (strcmp(s->label, ll)==0) {
419                  /* TRANSLATORS: for extend: */
420                  printf(msg(/*Breaking survey loop at station %s*/517), ll);
421                  putnl();
422                  p->fBroken = 1;
423                  goto loopend;
424               }
425            }
426         }
427         warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll);
428      } else {
429         /* Two arguments - look for specified leg and disconnect it at the
430          * first station. */
431         for (l = headleg.next; l; l=l->next) {
432            point * fr = l->fr;
433            point * to = l->to;
434            if (fr && to) {
435               for (s=fr->stns; s; s=s->next) {
436                  int b = 0;
437                  if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) {
438                     char * lr = (b ? ll : ln);
439                     for (t=to->stns; t; t=t->next) {
440                        if (strcmp(t->label,lr)==0) {
441                           /* TRANSLATORS: for extend: */
442                           printf(msg(/*Breaking survey loop at leg %s → %s*/518), s->label, t->label);
443                           putnl();
444                           l->broken = (b ? BREAK_TO : BREAK_FR);
445                           goto loopend;
446                        }
447                     }
448                  }
449               }
450            }
451         }
452         warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln);
453      }
454   } else {
455      fatalerror_in_file(fnm, lineno, /*Unknown command “%s”*/12, ln);
456   }
457 loopend:
458   ln = delimword(lc, &lc);
459   if (*ln != 0) {
460      fatalerror_in_file(fnm, lineno, /*End of line not blank*/15);
461      /* FIXME: give ln as context? */
462   }
463}
464
465static const struct option long_opts[] = {
466   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
467   {"survey", required_argument, 0, 's'},
468   {"specfile", required_argument, 0, 'p'},
469   {"show-breaks", no_argument, 0, 'b' },
470   {"help", no_argument, 0, HLP_HELP},
471   {"version", no_argument, 0, HLP_VERSION},
472   {0, 0, 0, 0}
473};
474
475#define short_opts "s:p:b"
476
477static struct help_msg help[] = {
478/*                              <-- */
479   {HLP_ENCODELONG(0),        /*only load the sub-survey with this prefix*/199, 0},
480   /* TRANSLATORS: --help output for extend --specfile option */
481   {HLP_ENCODELONG(1),        /*.espec file to control extending*/90, 0},
482   /* TRANSLATORS: --help output for extend --show-breaks option */
483   {HLP_ENCODELONG(2),        /*show breaks with surface survey legs in output*/91, 0},
484   {0, 0, 0}
485};
486
487static point *
488pick_start_stn(void)
489{
490   point * best = NULL;
491   double zMax = -DBL_MAX;
492   point *p;
493
494   /* Start at the highest entrance with some legs attached. */
495   for (p = headpoint.next; p != NULL; p = p->next) {
496      if (p->order > 0 && p->p.z > zMax) {
497         const stn *s;
498         for (s = p->stns; s; s = s->next) {
499            if (s->flags & img_SFLAG_ENTRANCE) {
500               zMax = p->p.z;
501               return p;
502            }
503         }
504      }
505   }
506   if (best) return best;
507
508   /* If no entrances with legs, start at the highest 1-node. */
509   for (p = headpoint.next; p != NULL; p = p->next) {
510      if (p->order == 1 && p->p.z > zMax) {
511         best = p;
512         zMax = p->p.z;
513      }
514   }
515   if (best) return best;
516
517   /* of course we may have no 1-nodes... */
518   for (p = headpoint.next; p != NULL; p = p->next) {
519      if (p->order != 0 && p->p.z > zMax) {
520         best = p;
521         zMax = p->p.z;
522      }
523   }
524   if (best) return best;
525
526   /* There are no legs - just pick the highest station... */
527   for (p = headpoint.next; p != NULL; p = p->next) {
528      if (p->p.z > zMax) {
529         best = p;
530         zMax = p->p.z;
531      }
532   }
533   return best;
534}
535
536int
537main(int argc, char **argv)
538{
539   const char *fnm_in, *fnm_out;
540   char *desc;
541   img_point pt;
542   int result;
543   point *fr = NULL, *to;
544   const char *survey = NULL;
545   const char *specfile = NULL;
546   img *pimg;
547   int xsections = 0, splays = 0;
548
549   msg_init(argv);
550
551   /* TRANSLATORS: Part of extend --help */
552   cmdline_set_syntax_message(/*INPUT_3D_FILE [OUTPUT_3D_FILE]*/267, 0, NULL);
553   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2);
554   while (1) {
555      int opt = cmdline_getopt();
556      if (opt == EOF) break;
557      switch (opt) {
558         case 'b':
559            show_breaks = 1;
560            break;
561         case 's':
562            survey = optarg;
563            break;
564         case 'p':
565            specfile = optarg;
566            break;
567      }
568   }
569   fnm_in = argv[optind++];
570   if (argv[optind]) {
571      fnm_out = argv[optind];
572   } else {
573      char * base_in = base_from_fnm(fnm_in);
574      char * base_out = osmalloc(strlen(base_in) + 8);
575      strcpy(base_out, base_in);
576      strcat(base_out, "_extend");
577      fnm_out = add_ext(base_out, EXT_SVX_3D);
578      osfree(base_in);
579      osfree(base_out);
580   }
581
582   /* try to open image file, and check it has correct header */
583   pimg = img_open_survey(fnm_in, survey);
584   if (pimg == NULL) fatalerror(img_error2msg(img_error()), fnm_in);
585
586   putnl();
587   puts(msg(/*Reading in data - please wait…*/105));
588
589   htab = osmalloc(ossizeof(pfx*) * HTAB_SIZE);
590   {
591       int i;
592       for (i = 0; i < HTAB_SIZE; ++i) htab[i] = NULL;
593   }
594
595   do {
596      result = img_read_item(pimg, &pt);
597      switch (result) {
598      case img_MOVE:
599         fr = find_point(&pt);
600         break;
601      case img_LINE:
602         if (!fr) {
603            result = img_BAD;
604            break;
605         }
606         to = find_point(&pt);
607         if (!(pimg->flags & img_FLAG_SURFACE)) {
608            if (pimg->flags & img_FLAG_SPLAY) {
609               ++splays;
610            } else {
611               add_leg(fr, to, pimg->label, pimg->flags);
612            }
613         }
614         fr = to;
615         break;
616      case img_LABEL:
617         to = find_point(&pt);
618         add_label(to, pimg->label, pimg->flags);
619         break;
620      case img_BAD:
621         (void)img_close(pimg);
622         fatalerror(img_error2msg(img_error()), fnm_in);
623         break;
624      case img_XSECT:
625      case img_XSECT_END:
626         ++xsections;
627         break;
628      }
629   } while (result != img_STOP);
630
631   if (splays) {
632      img_rewind(pimg);
633      fr = NULL;
634      do {
635         result = img_read_item(pimg, &pt);
636         switch (result) {
637         case img_MOVE:
638            fr = find_point(&pt);
639            break;
640         case img_LINE:
641            if (!fr) {
642               result = img_BAD;
643               break;
644            }
645            to = find_point(&pt);
646            if (!(pimg->flags & img_FLAG_SURFACE)) {
647               if (pimg->flags & img_FLAG_SPLAY) {
648                  splay *sp = osmalloc(ossizeof(splay));
649                  --splays;
650                  if (fr->order) {
651                     if (to->order == 0) {
652                        sp->pt = to;
653                        sp->next = fr->splays;
654                        fr->splays = sp;
655                     } else {
656                        printf("Splay without a dead end from %s to %s\n", fr->stns->label, to->stns->label);
657                        osfree(sp);
658                     }
659                  } else if (to->order) {
660                     sp->pt = fr;
661                     sp->next = to->splays;
662                     to->splays = sp;
663                  } else {
664                     printf("Isolated splay from %s to %s\n", fr->stns->label, to->stns->label);
665                     osfree(sp);
666                  }
667               }
668            }
669            fr = to;
670            break;
671         }
672      } while (splays && result != img_STOP);
673   }
674
675   desc = osstrdup(pimg->title);
676
677   if (specfile) {
678      FILE *fs = NULL;
679      char *fnm_used;
680      /* TRANSLATORS: for extend: */
681      printf(msg(/*Applying specfile: “%s”*/521), specfile);
682      putnl();
683      fs = fopenWithPthAndExt("", specfile, NULL, "r", &fnm_used);
684      if (fs == NULL) fatalerror(/*Couldn’t open file “%s”*/24, specfile);
685      while (!feof(fs)) {
686         char *lbuf = getline_alloc(fs, 32);
687         lineno++;
688         if (!lbuf)
689            fatalerror_in_file(fnm_used, lineno, /*Error reading file*/18);
690         parseconfigline(fnm_used, lbuf);
691         osfree(lbuf);
692      }
693      osfree(fnm_used);
694   }
695
696   if (start == NULL) {
697      /* *start wasn't specified in specfile. */
698      start = pick_start_stn();
699      if (!start) fatalerror(/*No survey data*/43);
700   }
701
702   /* TRANSLATORS: for extend:
703    * Used to tell the user that a file is being written - %s is the filename
704    */
705   printf(msg(/*Writing %s…*/522), fnm_out);
706   putnl();
707   pimg_out = img_open_write(fnm_out, desc, img_FFLAG_EXTENDED);
708
709   /* Only does single connected component currently. */
710   do_stn(start, 0.0, NULL, ERIGHT, 0, 0.0, 0.0);
711
712   if (xsections) {
713      img_rewind(pimg);
714      /* Read ahead on pimg before writing pimg_out so we find out if an
715       * img_XSECT_END comes next. */
716      char * label = NULL;
717      int flags = 0;
718      do {
719         result = img_read_item(pimg, &pt);
720         if (result != img_XSECT && result != img_XSECT_END)
721            continue;
722         --xsections;
723         if (label) {
724            if (result == img_XSECT_END)
725               flags |= img_XFLAG_END;
726            img_write_item(pimg_out, img_XSECT, flags, label, 0, 0, 0);
727            osfree(label);
728            label = NULL;
729         }
730         if (result == img_XSECT) {
731            label = osstrdup(pimg->label);
732            flags = pimg->flags;
733            pimg_out->l = pimg->l;
734            pimg_out->r = pimg->r;
735            pimg_out->u = pimg->u;
736            pimg_out->d = pimg->d;
737         }
738      } while (xsections && result != img_STOP);
739   }
740
741   (void)img_close(pimg);
742
743   if (!img_close(pimg_out)) {
744      (void)remove(fnm_out);
745      fatalerror(img_error2msg(img_error()), fnm_out);
746   }
747
748   return EXIT_SUCCESS;
749}
750
751static int adjust_direction(int dir, int by) {
752    if (by == ESWAP)
753        return dir ^ (ELEFT|ERIGHT);
754    if (by)
755        return by;
756    return dir;
757}
758
759static void
760do_splays(point *p, double X, int dir, double tdx, double tdy)
761{
762   const splay *sp;
763   double a;
764   double C, S;
765
766   if (!p->splays) return;
767
768   if (tdx == 0 && tdy == 0) {
769       /* Two adjacent plumbs, or a pair of legs that exactly cancel. */
770       return;
771   }
772
773   /* Bearing in radians. */
774   a = atan2(tdx, tdy);
775   if (dir == ELEFT) {
776       a = -M_PI_2 - a;
777   } else {
778       a = M_PI_2 - a;
779   }
780   C = cos(a);
781   S = sin(a);
782   for (sp = p->splays; sp; sp = sp->next) {
783      double x = X;
784      double z = p->p.z;
785      img_write_item(pimg_out, img_MOVE, 0, NULL, x, 0, z);
786
787      double dx = sp->pt->p.x - p->p.x;
788      double dy = sp->pt->p.y - p->p.y;
789      double dz = sp->pt->p.z - p->p.z;
790
791      double tmp = dx * C + dy * S;
792      dy = dy * C - dx * S;
793      dx = tmp;
794
795      img_write_item(pimg_out, img_LINE, img_FLAG_SPLAY, NULL, x + dx, dy, z + dz);
796   }
797   p->splays = NULL;
798}
799
800static void
801do_stn(point *p, double X, const char *prefix, int dir, int labOnly,
802       double odx, double ody)
803{
804   leg *l, *lp;
805   double dX;
806   const stn *s;
807   int odir = dir;
808   int try_all;
809   int order = p->order;
810
811   for (s = p->stns; s; s = s->next) {
812      img_write_item(pimg_out, img_LABEL, s->flags, s->label, X, 0, p->p.z);
813   }
814
815   if (show_breaks && p->X != HUGE_VAL && p->X != X) {
816      /* Draw "surface" leg between broken stations. */
817      img_write_item(pimg_out, img_MOVE, 0, NULL, p->X, 0, p->p.z);
818      img_write_item(pimg_out, img_LINE, img_FLAG_SURFACE, NULL, X, 0, p->p.z);
819   }
820   p->X = X;
821   if (labOnly || p->fBroken) {
822      return;
823   }
824
825
826   if (order == 0) {
827      /* We've reached a dead end. */
828      do_splays(p, X, dir, odx, ody);
829      return;
830   }
831
832   /* It's better to follow legs along a survey, so make two passes and only
833    * follow legs in the same survey for the first pass.
834    */
835   for (try_all = 0; try_all != 2; ++try_all) {
836      lp = &headleg;
837      for (l = lp->next; l; lp = l, l = lp->next) {
838         dir = odir;
839         if (l->fDone) {
840            /* this case happens iff a recursive call causes the next leg to be
841             * removed, leaving our next pointing to a leg which has been dealt
842             * with... */
843            continue;
844         }
845         if (!try_all && l->prefix != prefix) {
846            continue;
847         }
848         int break_flag;
849         point *p2;
850         if (l->to == p) {
851            break_flag = BREAK_TO;
852            p2 = l->fr;
853         } else if (l->fr == p) {
854            break_flag = BREAK_FR;
855            p2 = l->to;
856         } else {
857            continue;
858         }
859         if (l->broken & break_flag) continue;
860         lp->next = l->next;
861         /* adjust direction of extension if necessary */
862         dir = adjust_direction(dir, p->dir);
863         dir = adjust_direction(dir, l->dir);
864
865         double dx = p2->p.x - p->p.x;
866         double dy = p2->p.y - p->p.y;
867         dX = hypot(dx, dy);
868         double X2 = X;
869         if (dir == ELEFT) {
870            X2 -= dX;
871         } else {
872            X2 += dX;
873         }
874
875         if (p->splays) {
876            do_splays(p, X, dir, odx + dx, ody + dy);
877         }
878
879         img_write_item(pimg_out, img_MOVE, 0, NULL, X, 0, p->p.z);
880         img_write_item(pimg_out, img_LINE, l->flags, l->prefix,
881                        X2, 0, p2->p.z);
882
883         /* We arrive at p2 via a leg, so that's one down right away. */
884         --p2->order;
885
886         l->fDone = 1;
887         /* l->broken doesn't have break_flag set as we checked that above. */
888         do_stn(p2, X2, l->prefix, dir, l->broken, dx, dy);
889         l = lp;
890         if (--order == 0) return;
891      }
892   }
893}
Note: See TracBrowser for help on using the repository browser.