Back to index

radiance  4R0+20100331
arch2rad.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: arch2rad.c,v 2.3 2003/11/15 17:54:06 schorsch Exp $";
00003 #endif
00004 /*
00005  * Convert Architrion file to Radiance
00006  *
00007  *     Greg Ward
00008  */
00009 
00010 #include <stdlib.h>
00011 #include <stdio.h>
00012 #include <string.h>
00013 
00014 #include "rtio.h"
00015 #include "trans.h"
00016 
00017 #define DEFMAPFILE   "/usr/local/lib/ray/lib/arch.map"
00018 
00019                             /* qualifiers */
00020 #define Q_COL        0
00021 #define Q_FAC        1
00022 #define Q_LAY        2
00023 #define Q_REF        3
00024 #define NQUALS              4
00025 
00026 char   *qname[NQUALS] = {
00027        "Color",
00028        "Face",
00029        "Layer",
00030        "RefId",
00031 };
00032 
00033 QLIST  qlist = {NQUALS, qname};
00034                             /* face ids */
00035 #define F_BOT        0
00036 #define F_END        1
00037 #define F_OPP        2
00038 #define F_REF        3
00039 #define F_SIL        4
00040 #define F_TOP        5
00041 #define NFACES              6
00042 ID     faceId[NFACES] = {
00043        {"bottom"},{"end"},{"opposite"},{"reference"},{"sill"},{"top"}
00044 };
00045                             /* valid qualifier ids */
00046 IDLIST qual[NQUALS] = {
00047        { 0, NULL },
00048        { NFACES, faceId },
00049        { 0, NULL },
00050        { 0, NULL },
00051 };
00052                             /* mapping rules */
00053 RULEHD *ourmap = NULL;
00054                             /* file header */
00055 struct header {
00056        char   *filename;
00057        char   *layer[9];
00058        char   length_u[16], area_u[16];
00059        double length_f, area_f;
00060        int    nblocks, nopenings;
00061 } fhead;
00062                             /* block or opening geometry */
00063 typedef struct {
00064        int    x[4], y[4], z[4], h[4];
00065 } PRISM;
00066                             /* macros for x,y,z */
00067 #define p_x(p,i)     ((p)->x[(i)&3])
00068 #define p_y(p,i)     ((p)->y[(i)&3])
00069 #define p_z(p,i)     ((i)&4 ? (p)->h[(i)&3] : (p)->z[i])
00070                             /* opening */
00071 typedef struct {
00072        PRISM  p;
00073        int    corner;
00074        int    depth;
00075        ID     frame;
00076 } OPNG;
00077                             /* block */
00078 typedef struct {
00079        short  layer;
00080        short  color;
00081        ID     refid;
00082        PRISM  p;
00083        int    nopenings;
00084        OPNG   *opening;
00085 } BLOCK;
00086                             /* point format */
00087 char   ptfmt[] = "\t%12.9g %12.9g %12.9g\n";
00088                             /* antimatter modifier id for openings */
00089 char   openmod[] = "opening";
00090                             /* return flags for checkface() */
00091 #define T1_OK        1
00092 #define T2_OK        2
00093 #define QL_OK        4
00094 
00095 char   *progname;           /* argv[0] */
00096 
00097 void arch2rad(FILE   *inp, FILE    *out);
00098 void arch2names(FILE *inp);
00099 
00100 int hasmatch(BLOCK   *bp, RULEHD   *mp);
00101 
00102 void getfhead(FILE   *fp);
00103 void puthead(FILE    *fp);
00104 
00105 int getblock(BLOCK   *bp, FILE     *fp);
00106 void putblock(BLOCK  *bp, FILE     *fp);
00107 void doneblock(BLOCK *bp);
00108 
00109 int getopening(OPNG  *op, FILE     *fp);
00110 void add2quals(BLOCK *bp);
00111 int matchrule(BLOCK  *bp, RULEHD   *rp);
00112 
00113 void putopenmod(char *sm, char     *ml[], int    nm, FILE      *fp);
00114 void putface(char    *m, char      *bn, char     *fn, PRISM    *p,
00115        int    a, int b, int c, int d, FILE       *fp);
00116 void putfaces(char   *m, BLOCK     *bp, int      ff, FILE      *fp);
00117 void putpoint(PRISM  *p, int       n, FILE       *fp);
00118 void putopening(OPNG *op, FILE     *fp);
00119 
00120 int checkface(PRISM  *p, int       a, int b, int c, int d);
00121 
00122 void lcross(long     vr[3], long   v1[3], long   v2[3]);
00123 
00124 
00125 int
00126 main(         /* translate Architrion file */
00127        int    argc,
00128        char   *argv[]
00129 )
00130 {
00131        int    donames = 0;         /* -n flag, produce namelist */
00132        int    i;
00133        
00134        progname = argv[0];
00135        for (i = 1; i < argc && argv[i][0] == '-'; i++)
00136               switch (argv[i][1]) {
00137               case 'n':            /* just produce name list */
00138                      donames++;
00139                      break;
00140               case 'm':            /* use custom mapfile */
00141                      ourmap = getmapping(argv[++i], &qlist);
00142                      break;
00143               default:
00144                      goto userr;
00145               }
00146        if (i < argc-1)
00147               goto userr;
00148        if (i == argc-1)
00149               if (freopen(argv[i], "r", stdin) == NULL) {
00150                      fprintf(stderr, "%s: cannot open\n", argv[i]);
00151                      exit(1);
00152               }
00153        getfhead(stdin);            /* get Architrion header */
00154        if (donames) {                            /* scan for ids */
00155               arch2names(stdin);
00156               printf("filename \"%s\"\n", fhead.filename);
00157               printf("filetype \"Architrion\"\n");
00158               write_quals(&qlist, qual, stdout);
00159        } else {                           /* translate file */
00160               if (ourmap == NULL)
00161                      ourmap = getmapping(DEFMAPFILE, &qlist);
00162               arch2rad(stdin, stdout);
00163        }
00164        return(0);
00165 userr:
00166        fprintf(stderr, "Usage: %s [-n][-m mapfile] [input]\n", argv[0]);
00167        exit(1);
00168 }
00169 
00170 
00171 void
00172 arch2rad(     /* translate Architrion file to Radiance */
00173        FILE   *inp,
00174        FILE   *out
00175 )
00176 {
00177        int    nbs, nos;
00178        BLOCK  blk;
00179        
00180        puthead(out);
00181        nbs = nos = 0;
00182        while (getblock(&blk, inp) != EOF) {
00183               putblock(&blk, out);
00184               nbs++;
00185               nos += blk.nopenings;
00186               doneblock(&blk);
00187        }
00188        if (nbs != fhead.nblocks)
00189               fprintf(stderr,
00190               "%s: warning -- block count incorrect (%d != %d)\n",
00191                             progname, nbs, fhead.nblocks);
00192        if (nos != fhead.nopenings)
00193               fprintf(stderr,
00194               "%s: warning -- opening count incorrect (%d != %d)\n",
00195                             progname, nos, fhead.nopenings);
00196 }
00197 
00198 
00199 void
00200 arch2names(          /* get name list from an Architrion file */
00201        FILE   *inp
00202 )
00203 {
00204        BLOCK  blk;
00205        
00206        while (getblock(&blk, inp) != EOF) {
00207               if (ourmap == NULL || hasmatch(&blk, ourmap))
00208                      add2quals(&blk);
00209               doneblock(&blk);
00210        }
00211 }
00212 
00213 
00214 int
00215 hasmatch(            /* check for any match in rule list */
00216        BLOCK  *bp,
00217        RULEHD *mp
00218 )
00219 {
00220        if (mp == NULL)
00221               return(0);
00222        if (hasmatch(bp, mp->next)) /* check in order -- more likely */
00223               return(1);
00224        return(matchrule(bp, mp));
00225 }
00226 
00227 
00228 void
00229 getfhead(                   /* get file header */
00230        FILE   *fp
00231 )
00232 {
00233        char   buf[MAXSTR];
00234        int    i, n;
00235        register int  c;
00236                                    /* get file name */
00237        if (fgets(buf, MAXSTR, fp) == NULL)
00238               goto readerr;
00239        buf[strlen(buf)-1] = '\0';
00240        fhead.filename = savestr(buf);
00241                                    /* get layers */
00242        fhead.layer[0] = "Worksheet";
00243        for (i = 1; i <= 8; i++) {
00244               if (fscanf(fp, "L%*[^0-8]%d", &n) != 1 || n != i)
00245                      goto readerr;
00246               while ((c = getc(fp)) != EOF && (c == ' ' || c == '\t'))
00247                      ;
00248               if (c == EOF)
00249                      goto readerr;
00250               ungetc(c, fp);
00251               if (fgets(buf, MAXSTR, fp) == NULL)
00252                      goto readerr;
00253               buf[strlen(buf)-1] = '\0';
00254               if (buf[0])
00255                      fhead.layer[i] = savestr(buf);
00256               else
00257                      fhead.layer[i] = NULL;
00258        }
00259                                    /* get units */
00260        if (fgets(buf, MAXSTR, fp) == NULL)
00261               goto readerr;
00262        if (sscanf(buf, "0 %*f %s %*f %s\n",
00263                      fhead.length_u, fhead.area_u) != 2) {
00264               fhead.length_u[0] = '\0';
00265               fhead.area_u[0] = '\0';
00266        }
00267        if (fgets(buf, MAXSTR, fp) == NULL ||
00268                      sscanf(buf, "0 %lf\n", &fhead.length_f) != 1)
00269               goto readerr;
00270        if (fgets(buf, MAXSTR, fp) == NULL ||
00271                      sscanf(buf, "0 %lf\n", &fhead.area_f) != 1)
00272               goto readerr;
00273                                    /* get number of blocks and openings */
00274        if (fgets(buf, MAXSTR, fp) == NULL ||
00275                      sscanf(buf, "0 %d %d\n", &fhead.nblocks,
00276                      &fhead.nopenings) != 2)
00277               goto readerr;
00278        return;
00279 readerr:
00280        fprintf(stderr, "%s: error reading Architrion header\n", progname);
00281        exit(1);
00282 }
00283 
00284 
00285 void
00286 puthead(                    /* put out header information */
00287        FILE   *fp
00288 )
00289 {
00290        register int  i;
00291        
00292        fprintf(fp, "# File created by: %s\n", progname);
00293        fprintf(fp, "# Input file: %s\n", fhead.filename);
00294        fprintf(fp, "# Input units: %s\n", fhead.length_u);
00295        fprintf(fp, "# Output units: meters\n");
00296        fprintf(fp, "# Named layers:\n");
00297        for (i = 0; i < 8; i++)
00298               if (fhead.layer[i] != NULL)
00299                      fprintf(fp, "#\tLayer No. %d\t%s\n", i, fhead.layer[i]);
00300 }
00301 
00302 
00303 int
00304 getblock(                   /* get an Architrion block */
00305        register BLOCK       *bp,
00306        FILE   *fp
00307 )
00308 {
00309        char   word[32];
00310        int    i;
00311        
00312        if (fgets(word, sizeof(word), fp) == NULL)
00313               return(EOF);
00314        if (strncmp(word, "Block", 5))
00315               return(EOF);
00316        if (fscanf(fp, "1 %hd %hd %d", &bp->layer,
00317                      &bp->color, &bp->nopenings) != 3)
00318               return(EOF);
00319        if (fgetid(&bp->refid, "\n", fp) == EOF)
00320               return(EOF);
00321        if (fscanf(fp, "1 %d %d %d %d %d %d %d %d\n",
00322                      &bp->p.x[0], &bp->p.y[0], &bp->p.z[0], &bp->p.h[0],
00323                      &bp->p.x[1], &bp->p.y[1], &bp->p.z[1], &bp->p.h[1]) != 8)
00324               return(EOF);
00325        if (fscanf(fp, "1 %d %d %d %d %d %d %d %d\n",
00326                      &bp->p.x[2], &bp->p.y[2], &bp->p.z[2], &bp->p.h[2],
00327                      &bp->p.x[3], &bp->p.y[3], &bp->p.z[3], &bp->p.h[3]) != 8)
00328               return(EOF);
00329        if (bp->nopenings == 0) {
00330               bp->opening = NULL;
00331               return(0);
00332        }
00333        bp->opening = (OPNG *)malloc(bp->nopenings*sizeof(OPNG));
00334        if (bp->opening == NULL)
00335               goto memerr;
00336        for (i = 0; i < bp->nopenings; i++)
00337               if (getopening(&bp->opening[i], fp) < 0)
00338                      return(EOF);
00339        return(0);
00340 memerr:
00341        fprintf(stderr, "%s: out of memory in getblock\n", progname);
00342        exit(1);
00343 }
00344 
00345 
00346 int
00347 getopening(          /* read in opening from fp */
00348        register OPNG *op,
00349        FILE   *fp
00350 )
00351 {
00352        register int  c;
00353        char   word[32];
00354 
00355        if (fgets(word, sizeof(word), fp) == NULL)
00356               return(EOF);
00357        if (strncmp(word, "Opening", 7))
00358               return(EOF);
00359        if (fscanf(fp, "2 %d %d %d %d %d %d %d %d\n",
00360                      &op->p.x[0], &op->p.y[0],
00361                      &op->p.z[0], &op->p.h[0],
00362                      &op->p.x[1], &op->p.y[1],
00363                      &op->p.z[1], &op->p.h[1]) != 8)
00364               return(EOF);
00365        if (fscanf(fp, "2 %d %d %d %d %d %d %d %d\n",
00366                      &op->p.x[2], &op->p.y[2],
00367                      &op->p.z[2], &op->p.h[2],
00368                      &op->p.x[3], &op->p.y[3],
00369                      &op->p.z[3], &op->p.h[3]) != 8)
00370               return(EOF);
00371        c = getc(fp);
00372        if (c == '3') {
00373               if (fscanf(fp, "%d %d", &op->corner, &op->depth) != 2)
00374                      return(EOF);
00375               if (fgetid(&op->frame, "\n", fp) == EOF)
00376                      return(EOF);
00377        } else {
00378               op->corner = -1;
00379               op->frame.name = NULL;
00380               if (c != EOF)
00381                      ungetc(c, fp);
00382        }
00383        return(0);
00384 }
00385 
00386 
00387 void
00388 doneblock(                  /* free data associated with bp */
00389        register BLOCK       *bp
00390 )
00391 {
00392        register int  i;
00393 
00394        if (bp->nopenings > 0) {
00395               for (i = 0; i < bp->nopenings; i++)
00396                      doneid(&bp->opening[i].frame);
00397               free((void *)bp->opening);
00398        }
00399        doneid(&bp->refid);
00400 }
00401 
00402 
00403 void
00404 add2quals(                  /* add to qualifier lists */
00405        register BLOCK       *bp
00406 )
00407 {
00408        ID     tmpid;
00409 
00410        if (fhead.layer[bp->layer] == NULL) {
00411               tmpid.name = NULL;
00412               tmpid.number = bp->layer;
00413        } else {
00414               tmpid.name = fhead.layer[bp->layer];
00415               tmpid.number = 0;
00416        }
00417        findid(&qual[Q_LAY], &tmpid, 1);
00418        tmpid.name = NULL;
00419        tmpid.number = bp->color;
00420        findid(&qual[Q_COL], &tmpid, 1);
00421        findid(&qual[Q_REF], &bp->refid, 1);
00422 }
00423 
00424 
00425 void
00426 putblock(                   /* put out a block */
00427        BLOCK  *bp,
00428        FILE   *fp
00429 )
00430 {
00431        RULEHD *rp;
00432        char   *mod[NFACES], *sillmod;
00433        int    nmods, i;
00434        int    donematch, newmatch;
00435        
00436        nmods = 0; sillmod = NULL;
00437        donematch = 0;
00438        for (rp = ourmap; rp != NULL; rp = rp->next) {
00439               newmatch = matchrule(bp, rp);             /* test this rule */
00440               newmatch &= ~donematch;                   /* don't repeat */
00441               if (newmatch == 0)
00442                      continue;
00443               mod[nmods++] = rp->mnam;
00444               if (sillmod == NULL && newmatch&F_SIL)
00445                      sillmod = rp->mnam;
00446               putfaces(rp->mnam, bp, newmatch, fp);     /* put out new faces */
00447               donematch |= newmatch;
00448               if (donematch == (1<<NFACES)-1)
00449                      break;                      /* done all faces */
00450        }
00451                                           /* put out openings */
00452        if (donematch && bp->nopenings > 0) {
00453               putopenmod(sillmod, mod, nmods, fp);
00454               for (i = 0; i < bp->nopenings; i++)
00455                      putopening(&bp->opening[i], fp);
00456        }
00457        if (ferror(fp)) {
00458               fprintf(stderr, "%s: write error in putblock\n", progname);
00459               exit(1);
00460        }
00461 }
00462 
00463 
00464 void
00465 putopenmod(          /* put out opening modifier */
00466        char   *sm,
00467        char   *ml[],
00468        int    nm,
00469        FILE   *fp
00470 )
00471 {
00472        int    rept, nrepts;
00473        register int  i, j;
00474        
00475        if (sm == NULL) {           /* if no sill modifier, use list */
00476               sm = *ml++;
00477               nm--;
00478        }
00479                                    /* check for repeats */
00480        rept = 0; nrepts = 0;
00481        for (i = 0; i < nm; i++) {
00482               if (ml[i] == sm || !strcmp(ml[i], VOIDID)) {
00483                      rept |= 1<<i;
00484                      nrepts++;
00485                      continue;
00486               }
00487               for (j = 0; j < i; j++)
00488                      if (!(rept & 1<<j) && ml[j] == ml[i]) {
00489                             rept |= 1<<j;
00490                             nrepts++;
00491                      }
00492        }
00493                                    /* print antimatter and modlist */
00494        fprintf(fp, "\n%s antimatter %s\n", VOIDID, openmod);
00495        fprintf(fp, "%d %s", 1+nm-nrepts, sm);
00496        for (i = 0; i < nm; i++)
00497               if (!(rept & 1<<i))
00498                      fprintf(fp, " %s", ml[i]);
00499        fprintf(fp, "\n0\n0\n");
00500 }
00501 
00502 
00503 void
00504 putopening(                 /* put out an opening */
00505        OPNG   *op,
00506        FILE   *fp
00507 )
00508 {
00509        static int    nopens = 0;
00510        char   buf[32];
00511        register PRISM       *p = &op->p;
00512        PRISM  newp;
00513        register int  i;
00514                                    /* copy original prism */
00515        for (i = 0; i < 4; i++) {
00516               newp.x[i] = p->x[i];
00517               newp.y[i] = p->y[i];
00518               newp.z[i] = p->z[i];
00519               newp.h[i] = p->h[i];
00520        }
00521                                    /* spread reference and opposite */
00522        if (p->x[2] > p->x[0]) {
00523               newp.x[0] -= 2;
00524               newp.x[1] -= 2;
00525               newp.x[2] += 2;
00526               newp.x[3] += 2;
00527        } else if (p->x[0] > p->x[2]) {
00528               newp.x[0] += 2;
00529               newp.x[1] += 2;
00530               newp.x[2] -= 2;
00531               newp.x[3] -= 2;
00532        }
00533        if (p->y[2] > p->y[0]) {
00534               newp.y[0] -= 2;
00535               newp.y[1] -= 2;
00536               newp.y[2] += 2;
00537               newp.y[3] += 2;
00538        } else if (p->y[0] > p->y[2]) {
00539               newp.y[0] += 2;
00540               newp.y[1] += 2;
00541               newp.y[2] -= 2;
00542               newp.y[3] -= 2;
00543        }
00544                                           /* put out faces */
00545        sprintf(buf, "op%d", ++nopens);
00546        putface(openmod, buf, "ref", &newp, 4, 5, 1, 0, fp);
00547        putface(openmod, buf, "opp", &newp, 2, 6, 7, 3, fp);
00548        putface(openmod, buf, "end1", &newp, 5, 6, 2, 1, fp);
00549        putface(openmod, buf, "end2", &newp, 3, 7, 4, 0, fp);
00550        putface(openmod, buf, "bot", &newp, 1, 2, 3, 0, fp);
00551        putface(openmod, buf, "top", &newp, 7, 6, 5, 4, fp);
00552 }
00553 
00554 
00555 int
00556 matchrule(                  /* see if block matches this rule */
00557        register BLOCK       *bp,
00558        register RULEHD      *rp
00559 )
00560 {
00561        register int  i;
00562        ID     tmpid;
00563        
00564        if (rp->qflg & FL(Q_LAY)) {        /* check layer */
00565               tmpid.name = fhead.layer[bp->layer];
00566               tmpid.number = bp->layer;
00567               if (!matchid(&tmpid, &idm(rp)[Q_LAY]))
00568                      return(0);
00569        }
00570        if (rp->qflg & FL(Q_COL)) {        /* check color */
00571               tmpid.name = NULL;
00572               tmpid.number = bp->color;
00573               if (!matchid(&tmpid, &idm(rp)[Q_COL]))
00574                      return(0);
00575        }
00576        if (rp->qflg & FL(Q_REF)) {        /* check reference id */
00577               if (!matchid(&bp->refid, &idm(rp)[Q_REF]))
00578                      return(0);
00579        }
00580        if (rp->qflg & FL(Q_FAC)) {        /* check faces */
00581               for (i = 0; i < NFACES; i++)
00582                      if (matchid(&faceId[i], &idm(rp)[Q_FAC]))
00583                             return(1<<i); /* match single face */
00584               return(0);
00585        }
00586        return((1<<NFACES)-1);                    /* matched all faces */
00587 }             
00588 
00589 
00590 void
00591 putfaces(                   /* put out faces */
00592        char   *m,
00593        BLOCK  *bp,
00594        register int  ff,
00595        FILE   *fp
00596 )
00597 {
00598        char   *blkname(), *bn;
00599 
00600        if (!strcmp(m, VOIDID))
00601               return;
00602        bn = blkname(bp);
00603        if (ff & 1<<F_REF)
00604               putface(m, bn, "ref", &bp->p, 4, 5, 1, 0, fp);
00605        if (ff & 1<<F_OPP)
00606               putface(m, bn, "opp", &bp->p, 2, 6, 7, 3, fp);
00607        if (ff & 1<<F_END) {
00608               putface(m, bn, "end1", &bp->p, 5, 6, 2, 1, fp);
00609               putface(m, bn, "end2", &bp->p, 3, 7, 4, 0, fp);
00610        }
00611        if (ff & 1<<F_BOT)
00612               putface(m, bn, "bot", &bp->p, 1, 2, 3, 0, fp);
00613        if (ff & 1<<F_TOP)
00614               putface(m, bn, "top", &bp->p, 7, 6, 5, 4, fp);
00615 }
00616 
00617 
00618 char *
00619 blkname(             /* think up a good name for this block */
00620        register BLOCK       *bp
00621 )
00622 {
00623        static char   nambuf[32];
00624        static int    blkcnt = 0;
00625        register char *nam;
00626        register int  i, j;
00627 
00628        sprintf(nambuf, "l%d.", bp->layer);
00629        i = strlen(nambuf);
00630        nam = bp->refid.name;
00631        if (nam == NULL) {
00632               nam = fhead.layer[bp->layer];
00633               if (nam != NULL)
00634                      i = 0;
00635        }
00636        if (nam != NULL) {
00637               for (j = 0; j < 12 && nam[j]; j++) {
00638                      if (nam[j] == ' ' || nam[j] == '\t')
00639                             nambuf[i++] = '_';
00640                      else
00641                             nambuf[i++] = nam[j];
00642               }
00643               nambuf[i++] = '.';
00644        }
00645        if (bp->refid.number != 0) {
00646               sprintf(nambuf+i, "r%d.", bp->refid.number);
00647               i = strlen(nambuf);
00648        }
00649        sprintf(nambuf+i, "b%d", ++blkcnt);
00650        return(nambuf);
00651 }
00652 
00653 
00654 void
00655 putface(      /* put out a face */
00656        char   *m,
00657        char   *bn,
00658        char   *fn,
00659        PRISM  *p,
00660        int    a,
00661        int b,
00662        int c,
00663        int d,
00664        FILE   *fp
00665 )
00666 {
00667        int    cf;
00668        
00669        cf = checkface(p, a, b, c, d);
00670        if (cf & QL_OK) {
00671               fprintf(fp, "\n%s polygon %s.%s\n", m, bn, fn);
00672               fprintf(fp, "0\n0\n12\n");
00673               putpoint(p, a, fp);
00674               putpoint(p, b, fp);
00675               putpoint(p, c, fp);
00676               putpoint(p, d, fp);
00677               return;
00678        }
00679        if (cf & T1_OK) {
00680               fprintf(fp, "\n%s polygon %s.%sA\n", m, bn, fn);
00681               fprintf(fp, "0\n0\n9\n");
00682               putpoint(p, d, fp);
00683               putpoint(p, a, fp);
00684               putpoint(p, b, fp);
00685        }
00686        if (cf & T2_OK) {
00687               fprintf(fp, "\n%s polygon %s.%sB\n", m, bn, fn);
00688               fprintf(fp, "0\n0\n9\n");
00689               putpoint(p, b, fp);
00690               putpoint(p, c, fp);
00691               putpoint(p, d, fp);
00692        }
00693 }
00694 
00695 
00696 #define ldot(v1,v2)  (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
00697 
00698 
00699 void
00700 lcross(                     /* compute cross product */
00701        register long vr[3],
00702        register long v1[3],
00703        register long v2[3]
00704 )
00705 {
00706        vr[0] = v1[1]*v2[2] - v1[2]*v2[1];
00707        vr[1] = v1[2]*v2[0] - v1[0]*v2[2];
00708        vr[2] = v1[0]*v2[1] - v1[1]*v2[0];
00709 }
00710 
00711 
00712 #define labs(a)             ((a)>0 ? (a) : -(a))
00713 
00714 
00715 int
00716 checkface(           /* check a face for validity */
00717        register PRISM       *p,
00718        int    a,
00719        int b,
00720        int c,
00721        int d
00722 )
00723 {
00724        int    rval = 0;
00725        long   lt;
00726        long   vc1[3], vc2[3], vt1[3], vt2[3];
00727        long   vc1l, vc2l, vc1vc2;
00728                                    /* check first triangle */
00729        vt1[0] = p_x(p,b) - p_x(p,a);
00730        vt1[1] = p_y(p,b) - p_y(p,a);
00731        vt1[2] = p_z(p,b) - p_z(p,a);
00732        vt2[0] = p_x(p,d) - p_x(p,a);
00733        vt2[1] = p_y(p,d) - p_y(p,a);
00734        vt2[2] = p_z(p,d) - p_z(p,a);
00735        lcross(vc1, vt1, vt2);
00736        lt = labs(vc1[0]) + labs(vc1[1]) + labs(vc1[2]);
00737        if (lt > 1L<<18) lt = 16;
00738        else if (lt > 1L<<12) lt = 8;
00739        else lt = 0;
00740        if (lt) { vc1[0] >>= lt; vc1[1] >>= lt; vc1[2] >>= lt; }
00741        vc1l = ldot(vc1,vc1);
00742        if (vc1l > 4)
00743               rval |= T1_OK;
00744                                    /* check second triangle */
00745        vt1[0] = p_x(p,d) - p_x(p,c);
00746        vt1[1] = p_y(p,d) - p_y(p,c);
00747        vt1[2] = p_z(p,d) - p_z(p,c);
00748        vt2[0] = p_x(p,b) - p_x(p,c);
00749        vt2[1] = p_y(p,b) - p_y(p,c);
00750        vt2[2] = p_z(p,b) - p_z(p,c);
00751        lcross(vc2, vt1, vt2);
00752        lt = labs(vc2[0]) + labs(vc2[1]) + labs(vc2[2]);
00753        if (lt > 1L<<18) lt = 16;
00754        else if (lt > 1L<<12) lt = 8;
00755        else lt = 0;
00756        if (lt) { vc2[0] >>= lt; vc2[1] >>= lt; vc2[2] >>= lt; }
00757        vc2l = ldot(vc2,vc2);
00758        if (vc2l > 4)
00759               rval |= T2_OK;
00760                                    /* check quadrilateral */
00761        if (rval == (T1_OK|T2_OK)) {
00762               vc1vc2 = ldot(vc1,vc2);
00763               if (vc1vc2*vc1vc2 >= vc1l*vc2l-8)
00764                      rval |= QL_OK;
00765        }
00766        return(rval);
00767 }
00768 
00769 
00770 void
00771 putpoint(                   /* put out a point */
00772        register PRISM       *p,
00773        int    n,
00774        FILE   *fp
00775 )
00776 {
00777        register int  i = n&3;
00778        
00779        fprintf(fp, ptfmt, p->x[i]*fhead.length_f, p->y[i]*fhead.length_f,
00780                      (n&4 ? p->h[i] : p->z[i])*fhead.length_f);
00781 }
00782 
00783 
00784 void
00785 eputs(
00786        char   *s
00787 )
00788 {
00789        fputs(s, stderr);
00790 }
00791 
00792 
00793 void
00794 quit(
00795        int    code
00796 )
00797 {
00798        exit(code);
00799 }
00800