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.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 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     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 305 | Line 334 | main(int argc, char *argv[])
334                                          /* get options */
335          for (i = 1; i < argc && argv[i][0] == '-'; i++)
336                  switch (argv[i][1]) {
337 <                case 'g':
338 <                        grefl = atof(argv[++i]);
337 >                case 'g':                       /* ground reflectance */
338 >                        grefl[0] = atof(argv[++i]);
339 >                        grefl[1] = atof(argv[++i]);
340 >                        grefl[2] = atof(argv[++i]);
341                          break;
342 <                case 'v':
342 >                case 'v':                       /* verbose progress reports */
343                          verbose++;
344                          break;
345 <                case 'o':
345 >                case 'h':                       /* turn off header */
346 >                        doheader = 0;
347 >                        break;
348 >                case 'o':                       /* output format */
349                          switch (argv[i][2]) {
350                          case 'f':
351                          case 'd':
# Line 322 | Line 356 | main(int argc, char *argv[])
356                                  goto userr;
357                          }
358                          break;
359 <                case 'm':
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':
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':
334 <                        do_sun = 1;
382 >                case 'd':                       /* solar (direct) only */
383                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
384 +                        if (suncolor[1] <= 1e-4) {
385 +                                inconsistent = 1;
386 +                                suncolor[0] = suncolor[1] = suncolor[2] = 1;
387 +                        }
388                          break;
389 <                case 's':
390 <                        do_sun = 0;
391 <                        if (skycolor[1] <= 1e-4)
389 >                case 's':                       /* sky only (no direct) */
390 >                        suncolor[0] = suncolor[1] = suncolor[2] = 0;
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 358 | Line 430 | main(int argc, char *argv[])
430                                          progname);
431          }
432                                          /* read weather tape header */
433 <        if (scanf("place %[^\n]\n", buf) != 1)
433 >        if (scanf("place %[^\r\n] ", buf) != 1)
434                  goto fmterr;
435          if (scanf("latitude %lf\n", &s_latitude) != 1)
436                  goto fmterr;
# Line 393 | 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);
474 +        s_longitude = DegToRad(s_longitude);
475 +        s_meridian = DegToRad(s_meridian);
476                                          /* process each time step in tape */
477          while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) {
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 412 | 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);
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 425 | Line 507 | main(int argc, char *argv[])
507                  }
508                                          /* compute sky patch values */
509                  ComputeSky(mtx_data+mtx_offset);
510 <                if (do_sun)
429 <                        AddDirect(mtx_data+mtx_offset);
510 >                AddDirect(mtx_data+mtx_offset);
511          }
512                                          /* check for junk at end */
513          while ((i = fgetc(stdin)) != EOF)
# Line 439 | 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;
545                  switch (outfmt) {
546                  case 'a':
547                          for (j = 0; j < ntsteps; j++) {
548 <                                printf("%.3e %.3e %.3e\n", mtx_data[mtx_offset],
548 >                                printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset],
549                                                  mtx_data[mtx_offset+1],
550                                                  mtx_data[mtx_offset+2]);
551                                  mtx_offset += 3*nskypatch;
552                          }
553 <                        fputc('\n', stdout);
553 >                        if (ntsteps > 1)
554 >                                fputc('\n', stdout);
555                          break;
556                  case 'f':
557                          for (j = 0; j < ntsteps; j++) {
# Line 485 | 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 refl][-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 506 | Line 601 | ComputeSky(float *parr)
601   {
602          int index;                      /* Category index */
603          double norm_diff_illum;         /* Normalized diffuse illuimnance */
509        double zlumin;                  /* Zenith luminance */
604          int i;
511
512        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
513                memset(parr, 0, sizeof(float)*3*nskypatch);
514                return;
515        }
605          
606          /* Calculate atmospheric precipitable water content */
607          apwc = CalcPrecipWater(dew_point);
608  
609 <        /* Limit solar altitude to keep circumsolar off zenith */
610 <        if (altitude > DegToRad(87.0))
611 <                altitude = DegToRad(87.0);
609 >        /* Calculate sun zenith angle (don't let it dip below horizon) */
610 >        /* Also limit minimum angle to keep circumsolar off zenith */
611 >        if (altitude <= 0.0)
612 >                sun_zenith = DegToRad(90.0);
613 >        else if (altitude >= DegToRad(87.0))
614 >                sun_zenith = DegToRad(3.0);
615 >        else
616 >                sun_zenith = DegToRad(90.0) - altitude;
617  
524        /* Calculate sun zenith angle */
525        sun_zenith = DegToRad(90.0) - altitude;
526
618          /* Compute the inputs for the calculation of the sky distribution */
619          
620          if (input == 0)                                 /* XXX never used */
# Line 542 | 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 553 | 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;
663 +        }
664          /* Compute ground radiance (include solar contribution if any) */
665 <        parr[0] = diff_illum * (1./PI/WHTEFFICACY);
665 >        parr[0] = diff_illum;
666          if (altitude > 0)
667 <                parr[0] += dir_illum * sin(altitude) * (1./PI/WHTEFFICACY);
668 <        parr[2] = parr[1] = parr[0];
667 >                parr[0] += dir_illum * sin(altitude);
668 >        parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY);
669 >        multcolor(parr, grefl);
670  
671          /* Calculate Perez sky model parameters */
672          CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index);
# Line 568 | 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  
574        /* Calculate relative zenith luminance */
575        zlumin = CalcRelLuminance(sun_zenith, 0.0);
576
577        /* Calculate absolute zenith illuminance */
578        zlumin *= norm_diff_illum;
579
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 589 | Line 698 | void
698   AddDirect(float *parr)
699   {
700          FVECT   svec;
701 <        double  near_dprod[4];
702 <        int     near_patch[4];
703 <        double  wta[4], wtot;
701 >        double  near_dprod[NSUNPATCH];
702 >        int     near_patch[NSUNPATCH];
703 >        double  wta[NSUNPATCH], wtot;
704          int     i, j, p;
705  
706 <        if (!do_sun || dir_illum < 1e-4)
706 >        if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4)
707                  return;
708 <                                        /* identify 4 closest patches */
709 <        for (i = 4; 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 605 | Line 718 | AddDirect(float *parr)
718                  double  dprod;
719                  rh_vector(pvec, p);
720                  dprod = DOT(pvec, svec);
721 <                for (i = 0; i < 4; i++)
721 >                for (i = 0; i < nsuns; i++)
722                          if (dprod > near_dprod[i]) {
723 <                                for (j = 4; --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 617 | Line 730 | AddDirect(float *parr)
730                          }
731          }
732          wtot = 0;                       /* weight by proximity */
733 <        for (i = 4; 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 = 4; i--; )
737 <                parr[near_patch[i]] += wta[i] * dir_illum /
738 <                                        (wtot * rh_dom[near_patch[i]]);
736 >        for (i = nsuns; i--; ) {
737 >                float   *pdest = parr + 3*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];
745 >        }
746   }
747  
748   /* Initialize Reinhart sky patch positions (GW) */
# Line 656 | Line 776 | rh_init(void)
776          for (i = 0; i < NROW*rhsubdiv; i++) {
777                  const float     ralt = alpha*(i + .5);
778                  const int       ninrow = tnaz[i/rhsubdiv]*rhsubdiv;
779 <                const float     dom = (sin(alpha*(i+1)) - sin(alpha*i))/ninrow;
779 >                const float     dom = 2.*PI*(sin(alpha*(i+1)) - sin(alpha*i)) /
780 >                                                (double)ninrow;
781                  for (j = 0; j < ninrow; j++) {
782                          rh_palt[p] = ralt;
783                          rh_pazi[p] = 2.*PI * j / (double)ninrow;
# Line 761 | 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 792 | 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 818 | 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 842 | 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 928 | 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