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.6 by greg, Fri Jan 25 23:17:31 2013 UTC vs.
Revision 2.22 by greg, Fri Oct 30 17:06:34 2015 UTC

# Line 86 | Line 86 | static const char RCSid[] = "$Id$";
86   #include <string.h>
87   #include <ctype.h>
88   #include "rtmath.h"
89 + #include "resolu.h"
90 + #include "platform.h"
91   #include "color.h"
92 + #include "resolu.h"
93  
94   char *progname;                                                         /* Program name */
95   char errmsg[128];                                                       /* Error message buffer */
# Line 108 | Line 111 | double sky_clearness;                  /* Sky clearness */
111   double solar_rad;                       /* Solar radiance */
112   double sun_zenith;                      /* Sun zenith angle (radians) */
113   int     input = 0;                              /* Input type */
114 + int     output = 0;                             /* Output type */
115  
116   extern double dmax( double, double );
117   extern double CalcAirMass();
# Line 208 | Line 212 | static const CategoryBounds SkyClearCat[8] =
212          { 1.950, 2.800 },
213          { 2.800, 4.500 },
214          { 4.500, 6.200 },
215 <        { 6.200, 12.00 }        /* Clear */
215 >        { 6.200, 12.01 }        /* Clear */
216   };
217  
218   /* Luminous efficacy model coefficients */
# Line 247 | Line 251 | static const ModelCoeff DirectLumEff[8] =
251   };
252  
253   #ifndef NSUNPATCH
254 < #define NSUNPATCH       4               /* # patches to spread sun into */
254 > #define NSUNPATCH       4               /* max. # patches to spread sun into */
255   #endif
256  
257   extern int jdate(int month, int day);
# Line 260 | Line 264 | extern double  s_latitude;
264   extern double  s_longitude;
265   extern double  s_meridian;
266  
267 + int             nsuns = NSUNPATCH;      /* number of sun patches to use */
268 + double          fixed_sun_sa = -1;      /* fixed solid angle per sun? */
269 +
270   int             verbose = 0;            /* progress reports to stderr? */
271  
272   int             outfmt = 'a';           /* output format */
# Line 288 | Line 295 | extern int     rh_init(void);
295   extern float *  resize_dmatrix(float *mtx_data, int nsteps, int npatch);
296   extern void     AddDirect(float *parr);
297  
298 +
299 + static const char *
300 + getfmtname(int fmt)
301 + {
302 +        switch (fmt) {
303 +        case 'a':
304 +                return("ascii");
305 +        case 'f':
306 +                return("float");
307 +        case 'd':
308 +                return("double");
309 +        }
310 +        return("unknown");
311 + }
312 +
313 +
314   int
315   main(int argc, char *argv[])
316   {
317          char    buf[256];
318 +        int     doheader = 1;           /* output header? */
319 +        double  rotation = 0;           /* site rotation (degrees) */
320          double  elevation;              /* site elevation (meters) */
321          int     dir_is_horiz;           /* direct is meas. on horizontal? */
322          float   *mtx_data = NULL;       /* our matrix data */
323          int     ntsteps = 0;            /* number of rows in matrix */
324 +        int     step_alloc = 0;
325          int     last_monthly = 0;       /* month of last report */
326 +        int     inconsistent = 0;       /* inconsistent options set? */
327          int     mo, da;                 /* month (1-12) and day (1-31) */
328          double  hr;                     /* hour (local standard time) */
329          double  dir, dif;               /* direct and diffuse values */
# Line 315 | Line 342 | main(int argc, char *argv[])
342                  case 'v':                       /* verbose progress reports */
343                          verbose++;
344                          break;
345 +                case 'h':                       /* turn off header */
346 +                        doheader = 0;
347 +                        break;
348                  case 'o':                       /* output format */
349                          switch (argv[i][2]) {
350                          case 'f':
# Line 326 | Line 356 | main(int argc, char *argv[])
356                                  goto userr;
357                          }
358                          break;
359 +                case 'O':                       /* output type */
360 +                        switch (argv[i][2]) {
361 +                        case '0':
362 +                                output = 0;
363 +                                break;
364 +                        case '1':
365 +                                output = 1;
366 +                                break;
367 +                        default:
368 +                                goto userr;
369 +                        }
370 +                        if (argv[i][3])
371 +                                goto userr;
372 +                        break;
373                  case 'm':                       /* Reinhart subdivisions */
374                          rhsubdiv = atoi(argv[++i]);
375                          break;
376                  case 'c':                       /* sky color */
377 +                        inconsistent |= (skycolor[1] <= 1e-4);
378                          skycolor[0] = atof(argv[++i]);
379                          skycolor[1] = atof(argv[++i]);
380                          skycolor[2] = atof(argv[++i]);
381                          break;
382                  case 'd':                       /* solar (direct) only */
383                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
384 <                        if (suncolor[1] <= 1e-4)
384 >                        if (suncolor[1] <= 1e-4) {
385 >                                inconsistent = 1;
386                                  suncolor[0] = suncolor[1] = suncolor[2] = 1;
387 +                        }
388                          break;
389                  case 's':                       /* sky only (no direct) */
390                          suncolor[0] = suncolor[1] = suncolor[2] = 0;
391 <                        if (skycolor[1] <= 1e-4)
391 >                        if (skycolor[1] <= 1e-4) {
392 >                                inconsistent = 1;
393                                  skycolor[0] = skycolor[1] = skycolor[2] = 1;
394 +                        }
395                          break;
396 +                case 'r':                       /* rotate distribution */
397 +                        if (argv[i][2] && argv[i][2] != 'z')
398 +                                goto userr;
399 +                        rotation = atof(argv[++i]);
400 +                        break;
401 +                case '5':                       /* 5-phase calculation */
402 +                        nsuns = 1;
403 +                        fixed_sun_sa = PI/360.*atof(argv[++i]);
404 +                        if (fixed_sun_sa <= 0) {
405 +                                fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n",
406 +                                                argv[0]);
407 +                                exit(1);
408 +                        }
409 +                        fixed_sun_sa *= fixed_sun_sa*PI;
410 +                        break;
411                  default:
412                          goto userr;
413                  }
414          if (i < argc-1)
415                  goto userr;
416 +        if (inconsistent)
417 +                fprintf(stderr, "%s: WARNING: inconsistent -s, -d, -c options!\n",
418 +                                progname);
419          if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
420                  fprintf(stderr, "%s: cannot open '%s' for input\n",
421                                  progname, argv[i]);
# Line 398 | Line 465 | main(int argc, char *argv[])
465                                  progname, s_latitude, s_longitude);
466                  fprintf(stderr, "%s: %d sky patches per time step\n",
467                                  progname, nskypatch);
468 +                if (rotation != 0)
469 +                        fprintf(stderr, "%s: rotating output %.0f degrees\n",
470 +                                        progname, rotation);
471          }
472                                          /* convert quantities to radians */
473          s_latitude = DegToRad(s_latitude);
# Line 408 | Line 478 | main(int argc, char *argv[])
478                  double          sda, sta;
479                                          /* make space for next time step */
480                  mtx_offset = 3*nskypatch*ntsteps++;
481 <                mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch);
481 >                if (ntsteps > step_alloc) {
482 >                        step_alloc += (step_alloc>>1) + ntsteps + 7;
483 >                        mtx_data = resize_dmatrix(mtx_data, step_alloc, nskypatch);
484 >                }
485                  if (dif <= 1e-4) {
486                          memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
487                          continue;
# Line 421 | Line 494 | main(int argc, char *argv[])
494                  sda = sdec(julian_date);
495                  sta = stadj(julian_date);
496                  altitude = salt(sda, hr+sta);
497 <                azimuth = sazi(sda, hr+sta) + PI;
497 >                azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation);
498                                          /* convert measured values */
499                  if (dir_is_horiz && altitude > 0.)
500                          dir /= sin(altitude);
# Line 447 | Line 520 | main(int argc, char *argv[])
520                          break;
521                  }
522                                          /* write out matrix */
523 +        if (outfmt != 'a')
524 +                SET_FILE_BINARY(stdout);
525   #ifdef getc_unlocked
526          flockfile(stdout);
527   #endif
528          if (verbose)
529                  fprintf(stderr, "%s: writing %smatrix with %d time steps...\n",
530                                  progname, outfmt=='a' ? "" : "binary ", ntsteps);
531 +        if (doheader) {
532 +                newheader("RADIANCE", stdout);
533 +                printargs(argc, argv, stdout);
534 +                printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude),
535 +                                        -RadToDeg(s_longitude));
536 +                printf("NROWS=%d\n", nskypatch);
537 +                printf("NCOLS=%d\n", ntsteps);
538 +                printf("NCOMP=3\n");
539 +                fputformat((char *)getfmtname(outfmt), stdout);
540 +                putchar('\n');
541 +        }
542                                          /* patches are rows (outer sort) */
543          for (i = 0; i < nskypatch; i++) {
544                  mtx_offset = 3*i;
# Line 494 | Line 580 | main(int argc, char *argv[])
580                  fprintf(stderr, "%s: done.\n", progname);
581          exit(0);
582   userr:
583 <        fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g r g b][-c r g b][-o{f|d}] [tape.wea]\n",
583 >        fprintf(stderr, "Usage: %s [-v][-h][-d|-s][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n",
584                          progname);
585          exit(1);
586   fmterr:
# Line 515 | Line 601 | ComputeSky(float *parr)
601   {
602          int index;                      /* Category index */
603          double norm_diff_illum;         /* Normalized diffuse illuimnance */
518        double zlumin;                  /* Zenith luminance */
604          int i;
605          
606          /* Calculate atmospheric precipitable water content */
# Line 548 | Line 633 | ComputeSky(float *parr)
633                  sky_brightness = CalcSkyBrightness();
634                  sky_clearness =  CalcSkyClearness();
635  
636 +                /* Limit sky clearness */
637 +                if (sky_clearness > 11.9)
638 +                        sky_clearness = 11.9;
639 +
640 +                /* Limit sky brightness */
641 +                if (sky_brightness < 0.01)
642 +                        sky_brightness = 0.01;
643 +
644                  /* Calculate illuminance */
645                  index = GetCategoryIndex();
646                  diff_illum = diff_irrad * CalcDiffuseIllumRatio(index);
# Line 559 | Line 652 | ComputeSky(float *parr)
652                  index = CalcSkyParamFromIllum();
653          }
654  
655 +        if (output == 1) {                      /* hack for solar radiance */
656 +                diff_illum = diff_irrad * WHTEFFICACY;
657 +                dir_illum = dir_irrad * WHTEFFICACY;
658 +        }
659 +
660          if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
661                  memset(parr, 0, sizeof(float)*3*nskypatch);
662                  return;
# Line 579 | Line 677 | ComputeSky(float *parr)
677          /* Calculate relative horizontal illuminance */
678          norm_diff_illum = CalcRelHorzIllum(parr);
679  
680 +        /* Check for zero sky -- make uniform in that case */
681 +        if (norm_diff_illum <= FTINY) {
682 +                for (i = 1; i < nskypatch; i++)
683 +                        setcolor(parr+3*i, 1., 1., 1.);
684 +                norm_diff_illum = PI;
685 +        }
686          /* Normalization coefficient */
687          norm_diff_illum = diff_illum / norm_diff_illum;
688  
585        /* Calculate relative zenith luminance */
586        zlumin = CalcRelLuminance(sun_zenith, 0.0);
587
588        /* Calculate absolute zenith illuminance */
589        zlumin *= norm_diff_illum;
590
689          /* Apply to sky patches to get absolute radiance values */
690          for (i = 1; i < nskypatch; i++) {
691 <                scalecolor(parr+3*i, zlumin*(1./WHTEFFICACY));
691 >                scalecolor(parr+3*i, norm_diff_illum*(1./WHTEFFICACY));
692                  multcolor(parr+3*i, skycolor);
693          }
694   }
# Line 607 | Line 705 | AddDirect(float *parr)
705  
706          if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4)
707                  return;
708 <                                        /* identify NSUNPATCH closest patches */
709 <        for (i = NSUNPATCH; i--; )
708 >                                        /* identify nsuns closest patches */
709 >        if (nsuns > NSUNPATCH)
710 >                nsuns = NSUNPATCH;
711 >        else if (nsuns <= 0)
712 >                nsuns = 1;
713 >        for (i = nsuns; i--; )
714                  near_dprod[i] = -1.;
715          vector(svec, altitude, azimuth);
716          for (p = 1; p < nskypatch; p++) {
# Line 616 | Line 718 | AddDirect(float *parr)
718                  double  dprod;
719                  rh_vector(pvec, p);
720                  dprod = DOT(pvec, svec);
721 <                for (i = 0; i < NSUNPATCH; i++)
721 >                for (i = 0; i < nsuns; i++)
722                          if (dprod > near_dprod[i]) {
723 <                                for (j = NSUNPATCH; --j > i; ) {
723 >                                for (j = nsuns; --j > i; ) {
724                                          near_dprod[j] = near_dprod[j-1];
725                                          near_patch[j] = near_patch[j-1];
726                                  }
# Line 628 | Line 730 | AddDirect(float *parr)
730                          }
731          }
732          wtot = 0;                       /* weight by proximity */
733 <        for (i = NSUNPATCH; i--; )
733 >        for (i = nsuns; i--; )
734                  wtot += wta[i] = 1./(1.002 - near_dprod[i]);
735                                          /* add to nearest patch radiances */
736 <        for (i = NSUNPATCH; i--; ) {
736 >        for (i = nsuns; i--; ) {
737                  float   *pdest = parr + 3*near_patch[i];
738 <                float   val_add = wta[i] * dir_illum /
739 <                                (WHTEFFICACY * wtot * rh_dom[near_patch[i]]);
738 >                float   val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot);
739 >
740 >                val_add /= (fixed_sun_sa > 0)   ? fixed_sun_sa
741 >                                                : rh_dom[near_patch[i]] ;
742                  *pdest++ += val_add*suncolor[0];
743                  *pdest++ += val_add*suncolor[1];
744                  *pdest++ += val_add*suncolor[2];
# Line 778 | Line 882 | double CalcSkyClearness()
882          double sz_cubed;        /* Sun zenith angle cubed */
883  
884          /* Calculate sun zenith angle cubed */
885 <        sz_cubed = pow(sun_zenith, 3.0);
885 >        sz_cubed = sun_zenith*sun_zenith*sun_zenith;
886  
887          return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 *
888                          sz_cubed) / (1.0 + 1.041 * sz_cubed);
# Line 809 | Line 913 | double CalcDiffuseIrradiance()
913   double CalcDirectIrradiance()
914   {
915          return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041
916 <                        * pow(sun_zenith, 3.0)));
916 >                        * sun_zenith*sun_zenith*sun_zenith));
917   }
918  
919   /* Calculate sky brightness and clearness from illuminance values */
# Line 835 | Line 939 | int CalcSkyParamFromIllum()
939                  sky_clearness = 12.0;
940  
941          /* Limit sky brightness */
942 <        if (sky_brightness < 0.05)
942 >        if (sky_brightness < 0.01)
943                          sky_brightness = 0.01;
944  
945          while (((fabs(diff_irrad - test1) > 10.0) ||
# Line 859 | Line 963 | int CalcSkyParamFromIllum()
963                          sky_clearness = 12.0;
964          
965                  /* Limit sky brightness */
966 <                if (sky_brightness < 0.05)
966 >                if (sky_brightness < 0.01)
967                          sky_brightness = 0.01;
968          }
969  
# Line 945 | Line 1049 | double CalcRelHorzIllum( float *parr )
1049          double rh_illum = 0.0;  /* Relative horizontal illuminance */
1050  
1051          for (i = 1; i < nskypatch; i++)
1052 <                rh_illum += parr[3*i+1] * rh_cos(i);
1052 >                rh_illum += parr[3*i+1] * rh_cos(i) * rh_dom[i];
1053  
1054 <        return rh_illum * (2.0 * PI / (nskypatch-1));
1054 >        return rh_illum;
1055   }
1056  
1057   /* Calculate earth orbit eccentricity correction factor */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines