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.34 by greg, Tue Jan 7 01:42:30 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 247 | Line 249 | static const ModelCoeff DirectLumEff[8] =
249   };
250  
251   #ifndef NSUNPATCH
252 < #define NSUNPATCH       4               /* # patches to spread sun into */
252 > #define NSUNPATCH       4               /* max. # patches to spread sun into */
253   #endif
254  
255 < extern int jdate(int month, int day);
256 < 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;
255 > int             nsuns = NSUNPATCH;      /* number of sun patches to use */
256 > double          fixed_sun_sa = -1;      /* fixed solid angle per sun? */
257  
258   int             verbose = 0;            /* progress reports to stderr? */
259  
# Line 288 | Line 283 | extern int     rh_init(void);
283   extern float *  resize_dmatrix(float *mtx_data, int nsteps, int npatch);
284   extern void     AddDirect(float *parr);
285  
286 +
287 + static const char *
288 + getfmtname(int fmt)
289 + {
290 +        switch (fmt) {
291 +        case 'a':
292 +                return("ascii");
293 +        case 'f':
294 +                return("float");
295 +        case 'd':
296 +                return("double");
297 +        }
298 +        return("unknown");
299 + }
300 +
301 +
302   int
303   main(int argc, char *argv[])
304   {
305          char    buf[256];
306 +        int     doheader = 1;           /* output header? */
307 +        double  rotation = 0;           /* site rotation (degrees) */
308          double  elevation;              /* site elevation (meters) */
309          int     dir_is_horiz;           /* direct is meas. on horizontal? */
310 +        FILE    *sunsfp = NULL;         /* output file for individual suns */
311          float   *mtx_data = NULL;       /* our matrix data */
312 <        int     ntsteps = 0;            /* number of rows in matrix */
312 >        int     avgSky = 0;             /* compute average sky r.t. matrix? */
313 >        int     ntsteps = 0;            /* number of time steps */
314 >        int     tstorage = 0;           /* number of allocated time steps */
315 >        int     nstored = 0;            /* number of time steps in matrix */
316          int     last_monthly = 0;       /* month of last report */
317          int     mo, da;                 /* month (1-12) and day (1-31) */
318          double  hr;                     /* hour (local standard time) */
# Line 315 | Line 332 | main(int argc, char *argv[])
332                  case 'v':                       /* verbose progress reports */
333                          verbose++;
334                          break;
335 +                case 'h':                       /* turn off header */
336 +                        doheader = 0;
337 +                        break;
338                  case 'o':                       /* output format */
339                          switch (argv[i][2]) {
340                          case 'f':
# Line 326 | Line 346 | main(int argc, char *argv[])
346                                  goto userr;
347                          }
348                          break;
349 +                case 'O':                       /* output type */
350 +                        switch (argv[i][2]) {
351 +                        case '0':
352 +                                output = 0;
353 +                                break;
354 +                        case '1':
355 +                                output = 1;
356 +                                break;
357 +                        default:
358 +                                goto userr;
359 +                        }
360 +                        if (argv[i][3])
361 +                                goto userr;
362 +                        break;
363                  case 'm':                       /* Reinhart subdivisions */
364                          rhsubdiv = atoi(argv[++i]);
365                          break;
# Line 334 | Line 368 | main(int argc, char *argv[])
368                          skycolor[1] = atof(argv[++i]);
369                          skycolor[2] = atof(argv[++i]);
370                          break;
371 +                case 'n':                       /* no matrix output */
372 +                        avgSky = -1;
373 +                        rhsubdiv = 1;
374 +                        /* fall through */
375                  case 'd':                       /* solar (direct) only */
376                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
377 <                        if (suncolor[1] <= 1e-4)
340 <                                suncolor[0] = suncolor[1] = suncolor[2] = 1;
377 >                        grefl[0] = grefl[1] = grefl[2] = 0;
378                          break;
379 +                case 'D':                       /* output suns to file */
380 +                        sunsfp = fopen(argv[++i], "w");
381 +                        if (!sunsfp) {
382 +                                fprintf(stderr, "%s: cannot open '%s' for output\n",
383 +                                                argv[0], argv[i]);
384 +                                exit(1);
385 +                        }
386 +                        fixed_sun_sa = PI/360.*0.533;
387 +                        fixed_sun_sa *= PI*fixed_sun_sa;
388 +                        break;
389                  case 's':                       /* sky only (no direct) */
390                          suncolor[0] = suncolor[1] = suncolor[2] = 0;
344                        if (skycolor[1] <= 1e-4)
345                                skycolor[0] = skycolor[1] = skycolor[2] = 1;
391                          break;
392 +                case 'r':                       /* rotate distribution */
393 +                        if (argv[i][2] && argv[i][2] != 'z')
394 +                                goto userr;
395 +                        rotation = atof(argv[++i]);
396 +                        break;
397 +                case '5':                       /* 5-phase calculation */
398 +                        nsuns = 1;
399 +                        fixed_sun_sa = PI/360.*atof(argv[++i]);
400 +                        if (fixed_sun_sa <= 0) {
401 +                                fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n",
402 +                                                argv[0]);
403 +                                exit(1);
404 +                        }
405 +                        fixed_sun_sa *= fixed_sun_sa*PI;
406 +                        break;
407 +                case 'A':                       /* compute average sky */
408 +                        avgSky = 1;
409 +                        break;
410                  default:
411                          goto userr;
412                  }
# Line 398 | Line 461 | main(int argc, char *argv[])
461                                  progname, s_latitude, s_longitude);
462                  fprintf(stderr, "%s: %d sky patches per time step\n",
463                                  progname, nskypatch);
464 +                if (rotation != 0)
465 +                        fprintf(stderr, "%s: rotating output %.0f degrees\n",
466 +                                        progname, rotation);
467          }
468                                          /* convert quantities to radians */
469          s_latitude = DegToRad(s_latitude);
470          s_longitude = DegToRad(s_longitude);
471          s_meridian = DegToRad(s_meridian);
472 +                                        /* initial allocation */
473 +        mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch);
474                                          /* process each time step in tape */
475          while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) {
476                  double          sda, sta;
477 <                                        /* make space for next time step */
478 <                mtx_offset = 3*nskypatch*ntsteps++;
479 <                mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch);
477 >
478 >                mtx_offset = 3*nskypatch*nstored;
479 >                nstored += !avgSky | !nstored;
480 >                                        /* make space for next row */
481 >                if (nstored > tstorage) {
482 >                        tstorage += (tstorage>>1) + nstored + 7;
483 >                        mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch);
484 >                }
485 >                ntsteps++;              /* keep count of time steps */
486                  if (dif <= 1e-4) {
487 <                        memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
487 >                        if (!avgSky | !mtx_offset)
488 >                                memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
489                          continue;
490                  }
416                if (verbose && mo != last_monthly)
417                        fprintf(stderr, "%s: stepping through month %d...\n",
418                                                progname, last_monthly=mo);
491                                          /* compute solar position */
492                  julian_date = jdate(mo, da);
493                  sda = sdec(julian_date);
494                  sta = stadj(julian_date);
495                  altitude = salt(sda, hr+sta);
496 <                azimuth = sazi(sda, hr+sta) + PI;
496 >                azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation);
497                                          /* convert measured values */
498                  if (dir_is_horiz && altitude > 0.)
499                          dir /= sin(altitude);
# Line 434 | Line 506 | main(int argc, char *argv[])
506                  }
507                                          /* compute sky patch values */
508                  ComputeSky(mtx_data+mtx_offset);
509 +                                        /* output sun if indicated */
510 +                if (sunsfp && (altitude > 0) & (dir_illum > 1e-4)) {
511 +                        double  srad = dir_illum/(WHTEFFICACY * fixed_sun_sa);
512 +                        FVECT   sv;
513 +                        vector(sv, altitude, azimuth);
514 +                        fprintf(sunsfp, "\nvoid light solar%d\n0\n0\n", ntsteps);
515 +                        fprintf(sunsfp, "3 %.3e %.3e %.3e\n", srad*suncolor[0],
516 +                                        srad*suncolor[1], srad*suncolor[2]);
517 +                        fprintf(sunsfp, "\nsolar%d source sun%d\n0\n0\n", ntsteps, ntsteps);
518 +                        fprintf(sunsfp, "4 %.6f %.6f %.6f 0.533\n", sv[0], sv[1], sv[2]);
519 +                }
520 +                if (avgSky < 0)         /* no matrix? */
521 +                        continue;
522 +
523                  AddDirect(mtx_data+mtx_offset);
524 +                                        /* update cumulative sky? */
525 +                for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
526 +                        mtx_data[i] += mtx_data[mtx_offset+i];
527 +                                        /* monthly reporting */
528 +                if (verbose && mo != last_monthly)
529 +                        fprintf(stderr, "%s: stepping through month %d...\n",
530 +                                                progname, last_monthly=mo);
531          }
532 +        if (!ntsteps) {
533 +                fprintf(stderr, "%s: no valid time steps on input\n", progname);
534 +                exit(1);
535 +        }
536                                          /* check for junk at end */
537          while ((i = fgetc(stdin)) != EOF)
538                  if (!isspace(i)) {
# Line 446 | Line 543 | main(int argc, char *argv[])
543                          fputs(buf, stderr); fputc('\n', stderr);
544                          break;
545                  }
546 +
547 +        if (avgSky < 0)                 /* no matrix output? */
548 +                goto alldone;
549 +
550 +        dif = 1./(double)ntsteps;       /* average sky? */
551 +        for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
552 +                mtx_data[i] *= dif;
553                                          /* write out matrix */
554 +        if (outfmt != 'a')
555 +                SET_FILE_BINARY(stdout);
556   #ifdef getc_unlocked
557          flockfile(stdout);
558   #endif
559          if (verbose)
560                  fprintf(stderr, "%s: writing %smatrix with %d time steps...\n",
561 <                                progname, outfmt=='a' ? "" : "binary ", ntsteps);
561 >                                progname, outfmt=='a' ? "" : "binary ", nstored);
562 >        if (doheader) {
563 >                newheader("RADIANCE", stdout);
564 >                printargs(argc, argv, stdout);
565 >                printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude),
566 >                                        -RadToDeg(s_longitude));
567 >                printf("NROWS=%d\n", nskypatch);
568 >                printf("NCOLS=%d\n", nstored);
569 >                printf("NCOMP=3\n");
570 >                if ((outfmt == 'f') | (outfmt == 'd'))
571 >                        fputendian(stdout);
572 >                fputformat((char *)getfmtname(outfmt), stdout);
573 >                putchar('\n');
574 >        }
575                                          /* patches are rows (outer sort) */
576          for (i = 0; i < nskypatch; i++) {
577                  mtx_offset = 3*i;
578                  switch (outfmt) {
579                  case 'a':
580 <                        for (j = 0; j < ntsteps; j++) {
580 >                        for (j = 0; j < nstored; j++) {
581                                  printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset],
582                                                  mtx_data[mtx_offset+1],
583                                                  mtx_data[mtx_offset+2]);
584                                  mtx_offset += 3*nskypatch;
585                          }
586 <                        if (ntsteps > 1)
586 >                        if (nstored > 1)
587                                  fputc('\n', stdout);
588                          break;
589                  case 'f':
590 <                        for (j = 0; j < ntsteps; j++) {
591 <                                fwrite(mtx_data+mtx_offset, sizeof(float), 3,
590 >                        for (j = 0; j < nstored; j++) {
591 >                                putbinary(mtx_data+mtx_offset, sizeof(float), 3,
592                                                  stdout);
593                                  mtx_offset += 3*nskypatch;
594                          }
595                          break;
596                  case 'd':
597 <                        for (j = 0; j < ntsteps; j++) {
597 >                        for (j = 0; j < nstored; j++) {
598                                  double  ment[3];
599                                  ment[0] = mtx_data[mtx_offset];
600                                  ment[1] = mtx_data[mtx_offset+1];
601                                  ment[2] = mtx_data[mtx_offset+2];
602 <                                fwrite(ment, sizeof(double), 3, stdout);
602 >                                putbinary(ment, sizeof(double), 3, stdout);
603                                  mtx_offset += 3*nskypatch;
604                          }
605                          break;
# Line 488 | Line 607 | main(int argc, char *argv[])
607                  if (ferror(stdout))
608                          goto writerr;
609          }
610 <        if (fflush(stdout) == EOF)
610 > alldone:
611 >        if (fflush(NULL) == EOF)
612                  goto writerr;
613          if (verbose)
614                  fprintf(stderr, "%s: done.\n", progname);
615          exit(0);
616   userr:
617 <        fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g r g b][-c r g b][-o{f|d}] [tape.wea]\n",
617 >        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",
618                          progname);
619          exit(1);
620   fmterr:
621 <        fprintf(stderr, "%s: input weather tape format error\n", progname);
621 >        fprintf(stderr, "%s: weather tape format error in header\n", progname);
622          exit(1);
623   writerr:
624          fprintf(stderr, "%s: write error on output\n", progname);
# Line 547 | Line 667 | ComputeSky(float *parr)
667                  sky_brightness = CalcSkyBrightness();
668                  sky_clearness =  CalcSkyClearness();
669  
670 +                /* Limit sky clearness */
671 +                if (sky_clearness > 11.9)
672 +                        sky_clearness = 11.9;
673 +
674 +                /* Limit sky brightness */
675 +                if (sky_brightness < 0.01)
676 +                        sky_brightness = 0.01;
677 +
678                  /* Calculate illuminance */
679                  index = GetCategoryIndex();
680                  diff_illum = diff_irrad * CalcDiffuseIllumRatio(index);
# Line 558 | Line 686 | ComputeSky(float *parr)
686                  index = CalcSkyParamFromIllum();
687          }
688  
689 <        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
690 <                memset(parr, 0, sizeof(float)*3*nskypatch);
691 <                return;
689 >        if (output == 1) {                      /* hack for solar radiance */
690 >                diff_illum = diff_irrad * WHTEFFICACY;
691 >                dir_illum = dir_irrad * WHTEFFICACY;
692          }
693          /* Compute ground radiance (include solar contribution if any) */
694          parr[0] = diff_illum;
# Line 569 | Line 697 | ComputeSky(float *parr)
697          parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY);
698          multcolor(parr, grefl);
699  
700 +        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
701 +                memset(parr+3, 0, sizeof(float)*3*(nskypatch-1));
702 +                return;
703 +        }
704          /* Calculate Perez sky model parameters */
705          CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index);
706  
# Line 578 | Line 710 | ComputeSky(float *parr)
710          /* Calculate relative horizontal illuminance */
711          norm_diff_illum = CalcRelHorzIllum(parr);
712  
713 +        /* Check for zero sky -- make uniform in that case */
714 +        if (norm_diff_illum <= FTINY) {
715 +                for (i = 1; i < nskypatch; i++)
716 +                        setcolor(parr+3*i, 1., 1., 1.);
717 +                norm_diff_illum = PI;
718 +        }
719          /* Normalization coefficient */
720          norm_diff_illum = diff_illum / norm_diff_illum;
721  
# Line 600 | Line 738 | AddDirect(float *parr)
738  
739          if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4)
740                  return;
741 <                                        /* identify NSUNPATCH closest patches */
742 <        for (i = NSUNPATCH; i--; )
741 >                                        /* identify nsuns closest patches */
742 >        if (nsuns > NSUNPATCH)
743 >                nsuns = NSUNPATCH;
744 >        else if (nsuns <= 0)
745 >                nsuns = 1;
746 >        for (i = nsuns; i--; )
747                  near_dprod[i] = -1.;
748          vector(svec, altitude, azimuth);
749          for (p = 1; p < nskypatch; p++) {
# Line 609 | Line 751 | AddDirect(float *parr)
751                  double  dprod;
752                  rh_vector(pvec, p);
753                  dprod = DOT(pvec, svec);
754 <                for (i = 0; i < NSUNPATCH; i++)
754 >                for (i = 0; i < nsuns; i++)
755                          if (dprod > near_dprod[i]) {
756 <                                for (j = NSUNPATCH; --j > i; ) {
756 >                                for (j = nsuns; --j > i; ) {
757                                          near_dprod[j] = near_dprod[j-1];
758                                          near_patch[j] = near_patch[j-1];
759                                  }
# Line 621 | Line 763 | AddDirect(float *parr)
763                          }
764          }
765          wtot = 0;                       /* weight by proximity */
766 <        for (i = NSUNPATCH; i--; )
766 >        for (i = nsuns; i--; )
767                  wtot += wta[i] = 1./(1.002 - near_dprod[i]);
768                                          /* add to nearest patch radiances */
769 <        for (i = NSUNPATCH; i--; ) {
769 >        for (i = nsuns; i--; ) {
770                  float   *pdest = parr + 3*near_patch[i];
771 <                float   val_add = wta[i] * dir_illum /
772 <                                (WHTEFFICACY * wtot * rh_dom[near_patch[i]]);
771 >                float   val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot);
772 >
773 >                val_add /= (fixed_sun_sa > 0)   ? fixed_sun_sa
774 >                                                : rh_dom[near_patch[i]] ;
775                  *pdest++ += val_add*suncolor[0];
776                  *pdest++ += val_add*suncolor[1];
777                  *pdest++ += val_add*suncolor[2];
# Line 771 | Line 915 | double CalcSkyClearness()
915          double sz_cubed;        /* Sun zenith angle cubed */
916  
917          /* Calculate sun zenith angle cubed */
918 <        sz_cubed = pow(sun_zenith, 3.0);
918 >        sz_cubed = sun_zenith*sun_zenith*sun_zenith;
919  
920          return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 *
921                          sz_cubed) / (1.0 + 1.041 * sz_cubed);
# Line 802 | Line 946 | double CalcDiffuseIrradiance()
946   double CalcDirectIrradiance()
947   {
948          return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041
949 <                        * pow(sun_zenith, 3.0)));
949 >                        * sun_zenith*sun_zenith*sun_zenith));
950   }
951  
952   /* Calculate sky brightness and clearness from illuminance values */
# Line 828 | Line 972 | int CalcSkyParamFromIllum()
972                  sky_clearness = 12.0;
973  
974          /* Limit sky brightness */
975 <        if (sky_brightness < 0.05)
975 >        if (sky_brightness < 0.01)
976                          sky_brightness = 0.01;
977  
978          while (((fabs(diff_irrad - test1) > 10.0) ||
# Line 841 | Line 985 | int CalcSkyParamFromIllum()
985                  /* Convert illuminance to irradiance */
986                  index = GetCategoryIndex();
987                  diff_irrad = diff_illum / CalcDiffuseIllumRatio(index);
988 <                dir_irrad = dir_illum / CalcDirectIllumRatio(index);
988 >                dir_irrad = CalcDirectIllumRatio(index);
989 >                if (dir_irrad > 0.1)
990 >                        dir_irrad = dir_illum / dir_irrad;
991          
992                  /* Calculate sky brightness and clearness */
993                  sky_brightness = CalcSkyBrightness();
# Line 852 | Line 998 | int CalcSkyParamFromIllum()
998                          sky_clearness = 12.0;
999          
1000                  /* Limit sky brightness */
1001 <                if (sky_brightness < 0.05)
1001 >                if (sky_brightness < 0.01)
1002                          sky_brightness = 0.01;
1003          }
1004  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines