[7ee2b42] | 1 | /* extend.c |
---|
[d1b1380] | 2 | * Produce an extended elevation |
---|
[a405bc1] | 3 | * Copyright (C) 1995-2002,2005,2010,2011,2013 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 | |
---|
[a420b49] | 21 | #ifdef HAVE_CONFIG_H |
---|
| 22 | # include <config.h> |
---|
| 23 | #endif |
---|
[d1b1380] | 24 | |
---|
[e55ee28] | 25 | #include <float.h> |
---|
[d1b1380] | 26 | #include <stdio.h> |
---|
| 27 | #include <stdlib.h> |
---|
| 28 | #include <string.h> |
---|
| 29 | |
---|
[acc20b1] | 30 | #include "cmdline.h" |
---|
[693388e] | 31 | #include "debug.h" |
---|
[d1b1380] | 32 | #include "filelist.h" |
---|
[693388e] | 33 | #include "filename.h" |
---|
| 34 | #include "hash.h" |
---|
[a405bc1] | 35 | #include "img_hosted.h" |
---|
[693388e] | 36 | #include "message.h" |
---|
| 37 | #include "useful.h" |
---|
[d1b1380] | 38 | |
---|
[693388e] | 39 | /* To save memory we should probably use the prefix hash for the prefix on |
---|
| 40 | * point labels (FIXME) */ |
---|
[ea1c342] | 41 | |
---|
| 42 | typedef struct stn { |
---|
| 43 | const char *label; |
---|
| 44 | int flags; |
---|
[ee11c6a7] | 45 | const struct stn *next; |
---|
[ea1c342] | 46 | } stn; |
---|
| 47 | |
---|
[d1b1380] | 48 | typedef struct POINT { |
---|
[0ed0e16] | 49 | img_point p; |
---|
[ea1c342] | 50 | const stn *stns; |
---|
[647407d] | 51 | unsigned int order; |
---|
[d92d282] | 52 | char dir; |
---|
| 53 | char fDone; |
---|
| 54 | char fBroken; |
---|
[d1b1380] | 55 | struct POINT *next; |
---|
| 56 | } point; |
---|
| 57 | |
---|
| 58 | typedef struct LEG { |
---|
| 59 | point *fr, *to; |
---|
[693388e] | 60 | const char *prefix; |
---|
[d92d282] | 61 | char dir; |
---|
| 62 | char fDone; |
---|
| 63 | char broken; |
---|
[95c3272] | 64 | int flags; |
---|
| 65 | struct LEG *next; |
---|
[d1b1380] | 66 | } leg; |
---|
| 67 | |
---|
[d92d282] | 68 | /* Values for leg.broken: */ |
---|
| 69 | #define BREAK_FR 0x01 |
---|
| 70 | #define BREAK_TO 0x02 |
---|
[d1b1380] | 71 | |
---|
[d92d282] | 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}; |
---|
[d1b1380] | 80 | |
---|
[31b7105] | 81 | static img *pimg_out; |
---|
[d1b1380] | 82 | |
---|
[d92d282] | 83 | static void do_stn(point *, double, const char *, int, int); |
---|
[d1b1380] | 84 | |
---|
[693388e] | 85 | typedef struct pfx { |
---|
| 86 | const char *label; |
---|
| 87 | struct pfx *next; |
---|
| 88 | } pfx; |
---|
| 89 | |
---|
| 90 | static pfx **htab; |
---|
| 91 | |
---|
[63dc4eb] | 92 | #define HTAB_SIZE 0x2000 |
---|
| 93 | |
---|
[693388e] | 94 | static const char * |
---|
| 95 | find_prefix(const char *prefix) |
---|
| 96 | { |
---|
| 97 | pfx *p; |
---|
| 98 | int hash; |
---|
| 99 | |
---|
[4c07c51] | 100 | SVX_ASSERT(prefix); |
---|
[421b7d2] | 101 | |
---|
[63dc4eb] | 102 | hash = hash_string(prefix) & (HTAB_SIZE - 1); |
---|
[693388e] | 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 | |
---|
[a420b49] | 115 | static point * |
---|
[0ed0e16] | 116 | find_point(const img_point *pt) |
---|
[a420b49] | 117 | { |
---|
[d1b1380] | 118 | point *p; |
---|
[647407d] | 119 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
[0ed0e16] | 120 | if (pt->x == p->p.x && pt->y == p->p.y && pt->z == p->p.z) { |
---|
[647407d] | 121 | return p; |
---|
| 122 | } |
---|
| 123 | } |
---|
[a420b49] | 124 | |
---|
| 125 | p = osmalloc(ossizeof(point)); |
---|
[0ed0e16] | 126 | p->p = *pt; |
---|
[ea1c342] | 127 | p->stns = NULL; |
---|
[7ee2b42] | 128 | p->order = 0; |
---|
[d92d282] | 129 | p->dir = 0; |
---|
| 130 | p->fDone = 0; |
---|
[8add4c9] | 131 | p->fBroken = 0; |
---|
[a420b49] | 132 | p->next = headpoint.next; |
---|
| 133 | headpoint.next = p; |
---|
[d1b1380] | 134 | return p; |
---|
| 135 | } |
---|
| 136 | |
---|
[a420b49] | 137 | static void |
---|
[693388e] | 138 | add_leg(point *fr, point *to, const char *prefix, int flags) |
---|
[a420b49] | 139 | { |
---|
[95c3272] | 140 | leg *l; |
---|
[7ee2b42] | 141 | fr->order++; |
---|
| 142 | to->order++; |
---|
[95c3272] | 143 | l = osmalloc(ossizeof(leg)); |
---|
| 144 | l->fr = fr; |
---|
| 145 | l->to = to; |
---|
[693388e] | 146 | if (prefix) |
---|
| 147 | l->prefix = find_prefix(prefix); |
---|
| 148 | else |
---|
| 149 | l->prefix = NULL; |
---|
[95c3272] | 150 | l->next = headleg.next; |
---|
[d92d282] | 151 | l->dir = 0; |
---|
[95c3272] | 152 | l->fDone = 0; |
---|
[8add4c9] | 153 | l->broken = 0; |
---|
[95c3272] | 154 | l->flags = flags; |
---|
| 155 | headleg.next = l; |
---|
[d1b1380] | 156 | } |
---|
| 157 | |
---|
[a420b49] | 158 | static void |
---|
[95c3272] | 159 | add_label(point *p, const char *label, int flags) |
---|
[a420b49] | 160 | { |
---|
[ea1c342] | 161 | stn *s = osnew(stn); |
---|
| 162 | s->label = osstrdup(label); |
---|
| 163 | s->flags = flags; |
---|
| 164 | s->next = p->stns; |
---|
| 165 | p->stns = s; |
---|
[d1b1380] | 166 | } |
---|
| 167 | |
---|
[d92d282] | 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 | |
---|
[e02a6a6] | 181 | ch = GETC(fh); |
---|
[d92d282] | 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 | } |
---|
[e02a6a6] | 194 | ch = GETC(fh); |
---|
[d92d282] | 195 | } |
---|
| 196 | if (ch == '\n' || ch == '\r') { |
---|
| 197 | int otherone = ch ^ ('\n' ^ '\r'); |
---|
[e02a6a6] | 198 | ch = GETC(fh); |
---|
[d92d282] | 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 | } |
---|
[6e4a123] | 226 | |
---|
[d92d282] | 227 | *le = '\0'; |
---|
| 228 | return ln; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | static void |
---|
[7926772] | 232 | parseconfigline(const char *fnm, char *ln) |
---|
[d92d282] | 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); |
---|
[7926772] | 246 | if (*ln == 0) |
---|
| 247 | fatalerror_in_file(fnm, lineno, /*Expecting station name*/28); |
---|
[d92d282] | 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; |
---|
[7926772] | 252 | printf(msg(/*Starting from station %s*/512),ln); |
---|
[d92d282] | 253 | putnl(); |
---|
| 254 | goto loopend; |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | } |
---|
[7926772] | 258 | warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ln); |
---|
[d92d282] | 259 | } else if (strcmp(ln, "*eleft")==0) { |
---|
| 260 | char *ll = delimword(lc, &lc); |
---|
[7926772] | 261 | if (*ll == 0) |
---|
| 262 | fatalerror_in_file(fnm, lineno, /*Expecting station name*/28); |
---|
[d92d282] | 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) { |
---|
[7926772] | 268 | printf(msg(/*Extending to the left from station %s*/513), ll); |
---|
[d92d282] | 269 | putnl(); |
---|
| 270 | p->dir = ELEFT; |
---|
| 271 | goto loopend; |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | } |
---|
[7926772] | 275 | warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll); |
---|
[d92d282] | 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) { |
---|
[ee7511a] | 287 | printf(msg(/*Extending to the left from leg %s → %s*/515), s->label, t->label); |
---|
[d92d282] | 288 | putnl(); |
---|
| 289 | l->dir = ELEFT; |
---|
| 290 | goto loopend; |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | } |
---|
| 296 | } |
---|
[ee7511a] | 297 | warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln); |
---|
[d92d282] | 298 | } |
---|
| 299 | } else if (strcmp(ln, "*eright")==0) { |
---|
| 300 | char *ll = delimword(lc, &lc); |
---|
[7926772] | 301 | if (*ll == 0) |
---|
| 302 | fatalerror_in_file(fnm, lineno, /*Expecting station name*/28); |
---|
[d92d282] | 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) { |
---|
[7926772] | 308 | printf(msg(/*Extending to the right from station %s*/514), ll); |
---|
[d92d282] | 309 | putnl(); |
---|
| 310 | p->dir = ERIGHT; |
---|
| 311 | goto loopend; |
---|
| 312 | } |
---|
| 313 | } |
---|
| 314 | } |
---|
[7926772] | 315 | warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll); |
---|
[d92d282] | 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) { |
---|
[ee7511a] | 327 | printf(msg(/*Extending to the right from leg %s → %s*/516), s->label, t->label); |
---|
[7926772] | 328 | putnl(); |
---|
[d92d282] | 329 | l->dir=ERIGHT; |
---|
| 330 | goto loopend; |
---|
| 331 | } |
---|
| 332 | } |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | } |
---|
[ee7511a] | 337 | warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln); |
---|
[d92d282] | 338 | } |
---|
| 339 | } else if (strcmp(ln, "*eswap")==0) { |
---|
| 340 | char *ll = delimword(lc, &lc); |
---|
[7926772] | 341 | if (*ll == 0) |
---|
| 342 | fatalerror_in_file(fnm, lineno, /*Expecting station name*/28); |
---|
[d92d282] | 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) { |
---|
[7926772] | 348 | printf(msg(/*Swapping extend direction from station %s*/519),ll); |
---|
[d92d282] | 349 | putnl(); |
---|
| 350 | p->dir = ESWAP; |
---|
| 351 | goto loopend; |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | } |
---|
[7926772] | 355 | warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll); |
---|
[d92d282] | 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) { |
---|
[ee7511a] | 367 | printf(msg(/*Swapping extend direction from leg %s → %s*/520), s->label, t->label); |
---|
[7926772] | 368 | putnl(); |
---|
[d92d282] | 369 | l->dir = ESWAP; |
---|
| 370 | goto loopend; |
---|
| 371 | } |
---|
| 372 | } |
---|
| 373 | } |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | } |
---|
[ee7511a] | 377 | warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln); |
---|
[d92d282] | 378 | } |
---|
| 379 | } else if (strcmp(ln, "*break")==0) { |
---|
| 380 | char *ll = delimword(lc, &lc); |
---|
[7926772] | 381 | if (*ll == 0) |
---|
| 382 | fatalerror_in_file(fnm, lineno, /*Expecting station name*/28); |
---|
[d92d282] | 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) { |
---|
[7926772] | 388 | printf(msg(/*Breaking survey loop at station %s*/517), ll); |
---|
[d92d282] | 389 | putnl(); |
---|
| 390 | p->fBroken = 1; |
---|
| 391 | goto loopend; |
---|
| 392 | } |
---|
| 393 | } |
---|
| 394 | } |
---|
[7926772] | 395 | warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll); |
---|
[d92d282] | 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) { |
---|
[ee7511a] | 407 | printf(msg(/*Breaking survey loop at leg %s → %s*/518), s->label, t->label); |
---|
[d92d282] | 408 | putnl(); |
---|
| 409 | l->broken = (b ? BREAK_TO : BREAK_FR); |
---|
| 410 | goto loopend; |
---|
| 411 | } |
---|
| 412 | } |
---|
| 413 | } |
---|
| 414 | } |
---|
| 415 | } |
---|
| 416 | } |
---|
[ee7511a] | 417 | warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln); |
---|
[d92d282] | 418 | } |
---|
| 419 | } else { |
---|
[0804fbe] | 420 | fatalerror_in_file(fnm, lineno, /*Unknown command “%s”*/12, ln); |
---|
[d92d282] | 421 | } |
---|
| 422 | loopend: |
---|
| 423 | ln = delimword(lc, &lc); |
---|
| 424 | if (*ln != 0) { |
---|
[7926772] | 425 | fatalerror_in_file(fnm, lineno, /*End of line not blank*/15); |
---|
| 426 | /* FIXME: give ln as context? */ |
---|
[d92d282] | 427 | } |
---|
| 428 | } |
---|
| 429 | |
---|
[acc20b1] | 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; */ |
---|
[76bbb7c9] | 432 | {"survey", required_argument, 0, 's'}, |
---|
[d92d282] | 433 | {"specfile", required_argument, 0, 'p'}, |
---|
[acc20b1] | 434 | {"help", no_argument, 0, HLP_HELP}, |
---|
| 435 | {"version", no_argument, 0, HLP_VERSION}, |
---|
| 436 | {0, 0, 0, 0} |
---|
| 437 | }; |
---|
| 438 | |
---|
[d92d282] | 439 | #define short_opts "s:p:" |
---|
[acc20b1] | 440 | |
---|
| 441 | static struct help_msg help[] = { |
---|
| 442 | /* <-- */ |
---|
[45af761] | 443 | {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0}, |
---|
| 444 | {0, 0, 0} |
---|
[acc20b1] | 445 | }; |
---|
| 446 | |
---|
[a420b49] | 447 | int |
---|
| 448 | main(int argc, char **argv) |
---|
| 449 | { |
---|
[6f185e5] | 450 | const char *fnm_in, *fnm_out; |
---|
[a2ad284] | 451 | char *desc; |
---|
[0ed0e16] | 452 | img_point pt; |
---|
[d1b1380] | 453 | int result; |
---|
[647407d] | 454 | point *fr = NULL, *to; |
---|
[badeec8] | 455 | double zMax = -DBL_MAX; |
---|
[647407d] | 456 | point *p; |
---|
[76bbb7c9] | 457 | const char *survey = NULL; |
---|
[d92d282] | 458 | const char *specfile = NULL; |
---|
[31b7105] | 459 | img *pimg; |
---|
| 460 | int have_xsect = 0; |
---|
[d1b1380] | 461 | |
---|
[bdfe97f] | 462 | msg_init(argv); |
---|
[d1b1380] | 463 | |
---|
[d8dbdff] | 464 | cmdline_set_syntax_message(/*INPUT_3D_FILE [OUTPUT_3D_FILE]*/267, 0, NULL); |
---|
[acc20b1] | 465 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2); |
---|
[76bbb7c9] | 466 | while (1) { |
---|
| 467 | int opt = cmdline_getopt(); |
---|
| 468 | if (opt == EOF) break; |
---|
| 469 | if (opt == 's') survey = optarg; |
---|
[d92d282] | 470 | if (opt == 'p') specfile = optarg; |
---|
[acc20b1] | 471 | } |
---|
[6f185e5] | 472 | fnm_in = argv[optind++]; |
---|
[acc20b1] | 473 | if (argv[optind]) { |
---|
[6f185e5] | 474 | fnm_out = argv[optind]; |
---|
[acc20b1] | 475 | } else { |
---|
[6379f93] | 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); |
---|
[d1b1380] | 483 | } |
---|
| 484 | |
---|
| 485 | /* try to open image file, and check it has correct header */ |
---|
[a2ad284] | 486 | pimg = img_open_survey(fnm_in, survey); |
---|
[a405bc1] | 487 | if (pimg == NULL) fatalerror(img_error2msg(img_error()), fnm_in); |
---|
[d1b1380] | 488 | |
---|
| 489 | putnl(); |
---|
[ee7511a] | 490 | puts(msg(/*Reading in data - please wait…*/105)); |
---|
[d1b1380] | 491 | |
---|
[63dc4eb] | 492 | htab = osmalloc(ossizeof(pfx*) * HTAB_SIZE); |
---|
| 493 | { |
---|
| 494 | int i; |
---|
| 495 | for (i = 0; i < HTAB_SIZE; ++i) htab[i] = NULL; |
---|
| 496 | } |
---|
[421b7d2] | 497 | |
---|
[d1b1380] | 498 | do { |
---|
[23f7ea7] | 499 | result = img_read_item(pimg, &pt); |
---|
[d1b1380] | 500 | switch (result) { |
---|
[a420b49] | 501 | case img_MOVE: |
---|
[421b7d2] | 502 | fr = find_point(&pt); |
---|
| 503 | break; |
---|
[a420b49] | 504 | case img_LINE: |
---|
[421b7d2] | 505 | if (!fr) { |
---|
[76bbb7c9] | 506 | result = img_BAD; |
---|
| 507 | break; |
---|
[421b7d2] | 508 | } |
---|
| 509 | to = find_point(&pt); |
---|
| 510 | if (!(pimg->flags & (img_FLAG_SURFACE|img_FLAG_SPLAY))) |
---|
[693388e] | 511 | add_leg(fr, to, pimg->label, pimg->flags); |
---|
[421b7d2] | 512 | fr = to; |
---|
| 513 | break; |
---|
[a420b49] | 514 | case img_LABEL: |
---|
[388ad50] | 515 | to = find_point(&pt); |
---|
| 516 | add_label(to, pimg->label, pimg->flags); |
---|
[421b7d2] | 517 | break; |
---|
[ed0f5b6] | 518 | case img_BAD: |
---|
[6f185e5] | 519 | (void)img_close(pimg); |
---|
[a405bc1] | 520 | fatalerror(img_error2msg(img_error()), fnm_in); |
---|
[31b7105] | 521 | break; |
---|
| 522 | case img_XSECT: |
---|
| 523 | have_xsect = 1; |
---|
| 524 | break; |
---|
[d1b1380] | 525 | } |
---|
[ed0f5b6] | 526 | } while (result != img_STOP); |
---|
[d1b1380] | 527 | |
---|
[0c50ce3] | 528 | desc = osstrdup(pimg->title); |
---|
[a2ad284] | 529 | |
---|
[d92d282] | 530 | if (specfile) { |
---|
| 531 | FILE *fs = NULL; |
---|
[7926772] | 532 | char *fnm_used; |
---|
[0804fbe] | 533 | printf(msg(/*Applying specfile: “%s”*/521), specfile); |
---|
[d92d282] | 534 | putnl(); |
---|
[7926772] | 535 | fs = fopenWithPthAndExt("", specfile, NULL, "r", &fnm_used); |
---|
[ee7511a] | 536 | if (fs == NULL) fatalerror(/*Couldn’t open file “%s”*/93, specfile); |
---|
[d92d282] | 537 | while (!feof(fs)) { |
---|
[7926772] | 538 | char *lbuf = getline_alloc(fs, 32); |
---|
[d92d282] | 539 | lineno++; |
---|
[7926772] | 540 | if (!lbuf) |
---|
| 541 | fatalerror_in_file(fnm_used, lineno, /*Error reading file*/18); |
---|
| 542 | parseconfigline(fnm_used, lbuf); |
---|
[d92d282] | 543 | osfree(lbuf); |
---|
[647407d] | 544 | } |
---|
[7926772] | 545 | osfree(fnm_used); |
---|
[647407d] | 546 | } |
---|
[d92d282] | 547 | |
---|
| 548 | if (start == NULL) { /* i.e. start wasn't specified in specfile */ |
---|
[8add4c9] | 549 | |
---|
[d92d282] | 550 | /* start at the highest entrance with some legs attached */ |
---|
[647407d] | 551 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
[d92d282] | 552 | if (p->order > 0 && p->p.z > zMax) { |
---|
| 553 | const stn *s; |
---|
| 554 | for (s = p->stns; s; s = s->next) { |
---|
| 555 | if (s->flags & img_SFLAG_ENTRANCE) { |
---|
| 556 | start = p; |
---|
| 557 | zMax = p->p.z; |
---|
| 558 | break; |
---|
| 559 | } |
---|
| 560 | } |
---|
[647407d] | 561 | } |
---|
| 562 | } |
---|
| 563 | if (start == NULL) { |
---|
[d92d282] | 564 | /* if no entrances with legs, start at the highest 1-node */ |
---|
[7ee2b42] | 565 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
[d92d282] | 566 | if (p->order == 1 && p->p.z > zMax) { |
---|
[7ee2b42] | 567 | start = p; |
---|
| 568 | zMax = p->p.z; |
---|
| 569 | } |
---|
| 570 | } |
---|
[d92d282] | 571 | /* of course we may have no 1-nodes... */ |
---|
[d216f5d] | 572 | if (start == NULL) { |
---|
| 573 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
[d92d282] | 574 | if (p->order != 0 && p->p.z > zMax) { |
---|
[d216f5d] | 575 | start = p; |
---|
| 576 | zMax = p->p.z; |
---|
| 577 | } |
---|
| 578 | } |
---|
[d92d282] | 579 | if (start == NULL) { |
---|
| 580 | /* There are no legs - just pick the highest station... */ |
---|
| 581 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
| 582 | if (p->p.z > zMax) { |
---|
| 583 | start = p; |
---|
| 584 | zMax = p->p.z; |
---|
| 585 | } |
---|
| 586 | } |
---|
| 587 | if (!start) fatalerror(/*No survey data*/43); |
---|
| 588 | } |
---|
[d216f5d] | 589 | } |
---|
[647407d] | 590 | } |
---|
| 591 | } |
---|
[d92d282] | 592 | |
---|
[ee7511a] | 593 | printf(msg(/*Writing %s…*/522), fnm_out); |
---|
[d92d282] | 594 | putnl(); |
---|
[0c50ce3] | 595 | pimg_out = img_open_write(fnm_out, desc, img_FFLAG_EXTENDED); |
---|
[d1b1380] | 596 | |
---|
[d92d282] | 597 | /* Only does single connected component currently. */ |
---|
| 598 | do_stn(start, 0.0, NULL, ERIGHT, 0); |
---|
[31b7105] | 599 | |
---|
| 600 | if (have_xsect) { |
---|
| 601 | img_rewind(pimg); |
---|
[2f1c0c0] | 602 | /* Read ahead on pimg before writing pimg_out so we find out if an |
---|
| 603 | * img_XSECT_END comes next. */ |
---|
| 604 | char * label = NULL; |
---|
| 605 | int flags = 0; |
---|
[31b7105] | 606 | do { |
---|
| 607 | result = img_read_item(pimg, &pt); |
---|
[2f1c0c0] | 608 | if (result == img_XSECT || result == img_XSECT_END) { |
---|
| 609 | if (label) { |
---|
| 610 | if (result == img_XSECT_END) |
---|
| 611 | flags |= img_XFLAG_END; |
---|
| 612 | img_write_item(pimg_out, img_XSECT, flags, label, 0, 0, 0); |
---|
| 613 | osfree(label); |
---|
| 614 | label = NULL; |
---|
| 615 | } |
---|
| 616 | } |
---|
[31b7105] | 617 | if (result == img_XSECT) { |
---|
[2f1c0c0] | 618 | label = osstrdup(pimg->label); |
---|
| 619 | flags = pimg->flags; |
---|
| 620 | pimg_out->l = pimg->l; |
---|
| 621 | pimg_out->r = pimg->r; |
---|
| 622 | pimg_out->u = pimg->u; |
---|
[31b7105] | 623 | pimg_out->d = pimg->d; |
---|
| 624 | } |
---|
| 625 | } while (result != img_STOP); |
---|
| 626 | } |
---|
[8add4c9] | 627 | |
---|
[31b7105] | 628 | (void)img_close(pimg); |
---|
| 629 | |
---|
| 630 | if (!img_close(pimg_out)) { |
---|
[6f185e5] | 631 | (void)remove(fnm_out); |
---|
[a405bc1] | 632 | fatalerror(img_error2msg(img_error()), fnm_out); |
---|
[6f185e5] | 633 | } |
---|
[cb3d1e2] | 634 | |
---|
[d1b1380] | 635 | return EXIT_SUCCESS; |
---|
| 636 | } |
---|
| 637 | |
---|
[d92d282] | 638 | static int adjust_direction(int dir, int by) { |
---|
| 639 | if (by == ESWAP) |
---|
| 640 | return dir ^ (ELEFT|ERIGHT); |
---|
| 641 | if (by) |
---|
| 642 | return by; |
---|
| 643 | return dir; |
---|
| 644 | } |
---|
| 645 | |
---|
[a420b49] | 646 | static void |
---|
[d92d282] | 647 | do_stn(point *p, double X, const char *prefix, int dir, int labOnly) |
---|
[a420b49] | 648 | { |
---|
| 649 | leg *l, *lp; |
---|
[badeec8] | 650 | double dX; |
---|
[ee11c6a7] | 651 | const stn *s; |
---|
[d92d282] | 652 | int odir = dir; |
---|
[ea1c342] | 653 | |
---|
| 654 | for (s = p->stns; s; s = s->next) { |
---|
[31b7105] | 655 | img_write_item(pimg_out, img_LABEL, s->flags, s->label, X, 0, p->p.z); |
---|
[ea1c342] | 656 | } |
---|
[d92d282] | 657 | if (labOnly || p->fBroken) { |
---|
| 658 | return; |
---|
| 659 | } |
---|
| 660 | |
---|
[7d9b3a9] | 661 | lp = &headleg; |
---|
[21c226e] | 662 | for (l = lp->next; l; lp = l, l = lp->next) { |
---|
[d92d282] | 663 | dir = odir; |
---|
[21c226e] | 664 | if (l->fDone) { |
---|
[d92d282] | 665 | /* this case happens if a recursive call causes the next leg to be |
---|
[21c226e] | 666 | * removed, leaving our next pointing to a leg which has been dealt |
---|
| 667 | * with... */ |
---|
[d92d282] | 668 | continue; |
---|
| 669 | } |
---|
| 670 | if (l->prefix != prefix) { |
---|
| 671 | continue; |
---|
| 672 | } |
---|
| 673 | if (l->to == p) { |
---|
| 674 | if (l->broken & BREAK_TO) continue; |
---|
| 675 | lp->next = l->next; |
---|
| 676 | /* adjust direction of extension if necessary */ |
---|
| 677 | dir = adjust_direction(dir, l->to->dir); |
---|
| 678 | dir = adjust_direction(dir, l->dir); |
---|
| 679 | |
---|
| 680 | dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y); |
---|
| 681 | if (dir == ELEFT) dX = -dX; |
---|
[31b7105] | 682 | img_write_item(pimg_out, img_MOVE, 0, NULL, X + dX, 0, l->fr->p.z); |
---|
| 683 | img_write_item(pimg_out, img_LINE, l->flags, l->prefix, |
---|
[d92d282] | 684 | X, 0, l->to->p.z); |
---|
| 685 | l->fDone = 1; |
---|
| 686 | do_stn(l->fr, X + dX, l->prefix, dir, (l->broken & BREAK_FR)); |
---|
| 687 | l = lp; |
---|
| 688 | } else if (l->fr == p) { |
---|
| 689 | if (l->broken & BREAK_FR) continue; |
---|
| 690 | lp->next = l->next; |
---|
| 691 | /* adjust direction of extension if necessary */ |
---|
| 692 | dir = adjust_direction(dir, l->fr->dir); |
---|
| 693 | dir = adjust_direction(dir, l->dir); |
---|
| 694 | |
---|
| 695 | dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y); |
---|
| 696 | if (dir == ELEFT) dX = -dX; |
---|
[31b7105] | 697 | img_write_item(pimg_out, img_MOVE, 0, NULL, X, 0, l->fr->p.z); |
---|
| 698 | img_write_item(pimg_out, img_LINE, l->flags, l->prefix, |
---|
[d92d282] | 699 | X + dX, 0, l->to->p.z); |
---|
| 700 | l->fDone = 1; |
---|
| 701 | do_stn(l->to, X + dX, l->prefix, dir, (l->broken & BREAK_TO)); |
---|
| 702 | l = lp; |
---|
[21c226e] | 703 | } |
---|
| 704 | } |
---|
| 705 | lp = &headleg; |
---|
[7d9b3a9] | 706 | for (l = lp->next; l; lp = l, l = lp->next) { |
---|
[d92d282] | 707 | dir = odir; |
---|
[7d9b3a9] | 708 | if (l->fDone) { |
---|
| 709 | /* this case happens iff a recursive call causes the next leg to be |
---|
| 710 | * removed, leaving our next pointing to a leg which has been dealt |
---|
| 711 | * with... */ |
---|
[d92d282] | 712 | continue; |
---|
| 713 | } |
---|
| 714 | if (l->to == p) { |
---|
| 715 | if (l->broken & BREAK_TO) continue; |
---|
| 716 | lp->next = l->next; |
---|
| 717 | /* adjust direction of extension if necessary */ |
---|
| 718 | dir = adjust_direction(dir, l->to->dir); |
---|
| 719 | dir = adjust_direction(dir, l->dir); |
---|
| 720 | |
---|
| 721 | dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y); |
---|
| 722 | if (dir == ELEFT) dX = -dX; |
---|
[31b7105] | 723 | img_write_item(pimg_out, img_MOVE, 0, NULL, X + dX, 0, l->fr->p.z); |
---|
| 724 | img_write_item(pimg_out, img_LINE, l->flags, l->prefix, |
---|
[d92d282] | 725 | X, 0, l->to->p.z); |
---|
| 726 | l->fDone = 1; |
---|
| 727 | do_stn(l->fr, X + dX, l->prefix, dir, (l->broken & BREAK_FR)); |
---|
| 728 | l = lp; |
---|
| 729 | } else if (l->fr == p) { |
---|
| 730 | if (l->broken & BREAK_FR) continue; |
---|
| 731 | lp->next = l->next; |
---|
| 732 | /* adjust direction of extension if necessary */ |
---|
| 733 | dir = adjust_direction(dir, l->fr->dir); |
---|
| 734 | dir = adjust_direction(dir, l->dir); |
---|
| 735 | |
---|
| 736 | dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y); |
---|
| 737 | if (dir == ELEFT) dX = -dX; |
---|
[31b7105] | 738 | img_write_item(pimg_out, img_MOVE, 0, NULL, X, 0, l->fr->p.z); |
---|
| 739 | img_write_item(pimg_out, img_LINE, l->flags, l->prefix, |
---|
[d92d282] | 740 | X + dX, 0, l->to->p.z); |
---|
| 741 | l->fDone = 1; |
---|
| 742 | do_stn(l->to, X + dX, l->prefix, dir, (l->broken & BREAK_TO)); |
---|
| 743 | l = lp; |
---|
[d1b1380] | 744 | } |
---|
| 745 | } |
---|
| 746 | } |
---|