source: git/src/extend.c

faster-cavernlog
Last change on this file was b3b0900, checked in by Olly Betts <olly@…>, 3 weeks ago

Clean up header includes

  • Property mode set to 100644
File size: 22.1 KB
RevLine 
[7ee2b42]1/* extend.c
[d1b1380]2 * Produce an extended elevation
[a412bc9]3 * Copyright (C) 1995-2002,2005,2010,2011,2013,2014,2016,2017 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
[4c83f84]21#include <config.h>
[d1b1380]22
[e55ee28]23#include <float.h>
[d1b1380]24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
[acc20b1]28#include "cmdline.h"
[693388e]29#include "debug.h"
[d1b1380]30#include "filelist.h"
[693388e]31#include "filename.h"
32#include "hash.h"
[a405bc1]33#include "img_hosted.h"
[693388e]34#include "message.h"
[b3b0900]35#include "osalloc.h"
[693388e]36#include "useful.h"
[d1b1380]37
[693388e]38/* To save memory we should probably use the prefix hash for the prefix on
39 * point labels (FIXME) */
[ea1c342]40
41typedef struct stn {
42   const char *label;
43   int flags;
[ee11c6a7]44   const struct stn *next;
[ea1c342]45} stn;
46
[ae6d92e]47typedef struct splay {
48   struct POINT *pt;
49   struct splay *next;
50} splay;
51
[d1b1380]52typedef struct POINT {
[0ed0e16]53   img_point p;
[9f841e3]54   double X;
[ea1c342]55   const stn *stns;
[647407d]56   unsigned int order;
[d92d282]57   char dir;
58   char fDone;
59   char fBroken;
[ae6d92e]60   splay *splays;
[d1b1380]61   struct POINT *next;
62} point;
63
64typedef struct LEG {
65   point *fr, *to;
[693388e]66   const char *prefix;
[d92d282]67   char dir;
68   char fDone;
69   char broken;
[95c3272]70   int flags;
71   struct LEG *next;
[d1b1380]72} leg;
73
[d92d282]74/* Values for leg.broken: */
75#define BREAK_FR    0x01
76#define BREAK_TO    0x02
[d1b1380]77
[d92d282]78/* Values for point.dir and leg.dir: */
79#define ELEFT  0x01
80#define ERIGHT 0x02
81#define ESWAP  0x04
82
[ae6d92e]83static point headpoint = {{0, 0, 0}, 0, NULL, 0, 0, 0, 0, NULL, NULL};
[d92d282]84
85static leg headleg = {NULL, NULL, NULL, 0, 0, 0, 0, NULL};
[d1b1380]86
[31b7105]87static img *pimg_out;
[d1b1380]88
[c50cbcf]89static int show_breaks = 0;
90
[e67cb60]91static void do_stn(point *, double, const char *, int, int, double, double);
[d1b1380]92
[693388e]93typedef struct pfx {
94   const char *label;
95   struct pfx *next;
96} pfx;
97
98static pfx **htab;
99
[63dc4eb]100#define HTAB_SIZE 0x2000
101
[693388e]102static const char *
103find_prefix(const char *prefix)
104{
105   pfx *p;
106   int hash;
107
[4c07c51]108   SVX_ASSERT(prefix);
[421b7d2]109
[63dc4eb]110   hash = hash_string(prefix) & (HTAB_SIZE - 1);
[693388e]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
[a420b49]123static point *
[0ed0e16]124find_point(const img_point *pt)
[a420b49]125{
[d1b1380]126   point *p;
[647407d]127   for (p = headpoint.next; p != NULL; p = p->next) {
[0ed0e16]128      if (pt->x == p->p.x && pt->y == p->p.y && pt->z == p->p.z) {
[647407d]129         return p;
130      }
131   }
[a420b49]132
133   p = osmalloc(ossizeof(point));
[0ed0e16]134   p->p = *pt;
[9f841e3]135   p->X = HUGE_VAL;
[ea1c342]136   p->stns = NULL;
[7ee2b42]137   p->order = 0;
[d92d282]138   p->dir = 0;
139   p->fDone = 0;
[8add4c9]140   p->fBroken = 0;
[ae6d92e]141   p->splays = NULL;
[a420b49]142   p->next = headpoint.next;
143   headpoint.next = p;
[d1b1380]144   return p;
145}
146
[a420b49]147static void
[693388e]148add_leg(point *fr, point *to, const char *prefix, int flags)
[a420b49]149{
[95c3272]150   leg *l;
[7ee2b42]151   fr->order++;
152   to->order++;
[95c3272]153   l = osmalloc(ossizeof(leg));
154   l->fr = fr;
155   l->to = to;
[693388e]156   if (prefix)
157      l->prefix = find_prefix(prefix);
158   else
159      l->prefix = NULL;
[95c3272]160   l->next = headleg.next;
[d92d282]161   l->dir = 0;
[95c3272]162   l->fDone = 0;
[8add4c9]163   l->broken = 0;
[95c3272]164   l->flags = flags;
165   headleg.next = l;
[d1b1380]166}
167
[a420b49]168static void
[95c3272]169add_label(point *p, const char *label, int flags)
[a420b49]170{
[ea1c342]171   stn *s = osnew(stn);
172   s->label = osstrdup(label);
173   s->flags = flags;
174   s->next = p->stns;
175   p->stns = s;
[d1b1380]176}
177
[d92d282]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
[e02a6a6]191   ch = GETC(fh);
[d92d282]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      }
[e02a6a6]204      ch = GETC(fh);
[d92d282]205   }
206   if (ch == '\n' || ch == '\r') {
207      int otherone = ch ^ ('\n' ^ '\r');
[e02a6a6]208      ch = GETC(fh);
[d92d282]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   }
[6e4a123]236
[d92d282]237   *le = '\0';
238   return ln;
239}
240
241static void
[7926772]242parseconfigline(const char *fnm, char *ln)
[d92d282]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);
[7926772]256      if (*ln == 0)
[0b8c321]257         /* TRANSLATORS: Here "station" is a survey station, not a train station. */
[7926772]258         fatalerror_in_file(fnm, lineno, /*Expecting station name*/28);
[d92d282]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;
[736f7df]263               /* TRANSLATORS: for extend: "extend" is starting to produce an extended elevation from station %s */
[7926772]264               printf(msg(/*Starting from station %s*/512),ln);
[d92d282]265               putnl();
266               goto loopend;
267            }
268         }
269      }
[736f7df]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 */
[7926772]273      warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ln);
[d92d282]274   } else if (strcmp(ln, "*eleft")==0) {
275      char *ll = delimword(lc, &lc);
[7926772]276      if (*ll == 0)
277         fatalerror_in_file(fnm, lineno, /*Expecting station name*/28);
[d92d282]278      ln = delimword(lc, &lc);
[deb536fb]279      if (*ln == 0) {
280         /* One argument - look for station to switch at. */
[d92d282]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) {
[736f7df]284                  /* TRANSLATORS: for extend: */
[7926772]285                  printf(msg(/*Extending to the left from station %s*/513), ll);
[d92d282]286                  putnl();
287                  p->dir = ELEFT;
288                  goto loopend;
289               }
290            }
291         }
[7926772]292         warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll);
[deb536fb]293      } else {
294         /* Two arguments - look for a specified leg. */
[d92d282]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) {
[736f7df]305                           /* TRANSLATORS: for extend: */
[ee7511a]306                           printf(msg(/*Extending to the left from leg %s → %s*/515), s->label, t->label);
[d92d282]307                           putnl();
308                           l->dir = ELEFT;
309                           goto loopend;
310                        }
311                     }
312                  }
313               }
314            }
315         }
[736f7df]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 */
[ee7511a]319         warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln);
[d92d282]320      }
321   } else if (strcmp(ln, "*eright")==0) {
322      char *ll = delimword(lc, &lc);
[7926772]323      if (*ll == 0)
324         fatalerror_in_file(fnm, lineno, /*Expecting station name*/28);
[d92d282]325      ln = delimword(lc, &lc);
[deb536fb]326      if (*ln == 0) {
327         /* One argument - look for station to switch at. */
[d92d282]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) {
[736f7df]331                  /* TRANSLATORS: for extend: */
[7926772]332                  printf(msg(/*Extending to the right from station %s*/514), ll);
[d92d282]333                  putnl();
334                  p->dir = ERIGHT;
335                  goto loopend;
336               }
337            }
338         }
[7926772]339         warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll);
[deb536fb]340      } else {
341         /* Two arguments - look for a specified leg. */
[d92d282]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) {
[736f7df]352                           /* TRANSLATORS: for extend: */
[ee7511a]353                           printf(msg(/*Extending to the right from leg %s → %s*/516), s->label, t->label);
[7926772]354                           putnl();
[d92d282]355                           l->dir=ERIGHT;
356                           goto loopend;
357                        }
358                     }
359                  }
360               }
361            }
362         }
[ee7511a]363         warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln);
[d92d282]364      }
365   } else if (strcmp(ln, "*eswap")==0) {
366      char *ll = delimword(lc, &lc);
[7926772]367      if (*ll == 0)
368         fatalerror_in_file(fnm, lineno, /*Expecting station name*/28);
[d92d282]369      ln = delimword(lc, &lc);
[deb536fb]370      if (*ln == 0) {
371         /* One argument - look for station to switch at. */
[d92d282]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) {
[736f7df]375                  /* TRANSLATORS: for extend: */
[7926772]376                  printf(msg(/*Swapping extend direction from station %s*/519),ll);
[d92d282]377                  putnl();
378                  p->dir = ESWAP;
379                  goto loopend;
380               }
381            }
382         }
[7926772]383         warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll);
[deb536fb]384      } else {
385         /* Two arguments - look for a specified leg. */
[d92d282]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) {
[736f7df]396                           /* TRANSLATORS: for extend: */
[ee7511a]397                           printf(msg(/*Swapping extend direction from leg %s → %s*/520), s->label, t->label);
[7926772]398                           putnl();
[d92d282]399                           l->dir = ESWAP;
400                           goto loopend;
401                        }
402                     }
403                  }
404               }
405            }
406         }
[ee7511a]407         warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln);
[d92d282]408      }
409   } else if (strcmp(ln, "*break")==0) {
410      char *ll = delimword(lc, &lc);
[7926772]411      if (*ll == 0)
412         fatalerror_in_file(fnm, lineno, /*Expecting station name*/28);
[d92d282]413      ln = delimword(lc, &lc);
[deb536fb]414      if (*ln == 0) {
415         /* One argument - look for specified station to break at. */
[d92d282]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) {
[736f7df]419                  /* TRANSLATORS: for extend: */
[7926772]420                  printf(msg(/*Breaking survey loop at station %s*/517), ll);
[d92d282]421                  putnl();
422                  p->fBroken = 1;
423                  goto loopend;
424               }
425            }
426         }
[7926772]427         warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll);
[deb536fb]428      } else {
429         /* Two arguments - look for specified leg and disconnect it at the
430          * first station. */
[d92d282]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) {
[736f7df]441                           /* TRANSLATORS: for extend: */
[ee7511a]442                           printf(msg(/*Breaking survey loop at leg %s → %s*/518), s->label, t->label);
[d92d282]443                           putnl();
444                           l->broken = (b ? BREAK_TO : BREAK_FR);
445                           goto loopend;
446                        }
447                     }
448                  }
449               }
450            }
451         }
[ee7511a]452         warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln);
[d92d282]453      }
454   } else {
[0804fbe]455      fatalerror_in_file(fnm, lineno, /*Unknown command “%s”*/12, ln);
[d92d282]456   }
457 loopend:
458   ln = delimword(lc, &lc);
459   if (*ln != 0) {
[7926772]460      fatalerror_in_file(fnm, lineno, /*End of line not blank*/15);
461      /* FIXME: give ln as context? */
[d92d282]462   }
463}
464
[acc20b1]465static const struct option long_opts[] = {
466   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
[76bbb7c9]467   {"survey", required_argument, 0, 's'},
[d92d282]468   {"specfile", required_argument, 0, 'p'},
[c50cbcf]469   {"show-breaks", no_argument, 0, 'b' },
[acc20b1]470   {"help", no_argument, 0, HLP_HELP},
471   {"version", no_argument, 0, HLP_VERSION},
472   {0, 0, 0, 0}
473};
474
[c50cbcf]475#define short_opts "s:p:b"
[acc20b1]476
477static struct help_msg help[] = {
478/*                              <-- */
[45af761]479   {HLP_ENCODELONG(0),        /*only load the sub-survey with this prefix*/199, 0},
[de934213]480   /* TRANSLATORS: --help output for extend --specfile option */
481   {HLP_ENCODELONG(1),        /*.espec file to control extending*/90, 0},
[c50cbcf]482   /* TRANSLATORS: --help output for extend --show-breaks option */
483   {HLP_ENCODELONG(2),        /*show breaks with surface survey legs in output*/91, 0},
[45af761]484   {0, 0, 0}
[acc20b1]485};
486
[47d8509]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
[a420b49]536int
537main(int argc, char **argv)
538{
[6f185e5]539   const char *fnm_in, *fnm_out;
[a2ad284]540   char *desc;
[0ed0e16]541   img_point pt;
[d1b1380]542   int result;
[647407d]543   point *fr = NULL, *to;
[76bbb7c9]544   const char *survey = NULL;
[d92d282]545   const char *specfile = NULL;
[31b7105]546   img *pimg;
[ae6d92e]547   int xsections = 0, splays = 0;
[d1b1380]548
[bdfe97f]549   msg_init(argv);
[d1b1380]550
[736f7df]551   /* TRANSLATORS: Part of extend --help */
[d8dbdff]552   cmdline_set_syntax_message(/*INPUT_3D_FILE [OUTPUT_3D_FILE]*/267, 0, NULL);
[acc20b1]553   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2);
[76bbb7c9]554   while (1) {
555      int opt = cmdline_getopt();
556      if (opt == EOF) break;
[cffe70f]557      switch (opt) {
[c50cbcf]558         case 'b':
559            show_breaks = 1;
560            break;
[cffe70f]561         case 's':
562            survey = optarg;
563            break;
564         case 'p':
565            specfile = optarg;
566            break;
567      }
[acc20b1]568   }
[6f185e5]569   fnm_in = argv[optind++];
[acc20b1]570   if (argv[optind]) {
[6f185e5]571      fnm_out = argv[optind];
[acc20b1]572   } else {
[6379f93]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);
[d1b1380]580   }
581
582   /* try to open image file, and check it has correct header */
[a2ad284]583   pimg = img_open_survey(fnm_in, survey);
[a405bc1]584   if (pimg == NULL) fatalerror(img_error2msg(img_error()), fnm_in);
[d1b1380]585
586   putnl();
[ee7511a]587   puts(msg(/*Reading in data - please wait…*/105));
[d1b1380]588
[63dc4eb]589   htab = osmalloc(ossizeof(pfx*) * HTAB_SIZE);
590   {
591       int i;
592       for (i = 0; i < HTAB_SIZE; ++i) htab[i] = NULL;
593   }
[421b7d2]594
[d1b1380]595   do {
[23f7ea7]596      result = img_read_item(pimg, &pt);
[d1b1380]597      switch (result) {
[a420b49]598      case img_MOVE:
[421b7d2]599         fr = find_point(&pt);
600         break;
[a420b49]601      case img_LINE:
[421b7d2]602         if (!fr) {
[76bbb7c9]603            result = img_BAD;
604            break;
[421b7d2]605         }
606         to = find_point(&pt);
[ae6d92e]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         }
[421b7d2]614         fr = to;
615         break;
[a420b49]616      case img_LABEL:
[388ad50]617         to = find_point(&pt);
618         add_label(to, pimg->label, pimg->flags);
[421b7d2]619         break;
[ed0f5b6]620      case img_BAD:
[6f185e5]621         (void)img_close(pimg);
[a405bc1]622         fatalerror(img_error2msg(img_error()), fnm_in);
[31b7105]623         break;
624      case img_XSECT:
[a412bc9]625      case img_XSECT_END:
626         ++xsections;
[b49ac56]627         break;
[d1b1380]628      }
[ed0f5b6]629   } while (result != img_STOP);
[d1b1380]630
[ae6d92e]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
[0c50ce3]675   desc = osstrdup(pimg->title);
[a2ad284]676
[d92d282]677   if (specfile) {
678      FILE *fs = NULL;
[7926772]679      char *fnm_used;
[736f7df]680      /* TRANSLATORS: for extend: */
[0804fbe]681      printf(msg(/*Applying specfile: “%s”*/521), specfile);
[d92d282]682      putnl();
[7926772]683      fs = fopenWithPthAndExt("", specfile, NULL, "r", &fnm_used);
[ffee37e]684      if (fs == NULL) fatalerror(/*Couldn’t open file “%s”*/24, specfile);
[d92d282]685      while (!feof(fs)) {
[7926772]686         char *lbuf = getline_alloc(fs, 32);
[d92d282]687         lineno++;
[7926772]688         if (!lbuf)
689            fatalerror_in_file(fnm_used, lineno, /*Error reading file*/18);
690         parseconfigline(fnm_used, lbuf);
[d92d282]691         osfree(lbuf);
[647407d]692      }
[7926772]693      osfree(fnm_used);
[647407d]694   }
[d92d282]695
[deb536fb]696   if (start == NULL) {
[47d8509]697      /* *start wasn't specified in specfile. */
698      start = pick_start_stn();
699      if (!start) fatalerror(/*No survey data*/43);
[647407d]700   }
[d92d282]701
[736f7df]702   /* TRANSLATORS: for extend:
703    * Used to tell the user that a file is being written - %s is the filename
704    */
[ee7511a]705   printf(msg(/*Writing %s…*/522), fnm_out);
[d92d282]706   putnl();
[0c50ce3]707   pimg_out = img_open_write(fnm_out, desc, img_FFLAG_EXTENDED);
[d1b1380]708
[d92d282]709   /* Only does single connected component currently. */
[e67cb60]710   do_stn(start, 0.0, NULL, ERIGHT, 0, 0.0, 0.0);
[31b7105]711
[a412bc9]712   if (xsections) {
[31b7105]713      img_rewind(pimg);
[2f1c0c0]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;
[31b7105]718      do {
719         result = img_read_item(pimg, &pt);
[a412bc9]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;
[2f1c0c0]729         }
[31b7105]730         if (result == img_XSECT) {
[2f1c0c0]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;
[31b7105]736            pimg_out->d = pimg->d;
737         }
[a412bc9]738      } while (xsections && result != img_STOP);
[31b7105]739   }
[8add4c9]740
[31b7105]741   (void)img_close(pimg);
742
743   if (!img_close(pimg_out)) {
[6f185e5]744      (void)remove(fnm_out);
[a405bc1]745      fatalerror(img_error2msg(img_error()), fnm_out);
[6f185e5]746   }
[cb3d1e2]747
[d1b1380]748   return EXIT_SUCCESS;
749}
750
[d92d282]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
[a420b49]759static void
[e67cb60]760do_splays(point *p, double X, int dir, double tdx, double tdy)
[ae6d92e]761{
762   const splay *sp;
763   double a;
764   double C, S;
765
[e67cb60]766   if (!p->splays) return;
[ae6d92e]767
[e67cb60]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);
[ae6d92e]775   if (dir == ELEFT) {
[e67cb60]776       a = -M_PI_2 - a;
[ae6d92e]777   } else {
[e67cb60]778       a = M_PI_2 - a;
[ae6d92e]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
[e67cb60]801do_stn(point *p, double X, const char *prefix, int dir, int labOnly,
802       double odx, double ody)
[a420b49]803{
804   leg *l, *lp;
[badeec8]805   double dX;
[ee11c6a7]806   const stn *s;
[d92d282]807   int odir = dir;
[74506f1]808   int try_all;
[ae6d92e]809   int order = p->order;
810
[ea1c342]811   for (s = p->stns; s; s = s->next) {
[31b7105]812      img_write_item(pimg_out, img_LABEL, s->flags, s->label, X, 0, p->p.z);
[ea1c342]813   }
[ae6d92e]814
[110957b]815   if (show_breaks && p->X != HUGE_VAL && p->X != X) {
[9f841e3]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;
[d92d282]821   if (labOnly || p->fBroken) {
822      return;
823   }
824
[ae6d92e]825
826   if (order == 0) {
827      /* We've reached a dead end. */
[e67cb60]828      do_splays(p, X, dir, odx, ody);
[ae6d92e]829      return;
830   }
831
[a16d11b]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    */
[74506f1]835   for (try_all = 0; try_all != 2; ++try_all) {
[a16d11b]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         }
[ea8d9a1]848         int break_flag;
849         point *p2;
[a16d11b]850         if (l->to == p) {
[ea8d9a1]851            break_flag = BREAK_TO;
852            p2 = l->fr;
[a16d11b]853         } else if (l->fr == p) {
[ea8d9a1]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) {
[ae6d92e]870            X2 -= dX;
[ea8d9a1]871         } else {
[ae6d92e]872            X2 += dX;
[a16d11b]873         }
[ea8d9a1]874
[5b1703f]875         if (p->splays) {
[e67cb60]876            do_splays(p, X, dir, odx + dx, ody + dy);
[5b1703f]877         }
[ae6d92e]878
[ea8d9a1]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
[e67cb60]883         /* We arrive at p2 via a leg, so that's one down right away. */
884         --p2->order;
885
[ea8d9a1]886         l->fDone = 1;
887         /* l->broken doesn't have break_flag set as we checked that above. */
[e67cb60]888         do_stn(p2, X2, l->prefix, dir, l->broken, dx, dy);
[ea8d9a1]889         l = lp;
[ae6d92e]890         if (--order == 0) return;
[d1b1380]891      }
892   }
893}
Note: See TracBrowser for help on using the repository browser.