5 |
|
#endif |
6 |
|
|
7 |
|
/* |
8 |
< |
* Do dah program to convert lamp color from table. |
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 14 for incandescent fixtures or 70\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 |
< |
float *lcol; |
63 |
< |
char buf[80]; |
62 |
> |
char buf[64]; |
63 |
> |
int i; |
64 |
|
|
65 |
|
if (argc > 1) lamptab = argv[1]; |
66 |
|
if (loadlamps(lamptab) == 0) { |
67 |
< |
perror(lamptab); |
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 |
< |
printf("Enter lamp type: "); |
73 |
< |
buf[0] = '\0'; |
74 |
< |
gets(buf); |
75 |
< |
if (!buf[0]) exit(0); |
76 |
< |
if ((lcol = matchlamp(buf)) == NULL) |
77 |
< |
printf("No match in table\n"); |
78 |
< |
else |
79 |
< |
printf("RGB = (%f,%f,%f)\n", lcol[0], lcol[1], lcol[2]); |
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(name, dp, help) /* get a positive double from stdin */ |
186 |
+ |
char *name; |
187 |
+ |
double *dp; |
188 |
+ |
char *help; |
189 |
+ |
{ |
190 |
+ |
char buf[32]; |
191 |
+ |
again: |
192 |
+ |
printf("%s [%g]: ", name, *dp); |
193 |
+ |
if (gets(buf) == NULL) |
194 |
+ |
return(0); |
195 |
+ |
if (buf[0] == '?') { |
196 |
+ |
puts(help); |
197 |
+ |
goto again; |
198 |
+ |
} |
199 |
+ |
if ((buf[0] < '0' || buf[0] > '9') && buf[0] != '.') |
200 |
+ |
return(0); |
201 |
+ |
*dp = atof(buf); |
202 |
+ |
return(1); |
203 |
+ |
} |
204 |
+ |
|
205 |
+ |
|
206 |
+ |
getpolygon() /* get projected area for a polygon */ |
207 |
+ |
{ |
208 |
+ |
static double area = 1.0; |
209 |
+ |
|
210 |
+ |
getd("Polygon area", &area, |
211 |
+ |
"Enter the total radiating area of the polygon."); |
212 |
+ |
projarea = PI*unit2meter*unit2meter * area; |
213 |
+ |
return(1); |
214 |
+ |
} |
215 |
+ |
|
216 |
+ |
|
217 |
+ |
getsphere() /* get projected area for a sphere */ |
218 |
+ |
{ |
219 |
+ |
static double radius = 1.0; |
220 |
+ |
|
221 |
+ |
getd("Sphere radius", &radius, |
222 |
+ |
"Enter the distance from the sphere's center to its surface."); |
223 |
+ |
projarea = 4.*PI*PI*unit2meter*unit2meter * radius*radius; |
224 |
+ |
return(1); |
225 |
+ |
} |
226 |
+ |
|
227 |
+ |
|
228 |
+ |
getcylinder() /* get projected area for a cylinder */ |
229 |
+ |
{ |
230 |
+ |
static double length = 1.0, radius = 0.1; |
231 |
+ |
|
232 |
+ |
getd("Cylinder length", &length, |
233 |
+ |
"Enter the length of the cylinder."); |
234 |
+ |
getd("Cylinder radius", &radius, |
235 |
+ |
"Enter the distance from the cylinder's axis to its surface."); |
236 |
+ |
projarea = PI*PI*2.*PI*unit2meter*unit2meter * radius*length; |
237 |
+ |
return(1); |
238 |
+ |
} |
239 |
+ |
|
240 |
+ |
|
241 |
+ |
getring() /* get projected area for a ring */ |
242 |
+ |
{ |
243 |
+ |
static double radius = 1.0; |
244 |
+ |
|
245 |
+ |
getd("Disk radius", &radius, |
246 |
+ |
"Enter the distance from the ring's center to its outer edge.\n\ |
247 |
+ |
The inner radius must be zero."); |
248 |
+ |
projarea = PI*PI*unit2meter*unit2meter * radius*radius; |
249 |
+ |
return(1); |
250 |
|
} |