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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines