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