2 * Produce an extended elevation
3 * Copyright (C) 1995-2002,2005,2010,2011,2013,2014,2016,2017,2024 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
33 #include "img_hosted.h"
38 /* To save memory we should probably use the prefix hash for the prefix on
39 * point labels (FIXME) */
44 const struct stn
*next
;
47 typedef struct splay
{
52 typedef struct POINT
{
74 /* Values for leg.broken: */
78 /* Values for point.dir and leg.dir: */
83 static point headpoint
= {{0, 0, 0}, 0, NULL
, 0, 0, 0, 0, NULL
, NULL
};
85 static leg headleg
= {NULL
, NULL
, NULL
, 0, 0, 0, 0, NULL
};
89 static int show_breaks
= 0;
91 static void do_stn(point
*, double, const char *, int, int, double, double);
100 #define HTAB_SIZE 0x2000
103 find_prefix(const char *prefix
)
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
;
116 p
->label
= osstrdup(prefix
);
117 p
->next
= htab
[hash
];
124 find_point(const img_point
*pt
)
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
) {
133 p
= osmalloc(ossizeof(point
));
142 p
->next
= headpoint
.next
;
148 add_leg(point
*fr
, point
*to
, const char *prefix
, int flags
)
153 l
= osmalloc(ossizeof(leg
));
157 l
->prefix
= find_prefix(prefix
);
160 l
->next
= headleg
.next
;
169 add_label(point
*p
, const char *label
, int flags
)
172 s
->label
= osstrdup(label
);
178 /* Read in config file */
181 /* lifted from img.c Should be put somewhere common? JPNP*/
183 getline_alloc(FILE *fh
, size_t ilen
)
188 char *buf
= xosmalloc(len
);
189 if (!buf
) return NULL
;
192 while (ch
!= '\n' && ch
!= '\r' && ch
!= EOF
) {
197 p
= xosrealloc(buf
, len
);
206 if (ch
== '\n' || ch
== '\r') {
207 int otherone
= ch
^ ('\n' ^ '\r');
209 /* if it's not the other eol character, put it back */
210 if (ch
!= otherone
) ungetc(ch
, fh
);
216 static int lineno
= 0;
217 static point
*start
= NULL
;
220 delimword(char *ln
, char** lr
)
224 while (*ln
== ' ' || *ln
== '\t' || *ln
== '\n' || *ln
== '\r')
228 while (*le
!= ' ' && *le
!= '\t' && *le
!= '\n' && *le
!= '\r' && *le
!= ';' && *le
!= '\0')
231 if (*le
== '\0' || *le
== ';') {
242 parseconfigline(const char *fnm
, char *ln
)
250 ln
= delimword(ln
, &lc
);
252 if (*ln
== '\0') return;
254 if (strcmp(ln
, "*start")==0) {
255 ln
= delimword(lc
, &lc
);
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) {
263 /* TRANSLATORS: for extend: "extend" is starting to produce an extended elevation from station %s */
264 printf(msg(/*Starting from station %s*/512),ln
);
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
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
);
277 fatalerror_in_file(fnm
, lineno
, /*Expecting station name*/28);
278 ln
= delimword(lc
, &lc
);
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
);
292 warning_in_file(fnm
, lineno
, /*Failed to find station %s*/510, ll
);
294 /* Two arguments - look for a specified leg. */
295 for (l
= headleg
.next
; l
; l
=l
->next
) {
299 for (s
=fr
->stns
; s
; s
=s
->next
) {
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
);
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
319 warning_in_file(fnm
, lineno
, /*Failed to find leg %s → %s*/511, ll
, ln
);
321 } else if (strcmp(ln
, "*eright")==0) {
322 char *ll
= delimword(lc
, &lc
);
324 fatalerror_in_file(fnm
, lineno
, /*Expecting station name*/28);
325 ln
= delimword(lc
, &lc
);
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
);
339 warning_in_file(fnm
, lineno
, /*Failed to find station %s*/510, ll
);
341 /* Two arguments - look for a specified leg. */
342 for (l
= headleg
.next
; l
; l
=l
->next
) {
346 for (s
=fr
->stns
; s
; s
=s
->next
) {
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
);
363 warning_in_file(fnm
, lineno
, /*Failed to find leg %s → %s*/511, ll
, ln
);
365 } else if (strcmp(ln
, "*eswap")==0) {
366 char *ll
= delimword(lc
, &lc
);
368 fatalerror_in_file(fnm
, lineno
, /*Expecting station name*/28);
369 ln
= delimword(lc
, &lc
);
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
);
383 warning_in_file(fnm
, lineno
, /*Failed to find station %s*/510, ll
);
385 /* Two arguments - look for a specified leg. */
386 for (l
= headleg
.next
; l
; l
=l
->next
) {
390 for (s
=fr
->stns
; s
; s
=s
->next
) {
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
);
407 warning_in_file(fnm
, lineno
, /*Failed to find leg %s → %s*/511, ll
, ln
);
409 } else if (strcmp(ln
, "*break")==0) {
410 char *ll
= delimword(lc
, &lc
);
412 fatalerror_in_file(fnm
, lineno
, /*Expecting station name*/28);
413 ln
= delimword(lc
, &lc
);
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
);
427 warning_in_file(fnm
, lineno
, /*Failed to find station %s*/510, ll
);
429 /* Two arguments - look for specified leg and disconnect it at the
431 for (l
= headleg
.next
; l
; l
=l
->next
) {
435 for (s
=fr
->stns
; s
; s
=s
->next
) {
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
);
444 l
->broken
= (b
? BREAK_TO
: BREAK_FR
);
452 warning_in_file(fnm
, lineno
, /*Failed to find leg %s → %s*/511, ll
, ln
);
455 fatalerror_in_file(fnm
, lineno
, /*Unknown command “%s”*/12, ln
);
458 ln
= delimword(lc
, &lc
);
460 fatalerror_in_file(fnm
, lineno
, /*End of line not blank*/15);
461 /* FIXME: give ln as context? */
465 static 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
},
475 #define short_opts "s:p:b"
477 static struct help_msg help
[] = {
479 {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0, 0},
480 /* TRANSLATORS: --help output for extend --specfile option */
481 {HLP_ENCODELONG(1), /*.espec file to control extending*/90, 0, "ESPEC_FILE"},
482 /* TRANSLATORS: --help output for extend --show-breaks option */
483 {HLP_ENCODELONG(2), /*show breaks with surface survey legs in output*/91, 0, 0},
491 double zMax
= -DBL_MAX
;
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
) {
498 for (s
= p
->stns
; s
; s
= s
->next
) {
499 if (s
->flags
& img_SFLAG_ENTRANCE
) {
506 if (best
) return best
;
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
) {
515 if (best
) return best
;
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
) {
524 if (best
) return best
;
526 /* There are no legs - just pick the highest station... */
527 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
537 main(int argc
, char **argv
)
539 const char *fnm_in
, *fnm_out
;
543 point
*fr
= NULL
, *to
;
544 const char *survey
= NULL
;
545 const char *specfile
= NULL
;
547 int xsections
= 0, splays
= 0;
551 /* TRANSLATORS: Part of extend --help */
552 cmdline_set_syntax_message(/*INPUT_FILE [OUTPUT_3D_FILE]*/267, 0, NULL
);
553 cmdline_init(argc
, argv
, short_opts
, long_opts
, NULL
, help
, 1, 2);
555 int opt
= cmdline_getopt();
556 if (opt
== EOF
) break;
569 fnm_in
= argv
[optind
++];
571 fnm_out
= argv
[optind
];
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
);
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
);
587 puts(msg(/*Reading in data - please wait…*/105));
589 htab
= osmalloc(ossizeof(pfx
*) * HTAB_SIZE
);
592 for (i
= 0; i
< HTAB_SIZE
; ++i
) htab
[i
] = NULL
;
596 result
= img_read_item(pimg
, &pt
);
599 fr
= find_point(&pt
);
606 to
= find_point(&pt
);
607 if (!(pimg
->flags
& img_FLAG_SURFACE
)) {
608 if (pimg
->flags
& img_FLAG_SPLAY
) {
611 add_leg(fr
, to
, pimg
->label
, pimg
->flags
);
617 to
= find_point(&pt
);
618 add_label(to
, pimg
->label
, pimg
->flags
);
621 (void)img_close(pimg
);
622 fatalerror(img_error2msg(img_error()), fnm_in
);
629 } while (result
!= img_STOP
);
635 result
= img_read_item(pimg
, &pt
);
638 fr
= find_point(&pt
);
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
));
651 if (to
->order
== 0) {
653 sp
->next
= fr
->splays
;
656 printf("Splay without a dead end from %s to %s\n", fr
->stns
->label
, to
->stns
->label
);
659 } else if (to
->order
) {
661 sp
->next
= to
->splays
;
664 printf("Isolated splay from %s to %s\n", fr
->stns
->label
, to
->stns
->label
);
672 } while (splays
&& result
!= img_STOP
);
675 desc
= osstrdup(pimg
->title
);
680 /* TRANSLATORS: for extend: */
681 printf(msg(/*Applying specfile: “%s”*/521), specfile
);
683 fs
= fopenWithPthAndExt("", specfile
, NULL
, "r", &fnm_used
);
684 if (fs
== NULL
) fatalerror(/*Couldn’t open file “%s”*/24, specfile
);
686 char *lbuf
= getline_alloc(fs
, 32);
689 fatalerror_in_file(fnm_used
, lineno
, /*Error reading file*/18);
690 parseconfigline(fnm_used
, lbuf
);
697 /* *start wasn't specified in specfile. */
698 start
= pick_start_stn();
699 if (!start
) fatalerror(/*No survey data*/43);
702 /* TRANSLATORS: for extend:
703 * Used to tell the user that a file is being written - %s is the filename
705 printf(msg(/*Writing %s…*/522), fnm_out
);
707 pimg_out
= img_open_write(fnm_out
, desc
, img_FFLAG_EXTENDED
);
709 /* Only does single connected component currently. */
710 do_stn(start
, 0.0, NULL
, ERIGHT
, 0, 0.0, 0.0);
714 /* Read ahead on pimg before writing pimg_out so we find out if an
715 * img_XSECT_END comes next. */
719 result
= img_read_item(pimg
, &pt
);
720 if (result
!= img_XSECT
&& result
!= img_XSECT_END
)
724 if (result
== img_XSECT_END
)
725 flags
|= img_XFLAG_END
;
726 img_write_item(pimg_out
, img_XSECT
, flags
, label
, 0, 0, 0);
730 if (result
== img_XSECT
) {
731 label
= osstrdup(pimg
->label
);
733 pimg_out
->l
= pimg
->l
;
734 pimg_out
->r
= pimg
->r
;
735 pimg_out
->u
= pimg
->u
;
736 pimg_out
->d
= pimg
->d
;
738 } while (xsections
&& result
!= img_STOP
);
741 (void)img_close(pimg
);
743 if (!img_close(pimg_out
)) {
744 (void)remove(fnm_out
);
745 fatalerror(img_error2msg(img_error()), fnm_out
);
751 static int adjust_direction(int dir
, int by
) {
753 return dir
^ (ELEFT
|ERIGHT
);
760 do_splays(point
*p
, double X
, int dir
, double tdx
, double tdy
)
766 if (!p
->splays
) return;
768 if (tdx
== 0 && tdy
== 0) {
769 /* Two adjacent plumbs, or a pair of legs that exactly cancel. */
773 /* Bearing in radians. */
782 for (sp
= p
->splays
; sp
; sp
= sp
->next
) {
785 img_write_item(pimg_out
, img_MOVE
, 0, NULL
, x
, 0, z
);
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
;
791 double tmp
= dx
* C
+ dy
* S
;
792 dy
= dy
* C
- dx
* S
;
795 img_write_item(pimg_out
, img_LINE
, img_FLAG_SPLAY
, NULL
, x
+ dx
, dy
, z
+ dz
);
801 do_stn(point
*p
, double X
, const char *prefix
, int dir
, int labOnly
,
802 double odx
, double ody
)
809 int order
= p
->order
;
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
);
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
);
821 if (labOnly
|| p
->fBroken
) {
827 /* We've reached a dead end. */
828 do_splays(p
, X
, dir
, odx
, ody
);
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.
835 for (try_all
= 0; try_all
!= 2; ++try_all
) {
837 for (l
= lp
->next
; l
; lp
= l
, l
= lp
->next
) {
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
845 if (!try_all
&& l
->prefix
!= prefix
) {
851 break_flag
= BREAK_TO
;
853 } else if (l
->fr
== p
) {
854 break_flag
= BREAK_FR
;
859 if (l
->broken
& break_flag
) continue;
861 /* adjust direction of extension if necessary */
862 dir
= adjust_direction(dir
, p
->dir
);
863 dir
= adjust_direction(dir
, l
->dir
);
865 double dx
= p2
->p
.x
- p
->p
.x
;
866 double dy
= p2
->p
.y
- p
->p
.y
;
876 do_splays(p
, X
, dir
, odx
+ dx
, ody
+ dy
);
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
,
883 /* We arrive at p2 via a leg, so that's one down right away. */
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
);
890 if (--order
== 0) return;