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
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.
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.
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
35 #include "img_hosted.h"
39 /* To save memory we should probably use the prefix hash for the prefix on
40 * point labels (FIXME) */
45 const struct stn
*next
;
48 typedef struct splay
{
53 typedef struct POINT
{
75 /* Values for leg.broken: */
79 /* Values for point.dir and leg.dir: */
84 static point headpoint
= {{0, 0, 0}, 0, NULL
, 0, 0, 0, 0, NULL
, NULL
};
86 static leg headleg
= {NULL
, NULL
, NULL
, 0, 0, 0, 0, NULL
};
90 static int show_breaks
= 0;
92 static void do_stn(point
*, double, const char *, int, int, double, double);
101 #define HTAB_SIZE 0x2000
104 find_prefix(const char *prefix
)
111 hash
= hash_string(prefix
) & (HTAB_SIZE
- 1);
112 for (p
= htab
[hash
]; p
; p
= p
->next
) {
113 if (strcmp(prefix
, p
->label
) == 0) return p
->label
;
117 p
->label
= osstrdup(prefix
);
118 p
->next
= htab
[hash
];
125 find_point(const img_point
*pt
)
128 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
129 if (pt
->x
== p
->p
.x
&& pt
->y
== p
->p
.y
&& pt
->z
== p
->p
.z
) {
134 p
= osmalloc(ossizeof(point
));
143 p
->next
= headpoint
.next
;
149 add_leg(point
*fr
, point
*to
, const char *prefix
, int flags
)
154 l
= osmalloc(ossizeof(leg
));
158 l
->prefix
= find_prefix(prefix
);
161 l
->next
= headleg
.next
;
170 add_label(point
*p
, const char *label
, int flags
)
173 s
->label
= osstrdup(label
);
179 /* Read in config file */
182 /* lifted from img.c Should be put somewhere common? JPNP*/
184 getline_alloc(FILE *fh
, size_t ilen
)
189 char *buf
= xosmalloc(len
);
190 if (!buf
) return NULL
;
193 while (ch
!= '\n' && ch
!= '\r' && ch
!= EOF
) {
198 p
= xosrealloc(buf
, len
);
207 if (ch
== '\n' || ch
== '\r') {
208 int otherone
= ch
^ ('\n' ^ '\r');
210 /* if it's not the other eol character, put it back */
211 if (ch
!= otherone
) ungetc(ch
, fh
);
217 static int lineno
= 0;
218 static point
*start
= NULL
;
221 delimword(char *ln
, char** lr
)
225 while (*ln
== ' ' || *ln
== '\t' || *ln
== '\n' || *ln
== '\r')
229 while (*le
!= ' ' && *le
!= '\t' && *le
!= '\n' && *le
!= '\r' && *le
!= ';' && *le
!= '\0')
232 if (*le
== '\0' || *le
== ';') {
243 parseconfigline(const char *fnm
, char *ln
)
251 ln
= delimword(ln
, &lc
);
253 if (*ln
== '\0') return;
255 if (strcmp(ln
, "*start")==0) {
256 ln
= delimword(lc
, &lc
);
258 /* TRANSLATORS: Here "station" is a survey station, not a train station. */
259 fatalerror_in_file(fnm
, lineno
, /*Expecting station name*/28);
260 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
261 for (s
= p
->stns
; s
; s
= s
->next
) {
262 if (strcmp(s
->label
, ln
)==0) {
264 /* TRANSLATORS: for extend: "extend" is starting to produce an extended elevation from station %s */
265 printf(msg(/*Starting from station %s*/512),ln
);
271 /* TRANSLATORS: for extend: the user specified breaking a loop or
272 * changing extend direction at this station, but we didn’t find it in
274 warning_in_file(fnm
, lineno
, /*Failed to find station %s*/510, ln
);
275 } else if (strcmp(ln
, "*eleft")==0) {
276 char *ll
= delimword(lc
, &lc
);
278 fatalerror_in_file(fnm
, lineno
, /*Expecting station name*/28);
279 ln
= delimword(lc
, &lc
);
281 /* One argument - look for station to switch at. */
282 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
283 for (s
= p
->stns
; s
; s
= s
->next
) {
284 if (strcmp(s
->label
, ll
)==0) {
285 /* TRANSLATORS: for extend: */
286 printf(msg(/*Extending to the left from station %s*/513), ll
);
293 warning_in_file(fnm
, lineno
, /*Failed to find station %s*/510, ll
);
295 /* Two arguments - look for a specified leg. */
296 for (l
= headleg
.next
; l
; l
=l
->next
) {
300 for (s
=fr
->stns
; s
; s
=s
->next
) {
302 if (strcmp(s
->label
,ll
)==0 || (strcmp(s
->label
, ln
)==0 && (b
= 1)) ) {
303 char * lr
= (b
? ll
: ln
);
304 for (t
=to
->stns
; t
; t
=t
->next
) {
305 if (strcmp(t
->label
,lr
)==0) {
306 /* TRANSLATORS: for extend: */
307 printf(msg(/*Extending to the left from leg %s → %s*/515), s
->label
, t
->label
);
317 /* TRANSLATORS: for extend: the user specified breaking a loop or
318 * changing extend direction at this leg, but we didn’t find it in the
320 warning_in_file(fnm
, lineno
, /*Failed to find leg %s → %s*/511, ll
, ln
);
322 } else if (strcmp(ln
, "*eright")==0) {
323 char *ll
= delimword(lc
, &lc
);
325 fatalerror_in_file(fnm
, lineno
, /*Expecting station name*/28);
326 ln
= delimword(lc
, &lc
);
328 /* One argument - look for station to switch at. */
329 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
330 for (s
= p
->stns
; s
; s
= s
->next
) {
331 if (strcmp(s
->label
, ll
)==0) {
332 /* TRANSLATORS: for extend: */
333 printf(msg(/*Extending to the right from station %s*/514), ll
);
340 warning_in_file(fnm
, lineno
, /*Failed to find station %s*/510, ll
);
342 /* Two arguments - look for a specified leg. */
343 for (l
= headleg
.next
; l
; l
=l
->next
) {
347 for (s
=fr
->stns
; s
; s
=s
->next
) {
349 if (strcmp(s
->label
,ll
)==0 || (strcmp(s
->label
, ln
)==0 && (b
= 1)) ) {
350 char * lr
= (b
? ll
: ln
);
351 for (t
=to
->stns
; t
; t
=t
->next
) {
352 if (strcmp(t
->label
,lr
)==0) {
353 /* TRANSLATORS: for extend: */
354 printf(msg(/*Extending to the right from leg %s → %s*/516), s
->label
, t
->label
);
364 warning_in_file(fnm
, lineno
, /*Failed to find leg %s → %s*/511, ll
, ln
);
366 } else if (strcmp(ln
, "*eswap")==0) {
367 char *ll
= delimword(lc
, &lc
);
369 fatalerror_in_file(fnm
, lineno
, /*Expecting station name*/28);
370 ln
= delimword(lc
, &lc
);
372 /* One argument - look for station to switch at. */
373 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
374 for (s
= p
->stns
; s
; s
= s
->next
) {
375 if (strcmp(s
->label
, ll
)==0) {
376 /* TRANSLATORS: for extend: */
377 printf(msg(/*Swapping extend direction from station %s*/519),ll
);
384 warning_in_file(fnm
, lineno
, /*Failed to find station %s*/510, ll
);
386 /* Two arguments - look for a specified leg. */
387 for (l
= headleg
.next
; l
; l
=l
->next
) {
391 for (s
=fr
->stns
; s
; s
=s
->next
) {
393 if (strcmp(s
->label
,ll
)==0 || (strcmp(s
->label
, ln
)==0 && (b
= 1)) ) {
394 char * lr
= (b
? ll
: ln
);
395 for (t
=to
->stns
; t
; t
=t
->next
) {
396 if (strcmp(t
->label
,lr
)==0) {
397 /* TRANSLATORS: for extend: */
398 printf(msg(/*Swapping extend direction from leg %s → %s*/520), s
->label
, t
->label
);
408 warning_in_file(fnm
, lineno
, /*Failed to find leg %s → %s*/511, ll
, ln
);
410 } else if (strcmp(ln
, "*break")==0) {
411 char *ll
= delimword(lc
, &lc
);
413 fatalerror_in_file(fnm
, lineno
, /*Expecting station name*/28);
414 ln
= delimword(lc
, &lc
);
416 /* One argument - look for specified station to break at. */
417 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
418 for (s
= p
->stns
; s
; s
= s
->next
) {
419 if (strcmp(s
->label
, ll
)==0) {
420 /* TRANSLATORS: for extend: */
421 printf(msg(/*Breaking survey loop at station %s*/517), ll
);
428 warning_in_file(fnm
, lineno
, /*Failed to find station %s*/510, ll
);
430 /* Two arguments - look for specified leg and disconnect it at the
432 for (l
= headleg
.next
; l
; l
=l
->next
) {
436 for (s
=fr
->stns
; s
; s
=s
->next
) {
438 if (strcmp(s
->label
,ll
)==0 || (strcmp(s
->label
, ln
)==0 && (b
= 1)) ) {
439 char * lr
= (b
? ll
: ln
);
440 for (t
=to
->stns
; t
; t
=t
->next
) {
441 if (strcmp(t
->label
,lr
)==0) {
442 /* TRANSLATORS: for extend: */
443 printf(msg(/*Breaking survey loop at leg %s → %s*/518), s
->label
, t
->label
);
445 l
->broken
= (b
? BREAK_TO
: BREAK_FR
);
453 warning_in_file(fnm
, lineno
, /*Failed to find leg %s → %s*/511, ll
, ln
);
456 fatalerror_in_file(fnm
, lineno
, /*Unknown command “%s”*/12, ln
);
459 ln
= delimword(lc
, &lc
);
461 fatalerror_in_file(fnm
, lineno
, /*End of line not blank*/15);
462 /* FIXME: give ln as context? */
466 static const struct option long_opts
[] = {
467 /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
468 {"survey", required_argument
, 0, 's'},
469 {"specfile", required_argument
, 0, 'p'},
470 {"show-breaks", no_argument
, 0, 'b' },
471 {"help", no_argument
, 0, HLP_HELP
},
472 {"version", no_argument
, 0, HLP_VERSION
},
476 #define short_opts "s:p:b"
478 static struct help_msg help
[] = {
480 {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0},
481 /* TRANSLATORS: --help output for extend --specfile option */
482 {HLP_ENCODELONG(1), /*.espec file to control extending*/90, 0},
483 /* TRANSLATORS: --help output for extend --show-breaks option */
484 {HLP_ENCODELONG(2), /*show breaks with surface survey legs in output*/91, 0},
492 double zMax
= -DBL_MAX
;
495 /* Start at the highest entrance with some legs attached. */
496 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
497 if (p
->order
> 0 && p
->p
.z
> zMax
) {
499 for (s
= p
->stns
; s
; s
= s
->next
) {
500 if (s
->flags
& img_SFLAG_ENTRANCE
) {
507 if (best
) return best
;
509 /* If no entrances with legs, start at the highest 1-node. */
510 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
511 if (p
->order
== 1 && p
->p
.z
> zMax
) {
516 if (best
) return best
;
518 /* of course we may have no 1-nodes... */
519 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
520 if (p
->order
!= 0 && p
->p
.z
> zMax
) {
525 if (best
) return best
;
527 /* There are no legs - just pick the highest station... */
528 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
538 main(int argc
, char **argv
)
540 const char *fnm_in
, *fnm_out
;
544 point
*fr
= NULL
, *to
;
545 const char *survey
= NULL
;
546 const char *specfile
= NULL
;
548 int xsections
= 0, splays
= 0;
552 /* TRANSLATORS: Part of extend --help */
553 cmdline_set_syntax_message(/*INPUT_3D_FILE [OUTPUT_3D_FILE]*/267, 0, NULL
);
554 cmdline_init(argc
, argv
, short_opts
, long_opts
, NULL
, help
, 1, 2);
556 int opt
= cmdline_getopt();
557 if (opt
== EOF
) break;
570 fnm_in
= argv
[optind
++];
572 fnm_out
= argv
[optind
];
574 char * base_in
= base_from_fnm(fnm_in
);
575 char * base_out
= osmalloc(strlen(base_in
) + 8);
576 strcpy(base_out
, base_in
);
577 strcat(base_out
, "_extend");
578 fnm_out
= add_ext(base_out
, EXT_SVX_3D
);
583 /* try to open image file, and check it has correct header */
584 pimg
= img_open_survey(fnm_in
, survey
);
585 if (pimg
== NULL
) fatalerror(img_error2msg(img_error()), fnm_in
);
588 puts(msg(/*Reading in data - please wait…*/105));
590 htab
= osmalloc(ossizeof(pfx
*) * HTAB_SIZE
);
593 for (i
= 0; i
< HTAB_SIZE
; ++i
) htab
[i
] = NULL
;
597 result
= img_read_item(pimg
, &pt
);
600 fr
= find_point(&pt
);
607 to
= find_point(&pt
);
608 if (!(pimg
->flags
& img_FLAG_SURFACE
)) {
609 if (pimg
->flags
& img_FLAG_SPLAY
) {
612 add_leg(fr
, to
, pimg
->label
, pimg
->flags
);
618 to
= find_point(&pt
);
619 add_label(to
, pimg
->label
, pimg
->flags
);
622 (void)img_close(pimg
);
623 fatalerror(img_error2msg(img_error()), fnm_in
);
630 } while (result
!= img_STOP
);
636 result
= img_read_item(pimg
, &pt
);
639 fr
= find_point(&pt
);
646 to
= find_point(&pt
);
647 if (!(pimg
->flags
& img_FLAG_SURFACE
)) {
648 if (pimg
->flags
& img_FLAG_SPLAY
) {
649 splay
*sp
= osmalloc(ossizeof(splay
));
652 if (to
->order
== 0) {
654 sp
->next
= fr
->splays
;
657 printf("Splay without a dead end from %s to %s\n", fr
->stns
->label
, to
->stns
->label
);
660 } else if (to
->order
) {
662 sp
->next
= to
->splays
;
665 printf("Isolated splay from %s to %s\n", fr
->stns
->label
, to
->stns
->label
);
673 } while (splays
&& result
!= img_STOP
);
676 desc
= osstrdup(pimg
->title
);
681 /* TRANSLATORS: for extend: */
682 printf(msg(/*Applying specfile: “%s”*/521), specfile
);
684 fs
= fopenWithPthAndExt("", specfile
, NULL
, "r", &fnm_used
);
685 if (fs
== NULL
) fatalerror(/*Couldn’t open file “%s”*/24, specfile
);
687 char *lbuf
= getline_alloc(fs
, 32);
690 fatalerror_in_file(fnm_used
, lineno
, /*Error reading file*/18);
691 parseconfigline(fnm_used
, lbuf
);
698 /* *start wasn't specified in specfile. */
699 start
= pick_start_stn();
700 if (!start
) fatalerror(/*No survey data*/43);
703 /* TRANSLATORS: for extend:
704 * Used to tell the user that a file is being written - %s is the filename
706 printf(msg(/*Writing %s…*/522), fnm_out
);
708 pimg_out
= img_open_write(fnm_out
, desc
, img_FFLAG_EXTENDED
);
710 /* Only does single connected component currently. */
711 do_stn(start
, 0.0, NULL
, ERIGHT
, 0, 0.0, 0.0);
715 /* Read ahead on pimg before writing pimg_out so we find out if an
716 * img_XSECT_END comes next. */
720 result
= img_read_item(pimg
, &pt
);
721 if (result
!= img_XSECT
&& result
!= img_XSECT_END
)
725 if (result
== img_XSECT_END
)
726 flags
|= img_XFLAG_END
;
727 img_write_item(pimg_out
, img_XSECT
, flags
, label
, 0, 0, 0);
731 if (result
== img_XSECT
) {
732 label
= osstrdup(pimg
->label
);
734 pimg_out
->l
= pimg
->l
;
735 pimg_out
->r
= pimg
->r
;
736 pimg_out
->u
= pimg
->u
;
737 pimg_out
->d
= pimg
->d
;
739 } while (xsections
&& result
!= img_STOP
);
742 (void)img_close(pimg
);
744 if (!img_close(pimg_out
)) {
745 (void)remove(fnm_out
);
746 fatalerror(img_error2msg(img_error()), fnm_out
);
752 static int adjust_direction(int dir
, int by
) {
754 return dir
^ (ELEFT
|ERIGHT
);
761 do_splays(point
*p
, double X
, int dir
, double tdx
, double tdy
)
767 if (!p
->splays
) return;
769 if (tdx
== 0 && tdy
== 0) {
770 /* Two adjacent plumbs, or a pair of legs that exactly cancel. */
774 /* Bearing in radians. */
783 for (sp
= p
->splays
; sp
; sp
= sp
->next
) {
786 img_write_item(pimg_out
, img_MOVE
, 0, NULL
, x
, 0, z
);
788 double dx
= sp
->pt
->p
.x
- p
->p
.x
;
789 double dy
= sp
->pt
->p
.y
- p
->p
.y
;
790 double dz
= sp
->pt
->p
.z
- p
->p
.z
;
792 double tmp
= dx
* C
+ dy
* S
;
793 dy
= dy
* C
- dx
* S
;
796 img_write_item(pimg_out
, img_LINE
, img_FLAG_SPLAY
, NULL
, x
+ dx
, dy
, z
+ dz
);
802 do_stn(point
*p
, double X
, const char *prefix
, int dir
, int labOnly
,
803 double odx
, double ody
)
810 int order
= p
->order
;
812 for (s
= p
->stns
; s
; s
= s
->next
) {
813 img_write_item(pimg_out
, img_LABEL
, s
->flags
, s
->label
, X
, 0, p
->p
.z
);
816 if (show_breaks
&& p
->X
!= HUGE_VAL
&& p
->X
!= X
) {
817 /* Draw "surface" leg between broken stations. */
818 img_write_item(pimg_out
, img_MOVE
, 0, NULL
, p
->X
, 0, p
->p
.z
);
819 img_write_item(pimg_out
, img_LINE
, img_FLAG_SURFACE
, NULL
, X
, 0, p
->p
.z
);
822 if (labOnly
|| p
->fBroken
) {
828 /* We've reached a dead end. */
829 do_splays(p
, X
, dir
, odx
, ody
);
833 /* It's better to follow legs along a survey, so make two passes and only
834 * follow legs in the same survey for the first pass.
836 for (try_all
= 0; try_all
!= 2; ++try_all
) {
838 for (l
= lp
->next
; l
; lp
= l
, l
= lp
->next
) {
841 /* this case happens iff a recursive call causes the next leg to be
842 * removed, leaving our next pointing to a leg which has been dealt
846 if (!try_all
&& l
->prefix
!= prefix
) {
852 break_flag
= BREAK_TO
;
854 } else if (l
->fr
== p
) {
855 break_flag
= BREAK_FR
;
860 if (l
->broken
& break_flag
) continue;
862 /* adjust direction of extension if necessary */
863 dir
= adjust_direction(dir
, p
->dir
);
864 dir
= adjust_direction(dir
, l
->dir
);
866 double dx
= p2
->p
.x
- p
->p
.x
;
867 double dy
= p2
->p
.y
- p
->p
.y
;
877 do_splays(p
, X
, dir
, odx
+ dx
, ody
+ dy
);
880 img_write_item(pimg_out
, img_MOVE
, 0, NULL
, X
, 0, p
->p
.z
);
881 img_write_item(pimg_out
, img_LINE
, l
->flags
, l
->prefix
,
884 /* We arrive at p2 via a leg, so that's one down right away. */
888 /* l->broken doesn't have break_flag set as we checked that above. */
889 do_stn(p2
, X2
, l
->prefix
, dir
, l
->broken
, dx
, dy
);
891 if (--order
== 0) return;