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.35 by greg, Tue Jan 7 18:26:55 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 208 | Line 210 | static const CategoryBounds SkyClearCat[8] =
210          { 1.950, 2.800 },
211          { 2.800, 4.500 },
212          { 4.500, 6.200 },
213 <        { 6.200, 12.00 }        /* Clear */
213 >        { 6.200, 12.01 }        /* Clear */
214   };
215  
216   /* Luminous efficacy model coefficients */
# Line 246 | Line 248 | static const ModelCoeff DirectLumEff[8] =
248          { 101.18,  1.58, -1.10,  -8.29 }
249   };
250  
251 < extern int jdate(int month, int day);
252 < extern double stadj(int  jd);
253 < extern double sdec(int  jd);
252 < extern double salt(double sd, double st);
253 < extern double sazi(double sd, double st);
254 <                                        /* sun calculation constants */
255 < extern double  s_latitude;
256 < extern double  s_longitude;
257 < extern double  s_meridian;
251 > #ifndef NSUNPATCH
252 > #define NSUNPATCH       4               /* max. # patches to spread sun into */
253 > #endif
254  
255 < double          grefl = 0.2;            /* diffuse ground reflectance */
255 > #define SUN_ANG_DEG     0.533           /* sun full-angle in degrees */
256  
257 + int             nsuns = NSUNPATCH;      /* number of sun patches to use */
258 + double          fixed_sun_sa = -1;      /* fixed solid angle per sun? */
259 +
260   int             verbose = 0;            /* progress reports to stderr? */
261  
262   int             outfmt = 'a';           /* output format */
263  
264   int             rhsubdiv = 1;           /* Reinhart sky subdivisions */
265  
266 < float           skycolor[3] = {.96, 1.004, 1.118};      /* sky coloration */
266 > COLOR           skycolor = {.96, 1.004, 1.118}; /* sky coloration */
267 > COLOR           suncolor = {1., 1., 1.};        /* sun color */
268 > COLOR           grefl = {.2, .2, .2};           /* ground reflectance */
269  
269 int             do_sun = 1;             /* output direct solar contribution? */
270
270   int             nskypatch;              /* number of Reinhart patches */
271   float           *rh_palt;               /* sky patch altitudes (radians) */
272   float           *rh_pazi;               /* sky patch azimuths (radians) */
# Line 284 | Line 283 | float          *rh_dom;                /* sky patch solid angle (sr) */
283  
284   extern int      rh_init(void);
285   extern float *  resize_dmatrix(float *mtx_data, int nsteps, int npatch);
286 + extern void     OutputSun(int id, FILE *fp);
287   extern void     AddDirect(float *parr);
288  
289 +
290 + static const char *
291 + getfmtname(int fmt)
292 + {
293 +        switch (fmt) {
294 +        case 'a':
295 +                return("ascii");
296 +        case 'f':
297 +                return("float");
298 +        case 'd':
299 +                return("double");
300 +        }
301 +        return("unknown");
302 + }
303 +
304 +
305   int
306   main(int argc, char *argv[])
307   {
308          char    buf[256];
309 +        int     doheader = 1;           /* output header? */
310 +        double  rotation = 0;           /* site rotation (degrees) */
311          double  elevation;              /* site elevation (meters) */
312          int     dir_is_horiz;           /* direct is meas. on horizontal? */
313 +        FILE    *sunsfp = NULL;         /* output file for individual suns */
314          float   *mtx_data = NULL;       /* our matrix data */
315 <        int     ntsteps = 0;            /* number of rows in matrix */
315 >        int     avgSky = 0;             /* compute average sky r.t. matrix? */
316 >        int     ntsteps = 0;            /* number of time steps */
317 >        int     tstorage = 0;           /* number of allocated time steps */
318 >        int     nstored = 0;            /* number of time steps in matrix */
319          int     last_monthly = 0;       /* month of last report */
320          int     mo, da;                 /* month (1-12) and day (1-31) */
321          double  hr;                     /* hour (local standard time) */
# Line 305 | Line 327 | main(int argc, char *argv[])
327                                          /* get options */
328          for (i = 1; i < argc && argv[i][0] == '-'; i++)
329                  switch (argv[i][1]) {
330 <                case 'g':
331 <                        grefl = atof(argv[++i]);
330 >                case 'g':                       /* ground reflectance */
331 >                        grefl[0] = atof(argv[++i]);
332 >                        grefl[1] = atof(argv[++i]);
333 >                        grefl[2] = atof(argv[++i]);
334                          break;
335 <                case 'v':
335 >                case 'v':                       /* verbose progress reports */
336                          verbose++;
337                          break;
338 <                case 'o':
338 >                case 'h':                       /* turn off header */
339 >                        doheader = 0;
340 >                        break;
341 >                case 'o':                       /* output format */
342                          switch (argv[i][2]) {
343                          case 'f':
344                          case 'd':
# Line 322 | Line 349 | main(int argc, char *argv[])
349                                  goto userr;
350                          }
351                          break;
352 <                case 'm':
352 >                case 'O':                       /* output type */
353 >                        switch (argv[i][2]) {
354 >                        case '0':
355 >                                output = 0;
356 >                                break;
357 >                        case '1':
358 >                                output = 1;
359 >                                break;
360 >                        default:
361 >                                goto userr;
362 >                        }
363 >                        if (argv[i][3])
364 >                                goto userr;
365 >                        break;
366 >                case 'm':                       /* Reinhart subdivisions */
367                          rhsubdiv = atoi(argv[++i]);
368                          break;
369 <                case 'c':
369 >                case 'c':                       /* sky color */
370                          skycolor[0] = atof(argv[++i]);
371                          skycolor[1] = atof(argv[++i]);
372                          skycolor[2] = atof(argv[++i]);
373                          break;
374 <                case 'd':
375 <                        do_sun = 1;
374 >                case 'n':                       /* no matrix output */
375 >                        avgSky = -1;
376 >                        rhsubdiv = 1;
377 >                        /* fall through */
378 >                case 'd':                       /* solar (direct) only */
379                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
380 +                        grefl[0] = grefl[1] = grefl[2] = 0;
381                          break;
382 <                case 's':
383 <                        do_sun = 0;
384 <                        if (skycolor[1] <= 1e-4)
385 <                                skycolor[0] = skycolor[1] = skycolor[2] = 1;
382 >                case 'D':                       /* output suns to file */
383 >                        sunsfp = fopen(argv[++i], "w");
384 >                        if (!sunsfp) {
385 >                                fprintf(stderr, "%s: cannot open '%s' for output\n",
386 >                                                progname, argv[i]);
387 >                                exit(1);
388 >                        }
389                          break;
390 +                case 's':                       /* sky only (no direct) */
391 +                        suncolor[0] = suncolor[1] = suncolor[2] = 0;
392 +                        break;
393 +                case 'r':                       /* rotate distribution */
394 +                        if (argv[i][2] && argv[i][2] != 'z')
395 +                                goto userr;
396 +                        rotation = atof(argv[++i]);
397 +                        break;
398 +                case '5':                       /* 5-phase calculation */
399 +                        nsuns = 1;
400 +                        fixed_sun_sa = PI/360.*atof(argv[++i]);
401 +                        if (fixed_sun_sa <= 0) {
402 +                                fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n",
403 +                                                progname);
404 +                                exit(1);
405 +                        }
406 +                        fixed_sun_sa *= fixed_sun_sa*PI;
407 +                        break;
408 +                case 'A':                       /* compute average sky */
409 +                        avgSky = 1;
410 +                        break;
411                  default:
412                          goto userr;
413                  }
# Line 358 | Line 427 | main(int argc, char *argv[])
427                                          progname);
428          }
429                                          /* read weather tape header */
430 <        if (scanf("place %[^\n]\n", buf) != 1)
430 >        if (scanf("place %[^\r\n] ", buf) != 1)
431                  goto fmterr;
432          if (scanf("latitude %lf\n", &s_latitude) != 1)
433                  goto fmterr;
# Line 391 | Line 460 | main(int argc, char *argv[])
460                  fprintf(stderr, "%s: location '%s'\n", progname, buf);
461                  fprintf(stderr, "%s: (lat,long)=(%.1f,%.1f) degrees north, west\n",
462                                  progname, s_latitude, s_longitude);
463 <                fprintf(stderr, "%s: %d sky patches per time step\n",
464 <                                progname, nskypatch);
463 >                if (avgSky >= 0)
464 >                        fprintf(stderr, "%s: %d sky patches\n",
465 >                                        progname, nskypatch);
466 >                if (sunsfp)
467 >                        fprintf(stderr, "%s: outputting suns to file\n",
468 >                                        progname);
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);
475 +        s_longitude = DegToRad(s_longitude);
476 +        s_meridian = DegToRad(s_meridian);
477 +                                        /* initial allocation */
478 +        mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch);
479                                          /* process each time step in tape */
480          while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) {
481                  double          sda, sta;
482 <                                        /* make space for next time step */
483 <                mtx_offset = 3*nskypatch*ntsteps++;
484 <                mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch);
482 >
483 >                mtx_offset = 3*nskypatch*nstored;
484 >                nstored += !avgSky | !nstored;
485 >                                        /* make space for next row */
486 >                if (nstored > tstorage) {
487 >                        tstorage += (tstorage>>1) + nstored + 7;
488 >                        mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch);
489 >                }
490 >                ntsteps++;              /* keep count of time steps */
491                  if (dif <= 1e-4) {
492 <                        memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
492 >                        if (!avgSky | !mtx_offset)
493 >                                memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
494                          continue;
495                  }
407                if (verbose && mo != last_monthly)
408                        fprintf(stderr, "%s: stepping through month %d...\n",
409                                                progname, last_monthly=mo);
496                                          /* compute solar position */
497                  julian_date = jdate(mo, da);
498                  sda = sdec(julian_date);
499                  sta = stadj(julian_date);
500                  altitude = salt(sda, hr+sta);
501 <                azimuth = sazi(sda, hr+sta);
501 >                azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation);
502                                          /* convert measured values */
503                  if (dir_is_horiz && altitude > 0.)
504                          dir /= sin(altitude);
# Line 425 | Line 511 | main(int argc, char *argv[])
511                  }
512                                          /* compute sky patch values */
513                  ComputeSky(mtx_data+mtx_offset);
514 <                if (do_sun)
515 <                        AddDirect(mtx_data+mtx_offset);
514 >
515 >                if (sunsfp)             /* output sun if indicated */
516 >                        OutputSun(ntsteps, sunsfp);
517 >
518 >                if (avgSky < 0)         /* no matrix? */
519 >                        continue;
520 >
521 >                AddDirect(mtx_data+mtx_offset);
522 >                                        /* update cumulative sky? */
523 >                for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
524 >                        mtx_data[i] += mtx_data[mtx_offset+i];
525 >                                        /* monthly reporting */
526 >                if (verbose && mo != last_monthly)
527 >                        fprintf(stderr, "%s: stepping through month %d...\n",
528 >                                                progname, last_monthly=mo);
529          }
530 +        if (!ntsteps) {
531 +                fprintf(stderr, "%s: no valid time steps on input\n", progname);
532 +                exit(1);
533 +        }
534                                          /* check for junk at end */
535          while ((i = fgetc(stdin)) != EOF)
536                  if (!isspace(i)) {
# Line 438 | Line 541 | main(int argc, char *argv[])
541                          fputs(buf, stderr); fputc('\n', stderr);
542                          break;
543                  }
544 +
545 +        if (avgSky < 0)                 /* no matrix output? */
546 +                goto alldone;
547 +
548 +        dif = 1./(double)ntsteps;       /* average sky? */
549 +        for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
550 +                mtx_data[i] *= dif;
551                                          /* write out matrix */
552 +        if (outfmt != 'a')
553 +                SET_FILE_BINARY(stdout);
554   #ifdef getc_unlocked
555          flockfile(stdout);
556   #endif
557          if (verbose)
558                  fprintf(stderr, "%s: writing %smatrix with %d time steps...\n",
559 <                                progname, outfmt=='a' ? "" : "binary ", ntsteps);
559 >                                progname, outfmt=='a' ? "" : "binary ", nstored);
560 >        if (doheader) {
561 >                newheader("RADIANCE", stdout);
562 >                printargs(argc, argv, stdout);
563 >                printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude),
564 >                                        -RadToDeg(s_longitude));
565 >                printf("NROWS=%d\n", nskypatch);
566 >                printf("NCOLS=%d\n", nstored);
567 >                printf("NCOMP=3\n");
568 >                if ((outfmt == 'f') | (outfmt == 'd'))
569 >                        fputendian(stdout);
570 >                fputformat((char *)getfmtname(outfmt), stdout);
571 >                putchar('\n');
572 >        }
573                                          /* patches are rows (outer sort) */
574          for (i = 0; i < nskypatch; i++) {
575                  mtx_offset = 3*i;
576                  switch (outfmt) {
577                  case 'a':
578 <                        for (j = 0; j < ntsteps; j++) {
579 <                                printf("%.3e %.3e %.3e\n", mtx_data[mtx_offset],
578 >                        for (j = 0; j < nstored; j++) {
579 >                                printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset],
580                                                  mtx_data[mtx_offset+1],
581                                                  mtx_data[mtx_offset+2]);
582                                  mtx_offset += 3*nskypatch;
583                          }
584 <                        fputc('\n', stdout);
584 >                        if (nstored > 1)
585 >                                fputc('\n', stdout);
586                          break;
587                  case 'f':
588 <                        for (j = 0; j < ntsteps; j++) {
589 <                                fwrite(mtx_data+mtx_offset, sizeof(float), 3,
588 >                        for (j = 0; j < nstored; j++) {
589 >                                putbinary(mtx_data+mtx_offset, sizeof(float), 3,
590                                                  stdout);
591                                  mtx_offset += 3*nskypatch;
592                          }
593                          break;
594                  case 'd':
595 <                        for (j = 0; j < ntsteps; j++) {
595 >                        for (j = 0; j < nstored; j++) {
596                                  double  ment[3];
597                                  ment[0] = mtx_data[mtx_offset];
598                                  ment[1] = mtx_data[mtx_offset+1];
599                                  ment[2] = mtx_data[mtx_offset+2];
600 <                                fwrite(ment, sizeof(double), 3, stdout);
600 >                                putbinary(ment, sizeof(double), 3, stdout);
601                                  mtx_offset += 3*nskypatch;
602                          }
603                          break;
# Line 479 | Line 605 | main(int argc, char *argv[])
605                  if (ferror(stdout))
606                          goto writerr;
607          }
608 <        if (fflush(stdout) == EOF)
608 > alldone:
609 >        if (fflush(NULL) == EOF)
610                  goto writerr;
611          if (verbose)
612                  fprintf(stderr, "%s: done.\n", progname);
613          exit(0);
614   userr:
615 <        fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g refl][-c r g b][-o{f|d}] [tape.wea]\n",
615 >        fprintf(stderr, "Usage: %s [-v][-h][-A][-d|-s|-n][-D file][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n",
616                          progname);
617          exit(1);
618   fmterr:
619 <        fprintf(stderr, "%s: input weather tape format error\n", progname);
619 >        fprintf(stderr, "%s: weather tape format error in header\n", progname);
620          exit(1);
621   writerr:
622          fprintf(stderr, "%s: write error on output\n", progname);
# Line 506 | Line 633 | ComputeSky(float *parr)
633   {
634          int index;                      /* Category index */
635          double norm_diff_illum;         /* Normalized diffuse illuimnance */
509        double zlumin;                  /* Zenith luminance */
636          int i;
511
512        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
513                memset(parr, 0, sizeof(float)*3*nskypatch);
514                return;
515        }
637          
638          /* Calculate atmospheric precipitable water content */
639          apwc = CalcPrecipWater(dew_point);
640  
641 <        /* Limit solar altitude to keep circumsolar off zenith */
642 <        if (altitude > DegToRad(87.0))
643 <                altitude = DegToRad(87.0);
641 >        /* Calculate sun zenith angle (don't let it dip below horizon) */
642 >        /* Also limit minimum angle to keep circumsolar off zenith */
643 >        if (altitude <= 0.0)
644 >                sun_zenith = DegToRad(90.0);
645 >        else if (altitude >= DegToRad(87.0))
646 >                sun_zenith = DegToRad(3.0);
647 >        else
648 >                sun_zenith = DegToRad(90.0) - altitude;
649  
524        /* Calculate sun zenith angle */
525        sun_zenith = DegToRad(90.0) - altitude;
526
650          /* Compute the inputs for the calculation of the sky distribution */
651          
652          if (input == 0)                                 /* XXX never used */
# Line 542 | Line 665 | ComputeSky(float *parr)
665                  sky_brightness = CalcSkyBrightness();
666                  sky_clearness =  CalcSkyClearness();
667  
668 +                /* Limit sky clearness */
669 +                if (sky_clearness > 11.9)
670 +                        sky_clearness = 11.9;
671 +
672 +                /* Limit sky brightness */
673 +                if (sky_brightness < 0.01)
674 +                        sky_brightness = 0.01;
675 +
676                  /* Calculate illuminance */
677                  index = GetCategoryIndex();
678                  diff_illum = diff_irrad * CalcDiffuseIllumRatio(index);
# Line 553 | Line 684 | ComputeSky(float *parr)
684                  index = CalcSkyParamFromIllum();
685          }
686  
687 +        if (output == 1) {                      /* hack for solar radiance */
688 +                diff_illum = diff_irrad * WHTEFFICACY;
689 +                dir_illum = dir_irrad * WHTEFFICACY;
690 +        }
691          /* Compute ground radiance (include solar contribution if any) */
692 <        parr[0] = diff_illum * (1./PI/WHTEFFICACY);
692 >        parr[0] = diff_illum;
693          if (altitude > 0)
694 <                parr[0] += dir_illum * sin(altitude) * (1./PI/WHTEFFICACY);
695 <        parr[2] = parr[1] = parr[0];
694 >                parr[0] += dir_illum * sin(altitude);
695 >        parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY);
696 >        multcolor(parr, grefl);
697  
698 +        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
699 +                memset(parr+3, 0, sizeof(float)*3*(nskypatch-1));
700 +                return;
701 +        }
702          /* Calculate Perez sky model parameters */
703          CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index);
704  
# Line 568 | Line 708 | ComputeSky(float *parr)
708          /* Calculate relative horizontal illuminance */
709          norm_diff_illum = CalcRelHorzIllum(parr);
710  
711 +        /* Check for zero sky -- make uniform in that case */
712 +        if (norm_diff_illum <= FTINY) {
713 +                for (i = 1; i < nskypatch; i++)
714 +                        setcolor(parr+3*i, 1., 1., 1.);
715 +                norm_diff_illum = PI;
716 +        }
717          /* Normalization coefficient */
718          norm_diff_illum = diff_illum / norm_diff_illum;
719  
574        /* Calculate relative zenith luminance */
575        zlumin = CalcRelLuminance(sun_zenith, 0.0);
576
577        /* Calculate absolute zenith illuminance */
578        zlumin *= norm_diff_illum;
579
720          /* Apply to sky patches to get absolute radiance values */
721          for (i = 1; i < nskypatch; i++) {
722 <                scalecolor(parr+3*i, zlumin*(1./WHTEFFICACY));
722 >                scalecolor(parr+3*i, norm_diff_illum*(1./WHTEFFICACY));
723                  multcolor(parr+3*i, skycolor);
724          }
725   }
# Line 589 | Line 729 | void
729   AddDirect(float *parr)
730   {
731          FVECT   svec;
732 <        double  near_dprod[4];
733 <        int     near_patch[4];
734 <        double  wta[4], wtot;
732 >        double  near_dprod[NSUNPATCH];
733 >        int     near_patch[NSUNPATCH];
734 >        double  wta[NSUNPATCH], wtot;
735          int     i, j, p;
736  
737 <        if (!do_sun || dir_illum < 1e-4)
737 >        if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4)
738                  return;
739 <                                        /* identify 4 closest patches */
740 <        for (i = 4; i--; )
739 >                                        /* identify nsuns closest patches */
740 >        if (nsuns > NSUNPATCH)
741 >                nsuns = NSUNPATCH;
742 >        else if (nsuns <= 0)
743 >                nsuns = 1;
744 >        for (i = nsuns; i--; )
745                  near_dprod[i] = -1.;
746          vector(svec, altitude, azimuth);
747          for (p = 1; p < nskypatch; p++) {
# Line 605 | Line 749 | AddDirect(float *parr)
749                  double  dprod;
750                  rh_vector(pvec, p);
751                  dprod = DOT(pvec, svec);
752 <                for (i = 0; i < 4; i++)
752 >                for (i = 0; i < nsuns; i++)
753                          if (dprod > near_dprod[i]) {
754 <                                for (j = 4; --j > i; ) {
754 >                                for (j = nsuns; --j > i; ) {
755                                          near_dprod[j] = near_dprod[j-1];
756                                          near_patch[j] = near_patch[j-1];
757                                  }
# Line 617 | Line 761 | AddDirect(float *parr)
761                          }
762          }
763          wtot = 0;                       /* weight by proximity */
764 <        for (i = 4; i--; )
764 >        for (i = nsuns; i--; )
765                  wtot += wta[i] = 1./(1.002 - near_dprod[i]);
766                                          /* add to nearest patch radiances */
767 <        for (i = 4; i--; )
768 <                parr[near_patch[i]] += wta[i] * dir_illum /
769 <                                        (wtot * rh_dom[near_patch[i]]);
767 >        for (i = nsuns; i--; ) {
768 >                float   *pdest = parr + 3*near_patch[i];
769 >                float   val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot);
770 >
771 >                val_add /= (fixed_sun_sa > 0)   ? fixed_sun_sa
772 >                                                : rh_dom[near_patch[i]] ;
773 >                *pdest++ += val_add*suncolor[0];
774 >                *pdest++ += val_add*suncolor[1];
775 >                *pdest++ += val_add*suncolor[2];
776 >        }
777   }
778  
779 + /* Output a sun to indicated file if appropriate for this time step */
780 + void
781 + OutputSun(int id, FILE *fp)
782 + {
783 +        double  srad;
784 +        FVECT   sv;
785 +
786 +        if ((altitude <= 0) | (dir_illum <= 1e-4))
787 +                return;
788 +
789 +        srad = DegToRad(SUN_ANG_DEG/2.);
790 +        srad = dir_illum/(WHTEFFICACY * PI*srad*srad);
791 +        vector(sv, altitude, azimuth);
792 +        fprintf(fp, "\nvoid light solar%d\n0\n0\n", id);
793 +        fprintf(fp, "3 %.3e %.3e %.3e\n", srad*suncolor[0],
794 +                        srad*suncolor[1], srad*suncolor[2]);
795 +        fprintf(fp, "\nsolar%d source sun%d\n0\n0\n", id, id);
796 +        fprintf(fp, "4 %.6f %.6f %.6f %.4f\n", sv[0], sv[1], sv[2], SUN_ANG_DEG);
797 + }
798 +
799   /* Initialize Reinhart sky patch positions (GW) */
800   int
801   rh_init(void)
# Line 656 | Line 827 | rh_init(void)
827          for (i = 0; i < NROW*rhsubdiv; i++) {
828                  const float     ralt = alpha*(i + .5);
829                  const int       ninrow = tnaz[i/rhsubdiv]*rhsubdiv;
830 <                const float     dom = (sin(alpha*(i+1)) - sin(alpha*i))/ninrow;
830 >                const float     dom = 2.*PI*(sin(alpha*(i+1)) - sin(alpha*i)) /
831 >                                                (double)ninrow;
832                  for (j = 0; j < ninrow; j++) {
833                          rh_palt[p] = ralt;
834                          rh_pazi[p] = 2.*PI * j / (double)ninrow;
# Line 761 | Line 933 | double CalcSkyClearness()
933          double sz_cubed;        /* Sun zenith angle cubed */
934  
935          /* Calculate sun zenith angle cubed */
936 <        sz_cubed = pow(sun_zenith, 3.0);
936 >        sz_cubed = sun_zenith*sun_zenith*sun_zenith;
937  
938          return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 *
939                          sz_cubed) / (1.0 + 1.041 * sz_cubed);
# Line 792 | Line 964 | double CalcDiffuseIrradiance()
964   double CalcDirectIrradiance()
965   {
966          return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041
967 <                        * pow(sun_zenith, 3.0)));
967 >                        * sun_zenith*sun_zenith*sun_zenith));
968   }
969  
970   /* Calculate sky brightness and clearness from illuminance values */
# Line 818 | Line 990 | int CalcSkyParamFromIllum()
990                  sky_clearness = 12.0;
991  
992          /* Limit sky brightness */
993 <        if (sky_brightness < 0.05)
993 >        if (sky_brightness < 0.01)
994                          sky_brightness = 0.01;
995  
996          while (((fabs(diff_irrad - test1) > 10.0) ||
# Line 831 | Line 1003 | int CalcSkyParamFromIllum()
1003                  /* Convert illuminance to irradiance */
1004                  index = GetCategoryIndex();
1005                  diff_irrad = diff_illum / CalcDiffuseIllumRatio(index);
1006 <                dir_irrad = dir_illum / CalcDirectIllumRatio(index);
1006 >                dir_irrad = CalcDirectIllumRatio(index);
1007 >                if (dir_irrad > 0.1)
1008 >                        dir_irrad = dir_illum / dir_irrad;
1009          
1010                  /* Calculate sky brightness and clearness */
1011                  sky_brightness = CalcSkyBrightness();
# Line 842 | Line 1016 | int CalcSkyParamFromIllum()
1016                          sky_clearness = 12.0;
1017          
1018                  /* Limit sky brightness */
1019 <                if (sky_brightness < 0.05)
1019 >                if (sky_brightness < 0.01)
1020                          sky_brightness = 0.01;
1021          }
1022  
# Line 928 | Line 1102 | double CalcRelHorzIllum( float *parr )
1102          double rh_illum = 0.0;  /* Relative horizontal illuminance */
1103  
1104          for (i = 1; i < nskypatch; i++)
1105 <                rh_illum += parr[3*i+1] * rh_cos(i);
1105 >                rh_illum += parr[3*i+1] * rh_cos(i) * rh_dom[i];
1106  
1107 <        return rh_illum * (2.0 * PI / (nskypatch-1));
1107 >        return rh_illum;
1108   }
1109  
1110   /* Calculate earth orbit eccentricity correction factor */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines