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.37 by greg, Sat Aug 15 03:28:56 2020 UTC

# Line 81 | Line 81 | static const char RCSid[] = "$Id$";
81  
82   /* Include files */
83   #define _USE_MATH_DEFINES
84 #include <stdio.h>
84   #include <stdlib.h>
86 #include <string.h>
85   #include <ctype.h>
86 + #include "platform.h"
87   #include "rtmath.h"
88 + #include "rtio.h"
89   #include "color.h"
90 + #include "sun.h"
91  
92   char *progname;                                                         /* Program name */
93   char errmsg[128];                                                       /* Error message buffer */
# 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 133 | Line 135 | extern void ComputeSky( float *parr );
135   /* Radiuans into degrees */
136   #define RadToDeg(rad)   ((rad)*(180./PI))
137  
136
138   /* Perez sky model coefficients */
139  
140   /* Reference:   Perez, R., R. Seals, and J. Michalsky, 1993. "All- */
# Line 208 | Line 209 | static const CategoryBounds SkyClearCat[8] =
209          { 1.950, 2.800 },
210          { 2.800, 4.500 },
211          { 4.500, 6.200 },
212 <        { 6.200, 12.00 }        /* Clear */
212 >        { 6.200, 12.01 }        /* Clear */
213   };
214  
215   /* Luminous efficacy model coefficients */
# Line 247 | Line 248 | static const ModelCoeff DirectLumEff[8] =
248   };
249  
250   #ifndef NSUNPATCH
251 < #define NSUNPATCH       4               /* # patches to spread sun into */
251 > #define NSUNPATCH       4               /* max. # patches to spread sun into */
252   #endif
253  
254 < extern int jdate(int month, int day);
254 < extern double stadj(int  jd);
255 < extern double sdec(int  jd);
256 < extern double salt(double sd, double st);
257 < extern double sazi(double sd, double st);
258 <                                        /* sun calculation constants */
259 < extern double  s_latitude;
260 < extern double  s_longitude;
261 < extern double  s_meridian;
254 > #define SUN_ANG_DEG     0.533           /* sun full-angle in degrees */
255  
256 < double          grefl = 0.2;            /* diffuse ground reflectance */
256 > int             nsuns = NSUNPATCH;      /* number of sun patches to use */
257 > double          fixed_sun_sa = -1;      /* fixed solid angle per sun? */
258  
259   int             verbose = 0;            /* progress reports to stderr? */
260  
# Line 268 | Line 262 | int            outfmt = 'a';           /* output format */
262  
263   int             rhsubdiv = 1;           /* Reinhart sky subdivisions */
264  
265 < float           skycolor[3] = {.96, 1.004, 1.118};      /* sky coloration */
265 > COLOR           skycolor = {.96, 1.004, 1.118}; /* sky coloration */
266 > COLOR           suncolor = {1., 1., 1.};        /* sun color */
267 > COLOR           grefl = {.2, .2, .2};           /* ground reflectance */
268  
273 int             do_sun = 1;             /* output direct solar contribution? */
274
269   int             nskypatch;              /* number of Reinhart patches */
270   float           *rh_palt;               /* sky patch altitudes (radians) */
271   float           *rh_pazi;               /* sky patch azimuths (radians) */
272   float           *rh_dom;                /* sky patch solid angle (sr) */
273  
274 < #define         vector(v,alt,azi)       (       (v)[1] = tcos(alt), \
275 <                                                (v)[0] = (v)[1]*tsin(azi), \
276 <                                                (v)[1] *= tcos(azi), \
277 <                                                (v)[2] = tsin(alt) )
274 > #define         vector(v,alt,azi)       (       (v)[1] = cos(alt), \
275 >                                                (v)[0] = (v)[1]*sin(azi), \
276 >                                                (v)[1] *= cos(azi), \
277 >                                                (v)[2] = sin(alt) )
278  
279   #define         rh_vector(v,i)          vector(v,rh_palt[i],rh_pazi[i])
280  
281   #define         rh_cos(i)               tsin(rh_palt[i])
282  
283 + #define         solar_minute(jd,hr)     ((24*60)*((jd)-1)+(int)((hr)*60.+.5))
284 +
285   extern int      rh_init(void);
286   extern float *  resize_dmatrix(float *mtx_data, int nsteps, int npatch);
287 + extern void     OutputSun(int id, int goodsun, FILE *fp, FILE *mfp);
288   extern void     AddDirect(float *parr);
289  
290 +
291 + static const char *
292 + getfmtname(int fmt)
293 + {
294 +        switch (fmt) {
295 +        case 'a':
296 +                return("ascii");
297 +        case 'f':
298 +                return("float");
299 +        case 'd':
300 +                return("double");
301 +        }
302 +        return("unknown");
303 + }
304 +
305 +
306   int
307   main(int argc, char *argv[])
308   {
309          char    buf[256];
310 +        int     doheader = 1;           /* output header? */
311 +        double  rotation = 0;           /* site rotation (degrees) */
312          double  elevation;              /* site elevation (meters) */
313 +        int     leap_day = 0;           /* add leap day? */
314 +        int     sun_hours_only = 0;     /* only output sun hours? */
315          int     dir_is_horiz;           /* direct is meas. on horizontal? */
316 +        FILE    *sunsfp = NULL;         /* output file for individual suns */
317 +        FILE    *modsfp = NULL;         /* modifier output file */
318          float   *mtx_data = NULL;       /* our matrix data */
319 <        int     ntsteps = 0;            /* number of rows in matrix */
319 >        int     avgSky = 0;             /* compute average sky r.t. matrix? */
320 >        int     ntsteps = 0;            /* number of time steps */
321 >        int     tstorage = 0;           /* number of allocated time steps */
322 >        int     nstored = 0;            /* number of time steps in matrix */
323          int     last_monthly = 0;       /* month of last report */
324          int     mo, da;                 /* month (1-12) and day (1-31) */
325          double  hr;                     /* hour (local standard time) */
# Line 309 | Line 331 | main(int argc, char *argv[])
331                                          /* get options */
332          for (i = 1; i < argc && argv[i][0] == '-'; i++)
333                  switch (argv[i][1]) {
334 <                case 'g':
335 <                        grefl = atof(argv[++i]);
334 >                case 'g':                       /* ground reflectance */
335 >                        grefl[0] = atof(argv[++i]);
336 >                        grefl[1] = atof(argv[++i]);
337 >                        grefl[2] = atof(argv[++i]);
338                          break;
339 <                case 'v':
339 >                case 'v':                       /* verbose progress reports */
340                          verbose++;
341                          break;
342 <                case 'o':
342 >                case 'h':                       /* turn off header */
343 >                        doheader = 0;
344 >                        break;
345 >                case 'o':                       /* output format */
346                          switch (argv[i][2]) {
347                          case 'f':
348                          case 'd':
# Line 326 | Line 353 | main(int argc, char *argv[])
353                                  goto userr;
354                          }
355                          break;
356 <                case 'm':
356 >                case 'O':                       /* output type */
357 >                        switch (argv[i][2]) {
358 >                        case '0':
359 >                                output = 0;
360 >                                break;
361 >                        case '1':
362 >                                output = 1;
363 >                                break;
364 >                        default:
365 >                                goto userr;
366 >                        }
367 >                        if (argv[i][3])
368 >                                goto userr;
369 >                        break;
370 >                case 'm':                       /* Reinhart subdivisions */
371                          rhsubdiv = atoi(argv[++i]);
372                          break;
373 <                case 'c':
373 >                case 'c':                       /* sky color */
374                          skycolor[0] = atof(argv[++i]);
375                          skycolor[1] = atof(argv[++i]);
376                          skycolor[2] = atof(argv[++i]);
377                          break;
378 <                case 'd':
379 <                        do_sun = 1;
378 >                case 'D':                       /* output suns to file */
379 >                        if (strcmp(argv[++i], "-")) {
380 >                                sunsfp = fopen(argv[i], "w");
381 >                                if (sunsfp == NULL) {
382 >                                        fprintf(stderr,
383 >                                        "%s: cannot open '%s' for output\n",
384 >                                                        progname, argv[i]);
385 >                                        exit(1);
386 >                                }
387 >                                break;          /* still may output matrix */
388 >                        }
389 >                        sunsfp = stdout;        /* sending to stdout, so... */
390 >                        /* fall through */
391 >                case 'n':                       /* no matrix output */
392 >                        avgSky = -1;
393 >                        rhsubdiv = 1;
394 >                        /* fall through */
395 >                case 'd':                       /* solar (direct) only */
396                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
397 +                        grefl[0] = grefl[1] = grefl[2] = 0;
398                          break;
399 <                case 's':
400 <                        do_sun = 0;
401 <                        if (skycolor[1] <= 1e-4)
402 <                                skycolor[0] = skycolor[1] = skycolor[2] = 1;
399 >                case 'M':                       /* send sun modifiers to file */
400 >                        if ((modsfp = fopen(argv[++i], "w")) == NULL) {
401 >                                fprintf(stderr, "%s: cannot open '%s' for output\n",
402 >                                                progname, argv[i]);
403 >                                exit(1);
404 >                        }
405                          break;
406 +                case 's':                       /* sky only (no direct) */
407 +                        suncolor[0] = suncolor[1] = suncolor[2] = 0;
408 +                        break;
409 +                case 'u':                       /* solar hours only */
410 +                        sun_hours_only = 1;
411 +                        break;
412 +                case 'r':                       /* rotate distribution */
413 +                        if (argv[i][2] && argv[i][2] != 'z')
414 +                                goto userr;
415 +                        rotation = atof(argv[++i]);
416 +                        break;
417 +                case '5':                       /* 5-phase calculation */
418 +                        nsuns = 1;
419 +                        fixed_sun_sa = PI/360.*atof(argv[++i]);
420 +                        if (fixed_sun_sa <= 0) {
421 +                                fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n",
422 +                                                progname);
423 +                                exit(1);
424 +                        }
425 +                        fixed_sun_sa *= fixed_sun_sa*PI;
426 +                        break;
427 +                case 'A':                       /* compute average sky */
428 +                        avgSky = 1;
429 +                        break;
430                  default:
431                          goto userr;
432                  }
# Line 353 | Line 437 | main(int argc, char *argv[])
437                                  progname, argv[i]);
438                  exit(1);
439          }
440 +        if ((modsfp != NULL) & (sunsfp == NULL))
441 +                fprintf(stderr, "%s: warning -M output will be empty without -D\n",
442 +                                progname);
443          if (verbose) {
444                  if (i == argc-1)
445                          fprintf(stderr, "%s: reading weather tape '%s'\n",
# Line 395 | Line 482 | main(int argc, char *argv[])
482                  fprintf(stderr, "%s: location '%s'\n", progname, buf);
483                  fprintf(stderr, "%s: (lat,long)=(%.1f,%.1f) degrees north, west\n",
484                                  progname, s_latitude, s_longitude);
485 <                fprintf(stderr, "%s: %d sky patches per time step\n",
486 <                                progname, nskypatch);
485 >                if (avgSky >= 0)
486 >                        fprintf(stderr, "%s: %d sky patches\n",
487 >                                        progname, nskypatch);
488 >                if (sunsfp)
489 >                        fprintf(stderr, "%s: outputting suns to file\n",
490 >                                        progname);
491 >                if (rotation != 0)
492 >                        fprintf(stderr, "%s: rotating output %.0f degrees\n",
493 >                                        progname, rotation);
494          }
495                                          /* convert quantities to radians */
496          s_latitude = DegToRad(s_latitude);
497          s_longitude = DegToRad(s_longitude);
498          s_meridian = DegToRad(s_meridian);
499 +                                        /* initial allocation */
500 +        mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch);
501                                          /* process each time step in tape */
502          while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) {
503                  double          sda, sta;
504 <                                        /* make space for next time step */
409 <                mtx_offset = 3*nskypatch*ntsteps++;
410 <                mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch);
411 <                if (dif <= 1e-4) {
412 <                        memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
413 <                        continue;
414 <                }
415 <                if (verbose && mo != last_monthly)
416 <                        fprintf(stderr, "%s: stepping through month %d...\n",
417 <                                                progname, last_monthly=mo);
504 >                int             sun_in_sky;
505                                          /* compute solar position */
506 <                julian_date = jdate(mo, da);
506 >                if ((mo == 2) & (da == 29)) {
507 >                        julian_date = 60;
508 >                        leap_day = 1;
509 >                } else
510 >                        julian_date = jdate(mo, da) + leap_day;
511                  sda = sdec(julian_date);
512                  sta = stadj(julian_date);
513                  altitude = salt(sda, hr+sta);
514 <                azimuth = sazi(sda, hr+sta) + PI;
514 >                sun_in_sky = (altitude > -DegToRad(SUN_ANG_DEG/2.));
515 >                if (sun_hours_only && !sun_in_sky)
516 >                        continue;       /* skipping nighttime points */
517 >                azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation);
518 >
519 >                mtx_offset = 3*nskypatch*nstored;
520 >                nstored += !avgSky | !nstored;
521 >                                        /* make space for next row */
522 >                if (nstored > tstorage) {
523 >                        tstorage += (tstorage>>1) + nstored + 7;
524 >                        mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch);
525 >                }
526 >                ntsteps++;              /* keep count of time steps */
527 >
528 >                if (dir+dif <= 1e-4) {  /* effectively nighttime? */
529 >                        if (!avgSky | !mtx_offset)
530 >                                memset(mtx_data+mtx_offset, 0,
531 >                                                sizeof(float)*3*nskypatch);
532 >                                        /* output black sun? */
533 >                        if (sunsfp && sun_in_sky)
534 >                                OutputSun(solar_minute(julian_date,hr), 0,
535 >                                                        sunsfp, modsfp);
536 >                        continue;
537 >                }
538                                          /* convert measured values */
539                  if (dir_is_horiz && altitude > 0.)
540                          dir /= sin(altitude);
# Line 433 | Line 547 | main(int argc, char *argv[])
547                  }
548                                          /* compute sky patch values */
549                  ComputeSky(mtx_data+mtx_offset);
550 <                if (do_sun)
551 <                        AddDirect(mtx_data+mtx_offset);
550 >                                        /* output sun if requested */
551 >                if (sunsfp && sun_in_sky)
552 >                        OutputSun(solar_minute(julian_date,hr), 1,
553 >                                                sunsfp, modsfp);
554 >
555 >                if (avgSky < 0)         /* no matrix? */
556 >                        continue;
557 >
558 >                AddDirect(mtx_data+mtx_offset);
559 >                                        /* update cumulative sky? */
560 >                for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
561 >                        mtx_data[i] += mtx_data[mtx_offset+i];
562 >                                        /* monthly reporting */
563 >                if (verbose && mo != last_monthly)
564 >                        fprintf(stderr, "%s: stepping through month %d...\n",
565 >                                                progname, last_monthly=mo);
566 >                                        /* note whether leap-day was given */
567          }
568 +        if (!ntsteps) {
569 +                fprintf(stderr, "%s: no valid time steps on input\n", progname);
570 +                exit(1);
571 +        }
572                                          /* check for junk at end */
573          while ((i = fgetc(stdin)) != EOF)
574                  if (!isspace(i)) {
# Line 446 | Line 579 | main(int argc, char *argv[])
579                          fputs(buf, stderr); fputc('\n', stderr);
580                          break;
581                  }
582 +
583 +        if (avgSky < 0)                 /* no matrix output? */
584 +                goto alldone;
585 +
586 +        dif = 1./(double)ntsteps;       /* average sky? */
587 +        for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
588 +                mtx_data[i] *= dif;
589                                          /* write out matrix */
590 +        if (outfmt != 'a')
591 +                SET_FILE_BINARY(stdout);
592   #ifdef getc_unlocked
593          flockfile(stdout);
594   #endif
595          if (verbose)
596                  fprintf(stderr, "%s: writing %smatrix with %d time steps...\n",
597 <                                progname, outfmt=='a' ? "" : "binary ", ntsteps);
597 >                                progname, outfmt=='a' ? "" : "binary ", nstored);
598 >        if (doheader) {
599 >                newheader("RADIANCE", stdout);
600 >                printargs(argc, argv, stdout);
601 >                printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude),
602 >                                        -RadToDeg(s_longitude));
603 >                printf("NROWS=%d\n", nskypatch);
604 >                printf("NCOLS=%d\n", nstored);
605 >                printf("NCOMP=3\n");
606 >                if ((outfmt == 'f') | (outfmt == 'd'))
607 >                        fputendian(stdout);
608 >                fputformat((char *)getfmtname(outfmt), stdout);
609 >                putchar('\n');
610 >        }
611                                          /* patches are rows (outer sort) */
612          for (i = 0; i < nskypatch; i++) {
613                  mtx_offset = 3*i;
614                  switch (outfmt) {
615                  case 'a':
616 <                        for (j = 0; j < ntsteps; j++) {
616 >                        for (j = 0; j < nstored; j++) {
617                                  printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset],
618                                                  mtx_data[mtx_offset+1],
619                                                  mtx_data[mtx_offset+2]);
620                                  mtx_offset += 3*nskypatch;
621                          }
622 <                        if (ntsteps > 1)
622 >                        if (nstored > 1)
623                                  fputc('\n', stdout);
624                          break;
625                  case 'f':
626 <                        for (j = 0; j < ntsteps; j++) {
627 <                                fwrite(mtx_data+mtx_offset, sizeof(float), 3,
626 >                        for (j = 0; j < nstored; j++) {
627 >                                putbinary(mtx_data+mtx_offset, sizeof(float), 3,
628                                                  stdout);
629                                  mtx_offset += 3*nskypatch;
630                          }
631                          break;
632                  case 'd':
633 <                        for (j = 0; j < ntsteps; j++) {
633 >                        for (j = 0; j < nstored; j++) {
634                                  double  ment[3];
635                                  ment[0] = mtx_data[mtx_offset];
636                                  ment[1] = mtx_data[mtx_offset+1];
637                                  ment[2] = mtx_data[mtx_offset+2];
638 <                                fwrite(ment, sizeof(double), 3, stdout);
638 >                                putbinary(ment, sizeof(double), 3, stdout);
639                                  mtx_offset += 3*nskypatch;
640                          }
641                          break;
# Line 488 | Line 643 | main(int argc, char *argv[])
643                  if (ferror(stdout))
644                          goto writerr;
645          }
646 <        if (fflush(stdout) == EOF)
646 > alldone:
647 >        if (fflush(NULL) == EOF)
648                  goto writerr;
649          if (verbose)
650                  fprintf(stderr, "%s: done.\n", progname);
651          exit(0);
652   userr:
653 <        fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g refl][-c r g b][-o{f|d}] [tape.wea]\n",
653 >        fprintf(stderr, "Usage: %s [-v][-h][-A][-d|-s|-n][-u][-D file [-M modfile]][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n",
654                          progname);
655          exit(1);
656   fmterr:
657 <        fprintf(stderr, "%s: input weather tape format error\n", progname);
657 >        fprintf(stderr, "%s: weather tape format error in header\n", progname);
658          exit(1);
659   writerr:
660          fprintf(stderr, "%s: write error on output\n", progname);
# Line 515 | Line 671 | ComputeSky(float *parr)
671   {
672          int index;                      /* Category index */
673          double norm_diff_illum;         /* Normalized diffuse illuimnance */
518        double zlumin;                  /* Zenith luminance */
674          int i;
675          
676          /* Calculate atmospheric precipitable water content */
677          apwc = CalcPrecipWater(dew_point);
678  
679 <        /* Limit solar altitude to keep circumsolar off zenith */
680 <        if (altitude > DegToRad(87.0))
681 <                altitude = DegToRad(87.0);
679 >        /* Calculate sun zenith angle (don't let it dip below horizon) */
680 >        /* Also limit minimum angle to keep circumsolar off zenith */
681 >        if (altitude <= 0.0)
682 >                sun_zenith = DegToRad(90.0);
683 >        else if (altitude >= DegToRad(87.0))
684 >                sun_zenith = DegToRad(3.0);
685 >        else
686 >                sun_zenith = DegToRad(90.0) - altitude;
687  
528        /* Calculate sun zenith angle */
529        sun_zenith = DegToRad(90.0) - altitude;
530
688          /* Compute the inputs for the calculation of the sky distribution */
689          
690          if (input == 0)                                 /* XXX never used */
# Line 546 | Line 703 | ComputeSky(float *parr)
703                  sky_brightness = CalcSkyBrightness();
704                  sky_clearness =  CalcSkyClearness();
705  
706 +                /* Limit sky clearness */
707 +                if (sky_clearness > 11.9)
708 +                        sky_clearness = 11.9;
709 +
710 +                /* Limit sky brightness */
711 +                if (sky_brightness < 0.01)
712 +                        sky_brightness = 0.01;
713 +
714                  /* Calculate illuminance */
715                  index = GetCategoryIndex();
716                  diff_illum = diff_irrad * CalcDiffuseIllumRatio(index);
# Line 557 | Line 722 | ComputeSky(float *parr)
722                  index = CalcSkyParamFromIllum();
723          }
724  
725 <        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
726 <                memset(parr, 0, sizeof(float)*3*nskypatch);
727 <                return;
725 >        if (output == 1) {                      /* hack for solar radiance */
726 >                diff_illum = diff_irrad * WHTEFFICACY;
727 >                dir_illum = dir_irrad * WHTEFFICACY;
728          }
729          /* Compute ground radiance (include solar contribution if any) */
730          parr[0] = diff_illum;
731          if (altitude > 0)
732                  parr[0] += dir_illum * sin(altitude);
733 <        parr[2] = parr[1] = parr[0] *= grefl*(1./PI/WHTEFFICACY);
733 >        parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY);
734 >        multcolor(parr, grefl);
735  
736 +        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
737 +                memset(parr+3, 0, sizeof(float)*3*(nskypatch-1));
738 +                return;
739 +        }
740          /* Calculate Perez sky model parameters */
741          CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index);
742  
# Line 576 | Line 746 | ComputeSky(float *parr)
746          /* Calculate relative horizontal illuminance */
747          norm_diff_illum = CalcRelHorzIllum(parr);
748  
749 +        /* Check for zero sky -- make uniform in that case */
750 +        if (norm_diff_illum <= FTINY) {
751 +                for (i = 1; i < nskypatch; i++)
752 +                        setcolor(parr+3*i, 1., 1., 1.);
753 +                norm_diff_illum = PI;
754 +        }
755          /* Normalization coefficient */
756          norm_diff_illum = diff_illum / norm_diff_illum;
757  
582        /* Calculate relative zenith luminance */
583        zlumin = CalcRelLuminance(sun_zenith, 0.0);
584
585        /* Calculate absolute zenith illuminance */
586        zlumin *= norm_diff_illum;
587
758          /* Apply to sky patches to get absolute radiance values */
759          for (i = 1; i < nskypatch; i++) {
760 <                scalecolor(parr+3*i, zlumin*(1./WHTEFFICACY));
760 >                scalecolor(parr+3*i, norm_diff_illum*(1./WHTEFFICACY));
761                  multcolor(parr+3*i, skycolor);
762          }
763   }
# Line 602 | Line 772 | AddDirect(float *parr)
772          double  wta[NSUNPATCH], wtot;
773          int     i, j, p;
774  
775 <        if (!do_sun || dir_illum < 1e-4)
775 >        if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4)
776                  return;
777 <                                        /* identify NSUNPATCH closest patches */
778 <        for (i = NSUNPATCH; i--; )
777 >                                        /* identify nsuns closest patches */
778 >        if (nsuns > NSUNPATCH)
779 >                nsuns = NSUNPATCH;
780 >        else if (nsuns <= 0)
781 >                nsuns = 1;
782 >        for (i = nsuns; i--; )
783                  near_dprod[i] = -1.;
784          vector(svec, altitude, azimuth);
785          for (p = 1; p < nskypatch; p++) {
# Line 613 | Line 787 | AddDirect(float *parr)
787                  double  dprod;
788                  rh_vector(pvec, p);
789                  dprod = DOT(pvec, svec);
790 <                for (i = 0; i < NSUNPATCH; i++)
790 >                for (i = 0; i < nsuns; i++)
791                          if (dprod > near_dprod[i]) {
792 <                                for (j = NSUNPATCH; --j > i; ) {
792 >                                for (j = nsuns; --j > i; ) {
793                                          near_dprod[j] = near_dprod[j-1];
794                                          near_patch[j] = near_patch[j-1];
795                                  }
# Line 625 | Line 799 | AddDirect(float *parr)
799                          }
800          }
801          wtot = 0;                       /* weight by proximity */
802 <        for (i = NSUNPATCH; i--; )
802 >        for (i = nsuns; i--; )
803                  wtot += wta[i] = 1./(1.002 - near_dprod[i]);
804                                          /* add to nearest patch radiances */
805 <        for (i = NSUNPATCH; i--; ) {
805 >        for (i = nsuns; i--; ) {
806                  float   *pdest = parr + 3*near_patch[i];
807 <                float   val_add = wta[i] * dir_illum /
808 <                                (WHTEFFICACY * wtot * rh_dom[near_patch[i]]);
809 <                *pdest++ += val_add;
810 <                *pdest++ += val_add;
811 <                *pdest++ += val_add;
807 >                float   val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot);
808 >
809 >                val_add /= (fixed_sun_sa > 0)   ? fixed_sun_sa
810 >                                                : rh_dom[near_patch[i]] ;
811 >                *pdest++ += val_add*suncolor[0];
812 >                *pdest++ += val_add*suncolor[1];
813 >                *pdest++ += val_add*suncolor[2];
814          }
815   }
816  
817 + /* Output a sun to indicated file if appropriate for this time step */
818 + void
819 + OutputSun(int id, int goodsun, FILE *fp, FILE *mfp)
820 + {
821 +        double  srad;
822 +        FVECT   sv;
823 +
824 +        srad = DegToRad(SUN_ANG_DEG/2.);
825 +        srad = goodsun ? dir_illum/(WHTEFFICACY * PI*srad*srad) : 0;
826 +        vector(sv, altitude, azimuth);
827 +        fprintf(fp, "\nvoid light solar%d\n0\n0\n", id);
828 +        fprintf(fp, "3 %.3e %.3e %.3e\n", srad*suncolor[0],
829 +                        srad*suncolor[1], srad*suncolor[2]);
830 +        fprintf(fp, "\nsolar%d source sun%d\n0\n0\n", id, id);
831 +        fprintf(fp, "4 %.6f %.6f %.6f %.4f\n", sv[0], sv[1], sv[2], SUN_ANG_DEG);
832 +        
833 +        if (mfp != NULL)                /* saving modifier IDs? */
834 +                fprintf(mfp, "solar%d\n", id);
835 + }
836 +
837   /* Initialize Reinhart sky patch positions (GW) */
838   int
839   rh_init(void)
# Line 775 | Line 971 | double CalcSkyClearness()
971          double sz_cubed;        /* Sun zenith angle cubed */
972  
973          /* Calculate sun zenith angle cubed */
974 <        sz_cubed = pow(sun_zenith, 3.0);
974 >        sz_cubed = sun_zenith*sun_zenith*sun_zenith;
975  
976          return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 *
977                          sz_cubed) / (1.0 + 1.041 * sz_cubed);
# Line 806 | Line 1002 | double CalcDiffuseIrradiance()
1002   double CalcDirectIrradiance()
1003   {
1004          return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041
1005 <                        * pow(sun_zenith, 3.0)));
1005 >                        * sun_zenith*sun_zenith*sun_zenith));
1006   }
1007  
1008   /* Calculate sky brightness and clearness from illuminance values */
# Line 832 | Line 1028 | int CalcSkyParamFromIllum()
1028                  sky_clearness = 12.0;
1029  
1030          /* Limit sky brightness */
1031 <        if (sky_brightness < 0.05)
1031 >        if (sky_brightness < 0.01)
1032                          sky_brightness = 0.01;
1033  
1034          while (((fabs(diff_irrad - test1) > 10.0) ||
# Line 845 | Line 1041 | int CalcSkyParamFromIllum()
1041                  /* Convert illuminance to irradiance */
1042                  index = GetCategoryIndex();
1043                  diff_irrad = diff_illum / CalcDiffuseIllumRatio(index);
1044 <                dir_irrad = dir_illum / CalcDirectIllumRatio(index);
1044 >                dir_irrad = CalcDirectIllumRatio(index);
1045 >                if (dir_irrad > 0.1)
1046 >                        dir_irrad = dir_illum / dir_irrad;
1047          
1048                  /* Calculate sky brightness and clearness */
1049                  sky_brightness = CalcSkyBrightness();
# Line 856 | Line 1054 | int CalcSkyParamFromIllum()
1054                          sky_clearness = 12.0;
1055          
1056                  /* Limit sky brightness */
1057 <                if (sky_brightness < 0.05)
1057 >                if (sky_brightness < 0.01)
1058                          sky_brightness = 0.01;
1059          }
1060  
# Line 942 | Line 1140 | double CalcRelHorzIllum( float *parr )
1140          double rh_illum = 0.0;  /* Relative horizontal illuminance */
1141  
1142          for (i = 1; i < nskypatch; i++)
1143 <                rh_illum += parr[3*i+1] * rh_cos(i);
1143 >                rh_illum += parr[3*i+1] * rh_cos(i) * rh_dom[i];
1144  
1145 <        return rh_illum * (2.0 * PI / (nskypatch-1));
1145 >        return rh_illum;
1146   }
1147  
1148   /* Calculate earth orbit eccentricity correction factor */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines