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.14 by greg, Fri May 30 00:00:54 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  
92   char *progname;                                                         /* Program name */
# Line 108 | Line 109 | double sky_clearness;                  /* Sky clearness */
109   double solar_rad;                       /* Solar radiance */
110   double sun_zenith;                      /* Sun zenith angle (radians) */
111   int     input = 0;                              /* Input type */
112 + int     output = 0;                             /* Output type */
113  
114   extern double dmax( double, double );
115   extern double CalcAirMass();
# Line 208 | Line 210 | static const CategoryBounds SkyClearCat[8] =
210          { 1.950, 2.800 },
211          { 2.800, 4.500 },
212          { 4.500, 6.200 },
213 <        { 6.200, 12.00 }        /* Clear */
213 >        { 6.200, 12.01 }        /* Clear */
214   };
215  
216   /* Luminous efficacy model coefficients */
# Line 247 | Line 249 | static const ModelCoeff DirectLumEff[8] =
249   };
250  
251   #ifndef NSUNPATCH
252 < #define NSUNPATCH       4               /* # patches to spread sun into */
252 > #define NSUNPATCH       4               /* max. # patches to spread sun into */
253   #endif
254  
255   extern int jdate(int month, int day);
# Line 260 | Line 262 | extern double  s_latitude;
262   extern double  s_longitude;
263   extern double  s_meridian;
264  
265 < double          grefl = 0.2;            /* diffuse ground reflectance */
265 > int             nsuns = NSUNPATCH;      /* number of sun patches to use */
266 > double          fixed_sun_sa = -1;      /* fixed solid angle per sun? */
267  
268   int             verbose = 0;            /* progress reports to stderr? */
269  
# Line 268 | Line 271 | int            outfmt = 'a';           /* output format */
271  
272   int             rhsubdiv = 1;           /* Reinhart sky subdivisions */
273  
274 < float           skycolor[3] = {.96, 1.004, 1.118};      /* sky coloration */
274 > COLOR           skycolor = {.96, 1.004, 1.118}; /* sky coloration */
275 > COLOR           suncolor = {1., 1., 1.};        /* sun color */
276 > COLOR           grefl = {.2, .2, .2};           /* ground reflectance */
277  
273 int             do_sun = 1;             /* output direct solar contribution? */
274
278   int             nskypatch;              /* number of Reinhart patches */
279   float           *rh_palt;               /* sky patch altitudes (radians) */
280   float           *rh_pazi;               /* sky patch azimuths (radians) */
# Line 290 | Line 293 | extern int     rh_init(void);
293   extern float *  resize_dmatrix(float *mtx_data, int nsteps, int npatch);
294   extern void     AddDirect(float *parr);
295  
296 +
297 + static const char *
298 + getfmtname(int fmt)
299 + {
300 +        switch (fmt) {
301 +        case 'a':
302 +                return("ascii");
303 +        case 'f':
304 +                return("float");
305 +        case 'd':
306 +                return("double");
307 +        }
308 +        return("unknown");
309 + }
310 +
311 +
312   int
313   main(int argc, char *argv[])
314   {
315          char    buf[256];
316 +        int     doheader = 1;           /* output header? */
317 +        double  rotation = 0;           /* site rotation (degrees) */
318          double  elevation;              /* site elevation (meters) */
319          int     dir_is_horiz;           /* direct is meas. on horizontal? */
320          float   *mtx_data = NULL;       /* our matrix data */
# Line 309 | Line 330 | main(int argc, char *argv[])
330                                          /* get options */
331          for (i = 1; i < argc && argv[i][0] == '-'; i++)
332                  switch (argv[i][1]) {
333 <                case 'g':
334 <                        grefl = atof(argv[++i]);
333 >                case 'g':                       /* ground reflectance */
334 >                        grefl[0] = atof(argv[++i]);
335 >                        grefl[1] = atof(argv[++i]);
336 >                        grefl[2] = atof(argv[++i]);
337                          break;
338 <                case 'v':
338 >                case 'v':                       /* verbose progress reports */
339                          verbose++;
340                          break;
341 <                case 'o':
341 >                case 'h':                       /* turn off header */
342 >                        doheader = 0;
343 >                        break;
344 >                case 'o':                       /* output format */
345                          switch (argv[i][2]) {
346                          case 'f':
347                          case 'd':
# Line 326 | Line 352 | main(int argc, char *argv[])
352                                  goto userr;
353                          }
354                          break;
355 <                case 'm':
355 >                case 'O':                       /* output type */
356 >                        switch (argv[i][2]) {
357 >                        case '0':
358 >                                output = 0;
359 >                                break;
360 >                        case '1':
361 >                                output = 1;
362 >                                break;
363 >                        default:
364 >                                goto userr;
365 >                        }
366 >                        if (argv[i][3])
367 >                                goto userr;
368 >                        break;
369 >                case 'm':                       /* Reinhart subdivisions */
370                          rhsubdiv = atoi(argv[++i]);
371                          break;
372 <                case 'c':
372 >                case 'c':                       /* sky color */
373                          skycolor[0] = atof(argv[++i]);
374                          skycolor[1] = atof(argv[++i]);
375                          skycolor[2] = atof(argv[++i]);
376                          break;
377 <                case 'd':
338 <                        do_sun = 1;
377 >                case 'd':                       /* solar (direct) only */
378                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
379 +                        if (suncolor[1] <= 1e-4)
380 +                                suncolor[0] = suncolor[1] = suncolor[2] = 1;
381                          break;
382 <                case 's':
383 <                        do_sun = 0;
382 >                case 's':                       /* sky only (no direct) */
383 >                        suncolor[0] = suncolor[1] = suncolor[2] = 0;
384                          if (skycolor[1] <= 1e-4)
385                                  skycolor[0] = skycolor[1] = skycolor[2] = 1;
386                          break;
387 +                case 'r':                       /* rotate distribution */
388 +                        if (argv[i][2] && argv[i][2] != 'z')
389 +                                goto userr;
390 +                        rotation = atof(argv[++i]);
391 +                        break;
392 +                case '5':                       /* 5-phase calculation */
393 +                        nsuns = 1;
394 +                        fixed_sun_sa = 6.797e-05;
395 +                        break;
396                  default:
397                          goto userr;
398                  }
# Line 397 | Line 447 | main(int argc, char *argv[])
447                                  progname, s_latitude, s_longitude);
448                  fprintf(stderr, "%s: %d sky patches per time step\n",
449                                  progname, nskypatch);
450 +                if (rotation != 0)
451 +                        fprintf(stderr, "%s: rotating output %.0f degrees\n",
452 +                                        progname, rotation);
453          }
454                                          /* convert quantities to radians */
455          s_latitude = DegToRad(s_latitude);
# Line 420 | Line 473 | main(int argc, char *argv[])
473                  sda = sdec(julian_date);
474                  sta = stadj(julian_date);
475                  altitude = salt(sda, hr+sta);
476 <                azimuth = sazi(sda, hr+sta) + PI;
476 >                azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation);
477                                          /* convert measured values */
478                  if (dir_is_horiz && altitude > 0.)
479                          dir /= sin(altitude);
# Line 433 | Line 486 | main(int argc, char *argv[])
486                  }
487                                          /* compute sky patch values */
488                  ComputeSky(mtx_data+mtx_offset);
489 <                if (do_sun)
437 <                        AddDirect(mtx_data+mtx_offset);
489 >                AddDirect(mtx_data+mtx_offset);
490          }
491                                          /* check for junk at end */
492          while ((i = fgetc(stdin)) != EOF)
# Line 447 | Line 499 | main(int argc, char *argv[])
499                          break;
500                  }
501                                          /* write out matrix */
502 +        if (outfmt != 'a')
503 +                SET_FILE_BINARY(stdout);
504   #ifdef getc_unlocked
505          flockfile(stdout);
506   #endif
507          if (verbose)
508                  fprintf(stderr, "%s: writing %smatrix with %d time steps...\n",
509                                  progname, outfmt=='a' ? "" : "binary ", ntsteps);
510 +        if (doheader) {
511 +                newheader("RADIANCE", stdout);
512 +                printargs(argc, argv, stdout);
513 +                printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude),
514 +                                        -RadToDeg(s_longitude));
515 +                printf("NROWS=%d\n", nskypatch);
516 +                printf("NCOLS=%d\n", ntsteps);
517 +                printf("NCOMP=3\n");
518 +                fputformat(getfmtname(outfmt), stdout);
519 +                putchar('\n');
520 +        }
521                                          /* patches are rows (outer sort) */
522          for (i = 0; i < nskypatch; i++) {
523                  mtx_offset = 3*i;
# Line 494 | Line 559 | main(int argc, char *argv[])
559                  fprintf(stderr, "%s: done.\n", progname);
560          exit(0);
561   userr:
562 <        fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g refl][-c r g b][-o{f|d}] [tape.wea]\n",
562 >        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",
563                          progname);
564          exit(1);
565   fmterr:
# Line 515 | Line 580 | ComputeSky(float *parr)
580   {
581          int index;                      /* Category index */
582          double norm_diff_illum;         /* Normalized diffuse illuimnance */
518        double zlumin;                  /* Zenith luminance */
583          int i;
584          
585          /* Calculate atmospheric precipitable water content */
586          apwc = CalcPrecipWater(dew_point);
587  
588 <        /* Limit solar altitude to keep circumsolar off zenith */
589 <        if (altitude > DegToRad(87.0))
590 <                altitude = DegToRad(87.0);
588 >        /* Calculate sun zenith angle (don't let it dip below horizon) */
589 >        /* Also limit minimum angle to keep circumsolar off zenith */
590 >        if (altitude <= 0.0)
591 >                sun_zenith = DegToRad(90.0);
592 >        else if (altitude >= DegToRad(87.0))
593 >                sun_zenith = DegToRad(3.0);
594 >        else
595 >                sun_zenith = DegToRad(90.0) - altitude;
596  
528        /* Calculate sun zenith angle */
529        sun_zenith = DegToRad(90.0) - altitude;
530
597          /* Compute the inputs for the calculation of the sky distribution */
598          
599          if (input == 0)                                 /* XXX never used */
# Line 546 | Line 612 | ComputeSky(float *parr)
612                  sky_brightness = CalcSkyBrightness();
613                  sky_clearness =  CalcSkyClearness();
614  
615 +                /* Limit sky clearness */
616 +                if (sky_clearness > 11.9)
617 +                        sky_clearness = 11.9;
618 +
619 +                /* Limit sky brightness */
620 +                if (sky_brightness < 0.01)
621 +                        sky_brightness = 0.01;
622 +
623                  /* Calculate illuminance */
624                  index = GetCategoryIndex();
625                  diff_illum = diff_irrad * CalcDiffuseIllumRatio(index);
# Line 557 | Line 631 | ComputeSky(float *parr)
631                  index = CalcSkyParamFromIllum();
632          }
633  
634 +        if (output == 1) {                      /* hack for solar radiance */
635 +                diff_illum = diff_irrad * WHTEFFICACY;
636 +                dir_illum = dir_irrad * WHTEFFICACY;
637 +        }
638 +
639          if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
640                  memset(parr, 0, sizeof(float)*3*nskypatch);
641                  return;
# Line 565 | Line 644 | ComputeSky(float *parr)
644          parr[0] = diff_illum;
645          if (altitude > 0)
646                  parr[0] += dir_illum * sin(altitude);
647 <        parr[2] = parr[1] = parr[0] *= grefl*(1./PI/WHTEFFICACY);
647 >        parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY);
648 >        multcolor(parr, grefl);
649  
650          /* Calculate Perez sky model parameters */
651          CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index);
# Line 576 | Line 656 | ComputeSky(float *parr)
656          /* Calculate relative horizontal illuminance */
657          norm_diff_illum = CalcRelHorzIllum(parr);
658  
659 +        /* Check for zero sky -- make uniform in that case */
660 +        if (norm_diff_illum <= FTINY) {
661 +                for (i = 1; i < nskypatch; i++)
662 +                        setcolor(parr+3*i, 1., 1., 1.);
663 +                norm_diff_illum = PI;
664 +        }
665          /* Normalization coefficient */
666          norm_diff_illum = diff_illum / norm_diff_illum;
667  
582        /* Calculate relative zenith luminance */
583        zlumin = CalcRelLuminance(sun_zenith, 0.0);
584
585        /* Calculate absolute zenith illuminance */
586        zlumin *= norm_diff_illum;
587
668          /* Apply to sky patches to get absolute radiance values */
669          for (i = 1; i < nskypatch; i++) {
670 <                scalecolor(parr+3*i, zlumin*(1./WHTEFFICACY));
670 >                scalecolor(parr+3*i, norm_diff_illum*(1./WHTEFFICACY));
671                  multcolor(parr+3*i, skycolor);
672          }
673   }
# Line 602 | Line 682 | AddDirect(float *parr)
682          double  wta[NSUNPATCH], wtot;
683          int     i, j, p;
684  
685 <        if (!do_sun || dir_illum < 1e-4)
685 >        if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4)
686                  return;
687 <                                        /* identify NSUNPATCH closest patches */
688 <        for (i = NSUNPATCH; i--; )
687 >                                        /* identify nsuns closest patches */
688 >        if (nsuns > NSUNPATCH)
689 >                nsuns = NSUNPATCH;
690 >        else if (nsuns <= 0)
691 >                nsuns = 1;
692 >        for (i = nsuns; i--; )
693                  near_dprod[i] = -1.;
694          vector(svec, altitude, azimuth);
695          for (p = 1; p < nskypatch; p++) {
# Line 613 | Line 697 | AddDirect(float *parr)
697                  double  dprod;
698                  rh_vector(pvec, p);
699                  dprod = DOT(pvec, svec);
700 <                for (i = 0; i < NSUNPATCH; i++)
700 >                for (i = 0; i < nsuns; i++)
701                          if (dprod > near_dprod[i]) {
702 <                                for (j = NSUNPATCH; --j > i; ) {
702 >                                for (j = nsuns; --j > i; ) {
703                                          near_dprod[j] = near_dprod[j-1];
704                                          near_patch[j] = near_patch[j-1];
705                                  }
# Line 625 | Line 709 | AddDirect(float *parr)
709                          }
710          }
711          wtot = 0;                       /* weight by proximity */
712 <        for (i = NSUNPATCH; i--; )
712 >        for (i = nsuns; i--; )
713                  wtot += wta[i] = 1./(1.002 - near_dprod[i]);
714                                          /* add to nearest patch radiances */
715 <        for (i = NSUNPATCH; i--; ) {
715 >        for (i = nsuns; i--; ) {
716                  float   *pdest = parr + 3*near_patch[i];
717 <                float   val_add = wta[i] * dir_illum /
718 <                                (WHTEFFICACY * wtot * rh_dom[near_patch[i]]);
719 <                *pdest++ += val_add;
720 <                *pdest++ += val_add;
721 <                *pdest++ += val_add;
717 >                float   val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot);
718 >
719 >                val_add /= (fixed_sun_sa > 0)   ? fixed_sun_sa
720 >                                                : rh_dom[near_patch[i]] ;
721 >                *pdest++ += val_add*suncolor[0];
722 >                *pdest++ += val_add*suncolor[1];
723 >                *pdest++ += val_add*suncolor[2];
724          }
725   }
726  
# Line 775 | Line 861 | double CalcSkyClearness()
861          double sz_cubed;        /* Sun zenith angle cubed */
862  
863          /* Calculate sun zenith angle cubed */
864 <        sz_cubed = pow(sun_zenith, 3.0);
864 >        sz_cubed = sun_zenith*sun_zenith*sun_zenith;
865  
866          return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 *
867                          sz_cubed) / (1.0 + 1.041 * sz_cubed);
# Line 806 | Line 892 | double CalcDiffuseIrradiance()
892   double CalcDirectIrradiance()
893   {
894          return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041
895 <                        * pow(sun_zenith, 3.0)));
895 >                        * sun_zenith*sun_zenith*sun_zenith));
896   }
897  
898   /* Calculate sky brightness and clearness from illuminance values */
# Line 832 | Line 918 | int CalcSkyParamFromIllum()
918                  sky_clearness = 12.0;
919  
920          /* Limit sky brightness */
921 <        if (sky_brightness < 0.05)
921 >        if (sky_brightness < 0.01)
922                          sky_brightness = 0.01;
923  
924          while (((fabs(diff_irrad - test1) > 10.0) ||
# Line 856 | Line 942 | int CalcSkyParamFromIllum()
942                          sky_clearness = 12.0;
943          
944                  /* Limit sky brightness */
945 <                if (sky_brightness < 0.05)
945 >                if (sky_brightness < 0.01)
946                          sky_brightness = 0.01;
947          }
948  
# Line 942 | Line 1028 | double CalcRelHorzIllum( float *parr )
1028          double rh_illum = 0.0;  /* Relative horizontal illuminance */
1029  
1030          for (i = 1; i < nskypatch; i++)
1031 <                rh_illum += parr[3*i+1] * rh_cos(i);
1031 >                rh_illum += parr[3*i+1] * rh_cos(i) * rh_dom[i];
1032  
1033 <        return rh_illum * (2.0 * PI / (nskypatch-1));
1033 >        return rh_illum;
1034   }
1035  
1036   /* Calculate earth orbit eccentricity correction factor */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines