ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/dctimestep.c
(Generate patch)

Comparing ray/src/util/dctimestep.c (file contents):
Revision 2.1 by greg, Wed Jun 17 20:41:47 2009 UTC vs.
Revision 2.13 by greg, Thu Jul 1 22:44:25 2010 UTC

# Line 16 | Line 16 | static const char RCSid[] = "$Id$";
16  
17   char    *progname;                      /* global argv[0] */
18  
19 < /* Data types for matrix loading */
19 > /* Data types for file loading */
20   enum {DTfromHeader, DTascii, DTfloat, DTdouble, DTrgbe, DTxyze};
21  
22   /* A color coefficient matrix -- vectors have ncols==1 */
# Line 38 | Line 38 | cm_alloc(int nrows, int ncols)
38          CMATRIX *cm;
39  
40          if ((nrows <= 0) | (ncols <= 0))
41 <                return(NULL);
41 >                error(USER, "attempt to create empty matrix");
42          cm = (CMATRIX *)malloc(sizeof(CMATRIX) +
43                                  3*sizeof(COLORV)*(nrows*ncols - 1));
44          if (cm == NULL)
# Line 108 | Line 108 | cm_load(const char *fname, int nrows, int ncols, int d
108          CMATRIX *cm;
109          FILE    *fp = stdin;
110  
111 +        if (ncols <= 0)
112 +                error(USER, "Non-positive number of columns");
113          if (fname == NULL)
114                  fname = "<stdin>";
115          else if ((fp = fopen(fname, "r")) == NULL) {
# Line 125 | Line 127 | cm_load(const char *fname, int nrows, int ncols, int d
127                  break;
128          default:
129                  error(USER, "unexpected data type in cm_load()");
128                return(NULL);
130          }
131          if (nrows <= 0) {                       /* don't know length? */
132                  int     guessrows = 147;        /* usually big enough */
# Line 135 | Line 136 | cm_load(const char *fname, int nrows, int ncols, int d
136                                  long    endpos = ftell(fp);
137                                  long    elemsiz = 3*(dtype==DTfloat ?
138                                              sizeof(float) : sizeof(double));
139 <                                guessrows = (endpos - startpos)/elemsiz;
139 >
140 >                                if ((endpos - startpos) % (ncols*elemsiz)) {
141 >                                        sprintf(errmsg,
142 >                                        "improper length for binary file '%s'",
143 >                                                        fname);
144 >                                        error(USER, errmsg);
145 >                                }
146 >                                guessrows = (endpos - startpos)/(ncols*elemsiz);
147                                  if (fseek(fp, startpos, SEEK_SET) < 0) {
148                                          sprintf(errmsg,
149                                                  "fseek() error on file '%s'",
150                                                          fname);
151                                          error(SYSTEM, errmsg);
152                                  }
153 <                                if ((endpos - startpos) % elemsiz == 0)
146 <                                        nrows = guessrows;      /* confident */
153 >                                nrows = guessrows;      /* we're confident */
154                          }
155                  }
156                  cm = cm_alloc(guessrows, ncols);
# Line 155 | Line 162 | cm_load(const char *fname, int nrows, int ncols, int d
162                  int     maxrow = (nrows > 0 ? nrows : 32000);
163                  int     r, c;
164                  for (r = 0; r < maxrow; r++) {
165 <                    if (r >= cm->nrows)         /* need more space? */
165 >                    if (r >= cm->nrows)                 /* need more space? */
166                          cm = cm_resize(cm, 2*cm->nrows);
167                      for (c = 0; c < ncols; c++) {
168                          COLORV  *cv = cm_lval(cm,r,c);
169                          if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3)
170 <                                if ((nrows <= 0) & (r > 0) & (c == 0)) {
170 >                                if ((nrows <= 0) & (r > 0) & !c) {
171                                          cm = cm_resize(cm, maxrow=r);
172                                          break;
173                                  } else
# Line 188 | Line 195 | cm_load(const char *fname, int nrows, int ncols, int d
195                                          if (nread == cm->nrows*cm->ncols)
196                                                          /* need more space? */
197                                                  cm = cm_resize(cm, 2*cm->nrows);
198 <                                        else if (nread % cm->ncols == 0)
198 >                                        else if (nread && !(nread % cm->ncols))
199                                                          /* seem to be  done */
200                                                  cm = cm_resize(cm, nread/cm->ncols);
201                                          else            /* ended mid-row */
# Line 210 | Line 217 | cm_load(const char *fname, int nrows, int ncols, int d
217                                  copycolor(cvp, dc);
218                                  cvp += 3;
219                          }
213
220                  } else /* dtype == DTfloat */ {
221                          float   fc[3];                  /* load from float */
222                          COLORV  *cvp = cm->cmem;
# Line 290 | Line 296 | static CMATRIX *
296   cm_bsdf(const struct BSDF_data *bsdf)
297   {
298          CMATRIX *cm = cm_alloc(bsdf->nout, bsdf->ninc);
299 <        COLORV  *mp = cm->cmem;
299 >        int     nbadohm = 0;
300 >        int     nneg = 0;
301          int     r, c;
302          
303 <        for (r = 0; r < cm->nrows; r++)
304 <                for (c = 0; c < cm->ncols; c++, mp += 3)
305 <                        mp[0] = mp[1] = mp[2] = BSDF_value(bsdf,c,r);
303 >        for (c = 0; c < cm->ncols; c++) {
304 >                float   dom = getBSDF_incohm(bsdf,c);
305 >                FVECT   v;
306 >                
307 >                if (dom <= .0) {
308 >                        nbadohm++;
309 >                        continue;
310 >                }
311 >                if (!getBSDF_incvec(v,bsdf,c) || v[2] > FTINY)
312 >                        error(USER, "illegal incoming BTDF direction");
313 >                dom *= -v[2];
314 >
315 >                for (r = 0; r < cm->nrows; r++) {
316 >                        float   f = BSDF_value(bsdf,c,r);
317 >                        COLORV  *mp = cm_lval(cm,r,c);
318 >
319 >                        if (f <= .0) {
320 >                                nneg += (f < -FTINY);
321 >                                f = .0f;
322 >                        }
323 >                        mp[0] = mp[1] = mp[2] = f * dom;
324 >                }
325 >        }
326 >        if (nneg || nbadohm) {
327 >                sprintf(errmsg,
328 >                    "BTDF has %d negatives and %d bad incoming solid angles",
329 >                                nneg, nbadohm);
330 >                error(WARNING, errmsg);
331 >        }
332          return(cm);
333   }
334  
335   /* Sum together a set of images and write result to stdout */
336   static int
337 < sum_images(const char *fspec, const CMATRIX *cv)
337 > sum_images(const char *fspec, const CMATRIX *cv, FILE *fout)
338   {
339          int     myDT = DTfromHeader;
340          CMATRIX *pmat;
# Line 338 | Line 371 | sum_images(const char *fspec, const CMATRIX *cv)
371                                  error(SYSTEM, "out of memory in sum_images()");
372                          pmat = cm_alloc(myYR, myXR);
373                          memset(pmat->cmem, 0, sizeof(COLOR)*myXR*myYR);
374 +                                                        /* finish header */
375 +                        fputformat(myDT==DTrgbe ? COLRFMT : CIEFMT, fout);
376 +                        fputc('\n', fout);
377 +                        fprtresolu(myXR, myYR, fout);
378 +                        fflush(fout);
379                  } else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) {
380                          sprintf(errmsg, "picture '%s' format/size mismatch",
381                                          fname);
# Line 360 | Line 398 | sum_images(const char *fspec, const CMATRIX *cv)
398                  fclose(fp);                             /* done this picture */
399          }
400          free(scanline);
363                                                        /* finish header */
364        fputformat(myDT==DTrgbe ? COLRFMT : CIEFMT, stdout);
365        fputc('\n', stdout);
366        fprtresolu(myXR, myYR, stdout);
401                                                          /* write scanlines */
402          for (y = 0; y < myYR; y++)
403 <                if (fwritescan((COLOR *)cm_lval(pmat, y, 0), myXR, stdout) < 0)
403 >                if (fwritescan((COLOR *)cm_lval(pmat, y, 0), myXR, fout) < 0)
404                          return(0);
405          cm_free(pmat);                                  /* all done */
406 <        return(fflush(stdout) == 0);
406 >        return(fflush(fout) == 0);
407   }
408  
409 < /* check to see if a string contains a %d specification */
410 < int
411 < hasDecimalSpec(const char *s)
409 > /* check to see if a string contains a %d or %o specification */
410 > static int
411 > hasNumberFormat(const char *s)
412   {
413          while (*s && *s != '%')
414                  s++;
# Line 384 | Line 418 | hasDecimalSpec(const char *s)
418                  ++s;
419          while (isdigit(*s));
420  
421 <        return(*s == 'd');
421 >        return(*s == 'd' | *s == 'i' | *s == 'o' | *s == 'x' | *s == 'X');
422   }
423  
424   int
425   main(int argc, char *argv[])
426   {
427 <        CMATRIX                 *tvec, *Dmat, *Tmat, *vtmp, *cvec;
394 <        struct BSDF_data        *btdf;
427 >        CMATRIX                 *cvec;
428  
429          progname = argv[0];
430  
431 <        if ((argc < 4) | (argc > 5)) {
432 <                fprintf(stderr, "Usage: %s Vspec Tbsdf.xml Dmat.dat [tregvec]\n",
433 <                                progname);
431 >        if ((argc < 2) | (argc > 5)) {
432 >                fprintf(stderr, "Usage: %s DCspec [tregvec]\n", progname);
433 >                fprintf(stderr, "   or: %s Vspec Tbsdf.xml Dmat.dat [tregvec]\n",
434 >                                        progname);
435                  return(1);
436          }
437 <        tvec = cm_load(argv[4], 0, 1, DTascii); /* argv[4]==NULL iff argc==4 */
438 <        Dmat = cm_load(argv[3], 0, tvec->nrows, DTfromHeader);
439 <        btdf = load_BSDF(argv[2]);
440 <        if (btdf == NULL)
441 <                return(1);
442 <        if (btdf->ninc != Dmat->nrows) {
443 <                sprintf(errmsg, "Incoming BTDF dir (%d) mismatch to D (%d)",
444 <                                btdf->ninc, Dmat->nrows);
445 <                error(USER, errmsg);
446 <        }
437 >
438 >        if (argc > 3) {                         /* VTDs expression */
439 >                CMATRIX *svec, *Dmat, *Tmat, *ivec;
440 >                struct BSDF_data        *btdf;
441 >                                                /* get sky vector */
442 >                svec = cm_load(argv[4], 0, 1, DTascii);
443 >                                                /* load BSDF */
444 >                btdf = load_BSDF(argv[2]);
445 >                if (btdf == NULL)
446 >                        return(1);
447 >                                                /* load Daylight matrix */
448 >                Dmat = cm_load(argv[3], btdf->ninc, svec->nrows, DTfromHeader);
449                                                  /* multiply vector through */
450 <        vtmp = cm_multiply(Dmat, tvec);
451 <        cm_free(Dmat); cm_free(tvec);
452 <        Tmat = cm_bsdf(btdf);                   /* convert BTDF to matrix */
453 <        free_BSDF(btdf);
454 <        cvec = cm_multiply(Tmat, vtmp);         /* cvec = component vector */
455 <        cm_free(Tmat); cm_free(vtmp);
456 <        if (hasDecimalSpec(argv[1])) {          /* generating image */
450 >                ivec = cm_multiply(Dmat, svec);
451 >                cm_free(Dmat); cm_free(svec);
452 >                Tmat = cm_bsdf(btdf);           /* convert BTDF to matrix */
453 >                free_BSDF(btdf);
454 >                cvec = cm_multiply(Tmat, ivec); /* cvec = component vector */
455 >                cm_free(Tmat); cm_free(ivec);
456 >        } else {                                /* else just use sky vector */
457 >                cvec = cm_load(argv[2], 0, 1, DTascii);
458 >        }
459 >
460 >        if (hasNumberFormat(argv[1])) {         /* generating image */
461                  SET_FILE_BINARY(stdout);
462                  newheader("RADIANCE", stdout);
463                  printargs(argc, argv, stdout);
464                  fputnow(stdout);
465 <                if (!sum_images(argv[1], cvec))
465 >                if (!sum_images(argv[1], cvec, stdout))
466                          return(1);
467          } else {                                /* generating vector */
468                  CMATRIX *Vmat = cm_load(argv[1], 0, cvec->nrows, DTfromHeader);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines