ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/gensky.c
(Generate patch)

Comparing ray/src/gen/gensky.c (file contents):
Revision 2.9 by greg, Wed Dec 9 08:32:52 1992 UTC vs.
Revision 2.17 by greg, Tue Jul 16 14:14:50 1996 UTC

# Line 17 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
17  
18   #include  <math.h>
19  
20 + #include  <ctype.h>
21 +
22   #include  "color.h"
23  
22 #ifndef atof
23 extern double  atof();
24 #endif
24   extern char  *strcpy(), *strcat(), *malloc();
25 < extern double  stadj(), sdec(), sazi(), salt();
25 > extern double  stadj(), sdec(), sazi(), salt(), tz2mer();
26  
27 < #define  PI             3.141592654
27 > #ifndef  PI
28 > #define  PI             3.14159265358979323846
29 > #endif
30  
31   #define  DOT(v1,v2)     (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
32  
33 + #define  S_CLEAR        1
34 + #define  S_OVER         2
35 + #define  S_UNIF         3
36 + #define  S_INTER        4
37 +
38 + #define  overcast       (skytype==S_OVER|skytype==S_UNIF)
39 +
40   double  normsc();
41                                          /* sun calculation constants */
42   extern double  s_latitude;
43   extern double  s_longitude;
44   extern double  s_meridian;
45 +
46 + #undef  toupper
47 + #define  toupper(c)     ((c) & ~0x20)   /* ASCII trick to convert case */
48 +
49 +                                        /* European and North American zones */
50 + struct {
51 +        char    zname[8];       /* time zone name (all caps) */
52 +        float   zmer;           /* standard meridian */
53 + } tzone[] = {
54 +        "YST", 135, "YDT", 120,
55 +        "PST", 120, "PDT", 105,
56 +        "MST", 105, "MDT", 90,
57 +        "CST", 90, "CDT", 75,
58 +        "EST", 75, "EDT", 60,
59 +        "AST", 60, "ADT", 45,
60 +        "NST", 52.5, "NDT", 37.5,
61 +        "GMT", 0, "BST", -15,
62 +        "WET", -15, "WETDST", -30,
63 +        "MET", -30, "METDST", -45,
64 +        "MEZ", -30, "MESZ", -45,
65 +        "", 0
66 + };
67                                          /* required values */
68   int  month, day;                                /* date */
69   double  hour;                                   /* time */
70   int  tsolar;                                    /* 0=standard, 1=solar */
71   double  altitude, azimuth;                      /* or solar angles */
72                                          /* default values */
73 < int  cloudy = 0;                                /* 1=standard, 2=uniform */
73 > int  skytype = S_CLEAR;                         /* sky type */
74   int  dosun = 1;
75 < double  zenithbr = -1.0;
75 > double  zenithbr = 0.0;
76 > int     u_zenith = 0;                           /* -1=irradiance, 1=radiance */
77   double  turbidity = 2.75;
78   double  gprefl = 0.2;
79                                          /* computed values */
80   double  sundir[3];
81   double  groundbr;
82   double  F2;
83 < double  solarbr = -1.0;
83 > double  solarbr = 0.0;
84 > int     u_solar = 0;                            /* -1=irradiance, 1=radiance */
85  
86   char  *progname;
87   char  errmsg[128];
# Line 79 | Line 111 | char  *argv[];
111                  day = atoi(argv[2]);
112                  if (day < 1 || day > 31)
113                          userror("bad day");
114 <                hour = atof(argv[3]);
83 <                if (hour < 0 || hour >= 24)
84 <                        userror("bad hour");
85 <                tsolar = argv[3][0] == '+';
114 >                cvthour(argv[3]);
115          }
116          for (i = 4; i < argc; i++)
117                  if (argv[i][0] == '-' || argv[i][0] == '+')
118                          switch (argv[i][1]) {
119                          case 's':
120 <                                cloudy = 0;
120 >                                skytype = S_CLEAR;
121                                  dosun = argv[i][0] == '+';
122                                  break;
123                          case 'r':
124 +                        case 'R':
125 +                                u_solar = argv[i][1]=='R' ? -1 : 1;
126                                  solarbr = atof(argv[++i]);
127                                  break;
128                          case 'c':
129 <                                cloudy = argv[i][0] == '+' ? 2 : 1;
99 <                                dosun = 0;
129 >                                skytype = S_OVER;
130                                  break;
131 +                        case 'u':
132 +                                skytype = S_UNIF;
133 +                                break;
134 +                        case 'i':
135 +                                skytype = S_INTER;
136 +                                dosun = argv[i][0] == '+';
137 +                                break;
138                          case 't':
139                                  turbidity = atof(argv[++i]);
140                                  break;
141                          case 'b':
142 +                        case 'B':
143 +                                u_zenith = argv[i][1]=='B' ? -1 : 1;
144                                  zenithbr = atof(argv[++i]);
145                                  break;
146                          case 'g':
# Line 132 | Line 171 | char  *argv[];
171  
172          computesky();
173          printsky();
174 +
175 +        exit(0);
176   }
177  
178  
179   computesky()                    /* compute sky parameters */
180   {
181 +        double  normfactor;
182                                          /* compute solar direction */
183          if (month) {                    /* from date and time */
184                  int  jd;
# Line 150 | Line 192 | computesky()                   /* compute sky parameters */
192                          st = hour + stadj(jd);
193                  altitude = salt(sd, st);
194                  azimuth = sazi(sd, st);
195 +                printf("# Local solar time: %.2f\n", st);
196 +                printf("# Solar altitude and azimuth: %.1f %.1f\n",
197 +                                180./PI*altitude, 180./PI*azimuth);
198          }
199 <        if (!cloudy && altitude > 87.*PI/180.) {
199 >        if (!overcast && altitude > 87.*PI/180.) {
200                  fprintf(stderr,
201   "%s: warning - sun too close to zenith, reducing altitude to 87 degrees\n",
202                                  progname);
# Line 163 | Line 208 | computesky()                   /* compute sky parameters */
208          sundir[1] = -cos(azimuth)*cos(altitude);
209          sundir[2] = sin(altitude);
210  
211 +                                        /* Compute normalization factor */
212 +        switch (skytype) {
213 +        case S_UNIF:
214 +                normfactor = 1.0;
215 +                break;
216 +        case S_OVER:
217 +                normfactor = 0.777778;
218 +                break;
219 +        case S_CLEAR:
220 +                F2 = 0.274*(0.91 + 10.0*exp(-3.0*(PI/2.0-altitude)) +
221 +                                0.45*sundir[2]*sundir[2]);
222 +                normfactor = normsc()/F2/PI;
223 +                break;
224 +        case S_INTER:
225 +                F2 = (2.739 + .9891*sin(.3119+2.6*altitude)) *
226 +                        exp(-(PI/2.0-altitude)*(.4441+1.48*altitude));
227 +                normfactor = normsc()/F2/PI;
228 +                break;
229 +        }
230                                          /* Compute zenith brightness */
231 <        if (zenithbr <= 0.0)
232 <                if (cloudy) {
231 >        if (u_zenith == -1)
232 >                zenithbr /= normfactor*PI;
233 >        else if (u_zenith == 0) {
234 >                if (overcast)
235                          zenithbr = 8.6*sundir[2] + .123;
236 <                        zenithbr *= 1000.0/WHTEFFICACY;
171 <                } else {
236 >                else
237                          zenithbr = (1.376*turbidity-1.81)*tan(altitude)+0.38;
238 <                        zenithbr *= 1000.0/SKYEFFICACY;
239 <                }
240 <        if (zenithbr < 0.0)
241 <                zenithbr = 0.0;
177 <                                        /* Compute horizontal radiance */
178 <        if (cloudy) {
179 <                if (cloudy == 2)
180 <                        groundbr = zenithbr;
238 >                if (skytype == S_INTER)
239 >                        zenithbr = (zenithbr + 8.6*sundir[2] + .123)/2.0;
240 >                if (zenithbr < 0.0)
241 >                        zenithbr = 0.0;
242                  else
243 <                        groundbr = zenithbr*0.777778;
183 <                printf("# Ground ambient level: %f\n", groundbr);
184 <        } else {
185 <                F2 = 0.274*(0.91 + 10.0*exp(-3.0*(PI/2.0-altitude)) +
186 <                                0.45*sundir[2]*sundir[2]);
187 <                groundbr = zenithbr*normsc(altitude)/F2/PI;
188 <                printf("# Ground ambient level: %f\n", groundbr);
189 <                if (sundir[2] > 0.0 && solarbr != 0.0) {
190 <                        if (solarbr < 0.0)
191 <                                solarbr = 1.5e9/SUNEFFICACY *
192 <                                (1.147 - .147/(sundir[2]>.16?sundir[2]:.16));
193 <                        groundbr += solarbr*6e-5*sundir[2]/PI;
194 <                } else
195 <                        dosun = 0;
243 >                        zenithbr *= 1000.0/SKYEFFICACY;
244          }
245 +                                        /* Compute horizontal radiance */
246 +        groundbr = zenithbr*normfactor;
247 +        printf("# Ground ambient level: %.1f\n", groundbr);
248 +        if (!overcast && sundir[2] > 0.0 && (!u_solar || solarbr > 0.0)) {
249 +                if (u_solar == -1)
250 +                        solarbr /= 6e-5*sundir[2];
251 +                else if (u_solar == 0) {
252 +                        solarbr = 1.5e9/SUNEFFICACY *
253 +                        (1.147 - .147/(sundir[2]>.16?sundir[2]:.16));
254 +                        if (skytype == S_INTER)
255 +                                solarbr *= 0.15;        /* fudge factor! */
256 +                }
257 +                groundbr += 6e-5/PI*solarbr*sundir[2];
258 +        } else
259 +                dosun = 0;
260          groundbr *= gprefl;
261   }
262  
# Line 210 | Line 273 | printsky()                     /* print out sky */
273          }
274          
275          printf("\nvoid brightfunc skyfunc\n");
276 <        printf("2 skybright skybright.cal\n");
276 >        printf("2 skybr skybright.cal\n");
277          printf("0\n");
278 <        if (cloudy)
279 <                printf("3 %d %.2e %.2e\n", cloudy, zenithbr, groundbr);
278 >        if (overcast)
279 >                printf("3 %d %.2e %.2e\n", skytype, zenithbr, groundbr);
280          else
281 <                printf("7 -1 %.2e %.2e %.2e %f %f %f\n", zenithbr, groundbr,
282 <                                F2, sundir[0], sundir[1], sundir[2]);
281 >                printf("7 %d %.2e %.2e %.2e %f %f %f\n",
282 >                                skytype, zenithbr, groundbr, F2,
283 >                                sundir[0], sundir[1], sundir[2]);
284   }
285  
286  
287   printdefaults()                 /* print default values */
288   {
289 <        if (cloudy == 1)
289 >        switch (skytype) {
290 >        case S_OVER:
291                  printf("-c\t\t\t\t# Cloudy sky\n");
292 <        else if (cloudy == 2)
293 <                printf("+c\t\t\t\t# Uniform cloudy sky\n");
294 <        else if (dosun)
295 <                printf("+s\t\t\t\t# Sunny sky with sun\n");
296 <        else
297 <                printf("-s\t\t\t\t# Sunny sky without sun\n");
292 >                break;
293 >        case S_UNIF:
294 >                printf("-u\t\t\t\t# Uniform cloudy sky\n");
295 >                break;
296 >        case S_INTER:
297 >                if (dosun)
298 >                        printf("+i\t\t\t\t# Intermediate sky with sun\n");
299 >                else
300 >                        printf("-i\t\t\t\t# Intermediate sky without sun\n");
301 >                break;
302 >        case S_CLEAR:
303 >                if (dosun)
304 >                        printf("+s\t\t\t\t# Sunny sky with sun\n");
305 >                else
306 >                        printf("-s\t\t\t\t# Sunny sky without sun\n");
307 >                break;
308 >        }
309          printf("-g %f\t\t\t# Ground plane reflectance\n", gprefl);
310          if (zenithbr > 0.0)
311                  printf("-b %f\t\t\t# Zenith radiance (watts/ster/m2\n", zenithbr);
# Line 254 | Line 330 | char  *msg;
330  
331  
332   double
333 < normsc(theta)                   /* compute normalization factor (E0*F2/L0) */
258 < double  theta;
333 > normsc()                        /* compute normalization factor (E0*F2/L0) */
334   {
335 <        static double  nf[5] = {2.766521, 0.547665,
336 <                                -0.369832, 0.009237, 0.059229};
335 >        static double  nfc[2][5] = {
336 >                                /* clear sky approx. */
337 >                {2.766521, 0.547665, -0.369832, 0.009237, 0.059229},
338 >                                /* intermediate sky approx. */
339 >                {3.5556, -2.7152, -1.3081, 1.0660, 0.60227},
340 >        };
341 >        register double  *nf;
342          double  x, nsc;
343          register int  i;
344                                          /* polynomial approximation */
345 <        x = (theta - PI/4.0)/(PI/4.0);
346 <        nsc = nf[4];
347 <        for (i = 3; i >= 0; i--)
345 >        nf = nfc[skytype==S_INTER];
346 >        x = (altitude - PI/4.0)/(PI/4.0);
347 >        nsc = nf[i=4];
348 >        while (i--)
349                  nsc = nsc*x + nf[i];
350  
351          return(nsc);
352 + }
353 +
354 +
355 + cvthour(hs)                     /* convert hour string */
356 + char  *hs;
357 + {
358 +        register char  *cp = hs;
359 +        register int    i, j;
360 +
361 +        if (tsolar = *cp == '+') cp++;          /* solar time? */
362 +        while (isdigit(*cp)) cp++;
363 +        if (*cp == ':')
364 +                hour = atoi(hs) + atoi(++cp)/60.0;
365 +        else {
366 +                hour = atof(hs);
367 +                if (*cp == '.') cp++;
368 +        }
369 +        while (isdigit(*cp)) cp++;
370 +        if (!*cp)
371 +                return;
372 +        if (tsolar || !isalpha(*cp)) {
373 +                fprintf(stderr, "%s: bad time format: %s\n", progname, hs);
374 +                exit(1);
375 +        }
376 +        i = 0;
377 +        do {
378 +                for (j = 0; cp[j]; j++)
379 +                        if (toupper(cp[j]) != tzone[i].zname[j])
380 +                                break;
381 +                if (!cp[j] && !tzone[i].zname[j]) {
382 +                        s_meridian = tzone[i].zmer * (PI/180);
383 +                        return;
384 +                }
385 +        } while (tzone[i++].zname[0]);
386 +
387 +        fprintf(stderr, "%s: unknown time zone: %s\n", progname, cp);
388 +        fprintf(stderr, "Known time zones:\n\t%s", tzone[0].zname);
389 +        for (i = 1; tzone[i].zname[0]; i++)
390 +                fprintf(stderr, " %s", tzone[i].zname);
391 +        putc('\n', stderr);
392 +        exit(1);
393   }
394  
395  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines