Back to index

radiance  4R0+20100331
mkillum3.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char RCSid[] = "$Id: mkillum3.c,v 2.12 2007/09/13 17:52:35 greg Exp $";
00003 #endif
00004 /*
00005  * Routines to print mkillum objects
00006  */
00007 
00008 #include  "mkillum.h"
00009 #include  "paths.h"
00010 
00011 #define  brt(col)    (.263*(col)[0]+.655*(col)[1]+.082*(col)[2])
00012 
00013 char   DATORD[] = "RGB";           /* data ordering */
00014 char   DATSUF[] = ".dat";          /* data file suffix */
00015 char   DSTSUF[] = ".dist";         /* distribution suffix */
00016 char   FNCFNM[] = "illum.cal";            /* function file name */
00017 
00018 void compinv(COLORV *rinv, COLORV *rp, int m);
00019 void colorout(int p, COLORV *da, int n, int m, double mult, FILE *fp);
00020 void fputnum(double d, FILE *fp);
00021 void brightout(COLORV *da, int n, int m, double mult, FILE *fp);
00022 void fputeol(FILE *fp);
00023 void compavg(COLOR col, COLORV *da, int n);
00024 char * dfname(struct illum_args *il, int c);
00025 FILE * dfopen(struct illum_args *il, int c);
00026 
00027 
00028 void
00029 printobj(            /* print out an object */
00030        char  *mod,
00031        register OBJREC  *obj
00032 )
00033 {
00034        register int  i;
00035 
00036        if (issurface(obj->otype) && !strcmp(mod, VOIDID))
00037               return;              /* don't print void surfaces */
00038        printf("\n%s %s %s", mod, ofun[obj->otype].funame, obj->oname);
00039        printf("\n%d", obj->oargs.nsargs);
00040        for (i = 0; i < obj->oargs.nsargs; i++)
00041               printf(" %s", obj->oargs.sarg[i]);
00042 #ifdef  IARGS
00043        printf("\n%d", obj->oargs.niargs);
00044        for (i = 0; i < obj->oargs.niargs; i++)
00045               printf(" %d", obj->oargs.iarg[i]);
00046 #else
00047        printf("\n0");
00048 #endif
00049        printf("\n%d", obj->oargs.nfargs);
00050        for (i = 0; i < obj->oargs.nfargs; i++) {
00051               if (i%3 == 0)
00052                      putchar('\n');
00053               printf(" %18.12g", obj->oargs.farg[i]);
00054        }
00055        putchar('\n');
00056 }
00057 
00058 
00059 char *
00060 dfname(                     /* return data file name */
00061        struct illum_args  *il,
00062        int  c
00063 )
00064 {
00065        char  fname[MAXSTR];
00066        register char  *s;
00067 
00068        s = strcpy(fname, il->datafile);
00069        s += strlen(s);
00070        if (c) *s++ = c;
00071        if (il->dfnum > 0) {
00072               sprintf(s, "%d", il->dfnum);
00073               s += strlen(s);
00074        }
00075        strcpy(s, DATSUF);
00076        return(getpath(fname, NULL, 0));
00077 }
00078 
00079 
00080 FILE *
00081 dfopen(                     /* open data file */
00082        register struct illum_args  *il,
00083        int  c
00084 )
00085 {
00086        char  *fn;
00087        FILE  *fp;
00088                                    /* get a usable file name */
00089        for (fn = dfname(il, c);
00090                      !(il->flags & IL_DATCLB) && access(fn, F_OK) == 0;
00091                      fn = dfname(il, c))
00092               il->dfnum++;
00093                                    /* open it for writing */
00094        if ((fp = fopen(fn, "w")) == NULL) {
00095               sprintf(errmsg, "cannot open data file \"%s\"", fn);
00096               error(SYSTEM, errmsg);
00097        }
00098        return(fp);
00099 }
00100 
00101 
00102 extern void
00103 flatout(             /* write hemispherical distribution */
00104        struct illum_args  *il,
00105        COLORV  *da,
00106        int  n,
00107        int  m,
00108        FVECT  u,
00109        FVECT  v,
00110        FVECT  w
00111 )
00112 {
00113        COLORV  *Ninv;
00114        FILE  *dfp;
00115        int  i;
00116 
00117        if ((Ninv = (COLORV *)malloc(3*m*sizeof(COLORV))) == NULL)
00118               error(SYSTEM, "out of memory in flatout");
00119        compinv(Ninv, da, m);
00120        if (il->flags & IL_COLDST) {
00121               printf("\n%s %s %s%s", VOIDID, ofun[PAT_CDATA].funame,
00122                             il->matname, DSTSUF);
00123               printf("\n9 red green blue");
00124               for (i = 0; i < 3; i++) {
00125                      dfp = dfopen(il, DATORD[i]);
00126                      fprintf(dfp, "2\n%f %f %d\n%f %f %d\n",
00127                                    1.+.5/n, .5/n, n+1,
00128                                    0., 2.*PI, m+1);
00129                      colorout(i, Ninv, 1, m, 1./il->nsamps/il->col[i], dfp);
00130                      colorout(i, da, n, m, 1./il->nsamps/il->col[i], dfp);
00131                      fputeol(dfp);
00132                      fclose(dfp);
00133                      printf(" %s", dfname(il, DATORD[i]));
00134               }
00135        } else {
00136               printf("\n%s %s %s%s", VOIDID, ofun[PAT_BDATA].funame,
00137                             il->matname, DSTSUF);
00138               printf("\n5 noneg");
00139               dfp = dfopen(il, 0);
00140               fprintf(dfp, "2\n%f %f %d\n%f %f %d\n",
00141                             1.+.5/n, .5/n, n+1,
00142                             0., 2.*PI, m+1);
00143               brightout(Ninv, 1, m, 1./il->nsamps/brt(il->col), dfp);
00144               brightout(da, n, m, 1./il->nsamps/brt(il->col), dfp);
00145               fputeol(dfp);
00146               fclose(dfp);
00147               printf(" %s", dfname(il, 0));
00148        }
00149        printf("\n\t%s il_alth il_azih", FNCFNM);
00150        printf("\n0\n9\n");
00151        printf("\t%f\t%f\t%f\n", u[0], u[1], u[2]);
00152        printf("\t%f\t%f\t%f\n", v[0], v[1], v[2]);
00153        printf("\t%f\t%f\t%f\n", w[0], w[1], w[2]);
00154        il->dfnum++;
00155        free((void *)Ninv);
00156 }
00157 
00158 
00159 extern void
00160 roundout(                   /* write spherical distribution */
00161        struct illum_args  *il,
00162        COLORV  *da,
00163        int  n,
00164        int  m
00165 )
00166 {
00167        COLORV  *Ninv, *Sinv;
00168        FILE  *dfp;
00169        int  i;
00170 
00171        if ((Ninv = (COLORV *)malloc(3*m*sizeof(COLORV))) == NULL ||
00172                      (Sinv = (COLORV *)malloc(3*m*sizeof(COLORV))) == NULL)
00173               error(SYSTEM, "out of memory in roundout");
00174        compinv(Ninv, da, m);
00175        compinv(Sinv, da+3*m*(n-1), m);
00176        if (il->flags & IL_COLDST) {
00177               printf("\n%s %s %s%s", VOIDID, ofun[PAT_CDATA].funame,
00178                             il->matname, DSTSUF);
00179               printf("\n9 red green blue");
00180               for (i = 0; i < 3; i++) {
00181                      dfp = dfopen(il, DATORD[i]);
00182                      fprintf(dfp, "2\n%f %f %d\n%f %f %d\n",
00183                                    1.+1./n, -1.-1./n, n+2,
00184                                    0., 2.*PI, m+1);
00185                      colorout(i, Ninv, 1, m, 1./il->nsamps/il->col[i], dfp);
00186                      colorout(i, da, n, m, 1./il->nsamps/il->col[i], dfp);
00187                      colorout(i, Sinv, 1, m, 1./il->nsamps/il->col[i], dfp);
00188                      fputeol(dfp);
00189                      fclose(dfp);
00190                      printf(" %s", dfname(il, DATORD[i]));
00191               }
00192        } else {
00193               printf("\n%s %s %s%s", VOIDID, ofun[PAT_BDATA].funame,
00194                             il->matname, DSTSUF);
00195               printf("\n5 noneg");
00196               dfp = dfopen(il, 0);
00197               fprintf(dfp, "2\n%f %f %d\n%f %f %d\n",
00198                             1.+1./n, -1.-1./n, n+2,
00199                             0., 2.*PI, m+1);
00200               brightout(Ninv, 1, m, 1./il->nsamps/brt(il->col), dfp);
00201               brightout(da, n, m, 1./il->nsamps/brt(il->col), dfp);
00202               brightout(Sinv, 1, m, 1./il->nsamps/brt(il->col), dfp);
00203               fputeol(dfp);
00204               fclose(dfp);
00205               printf(" %s", dfname(il, 0));
00206        }
00207        printf("\n\t%s il_alt il_azi", FNCFNM);
00208        printf("\n0\n0\n");
00209        il->dfnum++;
00210        free((void *)Ninv);
00211        free((void *)Sinv);
00212 }
00213 
00214 
00215 extern void
00216 illumout(            /* print illum object */
00217        register struct illum_args  *il,
00218        OBJREC  *ob
00219 )
00220 {
00221        double  cout[3];
00222 
00223        if (il->sampdens <= 0)
00224               printf("\n%s ", VOIDID);
00225        else
00226               printf("\n%s%s ", il->matname, DSTSUF);
00227        printf("%s %s", ofun[il->flags&IL_LIGHT?MAT_LIGHT:MAT_ILLUM].funame,
00228                      il->matname);
00229        if (il->flags & IL_LIGHT || !strcmp(il->altmat,VOIDID))
00230               printf("\n0");
00231        else
00232               printf("\n1 %s", il->altmat);
00233        if (il->flags & IL_COLAVG) {
00234               cout[0] = il->col[0];
00235               cout[1] = il->col[1];
00236               cout[2] = il->col[2];
00237        } else {
00238               cout[0] = cout[1] = cout[2] = brt(il->col);
00239        }
00240        printf("\n0\n3 %f %f %f\n", cout[0], cout[1], cout[2]);
00241 
00242        printobj(il->matname, ob);
00243 }
00244 
00245 
00246 void
00247 compavg(             /* compute average for set of data values */
00248        COLOR  col,
00249        register COLORV  *da,
00250        int  n
00251 )
00252 {
00253        register int  i;
00254 
00255        setcolor(col, 0.0, 0.0, 0.0);
00256        i = n;
00257        while (i-- > 0) {
00258               addcolor(col, da);
00259               da += 3;
00260        }
00261        scalecolor(col, 1./(double)n);
00262 }
00263 
00264 
00265 void
00266 compinv(             /* compute other side of row average */
00267        register COLORV  *rinv,
00268        register COLORV  *rp,
00269        int  m
00270 )
00271 {
00272        COLOR  avg;
00273 
00274        compavg(avg, rp, m);        /* row average */
00275        while (m-- > 0) {
00276               *rinv++ = 2.*avg[0] - *rp++;
00277               *rinv++ = 2.*avg[1] - *rp++;
00278               *rinv++ = 2.*avg[2] - *rp++;
00279        }
00280 }
00281 
00282 
00283 extern int
00284 average(             /* evaluate average value for distribution */
00285        register struct illum_args  *il,
00286        COLORV  *da,
00287        int  n
00288 )
00289 {
00290        compavg(il->col, da, n);    /* average */
00291        if (il->nsamps > 1) {
00292               il->col[0] /= (double)il->nsamps;
00293               il->col[1] /= (double)il->nsamps;
00294               il->col[2] /= (double)il->nsamps;
00295        }
00296                                    /* brighter than minimum? */
00297        return(brt(il->col) > il->minbrt+FTINY);
00298 }
00299 
00300 
00301 static int    colmcnt = 0;  /* count of columns written */
00302 
00303 void
00304 fputnum(                    /* put out a number to fp */
00305        double  d,
00306        FILE  *fp
00307 )
00308 {
00309        if (colmcnt++ % 5 == 0)
00310               putc('\n', fp);
00311        fprintf(fp, " %11e", d);
00312 }
00313 
00314 
00315 void
00316 fputeol(                    /* write end of line to fp */
00317        register FILE  *fp
00318 )
00319 {
00320        putc('\n', fp);
00321        colmcnt = 0;
00322 }
00323 
00324 
00325 void
00326 colorout(     /* put out color distribution data */
00327        int  p,
00328        register COLORV  *da,
00329        int  n,
00330        int  m,
00331        double  mult,
00332        FILE  *fp
00333 )
00334 {
00335        register int  i, j;
00336 
00337        for (i = 0; i < n; i++) {
00338               for (j = 0; j < m; j++) {
00339                      fputnum(mult*da[p], fp);
00340                      da += 3;
00341               }
00342               fputnum(mult*da[p-3*m], fp);       /* wrap phi */
00343        }
00344 }
00345 
00346 
00347 void
00348 brightout(    /* put out brightness distribution data */
00349        register COLORV  *da,
00350        int  n,
00351        int  m,
00352        double  mult,
00353        FILE  *fp
00354 )
00355 {
00356        register int  i, j;
00357 
00358        for (i = 0; i < n; i++) {
00359               for (j = 0; j < m; j++) {
00360                      fputnum(mult*brt(da), fp);
00361                      da += 3;
00362               }
00363               fputnum(mult*brt(da-3*m), fp);     /* wrap phi */
00364        }
00365 }