| 1 |
/* Copyright (c) 1990 Regents of the University of California */ |
| 2 |
|
| 3 |
#ifndef lint |
| 4 |
static char SCCSid[] = "$SunId$ LBL"; |
| 5 |
#endif |
| 6 |
|
| 7 |
/* |
| 8 |
* Convert Architrion file to Radiance |
| 9 |
* |
| 10 |
* Greg Ward |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <stdio.h> |
| 14 |
|
| 15 |
#include "trans.h" |
| 16 |
|
| 17 |
#define DEFMAPFILE "/usr/local/lib/ray/lib/arch.map" |
| 18 |
|
| 19 |
/* qualifiers */ |
| 20 |
#define Q_COL 0 |
| 21 |
#define Q_FAC 1 |
| 22 |
#define Q_LAY 2 |
| 23 |
#define Q_REF 3 |
| 24 |
#define NQUALS 4 |
| 25 |
|
| 26 |
char *qname[NQUALS] = { |
| 27 |
"Color", |
| 28 |
"Face", |
| 29 |
"Layer", |
| 30 |
"RefId", |
| 31 |
}; |
| 32 |
|
| 33 |
QLIST qlist = {NQUALS, qname}; |
| 34 |
/* face ids */ |
| 35 |
#define F_BOT 0 |
| 36 |
#define F_END 1 |
| 37 |
#define F_OPP 2 |
| 38 |
#define F_REF 3 |
| 39 |
#define F_SIL 4 |
| 40 |
#define F_TOP 5 |
| 41 |
#define NFACES 6 |
| 42 |
ID faceId[NFACES] = { |
| 43 |
{"bottom"},{"end"},{"opposite"},{"reference"},{"sill"},{"top"} |
| 44 |
}; |
| 45 |
/* valid qualifier ids */ |
| 46 |
IDLIST qual[NQUALS] = { |
| 47 |
{ 0, NULL }, |
| 48 |
{ NFACES, faceId }, |
| 49 |
{ 0, NULL }, |
| 50 |
{ 0, NULL }, |
| 51 |
}; |
| 52 |
/* mapping rules */ |
| 53 |
RULEHD *ourmap = NULL; |
| 54 |
/* file header */ |
| 55 |
struct header { |
| 56 |
char *filename; |
| 57 |
char *layer[9]; |
| 58 |
char length_u[16], area_u[16]; |
| 59 |
double length_f, area_f; |
| 60 |
int nblocks, nopenings; |
| 61 |
} fhead; |
| 62 |
/* block or opening geometry */ |
| 63 |
typedef struct { |
| 64 |
int x[4], y[4], z[4], h[4]; |
| 65 |
} PRISM; |
| 66 |
/* macros for x,y,z */ |
| 67 |
#define p_x(p,i) ((p)->x[(i)&3]) |
| 68 |
#define p_y(p,i) ((p)->y[(i)&3]) |
| 69 |
#define p_z(p,i) ((i)&4 ? (p)->h[(i)&3] : (p)->z[i]) |
| 70 |
/* opening */ |
| 71 |
typedef struct { |
| 72 |
PRISM p; |
| 73 |
int corner; |
| 74 |
int depth; |
| 75 |
ID frame; |
| 76 |
} OPNG; |
| 77 |
/* block */ |
| 78 |
typedef struct { |
| 79 |
short layer; |
| 80 |
short color; |
| 81 |
ID refid; |
| 82 |
PRISM p; |
| 83 |
int nopenings; |
| 84 |
OPNG *opening; |
| 85 |
} BLOCK; |
| 86 |
/* point format */ |
| 87 |
char ptfmt[] = "\t%12.9g %12.9g %12.9g\n"; |
| 88 |
/* antimatter modifier id for openings */ |
| 89 |
char openmod[] = "opening"; |
| 90 |
/* return flags for checkface() */ |
| 91 |
#define T1_OK 1 |
| 92 |
#define T2_OK 2 |
| 93 |
#define QL_OK 4 |
| 94 |
|
| 95 |
char *progname; /* argv[0] */ |
| 96 |
|
| 97 |
|
| 98 |
main(argc, argv) /* translate Architrion file */ |
| 99 |
int argc; |
| 100 |
char *argv[]; |
| 101 |
{ |
| 102 |
int donames = 0; /* -n flag, produce namelist */ |
| 103 |
int i; |
| 104 |
|
| 105 |
progname = argv[0]; |
| 106 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
| 107 |
switch (argv[i][1]) { |
| 108 |
case 'n': /* just produce name list */ |
| 109 |
donames++; |
| 110 |
break; |
| 111 |
case 'm': /* use custom mapfile */ |
| 112 |
ourmap = getmapping(argv[++i], &qlist); |
| 113 |
break; |
| 114 |
default: |
| 115 |
goto userr; |
| 116 |
} |
| 117 |
if (i < argc-1) |
| 118 |
goto userr; |
| 119 |
if (i == argc-1) |
| 120 |
if (freopen(argv[i], "r", stdin) == NULL) { |
| 121 |
fprintf(stderr, "%s: cannot open\n", argv[i]); |
| 122 |
exit(1); |
| 123 |
} |
| 124 |
getfhead(stdin); /* get Architrion header */ |
| 125 |
if (donames) { /* scan for ids */ |
| 126 |
arch2names(stdin); |
| 127 |
printf("filename \"%s\"\n", fhead.filename); |
| 128 |
printf("filetype \"Architrion\"\n"); |
| 129 |
write_quals(&qlist, qual, stdout); |
| 130 |
} else { /* translate file */ |
| 131 |
if (ourmap == NULL) |
| 132 |
ourmap = getmapping(DEFMAPFILE, &qlist); |
| 133 |
arch2rad(stdin, stdout); |
| 134 |
} |
| 135 |
return(0); |
| 136 |
userr: |
| 137 |
fprintf(stderr, "Usage: %s [-n][-m mapfile] [input]\n", argv[0]); |
| 138 |
exit(1); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
arch2rad(inp, out) /* translate Architrion file to Radiance */ |
| 143 |
FILE *inp, *out; |
| 144 |
{ |
| 145 |
int nbs, nos; |
| 146 |
BLOCK blk; |
| 147 |
|
| 148 |
puthead(out); |
| 149 |
nbs = nos = 0; |
| 150 |
while (getblock(&blk, inp) != EOF) { |
| 151 |
putblock(&blk, out); |
| 152 |
nbs++; |
| 153 |
nos += blk.nopenings; |
| 154 |
doneblock(&blk); |
| 155 |
} |
| 156 |
if (nbs != fhead.nblocks) |
| 157 |
fprintf(stderr, |
| 158 |
"%s: warning -- block count incorrect (%d != %d)\n", |
| 159 |
progname, nbs, fhead.nblocks); |
| 160 |
if (nos != fhead.nopenings) |
| 161 |
fprintf(stderr, |
| 162 |
"%s: warning -- opening count incorrect (%d != %d)\n", |
| 163 |
progname, nos, fhead.nopenings); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
arch2names(inp) /* get name list from an Architrion file */ |
| 168 |
FILE *inp; |
| 169 |
{ |
| 170 |
BLOCK blk; |
| 171 |
|
| 172 |
while (getblock(&blk, inp) != EOF) { |
| 173 |
if (ourmap == NULL || hasmatch(&blk, ourmap)) |
| 174 |
add2quals(&blk); |
| 175 |
doneblock(&blk); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
hasmatch(bp, mp) /* check for any match in rule list */ |
| 181 |
BLOCK *bp; |
| 182 |
RULEHD *mp; |
| 183 |
{ |
| 184 |
if (mp == NULL) |
| 185 |
return(0); |
| 186 |
if (hasmatch(bp, mp->next)) /* check in order -- more likely */ |
| 187 |
return(1); |
| 188 |
return(matchrule(bp, mp)); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
getfhead(fp) /* get file header */ |
| 193 |
FILE *fp; |
| 194 |
{ |
| 195 |
char buf[MAXSTR]; |
| 196 |
int i, n; |
| 197 |
/* get file name */ |
| 198 |
if (fgets(buf, MAXSTR, fp) == NULL) |
| 199 |
goto readerr; |
| 200 |
if ((n = strlen(buf)) < 1) |
| 201 |
goto readerr; |
| 202 |
buf[n-1] = '\0'; |
| 203 |
fhead.filename = savestr(buf); |
| 204 |
/* get layers */ |
| 205 |
fhead.layer[0] = "Worksheet"; |
| 206 |
for (i = 1; i <= 8; i++) { |
| 207 |
if (fscanf(fp, "Layer No.%d", &n) != 1 || n != i) |
| 208 |
goto readerr; |
| 209 |
while ((n = getc(fp)) != EOF && (n == ' ' || n == '\t')) |
| 210 |
; |
| 211 |
if (n == EOF) |
| 212 |
goto readerr; |
| 213 |
ungetc(n, fp); |
| 214 |
if (fgets(buf, MAXSTR, fp) == NULL) |
| 215 |
goto readerr; |
| 216 |
buf[strlen(buf)-1] = '\0'; |
| 217 |
if (buf[0]) |
| 218 |
fhead.layer[i] = savestr(buf); |
| 219 |
else |
| 220 |
fhead.layer[i] = NULL; |
| 221 |
} |
| 222 |
/* get units */ |
| 223 |
if (fgets(buf, MAXSTR, fp) == NULL) |
| 224 |
goto readerr; |
| 225 |
if (sscanf(buf, "0 %*f %s %*f %s\n", |
| 226 |
fhead.length_u, fhead.area_u) != 2) { |
| 227 |
fhead.length_u[0] = '\0'; |
| 228 |
fhead.area_u[0] = '\0'; |
| 229 |
} |
| 230 |
if (fgets(buf, MAXSTR, fp) == NULL || |
| 231 |
sscanf(buf, "0 %lf\n", &fhead.length_f) != 1) |
| 232 |
goto readerr; |
| 233 |
if (fgets(buf, MAXSTR, fp) == NULL || |
| 234 |
sscanf(buf, "0 %lf\n", &fhead.area_f) != 1) |
| 235 |
goto readerr; |
| 236 |
/* get number of blocks and openings */ |
| 237 |
if (fgets(buf, MAXSTR, fp) == NULL || |
| 238 |
sscanf(buf, "0 %d %d\n", &fhead.nblocks, |
| 239 |
&fhead.nopenings) != 2) |
| 240 |
goto readerr; |
| 241 |
return; |
| 242 |
readerr: |
| 243 |
fprintf(stderr, "%s: error reading Architrion header\n", progname); |
| 244 |
exit(1); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
puthead(fp) /* put out header information */ |
| 249 |
FILE *fp; |
| 250 |
{ |
| 251 |
register int i; |
| 252 |
|
| 253 |
fprintf(fp, "# File created by: %s\n", progname); |
| 254 |
fprintf(fp, "# Input file: %s\n", fhead.filename); |
| 255 |
fprintf(fp, "# Input units: %s\n", fhead.length_u); |
| 256 |
fprintf(fp, "# Output units: meters\n"); |
| 257 |
fprintf(fp, "# Named layers:\n"); |
| 258 |
for (i = 0; i < 8; i++) |
| 259 |
if (fhead.layer[i] != NULL) |
| 260 |
fprintf(fp, "#\tLayer No. %d\t%s\n", i, fhead.layer[i]); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
getblock(bp, fp) /* get an Architrion block */ |
| 265 |
register BLOCK *bp; |
| 266 |
FILE *fp; |
| 267 |
{ |
| 268 |
char word[32]; |
| 269 |
int i; |
| 270 |
|
| 271 |
if (fgets(word, sizeof(word), fp) == NULL) |
| 272 |
return(EOF); |
| 273 |
if (strncmp(word, "Block", 5)) |
| 274 |
return(EOF); |
| 275 |
if (fscanf(fp, "1 %hd %hd %d", &bp->layer, |
| 276 |
&bp->color, &bp->nopenings) != 3) |
| 277 |
return(EOF); |
| 278 |
if (fgetid(&bp->refid, "\n", fp) == EOF) |
| 279 |
return(EOF); |
| 280 |
if (fscanf(fp, "1 %d %d %d %d %d %d %d %d\n", |
| 281 |
&bp->p.x[0], &bp->p.y[0], &bp->p.z[0], &bp->p.h[0], |
| 282 |
&bp->p.x[1], &bp->p.y[1], &bp->p.z[1], &bp->p.h[1]) != 8) |
| 283 |
return(EOF); |
| 284 |
if (fscanf(fp, "1 %d %d %d %d %d %d %d %d\n", |
| 285 |
&bp->p.x[2], &bp->p.y[2], &bp->p.z[2], &bp->p.h[2], |
| 286 |
&bp->p.x[3], &bp->p.y[3], &bp->p.z[3], &bp->p.h[3]) != 8) |
| 287 |
return(EOF); |
| 288 |
if (bp->nopenings == 0) { |
| 289 |
bp->opening = NULL; |
| 290 |
return(0); |
| 291 |
} |
| 292 |
bp->opening = (OPNG *)malloc(bp->nopenings*sizeof(OPNG)); |
| 293 |
if (bp->opening == NULL) |
| 294 |
goto memerr; |
| 295 |
for (i = 0; i < bp->nopenings; i++) |
| 296 |
if (getopening(&bp->opening[i], fp) < 0) |
| 297 |
return(EOF); |
| 298 |
return(0); |
| 299 |
memerr: |
| 300 |
fprintf(stderr, "%s: out of memory in getblock\n", progname); |
| 301 |
exit(1); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
getopening(op, fp) /* read in opening from fp */ |
| 306 |
register OPNG *op; |
| 307 |
FILE *fp; |
| 308 |
{ |
| 309 |
register int c; |
| 310 |
char word[32]; |
| 311 |
|
| 312 |
if (fgets(word, sizeof(word), fp) == NULL) |
| 313 |
return(EOF); |
| 314 |
if (strncmp(word, "Opening", 7)) |
| 315 |
return(EOF); |
| 316 |
if (fscanf(fp, "2 %d %d %d %d %d %d %d %d\n", |
| 317 |
&op->p.x[0], &op->p.y[0], |
| 318 |
&op->p.z[0], &op->p.h[0], |
| 319 |
&op->p.x[1], &op->p.y[1], |
| 320 |
&op->p.z[1], &op->p.h[1]) != 8) |
| 321 |
return(EOF); |
| 322 |
if (fscanf(fp, "2 %d %d %d %d %d %d %d %d\n", |
| 323 |
&op->p.x[2], &op->p.y[2], |
| 324 |
&op->p.z[2], &op->p.h[2], |
| 325 |
&op->p.x[3], &op->p.y[3], |
| 326 |
&op->p.z[3], &op->p.h[3]) != 8) |
| 327 |
return(EOF); |
| 328 |
c = getc(fp); |
| 329 |
if (c == '3') { |
| 330 |
if (fscanf(fp, "%d %d", &op->corner, &op->depth) != 2) |
| 331 |
return(EOF); |
| 332 |
if (fgetid(&op->frame, "\n", fp) == EOF) |
| 333 |
return(EOF); |
| 334 |
} else { |
| 335 |
op->corner = -1; |
| 336 |
op->frame.name = NULL; |
| 337 |
if (c != EOF) |
| 338 |
ungetc(c, fp); |
| 339 |
} |
| 340 |
return(0); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
doneblock(bp) /* free data associated with bp */ |
| 345 |
register BLOCK *bp; |
| 346 |
{ |
| 347 |
register int i; |
| 348 |
|
| 349 |
if (bp->nopenings > 0) { |
| 350 |
for (i = 0; i < bp->nopenings; i++) |
| 351 |
doneid(&bp->opening[i].frame); |
| 352 |
free((char *)bp->opening); |
| 353 |
} |
| 354 |
doneid(&bp->refid); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
add2quals(bp) /* add to qualifier lists */ |
| 359 |
register BLOCK *bp; |
| 360 |
{ |
| 361 |
ID tmpid; |
| 362 |
|
| 363 |
if (fhead.layer[bp->layer] == NULL) { |
| 364 |
tmpid.name = NULL; |
| 365 |
tmpid.number = bp->layer; |
| 366 |
} else { |
| 367 |
tmpid.name = fhead.layer[bp->layer]; |
| 368 |
tmpid.number = 0; |
| 369 |
} |
| 370 |
findid(&qual[Q_LAY], &tmpid, 1); |
| 371 |
tmpid.name = NULL; |
| 372 |
tmpid.number = bp->color; |
| 373 |
findid(&qual[Q_COL], &tmpid, 1); |
| 374 |
findid(&qual[Q_REF], &bp->refid, 1); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
putblock(bp, fp) /* put out a block */ |
| 379 |
BLOCK *bp; |
| 380 |
FILE *fp; |
| 381 |
{ |
| 382 |
RULEHD *rp; |
| 383 |
char *mod[NFACES], *sillmod; |
| 384 |
int nmods, i; |
| 385 |
int donematch, newmatch; |
| 386 |
|
| 387 |
nmods = 0; sillmod = NULL; |
| 388 |
donematch = 0; |
| 389 |
for (rp = ourmap; rp != NULL; rp = rp->next) { |
| 390 |
newmatch = matchrule(bp, rp); /* test this rule */ |
| 391 |
newmatch &= ~donematch; /* don't repeat */ |
| 392 |
if (newmatch == 0) |
| 393 |
continue; |
| 394 |
mod[nmods++] = rp->mnam; |
| 395 |
if (sillmod == NULL && newmatch&F_SIL) |
| 396 |
sillmod = rp->mnam; |
| 397 |
putfaces(rp->mnam, bp, newmatch, fp); /* put out new faces */ |
| 398 |
donematch |= newmatch; |
| 399 |
if (donematch == (1<<NFACES)-1) |
| 400 |
break; /* done all faces */ |
| 401 |
} |
| 402 |
/* put out openings */ |
| 403 |
if (donematch && bp->nopenings > 0) { |
| 404 |
putopenmod(sillmod, mod, nmods, fp); |
| 405 |
for (i = 0; i < bp->nopenings; i++) |
| 406 |
putopening(&bp->opening[i], fp); |
| 407 |
} |
| 408 |
if (ferror(fp)) { |
| 409 |
fprintf(stderr, "%s: write error in putblock\n", progname); |
| 410 |
exit(1); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
putopenmod(sm, ml, nm, fp) /* put out opening modifier */ |
| 416 |
char *sm, *ml[]; |
| 417 |
int nm; |
| 418 |
FILE *fp; |
| 419 |
{ |
| 420 |
int rept, nrepts; |
| 421 |
register int i, j; |
| 422 |
|
| 423 |
if (sm == NULL) { /* if no sill modifier, use list */ |
| 424 |
sm = *ml++; |
| 425 |
nm--; |
| 426 |
} |
| 427 |
/* check for repeats */ |
| 428 |
rept = 0; nrepts = 0; |
| 429 |
for (i = 0; i < nm; i++) { |
| 430 |
if (ml[i] == sm) { |
| 431 |
rept |= 1<<i; |
| 432 |
nrepts++; |
| 433 |
} |
| 434 |
for (j = 0; j < i; j++) |
| 435 |
if (ml[j] == ml[i]) { |
| 436 |
rept |= 1<<j; |
| 437 |
nrepts++; |
| 438 |
} |
| 439 |
} |
| 440 |
/* print antimatter and modlist */ |
| 441 |
fprintf(fp, "\nvoid antimatter %s\n", openmod); |
| 442 |
fprintf(fp, "%d %s", 1+nm-nrepts, sm); |
| 443 |
for (i = 0; i < nm; i++) |
| 444 |
if (!(rept & 1<<i)) |
| 445 |
fprintf(fp, " %s", ml[i]); |
| 446 |
fprintf(fp, "\n0\n0\n"); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
putopening(op, fp) /* put out an opening */ |
| 451 |
OPNG *op; |
| 452 |
FILE *fp; |
| 453 |
{ |
| 454 |
static int nopens = 0; |
| 455 |
char buf[32]; |
| 456 |
register PRISM *p = &op->p; |
| 457 |
PRISM newp; |
| 458 |
register int i; |
| 459 |
/* copy original prism */ |
| 460 |
for (i = 0; i < 4; i++) { |
| 461 |
newp.x[i] = p->x[i]; |
| 462 |
newp.y[i] = p->y[i]; |
| 463 |
newp.z[i] = p->z[i]; |
| 464 |
newp.h[i] = p->h[i]; |
| 465 |
} |
| 466 |
/* spread reference and opposite */ |
| 467 |
if (p->x[1] > p->x[0]) { |
| 468 |
newp.x[0] -= 2; |
| 469 |
newp.x[1] += 2; |
| 470 |
newp.x[2] += 2; |
| 471 |
newp.x[3] -= 2; |
| 472 |
} else if (p->x[0] > p->x[1]) { |
| 473 |
newp.x[0] += 2; |
| 474 |
newp.x[1] -= 2; |
| 475 |
newp.x[2] -= 2; |
| 476 |
newp.x[3] += 2; |
| 477 |
} |
| 478 |
if (p->y[1] > p->y[0]) { |
| 479 |
newp.y[0] -= 2; |
| 480 |
newp.y[1] += 2; |
| 481 |
newp.y[2] += 2; |
| 482 |
newp.y[3] -= 2; |
| 483 |
} else if (p->y[0] > p->y[1]) { |
| 484 |
newp.y[0] += 2; |
| 485 |
newp.y[1] -= 2; |
| 486 |
newp.y[2] -= 2; |
| 487 |
newp.y[3] += 2; |
| 488 |
} |
| 489 |
/* put out faces */ |
| 490 |
sprintf(buf, "op%d", ++nopens); |
| 491 |
putface(openmod, buf, "ref", &newp, 3, 7, 4, 0, fp); |
| 492 |
putface(openmod, buf, "opp", &newp, 5, 6, 2, 1, fp); |
| 493 |
putface(openmod, buf, "end1", &newp, 2, 6, 7, 3, fp); |
| 494 |
putface(openmod, buf, "end2", &newp, 4, 5, 1, 0, fp); |
| 495 |
putface(openmod, buf, "bot", &newp, 1, 2, 3, 0, fp); |
| 496 |
putface(openmod, buf, "top", &newp, 7, 6, 5, 4, fp); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
int |
| 501 |
matchrule(bp, rp) /* see if block matches this rule */ |
| 502 |
register BLOCK *bp; |
| 503 |
register RULEHD *rp; |
| 504 |
{ |
| 505 |
register int i; |
| 506 |
ID tmpid; |
| 507 |
|
| 508 |
if (rp->qflg & FL(Q_LAY)) { /* check layer */ |
| 509 |
tmpid.name = NULL; |
| 510 |
tmpid.number = bp->layer; |
| 511 |
if (!matchid(&tmpid, &idm(rp)[Q_LAY])) |
| 512 |
return(0); |
| 513 |
} |
| 514 |
if (rp->qflg & FL(Q_COL)) { /* check color */ |
| 515 |
tmpid.name = NULL; |
| 516 |
tmpid.number = bp->color; |
| 517 |
if (!matchid(&tmpid, &idm(rp)[Q_COL])) |
| 518 |
return(0); |
| 519 |
} |
| 520 |
if (rp->qflg & FL(Q_REF)) { /* check reference id */ |
| 521 |
if (!matchid(&bp->refid, &idm(rp)[Q_REF])) |
| 522 |
return(0); |
| 523 |
} |
| 524 |
if (rp->qflg & FL(Q_FAC)) { /* check faces */ |
| 525 |
for (i = 0; i < NFACES; i++) |
| 526 |
if (matchid(&faceId[i], &idm(rp)[Q_FAC])) |
| 527 |
return(1<<i); /* match single face */ |
| 528 |
return(0); |
| 529 |
} |
| 530 |
return((1<<NFACES)-1); /* matched all faces */ |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
putfaces(m, bp, ff, fp) /* put out faces */ |
| 535 |
char *m; |
| 536 |
BLOCK *bp; |
| 537 |
register int ff; |
| 538 |
FILE *fp; |
| 539 |
{ |
| 540 |
char *blkname(), *bn; |
| 541 |
|
| 542 |
if (!strcmp(m, VOIDID)) |
| 543 |
return; |
| 544 |
bn = blkname(bp); |
| 545 |
if (ff & 1<<F_REF) |
| 546 |
putface(m, bn, "ref", &bp->p, 3, 7, 4, 0, fp); |
| 547 |
if (ff & 1<<F_OPP) |
| 548 |
putface(m, bn, "opp", &bp->p, 5, 6, 2, 1, fp); |
| 549 |
if (ff & 1<<F_END) { |
| 550 |
putface(m, bn, "end1", &bp->p, 2, 6, 7, 3, fp); |
| 551 |
putface(m, bn, "end2", &bp->p, 4, 5, 1, 0, fp); |
| 552 |
} |
| 553 |
if (ff & 1<<F_BOT) |
| 554 |
putface(m, bn, "bot", &bp->p, 1, 2, 3, 0, fp); |
| 555 |
if (ff & 1<<F_TOP) |
| 556 |
putface(m, bn, "top", &bp->p, 7, 6, 5, 4, fp); |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
char * |
| 561 |
blkname(bp) /* think up a good name for this block */ |
| 562 |
register BLOCK *bp; |
| 563 |
{ |
| 564 |
static char nambuf[32]; |
| 565 |
static int blkcnt = 0; |
| 566 |
register char *nam; |
| 567 |
register int i; |
| 568 |
|
| 569 |
nam = bp->refid.name; |
| 570 |
if (nam == NULL) |
| 571 |
nam = fhead.layer[bp->layer]; |
| 572 |
if (nam == NULL) { |
| 573 |
sprintf(nambuf, "l%d.", bp->layer); |
| 574 |
i = strlen(nambuf); |
| 575 |
} else { |
| 576 |
for (i = 0; i < 12 && nam[i]; i++) { |
| 577 |
if (nam[i] == ' ' || nam[i] == '\t') |
| 578 |
nambuf[i] = '_'; |
| 579 |
else |
| 580 |
nambuf[i] = nam[i]; |
| 581 |
} |
| 582 |
nambuf[i++] = '.'; |
| 583 |
} |
| 584 |
if (bp->refid.number != 0) { |
| 585 |
sprintf(nambuf+i, "r%d.", bp->refid.number); |
| 586 |
i = strlen(nambuf); |
| 587 |
} |
| 588 |
sprintf(nambuf+i, "b%d", ++blkcnt); |
| 589 |
return(nambuf); |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
putface(m, bn, fn, p, a, b, c, d, fp) /* put out a face */ |
| 594 |
char *m; |
| 595 |
char *bn, *fn; |
| 596 |
PRISM *p; |
| 597 |
int a, b, c, d; |
| 598 |
FILE *fp; |
| 599 |
{ |
| 600 |
int cf; |
| 601 |
|
| 602 |
cf = checkface(p, a, b, c, d); |
| 603 |
if (cf & QL_OK) { |
| 604 |
fprintf(fp, "\n%s polygon %s.%s\n", m, bn, fn); |
| 605 |
fprintf(fp, "0\n0\n12\n"); |
| 606 |
putpoint(p, a, fp); |
| 607 |
putpoint(p, b, fp); |
| 608 |
putpoint(p, c, fp); |
| 609 |
putpoint(p, d, fp); |
| 610 |
return; |
| 611 |
} |
| 612 |
if (cf & T1_OK) { |
| 613 |
fprintf(fp, "\n%s polygon %s.%sA\n", m, bn, fn); |
| 614 |
fprintf(fp, "0\n0\n9\n"); |
| 615 |
putpoint(p, d, fp); |
| 616 |
putpoint(p, a, fp); |
| 617 |
putpoint(p, b, fp); |
| 618 |
} |
| 619 |
if (cf & T2_OK) { |
| 620 |
fprintf(fp, "\n%s polygon %s.%sB\n", m, bn, fn); |
| 621 |
fprintf(fp, "0\n0\n9\n"); |
| 622 |
putpoint(p, b, fp); |
| 623 |
putpoint(p, c, fp); |
| 624 |
putpoint(p, d, fp); |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
#define ldot(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]) |
| 630 |
|
| 631 |
|
| 632 |
long |
| 633 |
lcross(vr, v1, v2) /* compute cross product */ |
| 634 |
register long vr[3], v1[3], v2[3]; |
| 635 |
{ |
| 636 |
vr[0] = v1[1]*v2[2] - v1[2]*v2[1]; |
| 637 |
vr[1] = v1[2]*v2[0] - v1[0]*v2[2]; |
| 638 |
vr[2] = v1[0]*v2[1] - v1[1]*v2[0]; |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
#define labs(a) ((a)>0 ? (a) : -(a)) |
| 643 |
|
| 644 |
|
| 645 |
checkface(p, a, b, c, d) /* check a face for validity */ |
| 646 |
register PRISM *p; |
| 647 |
int a, b, c, d; |
| 648 |
{ |
| 649 |
int rval = 0; |
| 650 |
long lt; |
| 651 |
long vc1[3], vc2[3], vt1[3], vt2[3]; |
| 652 |
long vc1l, vc2l, vc1vc2; |
| 653 |
/* check first triangle */ |
| 654 |
vt1[0] = p_x(p,b) - p_x(p,a); |
| 655 |
vt1[1] = p_y(p,b) - p_y(p,a); |
| 656 |
vt1[2] = p_z(p,b) - p_z(p,a); |
| 657 |
vt2[0] = p_x(p,d) - p_x(p,a); |
| 658 |
vt2[1] = p_y(p,d) - p_y(p,a); |
| 659 |
vt2[2] = p_z(p,d) - p_z(p,a); |
| 660 |
lcross(vc1, vt1, vt2); |
| 661 |
lt = labs(vc1[0]) + labs(vc1[1]) + labs(vc1[2]); |
| 662 |
if (lt > 1L<<18) lt = 16; |
| 663 |
else if (lt > 1L<<12) lt = 8; |
| 664 |
else lt = 0; |
| 665 |
if (lt) { vc1[0] >>= lt; vc1[1] >>= lt; vc1[2] >>= lt; } |
| 666 |
vc1l = ldot(vc1,vc1); |
| 667 |
if (vc1l > 4) |
| 668 |
rval |= T1_OK; |
| 669 |
/* check second triangle */ |
| 670 |
vt1[0] = p_x(p,d) - p_x(p,c); |
| 671 |
vt1[1] = p_y(p,d) - p_y(p,c); |
| 672 |
vt1[2] = p_z(p,d) - p_z(p,c); |
| 673 |
vt2[0] = p_x(p,b) - p_x(p,c); |
| 674 |
vt2[1] = p_y(p,b) - p_y(p,c); |
| 675 |
vt2[2] = p_z(p,b) - p_z(p,c); |
| 676 |
lcross(vc2, vt1, vt2); |
| 677 |
lt = labs(vc2[0]) + labs(vc2[1]) + labs(vc2[2]); |
| 678 |
if (lt > 1L<<18) lt = 16; |
| 679 |
else if (lt > 1L<<12) lt = 8; |
| 680 |
else lt = 0; |
| 681 |
if (lt) { vc2[0] >>= lt; vc2[1] >>= lt; vc2[2] >>= lt; } |
| 682 |
vc2l = ldot(vc2,vc2); |
| 683 |
if (vc2l > 4) |
| 684 |
rval |= T2_OK; |
| 685 |
/* check quadrilateral */ |
| 686 |
if (rval == (T1_OK|T2_OK)) { |
| 687 |
vc1vc2 = ldot(vc1,vc2); |
| 688 |
if (vc1vc2*vc1vc2 >= vc1l*vc2l-8) |
| 689 |
rval |= QL_OK; |
| 690 |
} |
| 691 |
return(rval); |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
putpoint(p, n, fp) /* put out a point */ |
| 696 |
register PRISM *p; |
| 697 |
int n; |
| 698 |
FILE *fp; |
| 699 |
{ |
| 700 |
register int i = n&3; |
| 701 |
|
| 702 |
fprintf(fp, ptfmt, p->x[i]*fhead.length_f, p->y[i]*fhead.length_f, |
| 703 |
(n&4 ? p->h[i] : p->z[i])*fhead.length_f); |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
eputs(s) |
| 708 |
char *s; |
| 709 |
{ |
| 710 |
fputs(s, stderr); |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
quit(code) |
| 715 |
int code; |
| 716 |
{ |
| 717 |
exit(code); |
| 718 |
} |