2 * Produce an extended elevation
3 * Copyright (C) 1995-2002,2005,2010,2011,2013,2014,2016 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 POINT
{
69 /* Values for leg.broken: */
73 /* Values for point.dir and leg.dir: */
78 static point headpoint
= {{0, 0, 0}, 0, NULL
, 0, 0, 0, 0, NULL
};
80 static leg headleg
= {NULL
, NULL
, NULL
, 0, 0, 0, 0, NULL
};
84 static int show_breaks
= 0;
86 static void do_stn(point
*, double, const char *, int, int);
95 #define HTAB_SIZE 0x2000
98 find_prefix(const char *prefix
)
105 hash
= hash_string(prefix
) & (HTAB_SIZE
- 1);
106 for (p
= htab
[hash
]; p
; p
= p
->next
) {
107 if (strcmp(prefix
, p
->label
) == 0) return p
->label
;
111 p
->label
= osstrdup(prefix
);
112 p
->next
= htab
[hash
];
119 find_point(const img_point
*pt
)
122 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
123 if (pt
->x
== p
->p
.x
&& pt
->y
== p
->p
.y
&& pt
->z
== p
->p
.z
) {
128 p
= osmalloc(ossizeof(point
));
136 p
->next
= headpoint
.next
;
142 add_leg(point
*fr
, point
*to
, const char *prefix
, int flags
)
147 l
= osmalloc(ossizeof(leg
));
151 l
->prefix
= find_prefix(prefix
);
154 l
->next
= headleg
.next
;
163 add_label(point
*p
, const char *label
, int flags
)
166 s
->label
= osstrdup(label
);
172 /* Read in config file */
175 /* lifted from img.c Should be put somewhere common? JPNP*/
177 getline_alloc(FILE *fh
, size_t ilen
)
182 char *buf
= xosmalloc(len
);
183 if (!buf
) return NULL
;
186 while (ch
!= '\n' && ch
!= '\r' && ch
!= EOF
) {
191 p
= xosrealloc(buf
, len
);
200 if (ch
== '\n' || ch
== '\r') {
201 int otherone
= ch
^ ('\n' ^ '\r');
203 /* if it's not the other eol character, put it back */
204 if (ch
!= otherone
) ungetc(ch
, fh
);
210 static int lineno
= 0;
211 static point
*start
= NULL
;
214 delimword(char *ln
, char** lr
)
218 while (*ln
== ' ' || *ln
== '\t' || *ln
== '\n' || *ln
== '\r')
222 while (*le
!= ' ' && *le
!= '\t' && *le
!= '\n' && *le
!= '\r' && *le
!= ';' && *le
!= '\0')
225 if (*le
== '\0' || *le
== ';') {
236 parseconfigline(const char *fnm
, char *ln
)
244 ln
= delimword(ln
, &lc
);
246 if (*ln
== '\0') return;
248 if (strcmp(ln
, "*start")==0) {
249 ln
= delimword(lc
, &lc
);
251 /* TRANSLATORS: Here "station" is a survey station, not a train station. */
252 fatalerror_in_file(fnm
, lineno
, /*Expecting station name*/28);
253 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
254 for (s
= p
->stns
; s
; s
= s
->next
) {
255 if (strcmp(s
->label
, ln
)==0) {
257 /* TRANSLATORS: for extend: "extend" is starting to produce an extended elevation from station %s */
258 printf(msg(/*Starting from station %s*/512),ln
);
264 /* TRANSLATORS: for extend: the user specified breaking a loop or
265 * changing extend direction at this station, but we didn’t find it in
267 warning_in_file(fnm
, lineno
, /*Failed to find station %s*/510, ln
);
268 } else if (strcmp(ln
, "*eleft")==0) {
269 char *ll
= delimword(lc
, &lc
);
271 fatalerror_in_file(fnm
, lineno
, /*Expecting station name*/28);
272 ln
= delimword(lc
, &lc
);
274 /* One argument - look for station to switch at. */
275 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
276 for (s
= p
->stns
; s
; s
= s
->next
) {
277 if (strcmp(s
->label
, ll
)==0) {
278 /* TRANSLATORS: for extend: */
279 printf(msg(/*Extending to the left from station %s*/513), ll
);
286 warning_in_file(fnm
, lineno
, /*Failed to find station %s*/510, ll
);
288 /* Two arguments - look for a specified leg. */
289 for (l
= headleg
.next
; l
; l
=l
->next
) {
293 for (s
=fr
->stns
; s
; s
=s
->next
) {
295 if (strcmp(s
->label
,ll
)==0 || (strcmp(s
->label
, ln
)==0 && (b
= 1)) ) {
296 char * lr
= (b
? ll
: ln
);
297 for (t
=to
->stns
; t
; t
=t
->next
) {
298 if (strcmp(t
->label
,lr
)==0) {
299 /* TRANSLATORS: for extend: */
300 printf(msg(/*Extending to the left from leg %s → %s*/515), s
->label
, t
->label
);
310 /* TRANSLATORS: for extend: the user specified breaking a loop or
311 * changing extend direction at this leg, but we didn’t find it in the
313 warning_in_file(fnm
, lineno
, /*Failed to find leg %s → %s*/511, ll
, ln
);
315 } else if (strcmp(ln
, "*eright")==0) {
316 char *ll
= delimword(lc
, &lc
);
318 fatalerror_in_file(fnm
, lineno
, /*Expecting station name*/28);
319 ln
= delimword(lc
, &lc
);
321 /* One argument - look for station to switch at. */
322 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
323 for (s
= p
->stns
; s
; s
= s
->next
) {
324 if (strcmp(s
->label
, ll
)==0) {
325 /* TRANSLATORS: for extend: */
326 printf(msg(/*Extending to the right from station %s*/514), ll
);
333 warning_in_file(fnm
, lineno
, /*Failed to find station %s*/510, ll
);
335 /* Two arguments - look for a specified leg. */
336 for (l
= headleg
.next
; l
; l
=l
->next
) {
340 for (s
=fr
->stns
; s
; s
=s
->next
) {
342 if (strcmp(s
->label
,ll
)==0 || (strcmp(s
->label
, ln
)==0 && (b
= 1)) ) {
343 char * lr
= (b
? ll
: ln
);
344 for (t
=to
->stns
; t
; t
=t
->next
) {
345 if (strcmp(t
->label
,lr
)==0) {
346 /* TRANSLATORS: for extend: */
347 printf(msg(/*Extending to the right from leg %s → %s*/516), s
->label
, t
->label
);
357 warning_in_file(fnm
, lineno
, /*Failed to find leg %s → %s*/511, ll
, ln
);
359 } else if (strcmp(ln
, "*eswap")==0) {
360 char *ll
= delimword(lc
, &lc
);
362 fatalerror_in_file(fnm
, lineno
, /*Expecting station name*/28);
363 ln
= delimword(lc
, &lc
);
365 /* One argument - look for station to switch at. */
366 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
367 for (s
= p
->stns
; s
; s
= s
->next
) {
368 if (strcmp(s
->label
, ll
)==0) {
369 /* TRANSLATORS: for extend: */
370 printf(msg(/*Swapping extend direction from station %s*/519),ll
);
377 warning_in_file(fnm
, lineno
, /*Failed to find station %s*/510, ll
);
379 /* Two arguments - look for a specified leg. */
380 for (l
= headleg
.next
; l
; l
=l
->next
) {
384 for (s
=fr
->stns
; s
; s
=s
->next
) {
386 if (strcmp(s
->label
,ll
)==0 || (strcmp(s
->label
, ln
)==0 && (b
= 1)) ) {
387 char * lr
= (b
? ll
: ln
);
388 for (t
=to
->stns
; t
; t
=t
->next
) {
389 if (strcmp(t
->label
,lr
)==0) {
390 /* TRANSLATORS: for extend: */
391 printf(msg(/*Swapping extend direction from leg %s → %s*/520), s
->label
, t
->label
);
401 warning_in_file(fnm
, lineno
, /*Failed to find leg %s → %s*/511, ll
, ln
);
403 } else if (strcmp(ln
, "*break")==0) {
404 char *ll
= delimword(lc
, &lc
);
406 fatalerror_in_file(fnm
, lineno
, /*Expecting station name*/28);
407 ln
= delimword(lc
, &lc
);
409 /* One argument - look for specified station to break at. */
410 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
411 for (s
= p
->stns
; s
; s
= s
->next
) {
412 if (strcmp(s
->label
, ll
)==0) {
413 /* TRANSLATORS: for extend: */
414 printf(msg(/*Breaking survey loop at station %s*/517), ll
);
421 warning_in_file(fnm
, lineno
, /*Failed to find station %s*/510, ll
);
423 /* Two arguments - look for specified leg and disconnect it at the
425 for (l
= headleg
.next
; l
; l
=l
->next
) {
429 for (s
=fr
->stns
; s
; s
=s
->next
) {
431 if (strcmp(s
->label
,ll
)==0 || (strcmp(s
->label
, ln
)==0 && (b
= 1)) ) {
432 char * lr
= (b
? ll
: ln
);
433 for (t
=to
->stns
; t
; t
=t
->next
) {
434 if (strcmp(t
->label
,lr
)==0) {
435 /* TRANSLATORS: for extend: */
436 printf(msg(/*Breaking survey loop at leg %s → %s*/518), s
->label
, t
->label
);
438 l
->broken
= (b
? BREAK_TO
: BREAK_FR
);
446 warning_in_file(fnm
, lineno
, /*Failed to find leg %s → %s*/511, ll
, ln
);
449 fatalerror_in_file(fnm
, lineno
, /*Unknown command “%s”*/12, ln
);
452 ln
= delimword(lc
, &lc
);
454 fatalerror_in_file(fnm
, lineno
, /*End of line not blank*/15);
455 /* FIXME: give ln as context? */
459 static const struct option long_opts
[] = {
460 /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
461 {"survey", required_argument
, 0, 's'},
462 {"specfile", required_argument
, 0, 'p'},
463 {"show-breaks", no_argument
, 0, 'b' },
464 {"help", no_argument
, 0, HLP_HELP
},
465 {"version", no_argument
, 0, HLP_VERSION
},
469 #define short_opts "s:p:b"
471 static struct help_msg help
[] = {
473 {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0},
474 /* TRANSLATORS: --help output for extend --specfile option */
475 {HLP_ENCODELONG(1), /*.espec file to control extending*/90, 0},
476 /* TRANSLATORS: --help output for extend --show-breaks option */
477 {HLP_ENCODELONG(2), /*show breaks with surface survey legs in output*/91, 0},
485 double zMax
= -DBL_MAX
;
488 /* Start at the highest entrance with some legs attached. */
489 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
490 if (p
->order
> 0 && p
->p
.z
> zMax
) {
492 for (s
= p
->stns
; s
; s
= s
->next
) {
493 if (s
->flags
& img_SFLAG_ENTRANCE
) {
500 if (best
) return best
;
502 /* If no entrances with legs, start at the highest 1-node. */
503 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
504 if (p
->order
== 1 && p
->p
.z
> zMax
) {
509 if (best
) return best
;
511 /* of course we may have no 1-nodes... */
512 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
513 if (p
->order
!= 0 && p
->p
.z
> zMax
) {
518 if (best
) return best
;
520 /* There are no legs - just pick the highest station... */
521 for (p
= headpoint
.next
; p
!= NULL
; p
= p
->next
) {
531 main(int argc
, char **argv
)
533 const char *fnm_in
, *fnm_out
;
537 point
*fr
= NULL
, *to
;
538 const char *survey
= NULL
;
539 const char *specfile
= NULL
;
545 /* TRANSLATORS: Part of extend --help */
546 cmdline_set_syntax_message(/*INPUT_3D_FILE [OUTPUT_3D_FILE]*/267, 0, NULL
);
547 cmdline_init(argc
, argv
, short_opts
, long_opts
, NULL
, help
, 1, 2);
549 int opt
= cmdline_getopt();
550 if (opt
== EOF
) break;
563 fnm_in
= argv
[optind
++];
565 fnm_out
= argv
[optind
];
567 char * base_in
= base_from_fnm(fnm_in
);
568 char * base_out
= osmalloc(strlen(base_in
) + 8);
569 strcpy(base_out
, base_in
);
570 strcat(base_out
, "_extend");
571 fnm_out
= add_ext(base_out
, EXT_SVX_3D
);
576 /* try to open image file, and check it has correct header */
577 pimg
= img_open_survey(fnm_in
, survey
);
578 if (pimg
== NULL
) fatalerror(img_error2msg(img_error()), fnm_in
);
581 puts(msg(/*Reading in data - please wait…*/105));
583 htab
= osmalloc(ossizeof(pfx
*) * HTAB_SIZE
);
586 for (i
= 0; i
< HTAB_SIZE
; ++i
) htab
[i
] = NULL
;
590 result
= img_read_item(pimg
, &pt
);
593 fr
= find_point(&pt
);
600 to
= find_point(&pt
);
601 if (!(pimg
->flags
& (img_FLAG_SURFACE
|img_FLAG_SPLAY
)))
602 add_leg(fr
, to
, pimg
->label
, pimg
->flags
);
606 to
= find_point(&pt
);
607 add_label(to
, pimg
->label
, pimg
->flags
);
610 (void)img_close(pimg
);
611 fatalerror(img_error2msg(img_error()), fnm_in
);
617 } while (result
!= img_STOP
);
619 desc
= osstrdup(pimg
->title
);
624 /* TRANSLATORS: for extend: */
625 printf(msg(/*Applying specfile: “%s”*/521), specfile
);
627 fs
= fopenWithPthAndExt("", specfile
, NULL
, "r", &fnm_used
);
628 if (fs
== NULL
) fatalerror(/*Couldn’t open file “%s”*/24, specfile
);
630 char *lbuf
= getline_alloc(fs
, 32);
633 fatalerror_in_file(fnm_used
, lineno
, /*Error reading file*/18);
634 parseconfigline(fnm_used
, lbuf
);
641 /* *start wasn't specified in specfile. */
642 start
= pick_start_stn();
643 if (!start
) fatalerror(/*No survey data*/43);
646 /* TRANSLATORS: for extend:
647 * Used to tell the user that a file is being written - %s is the filename
649 printf(msg(/*Writing %s…*/522), fnm_out
);
651 pimg_out
= img_open_write(fnm_out
, desc
, img_FFLAG_EXTENDED
);
653 /* Only does single connected component currently. */
654 do_stn(start
, 0.0, NULL
, ERIGHT
, 0);
658 /* Read ahead on pimg before writing pimg_out so we find out if an
659 * img_XSECT_END comes next. */
663 result
= img_read_item(pimg
, &pt
);
664 if (result
== img_XSECT
|| result
== img_XSECT_END
) {
666 if (result
== img_XSECT_END
)
667 flags
|= img_XFLAG_END
;
668 img_write_item(pimg_out
, img_XSECT
, flags
, label
, 0, 0, 0);
673 if (result
== img_XSECT
) {
674 label
= osstrdup(pimg
->label
);
676 pimg_out
->l
= pimg
->l
;
677 pimg_out
->r
= pimg
->r
;
678 pimg_out
->u
= pimg
->u
;
679 pimg_out
->d
= pimg
->d
;
681 } while (result
!= img_STOP
);
684 (void)img_close(pimg
);
686 if (!img_close(pimg_out
)) {
687 (void)remove(fnm_out
);
688 fatalerror(img_error2msg(img_error()), fnm_out
);
694 static int adjust_direction(int dir
, int by
) {
696 return dir
^ (ELEFT
|ERIGHT
);
703 do_stn(point
*p
, double X
, const char *prefix
, int dir
, int labOnly
)
711 for (s
= p
->stns
; s
; s
= s
->next
) {
712 img_write_item(pimg_out
, img_LABEL
, s
->flags
, s
->label
, X
, 0, p
->p
.z
);
714 if (show_breaks
&& p
->X
!= HUGE_VAL
&& p
->X
!= X
) {
715 /* Draw "surface" leg between broken stations. */
716 img_write_item(pimg_out
, img_MOVE
, 0, NULL
, p
->X
, 0, p
->p
.z
);
717 img_write_item(pimg_out
, img_LINE
, img_FLAG_SURFACE
, NULL
, X
, 0, p
->p
.z
);
720 if (labOnly
|| p
->fBroken
) {
724 /* It's better to follow legs along a survey, so make two passes and only
725 * follow legs in the same survey for the first pass.
727 for (try_all
= 0; try_all
!= 2; ++try_all
) {
729 for (l
= lp
->next
; l
; lp
= l
, l
= lp
->next
) {
732 /* this case happens iff a recursive call causes the next leg to be
733 * removed, leaving our next pointing to a leg which has been dealt
737 if (!try_all
&& l
->prefix
!= prefix
) {
741 if (l
->broken
& BREAK_TO
) continue;
743 /* adjust direction of extension if necessary */
744 dir
= adjust_direction(dir
, l
->to
->dir
);
745 dir
= adjust_direction(dir
, l
->dir
);
747 dX
= hypot(l
->fr
->p
.x
- l
->to
->p
.x
, l
->fr
->p
.y
- l
->to
->p
.y
);
748 if (dir
== ELEFT
) dX
= -dX
;
749 img_write_item(pimg_out
, img_MOVE
, 0, NULL
, X
+ dX
, 0, l
->fr
->p
.z
);
750 img_write_item(pimg_out
, img_LINE
, l
->flags
, l
->prefix
,
753 do_stn(l
->fr
, X
+ dX
, l
->prefix
, dir
, (l
->broken
& BREAK_FR
));
755 } else if (l
->fr
== p
) {
756 if (l
->broken
& BREAK_FR
) continue;
758 /* adjust direction of extension if necessary */
759 dir
= adjust_direction(dir
, l
->fr
->dir
);
760 dir
= adjust_direction(dir
, l
->dir
);
762 dX
= hypot(l
->fr
->p
.x
- l
->to
->p
.x
, l
->fr
->p
.y
- l
->to
->p
.y
);
763 if (dir
== ELEFT
) dX
= -dX
;
764 img_write_item(pimg_out
, img_MOVE
, 0, NULL
, X
, 0, l
->fr
->p
.z
);
765 img_write_item(pimg_out
, img_LINE
, l
->flags
, l
->prefix
,
766 X
+ dX
, 0, l
->to
->p
.z
);
768 do_stn(l
->to
, X
+ dX
, l
->prefix
, dir
, (l
->broken
& BREAK_TO
));