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.1 by greg, Fri Jan 18 01:12:59 2013 UTC vs.
Revision 2.21 by greg, Wed Sep 2 22:52:04 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 246 | Line 250 | static const ModelCoeff DirectLumEff[8] =
250          { 101.18,  1.58, -1.10,  -8.29 }
251   };
252  
253 + #ifndef NSUNPATCH
254 + #define NSUNPATCH       4               /* max. # patches to spread sun into */
255 + #endif
256 +
257   extern int jdate(int month, int day);
258   extern double stadj(int  jd);
259   extern double sdec(int  jd);
# Line 256 | Line 264 | extern double  s_latitude;
264   extern double  s_longitude;
265   extern double  s_meridian;
266  
267 < double          grefl = 0.2;            /* diffuse ground reflectance */
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  
# Line 264 | Line 273 | int            outfmt = 'a';           /* output format */
273  
274   int             rhsubdiv = 1;           /* Reinhart sky subdivisions */
275  
276 < float           skycolor[3] = {.96, 1.004, 1.118};      /* sky coloration */
276 > COLOR           skycolor = {.96, 1.004, 1.118}; /* sky coloration */
277 > COLOR           suncolor = {1., 1., 1.};        /* sun color */
278 > COLOR           grefl = {.2, .2, .2};           /* ground reflectance */
279  
269 int             do_sun = 1;             /* output direct solar contribution? */
270
280   int             nskypatch;              /* number of Reinhart patches */
281   float           *rh_palt;               /* sky patch altitudes (radians) */
282   float           *rh_pazi;               /* sky patch azimuths (radians) */
# Line 286 | 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     mo, da;                 /* month (1-12) and day (1-31) */
327          double  hr;                     /* hour (local standard time) */
# Line 305 | Line 333 | main(int argc, char *argv[])
333                                          /* get options */
334          for (i = 1; i < argc && argv[i][0] == '-'; i++)
335                  switch (argv[i][1]) {
336 <                case 'g':
337 <                        grefl = atof(argv[++i]);
336 >                case 'g':                       /* ground reflectance */
337 >                        grefl[0] = atof(argv[++i]);
338 >                        grefl[1] = atof(argv[++i]);
339 >                        grefl[2] = atof(argv[++i]);
340                          break;
341 <                case 'v':
341 >                case 'v':                       /* verbose progress reports */
342                          verbose++;
343                          break;
344 <                case 'o':
344 >                case 'h':                       /* turn off header */
345 >                        doheader = 0;
346 >                        break;
347 >                case 'o':                       /* output format */
348                          switch (argv[i][2]) {
349                          case 'f':
350                          case 'd':
# Line 322 | Line 355 | main(int argc, char *argv[])
355                                  goto userr;
356                          }
357                          break;
358 <                case 'm':
358 >                case 'O':                       /* output type */
359 >                        switch (argv[i][2]) {
360 >                        case '0':
361 >                                output = 0;
362 >                                break;
363 >                        case '1':
364 >                                output = 1;
365 >                                break;
366 >                        default:
367 >                                goto userr;
368 >                        }
369 >                        if (argv[i][3])
370 >                                goto userr;
371 >                        break;
372 >                case 'm':                       /* Reinhart subdivisions */
373                          rhsubdiv = atoi(argv[++i]);
374                          break;
375 <                case 'c':
375 >                case 'c':                       /* sky color */
376                          skycolor[0] = atof(argv[++i]);
377                          skycolor[1] = atof(argv[++i]);
378                          skycolor[2] = atof(argv[++i]);
379                          break;
380 <                case 'd':
334 <                        do_sun = 1;
380 >                case 'd':                       /* solar (direct) only */
381                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
382 +                        if (suncolor[1] <= 1e-4)
383 +                                suncolor[0] = suncolor[1] = suncolor[2] = 1;
384                          break;
385 <                case 's':
386 <                        do_sun = 0;
385 >                case 's':                       /* sky only (no direct) */
386 >                        suncolor[0] = suncolor[1] = suncolor[2] = 0;
387                          if (skycolor[1] <= 1e-4)
388                                  skycolor[0] = skycolor[1] = skycolor[2] = 1;
389                          break;
390 +                case 'r':                       /* rotate distribution */
391 +                        if (argv[i][2] && argv[i][2] != 'z')
392 +                                goto userr;
393 +                        rotation = atof(argv[++i]);
394 +                        break;
395 +                case '5':                       /* 5-phase calculation */
396 +                        nsuns = 1;
397 +                        fixed_sun_sa = PI/360.*atof(argv[++i]);
398 +                        if (fixed_sun_sa <= 0) {
399 +                                fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n",
400 +                                                argv[0]);
401 +                                exit(1);
402 +                        }
403 +                        fixed_sun_sa *= fixed_sun_sa*PI;
404 +                        break;
405                  default:
406                          goto userr;
407                  }
# Line 358 | Line 421 | main(int argc, char *argv[])
421                                          progname);
422          }
423                                          /* read weather tape header */
424 <        if (scanf("place %[^\n]\n", buf) != 1)
424 >        if (scanf("place %[^\r\n] ", buf) != 1)
425                  goto fmterr;
426          if (scanf("latitude %lf\n", &s_latitude) != 1)
427                  goto fmterr;
# Line 393 | Line 456 | main(int argc, char *argv[])
456                                  progname, s_latitude, s_longitude);
457                  fprintf(stderr, "%s: %d sky patches per time step\n",
458                                  progname, nskypatch);
459 +                if (rotation != 0)
460 +                        fprintf(stderr, "%s: rotating output %.0f degrees\n",
461 +                                        progname, rotation);
462          }
463 +                                        /* convert quantities to radians */
464 +        s_latitude = DegToRad(s_latitude);
465 +        s_longitude = DegToRad(s_longitude);
466 +        s_meridian = DegToRad(s_meridian);
467                                          /* process each time step in tape */
468          while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) {
469                  double          sda, sta;
470                                          /* make space for next time step */
471                  mtx_offset = 3*nskypatch*ntsteps++;
472 <                mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch);
472 >                if (ntsteps > step_alloc) {
473 >                        step_alloc += (step_alloc>>1) + ntsteps + 7;
474 >                        mtx_data = resize_dmatrix(mtx_data, step_alloc, nskypatch);
475 >                }
476                  if (dif <= 1e-4) {
477                          memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
478                          continue;
# Line 412 | Line 485 | main(int argc, char *argv[])
485                  sda = sdec(julian_date);
486                  sta = stadj(julian_date);
487                  altitude = salt(sda, hr+sta);
488 <                azimuth = sazi(sda, hr+sta);
488 >                azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation);
489                                          /* convert measured values */
490                  if (dir_is_horiz && altitude > 0.)
491                          dir /= sin(altitude);
# Line 425 | Line 498 | main(int argc, char *argv[])
498                  }
499                                          /* compute sky patch values */
500                  ComputeSky(mtx_data+mtx_offset);
501 <                if (do_sun)
429 <                        AddDirect(mtx_data+mtx_offset);
501 >                AddDirect(mtx_data+mtx_offset);
502          }
503                                          /* check for junk at end */
504          while ((i = fgetc(stdin)) != EOF)
# Line 439 | Line 511 | main(int argc, char *argv[])
511                          break;
512                  }
513                                          /* write out matrix */
514 +        if (outfmt != 'a')
515 +                SET_FILE_BINARY(stdout);
516   #ifdef getc_unlocked
517          flockfile(stdout);
518   #endif
519          if (verbose)
520                  fprintf(stderr, "%s: writing %smatrix with %d time steps...\n",
521                                  progname, outfmt=='a' ? "" : "binary ", ntsteps);
522 +        if (doheader) {
523 +                newheader("RADIANCE", stdout);
524 +                printargs(argc, argv, stdout);
525 +                printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude),
526 +                                        -RadToDeg(s_longitude));
527 +                printf("NROWS=%d\n", nskypatch);
528 +                printf("NCOLS=%d\n", ntsteps);
529 +                printf("NCOMP=3\n");
530 +                fputformat((char *)getfmtname(outfmt), stdout);
531 +                putchar('\n');
532 +        }
533                                          /* patches are rows (outer sort) */
534          for (i = 0; i < nskypatch; i++) {
535                  mtx_offset = 3*i;
536                  switch (outfmt) {
537                  case 'a':
538                          for (j = 0; j < ntsteps; j++) {
539 <                                printf("%.3e %.3e %.3e\n", mtx_data[mtx_offset],
539 >                                printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset],
540                                                  mtx_data[mtx_offset+1],
541                                                  mtx_data[mtx_offset+2]);
542                                  mtx_offset += 3*nskypatch;
543                          }
544 <                        fputc('\n', stdout);
544 >                        if (ntsteps > 1)
545 >                                fputc('\n', stdout);
546                          break;
547                  case 'f':
548                          for (j = 0; j < ntsteps; j++) {
# Line 485 | Line 571 | main(int argc, char *argv[])
571                  fprintf(stderr, "%s: done.\n", progname);
572          exit(0);
573   userr:
574 <        fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g refl][-c r g b][-o{f|d}] [tape.wea]\n",
574 >        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",
575                          progname);
576          exit(1);
577   fmterr:
# Line 506 | Line 592 | ComputeSky(float *parr)
592   {
593          int index;                      /* Category index */
594          double norm_diff_illum;         /* Normalized diffuse illuimnance */
509        double zlumin;                  /* Zenith luminance */
595          int i;
511
512        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
513                memset(parr, 0, sizeof(float)*3*nskypatch);
514                return;
515        }
596          
597          /* Calculate atmospheric precipitable water content */
598          apwc = CalcPrecipWater(dew_point);
599  
600 <        /* Limit solar altitude to keep circumsolar off zenith */
601 <        if (altitude > DegToRad(87.0))
602 <                altitude = DegToRad(87.0);
600 >        /* Calculate sun zenith angle (don't let it dip below horizon) */
601 >        /* Also limit minimum angle to keep circumsolar off zenith */
602 >        if (altitude <= 0.0)
603 >                sun_zenith = DegToRad(90.0);
604 >        else if (altitude >= DegToRad(87.0))
605 >                sun_zenith = DegToRad(3.0);
606 >        else
607 >                sun_zenith = DegToRad(90.0) - altitude;
608  
524        /* Calculate sun zenith angle */
525        sun_zenith = DegToRad(90.0) - altitude;
526
609          /* Compute the inputs for the calculation of the sky distribution */
610          
611          if (input == 0)                                 /* XXX never used */
# Line 542 | Line 624 | ComputeSky(float *parr)
624                  sky_brightness = CalcSkyBrightness();
625                  sky_clearness =  CalcSkyClearness();
626  
627 +                /* Limit sky clearness */
628 +                if (sky_clearness > 11.9)
629 +                        sky_clearness = 11.9;
630 +
631 +                /* Limit sky brightness */
632 +                if (sky_brightness < 0.01)
633 +                        sky_brightness = 0.01;
634 +
635                  /* Calculate illuminance */
636                  index = GetCategoryIndex();
637                  diff_illum = diff_irrad * CalcDiffuseIllumRatio(index);
# Line 553 | Line 643 | ComputeSky(float *parr)
643                  index = CalcSkyParamFromIllum();
644          }
645  
646 +        if (output == 1) {                      /* hack for solar radiance */
647 +                diff_illum = diff_irrad * WHTEFFICACY;
648 +                dir_illum = dir_irrad * WHTEFFICACY;
649 +        }
650 +
651 +        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
652 +                memset(parr, 0, sizeof(float)*3*nskypatch);
653 +                return;
654 +        }
655          /* Compute ground radiance (include solar contribution if any) */
656 <        parr[0] = diff_illum * (1./PI/WHTEFFICACY);
656 >        parr[0] = diff_illum;
657          if (altitude > 0)
658 <                parr[0] += dir_illum * sin(altitude) * (1./PI/WHTEFFICACY);
659 <        parr[2] = parr[1] = parr[0];
658 >                parr[0] += dir_illum * sin(altitude);
659 >        parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY);
660 >        multcolor(parr, grefl);
661  
662          /* Calculate Perez sky model parameters */
663          CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index);
# Line 568 | Line 668 | ComputeSky(float *parr)
668          /* Calculate relative horizontal illuminance */
669          norm_diff_illum = CalcRelHorzIllum(parr);
670  
671 +        /* Check for zero sky -- make uniform in that case */
672 +        if (norm_diff_illum <= FTINY) {
673 +                for (i = 1; i < nskypatch; i++)
674 +                        setcolor(parr+3*i, 1., 1., 1.);
675 +                norm_diff_illum = PI;
676 +        }
677          /* Normalization coefficient */
678          norm_diff_illum = diff_illum / norm_diff_illum;
679  
574        /* Calculate relative zenith luminance */
575        zlumin = CalcRelLuminance(sun_zenith, 0.0);
576
577        /* Calculate absolute zenith illuminance */
578        zlumin *= norm_diff_illum;
579
680          /* Apply to sky patches to get absolute radiance values */
681          for (i = 1; i < nskypatch; i++) {
682 <                scalecolor(parr+3*i, zlumin*(1./WHTEFFICACY));
682 >                scalecolor(parr+3*i, norm_diff_illum*(1./WHTEFFICACY));
683                  multcolor(parr+3*i, skycolor);
684          }
685   }
# Line 589 | Line 689 | void
689   AddDirect(float *parr)
690   {
691          FVECT   svec;
692 <        double  near_dprod[4];
693 <        int     near_patch[4];
694 <        double  wta[4], wtot;
692 >        double  near_dprod[NSUNPATCH];
693 >        int     near_patch[NSUNPATCH];
694 >        double  wta[NSUNPATCH], wtot;
695          int     i, j, p;
696  
697 <        if (!do_sun || dir_illum < 1e-4)
697 >        if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4)
698                  return;
699 <                                        /* identify 4 closest patches */
700 <        for (i = 4; i--; )
699 >                                        /* identify nsuns closest patches */
700 >        if (nsuns > NSUNPATCH)
701 >                nsuns = NSUNPATCH;
702 >        else if (nsuns <= 0)
703 >                nsuns = 1;
704 >        for (i = nsuns; i--; )
705                  near_dprod[i] = -1.;
706          vector(svec, altitude, azimuth);
707          for (p = 1; p < nskypatch; p++) {
# Line 605 | Line 709 | AddDirect(float *parr)
709                  double  dprod;
710                  rh_vector(pvec, p);
711                  dprod = DOT(pvec, svec);
712 <                for (i = 0; i < 4; i++)
712 >                for (i = 0; i < nsuns; i++)
713                          if (dprod > near_dprod[i]) {
714 <                                for (j = 4; --j > i; ) {
714 >                                for (j = nsuns; --j > i; ) {
715                                          near_dprod[j] = near_dprod[j-1];
716                                          near_patch[j] = near_patch[j-1];
717                                  }
# Line 617 | Line 721 | AddDirect(float *parr)
721                          }
722          }
723          wtot = 0;                       /* weight by proximity */
724 <        for (i = 4; i--; )
724 >        for (i = nsuns; i--; )
725                  wtot += wta[i] = 1./(1.002 - near_dprod[i]);
726                                          /* add to nearest patch radiances */
727 <        for (i = 4; i--; )
728 <                parr[near_patch[i]] += wta[i] * dir_illum /
729 <                                        (wtot * rh_dom[near_patch[i]]);
727 >        for (i = nsuns; i--; ) {
728 >                float   *pdest = parr + 3*near_patch[i];
729 >                float   val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot);
730 >
731 >                val_add /= (fixed_sun_sa > 0)   ? fixed_sun_sa
732 >                                                : rh_dom[near_patch[i]] ;
733 >                *pdest++ += val_add*suncolor[0];
734 >                *pdest++ += val_add*suncolor[1];
735 >                *pdest++ += val_add*suncolor[2];
736 >        }
737   }
738  
739   /* Initialize Reinhart sky patch positions (GW) */
# Line 656 | Line 767 | rh_init(void)
767          for (i = 0; i < NROW*rhsubdiv; i++) {
768                  const float     ralt = alpha*(i + .5);
769                  const int       ninrow = tnaz[i/rhsubdiv]*rhsubdiv;
770 <                const float     dom = (sin(alpha*(i+1)) - sin(alpha*i))/ninrow;
770 >                const float     dom = 2.*PI*(sin(alpha*(i+1)) - sin(alpha*i)) /
771 >                                                (double)ninrow;
772                  for (j = 0; j < ninrow; j++) {
773                          rh_palt[p] = ralt;
774                          rh_pazi[p] = 2.*PI * j / (double)ninrow;
# Line 761 | Line 873 | double CalcSkyClearness()
873          double sz_cubed;        /* Sun zenith angle cubed */
874  
875          /* Calculate sun zenith angle cubed */
876 <        sz_cubed = pow(sun_zenith, 3.0);
876 >        sz_cubed = sun_zenith*sun_zenith*sun_zenith;
877  
878          return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 *
879                          sz_cubed) / (1.0 + 1.041 * sz_cubed);
# Line 792 | Line 904 | double CalcDiffuseIrradiance()
904   double CalcDirectIrradiance()
905   {
906          return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041
907 <                        * pow(sun_zenith, 3.0)));
907 >                        * sun_zenith*sun_zenith*sun_zenith));
908   }
909  
910   /* Calculate sky brightness and clearness from illuminance values */
# Line 818 | Line 930 | int CalcSkyParamFromIllum()
930                  sky_clearness = 12.0;
931  
932          /* Limit sky brightness */
933 <        if (sky_brightness < 0.05)
933 >        if (sky_brightness < 0.01)
934                          sky_brightness = 0.01;
935  
936          while (((fabs(diff_irrad - test1) > 10.0) ||
# Line 842 | Line 954 | int CalcSkyParamFromIllum()
954                          sky_clearness = 12.0;
955          
956                  /* Limit sky brightness */
957 <                if (sky_brightness < 0.05)
957 >                if (sky_brightness < 0.01)
958                          sky_brightness = 0.01;
959          }
960  
# Line 928 | Line 1040 | double CalcRelHorzIllum( float *parr )
1040          double rh_illum = 0.0;  /* Relative horizontal illuminance */
1041  
1042          for (i = 1; i < nskypatch; i++)
1043 <                rh_illum += parr[3*i+1] * rh_cos(i);
1043 >                rh_illum += parr[3*i+1] * rh_cos(i) * rh_dom[i];
1044  
1045 <        return rh_illum * (2.0 * PI / (nskypatch-1));
1045 >        return rh_illum;
1046   }
1047  
1048   /* Calculate earth orbit eccentricity correction factor */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines