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.31 by greg, Sat Dec 28 18:05:14 2019 UTC vs.
Revision 2.36 by greg, Mon Apr 13 17:12:19 2020 UTC

# Line 252 | Line 252 | static const ModelCoeff DirectLumEff[8] =
252   #define NSUNPATCH       4               /* max. # patches to spread sun into */
253   #endif
254  
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  
# Line 270 | Line 272 | float          *rh_palt;               /* sky patch altitudes (radians) */
272   float           *rh_pazi;               /* sky patch azimuths (radians) */
273   float           *rh_dom;                /* sky patch solid angle (sr) */
274  
275 < #define         vector(v,alt,azi)       (       (v)[1] = tcos(alt), \
276 <                                                (v)[0] = (v)[1]*tsin(azi), \
277 <                                                (v)[1] *= tcos(azi), \
278 <                                                (v)[2] = tsin(alt) )
275 > #define         vector(v,alt,azi)       (       (v)[1] = cos(alt), \
276 >                                                (v)[0] = (v)[1]*sin(azi), \
277 >                                                (v)[1] *= cos(azi), \
278 >                                                (v)[2] = sin(alt) )
279  
280   #define         rh_vector(v,i)          vector(v,rh_palt[i],rh_pazi[i])
281  
282   #define         rh_cos(i)               tsin(rh_palt[i])
283  
284 + #define         solar_minute(jd,hr)     ((24*60)*((jd)-1)+(int)((hr)*60.+.5))
285 +
286   extern int      rh_init(void);
287   extern float *  resize_dmatrix(float *mtx_data, int nsteps, int npatch);
288 + extern void     OutputSun(int id, int goodsun, FILE *fp, FILE *mfp);
289   extern void     AddDirect(float *parr);
290  
291  
# Line 306 | Line 311 | main(int argc, char *argv[])
311          int     doheader = 1;           /* output header? */
312          double  rotation = 0;           /* site rotation (degrees) */
313          double  elevation;              /* site elevation (meters) */
314 +        int     leap_day = 0;           /* add leap day? */
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     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 */
316        int     inconsistent = 0;       /* inconsistent options set? */
324          int     mo, da;                 /* month (1-12) and day (1-31) */
325          double  hr;                     /* hour (local standard time) */
326          double  dir, dif;               /* direct and diffuse values */
# Line 364 | Line 371 | main(int argc, char *argv[])
371                          rhsubdiv = atoi(argv[++i]);
372                          break;
373                  case 'c':                       /* sky color */
367                        inconsistent |= (skycolor[1] <= 1e-4);
374                          skycolor[0] = atof(argv[++i]);
375                          skycolor[1] = atof(argv[++i]);
376                          skycolor[2] = atof(argv[++i]);
377                          break;
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 <                        if (suncolor[1] <= 1e-4) {
398 <                                inconsistent = 1;
399 <                                suncolor[0] = suncolor[1] = suncolor[2] = 1;
397 >                        grefl[0] = grefl[1] = grefl[2] = 0;
398 >                        break;
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;
381                        if (skycolor[1] <= 1e-4) {
382                                inconsistent = 1;
383                                skycolor[0] = skycolor[1] = skycolor[2] = 1;
384                        }
408                          break;
409                  case 'r':                       /* rotate distribution */
410                          if (argv[i][2] && argv[i][2] != 'z')
# Line 393 | Line 416 | main(int argc, char *argv[])
416                          fixed_sun_sa = PI/360.*atof(argv[++i]);
417                          if (fixed_sun_sa <= 0) {
418                                  fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n",
419 <                                                argv[0]);
419 >                                                progname);
420                                  exit(1);
421                          }
422                          fixed_sun_sa *= fixed_sun_sa*PI;
# Line 406 | Line 429 | main(int argc, char *argv[])
429                  }
430          if (i < argc-1)
431                  goto userr;
409        if (inconsistent)
410                fprintf(stderr, "%s: WARNING: inconsistent -s, -d, -c options!\n",
411                                progname);
432          if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
433                  fprintf(stderr, "%s: cannot open '%s' for input\n",
434                                  progname, argv[i]);
435                  exit(1);
436          }
437 +        if ((modsfp != NULL) & (sunsfp == NULL))
438 +                fprintf(stderr, "%s: warning -M output will be empty without -D\n",
439 +                                progname);
440          if (verbose) {
441                  if (i == argc-1)
442                          fprintf(stderr, "%s: reading weather tape '%s'\n",
# Line 456 | Line 479 | main(int argc, char *argv[])
479                  fprintf(stderr, "%s: location '%s'\n", progname, buf);
480                  fprintf(stderr, "%s: (lat,long)=(%.1f,%.1f) degrees north, west\n",
481                                  progname, s_latitude, s_longitude);
482 <                fprintf(stderr, "%s: %d sky patches per time step\n",
483 <                                progname, nskypatch);
482 >                if (avgSky >= 0)
483 >                        fprintf(stderr, "%s: %d sky patches\n",
484 >                                        progname, nskypatch);
485 >                if (sunsfp)
486 >                        fprintf(stderr, "%s: outputting suns to file\n",
487 >                                        progname);
488                  if (rotation != 0)
489                          fprintf(stderr, "%s: rotating output %.0f degrees\n",
490                                          progname, rotation);
# Line 480 | Line 507 | main(int argc, char *argv[])
507                          mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch);
508                  }
509                  ntsteps++;              /* keep count of time steps */
483                if (dif <= 1e-4) {
484                        if (!avgSky | !mtx_offset)
485                                memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
486                        continue;
487                }
488                if (verbose && mo != last_monthly)
489                        fprintf(stderr, "%s: stepping through month %d...\n",
490                                                progname, last_monthly=mo);
510                                          /* compute solar position */
511 <                julian_date = jdate(mo, da);
511 >                if ((mo == 2) & (da == 29)) {
512 >                        julian_date = 60;
513 >                        leap_day = 1;
514 >                } else
515 >                        julian_date = jdate(mo, da) + leap_day;
516                  sda = sdec(julian_date);
517                  sta = stadj(julian_date);
518                  altitude = salt(sda, hr+sta);
519                  azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation);
520 +
521 +                if (dir+dif <= 1e-4) {  /* effectively nighttime? */
522 +                        if (!avgSky | !mtx_offset)
523 +                                memset(mtx_data+mtx_offset, 0,
524 +                                                sizeof(float)*3*nskypatch);
525 +                        if (sunsfp)     /* output black sun */
526 +                                OutputSun(solar_minute(julian_date,hr), 0,
527 +                                                        sunsfp, modsfp);
528 +                        continue;
529 +                }
530                                          /* convert measured values */
531                  if (dir_is_horiz && altitude > 0.)
532                          dir /= sin(altitude);
# Line 506 | Line 539 | main(int argc, char *argv[])
539                  }
540                                          /* compute sky patch values */
541                  ComputeSky(mtx_data+mtx_offset);
542 +
543 +                if (sunsfp)             /* output sun if requested */
544 +                        OutputSun(solar_minute(julian_date,hr), 1,
545 +                                                sunsfp, modsfp);
546 +
547 +                if (avgSky < 0)         /* no matrix? */
548 +                        continue;
549 +
550                  AddDirect(mtx_data+mtx_offset);
551                                          /* update cumulative sky? */
552                  for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
553                          mtx_data[i] += mtx_data[mtx_offset+i];
554 +                                        /* monthly reporting */
555 +                if (verbose && mo != last_monthly)
556 +                        fprintf(stderr, "%s: stepping through month %d...\n",
557 +                                                progname, last_monthly=mo);
558 +                                        /* note whether leap-day was given */
559          }
560 +        if (!ntsteps) {
561 +                fprintf(stderr, "%s: no valid time steps on input\n", progname);
562 +                exit(1);
563 +        }
564                                          /* check for junk at end */
565          while ((i = fgetc(stdin)) != EOF)
566                  if (!isspace(i)) {
# Line 521 | Line 571 | main(int argc, char *argv[])
571                          fputs(buf, stderr); fputc('\n', stderr);
572                          break;
573                  }
574 <        if (!ntsteps) {
575 <                fprintf(stderr, "%s: no valid time steps on input\n", progname);
576 <                exit(1);
577 <        }
574 >
575 >        if (avgSky < 0)                 /* no matrix output? */
576 >                goto alldone;
577 >
578          dif = 1./(double)ntsteps;       /* average sky? */
579          for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
580                  mtx_data[i] *= dif;
# Line 585 | Line 635 | main(int argc, char *argv[])
635                  if (ferror(stdout))
636                          goto writerr;
637          }
638 <        if (fflush(stdout) == EOF)
638 > alldone:
639 >        if (fflush(NULL) == EOF)
640                  goto writerr;
641          if (verbose)
642                  fprintf(stderr, "%s: done.\n", progname);
643          exit(0);
644   userr:
645 <        fprintf(stderr, "Usage: %s [-v][-h][-A][-d|-s][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n",
645 >        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",
646                          progname);
647          exit(1);
648   fmterr:
649 <        fprintf(stderr, "%s: input weather tape format error\n", progname);
649 >        fprintf(stderr, "%s: weather tape format error in header\n", progname);
650          exit(1);
651   writerr:
652          fprintf(stderr, "%s: write error on output\n", progname);
# Line 667 | Line 718 | ComputeSky(float *parr)
718                  diff_illum = diff_irrad * WHTEFFICACY;
719                  dir_illum = dir_irrad * WHTEFFICACY;
720          }
670
671        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
672                memset(parr, 0, sizeof(float)*3*nskypatch);
673                return;
674        }
721          /* Compute ground radiance (include solar contribution if any) */
722          parr[0] = diff_illum;
723          if (altitude > 0)
# Line 679 | Line 725 | ComputeSky(float *parr)
725          parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY);
726          multcolor(parr, grefl);
727  
728 +        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
729 +                memset(parr+3, 0, sizeof(float)*3*(nskypatch-1));
730 +                return;
731 +        }
732          /* Calculate Perez sky model parameters */
733          CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index);
734  
# Line 754 | Line 804 | AddDirect(float *parr)
804                  *pdest++ += val_add*suncolor[1];
805                  *pdest++ += val_add*suncolor[2];
806          }
807 + }
808 +
809 + /* Output a sun to indicated file if appropriate for this time step */
810 + void
811 + OutputSun(int id, int goodsun, FILE *fp, FILE *mfp)
812 + {
813 +        double  srad;
814 +        FVECT   sv;
815 +
816 +        srad = DegToRad(SUN_ANG_DEG/2.);
817 +
818 +        if (altitude < -srad)           /* well below horizon? */
819 +                return;
820 +
821 +        srad = goodsun ? dir_illum/(WHTEFFICACY * PI*srad*srad) : 0;
822 +        vector(sv, altitude, azimuth);
823 +        fprintf(fp, "\nvoid light solar%d\n0\n0\n", id);
824 +        fprintf(fp, "3 %.3e %.3e %.3e\n", srad*suncolor[0],
825 +                        srad*suncolor[1], srad*suncolor[2]);
826 +        fprintf(fp, "\nsolar%d source sun%d\n0\n0\n", id, id);
827 +        fprintf(fp, "4 %.6f %.6f %.6f %.4f\n", sv[0], sv[1], sv[2], SUN_ANG_DEG);
828 +        
829 +        if (mfp != NULL)                /* saving modifier IDs? */
830 +                fprintf(mfp, "solar%d\n", id);
831   }
832  
833   /* Initialize Reinhart sky patch positions (GW) */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines