ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/gensky.c
Revision: 2.9
Committed: Wed Dec 9 08:32:52 1992 UTC (31 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +9 -1 lines
Log Message:
Added protection against sun too close to zenith

File Contents

# User Rev Content
1 greg 2.9 /* Copyright (c) 1992 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * gensky.c - program to generate sky functions.
9     * Our zenith is along the Z-axis, the X-axis
10     * points east, and the Y-axis points north.
11     * Radiance is in watts/steradian/sq. meter.
12     *
13     * 3/26/86
14     */
15    
16     #include <stdio.h>
17    
18     #include <math.h>
19    
20 greg 1.6 #include "color.h"
21 greg 1.1
22 greg 2.2 #ifndef atof
23     extern double atof();
24     #endif
25 greg 1.1 extern char *strcpy(), *strcat(), *malloc();
26     extern double stadj(), sdec(), sazi(), salt();
27    
28     #define PI 3.141592654
29    
30     #define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
31    
32     double normsc();
33     /* sun calculation constants */
34     extern double s_latitude;
35     extern double s_longitude;
36     extern double s_meridian;
37     /* required values */
38 greg 2.3 int month, day; /* date */
39 greg 2.5 double hour; /* time */
40     int tsolar; /* 0=standard, 1=solar */
41 greg 2.3 double altitude, azimuth; /* or solar angles */
42 greg 1.1 /* default values */
43 greg 2.4 int cloudy = 0; /* 1=standard, 2=uniform */
44 greg 1.1 int dosun = 1;
45     double zenithbr = -1.0;
46     double turbidity = 2.75;
47     double gprefl = 0.2;
48     /* computed values */
49     double sundir[3];
50     double groundbr;
51     double F2;
52 greg 2.8 double solarbr = -1.0;
53 greg 1.1
54     char *progname;
55     char errmsg[128];
56    
57    
58     main(argc, argv)
59     int argc;
60     char *argv[];
61     {
62     int i;
63    
64     progname = argv[0];
65     if (argc == 2 && !strcmp(argv[1], "-defaults")) {
66     printdefaults();
67     exit(0);
68     }
69     if (argc < 4)
70     userror("arg count");
71 greg 2.3 if (!strcmp(argv[1], "-ang")) {
72     altitude = atof(argv[2]) * (PI/180);
73     azimuth = atof(argv[3]) * (PI/180);
74     month = 0;
75     } else {
76     month = atoi(argv[1]);
77 greg 2.6 if (month < 1 || month > 12)
78     userror("bad month");
79 greg 2.3 day = atoi(argv[2]);
80 greg 2.6 if (day < 1 || day > 31)
81     userror("bad day");
82 greg 2.3 hour = atof(argv[3]);
83 greg 2.6 if (hour < 0 || hour >= 24)
84     userror("bad hour");
85 greg 2.5 tsolar = argv[3][0] == '+';
86 greg 2.3 }
87 greg 1.1 for (i = 4; i < argc; i++)
88     if (argv[i][0] == '-' || argv[i][0] == '+')
89     switch (argv[i][1]) {
90     case 's':
91     cloudy = 0;
92     dosun = argv[i][0] == '+';
93     break;
94 greg 2.8 case 'r':
95     solarbr = atof(argv[++i]);
96     break;
97 greg 1.1 case 'c':
98 greg 2.4 cloudy = argv[i][0] == '+' ? 2 : 1;
99 greg 1.1 dosun = 0;
100     break;
101     case 't':
102     turbidity = atof(argv[++i]);
103     break;
104     case 'b':
105     zenithbr = atof(argv[++i]);
106     break;
107     case 'g':
108     gprefl = atof(argv[++i]);
109     break;
110     case 'a':
111     s_latitude = atof(argv[++i]) * (PI/180);
112     break;
113     case 'o':
114     s_longitude = atof(argv[++i]) * (PI/180);
115     break;
116     case 'm':
117     s_meridian = atof(argv[++i]) * (PI/180);
118     break;
119     default:
120     sprintf(errmsg, "unknown option: %s", argv[i]);
121     userror(errmsg);
122     }
123     else
124     userror("bad option");
125 greg 1.5
126     if (fabs(s_meridian-s_longitude) > 30*PI/180)
127     fprintf(stderr,
128     "%s: warning: %.1f hours btwn. standard meridian and longitude\n",
129     progname, (s_longitude-s_meridian)*12/PI);
130 greg 1.1
131     printhead(argc, argv);
132    
133     computesky();
134     printsky();
135     }
136    
137    
138     computesky() /* compute sky parameters */
139     {
140     /* compute solar direction */
141 greg 2.3 if (month) { /* from date and time */
142     int jd;
143     double sd, st;
144    
145     jd = jdate(month, day); /* Julian date */
146     sd = sdec(jd); /* solar declination */
147 greg 2.5 if (tsolar) /* solar time */
148     st = hour;
149     else
150     st = hour + stadj(jd);
151 greg 2.3 altitude = salt(sd, st);
152     azimuth = sazi(sd, st);
153 greg 2.9 }
154     if (!cloudy && altitude > 87.*PI/180.) {
155     fprintf(stderr,
156     "%s: warning - sun too close to zenith, reducing altitude to 87 degrees\n",
157     progname);
158     printf(
159     "# warning - sun too close to zenith, reducing altitude to 87 degrees\n");
160     altitude = 87.*PI/180.;
161 greg 2.3 }
162 greg 1.1 sundir[0] = -sin(azimuth)*cos(altitude);
163     sundir[1] = -cos(azimuth)*cos(altitude);
164     sundir[2] = sin(altitude);
165    
166     /* Compute zenith brightness */
167     if (zenithbr <= 0.0)
168     if (cloudy) {
169     zenithbr = 8.6*sundir[2] + .123;
170 greg 2.6 zenithbr *= 1000.0/WHTEFFICACY;
171 greg 1.1 } else {
172     zenithbr = (1.376*turbidity-1.81)*tan(altitude)+0.38;
173 greg 1.6 zenithbr *= 1000.0/SKYEFFICACY;
174 greg 1.1 }
175 greg 1.2 if (zenithbr < 0.0)
176     zenithbr = 0.0;
177 greg 1.1 /* Compute horizontal radiance */
178     if (cloudy) {
179 greg 2.4 if (cloudy == 2)
180     groundbr = zenithbr;
181     else
182     groundbr = zenithbr*0.777778;
183 greg 1.1 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 greg 2.6 groundbr = zenithbr*normsc(altitude)/F2/PI;
188 greg 1.1 printf("# Ground ambient level: %f\n", groundbr);
189 greg 2.8 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 greg 1.1 groundbr += solarbr*6e-5*sundir[2]/PI;
194     } else
195     dosun = 0;
196     }
197     groundbr *= gprefl;
198     }
199    
200    
201     printsky() /* print out sky */
202     {
203     if (dosun) {
204     printf("\nvoid light solar\n");
205     printf("0\n0\n");
206 greg 1.6 printf("3 %.2e %.2e %.2e\n", solarbr, solarbr, solarbr);
207 greg 1.1 printf("\nsolar source sun\n");
208     printf("0\n0\n");
209     printf("4 %f %f %f 0.5\n", sundir[0], sundir[1], sundir[2]);
210     }
211    
212     printf("\nvoid brightfunc skyfunc\n");
213     printf("2 skybright skybright.cal\n");
214     printf("0\n");
215     if (cloudy)
216 greg 2.4 printf("3 %d %.2e %.2e\n", cloudy, zenithbr, groundbr);
217 greg 1.1 else
218 greg 1.6 printf("7 -1 %.2e %.2e %.2e %f %f %f\n", zenithbr, groundbr,
219     F2, sundir[0], sundir[1], sundir[2]);
220 greg 1.1 }
221    
222    
223     printdefaults() /* print default values */
224     {
225 greg 2.4 if (cloudy == 1)
226 greg 1.1 printf("-c\t\t\t\t# Cloudy sky\n");
227 greg 2.4 else if (cloudy == 2)
228     printf("+c\t\t\t\t# Uniform cloudy sky\n");
229 greg 1.1 else if (dosun)
230     printf("+s\t\t\t\t# Sunny sky with sun\n");
231     else
232     printf("-s\t\t\t\t# Sunny sky without sun\n");
233     printf("-g %f\t\t\t# Ground plane reflectance\n", gprefl);
234     if (zenithbr > 0.0)
235     printf("-b %f\t\t\t# Zenith radiance (watts/ster/m2\n", zenithbr);
236     else
237     printf("-t %f\t\t\t# Atmospheric turbidity\n", turbidity);
238     printf("-a %f\t\t\t# Site latitude (degrees)\n", s_latitude*(180/PI));
239     printf("-o %f\t\t\t# Site longitude (degrees)\n", s_longitude*(180/PI));
240     printf("-m %f\t\t\t# Standard meridian (degrees)\n", s_meridian*(180/PI));
241     }
242    
243    
244     userror(msg) /* print usage error and quit */
245     char *msg;
246     {
247     if (msg != NULL)
248     fprintf(stderr, "%s: Use error - %s\n", progname, msg);
249     fprintf(stderr, "Usage: %s month day hour [options]\n", progname);
250 greg 2.3 fprintf(stderr, " Or: %s -ang altitude azimuth [options]\n", progname);
251 greg 1.1 fprintf(stderr, " Or: %s -defaults\n", progname);
252     exit(1);
253     }
254    
255    
256     double
257     normsc(theta) /* compute normalization factor (E0*F2/L0) */
258     double theta;
259     {
260     static double nf[5] = {2.766521, 0.547665,
261     -0.369832, 0.009237, 0.059229};
262     double x, nsc;
263     register int i;
264     /* polynomial approximation */
265     x = (theta - PI/4.0)/(PI/4.0);
266     nsc = nf[4];
267     for (i = 3; i >= 0; i--)
268     nsc = nsc*x + nf[i];
269    
270     return(nsc);
271     }
272    
273    
274     printhead(ac, av) /* print command header */
275     register int ac;
276     register char **av;
277     {
278     putchar('#');
279     while (ac--) {
280     putchar(' ');
281     fputs(*av++, stdout);
282     }
283     putchar('\n');
284     }