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.11 by greg, Mon Jun 7 14:51:22 1993 UTC vs.
Revision 2.29 by greg, Thu Mar 30 20:19:05 2023 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  gensky.c - program to generate sky functions.
6   *              Our zenith is along the Z-axis, the X-axis
# Line 13 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10   *     3/26/86
11   */
12  
13 < #include  <stdio.h>
14 <
13 > #include  "rtio.h"
14 > #include  <stdlib.h>
15   #include  <math.h>
16 <
16 > #include  <ctype.h>
17 > #include  "sun.h"
18   #include  "color.h"
19  
20 < extern char  *strcpy(), *strcat(), *malloc();
21 < extern double  stadj(), sdec(), sazi(), salt();
20 > #ifndef  PI
21 > #define  PI             3.14159265358979323846
22 > #endif
23  
25 #define  PI             3.141592654
26
24   #define  DOT(v1,v2)     (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
25  
26 + #define  S_CLEAR        1
27 + #define  S_OVER         2
28 + #define  S_UNIF         3
29 + #define  S_INTER        4
30 +
31 + #define  overcast       ((skytype==S_OVER)|(skytype==S_UNIF))
32 +
33   double  normsc();
34 <                                        /* sun calculation constants */
35 < extern double  s_latitude;
36 < extern double  s_longitude;
37 < extern double  s_meridian;
34 >
35 > #undef  toupper
36 > #define  toupper(c)     ((c) & ~0x20)   /* ASCII trick to convert case */
37 >
38 >                                        /* European and North American zones */
39 > struct {
40 >        char    zname[8];       /* time zone name (all caps) */
41 >        float   zmer;           /* standard meridian */
42 > } tzone[] = {
43 >        {"YST", 135}, {"YDT", 120},
44 >        {"PST", 120}, {"PDT", 105},
45 >        {"MST", 105}, {"MDT", 90},
46 >        {"CST", 90}, {"CDT", 75},
47 >        {"EST", 75}, {"EDT", 60},
48 >        {"AST", 60}, {"ADT", 45},
49 >        {"NST", 52.5}, {"NDT", 37.5},
50 >        {"GMT", 0}, {"BST", -15},
51 >        {"CET", -15}, {"CEST", -30},
52 >        {"EET", -30}, {"EEST", -45},
53 >        {"AST", -45}, {"ADT", -60},
54 >        {"GST", -60}, {"GDT", -75},
55 >        {"IST", -82.5}, {"IDT", -97.5},
56 >        {"JST", -135}, {"NDT", -150},
57 >        {"NZST", -180}, {"NZDT", -195},
58 >        {"", 0}
59 > };
60                                          /* required values */
61 + int  year = 0;                                  /* year (optional) */
62   int  month, day;                                /* date */
63   double  hour;                                   /* time */
64   int  tsolar;                                    /* 0=standard, 1=solar */
65   double  altitude, azimuth;                      /* or solar angles */
66                                          /* default values */
67 < int  cloudy = 0;                                /* 1=standard, 2=uniform */
67 > int  skytype = S_CLEAR;                         /* sky type */
68   int  dosun = 1;
69 < double  zenithbr = -1.0;
70 < double  turbidity = 2.75;
69 > double  zenithbr = 0.0;
70 > int     u_zenith = 0;                           /* -1=irradiance, 1=radiance */
71 > double  turbidity = 2.45;
72   double  gprefl = 0.2;
73                                          /* computed values */
74   double  sundir[3];
75   double  groundbr;
76   double  F2;
77 < double  solarbr = -1.0;
77 > double  solarbr = 0.0;
78 > int     u_solar = 0;                            /* -1=irradiance, 1=radiance */
79  
80   char  *progname;
81   char  errmsg[128];
82  
83 + void computesky(void);
84 + void printsky(void);
85 + void printdefaults(void);
86 + void userror(char  *msg);
87 + double normsc(void);
88 + int cvthour(char  *hs);
89  
90 < main(argc, argv)
91 < int  argc;
92 < char  *argv[];
90 >
91 > int
92 > main(
93 >        int  argc,
94 >        char  *argv[]
95 > )
96   {
97 +        int  got_meridian = 0;
98          int  i;
99  
100          progname = argv[0];
# Line 76 | Line 115 | char  *argv[];
115                  day = atoi(argv[2]);
116                  if (day < 1 || day > 31)
117                          userror("bad day");
118 <                hour = atof(argv[3]);
80 <                if (hour < 0 || hour >= 24)
81 <                        userror("bad hour");
82 <                tsolar = argv[3][0] == '+';
118 >                got_meridian = cvthour(argv[3]);
119          }
120          for (i = 4; i < argc; i++)
121                  if (argv[i][0] == '-' || argv[i][0] == '+')
122                          switch (argv[i][1]) {
123                          case 's':
124 <                                cloudy = 0;
124 >                                skytype = S_CLEAR;
125                                  dosun = argv[i][0] == '+';
126                                  break;
127 +                        case 'y':
128 +                                year = atoi(argv[++i]);
129 +                                break;
130                          case 'r':
131 +                        case 'R':
132 +                                u_solar = argv[i][1]=='R' ? -1 : 1;
133                                  solarbr = atof(argv[++i]);
134                                  break;
135                          case 'c':
136 <                                cloudy = argv[i][0] == '+' ? 2 : 1;
96 <                                dosun = 0;
136 >                                skytype = S_OVER;
137                                  break;
138 +                        case 'u':
139 +                                skytype = S_UNIF;
140 +                                break;
141 +                        case 'i':
142 +                                skytype = S_INTER;
143 +                                dosun = argv[i][0] == '+';
144 +                                break;
145                          case 't':
146                                  turbidity = atof(argv[++i]);
147                                  break;
148                          case 'b':
149 +                        case 'B':
150 +                                u_zenith = argv[i][1]=='B' ? -1 : 1;
151                                  zenithbr = atof(argv[++i]);
152                                  break;
153                          case 'g':
# Line 111 | Line 160 | char  *argv[];
160                                  s_longitude = atof(argv[++i]) * (PI/180);
161                                  break;
162                          case 'm':
163 +                                if (got_meridian) {
164 +                                        ++i;
165 +                                        break;          /* time overrides */
166 +                                }
167                                  s_meridian = atof(argv[++i]) * (PI/180);
168                                  break;
169                          default:
# Line 120 | Line 173 | char  *argv[];
173                  else
174                          userror("bad option");
175  
176 <        if (fabs(s_meridian-s_longitude) > 30*PI/180)
176 >        if (year && (year < 1950) | (year > 2050))
177                  fprintf(stderr,
178 <        "%s: warning: %.1f hours btwn. standard meridian and longitude\n",
178 >                        "%s: warning - year should be in range 1950-2050\n",
179 >                        progname);
180 >        if (month && !tsolar && fabs(s_meridian-s_longitude) > 45*PI/180)
181 >                fprintf(stderr,
182 >        "%s: warning - %.1f hours btwn. standard meridian and longitude\n",
183                          progname, (s_longitude-s_meridian)*12/PI);
184  
185 <        printhead(argc, argv);
185 >        fputs("# ", stdout);
186 >        printargs(argc, argv, stdout);
187  
188          computesky();
189          printsky();
190 +
191 +        exit(0);
192   }
193  
194  
195 < computesky()                    /* compute sky parameters */
195 > void
196 > computesky(void)                        /* compute sky parameters */
197   {
198 +        double  normfactor;
199                                          /* compute solar direction */
200          if (month) {                    /* from date and time */
201 <                int  jd;
140 <                double  sd, st;
201 >                double  sd, st = hour;
202  
203 <                jd = jdate(month, day);         /* Julian date */
204 <                sd = sdec(jd);                  /* solar declination */
205 <                if (tsolar)                     /* solar time */
206 <                        st = hour;
207 <                else
208 <                        st = hour + stadj(jd);
203 >                if (year) {                     /* Michalsky algorithm? */
204 >                        double  mjd = mjdate(year, month, day, hour);
205 >                        if (tsolar)
206 >                                sd = msdec(mjd, NULL);
207 >                        else
208 >                                sd = msdec(mjd, &st);
209 >                } else {
210 >                        int  jd = jdate(month, day);    /* Julian date */
211 >                        sd = sdec(jd);                  /* solar declination */
212 >                        if (!tsolar)                    /* get solar time? */
213 >                                st = hour + stadj(jd);
214 >                }
215                  altitude = salt(sd, st);
216                  azimuth = sazi(sd, st);
217 <                printf("# Solar altitude and azimuth: %f %f\n",
217 >                printf("# Local solar time: %.2f\n", st);
218 >                printf("# Solar altitude and azimuth: %.1f %.1f\n",
219                                  180./PI*altitude, 180./PI*azimuth);
220          }
221 <        if (!cloudy && altitude > 87.*PI/180.) {
221 >        if (!overcast && altitude > 87.*PI/180.) {
222                  fprintf(stderr,
223   "%s: warning - sun too close to zenith, reducing altitude to 87 degrees\n",
224                                  progname);
# Line 162 | Line 230 | computesky()                   /* compute sky parameters */
230          sundir[1] = -cos(azimuth)*cos(altitude);
231          sundir[2] = sin(altitude);
232  
233 +                                        /* Compute normalization factor */
234 +        switch (skytype) {
235 +        case S_UNIF:
236 +                normfactor = 1.0;
237 +                break;
238 +        case S_OVER:
239 +                normfactor = 0.777778;
240 +                break;
241 +        case S_CLEAR:
242 +                F2 = 0.274*(0.91 + 10.0*exp(-3.0*(PI/2.0-altitude)) +
243 +                                0.45*sundir[2]*sundir[2]);
244 +                normfactor = normsc()/F2/PI;
245 +                break;
246 +        case S_INTER:
247 +                F2 = (2.739 + .9891*sin(.3119+2.6*altitude)) *
248 +                        exp(-(PI/2.0-altitude)*(.4441+1.48*altitude));
249 +                normfactor = normsc()/F2/PI;
250 +                break;
251 +        }
252                                          /* Compute zenith brightness */
253 <        if (zenithbr <= 0.0)
254 <                if (cloudy) {
253 >        if (u_zenith == -1)
254 >                zenithbr /= normfactor*PI;
255 >        else if (u_zenith == 0) {
256 >                if (overcast)
257                          zenithbr = 8.6*sundir[2] + .123;
258 <                        zenithbr *= 1000.0/WHTEFFICACY;
170 <                } else {
258 >                else
259                          zenithbr = (1.376*turbidity-1.81)*tan(altitude)+0.38;
260 <                        zenithbr *= 1000.0/SKYEFFICACY;
261 <                }
262 <        if (zenithbr < 0.0)
263 <                zenithbr = 0.0;
176 <                                        /* Compute horizontal radiance */
177 <        if (cloudy) {
178 <                if (cloudy == 2)
179 <                        groundbr = zenithbr;
260 >                if (skytype == S_INTER)
261 >                        zenithbr = (zenithbr + 8.6*sundir[2] + .123)/2.0;
262 >                if (zenithbr < 0.0)
263 >                        zenithbr = 0.0;
264                  else
265 <                        groundbr = zenithbr*0.777778;
182 <                printf("# Ground ambient level: %f\n", groundbr);
183 <        } else {
184 <                F2 = 0.274*(0.91 + 10.0*exp(-3.0*(PI/2.0-altitude)) +
185 <                                0.45*sundir[2]*sundir[2]);
186 <                groundbr = zenithbr*normsc(altitude)/F2/PI;
187 <                printf("# Ground ambient level: %f\n", groundbr);
188 <                if (sundir[2] > 0.0 && solarbr != 0.0) {
189 <                        if (solarbr < 0.0)
190 <                                solarbr = 1.5e9/SUNEFFICACY *
191 <                                (1.147 - .147/(sundir[2]>.16?sundir[2]:.16));
192 <                        groundbr += solarbr*6e-5*sundir[2]/PI;
193 <                } else
194 <                        dosun = 0;
265 >                        zenithbr *= 1000.0/SKYEFFICACY;
266          }
267 +                                        /* Compute horizontal radiance */
268 +        groundbr = zenithbr*normfactor;
269 +        printf("# Ground ambient level: %.1f\n", groundbr);
270 +        if (!overcast && sundir[2] > 0.0 && (!u_solar || solarbr > 0.0)) {
271 +                if (u_solar == -1)
272 +                        solarbr /= 6e-5*sundir[2];
273 +                else if (u_solar == 0) {
274 +                        solarbr = 1.5e9/SUNEFFICACY *
275 +                        (1.147 - .147/(sundir[2]>.16?sundir[2]:.16));
276 +                        if (skytype == S_INTER)
277 +                                solarbr *= 0.15;        /* fudge factor! */
278 +                }
279 +                groundbr += 6e-5/PI*solarbr*sundir[2];
280 +        } else
281 +                dosun = 0;
282          groundbr *= gprefl;
283   }
284  
285  
286 < printsky()                      /* print out sky */
286 > void
287 > printsky(void)                  /* print out sky */
288   {
289          if (dosun) {
290                  printf("\nvoid light solar\n");
291                  printf("0\n0\n");
292 <                printf("3 %.2e %.2e %.2e\n", solarbr, solarbr, solarbr);
292 >                printf("3 %.3e %.3e %.3e\n", solarbr, solarbr, solarbr);
293                  printf("\nsolar source sun\n");
294                  printf("0\n0\n");
295                  printf("4 %f %f %f 0.5\n", sundir[0], sundir[1], sundir[2]);
296          }
297          
298          printf("\nvoid brightfunc skyfunc\n");
299 <        printf("2 skybright skybright.cal\n");
299 >        printf("2 skybr skybright.cal\n");
300          printf("0\n");
301 <        if (cloudy)
302 <                printf("3 %d %.2e %.2e\n", cloudy, zenithbr, groundbr);
301 >        if (overcast)
302 >                printf("3 %d %.3e %.3e\n", skytype, zenithbr, groundbr);
303          else
304 <                printf("7 -1 %.2e %.2e %.2e %f %f %f\n", zenithbr, groundbr,
305 <                                F2, sundir[0], sundir[1], sundir[2]);
304 >                printf("7 %d %.3e %.3e %.3e %f %f %f\n",
305 >                                skytype, zenithbr, groundbr, F2,
306 >                                sundir[0], sundir[1], sundir[2]);
307   }
308  
309  
310 < printdefaults()                 /* print default values */
310 > void
311 > printdefaults(void)                     /* print default values */
312   {
313 <        if (cloudy == 1)
313 >        switch (skytype) {
314 >        case S_OVER:
315                  printf("-c\t\t\t\t# Cloudy sky\n");
316 <        else if (cloudy == 2)
317 <                printf("+c\t\t\t\t# Uniform cloudy sky\n");
318 <        else if (dosun)
319 <                printf("+s\t\t\t\t# Sunny sky with sun\n");
320 <        else
321 <                printf("-s\t\t\t\t# Sunny sky without sun\n");
316 >                break;
317 >        case S_UNIF:
318 >                printf("-u\t\t\t\t# Uniform cloudy sky\n");
319 >                break;
320 >        case S_INTER:
321 >                if (dosun)
322 >                        printf("+i\t\t\t\t# Intermediate sky with sun\n");
323 >                else
324 >                        printf("-i\t\t\t\t# Intermediate sky without sun\n");
325 >                break;
326 >        case S_CLEAR:
327 >                if (dosun)
328 >                        printf("+s\t\t\t\t# Sunny sky with sun\n");
329 >                else
330 >                        printf("-s\t\t\t\t# Sunny sky without sun\n");
331 >                break;
332 >        }
333          printf("-g %f\t\t\t# Ground plane reflectance\n", gprefl);
334          if (zenithbr > 0.0)
335                  printf("-b %f\t\t\t# Zenith radiance (watts/ster/m2\n", zenithbr);
# Line 240 | Line 341 | printdefaults()                        /* print default values */
341   }
342  
343  
344 < userror(msg)                    /* print usage error and quit */
345 < char  *msg;
344 > void
345 > userror(                        /* print usage error and quit */
346 >        char  *msg
347 > )
348   {
349          if (msg != NULL)
350                  fprintf(stderr, "%s: Use error - %s\n", progname, msg);
# Line 253 | Line 356 | char  *msg;
356  
357  
358   double
359 < normsc(theta)                   /* compute normalization factor (E0*F2/L0) */
257 < double  theta;
359 > normsc(void)                    /* compute normalization factor (E0*F2/L0) */
360   {
361 <        static double  nf[5] = {2.766521, 0.547665,
362 <                                -0.369832, 0.009237, 0.059229};
361 >        static double  nfc[2][5] = {
362 >                                /* clear sky approx. */
363 >                {2.766521, 0.547665, -0.369832, 0.009237, 0.059229},
364 >                                /* intermediate sky approx. */
365 >                {3.5556, -2.7152, -1.3081, 1.0660, 0.60227},
366 >        };
367 >        double  *nf;
368          double  x, nsc;
369 <        register int  i;
369 >        int  i;
370                                          /* polynomial approximation */
371 <        x = (theta - PI/4.0)/(PI/4.0);
372 <        nsc = nf[4];
373 <        for (i = 3; i >= 0; i--)
371 >        nf = nfc[skytype==S_INTER];
372 >        x = (altitude - PI/4.0)/(PI/4.0);
373 >        nsc = nf[i=4];
374 >        while (i--)
375                  nsc = nsc*x + nf[i];
376  
377          return(nsc);
378   }
379  
380  
381 < printhead(ac, av)               /* print command header */
382 < register int  ac;
383 < register char  **av;
381 > int
382 > cvthour(                        /* convert hour string */
383 >        char  *hs
384 > )
385   {
386 <        putchar('#');
387 <        while (ac--) {
388 <                putchar(' ');
389 <                fputs(*av++, stdout);
386 >        char  *cp = hs;
387 >        int     i, j;
388 >
389 >        if ( (tsolar = *cp == '+') ) cp++;              /* solar time? */
390 >        while (isdigit(*cp)) cp++;
391 >        if (*cp == ':')
392 >                hour = atoi(hs) + atoi(++cp)/60.0;
393 >        else {
394 >                hour = atof(hs);
395 >                if (*cp == '.') cp++;
396          }
397 <        putchar('\n');
397 >        while (isdigit(*cp)) cp++;
398 >        if (!*cp)
399 >                return(0);
400 >        if (tsolar || !isalpha(*cp)) {
401 >                fprintf(stderr, "%s: bad time format: %s\n", progname, hs);
402 >                exit(1);
403 >        }
404 >        i = 0;
405 >        do {
406 >                for (j = 0; cp[j]; j++)
407 >                        if (toupper(cp[j]) != tzone[i].zname[j])
408 >                                break;
409 >                if (!cp[j] && !tzone[i].zname[j]) {
410 >                        s_meridian = tzone[i].zmer * (PI/180);
411 >                        return(1);
412 >                }
413 >        } while (tzone[i++].zname[0]);
414 >
415 >        fprintf(stderr, "%s: unknown time zone: %s\n", progname, cp);
416 >        fprintf(stderr, "Known time zones:\n\t%s", tzone[0].zname);
417 >        for (i = 1; tzone[i].zname[0]; i++)
418 >                fprintf(stderr, " %s", tzone[i].zname);
419 >        putc('\n', stderr);
420 >        exit(1);
421   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines