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.2 by greg, Fri Jan 18 19:56:03 2013 UTC vs.
Revision 2.24 by greg, Fri Aug 19 15:13:40 2016 UTC

# Line 86 | Line 86 | static const char RCSid[] = "$Id$";
86   #include <string.h>
87   #include <ctype.h>
88   #include "rtmath.h"
89 + #include "rtio.h"
90 + #include "resolu.h"
91 + #include "platform.h"
92   #include "color.h"
93 + #include "resolu.h"
94  
95   char *progname;                                                         /* Program name */
96   char errmsg[128];                                                       /* Error message buffer */
# Line 108 | Line 112 | double sky_clearness;                  /* Sky clearness */
112   double solar_rad;                       /* Solar radiance */
113   double sun_zenith;                      /* Sun zenith angle (radians) */
114   int     input = 0;                              /* Input type */
115 + int     output = 0;                             /* Output type */
116  
117   extern double dmax( double, double );
118   extern double CalcAirMass();
# Line 208 | Line 213 | static const CategoryBounds SkyClearCat[8] =
213          { 1.950, 2.800 },
214          { 2.800, 4.500 },
215          { 4.500, 6.200 },
216 <        { 6.200, 12.00 }        /* Clear */
216 >        { 6.200, 12.01 }        /* Clear */
217   };
218  
219   /* Luminous efficacy model coefficients */
# Line 246 | Line 251 | static const ModelCoeff DirectLumEff[8] =
251          { 101.18,  1.58, -1.10,  -8.29 }
252   };
253  
254 + #ifndef NSUNPATCH
255 + #define NSUNPATCH       4               /* max. # patches to spread sun into */
256 + #endif
257 +
258   extern int jdate(int month, int day);
259   extern double stadj(int  jd);
260   extern double sdec(int  jd);
# Line 256 | Line 265 | extern double  s_latitude;
265   extern double  s_longitude;
266   extern double  s_meridian;
267  
268 < double          grefl = 0.2;            /* diffuse ground reflectance */
268 > int             nsuns = NSUNPATCH;      /* number of sun patches to use */
269 > double          fixed_sun_sa = -1;      /* fixed solid angle per sun? */
270  
271   int             verbose = 0;            /* progress reports to stderr? */
272  
# Line 264 | Line 274 | int            outfmt = 'a';           /* output format */
274  
275   int             rhsubdiv = 1;           /* Reinhart sky subdivisions */
276  
277 < float           skycolor[3] = {.96, 1.004, 1.118};      /* sky coloration */
277 > COLOR           skycolor = {.96, 1.004, 1.118}; /* sky coloration */
278 > COLOR           suncolor = {1., 1., 1.};        /* sun color */
279 > COLOR           grefl = {.2, .2, .2};           /* ground reflectance */
280  
269 int             do_sun = 1;             /* output direct solar contribution? */
270
281   int             nskypatch;              /* number of Reinhart patches */
282   float           *rh_palt;               /* sky patch altitudes (radians) */
283   float           *rh_pazi;               /* sky patch azimuths (radians) */
# Line 286 | Line 296 | extern int     rh_init(void);
296   extern float *  resize_dmatrix(float *mtx_data, int nsteps, int npatch);
297   extern void     AddDirect(float *parr);
298  
299 +
300 + static const char *
301 + getfmtname(int fmt)
302 + {
303 +        switch (fmt) {
304 +        case 'a':
305 +                return("ascii");
306 +        case 'f':
307 +                return("float");
308 +        case 'd':
309 +                return("double");
310 +        }
311 +        return("unknown");
312 + }
313 +
314 +
315   int
316   main(int argc, char *argv[])
317   {
318          char    buf[256];
319 +        int     doheader = 1;           /* output header? */
320 +        double  rotation = 0;           /* site rotation (degrees) */
321          double  elevation;              /* site elevation (meters) */
322          int     dir_is_horiz;           /* direct is meas. on horizontal? */
323          float   *mtx_data = NULL;       /* our matrix data */
324          int     ntsteps = 0;            /* number of rows in matrix */
325 +        int     step_alloc = 0;
326          int     last_monthly = 0;       /* month of last report */
327 +        int     inconsistent = 0;       /* inconsistent options set? */
328          int     mo, da;                 /* month (1-12) and day (1-31) */
329          double  hr;                     /* hour (local standard time) */
330          double  dir, dif;               /* direct and diffuse values */
# Line 305 | Line 335 | main(int argc, char *argv[])
335                                          /* get options */
336          for (i = 1; i < argc && argv[i][0] == '-'; i++)
337                  switch (argv[i][1]) {
338 <                case 'g':
339 <                        grefl = atof(argv[++i]);
338 >                case 'g':                       /* ground reflectance */
339 >                        grefl[0] = atof(argv[++i]);
340 >                        grefl[1] = atof(argv[++i]);
341 >                        grefl[2] = atof(argv[++i]);
342                          break;
343 <                case 'v':
343 >                case 'v':                       /* verbose progress reports */
344                          verbose++;
345                          break;
346 <                case 'o':
346 >                case 'h':                       /* turn off header */
347 >                        doheader = 0;
348 >                        break;
349 >                case 'o':                       /* output format */
350                          switch (argv[i][2]) {
351                          case 'f':
352                          case 'd':
# Line 322 | Line 357 | main(int argc, char *argv[])
357                                  goto userr;
358                          }
359                          break;
360 <                case 'm':
360 >                case 'O':                       /* output type */
361 >                        switch (argv[i][2]) {
362 >                        case '0':
363 >                                output = 0;
364 >                                break;
365 >                        case '1':
366 >                                output = 1;
367 >                                break;
368 >                        default:
369 >                                goto userr;
370 >                        }
371 >                        if (argv[i][3])
372 >                                goto userr;
373 >                        break;
374 >                case 'm':                       /* Reinhart subdivisions */
375                          rhsubdiv = atoi(argv[++i]);
376                          break;
377 <                case 'c':
377 >                case 'c':                       /* sky color */
378 >                        inconsistent |= (skycolor[1] <= 1e-4);
379                          skycolor[0] = atof(argv[++i]);
380                          skycolor[1] = atof(argv[++i]);
381                          skycolor[2] = atof(argv[++i]);
382                          break;
383 <                case 'd':
334 <                        do_sun = 1;
383 >                case 'd':                       /* solar (direct) only */
384                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
385 +                        if (suncolor[1] <= 1e-4) {
386 +                                inconsistent = 1;
387 +                                suncolor[0] = suncolor[1] = suncolor[2] = 1;
388 +                        }
389                          break;
390 <                case 's':
391 <                        do_sun = 0;
392 <                        if (skycolor[1] <= 1e-4)
390 >                case 's':                       /* sky only (no direct) */
391 >                        suncolor[0] = suncolor[1] = suncolor[2] = 0;
392 >                        if (skycolor[1] <= 1e-4) {
393 >                                inconsistent = 1;
394                                  skycolor[0] = skycolor[1] = skycolor[2] = 1;
395 +                        }
396                          break;
397 +                case 'r':                       /* rotate distribution */
398 +                        if (argv[i][2] && argv[i][2] != 'z')
399 +                                goto userr;
400 +                        rotation = atof(argv[++i]);
401 +                        break;
402 +                case '5':                       /* 5-phase calculation */
403 +                        nsuns = 1;
404 +                        fixed_sun_sa = PI/360.*atof(argv[++i]);
405 +                        if (fixed_sun_sa <= 0) {
406 +                                fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n",
407 +                                                argv[0]);
408 +                                exit(1);
409 +                        }
410 +                        fixed_sun_sa *= fixed_sun_sa*PI;
411 +                        break;
412                  default:
413                          goto userr;
414                  }
415          if (i < argc-1)
416                  goto userr;
417 +        if (inconsistent)
418 +                fprintf(stderr, "%s: WARNING: inconsistent -s, -d, -c options!\n",
419 +                                progname);
420          if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
421                  fprintf(stderr, "%s: cannot open '%s' for input\n",
422                                  progname, argv[i]);
# Line 393 | Line 466 | main(int argc, char *argv[])
466                                  progname, s_latitude, s_longitude);
467                  fprintf(stderr, "%s: %d sky patches per time step\n",
468                                  progname, nskypatch);
469 +                if (rotation != 0)
470 +                        fprintf(stderr, "%s: rotating output %.0f degrees\n",
471 +                                        progname, rotation);
472          }
473                                          /* convert quantities to radians */
474          s_latitude = DegToRad(s_latitude);
# Line 403 | Line 479 | main(int argc, char *argv[])
479                  double          sda, sta;
480                                          /* make space for next time step */
481                  mtx_offset = 3*nskypatch*ntsteps++;
482 <                mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch);
482 >                if (ntsteps > step_alloc) {
483 >                        step_alloc += (step_alloc>>1) + ntsteps + 7;
484 >                        mtx_data = resize_dmatrix(mtx_data, step_alloc, nskypatch);
485 >                }
486                  if (dif <= 1e-4) {
487                          memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
488                          continue;
# Line 416 | Line 495 | main(int argc, char *argv[])
495                  sda = sdec(julian_date);
496                  sta = stadj(julian_date);
497                  altitude = salt(sda, hr+sta);
498 <                azimuth = sazi(sda, hr+sta) + PI;
498 >                azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation);
499                                          /* convert measured values */
500                  if (dir_is_horiz && altitude > 0.)
501                          dir /= sin(altitude);
# Line 429 | Line 508 | main(int argc, char *argv[])
508                  }
509                                          /* compute sky patch values */
510                  ComputeSky(mtx_data+mtx_offset);
511 <                if (do_sun)
433 <                        AddDirect(mtx_data+mtx_offset);
511 >                AddDirect(mtx_data+mtx_offset);
512          }
513                                          /* check for junk at end */
514          while ((i = fgetc(stdin)) != EOF)
# Line 443 | Line 521 | main(int argc, char *argv[])
521                          break;
522                  }
523                                          /* write out matrix */
524 +        if (outfmt != 'a')
525 +                SET_FILE_BINARY(stdout);
526   #ifdef getc_unlocked
527          flockfile(stdout);
528   #endif
529          if (verbose)
530                  fprintf(stderr, "%s: writing %smatrix with %d time steps...\n",
531                                  progname, outfmt=='a' ? "" : "binary ", ntsteps);
532 +        if (doheader) {
533 +                newheader("RADIANCE", stdout);
534 +                printargs(argc, argv, stdout);
535 +                printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude),
536 +                                        -RadToDeg(s_longitude));
537 +                printf("NROWS=%d\n", nskypatch);
538 +                printf("NCOLS=%d\n", ntsteps);
539 +                printf("NCOMP=3\n");
540 +                fputformat((char *)getfmtname(outfmt), stdout);
541 +                putchar('\n');
542 +        }
543                                          /* patches are rows (outer sort) */
544          for (i = 0; i < nskypatch; i++) {
545                  mtx_offset = 3*i;
546                  switch (outfmt) {
547                  case 'a':
548                          for (j = 0; j < ntsteps; j++) {
549 <                                printf("%.3e %.3e %.3e\n", mtx_data[mtx_offset],
549 >                                printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset],
550                                                  mtx_data[mtx_offset+1],
551                                                  mtx_data[mtx_offset+2]);
552                                  mtx_offset += 3*nskypatch;
# Line 465 | Line 556 | main(int argc, char *argv[])
556                          break;
557                  case 'f':
558                          for (j = 0; j < ntsteps; j++) {
559 <                                fwrite(mtx_data+mtx_offset, sizeof(float), 3,
559 >                                putbinary(mtx_data+mtx_offset, sizeof(float), 3,
560                                                  stdout);
561                                  mtx_offset += 3*nskypatch;
562                          }
# Line 476 | Line 567 | main(int argc, char *argv[])
567                                  ment[0] = mtx_data[mtx_offset];
568                                  ment[1] = mtx_data[mtx_offset+1];
569                                  ment[2] = mtx_data[mtx_offset+2];
570 <                                fwrite(ment, sizeof(double), 3, stdout);
570 >                                putbinary(ment, sizeof(double), 3, stdout);
571                                  mtx_offset += 3*nskypatch;
572                          }
573                          break;
# Line 490 | Line 581 | main(int argc, char *argv[])
581                  fprintf(stderr, "%s: done.\n", progname);
582          exit(0);
583   userr:
584 <        fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g refl][-c r g b][-o{f|d}] [tape.wea]\n",
584 >        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",
585                          progname);
586          exit(1);
587   fmterr:
# Line 511 | Line 602 | ComputeSky(float *parr)
602   {
603          int index;                      /* Category index */
604          double norm_diff_illum;         /* Normalized diffuse illuimnance */
514        double zlumin;                  /* Zenith luminance */
605          int i;
606          
607          /* Calculate atmospheric precipitable water content */
608          apwc = CalcPrecipWater(dew_point);
609  
610 <        /* Limit solar altitude to keep circumsolar off zenith */
611 <        if (altitude > DegToRad(87.0))
612 <                altitude = DegToRad(87.0);
610 >        /* Calculate sun zenith angle (don't let it dip below horizon) */
611 >        /* Also limit minimum angle to keep circumsolar off zenith */
612 >        if (altitude <= 0.0)
613 >                sun_zenith = DegToRad(90.0);
614 >        else if (altitude >= DegToRad(87.0))
615 >                sun_zenith = DegToRad(3.0);
616 >        else
617 >                sun_zenith = DegToRad(90.0) - altitude;
618  
524        /* Calculate sun zenith angle */
525        sun_zenith = DegToRad(90.0) - altitude;
526
619          /* Compute the inputs for the calculation of the sky distribution */
620          
621          if (input == 0)                                 /* XXX never used */
# Line 542 | Line 634 | ComputeSky(float *parr)
634                  sky_brightness = CalcSkyBrightness();
635                  sky_clearness =  CalcSkyClearness();
636  
637 +                /* Limit sky clearness */
638 +                if (sky_clearness > 11.9)
639 +                        sky_clearness = 11.9;
640 +
641 +                /* Limit sky brightness */
642 +                if (sky_brightness < 0.01)
643 +                        sky_brightness = 0.01;
644 +
645                  /* Calculate illuminance */
646                  index = GetCategoryIndex();
647                  diff_illum = diff_irrad * CalcDiffuseIllumRatio(index);
# Line 553 | Line 653 | ComputeSky(float *parr)
653                  index = CalcSkyParamFromIllum();
654          }
655  
656 +        if (output == 1) {                      /* hack for solar radiance */
657 +                diff_illum = diff_irrad * WHTEFFICACY;
658 +                dir_illum = dir_irrad * WHTEFFICACY;
659 +        }
660 +
661          if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
662                  memset(parr, 0, sizeof(float)*3*nskypatch);
663                  return;
664          }
665          /* Compute ground radiance (include solar contribution if any) */
666 <        parr[0] = diff_illum * (1./PI/WHTEFFICACY);
666 >        parr[0] = diff_illum;
667          if (altitude > 0)
668 <                parr[0] += dir_illum * sin(altitude) * (1./PI/WHTEFFICACY);
669 <        parr[2] = parr[1] = parr[0];
668 >                parr[0] += dir_illum * sin(altitude);
669 >        parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY);
670 >        multcolor(parr, grefl);
671  
672          /* Calculate Perez sky model parameters */
673          CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index);
# Line 572 | Line 678 | ComputeSky(float *parr)
678          /* Calculate relative horizontal illuminance */
679          norm_diff_illum = CalcRelHorzIllum(parr);
680  
681 +        /* Check for zero sky -- make uniform in that case */
682 +        if (norm_diff_illum <= FTINY) {
683 +                for (i = 1; i < nskypatch; i++)
684 +                        setcolor(parr+3*i, 1., 1., 1.);
685 +                norm_diff_illum = PI;
686 +        }
687          /* Normalization coefficient */
688          norm_diff_illum = diff_illum / norm_diff_illum;
689  
578        /* Calculate relative zenith luminance */
579        zlumin = CalcRelLuminance(sun_zenith, 0.0);
580
581        /* Calculate absolute zenith illuminance */
582        zlumin *= norm_diff_illum;
583
690          /* Apply to sky patches to get absolute radiance values */
691          for (i = 1; i < nskypatch; i++) {
692 <                scalecolor(parr+3*i, zlumin*(1./WHTEFFICACY));
692 >                scalecolor(parr+3*i, norm_diff_illum*(1./WHTEFFICACY));
693                  multcolor(parr+3*i, skycolor);
694          }
695   }
# Line 593 | Line 699 | void
699   AddDirect(float *parr)
700   {
701          FVECT   svec;
702 <        double  near_dprod[4];
703 <        int     near_patch[4];
704 <        double  wta[4], wtot;
702 >        double  near_dprod[NSUNPATCH];
703 >        int     near_patch[NSUNPATCH];
704 >        double  wta[NSUNPATCH], wtot;
705          int     i, j, p;
706  
707 <        if (!do_sun || dir_illum < 1e-4)
707 >        if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4)
708                  return;
709 <                                        /* identify 4 closest patches */
710 <        for (i = 4; i--; )
709 >                                        /* identify nsuns closest patches */
710 >        if (nsuns > NSUNPATCH)
711 >                nsuns = NSUNPATCH;
712 >        else if (nsuns <= 0)
713 >                nsuns = 1;
714 >        for (i = nsuns; i--; )
715                  near_dprod[i] = -1.;
716          vector(svec, altitude, azimuth);
717          for (p = 1; p < nskypatch; p++) {
# Line 609 | Line 719 | AddDirect(float *parr)
719                  double  dprod;
720                  rh_vector(pvec, p);
721                  dprod = DOT(pvec, svec);
722 <                for (i = 0; i < 4; i++)
722 >                for (i = 0; i < nsuns; i++)
723                          if (dprod > near_dprod[i]) {
724 <                                for (j = 4; --j > i; ) {
724 >                                for (j = nsuns; --j > i; ) {
725                                          near_dprod[j] = near_dprod[j-1];
726                                          near_patch[j] = near_patch[j-1];
727                                  }
# Line 621 | Line 731 | AddDirect(float *parr)
731                          }
732          }
733          wtot = 0;                       /* weight by proximity */
734 <        for (i = 4; i--; )
734 >        for (i = nsuns; i--; )
735                  wtot += wta[i] = 1./(1.002 - near_dprod[i]);
736                                          /* add to nearest patch radiances */
737 <        for (i = 4; i--; ) {
737 >        for (i = nsuns; i--; ) {
738                  float   *pdest = parr + 3*near_patch[i];
739 <                float   val_add = wta[i] * dir_illum /
740 <                                (WHTEFFICACY * wtot * rh_dom[near_patch[i]]);
741 <                *pdest++ += val_add;
742 <                *pdest++ += val_add;
743 <                *pdest++ += val_add;
739 >                float   val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot);
740 >
741 >                val_add /= (fixed_sun_sa > 0)   ? fixed_sun_sa
742 >                                                : rh_dom[near_patch[i]] ;
743 >                *pdest++ += val_add*suncolor[0];
744 >                *pdest++ += val_add*suncolor[1];
745 >                *pdest++ += val_add*suncolor[2];
746          }
747   }
748  
# Line 665 | Line 777 | rh_init(void)
777          for (i = 0; i < NROW*rhsubdiv; i++) {
778                  const float     ralt = alpha*(i + .5);
779                  const int       ninrow = tnaz[i/rhsubdiv]*rhsubdiv;
780 <                const float     dom = (sin(alpha*(i+1)) - sin(alpha*i))/ninrow;
780 >                const float     dom = 2.*PI*(sin(alpha*(i+1)) - sin(alpha*i)) /
781 >                                                (double)ninrow;
782                  for (j = 0; j < ninrow; j++) {
783                          rh_palt[p] = ralt;
784                          rh_pazi[p] = 2.*PI * j / (double)ninrow;
# Line 770 | Line 883 | double CalcSkyClearness()
883          double sz_cubed;        /* Sun zenith angle cubed */
884  
885          /* Calculate sun zenith angle cubed */
886 <        sz_cubed = pow(sun_zenith, 3.0);
886 >        sz_cubed = sun_zenith*sun_zenith*sun_zenith;
887  
888          return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 *
889                          sz_cubed) / (1.0 + 1.041 * sz_cubed);
# Line 801 | Line 914 | double CalcDiffuseIrradiance()
914   double CalcDirectIrradiance()
915   {
916          return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041
917 <                        * pow(sun_zenith, 3.0)));
917 >                        * sun_zenith*sun_zenith*sun_zenith));
918   }
919  
920   /* Calculate sky brightness and clearness from illuminance values */
# Line 827 | Line 940 | int CalcSkyParamFromIllum()
940                  sky_clearness = 12.0;
941  
942          /* Limit sky brightness */
943 <        if (sky_brightness < 0.05)
943 >        if (sky_brightness < 0.01)
944                          sky_brightness = 0.01;
945  
946          while (((fabs(diff_irrad - test1) > 10.0) ||
# Line 851 | Line 964 | int CalcSkyParamFromIllum()
964                          sky_clearness = 12.0;
965          
966                  /* Limit sky brightness */
967 <                if (sky_brightness < 0.05)
967 >                if (sky_brightness < 0.01)
968                          sky_brightness = 0.01;
969          }
970  
# Line 937 | Line 1050 | double CalcRelHorzIllum( float *parr )
1050          double rh_illum = 0.0;  /* Relative horizontal illuminance */
1051  
1052          for (i = 1; i < nskypatch; i++)
1053 <                rh_illum += parr[3*i+1] * rh_cos(i);
1053 >                rh_illum += parr[3*i+1] * rh_cos(i) * rh_dom[i];
1054  
1055 <        return rh_illum * (2.0 * PI / (nskypatch-1));
1055 >        return rh_illum;
1056   }
1057  
1058   /* Calculate earth orbit eccentricity correction factor */
# Line 1015 | Line 1128 | void CalcSkyPatchLumin( float *parr )
1128          double aas;                             /* Sun-sky point azimuthal angle */
1129          double sspa;                    /* Sun-sky point angle */
1130          double zsa;                             /* Zenithal sun angle */
1131 +
1132 +        perez_param[0], perez_param[1], perez_param[2], perez_param[3], perez_param[4]);
1133  
1134          for (i = 1; i < nskypatch; i++)
1135          {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines