--- ray/src/gen/gendaymtx.c 2013/02/09 00:20:24 2.9 +++ ray/src/gen/gendaymtx.c 2014/06/17 18:51:47 2.15 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: gendaymtx.c,v 2.9 2013/02/09 00:20:24 greg Exp $"; +static const char RCSid[] = "$Id: gendaymtx.c,v 2.15 2014/06/17 18:51:47 greg Exp $"; #endif /* * gendaymtx.c @@ -86,6 +86,7 @@ static const char RCSid[] = "$Id: gendaymtx.c,v 2.9 20 #include #include #include "rtmath.h" +#include "platform.h" #include "color.h" char *progname; /* Program name */ @@ -108,6 +109,7 @@ double sky_clearness; /* Sky clearness */ double solar_rad; /* Solar radiance */ double sun_zenith; /* Sun zenith angle (radians) */ int input = 0; /* Input type */ +int output = 0; /* Output type */ extern double dmax( double, double ); extern double CalcAirMass(); @@ -208,7 +210,7 @@ static const CategoryBounds SkyClearCat[8] = { 1.950, 2.800 }, { 2.800, 4.500 }, { 4.500, 6.200 }, - { 6.200, 12.00 } /* Clear */ + { 6.200, 12.01 } /* Clear */ }; /* Luminous efficacy model coefficients */ @@ -247,7 +249,7 @@ static const ModelCoeff DirectLumEff[8] = }; #ifndef NSUNPATCH -#define NSUNPATCH 4 /* # patches to spread sun into */ +#define NSUNPATCH 4 /* max. # patches to spread sun into */ #endif extern int jdate(int month, int day); @@ -260,6 +262,9 @@ extern double s_latitude; extern double s_longitude; extern double s_meridian; +int nsuns = NSUNPATCH; /* number of sun patches to use */ +double fixed_sun_sa = -1; /* fixed solid angle per sun? */ + int verbose = 0; /* progress reports to stderr? */ int outfmt = 'a'; /* output format */ @@ -288,10 +293,27 @@ extern int rh_init(void); extern float * resize_dmatrix(float *mtx_data, int nsteps, int npatch); extern void AddDirect(float *parr); + +static const char * +getfmtname(int fmt) +{ + switch (fmt) { + case 'a': + return("ascii"); + case 'f': + return("float"); + case 'd': + return("double"); + } + return("unknown"); +} + + int main(int argc, char *argv[]) { char buf[256]; + int doheader = 1; /* output header? */ double rotation = 0; /* site rotation (degrees) */ double elevation; /* site elevation (meters) */ int dir_is_horiz; /* direct is meas. on horizontal? */ @@ -316,6 +338,9 @@ main(int argc, char *argv[]) case 'v': /* verbose progress reports */ verbose++; break; + case 'h': /* turn off header */ + doheader = 0; + break; case 'o': /* output format */ switch (argv[i][2]) { case 'f': @@ -327,6 +352,20 @@ main(int argc, char *argv[]) goto userr; } break; + case 'O': /* output type */ + switch (argv[i][2]) { + case '0': + output = 0; + break; + case '1': + output = 1; + break; + default: + goto userr; + } + if (argv[i][3]) + goto userr; + break; case 'm': /* Reinhart subdivisions */ rhsubdiv = atoi(argv[++i]); break; @@ -350,6 +389,10 @@ main(int argc, char *argv[]) goto userr; rotation = atof(argv[++i]); break; + case '5': /* 5-phase calculation */ + nsuns = 1; + fixed_sun_sa = 6.797e-05; + break; default: goto userr; } @@ -414,10 +457,14 @@ main(int argc, char *argv[]) s_meridian = DegToRad(s_meridian); /* process each time step in tape */ while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) { + static int step_alloc = 0; double sda, sta; /* make space for next time step */ mtx_offset = 3*nskypatch*ntsteps++; - mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch); + if (ntsteps > step_alloc) { + step_alloc += step_alloc>>1 + 8; + mtx_data = resize_dmatrix(mtx_data, step_alloc, nskypatch); + } if (dif <= 1e-4) { memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch); continue; @@ -456,12 +503,25 @@ main(int argc, char *argv[]) break; } /* write out matrix */ + if (outfmt != 'a') + SET_FILE_BINARY(stdout); #ifdef getc_unlocked flockfile(stdout); #endif if (verbose) fprintf(stderr, "%s: writing %smatrix with %d time steps...\n", progname, outfmt=='a' ? "" : "binary ", ntsteps); + if (doheader) { + newheader("RADIANCE", stdout); + printargs(argc, argv, stdout); + printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude), + -RadToDeg(s_longitude)); + printf("NROWS=%d\n", nskypatch); + printf("NCOLS=%d\n", ntsteps); + printf("NCOMP=3\n"); + fputformat(getfmtname(outfmt), stdout); + putchar('\n'); + } /* patches are rows (outer sort) */ for (i = 0; i < nskypatch; i++) { mtx_offset = 3*i; @@ -503,7 +563,7 @@ main(int argc, char *argv[]) fprintf(stderr, "%s: done.\n", progname); exit(0); userr: - fprintf(stderr, "Usage: %s [-v][-d|-s][-r deg][-m N][-g r g b][-c r g b][-o{f|d}] [tape.wea]\n", + fprintf(stderr, "Usage: %s [-v][-h][-d|-s][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n", progname); exit(1); fmterr: @@ -562,7 +622,7 @@ ComputeSky(float *parr) /* Limit sky brightness */ if (sky_brightness < 0.01) - sky_brightness = 0.01; + sky_brightness = 0.01; /* Calculate illuminance */ index = GetCategoryIndex(); @@ -575,6 +635,11 @@ ComputeSky(float *parr) index = CalcSkyParamFromIllum(); } + if (output == 1) { /* hack for solar radiance */ + diff_illum = diff_irrad * WHTEFFICACY; + dir_illum = dir_irrad * WHTEFFICACY; + } + if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ memset(parr, 0, sizeof(float)*3*nskypatch); return; @@ -595,6 +660,12 @@ ComputeSky(float *parr) /* Calculate relative horizontal illuminance */ norm_diff_illum = CalcRelHorzIllum(parr); + /* Check for zero sky -- make uniform in that case */ + if (norm_diff_illum <= FTINY) { + for (i = 1; i < nskypatch; i++) + setcolor(parr+3*i, 1., 1., 1.); + norm_diff_illum = PI; + } /* Normalization coefficient */ norm_diff_illum = diff_illum / norm_diff_illum; @@ -617,8 +688,12 @@ AddDirect(float *parr) if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4) return; - /* identify NSUNPATCH closest patches */ - for (i = NSUNPATCH; i--; ) + /* identify nsuns closest patches */ + if (nsuns > NSUNPATCH) + nsuns = NSUNPATCH; + else if (nsuns <= 0) + nsuns = 1; + for (i = nsuns; i--; ) near_dprod[i] = -1.; vector(svec, altitude, azimuth); for (p = 1; p < nskypatch; p++) { @@ -626,9 +701,9 @@ AddDirect(float *parr) double dprod; rh_vector(pvec, p); dprod = DOT(pvec, svec); - for (i = 0; i < NSUNPATCH; i++) + for (i = 0; i < nsuns; i++) if (dprod > near_dprod[i]) { - for (j = NSUNPATCH; --j > i; ) { + for (j = nsuns; --j > i; ) { near_dprod[j] = near_dprod[j-1]; near_patch[j] = near_patch[j-1]; } @@ -638,13 +713,15 @@ AddDirect(float *parr) } } wtot = 0; /* weight by proximity */ - for (i = NSUNPATCH; i--; ) + for (i = nsuns; i--; ) wtot += wta[i] = 1./(1.002 - near_dprod[i]); /* add to nearest patch radiances */ - for (i = NSUNPATCH; i--; ) { + for (i = nsuns; i--; ) { float *pdest = parr + 3*near_patch[i]; - float val_add = wta[i] * dir_illum / - (WHTEFFICACY * wtot * rh_dom[near_patch[i]]); + float val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot); + + val_add /= (fixed_sun_sa > 0) ? fixed_sun_sa + : rh_dom[near_patch[i]] ; *pdest++ += val_add*suncolor[0]; *pdest++ += val_add*suncolor[1]; *pdest++ += val_add*suncolor[2]; @@ -788,7 +865,7 @@ double CalcSkyClearness() double sz_cubed; /* Sun zenith angle cubed */ /* Calculate sun zenith angle cubed */ - sz_cubed = pow(sun_zenith, 3.0); + sz_cubed = sun_zenith*sun_zenith*sun_zenith; return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 * sz_cubed) / (1.0 + 1.041 * sz_cubed); @@ -819,7 +896,7 @@ double CalcDiffuseIrradiance() double CalcDirectIrradiance() { return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041 - * pow(sun_zenith, 3.0))); + * sun_zenith*sun_zenith*sun_zenith)); } /* Calculate sky brightness and clearness from illuminance values */