1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Program to convert lamp color from table and compute radiance. |
9 |
*/ |
10 |
|
11 |
#include <stdio.h> |
12 |
|
13 |
#define PI 3.14159265358979323846 |
14 |
|
15 |
extern char *gets(), *strcpy(); |
16 |
extern double atof(); |
17 |
extern float *matchlamp(); |
18 |
|
19 |
/* lamp parameters */ |
20 |
#define LTYPE 0 |
21 |
#define LUNIT 1 |
22 |
#define LGEOM 2 |
23 |
#define LOUTP 3 |
24 |
#define NPARAMS 4 |
25 |
|
26 |
int typecheck(), unitcheck(), geomcheck(), outpcheck(); |
27 |
|
28 |
float *lampcolor; /* the lamp color (RGB) */ |
29 |
double unit2meter; /* conversion from units to meters */ |
30 |
double projarea; /* projected area for this geometry */ |
31 |
double lumens; /* total lamp lumens */ |
32 |
|
33 |
struct { |
34 |
char *name; |
35 |
char value[64]; |
36 |
int (*check)(); |
37 |
char *help; |
38 |
} param[NPARAMS] = { |
39 |
{ "lamp type", "white", typecheck, |
40 |
"The lamp type is a string which corresponds to one of the types registered\n\ |
41 |
in the lamp table file. A value of \"white\" means an uncolored source,\n\ |
42 |
which may be preferable because it results in a color balanced image." }, |
43 |
{ "length unit", "meter", unitcheck, |
44 |
"Unit must be one of: \"meter\", \"centimeter\", \"foot\", or \"inch\".\n\ |
45 |
These may be abbreviated as a single letter." }, |
46 |
{ "lamp geometry", "polygon", geomcheck, |
47 |
"The lamp geometry must be one of: \"polygon\", \"sphere\", \"cylinder\"\n\ |
48 |
or \"ring\". These may be abbreviated as a single letter." }, |
49 |
{ "total lamp lumens", "0", outpcheck, |
50 |
"This is the overall light output of the lamp and its fixture. If you do\n\ |
51 |
not know this value explicitly, you can compute the approximate lumens\n\ |
52 |
by multiplying the input wattage by 15 for incandescent fixtures or 40\n\ |
53 |
for fluorescent fixtures." }, |
54 |
}; |
55 |
|
56 |
|
57 |
main(argc, argv) |
58 |
int argc; |
59 |
char *argv[]; |
60 |
{ |
61 |
char *lamptab = "lamp.tab"; |
62 |
char buf[64]; |
63 |
int i; |
64 |
|
65 |
if (argc > 1) lamptab = argv[1]; |
66 |
if (loadlamps(lamptab) == 0) { |
67 |
fprintf(stderr, "%s: no such lamp table\n", lamptab); |
68 |
exit(1); |
69 |
} |
70 |
printf("Program to compute lamp radiance. Enter '?' for help.\n"); |
71 |
for ( ; ; ) { |
72 |
i = 0; |
73 |
while (i < NPARAMS) { |
74 |
printf("Enter %s [%s]: ", param[i].name, |
75 |
param[i].value); |
76 |
if (gets(buf) == NULL) |
77 |
exit(0); |
78 |
if (buf[0] == '?') { |
79 |
puts(param[i].help); |
80 |
continue; |
81 |
} |
82 |
if (buf[0]) |
83 |
strcpy(param[i].value, buf); |
84 |
if (!(*param[i].check)(param[i].value)) { |
85 |
fprintf(stderr, "%s: bad value for %s\n", |
86 |
param[i].value, param[i].name); |
87 |
continue; |
88 |
} |
89 |
i++; |
90 |
} |
91 |
compute(); |
92 |
} |
93 |
} |
94 |
|
95 |
|
96 |
typecheck(s) /* check lamp type */ |
97 |
char *s; |
98 |
{ |
99 |
lampcolor = matchlamp(s); |
100 |
return(lampcolor != NULL); |
101 |
} |
102 |
|
103 |
|
104 |
unitcheck(s) /* compute conversion to meters */ |
105 |
char *s; |
106 |
{ |
107 |
int len = strlen(s); |
108 |
|
109 |
switch (*s) { |
110 |
case 'm': |
111 |
if (strncmp(s, "meters", len)) |
112 |
return(0); |
113 |
unit2meter = 1.0; |
114 |
return(1); |
115 |
case 'c': |
116 |
if (strncmp(s, "centimeters", len) && strncmp(s, "cms", len)) |
117 |
return(0); |
118 |
unit2meter = 0.01; |
119 |
return(1); |
120 |
case 'f': |
121 |
if (strncmp(s, "foot", len) && strncmp(s, "feet", len)) |
122 |
return(0); |
123 |
unit2meter = 0.3048; |
124 |
return(1); |
125 |
case 'i': |
126 |
if (strncmp(s, "inches", len)) |
127 |
return(0); |
128 |
unit2meter = 0.0254; |
129 |
return(1); |
130 |
} |
131 |
return(0); |
132 |
} |
133 |
|
134 |
|
135 |
geomcheck(s) /* check/set lamp geometry */ |
136 |
char *s; |
137 |
{ |
138 |
int len = strlen(s); |
139 |
|
140 |
switch (*s) { |
141 |
case 'p': |
142 |
if (strncmp(s, "polygonal", len)) |
143 |
return(0); |
144 |
return(getpolygon()); |
145 |
case 's': |
146 |
if (strncmp(s, "sphere", len) && strncmp(s, "spherical", len)) |
147 |
return(0); |
148 |
return(getsphere()); |
149 |
case 'c': |
150 |
if (strncmp(s,"cylinder",len) && strncmp(s,"cylindrical",len)) |
151 |
return(0); |
152 |
return(getcylinder()); |
153 |
case 'r': |
154 |
if (strncmp(s, "ring", len) && strncmp(s, "disk", len)) |
155 |
return(0); |
156 |
return(getring()); |
157 |
} |
158 |
return(0); |
159 |
} |
160 |
|
161 |
|
162 |
outpcheck(s) /* check lumen output value */ |
163 |
register char *s; |
164 |
{ |
165 |
if ((*s < '0' || *s > '9') && *s != '.') |
166 |
return(0); |
167 |
lumens = atof(s); |
168 |
return(1); |
169 |
} |
170 |
|
171 |
|
172 |
compute() /* compute lamp radiance */ |
173 |
{ |
174 |
double whiteval; |
175 |
|
176 |
whiteval = lumens/470./projarea; |
177 |
|
178 |
printf("Lamp color (RGB) = %f %f %f\n", |
179 |
lampcolor[0]*whiteval, |
180 |
lampcolor[1]*whiteval, |
181 |
lampcolor[2]*whiteval); |
182 |
} |
183 |
|
184 |
|
185 |
getd(dp) /* get a positive double from stdin */ |
186 |
double *dp; |
187 |
{ |
188 |
char buf[32]; |
189 |
|
190 |
if (gets(buf) == NULL) |
191 |
return(0); |
192 |
if ((buf[0] < '0' || buf[0] > '9') && buf[0] != '.') |
193 |
return(0); |
194 |
*dp = atof(buf); |
195 |
return(1); |
196 |
} |
197 |
|
198 |
|
199 |
getpolygon() /* get projected area for a polygon */ |
200 |
{ |
201 |
printf("Enter area of polygon: "); |
202 |
if (!getd(&projarea)) |
203 |
return(0); |
204 |
projarea *= unit2meter*unit2meter; |
205 |
projarea *= PI; |
206 |
return(1); |
207 |
} |
208 |
|
209 |
|
210 |
getsphere() /* get projected area for a sphere */ |
211 |
{ |
212 |
double radius; |
213 |
|
214 |
printf("Enter sphere radius: "); |
215 |
if (!getd(&radius)) |
216 |
return(0); |
217 |
radius *= unit2meter; |
218 |
projarea = 4.*PI*PI*radius*radius; |
219 |
return(1); |
220 |
} |
221 |
|
222 |
|
223 |
getcylinder() /* get projected area for a cylinder */ |
224 |
{ |
225 |
double length, radius; |
226 |
|
227 |
printf("Enter cylinder length: "); |
228 |
if (!getd(&length)) |
229 |
return(0); |
230 |
length *= unit2meter; |
231 |
printf("Enter cylinder radius: "); |
232 |
if (!getd(&radius)) |
233 |
return(0); |
234 |
radius *= unit2meter; |
235 |
projarea = PI*PI*2.*PI*radius*length; |
236 |
return(1); |
237 |
} |
238 |
|
239 |
|
240 |
getring() /* get projected area for a ring */ |
241 |
{ |
242 |
double radius; |
243 |
|
244 |
printf("Enter disk radius: "); |
245 |
if (!getd(&radius)) |
246 |
return(0); |
247 |
radius *= unit2meter; |
248 |
projarea = PI*PI*radius*radius; |
249 |
return(1); |
250 |
} |