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

Comparing ray/src/gen/gendaymtx.c (file contents):
Revision 2.10 by greg, Sat Apr 6 00:44:59 2013 UTC vs.
Revision 2.29 by greg, Wed Aug 14 21:00:14 2019 UTC

# Line 86 | Line 86 | static const char RCSid[] = "$Id$";
86   #include <string.h>
87   #include <ctype.h>
88   #include "rtmath.h"
89 + #include "rtio.h"
90 + #include "resolu.h"
91 + #include "platform.h"
92   #include "color.h"
93 + #include "resolu.h"
94  
95   char *progname;                                                         /* Program name */
96   char errmsg[128];                                                       /* Error message buffer */
# Line 108 | Line 112 | double sky_clearness;                  /* Sky clearness */
112   double solar_rad;                       /* Solar radiance */
113   double sun_zenith;                      /* Sun zenith angle (radians) */
114   int     input = 0;                              /* Input type */
115 + int     output = 0;                             /* Output type */
116  
117   extern double dmax( double, double );
118   extern double CalcAirMass();
# Line 208 | Line 213 | static const CategoryBounds SkyClearCat[8] =
213          { 1.950, 2.800 },
214          { 2.800, 4.500 },
215          { 4.500, 6.200 },
216 <        { 6.200, 12.00 }        /* Clear */
216 >        { 6.200, 12.01 }        /* Clear */
217   };
218  
219   /* Luminous efficacy model coefficients */
# Line 291 | Line 296 | extern int     rh_init(void);
296   extern float *  resize_dmatrix(float *mtx_data, int nsteps, int npatch);
297   extern void     AddDirect(float *parr);
298  
299 +
300 + static const char *
301 + getfmtname(int fmt)
302 + {
303 +        switch (fmt) {
304 +        case 'a':
305 +                return("ascii");
306 +        case 'f':
307 +                return("float");
308 +        case 'd':
309 +                return("double");
310 +        }
311 +        return("unknown");
312 + }
313 +
314 +
315   int
316   main(int argc, char *argv[])
317   {
318          char    buf[256];
319 +        int     doheader = 1;           /* output header? */
320          double  rotation = 0;           /* site rotation (degrees) */
321          double  elevation;              /* site elevation (meters) */
322          int     dir_is_horiz;           /* direct is meas. on horizontal? */
323          float   *mtx_data = NULL;       /* our matrix data */
324 <        int     ntsteps = 0;            /* number of rows in matrix */
324 >        int     avgSky = 0;             /* compute average sky r.t. matrix? */
325 >        int     ntsteps = 0;            /* number of time steps */
326 >        int     tstorage = 0;           /* number of allocated time steps */
327 >        int     nstored = 0;            /* number of time steps in matrix */
328          int     last_monthly = 0;       /* month of last report */
329 +        int     inconsistent = 0;       /* inconsistent options set? */
330          int     mo, da;                 /* month (1-12) and day (1-31) */
331          double  hr;                     /* hour (local standard time) */
332          double  dir, dif;               /* direct and diffuse values */
# Line 319 | Line 345 | main(int argc, char *argv[])
345                  case 'v':                       /* verbose progress reports */
346                          verbose++;
347                          break;
348 +                case 'h':                       /* turn off header */
349 +                        doheader = 0;
350 +                        break;
351                  case 'o':                       /* output format */
352                          switch (argv[i][2]) {
353                          case 'f':
# Line 330 | Line 359 | main(int argc, char *argv[])
359                                  goto userr;
360                          }
361                          break;
362 +                case 'O':                       /* output type */
363 +                        switch (argv[i][2]) {
364 +                        case '0':
365 +                                output = 0;
366 +                                break;
367 +                        case '1':
368 +                                output = 1;
369 +                                break;
370 +                        default:
371 +                                goto userr;
372 +                        }
373 +                        if (argv[i][3])
374 +                                goto userr;
375 +                        break;
376                  case 'm':                       /* Reinhart subdivisions */
377                          rhsubdiv = atoi(argv[++i]);
378                          break;
379                  case 'c':                       /* sky color */
380 +                        inconsistent |= (skycolor[1] <= 1e-4);
381                          skycolor[0] = atof(argv[++i]);
382                          skycolor[1] = atof(argv[++i]);
383                          skycolor[2] = atof(argv[++i]);
384                          break;
385                  case 'd':                       /* solar (direct) only */
386                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
387 <                        if (suncolor[1] <= 1e-4)
387 >                        if (suncolor[1] <= 1e-4) {
388 >                                inconsistent = 1;
389                                  suncolor[0] = suncolor[1] = suncolor[2] = 1;
390 +                        }
391                          break;
392                  case 's':                       /* sky only (no direct) */
393                          suncolor[0] = suncolor[1] = suncolor[2] = 0;
394 <                        if (skycolor[1] <= 1e-4)
394 >                        if (skycolor[1] <= 1e-4) {
395 >                                inconsistent = 1;
396                                  skycolor[0] = skycolor[1] = skycolor[2] = 1;
397 +                        }
398                          break;
399                  case 'r':                       /* rotate distribution */
400                          if (argv[i][2] && argv[i][2] != 'z')
# Line 355 | Line 403 | main(int argc, char *argv[])
403                          break;
404                  case '5':                       /* 5-phase calculation */
405                          nsuns = 1;
406 <                        fixed_sun_sa = 6.797e-05;
406 >                        fixed_sun_sa = PI/360.*atof(argv[++i]);
407 >                        if (fixed_sun_sa <= 0) {
408 >                                fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n",
409 >                                                argv[0]);
410 >                                exit(1);
411 >                        }
412 >                        fixed_sun_sa *= fixed_sun_sa*PI;
413                          break;
414 +                case 'A':                       /* compute average sky */
415 +                        avgSky = 1;
416 +                        break;
417                  default:
418                          goto userr;
419                  }
420          if (i < argc-1)
421                  goto userr;
422 +        if (inconsistent)
423 +                fprintf(stderr, "%s: WARNING: inconsistent -s, -d, -c options!\n",
424 +                                progname);
425          if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
426                  fprintf(stderr, "%s: cannot open '%s' for input\n",
427                                  progname, argv[i]);
# Line 419 | Line 479 | main(int argc, char *argv[])
479          s_latitude = DegToRad(s_latitude);
480          s_longitude = DegToRad(s_longitude);
481          s_meridian = DegToRad(s_meridian);
482 +                                        /* initial allocation */
483 +        mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch);
484                                          /* process each time step in tape */
485          while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) {
486                  double          sda, sta;
487 <                                        /* make space for next time step */
488 <                mtx_offset = 3*nskypatch*ntsteps++;
489 <                mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch);
487 >
488 >                mtx_offset = 3*nskypatch*nstored;
489 >                nstored += !avgSky | !nstored;
490 >                                        /* make space for next row */
491 >                if (nstored > tstorage) {
492 >                        tstorage += (tstorage>>1) + nstored + 7;
493 >                        mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch);
494 >                }
495 >                ntsteps++;              /* keep count of time steps */
496                  if (dif <= 1e-4) {
497 <                        memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
497 >                        if (!avgSky | !mtx_offset)
498 >                                memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
499                          continue;
500                  }
501                  if (verbose && mo != last_monthly)
# Line 451 | Line 520 | main(int argc, char *argv[])
520                                          /* compute sky patch values */
521                  ComputeSky(mtx_data+mtx_offset);
522                  AddDirect(mtx_data+mtx_offset);
523 +                                        /* update cumulative sky? */
524 +                for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
525 +                        mtx_data[i] += mtx_data[mtx_offset+i];
526          }
527                                          /* check for junk at end */
528          while ((i = fgetc(stdin)) != EOF)
# Line 462 | Line 534 | main(int argc, char *argv[])
534                          fputs(buf, stderr); fputc('\n', stderr);
535                          break;
536                  }
537 +        if (!ntsteps) {
538 +                fprintf(stderr, "%s: no valid time steps on input\n", progname);
539 +                exit(1);
540 +        }
541 +        dif = 1./(double)ntsteps;       /* average sky? */
542 +        for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
543 +                mtx_data[i] *= dif;
544                                          /* write out matrix */
545 +        if (outfmt != 'a')
546 +                SET_FILE_BINARY(stdout);
547   #ifdef getc_unlocked
548          flockfile(stdout);
549   #endif
550          if (verbose)
551                  fprintf(stderr, "%s: writing %smatrix with %d time steps...\n",
552 <                                progname, outfmt=='a' ? "" : "binary ", ntsteps);
552 >                                progname, outfmt=='a' ? "" : "binary ", nstored);
553 >        if (doheader) {
554 >                newheader("RADIANCE", stdout);
555 >                printargs(argc, argv, stdout);
556 >                printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude),
557 >                                        -RadToDeg(s_longitude));
558 >                printf("NROWS=%d\n", nskypatch);
559 >                printf("NCOLS=%d\n", nstored);
560 >                printf("NCOMP=3\n");
561 >                if ((outfmt == 'f') | (outfmt == 'd'))
562 >                        fputendian(stdout);
563 >                fputformat((char *)getfmtname(outfmt), stdout);
564 >                putchar('\n');
565 >        }
566                                          /* patches are rows (outer sort) */
567          for (i = 0; i < nskypatch; i++) {
568                  mtx_offset = 3*i;
569                  switch (outfmt) {
570                  case 'a':
571 <                        for (j = 0; j < ntsteps; j++) {
571 >                        for (j = 0; j < nstored; j++) {
572                                  printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset],
573                                                  mtx_data[mtx_offset+1],
574                                                  mtx_data[mtx_offset+2]);
575                                  mtx_offset += 3*nskypatch;
576                          }
577 <                        if (ntsteps > 1)
577 >                        if (nstored > 1)
578                                  fputc('\n', stdout);
579                          break;
580                  case 'f':
581 <                        for (j = 0; j < ntsteps; j++) {
582 <                                fwrite(mtx_data+mtx_offset, sizeof(float), 3,
581 >                        for (j = 0; j < nstored; j++) {
582 >                                putbinary(mtx_data+mtx_offset, sizeof(float), 3,
583                                                  stdout);
584                                  mtx_offset += 3*nskypatch;
585                          }
586                          break;
587                  case 'd':
588 <                        for (j = 0; j < ntsteps; j++) {
588 >                        for (j = 0; j < nstored; j++) {
589                                  double  ment[3];
590                                  ment[0] = mtx_data[mtx_offset];
591                                  ment[1] = mtx_data[mtx_offset+1];
592                                  ment[2] = mtx_data[mtx_offset+2];
593 <                                fwrite(ment, sizeof(double), 3, stdout);
593 >                                putbinary(ment, sizeof(double), 3, stdout);
594                                  mtx_offset += 3*nskypatch;
595                          }
596                          break;
# Line 510 | Line 604 | main(int argc, char *argv[])
604                  fprintf(stderr, "%s: done.\n", progname);
605          exit(0);
606   userr:
607 <        fprintf(stderr, "Usage: %s [-v][-d|-s][-r deg][-m N][-g r g b][-c r g b][-o{f|d}] [tape.wea]\n",
607 >        fprintf(stderr, "Usage: %s [-v][-h][-A][-d|-s][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n",
608                          progname);
609          exit(1);
610   fmterr:
# Line 569 | Line 663 | ComputeSky(float *parr)
663  
664                  /* Limit sky brightness */
665                  if (sky_brightness < 0.01)
666 <                        sky_brightness = 0.01;
666 >                        sky_brightness = 0.01;
667  
668                  /* Calculate illuminance */
669                  index = GetCategoryIndex();
# Line 582 | Line 676 | ComputeSky(float *parr)
676                  index = CalcSkyParamFromIllum();
677          }
678  
679 +        if (output == 1) {                      /* hack for solar radiance */
680 +                diff_illum = diff_irrad * WHTEFFICACY;
681 +                dir_illum = dir_irrad * WHTEFFICACY;
682 +        }
683 +
684          if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
685                  memset(parr, 0, sizeof(float)*3*nskypatch);
686                  return;
# Line 602 | Line 701 | ComputeSky(float *parr)
701          /* Calculate relative horizontal illuminance */
702          norm_diff_illum = CalcRelHorzIllum(parr);
703  
704 +        /* Check for zero sky -- make uniform in that case */
705 +        if (norm_diff_illum <= FTINY) {
706 +                for (i = 1; i < nskypatch; i++)
707 +                        setcolor(parr+3*i, 1., 1., 1.);
708 +                norm_diff_illum = PI;
709 +        }
710          /* Normalization coefficient */
711          norm_diff_illum = diff_illum / norm_diff_illum;
712  
# Line 801 | Line 906 | double CalcSkyClearness()
906          double sz_cubed;        /* Sun zenith angle cubed */
907  
908          /* Calculate sun zenith angle cubed */
909 <        sz_cubed = pow(sun_zenith, 3.0);
909 >        sz_cubed = sun_zenith*sun_zenith*sun_zenith;
910  
911          return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 *
912                          sz_cubed) / (1.0 + 1.041 * sz_cubed);
# Line 832 | Line 937 | double CalcDiffuseIrradiance()
937   double CalcDirectIrradiance()
938   {
939          return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041
940 <                        * pow(sun_zenith, 3.0)));
940 >                        * sun_zenith*sun_zenith*sun_zenith));
941   }
942  
943   /* Calculate sky brightness and clearness from illuminance values */
# Line 871 | Line 976 | int CalcSkyParamFromIllum()
976                  /* Convert illuminance to irradiance */
977                  index = GetCategoryIndex();
978                  diff_irrad = diff_illum / CalcDiffuseIllumRatio(index);
979 <                dir_irrad = dir_illum / CalcDirectIllumRatio(index);
979 >                dir_irrad = CalcDirectIllumRatio(index);
980 >                if (dir_irrad > 0.1)
981 >                        dir_irrad = dir_illum / dir_irrad;
982          
983                  /* Calculate sky brightness and clearness */
984                  sky_brightness = CalcSkyBrightness();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines