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.3 by greg, Sat Jan 19 20:38:36 2013 UTC vs.
Revision 2.17 by greg, Sun Oct 26 17:35:53 2014 UTC

# Line 86 | Line 86 | static const char RCSid[] = "$Id$";
86   #include <string.h>
87   #include <ctype.h>
88   #include "rtmath.h"
89 + #include "platform.h"
90   #include "color.h"
91 + #include "resolu.h"
92  
93   char *progname;                                                         /* Program name */
94   char errmsg[128];                                                       /* Error message buffer */
# Line 108 | Line 110 | double sky_clearness;                  /* Sky clearness */
110   double solar_rad;                       /* Solar radiance */
111   double sun_zenith;                      /* Sun zenith angle (radians) */
112   int     input = 0;                              /* Input type */
113 + int     output = 0;                             /* Output type */
114  
115   extern double dmax( double, double );
116   extern double CalcAirMass();
# Line 208 | Line 211 | static const CategoryBounds SkyClearCat[8] =
211          { 1.950, 2.800 },
212          { 2.800, 4.500 },
213          { 4.500, 6.200 },
214 <        { 6.200, 12.00 }        /* Clear */
214 >        { 6.200, 12.01 }        /* Clear */
215   };
216  
217   /* Luminous efficacy model coefficients */
# Line 247 | Line 250 | static const ModelCoeff DirectLumEff[8] =
250   };
251  
252   #ifndef NSUNPATCH
253 < #define NSUNPATCH       4               /* # patches to spread sun into */
253 > #define NSUNPATCH       4               /* max. # patches to spread sun into */
254   #endif
255  
256   extern int jdate(int month, int day);
# Line 260 | Line 263 | extern double  s_latitude;
263   extern double  s_longitude;
264   extern double  s_meridian;
265  
266 < double          grefl = 0.2;            /* diffuse ground reflectance */
266 > int             nsuns = NSUNPATCH;      /* number of sun patches to use */
267 > double          fixed_sun_sa = -1;      /* fixed solid angle per sun? */
268  
269   int             verbose = 0;            /* progress reports to stderr? */
270  
# Line 268 | Line 272 | int            outfmt = 'a';           /* output format */
272  
273   int             rhsubdiv = 1;           /* Reinhart sky subdivisions */
274  
275 < float           skycolor[3] = {.96, 1.004, 1.118};      /* sky coloration */
275 > COLOR           skycolor = {.96, 1.004, 1.118}; /* sky coloration */
276 > COLOR           suncolor = {1., 1., 1.};        /* sun color */
277 > COLOR           grefl = {.2, .2, .2};           /* ground reflectance */
278  
273 int             do_sun = 1;             /* output direct solar contribution? */
274
279   int             nskypatch;              /* number of Reinhart patches */
280   float           *rh_palt;               /* sky patch altitudes (radians) */
281   float           *rh_pazi;               /* sky patch azimuths (radians) */
# Line 290 | Line 294 | extern int     rh_init(void);
294   extern float *  resize_dmatrix(float *mtx_data, int nsteps, int npatch);
295   extern void     AddDirect(float *parr);
296  
297 +
298 + static const char *
299 + getfmtname(int fmt)
300 + {
301 +        switch (fmt) {
302 +        case 'a':
303 +                return("ascii");
304 +        case 'f':
305 +                return("float");
306 +        case 'd':
307 +                return("double");
308 +        }
309 +        return("unknown");
310 + }
311 +
312 +
313   int
314   main(int argc, char *argv[])
315   {
316          char    buf[256];
317 +        int     doheader = 1;           /* output header? */
318 +        double  rotation = 0;           /* site rotation (degrees) */
319          double  elevation;              /* site elevation (meters) */
320          int     dir_is_horiz;           /* direct is meas. on horizontal? */
321          float   *mtx_data = NULL;       /* our matrix data */
322          int     ntsteps = 0;            /* number of rows in matrix */
323 +        int     step_alloc = 0;
324          int     last_monthly = 0;       /* month of last report */
325          int     mo, da;                 /* month (1-12) and day (1-31) */
326          double  hr;                     /* hour (local standard time) */
# Line 309 | Line 332 | main(int argc, char *argv[])
332                                          /* get options */
333          for (i = 1; i < argc && argv[i][0] == '-'; i++)
334                  switch (argv[i][1]) {
335 <                case 'g':
336 <                        grefl = atof(argv[++i]);
335 >                case 'g':                       /* ground reflectance */
336 >                        grefl[0] = atof(argv[++i]);
337 >                        grefl[1] = atof(argv[++i]);
338 >                        grefl[2] = atof(argv[++i]);
339                          break;
340 <                case 'v':
340 >                case 'v':                       /* verbose progress reports */
341                          verbose++;
342                          break;
343 <                case 'o':
343 >                case 'h':                       /* turn off header */
344 >                        doheader = 0;
345 >                        break;
346 >                case 'o':                       /* output format */
347                          switch (argv[i][2]) {
348                          case 'f':
349                          case 'd':
# Line 326 | Line 354 | main(int argc, char *argv[])
354                                  goto userr;
355                          }
356                          break;
357 <                case 'm':
357 >                case 'O':                       /* output type */
358 >                        switch (argv[i][2]) {
359 >                        case '0':
360 >                                output = 0;
361 >                                break;
362 >                        case '1':
363 >                                output = 1;
364 >                                break;
365 >                        default:
366 >                                goto userr;
367 >                        }
368 >                        if (argv[i][3])
369 >                                goto userr;
370 >                        break;
371 >                case 'm':                       /* Reinhart subdivisions */
372                          rhsubdiv = atoi(argv[++i]);
373                          break;
374 <                case 'c':
374 >                case 'c':                       /* sky color */
375                          skycolor[0] = atof(argv[++i]);
376                          skycolor[1] = atof(argv[++i]);
377                          skycolor[2] = atof(argv[++i]);
378                          break;
379 <                case 'd':
338 <                        do_sun = 1;
379 >                case 'd':                       /* solar (direct) only */
380                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
381 +                        if (suncolor[1] <= 1e-4)
382 +                                suncolor[0] = suncolor[1] = suncolor[2] = 1;
383                          break;
384 <                case 's':
385 <                        do_sun = 0;
384 >                case 's':                       /* sky only (no direct) */
385 >                        suncolor[0] = suncolor[1] = suncolor[2] = 0;
386                          if (skycolor[1] <= 1e-4)
387                                  skycolor[0] = skycolor[1] = skycolor[2] = 1;
388                          break;
389 +                case 'r':                       /* rotate distribution */
390 +                        if (argv[i][2] && argv[i][2] != 'z')
391 +                                goto userr;
392 +                        rotation = atof(argv[++i]);
393 +                        break;
394 +                case '5':                       /* 5-phase calculation */
395 +                        nsuns = 1;
396 +                        fixed_sun_sa = 6.797e-05;
397 +                        break;
398                  default:
399                          goto userr;
400                  }
# Line 397 | Line 449 | main(int argc, char *argv[])
449                                  progname, s_latitude, s_longitude);
450                  fprintf(stderr, "%s: %d sky patches per time step\n",
451                                  progname, nskypatch);
452 +                if (rotation != 0)
453 +                        fprintf(stderr, "%s: rotating output %.0f degrees\n",
454 +                                        progname, rotation);
455          }
456                                          /* convert quantities to radians */
457          s_latitude = DegToRad(s_latitude);
# Line 407 | Line 462 | main(int argc, char *argv[])
462                  double          sda, sta;
463                                          /* make space for next time step */
464                  mtx_offset = 3*nskypatch*ntsteps++;
465 <                mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch);
465 >                if (ntsteps > step_alloc) {
466 >                        step_alloc += (step_alloc>>1) + ntsteps + 7;
467 >                        mtx_data = resize_dmatrix(mtx_data, step_alloc, nskypatch);
468 >                }
469                  if (dif <= 1e-4) {
470                          memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
471                          continue;
# Line 420 | Line 478 | main(int argc, char *argv[])
478                  sda = sdec(julian_date);
479                  sta = stadj(julian_date);
480                  altitude = salt(sda, hr+sta);
481 <                azimuth = sazi(sda, hr+sta) + PI;
481 >                azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation);
482                                          /* convert measured values */
483                  if (dir_is_horiz && altitude > 0.)
484                          dir /= sin(altitude);
# Line 433 | Line 491 | main(int argc, char *argv[])
491                  }
492                                          /* compute sky patch values */
493                  ComputeSky(mtx_data+mtx_offset);
494 <                if (do_sun)
437 <                        AddDirect(mtx_data+mtx_offset);
494 >                AddDirect(mtx_data+mtx_offset);
495          }
496                                          /* check for junk at end */
497          while ((i = fgetc(stdin)) != EOF)
# Line 447 | Line 504 | main(int argc, char *argv[])
504                          break;
505                  }
506                                          /* write out matrix */
507 +        if (outfmt != 'a')
508 +                SET_FILE_BINARY(stdout);
509   #ifdef getc_unlocked
510          flockfile(stdout);
511   #endif
512          if (verbose)
513                  fprintf(stderr, "%s: writing %smatrix with %d time steps...\n",
514                                  progname, outfmt=='a' ? "" : "binary ", ntsteps);
515 +        if (doheader) {
516 +                newheader("RADIANCE", stdout);
517 +                printargs(argc, argv, stdout);
518 +                printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude),
519 +                                        -RadToDeg(s_longitude));
520 +                printf("NROWS=%d\n", nskypatch);
521 +                printf("NCOLS=%d\n", ntsteps);
522 +                printf("NCOMP=3\n");
523 +                fputformat(getfmtname(outfmt), stdout);
524 +                putchar('\n');
525 +        }
526                                          /* patches are rows (outer sort) */
527          for (i = 0; i < nskypatch; i++) {
528                  mtx_offset = 3*i;
# Line 494 | Line 564 | main(int argc, char *argv[])
564                  fprintf(stderr, "%s: done.\n", progname);
565          exit(0);
566   userr:
567 <        fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g refl][-c r g b][-o{f|d}] [tape.wea]\n",
567 >        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",
568                          progname);
569          exit(1);
570   fmterr:
# Line 515 | Line 585 | ComputeSky(float *parr)
585   {
586          int index;                      /* Category index */
587          double norm_diff_illum;         /* Normalized diffuse illuimnance */
518        double zlumin;                  /* Zenith luminance */
588          int i;
589          
590          /* Calculate atmospheric precipitable water content */
591          apwc = CalcPrecipWater(dew_point);
592  
593 <        /* Limit solar altitude to keep circumsolar off zenith */
594 <        if (altitude > DegToRad(87.0))
595 <                altitude = DegToRad(87.0);
593 >        /* Calculate sun zenith angle (don't let it dip below horizon) */
594 >        /* Also limit minimum angle to keep circumsolar off zenith */
595 >        if (altitude <= 0.0)
596 >                sun_zenith = DegToRad(90.0);
597 >        else if (altitude >= DegToRad(87.0))
598 >                sun_zenith = DegToRad(3.0);
599 >        else
600 >                sun_zenith = DegToRad(90.0) - altitude;
601  
528        /* Calculate sun zenith angle */
529        sun_zenith = DegToRad(90.0) - altitude;
530
602          /* Compute the inputs for the calculation of the sky distribution */
603          
604          if (input == 0)                                 /* XXX never used */
# Line 546 | Line 617 | ComputeSky(float *parr)
617                  sky_brightness = CalcSkyBrightness();
618                  sky_clearness =  CalcSkyClearness();
619  
620 +                /* Limit sky clearness */
621 +                if (sky_clearness > 11.9)
622 +                        sky_clearness = 11.9;
623 +
624 +                /* Limit sky brightness */
625 +                if (sky_brightness < 0.01)
626 +                        sky_brightness = 0.01;
627 +
628                  /* Calculate illuminance */
629                  index = GetCategoryIndex();
630                  diff_illum = diff_irrad * CalcDiffuseIllumRatio(index);
# Line 557 | Line 636 | ComputeSky(float *parr)
636                  index = CalcSkyParamFromIllum();
637          }
638  
639 +        if (output == 1) {                      /* hack for solar radiance */
640 +                diff_illum = diff_irrad * WHTEFFICACY;
641 +                dir_illum = dir_irrad * WHTEFFICACY;
642 +        }
643 +
644          if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
645                  memset(parr, 0, sizeof(float)*3*nskypatch);
646                  return;
# Line 565 | Line 649 | ComputeSky(float *parr)
649          parr[0] = diff_illum;
650          if (altitude > 0)
651                  parr[0] += dir_illum * sin(altitude);
652 <        parr[2] = parr[1] = parr[0] *= grefl*(1./PI/WHTEFFICACY);
652 >        parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY);
653 >        multcolor(parr, grefl);
654  
655          /* Calculate Perez sky model parameters */
656          CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index);
# Line 576 | Line 661 | ComputeSky(float *parr)
661          /* Calculate relative horizontal illuminance */
662          norm_diff_illum = CalcRelHorzIllum(parr);
663  
664 +        /* Check for zero sky -- make uniform in that case */
665 +        if (norm_diff_illum <= FTINY) {
666 +                for (i = 1; i < nskypatch; i++)
667 +                        setcolor(parr+3*i, 1., 1., 1.);
668 +                norm_diff_illum = PI;
669 +        }
670          /* Normalization coefficient */
671          norm_diff_illum = diff_illum / norm_diff_illum;
672  
582        /* Calculate relative zenith luminance */
583        zlumin = CalcRelLuminance(sun_zenith, 0.0);
584
585        /* Calculate absolute zenith illuminance */
586        zlumin *= norm_diff_illum;
587
673          /* Apply to sky patches to get absolute radiance values */
674          for (i = 1; i < nskypatch; i++) {
675 <                scalecolor(parr+3*i, zlumin*(1./WHTEFFICACY));
675 >                scalecolor(parr+3*i, norm_diff_illum*(1./WHTEFFICACY));
676                  multcolor(parr+3*i, skycolor);
677          }
678   }
# Line 602 | Line 687 | AddDirect(float *parr)
687          double  wta[NSUNPATCH], wtot;
688          int     i, j, p;
689  
690 <        if (!do_sun || dir_illum < 1e-4)
690 >        if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4)
691                  return;
692 <                                        /* identify NSUNPATCH closest patches */
693 <        for (i = NSUNPATCH; i--; )
692 >                                        /* identify nsuns closest patches */
693 >        if (nsuns > NSUNPATCH)
694 >                nsuns = NSUNPATCH;
695 >        else if (nsuns <= 0)
696 >                nsuns = 1;
697 >        for (i = nsuns; i--; )
698                  near_dprod[i] = -1.;
699          vector(svec, altitude, azimuth);
700          for (p = 1; p < nskypatch; p++) {
# Line 613 | Line 702 | AddDirect(float *parr)
702                  double  dprod;
703                  rh_vector(pvec, p);
704                  dprod = DOT(pvec, svec);
705 <                for (i = 0; i < NSUNPATCH; i++)
705 >                for (i = 0; i < nsuns; i++)
706                          if (dprod > near_dprod[i]) {
707 <                                for (j = NSUNPATCH; --j > i; ) {
707 >                                for (j = nsuns; --j > i; ) {
708                                          near_dprod[j] = near_dprod[j-1];
709                                          near_patch[j] = near_patch[j-1];
710                                  }
# Line 625 | Line 714 | AddDirect(float *parr)
714                          }
715          }
716          wtot = 0;                       /* weight by proximity */
717 <        for (i = NSUNPATCH; i--; )
717 >        for (i = nsuns; i--; )
718                  wtot += wta[i] = 1./(1.002 - near_dprod[i]);
719                                          /* add to nearest patch radiances */
720 <        for (i = NSUNPATCH; i--; ) {
720 >        for (i = nsuns; i--; ) {
721                  float   *pdest = parr + 3*near_patch[i];
722 <                float   val_add = wta[i] * dir_illum /
723 <                                (WHTEFFICACY * wtot * rh_dom[near_patch[i]]);
724 <                *pdest++ += val_add;
725 <                *pdest++ += val_add;
726 <                *pdest++ += val_add;
722 >                float   val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot);
723 >
724 >                val_add /= (fixed_sun_sa > 0)   ? fixed_sun_sa
725 >                                                : rh_dom[near_patch[i]] ;
726 >                *pdest++ += val_add*suncolor[0];
727 >                *pdest++ += val_add*suncolor[1];
728 >                *pdest++ += val_add*suncolor[2];
729          }
730   }
731  
# Line 775 | Line 866 | double CalcSkyClearness()
866          double sz_cubed;        /* Sun zenith angle cubed */
867  
868          /* Calculate sun zenith angle cubed */
869 <        sz_cubed = pow(sun_zenith, 3.0);
869 >        sz_cubed = sun_zenith*sun_zenith*sun_zenith;
870  
871          return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 *
872                          sz_cubed) / (1.0 + 1.041 * sz_cubed);
# Line 806 | Line 897 | double CalcDiffuseIrradiance()
897   double CalcDirectIrradiance()
898   {
899          return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041
900 <                        * pow(sun_zenith, 3.0)));
900 >                        * sun_zenith*sun_zenith*sun_zenith));
901   }
902  
903   /* Calculate sky brightness and clearness from illuminance values */
# Line 832 | Line 923 | int CalcSkyParamFromIllum()
923                  sky_clearness = 12.0;
924  
925          /* Limit sky brightness */
926 <        if (sky_brightness < 0.05)
926 >        if (sky_brightness < 0.01)
927                          sky_brightness = 0.01;
928  
929          while (((fabs(diff_irrad - test1) > 10.0) ||
# Line 856 | Line 947 | int CalcSkyParamFromIllum()
947                          sky_clearness = 12.0;
948          
949                  /* Limit sky brightness */
950 <                if (sky_brightness < 0.05)
950 >                if (sky_brightness < 0.01)
951                          sky_brightness = 0.01;
952          }
953  
# Line 942 | Line 1033 | double CalcRelHorzIllum( float *parr )
1033          double rh_illum = 0.0;  /* Relative horizontal illuminance */
1034  
1035          for (i = 1; i < nskypatch; i++)
1036 <                rh_illum += parr[3*i+1] * rh_cos(i);
1036 >                rh_illum += parr[3*i+1] * rh_cos(i) * rh_dom[i];
1037  
1038 <        return rh_illum * (2.0 * PI / (nskypatch-1));
1038 >        return rh_illum;
1039   }
1040  
1041   /* Calculate earth orbit eccentricity correction factor */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines