[7ee2b42] | 1 | /* extend.c |
---|
[d1b1380] | 2 | * Produce an extended elevation |
---|
[736f7df] | 3 | * Copyright (C) 1995-2002,2005,2010,2011,2013,2014 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; |
---|
[736f7df] | 252 | /* TRANSLATORS: for extend: "extend" is starting to produce an extended elevation from station %s */ |
---|
[7926772] | 253 | printf(msg(/*Starting from station %s*/512),ln); |
---|
[d92d282] | 254 | putnl(); |
---|
| 255 | goto loopend; |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | } |
---|
[736f7df] | 259 | /* TRANSLATORS: for extend: the user specified breaking a loop or |
---|
| 260 | * changing extend direction at this station, but we didn’t find it in |
---|
| 261 | * the 3d file */ |
---|
[7926772] | 262 | warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ln); |
---|
[d92d282] | 263 | } else if (strcmp(ln, "*eleft")==0) { |
---|
| 264 | char *ll = delimword(lc, &lc); |
---|
[7926772] | 265 | if (*ll == 0) |
---|
| 266 | fatalerror_in_file(fnm, lineno, /*Expecting station name*/28); |
---|
[d92d282] | 267 | ln = delimword(lc, &lc); |
---|
| 268 | if (*ln == 0) { /* One argument, look for point to switch at. */ |
---|
| 269 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
| 270 | for (s = p->stns; s; s = s->next) { |
---|
| 271 | if (strcmp(s->label, ll)==0) { |
---|
[736f7df] | 272 | /* TRANSLATORS: for extend: */ |
---|
[7926772] | 273 | printf(msg(/*Extending to the left from station %s*/513), ll); |
---|
[d92d282] | 274 | putnl(); |
---|
| 275 | p->dir = ELEFT; |
---|
| 276 | goto loopend; |
---|
| 277 | } |
---|
| 278 | } |
---|
| 279 | } |
---|
[7926772] | 280 | warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll); |
---|
[d92d282] | 281 | } else { /* Two arguments look for a specific leg */ |
---|
| 282 | for (l = headleg.next; l; l=l->next) { |
---|
| 283 | point * fr = l->fr; |
---|
| 284 | point * to = l->to; |
---|
| 285 | if (fr && to) { |
---|
| 286 | for (s=fr->stns; s; s=s->next) { |
---|
| 287 | int b = 0; |
---|
| 288 | if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) { |
---|
| 289 | char * lr = (b ? ll : ln); |
---|
| 290 | for (t=to->stns; t; t=t->next) { |
---|
| 291 | if (strcmp(t->label,lr)==0) { |
---|
[736f7df] | 292 | /* TRANSLATORS: for extend: */ |
---|
[ee7511a] | 293 | printf(msg(/*Extending to the left from leg %s → %s*/515), s->label, t->label); |
---|
[d92d282] | 294 | putnl(); |
---|
| 295 | l->dir = ELEFT; |
---|
| 296 | goto loopend; |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | } |
---|
| 301 | } |
---|
| 302 | } |
---|
[736f7df] | 303 | /* TRANSLATORS: for extend: the user specified breaking a loop or |
---|
| 304 | * changing extend direction at this leg, but we didn’t find it in the |
---|
| 305 | * 3d file */ |
---|
[ee7511a] | 306 | warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln); |
---|
[d92d282] | 307 | } |
---|
| 308 | } else if (strcmp(ln, "*eright")==0) { |
---|
| 309 | char *ll = delimword(lc, &lc); |
---|
[7926772] | 310 | if (*ll == 0) |
---|
| 311 | fatalerror_in_file(fnm, lineno, /*Expecting station name*/28); |
---|
[d92d282] | 312 | ln = delimword(lc, &lc); |
---|
| 313 | if (*ln == 0) { /* One argument, look for point to switch at. */ |
---|
| 314 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
| 315 | for (s = p->stns; s; s = s->next) { |
---|
| 316 | if (strcmp(s->label, ll)==0) { |
---|
[736f7df] | 317 | /* TRANSLATORS: for extend: */ |
---|
[7926772] | 318 | printf(msg(/*Extending to the right from station %s*/514), ll); |
---|
[d92d282] | 319 | putnl(); |
---|
| 320 | p->dir = ERIGHT; |
---|
| 321 | goto loopend; |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | } |
---|
[7926772] | 325 | warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll); |
---|
[d92d282] | 326 | } else { /* Two arguments look for a specific leg */ |
---|
| 327 | for (l = headleg.next; l; l=l->next) { |
---|
| 328 | point * fr = l->fr; |
---|
| 329 | point * to = l->to; |
---|
| 330 | if (fr && to) { |
---|
| 331 | for (s=fr->stns; s; s=s->next) { |
---|
| 332 | int b = 0; |
---|
| 333 | if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) { |
---|
| 334 | char * lr = (b ? ll : ln); |
---|
| 335 | for (t=to->stns; t; t=t->next) { |
---|
| 336 | if (strcmp(t->label,lr)==0) { |
---|
[736f7df] | 337 | /* TRANSLATORS: for extend: */ |
---|
[ee7511a] | 338 | printf(msg(/*Extending to the right from leg %s → %s*/516), s->label, t->label); |
---|
[7926772] | 339 | putnl(); |
---|
[d92d282] | 340 | l->dir=ERIGHT; |
---|
| 341 | goto loopend; |
---|
| 342 | } |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | } |
---|
| 347 | } |
---|
[ee7511a] | 348 | warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln); |
---|
[d92d282] | 349 | } |
---|
| 350 | } else if (strcmp(ln, "*eswap")==0) { |
---|
| 351 | char *ll = delimword(lc, &lc); |
---|
[7926772] | 352 | if (*ll == 0) |
---|
| 353 | fatalerror_in_file(fnm, lineno, /*Expecting station name*/28); |
---|
[d92d282] | 354 | ln = delimword(lc, &lc); |
---|
| 355 | if (*ln == 0) { /* One argument, look for point to switch at. */ |
---|
| 356 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
| 357 | for (s = p->stns; s; s = s->next) { |
---|
| 358 | if (strcmp(s->label, ll)==0) { |
---|
[736f7df] | 359 | /* TRANSLATORS: for extend: */ |
---|
[7926772] | 360 | printf(msg(/*Swapping extend direction from station %s*/519),ll); |
---|
[d92d282] | 361 | putnl(); |
---|
| 362 | p->dir = ESWAP; |
---|
| 363 | goto loopend; |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | } |
---|
[7926772] | 367 | warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll); |
---|
[d92d282] | 368 | } else { /* Two arguments look for a specific leg */ |
---|
| 369 | for (l = headleg.next; l; l=l->next) { |
---|
| 370 | point * fr = l->fr; |
---|
| 371 | point * to = l->to; |
---|
| 372 | if (fr && to) { |
---|
| 373 | for (s=fr->stns; s; s=s->next) { |
---|
| 374 | int b = 0; |
---|
| 375 | if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) { |
---|
| 376 | char * lr = (b ? ll : ln); |
---|
| 377 | for (t=to->stns; t; t=t->next) { |
---|
| 378 | if (strcmp(t->label,lr)==0) { |
---|
[736f7df] | 379 | /* TRANSLATORS: for extend: */ |
---|
[ee7511a] | 380 | printf(msg(/*Swapping extend direction from leg %s → %s*/520), s->label, t->label); |
---|
[7926772] | 381 | putnl(); |
---|
[d92d282] | 382 | l->dir = ESWAP; |
---|
| 383 | goto loopend; |
---|
| 384 | } |
---|
| 385 | } |
---|
| 386 | } |
---|
| 387 | } |
---|
| 388 | } |
---|
| 389 | } |
---|
[ee7511a] | 390 | warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln); |
---|
[d92d282] | 391 | } |
---|
| 392 | } else if (strcmp(ln, "*break")==0) { |
---|
| 393 | char *ll = delimword(lc, &lc); |
---|
[7926772] | 394 | if (*ll == 0) |
---|
| 395 | fatalerror_in_file(fnm, lineno, /*Expecting station name*/28); |
---|
[d92d282] | 396 | ln = delimword(lc, &lc); |
---|
| 397 | if (*ln == 0) { /* One argument, look for point to break at. */ |
---|
| 398 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
| 399 | for (s = p->stns; s; s = s->next) { |
---|
| 400 | if (strcmp(s->label, ll)==0) { |
---|
[736f7df] | 401 | /* TRANSLATORS: for extend: */ |
---|
[7926772] | 402 | printf(msg(/*Breaking survey loop at station %s*/517), ll); |
---|
[d92d282] | 403 | putnl(); |
---|
| 404 | p->fBroken = 1; |
---|
| 405 | goto loopend; |
---|
| 406 | } |
---|
| 407 | } |
---|
| 408 | } |
---|
[7926772] | 409 | warning_in_file(fnm, lineno, /*Failed to find station %s*/510, ll); |
---|
[d92d282] | 410 | } else { /* Two arguments look for a specific leg */ |
---|
| 411 | for (l = headleg.next; l; l=l->next) { |
---|
| 412 | point * fr = l->fr; |
---|
| 413 | point * to = l->to; |
---|
| 414 | if (fr && to) { |
---|
| 415 | for (s=fr->stns; s; s=s->next) { |
---|
| 416 | int b = 0; |
---|
| 417 | if (strcmp(s->label,ll)==0 || (strcmp(s->label, ln)==0 && (b = 1)) ) { |
---|
| 418 | char * lr = (b ? ll : ln); |
---|
| 419 | for (t=to->stns; t; t=t->next) { |
---|
| 420 | if (strcmp(t->label,lr)==0) { |
---|
[736f7df] | 421 | /* TRANSLATORS: for extend: */ |
---|
[ee7511a] | 422 | printf(msg(/*Breaking survey loop at leg %s → %s*/518), s->label, t->label); |
---|
[d92d282] | 423 | putnl(); |
---|
| 424 | l->broken = (b ? BREAK_TO : BREAK_FR); |
---|
| 425 | goto loopend; |
---|
| 426 | } |
---|
| 427 | } |
---|
| 428 | } |
---|
| 429 | } |
---|
| 430 | } |
---|
| 431 | } |
---|
[ee7511a] | 432 | warning_in_file(fnm, lineno, /*Failed to find leg %s → %s*/511, ll, ln); |
---|
[d92d282] | 433 | } |
---|
| 434 | } else { |
---|
[0804fbe] | 435 | fatalerror_in_file(fnm, lineno, /*Unknown command “%s”*/12, ln); |
---|
[d92d282] | 436 | } |
---|
| 437 | loopend: |
---|
| 438 | ln = delimword(lc, &lc); |
---|
| 439 | if (*ln != 0) { |
---|
[7926772] | 440 | fatalerror_in_file(fnm, lineno, /*End of line not blank*/15); |
---|
| 441 | /* FIXME: give ln as context? */ |
---|
[d92d282] | 442 | } |
---|
| 443 | } |
---|
| 444 | |
---|
[acc20b1] | 445 | static const struct option long_opts[] = { |
---|
| 446 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
---|
[76bbb7c9] | 447 | {"survey", required_argument, 0, 's'}, |
---|
[d92d282] | 448 | {"specfile", required_argument, 0, 'p'}, |
---|
[acc20b1] | 449 | {"help", no_argument, 0, HLP_HELP}, |
---|
| 450 | {"version", no_argument, 0, HLP_VERSION}, |
---|
| 451 | {0, 0, 0, 0} |
---|
| 452 | }; |
---|
| 453 | |
---|
[d92d282] | 454 | #define short_opts "s:p:" |
---|
[acc20b1] | 455 | |
---|
| 456 | static struct help_msg help[] = { |
---|
| 457 | /* <-- */ |
---|
[45af761] | 458 | {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0}, |
---|
| 459 | {0, 0, 0} |
---|
[acc20b1] | 460 | }; |
---|
| 461 | |
---|
[a420b49] | 462 | int |
---|
| 463 | main(int argc, char **argv) |
---|
| 464 | { |
---|
[6f185e5] | 465 | const char *fnm_in, *fnm_out; |
---|
[a2ad284] | 466 | char *desc; |
---|
[0ed0e16] | 467 | img_point pt; |
---|
[d1b1380] | 468 | int result; |
---|
[647407d] | 469 | point *fr = NULL, *to; |
---|
[badeec8] | 470 | double zMax = -DBL_MAX; |
---|
[647407d] | 471 | point *p; |
---|
[76bbb7c9] | 472 | const char *survey = NULL; |
---|
[d92d282] | 473 | const char *specfile = NULL; |
---|
[31b7105] | 474 | img *pimg; |
---|
| 475 | int have_xsect = 0; |
---|
[d1b1380] | 476 | |
---|
[bdfe97f] | 477 | msg_init(argv); |
---|
[d1b1380] | 478 | |
---|
[736f7df] | 479 | /* TRANSLATORS: Part of extend --help */ |
---|
[d8dbdff] | 480 | cmdline_set_syntax_message(/*INPUT_3D_FILE [OUTPUT_3D_FILE]*/267, 0, NULL); |
---|
[acc20b1] | 481 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2); |
---|
[76bbb7c9] | 482 | while (1) { |
---|
| 483 | int opt = cmdline_getopt(); |
---|
| 484 | if (opt == EOF) break; |
---|
| 485 | if (opt == 's') survey = optarg; |
---|
[d92d282] | 486 | if (opt == 'p') specfile = optarg; |
---|
[acc20b1] | 487 | } |
---|
[6f185e5] | 488 | fnm_in = argv[optind++]; |
---|
[acc20b1] | 489 | if (argv[optind]) { |
---|
[6f185e5] | 490 | fnm_out = argv[optind]; |
---|
[acc20b1] | 491 | } else { |
---|
[6379f93] | 492 | char * base_in = base_from_fnm(fnm_in); |
---|
| 493 | char * base_out = osmalloc(strlen(base_in) + 8); |
---|
| 494 | strcpy(base_out, base_in); |
---|
| 495 | strcat(base_out, "_extend"); |
---|
| 496 | fnm_out = add_ext(base_out, EXT_SVX_3D); |
---|
| 497 | osfree(base_in); |
---|
| 498 | osfree(base_out); |
---|
[d1b1380] | 499 | } |
---|
| 500 | |
---|
| 501 | /* try to open image file, and check it has correct header */ |
---|
[a2ad284] | 502 | pimg = img_open_survey(fnm_in, survey); |
---|
[a405bc1] | 503 | if (pimg == NULL) fatalerror(img_error2msg(img_error()), fnm_in); |
---|
[d1b1380] | 504 | |
---|
| 505 | putnl(); |
---|
[ee7511a] | 506 | puts(msg(/*Reading in data - please wait…*/105)); |
---|
[d1b1380] | 507 | |
---|
[63dc4eb] | 508 | htab = osmalloc(ossizeof(pfx*) * HTAB_SIZE); |
---|
| 509 | { |
---|
| 510 | int i; |
---|
| 511 | for (i = 0; i < HTAB_SIZE; ++i) htab[i] = NULL; |
---|
| 512 | } |
---|
[421b7d2] | 513 | |
---|
[d1b1380] | 514 | do { |
---|
[23f7ea7] | 515 | result = img_read_item(pimg, &pt); |
---|
[d1b1380] | 516 | switch (result) { |
---|
[a420b49] | 517 | case img_MOVE: |
---|
[421b7d2] | 518 | fr = find_point(&pt); |
---|
| 519 | break; |
---|
[a420b49] | 520 | case img_LINE: |
---|
[421b7d2] | 521 | if (!fr) { |
---|
[76bbb7c9] | 522 | result = img_BAD; |
---|
| 523 | break; |
---|
[421b7d2] | 524 | } |
---|
| 525 | to = find_point(&pt); |
---|
| 526 | if (!(pimg->flags & (img_FLAG_SURFACE|img_FLAG_SPLAY))) |
---|
[693388e] | 527 | add_leg(fr, to, pimg->label, pimg->flags); |
---|
[421b7d2] | 528 | fr = to; |
---|
| 529 | break; |
---|
[a420b49] | 530 | case img_LABEL: |
---|
[388ad50] | 531 | to = find_point(&pt); |
---|
| 532 | add_label(to, pimg->label, pimg->flags); |
---|
[421b7d2] | 533 | break; |
---|
[ed0f5b6] | 534 | case img_BAD: |
---|
[6f185e5] | 535 | (void)img_close(pimg); |
---|
[a405bc1] | 536 | fatalerror(img_error2msg(img_error()), fnm_in); |
---|
[31b7105] | 537 | break; |
---|
| 538 | case img_XSECT: |
---|
| 539 | have_xsect = 1; |
---|
| 540 | break; |
---|
[d1b1380] | 541 | } |
---|
[ed0f5b6] | 542 | } while (result != img_STOP); |
---|
[d1b1380] | 543 | |
---|
[0c50ce3] | 544 | desc = osstrdup(pimg->title); |
---|
[a2ad284] | 545 | |
---|
[d92d282] | 546 | if (specfile) { |
---|
| 547 | FILE *fs = NULL; |
---|
[7926772] | 548 | char *fnm_used; |
---|
[736f7df] | 549 | /* TRANSLATORS: for extend: */ |
---|
[0804fbe] | 550 | printf(msg(/*Applying specfile: “%s”*/521), specfile); |
---|
[d92d282] | 551 | putnl(); |
---|
[7926772] | 552 | fs = fopenWithPthAndExt("", specfile, NULL, "r", &fnm_used); |
---|
[ffee37e] | 553 | if (fs == NULL) fatalerror(/*Couldn’t open file “%s”*/24, specfile); |
---|
[d92d282] | 554 | while (!feof(fs)) { |
---|
[7926772] | 555 | char *lbuf = getline_alloc(fs, 32); |
---|
[d92d282] | 556 | lineno++; |
---|
[7926772] | 557 | if (!lbuf) |
---|
| 558 | fatalerror_in_file(fnm_used, lineno, /*Error reading file*/18); |
---|
| 559 | parseconfigline(fnm_used, lbuf); |
---|
[d92d282] | 560 | osfree(lbuf); |
---|
[647407d] | 561 | } |
---|
[7926772] | 562 | osfree(fnm_used); |
---|
[647407d] | 563 | } |
---|
[d92d282] | 564 | |
---|
| 565 | if (start == NULL) { /* i.e. start wasn't specified in specfile */ |
---|
[8add4c9] | 566 | |
---|
[d92d282] | 567 | /* start at the highest entrance with some legs attached */ |
---|
[647407d] | 568 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
[d92d282] | 569 | if (p->order > 0 && p->p.z > zMax) { |
---|
| 570 | const stn *s; |
---|
| 571 | for (s = p->stns; s; s = s->next) { |
---|
| 572 | if (s->flags & img_SFLAG_ENTRANCE) { |
---|
| 573 | start = p; |
---|
| 574 | zMax = p->p.z; |
---|
| 575 | break; |
---|
| 576 | } |
---|
| 577 | } |
---|
[647407d] | 578 | } |
---|
| 579 | } |
---|
| 580 | if (start == NULL) { |
---|
[d92d282] | 581 | /* if no entrances with legs, start at the highest 1-node */ |
---|
[7ee2b42] | 582 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
[d92d282] | 583 | if (p->order == 1 && p->p.z > zMax) { |
---|
[7ee2b42] | 584 | start = p; |
---|
| 585 | zMax = p->p.z; |
---|
| 586 | } |
---|
| 587 | } |
---|
[d92d282] | 588 | /* of course we may have no 1-nodes... */ |
---|
[d216f5d] | 589 | if (start == NULL) { |
---|
| 590 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
[d92d282] | 591 | if (p->order != 0 && p->p.z > zMax) { |
---|
[d216f5d] | 592 | start = p; |
---|
| 593 | zMax = p->p.z; |
---|
| 594 | } |
---|
| 595 | } |
---|
[d92d282] | 596 | if (start == NULL) { |
---|
| 597 | /* There are no legs - just pick the highest station... */ |
---|
| 598 | for (p = headpoint.next; p != NULL; p = p->next) { |
---|
| 599 | if (p->p.z > zMax) { |
---|
| 600 | start = p; |
---|
| 601 | zMax = p->p.z; |
---|
| 602 | } |
---|
| 603 | } |
---|
| 604 | if (!start) fatalerror(/*No survey data*/43); |
---|
| 605 | } |
---|
[d216f5d] | 606 | } |
---|
[647407d] | 607 | } |
---|
| 608 | } |
---|
[d92d282] | 609 | |
---|
[736f7df] | 610 | /* TRANSLATORS: for extend: |
---|
| 611 | * Used to tell the user that a file is being written - %s is the filename |
---|
| 612 | */ |
---|
[ee7511a] | 613 | printf(msg(/*Writing %s…*/522), fnm_out); |
---|
[d92d282] | 614 | putnl(); |
---|
[0c50ce3] | 615 | pimg_out = img_open_write(fnm_out, desc, img_FFLAG_EXTENDED); |
---|
[d1b1380] | 616 | |
---|
[d92d282] | 617 | /* Only does single connected component currently. */ |
---|
| 618 | do_stn(start, 0.0, NULL, ERIGHT, 0); |
---|
[31b7105] | 619 | |
---|
| 620 | if (have_xsect) { |
---|
| 621 | img_rewind(pimg); |
---|
[2f1c0c0] | 622 | /* Read ahead on pimg before writing pimg_out so we find out if an |
---|
| 623 | * img_XSECT_END comes next. */ |
---|
| 624 | char * label = NULL; |
---|
| 625 | int flags = 0; |
---|
[31b7105] | 626 | do { |
---|
| 627 | result = img_read_item(pimg, &pt); |
---|
[2f1c0c0] | 628 | if (result == img_XSECT || result == img_XSECT_END) { |
---|
| 629 | if (label) { |
---|
| 630 | if (result == img_XSECT_END) |
---|
| 631 | flags |= img_XFLAG_END; |
---|
| 632 | img_write_item(pimg_out, img_XSECT, flags, label, 0, 0, 0); |
---|
| 633 | osfree(label); |
---|
| 634 | label = NULL; |
---|
| 635 | } |
---|
| 636 | } |
---|
[31b7105] | 637 | if (result == img_XSECT) { |
---|
[2f1c0c0] | 638 | label = osstrdup(pimg->label); |
---|
| 639 | flags = pimg->flags; |
---|
| 640 | pimg_out->l = pimg->l; |
---|
| 641 | pimg_out->r = pimg->r; |
---|
| 642 | pimg_out->u = pimg->u; |
---|
[31b7105] | 643 | pimg_out->d = pimg->d; |
---|
| 644 | } |
---|
| 645 | } while (result != img_STOP); |
---|
| 646 | } |
---|
[8add4c9] | 647 | |
---|
[31b7105] | 648 | (void)img_close(pimg); |
---|
| 649 | |
---|
| 650 | if (!img_close(pimg_out)) { |
---|
[6f185e5] | 651 | (void)remove(fnm_out); |
---|
[a405bc1] | 652 | fatalerror(img_error2msg(img_error()), fnm_out); |
---|
[6f185e5] | 653 | } |
---|
[cb3d1e2] | 654 | |
---|
[d1b1380] | 655 | return EXIT_SUCCESS; |
---|
| 656 | } |
---|
| 657 | |
---|
[d92d282] | 658 | static int adjust_direction(int dir, int by) { |
---|
| 659 | if (by == ESWAP) |
---|
| 660 | return dir ^ (ELEFT|ERIGHT); |
---|
| 661 | if (by) |
---|
| 662 | return by; |
---|
| 663 | return dir; |
---|
| 664 | } |
---|
| 665 | |
---|
[a420b49] | 666 | static void |
---|
[d92d282] | 667 | do_stn(point *p, double X, const char *prefix, int dir, int labOnly) |
---|
[a420b49] | 668 | { |
---|
| 669 | leg *l, *lp; |
---|
[badeec8] | 670 | double dX; |
---|
[ee11c6a7] | 671 | const stn *s; |
---|
[d92d282] | 672 | int odir = dir; |
---|
[ea1c342] | 673 | |
---|
| 674 | for (s = p->stns; s; s = s->next) { |
---|
[31b7105] | 675 | img_write_item(pimg_out, img_LABEL, s->flags, s->label, X, 0, p->p.z); |
---|
[ea1c342] | 676 | } |
---|
[d92d282] | 677 | if (labOnly || p->fBroken) { |
---|
| 678 | return; |
---|
| 679 | } |
---|
| 680 | |
---|
[7d9b3a9] | 681 | lp = &headleg; |
---|
[21c226e] | 682 | for (l = lp->next; l; lp = l, l = lp->next) { |
---|
[d92d282] | 683 | dir = odir; |
---|
[21c226e] | 684 | if (l->fDone) { |
---|
[d92d282] | 685 | /* this case happens if a recursive call causes the next leg to be |
---|
[21c226e] | 686 | * removed, leaving our next pointing to a leg which has been dealt |
---|
| 687 | * with... */ |
---|
[d92d282] | 688 | continue; |
---|
| 689 | } |
---|
| 690 | if (l->prefix != prefix) { |
---|
| 691 | continue; |
---|
| 692 | } |
---|
| 693 | if (l->to == p) { |
---|
| 694 | if (l->broken & BREAK_TO) continue; |
---|
| 695 | lp->next = l->next; |
---|
| 696 | /* adjust direction of extension if necessary */ |
---|
| 697 | dir = adjust_direction(dir, l->to->dir); |
---|
| 698 | dir = adjust_direction(dir, l->dir); |
---|
| 699 | |
---|
| 700 | dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y); |
---|
| 701 | if (dir == ELEFT) dX = -dX; |
---|
[31b7105] | 702 | img_write_item(pimg_out, img_MOVE, 0, NULL, X + dX, 0, l->fr->p.z); |
---|
| 703 | img_write_item(pimg_out, img_LINE, l->flags, l->prefix, |
---|
[d92d282] | 704 | X, 0, l->to->p.z); |
---|
| 705 | l->fDone = 1; |
---|
| 706 | do_stn(l->fr, X + dX, l->prefix, dir, (l->broken & BREAK_FR)); |
---|
| 707 | l = lp; |
---|
| 708 | } else if (l->fr == p) { |
---|
| 709 | if (l->broken & BREAK_FR) continue; |
---|
| 710 | lp->next = l->next; |
---|
| 711 | /* adjust direction of extension if necessary */ |
---|
| 712 | dir = adjust_direction(dir, l->fr->dir); |
---|
| 713 | dir = adjust_direction(dir, l->dir); |
---|
| 714 | |
---|
| 715 | dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y); |
---|
| 716 | if (dir == ELEFT) dX = -dX; |
---|
[31b7105] | 717 | img_write_item(pimg_out, img_MOVE, 0, NULL, X, 0, l->fr->p.z); |
---|
| 718 | img_write_item(pimg_out, img_LINE, l->flags, l->prefix, |
---|
[d92d282] | 719 | X + dX, 0, l->to->p.z); |
---|
| 720 | l->fDone = 1; |
---|
| 721 | do_stn(l->to, X + dX, l->prefix, dir, (l->broken & BREAK_TO)); |
---|
| 722 | l = lp; |
---|
[21c226e] | 723 | } |
---|
| 724 | } |
---|
| 725 | lp = &headleg; |
---|
[7d9b3a9] | 726 | for (l = lp->next; l; lp = l, l = lp->next) { |
---|
[d92d282] | 727 | dir = odir; |
---|
[7d9b3a9] | 728 | if (l->fDone) { |
---|
| 729 | /* this case happens iff a recursive call causes the next leg to be |
---|
| 730 | * removed, leaving our next pointing to a leg which has been dealt |
---|
| 731 | * with... */ |
---|
[d92d282] | 732 | continue; |
---|
| 733 | } |
---|
| 734 | if (l->to == p) { |
---|
| 735 | if (l->broken & BREAK_TO) continue; |
---|
| 736 | lp->next = l->next; |
---|
| 737 | /* adjust direction of extension if necessary */ |
---|
| 738 | dir = adjust_direction(dir, l->to->dir); |
---|
| 739 | dir = adjust_direction(dir, l->dir); |
---|
| 740 | |
---|
| 741 | dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y); |
---|
| 742 | if (dir == ELEFT) dX = -dX; |
---|
[31b7105] | 743 | img_write_item(pimg_out, img_MOVE, 0, NULL, X + dX, 0, l->fr->p.z); |
---|
| 744 | img_write_item(pimg_out, img_LINE, l->flags, l->prefix, |
---|
[d92d282] | 745 | X, 0, l->to->p.z); |
---|
| 746 | l->fDone = 1; |
---|
| 747 | do_stn(l->fr, X + dX, l->prefix, dir, (l->broken & BREAK_FR)); |
---|
| 748 | l = lp; |
---|
| 749 | } else if (l->fr == p) { |
---|
| 750 | if (l->broken & BREAK_FR) continue; |
---|
| 751 | lp->next = l->next; |
---|
| 752 | /* adjust direction of extension if necessary */ |
---|
| 753 | dir = adjust_direction(dir, l->fr->dir); |
---|
| 754 | dir = adjust_direction(dir, l->dir); |
---|
| 755 | |
---|
| 756 | dX = hypot(l->fr->p.x - l->to->p.x, l->fr->p.y - l->to->p.y); |
---|
| 757 | if (dir == ELEFT) dX = -dX; |
---|
[31b7105] | 758 | img_write_item(pimg_out, img_MOVE, 0, NULL, X, 0, l->fr->p.z); |
---|
| 759 | img_write_item(pimg_out, img_LINE, l->flags, l->prefix, |
---|
[d92d282] | 760 | X + dX, 0, l->to->p.z); |
---|
| 761 | l->fDone = 1; |
---|
| 762 | do_stn(l->to, X + dX, l->prefix, dir, (l->broken & BREAK_TO)); |
---|
| 763 | l = lp; |
---|
[d1b1380] | 764 | } |
---|
| 765 | } |
---|
| 766 | } |
---|