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.7 by greg, Sat Jan 26 00:59:08 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 247 | Line 251 | static const ModelCoeff DirectLumEff[8] =
251   };
252  
253   #ifndef NSUNPATCH
254 < #define NSUNPATCH       4               /* # patches to spread sun into */
254 > #define NSUNPATCH       4               /* max. # patches to spread sun into */
255   #endif
256  
257   extern int jdate(int month, int day);
# Line 260 | Line 264 | extern double  s_latitude;
264   extern double  s_longitude;
265   extern double  s_meridian;
266  
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  
272   int             outfmt = 'a';           /* output format */
# Line 288 | 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 315 | Line 341 | main(int argc, char *argv[])
341                  case 'v':                       /* verbose progress reports */
342                          verbose++;
343                          break;
344 +                case 'h':                       /* turn off header */
345 +                        doheader = 0;
346 +                        break;
347                  case 'o':                       /* output format */
348                          switch (argv[i][2]) {
349                          case 'f':
# Line 326 | Line 355 | main(int argc, char *argv[])
355                                  goto userr;
356                          }
357                          break;
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;
# Line 344 | Line 387 | main(int argc, char *argv[])
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 398 | 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);
# Line 408 | Line 469 | main(int argc, char *argv[])
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 421 | 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) + PI;
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 447 | 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;
# Line 494 | 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 r g b][-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 547 | 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 558 | 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;
# Line 578 | 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  
# Line 600 | Line 696 | AddDirect(float *parr)
696  
697          if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4)
698                  return;
699 <                                        /* identify NSUNPATCH closest patches */
700 <        for (i = NSUNPATCH; 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 609 | Line 709 | AddDirect(float *parr)
709                  double  dprod;
710                  rh_vector(pvec, p);
711                  dprod = DOT(pvec, svec);
712 <                for (i = 0; i < NSUNPATCH; i++)
712 >                for (i = 0; i < nsuns; i++)
713                          if (dprod > near_dprod[i]) {
714 <                                for (j = NSUNPATCH; --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 621 | Line 721 | AddDirect(float *parr)
721                          }
722          }
723          wtot = 0;                       /* weight by proximity */
724 <        for (i = NSUNPATCH; 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 = NSUNPATCH; i--; ) {
727 >        for (i = nsuns; i--; ) {
728                  float   *pdest = parr + 3*near_patch[i];
729 <                float   val_add = wta[i] * dir_illum /
730 <                                (WHTEFFICACY * wtot * rh_dom[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];
# Line 771 | 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 802 | 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 828 | 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 852 | 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  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines