1 | /* extend.c |
---|
2 | * Produce an extended elevation |
---|
3 | * Copyright (C) 1995-2002,2005,2010,2011 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 | #ifdef HAVE_CONFIG_H |
---|
22 | # include <config.h> |
---|
23 | #endif |
---|
24 | |
---|
25 | #include <float.h> |
---|
26 | #include <stdio.h> |
---|
27 | #include <stdlib.h> |
---|
28 | #include <string.h> |
---|
29 | |
---|
30 | #include "cmdline.h" |
---|
31 | #include "debug.h" |
---|
32 | #include "filelist.h" |
---|
33 | #include "filename.h" |
---|
34 | #include "hash.h" |
---|
35 | #include "img.h" |
---|
36 | #include "message.h" |
---|
37 | #include "useful.h" |
---|
38 | |
---|
39 | /* To save memory we should probably use the prefix hash for the prefix on |
---|
40 | * point labels (FIXME) */ |
---|
41 | |
---|
42 | typedef struct stn { |
---|
43 | const char *label; |
---|
44 | int flags; |
---|
45 | const struct stn *next; |
---|
46 | } stn; |
---|
47 | |
---|
48 | typedef struct POINT { |
---|
49 | img_point p; |
---|
50 | const stn *stns; |
---|
51 | unsigned int order; |
---|
52 | char dir; |
---|
53 | char fDone; |
---|
54 | char fBroken; |
---|
55 | struct POINT *next; |
---|
56 | } point; |
---|
57 | |
---|
58 | typedef struct LEG { |
---|
59 | point *fr, *to; |
---|
60 | const char *prefix; |
---|
61 | char dir; |
---|
62 | char fDone; |
---|
63 | char broken; |
---|
64 | int flags; |
---|
65 | struct LEG *next; |
---|
66 | } leg; |
---|
67 | |
---|
68 | /* Values for leg.broken: */ |
---|
69 | #define BREAK_FR 0x01 |
---|
70 | #define BREAK_TO 0x02 |
---|
71 | |
---|
72 | /* Values for point.dir and leg.dir: */ |
---|
73 | #define ELEFT 0x01 |
---|
74 | #define ERIGHT 0x02 |
---|
75 | #define ESWAP 0x04 |
---|
76 | |
---|
77 | static point headpoint = {{0, 0, 0}, NULL, 0, 0, 0, 0, NULL}; |
---|
78 | |
---|
79 | static leg headleg = {NULL, NULL, NULL, 0, 0, 0, 0, NULL}; |
---|
80 | |
---|
81 | static img *pimg_out; |
---|
82 | |
---|
83 | static void do_stn(point *, double, const char *, int, int); |
---|
84 | |
---|
85 | typedef struct pfx { |
---|
86 | const char *label; |
---|
87 | struct pfx *next; |
---|
88 | } pfx; |
---|
89 | |
---|
90 | static pfx **htab; |
---|
91 | |
---|
92 | #define HTAB_SIZE 0x2000 |
---|
93 | |
---|
94 | static const char * |
---|
95 | find_prefix(const char *prefix) |
---|
96 | { |
---|
97 | pfx *p; |
---|
98 | int hash; |
---|
99 | |
---|
100 | SVX_ASSERT(prefix); |
---|
101 | |
---|
102 | hash = hash_string(prefix) & (HTAB_SIZE - 1); |
---|
103 | for (p = htab[hash]; p; p = p->next) { |
---|
104 | if (strcmp(prefix, p->label) == 0) return p->label; |
---|
105 | } |
---|
106 | |
---|
107 | p = osnew(pfx); |
---|
108 | p->label = osstrdup(prefix); |
---|
109 | p->next = htab[hash]; |
---|
110 | htab[hash] = p; |
---|
111 | |
---|
112 | return p->label; |
---|
113 | } |
---|
114 | |
---|
115 | static point * |
---|
116 | find_point(const img_point *pt) |
---|
117 | { |
---|
118 | point *p; |
---|
119 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
120 | if (pt->x == p->p.x && pt->y == p->p.y && pt->z == p->p.z) { |
---|
121 | return p; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | p = osmalloc(ossizeof(point)); |
---|
126 | p->p = *pt; |
---|
127 | p->stns = NULL; |
---|
128 | p->order = 0; |
---|
129 | p->dir = 0; |
---|
130 | p->fDone = 0; |
---|
131 | p->fBroken = 0; |
---|
132 | p->next = headpoint.next; |
---|
133 | headpoint.next = p; |
---|
134 | return p; |
---|
135 | } |
---|
136 | |
---|
137 | static void |
---|
138 | add_leg(point *fr, point *to, const char *prefix, int flags) |
---|
139 | { |
---|
140 | leg *l; |
---|
141 | fr->order++; |
---|
142 | to->order++; |
---|
143 | l = osmalloc(ossizeof(leg)); |
---|
144 | l->fr = fr; |
---|
145 | l->to = to; |
---|
146 | if (prefix) |
---|
147 | l->prefix = find_prefix(prefix); |
---|
148 | else |
---|
149 | l->prefix = NULL; |
---|
150 | l->next = headleg.next; |
---|
151 | l->dir = 0; |
---|
152 | l->fDone = 0; |
---|
153 | l->broken = 0; |
---|
154 | l->flags = flags; |
---|
155 | headleg.next = l; |
---|
156 | } |
---|
157 | |
---|
158 | static void |
---|
159 | add_label(point *p, const char *label, int flags) |
---|
160 | { |
---|
161 | stn *s = osnew(stn); |
---|
162 | s->label = osstrdup(label); |
---|
163 | s->flags = flags; |
---|
164 | s->next = p->stns; |
---|
165 | p->stns = s; |
---|
166 | } |
---|
167 | |
---|
168 | /* Read in config file */ |
---|
169 | |
---|
170 | |
---|
171 | /* lifted from img.c Should be put somewhere common? JPNP*/ |
---|
172 | static char * |
---|
173 | getline_alloc(FILE *fh, size_t ilen) |
---|
174 | { |
---|
175 | int ch; |
---|
176 | size_t i = 0; |
---|
177 | size_t len = ilen; |
---|
178 | char *buf = xosmalloc(len); |
---|
179 | if (!buf) return NULL; |
---|
180 | |
---|
181 | ch = GETC(fh); |
---|
182 | while (ch != '\n' && ch != '\r' && ch != EOF) { |
---|
183 | buf[i++] = ch; |
---|
184 | if (i == len - 1) { |
---|
185 | char *p; |
---|
186 | len += len; |
---|
187 | p = xosrealloc(buf, len); |
---|
188 | if (!p) { |
---|
189 | osfree(buf); |
---|
190 | return NULL; |
---|
191 | } |
---|
192 | buf = p; |
---|
193 | } |
---|
194 | ch = GETC(fh); |
---|
195 | } |
---|
196 | if (ch == '\n' || ch == '\r') { |
---|
197 | int otherone = ch ^ ('\n' ^ '\r'); |
---|
198 | ch = GETC(fh); |
---|
199 | /* if it's not the other eol character, put it back */ |
---|
200 | if (ch != otherone) ungetc(ch, fh); |
---|
201 | } |
---|
202 | buf[i++] = '\0'; |
---|
203 | return buf; |
---|
204 | } |
---|
205 | |
---|
206 | static int lineno = 0; |
---|
207 | static point *start = NULL; |
---|
208 | |
---|
209 | static char* |
---|
210 | delimword(char *ln, char** lr) |
---|
211 | { |
---|
212 | char *le; |
---|
213 | |
---|
214 | while (*ln == ' ' || *ln == '\t' || *ln == '\n' || *ln == '\r') |
---|
215 | ln++; |
---|
216 | |
---|
217 | le = ln; |
---|
218 | while (*le != ' ' && *le != '\t' && *le != '\n' && *le != '\r' && *le != ';' && *le != '\0') |
---|
219 | le++; |
---|
220 | |
---|
221 | if (*le == '\0' || *le == ';') { |
---|
222 | *lr = le; |
---|
223 | } else { |
---|
224 | *lr = le + 1; |
---|
225 | } |
---|
226 | |
---|
227 | *le = '\0'; |
---|
228 | return ln; |
---|
229 | } |
---|
230 | |
---|
231 | static void |
---|
232 | parseconfigline(const char *fnm, char *ln) |
---|
233 | { |
---|
234 | point *p; |
---|
235 | const stn *s; |
---|
236 | const stn *t; |
---|
237 | leg *l; |
---|
238 | char *lc = NULL; |
---|
239 | |
---|
240 | ln = delimword(ln, &lc); |
---|
241 | |
---|
242 | if (*ln == '\0') return; |
---|
243 | |
---|
244 | if (strcmp(ln, "*start")==0) { |
---|
245 | ln = delimword(lc, &lc); |
---|
246 | if (*ln == 0) |
---|
247 | fatalerror_in_file(fnm, lineno, /*Expecting station name*/28); |
---|
248 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
249 | for (s = p->stns; s; s = s->next) { |
---|
250 | if (strcmp(s->label, ln)==0) { |
---|
251 | start = p; |
---|
252 | printf(msg(/*Starting from station %s*/512),ln); |
---|
253 | putnl(); |
---|
254 | goto loopend; |
---|
255 | } |
---|
256 | } |
---|
257 | } |
---|
258 | warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ln); |
---|
259 | } else if (strcmp(ln, "*eleft")==0) { |
---|
260 | char *ll = delimword(lc, &lc); |
---|
261 | if (*ll == 0) |
---|
262 | fatalerror_in_file(fnm, lineno, /*Expecting station name*/28); |
---|
263 | ln = delimword(lc, &lc); |
---|
264 | if (*ln == 0) { /* One argument, look for point to switch at. */ |
---|
265 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
266 | for (s = p->stns; s; s = s->next) { |
---|
267 | if (strcmp(s->label, ll)==0) { |
---|
268 | printf(msg(/*Extending to the left from station %s*/513), ll); |
---|
269 | putnl(); |
---|
270 | p->dir = ELEFT; |
---|
271 | goto loopend; |
---|
272 | } |
---|
273 | } |
---|
274 | } |
---|
275 | warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll); |
---|
276 | } else { /* Two arguments look for a specific leg */ |
---|
277 | for (l = headleg.next; l; l=l->next) { |
---|
278 | point * fr = l->fr; |
---|
279 | point * to = l->to; |
---|
280 | if (fr && to) { |
---|
281 | for (s=fr->stns; s; s=s->next) { |
---|
282 | int b = 0; |
---|
283 | if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) { |
---|
284 | char * lr = (b ? ll : ln); |
---|
285 | for (t=to->stns; t; t=t->next) { |
---|
286 | if (strcmp(t->label,lr)==0) { |
---|
287 | printf(msg(/*Extending to the left from leg %s -> %s*/515), s->label, t->label); |
---|
288 | putnl(); |
---|
289 | l->dir = ELEFT; |
---|
290 | goto loopend; |
---|
291 | } |
---|
292 | } |
---|
293 | } |
---|
294 | } |
---|
295 | } |
---|
296 | } |
---|
297 | warning_in_file(fnm, lineno, /*Failed to find leg %s -> %s*/511, ll, ln); |
---|
298 | } |
---|
299 | } else if (strcmp(ln, "*eright")==0) { |
---|
300 | char *ll = delimword(lc, &lc); |
---|
301 | if (*ll == 0) |
---|
302 | fatalerror_in_file(fnm, lineno, /*Expecting station name*/28); |
---|
303 | ln = delimword(lc, &lc); |
---|
304 | if (*ln == 0) { /* One argument, look for point to switch at. */ |
---|
305 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
306 | for (s = p->stns; s; s = s->next) { |
---|
307 | if (strcmp(s->label, ll)==0) { |
---|
308 | printf(msg(/*Extending to the right from station %s*/514), ll); |
---|
309 | putnl(); |
---|
310 | p->dir = ERIGHT; |
---|
311 | goto loopend; |
---|
312 | } |
---|
313 | } |
---|
314 | } |
---|
315 | warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll); |
---|
316 | } else { /* Two arguments look for a specific leg */ |
---|
317 | for (l = headleg.next; l; l=l->next) { |
---|
318 | point * fr = l->fr; |
---|
319 | point * to = l->to; |
---|
320 | if (fr && to) { |
---|
321 | for (s=fr->stns; s; s=s->next) { |
---|
322 | int b = 0; |
---|
323 | if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) { |
---|
324 | char * lr = (b ? ll : ln); |
---|
325 | for (t=to->stns; t; t=t->next) { |
---|
326 | if (strcmp(t->label,lr)==0) { |
---|
327 | printf(msg(/*Extending to the right from leg %s -> %s*/516), s->label, t->label); |
---|
328 | putnl(); |
---|
329 | l->dir=ERIGHT; |
---|
330 | goto loopend; |
---|
331 | } |
---|
332 | } |
---|
333 | } |
---|
334 | } |
---|
335 | } |
---|
336 | } |
---|
337 | warning_in_file(fnm, lineno, /*Failed to find leg %s -> %s*/511, ll, ln); |
---|
338 | } |
---|
339 | } else if (strcmp(ln, "*eswap")==0) { |
---|
340 | char *ll = delimword(lc, &lc); |
---|
341 | if (*ll == 0) |
---|
342 | fatalerror_in_file(fnm, lineno, /*Expecting station name*/28); |
---|
343 | ln = delimword(lc, &lc); |
---|
344 | if (*ln == 0) { /* One argument, look for point to switch at. */ |
---|
345 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
346 | for (s = p->stns; s; s = s->next) { |
---|
347 | if (strcmp(s->label, ll)==0) { |
---|
348 | printf(msg(/*Swapping extend direction from station %s*/519),ll); |
---|
349 | putnl(); |
---|
350 | p->dir = ESWAP; |
---|
351 | goto loopend; |
---|
352 | } |
---|
353 | } |
---|
354 | } |
---|
355 | warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll); |
---|
356 | } else { /* Two arguments look for a specific leg */ |
---|
357 | for (l = headleg.next; l; l=l->next) { |
---|
358 | point * fr = l->fr; |
---|
359 | point * to = l->to; |
---|
360 | if (fr && to) { |
---|
361 | for (s=fr->stns; s; s=s->next) { |
---|
362 | int b = 0; |
---|
363 | if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) { |
---|
364 | char * lr = (b ? ll : ln); |
---|
365 | for (t=to->stns; t; t=t->next) { |
---|
366 | if (strcmp(t->label,lr)==0) { |
---|
367 | printf(msg(/*Swapping extend direction from leg %s -> %s*/520), s->label, t->label); |
---|
368 | putnl(); |
---|
369 | l->dir = ESWAP; |
---|
370 | goto loopend; |
---|
371 | } |
---|
372 | } |
---|
373 | } |
---|
374 | } |
---|
375 | } |
---|
376 | } |
---|
377 | warning_in_file(fnm, lineno, /*Failed to find leg %s -> %s*/511, ll, ln); |
---|
378 | } |
---|
379 | } else if (strcmp(ln, "*break")==0) { |
---|
380 | char *ll = delimword(lc, &lc); |
---|
381 | if (*ll == 0) |
---|
382 | fatalerror_in_file(fnm, lineno, /*Expecting station name*/28); |
---|
383 | ln = delimword(lc, &lc); |
---|
384 | if (*ln == 0) { /* One argument, look for point to break at. */ |
---|
385 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
386 | for (s = p->stns; s; s = s->next) { |
---|
387 | if (strcmp(s->label, ll)==0) { |
---|
388 | printf(msg(/*Breaking survey loop at station %s*/517), ll); |
---|
389 | putnl(); |
---|
390 | p->fBroken = 1; |
---|
391 | goto loopend; |
---|
392 | } |
---|
393 | } |
---|
394 | } |
---|
395 | warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll); |
---|
396 | } else { /* Two arguments look for a specific leg */ |
---|
397 | for (l = headleg.next; l; l=l->next) { |
---|
398 | point * fr = l->fr; |
---|
399 | point * to = l->to; |
---|
400 | if (fr && to) { |
---|
401 | for (s=fr->stns; s; s=s->next) { |
---|
402 | int b = 0; |
---|
403 | if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) { |
---|
404 | char * lr = (b ? ll : ln); |
---|
405 | for (t=to->stns; t; t=t->next) { |
---|
406 | if (strcmp(t->label,lr)==0) { |
---|
407 | printf(msg(/*Breaking survey loop at leg %s -> %s*/518), s->label, t->label); |
---|
408 | putnl(); |
---|
409 | l->broken = (b ? BREAK_TO : BREAK_FR); |
---|
410 | goto loopend; |
---|
411 | } |
---|
412 | } |
---|
413 | } |
---|
414 | } |
---|
415 | } |
---|
416 | } |
---|
417 | warning_in_file(fnm, lineno, /*Failed to find leg %s -> %s*/511, ll, ln); |
---|
418 | } |
---|
419 | } else { |
---|
420 | fatalerror_in_file(fnm, lineno, /*Unknown command `%s'*/12, ln); |
---|
421 | } |
---|
422 | loopend: |
---|
423 | ln = delimword(lc, &lc); |
---|
424 | if (*ln != 0) { |
---|
425 | fatalerror_in_file(fnm, lineno, /*End of line not blank*/15); |
---|
426 | /* FIXME: give ln as context? */ |
---|
427 | } |
---|
428 | } |
---|
429 | |
---|
430 | static const struct option long_opts[] = { |
---|
431 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
---|
432 | {"survey", required_argument, 0, 's'}, |
---|
433 | {"specfile", required_argument, 0, 'p'}, |
---|
434 | {"help", no_argument, 0, HLP_HELP}, |
---|
435 | {"version", no_argument, 0, HLP_VERSION}, |
---|
436 | {0, 0, 0, 0} |
---|
437 | }; |
---|
438 | |
---|
439 | #define short_opts "s:p:" |
---|
440 | |
---|
441 | static struct help_msg help[] = { |
---|
442 | /* <-- */ |
---|
443 | {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0}, |
---|
444 | {0, 0, 0} |
---|
445 | }; |
---|
446 | |
---|
447 | int |
---|
448 | main(int argc, char **argv) |
---|
449 | { |
---|
450 | const char *fnm_in, *fnm_out; |
---|
451 | char *desc; |
---|
452 | img_point pt; |
---|
453 | int result; |
---|
454 | point *fr = NULL, *to; |
---|
455 | double zMax = -DBL_MAX; |
---|
456 | point *p; |
---|
457 | const char *survey = NULL; |
---|
458 | const char *specfile = NULL; |
---|
459 | img *pimg; |
---|
460 | int have_xsect = 0; |
---|
461 | |
---|
462 | msg_init(argv); |
---|
463 | |
---|
464 | cmdline_set_syntax_message("INPUT_3D_FILE [OUTPUT_3D_FILE]", NULL); |
---|
465 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2); |
---|
466 | while (1) { |
---|
467 | int opt = cmdline_getopt(); |
---|
468 | if (opt == EOF) break; |
---|
469 | if (opt == 's') survey = optarg; |
---|
470 | if (opt == 'p') specfile = optarg; |
---|
471 | } |
---|
472 | fnm_in = argv[optind++]; |
---|
473 | if (argv[optind]) { |
---|
474 | fnm_out = argv[optind]; |
---|
475 | } else { |
---|
476 | char * base_in = base_from_fnm(fnm_in); |
---|
477 | char * base_out = osmalloc(strlen(base_in) + 8); |
---|
478 | strcpy(base_out, base_in); |
---|
479 | strcat(base_out, "_extend"); |
---|
480 | fnm_out = add_ext(base_out, EXT_SVX_3D); |
---|
481 | osfree(base_in); |
---|
482 | osfree(base_out); |
---|
483 | } |
---|
484 | |
---|
485 | /* try to open image file, and check it has correct header */ |
---|
486 | pimg = img_open_survey(fnm_in, survey); |
---|
487 | if (pimg == NULL) fatalerror(img_error(), fnm_in); |
---|
488 | |
---|
489 | putnl(); |
---|
490 | puts(msg(/*Reading in data - please wait...*/105)); |
---|
491 | |
---|
492 | htab = osmalloc(ossizeof(pfx*) * HTAB_SIZE); |
---|
493 | { |
---|
494 | int i; |
---|
495 | for (i = 0; i < HTAB_SIZE; ++i) htab[i] = NULL; |
---|
496 | } |
---|
497 | |
---|
498 | do { |
---|
499 | result = img_read_item(pimg, &pt); |
---|
500 | switch (result) { |
---|
501 | case img_MOVE: |
---|
502 | fr = find_point(&pt); |
---|
503 | break; |
---|
504 | case img_LINE: |
---|
505 | if (!fr) { |
---|
506 | result = img_BAD; |
---|
507 | break; |
---|
508 | } |
---|
509 | to = find_point(&pt); |
---|
510 | if (!(pimg->flags & (img_FLAG_SURFACE|img_FLAG_SPLAY))) |
---|
511 | add_leg(fr, to, pimg->label, pimg->flags); |
---|
512 | fr = to; |
---|
513 | break; |
---|
514 | case img_LABEL: |
---|
515 | to = find_point(&pt); |
---|
516 | add_label(to, pimg->label, pimg->flags); |
---|
517 | break; |
---|
518 | case img_BAD: |
---|
519 | (void)img_close(pimg); |
---|
520 | fatalerror(img_error(), fnm_in); |
---|
521 | break; |
---|
522 | case img_XSECT: |
---|
523 | have_xsect = 1; |
---|
524 | break; |
---|
525 | } |
---|
526 | } while (result != img_STOP); |
---|
527 | |
---|
528 | desc = osmalloc(strlen(pimg->title) + 11 + 1); |
---|
529 | strcpy(desc, pimg->title); |
---|
530 | strcat(desc, " (extended)"); |
---|
531 | |
---|
532 | if (specfile) { |
---|
533 | FILE *fs = NULL; |
---|
534 | char *fnm_used; |
---|
535 | printf(msg(/*Applying specfile: `%s'*/521), specfile); |
---|
536 | putnl(); |
---|
537 | fs = fopenWithPthAndExt("", specfile, NULL, "r", &fnm_used); |
---|
538 | if (fs == NULL) fatalerror(/*Unable to open file*/93, specfile); |
---|
539 | while (!feof(fs)) { |
---|
540 | char *lbuf = getline_alloc(fs, 32); |
---|
541 | lineno++; |
---|
542 | if (!lbuf) |
---|
543 | fatalerror_in_file(fnm_used, lineno, /*Error reading file*/18); |
---|
544 | parseconfigline(fnm_used, lbuf); |
---|
545 | osfree(lbuf); |
---|
546 | } |
---|
547 | osfree(fnm_used); |
---|
548 | } |
---|
549 | |
---|
550 | if (start == NULL) { /* i.e. start wasn't specified in specfile */ |
---|
551 | |
---|
552 | /* start at the highest entrance with some legs attached */ |
---|
553 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
554 | if (p->order > 0 && p->p.z > zMax) { |
---|
555 | const stn *s; |
---|
556 | for (s = p->stns; s; s = s->next) { |
---|
557 | if (s->flags & img_SFLAG_ENTRANCE) { |
---|
558 | start = p; |
---|
559 | zMax = p->p.z; |
---|
560 | break; |
---|
561 | } |
---|
562 | } |
---|
563 | } |
---|
564 | } |
---|
565 | if (start == NULL) { |
---|
566 | /* if no entrances with legs, start at the highest 1-node */ |
---|
567 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
568 | if (p->order == 1 && p->p.z > zMax) { |
---|
569 | start = p; |
---|
570 | zMax = p->p.z; |
---|
571 | } |
---|
572 | } |
---|
573 | /* of course we may have no 1-nodes... */ |
---|
574 | if (start == NULL) { |
---|
575 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
576 | if (p->order != 0 && p->p.z > zMax) { |
---|
577 | start = p; |
---|
578 | zMax = p->p.z; |
---|
579 | } |
---|
580 | } |
---|
581 | if (start == NULL) { |
---|
582 | /* There are no legs - just pick the highest station... */ |
---|
583 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
584 | if (p->p.z > zMax) { |
---|
585 | start = p; |
---|
586 | zMax = p->p.z; |
---|
587 | } |
---|
588 | } |
---|
589 | if (!start) fatalerror(/*No survey data*/43); |
---|
590 | } |
---|
591 | } |
---|
592 | } |
---|
593 | } |
---|
594 | |
---|
595 | printf(msg(/*Writing %s...*/522), fnm_out); |
---|
596 | putnl(); |
---|
597 | pimg_out = img_open_write(fnm_out, desc, fTrue); |
---|
598 | |
---|
599 | /* Only does single connected component currently. */ |
---|
600 | do_stn(start, 0.0, NULL, ERIGHT, 0); |
---|
601 | |
---|
602 | if (have_xsect) { |
---|
603 | img_rewind(pimg); |
---|
604 | do { |
---|
605 | result = img_read_item(pimg, &pt); |
---|
606 | if (result == img_XSECT) { |
---|
607 | pimg_out->u = pimg->u; |
---|
608 | pimg_out->d = pimg->d; |
---|
609 | img_write_item(pimg_out, img_XSECT, pimg->flags, pimg->label, 0, 0, 0); |
---|
610 | } |
---|
611 | } while (result != img_STOP); |
---|
612 | } |
---|
613 | |
---|
614 | (void)img_close(pimg); |
---|
615 | |
---|
616 | if (!img_close(pimg_out)) { |
---|
617 | (void)remove(fnm_out); |
---|
618 | fatalerror(img_error(), fnm_out); |
---|
619 | } |
---|
620 | |
---|
621 | return EXIT_SUCCESS; |
---|
622 | } |
---|
623 | |
---|
624 | static int adjust_direction(int dir, int by) { |
---|
625 | if (by == ESWAP) |
---|
626 | return dir ^ (ELEFT|ERIGHT); |
---|
627 | if (by) |
---|
628 | return by; |
---|
629 | return dir; |
---|
630 | } |
---|
631 | |
---|
632 | static void |
---|
633 | do_stn(point *p, double X, const char *prefix, int dir, int labOnly) |
---|
634 | { |
---|
635 | leg *l, *lp; |
---|
636 | double dX; |
---|
637 | const stn *s; |
---|
638 | int odir = dir; |
---|
639 | |
---|
640 | for (s = p->stns; s; s = s->next) { |
---|
641 | img_write_item(pimg_out, img_LABEL, s->flags, s->label, X, 0, p->p.z); |
---|
642 | } |
---|
643 | if (labOnly || p->fBroken) { |
---|
644 | return; |
---|
645 | } |
---|
646 | |
---|
647 | lp = &headleg; |
---|
648 | for (l = lp->next; l; lp = l, l = lp->next) { |
---|
649 | dir = odir; |
---|
650 | if (l->fDone) { |
---|
651 | /* this case happens if a recursive call causes the next leg to be |
---|
652 | * removed, leaving our next pointing to a leg which has been dealt |
---|
653 | * with... */ |
---|
654 | continue; |
---|
655 | } |
---|
656 | if (l->prefix != prefix) { |
---|
657 | continue; |
---|
658 | } |
---|
659 | if (l->to == p) { |
---|
660 | if (l->broken & BREAK_TO) continue; |
---|
661 | lp->next = l->next; |
---|
662 | /* adjust direction of extension if necessary */ |
---|
663 | dir = adjust_direction(dir, l->to->dir); |
---|
664 | dir = adjust_direction(dir, l->dir); |
---|
665 | |
---|
666 | dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y); |
---|
667 | if (dir == ELEFT) dX = -dX; |
---|
668 | img_write_item(pimg_out, img_MOVE, 0, NULL, X + dX, 0, l->fr->p.z); |
---|
669 | img_write_item(pimg_out, img_LINE, l->flags, l->prefix, |
---|
670 | X, 0, l->to->p.z); |
---|
671 | l->fDone = 1; |
---|
672 | do_stn(l->fr, X + dX, l->prefix, dir, (l->broken & BREAK_FR)); |
---|
673 | l = lp; |
---|
674 | } else if (l->fr == p) { |
---|
675 | if (l->broken & BREAK_FR) continue; |
---|
676 | lp->next = l->next; |
---|
677 | /* adjust direction of extension if necessary */ |
---|
678 | dir = adjust_direction(dir, l->fr->dir); |
---|
679 | dir = adjust_direction(dir, l->dir); |
---|
680 | |
---|
681 | dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y); |
---|
682 | if (dir == ELEFT) dX = -dX; |
---|
683 | img_write_item(pimg_out, img_MOVE, 0, NULL, X, 0, l->fr->p.z); |
---|
684 | img_write_item(pimg_out, img_LINE, l->flags, l->prefix, |
---|
685 | X + dX, 0, l->to->p.z); |
---|
686 | l->fDone = 1; |
---|
687 | do_stn(l->to, X + dX, l->prefix, dir, (l->broken & BREAK_TO)); |
---|
688 | l = lp; |
---|
689 | } |
---|
690 | } |
---|
691 | lp = &headleg; |
---|
692 | for (l = lp->next; l; lp = l, l = lp->next) { |
---|
693 | dir = odir; |
---|
694 | if (l->fDone) { |
---|
695 | /* this case happens iff a recursive call causes the next leg to be |
---|
696 | * removed, leaving our next pointing to a leg which has been dealt |
---|
697 | * with... */ |
---|
698 | continue; |
---|
699 | } |
---|
700 | if (l->to == p) { |
---|
701 | if (l->broken & BREAK_TO) continue; |
---|
702 | lp->next = l->next; |
---|
703 | /* adjust direction of extension if necessary */ |
---|
704 | dir = adjust_direction(dir, l->to->dir); |
---|
705 | dir = adjust_direction(dir, l->dir); |
---|
706 | |
---|
707 | dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y); |
---|
708 | if (dir == ELEFT) dX = -dX; |
---|
709 | img_write_item(pimg_out, img_MOVE, 0, NULL, X + dX, 0, l->fr->p.z); |
---|
710 | img_write_item(pimg_out, img_LINE, l->flags, l->prefix, |
---|
711 | X, 0, l->to->p.z); |
---|
712 | l->fDone = 1; |
---|
713 | do_stn(l->fr, X + dX, l->prefix, dir, (l->broken & BREAK_FR)); |
---|
714 | l = lp; |
---|
715 | } else if (l->fr == p) { |
---|
716 | if (l->broken & BREAK_FR) continue; |
---|
717 | lp->next = l->next; |
---|
718 | /* adjust direction of extension if necessary */ |
---|
719 | dir = adjust_direction(dir, l->fr->dir); |
---|
720 | dir = adjust_direction(dir, l->dir); |
---|
721 | |
---|
722 | dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y); |
---|
723 | if (dir == ELEFT) dX = -dX; |
---|
724 | img_write_item(pimg_out, img_MOVE, 0, NULL, X, 0, l->fr->p.z); |
---|
725 | img_write_item(pimg_out, img_LINE, l->flags, l->prefix, |
---|
726 | X + dX, 0, l->to->p.z); |
---|
727 | l->fDone = 1; |
---|
728 | do_stn(l->to, X + dX, l->prefix, dir, (l->broken & BREAK_TO)); |
---|
729 | l = lp; |
---|
730 | } |
---|
731 | } |
---|
732 | } |
---|