1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
2.6 |
static const char RCSid[] = "$Id$"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
greg |
1.2 |
* Program to convert lamp color from table and compute radiance. |
6 |
greg |
1.1 |
*/ |
7 |
|
|
|
8 |
|
|
#include <stdio.h> |
9 |
|
|
|
10 |
greg |
2.4 |
#include <math.h> |
11 |
|
|
|
12 |
greg |
1.8 |
#include "color.h" |
13 |
|
|
|
14 |
greg |
1.2 |
#define PI 3.14159265358979323846 |
15 |
|
|
|
16 |
|
|
extern char *gets(), *strcpy(); |
17 |
greg |
1.1 |
extern float *matchlamp(); |
18 |
|
|
|
19 |
greg |
1.2 |
/* lamp parameters */ |
20 |
|
|
#define LTYPE 0 |
21 |
|
|
#define LUNIT 1 |
22 |
|
|
#define LGEOM 2 |
23 |
|
|
#define LOUTP 3 |
24 |
|
|
#define NPARAMS 4 |
25 |
greg |
1.1 |
|
26 |
greg |
1.2 |
int typecheck(), unitcheck(), geomcheck(), outpcheck(); |
27 |
|
|
|
28 |
|
|
float *lampcolor; /* the lamp color (RGB) */ |
29 |
|
|
double unit2meter; /* conversion from units to meters */ |
30 |
greg |
1.7 |
double area; /* radiating area for this geometry */ |
31 |
greg |
1.2 |
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 |
saba |
2.5 |
{ "lamp type", "WHITE", typecheck, |
40 |
greg |
1.2 |
"The lamp type is a string which corresponds to one of the types registered\n\ |
41 |
saba |
2.5 |
in the lamp table file. A value of \"WHITE\" means an uncolored source,\n\ |
42 |
greg |
1.2 |
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 |
greg |
1.3 |
by multiplying the input wattage by 14 for incandescent fixtures or 70\n\ |
53 |
greg |
1.2 |
for fluorescent fixtures." }, |
54 |
|
|
}; |
55 |
|
|
|
56 |
|
|
|
57 |
greg |
1.1 |
main(argc, argv) |
58 |
|
|
int argc; |
59 |
|
|
char *argv[]; |
60 |
|
|
{ |
61 |
|
|
char *lamptab = "lamp.tab"; |
62 |
greg |
1.2 |
char buf[64]; |
63 |
|
|
int i; |
64 |
greg |
1.1 |
|
65 |
|
|
if (argc > 1) lamptab = argv[1]; |
66 |
|
|
if (loadlamps(lamptab) == 0) { |
67 |
greg |
1.2 |
fprintf(stderr, "%s: no such lamp table\n", lamptab); |
68 |
greg |
1.1 |
exit(1); |
69 |
|
|
} |
70 |
greg |
1.2 |
printf("Program to compute lamp radiance. Enter '?' for help.\n"); |
71 |
greg |
1.1 |
for ( ; ; ) { |
72 |
greg |
1.2 |
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 |
greg |
1.1 |
} |
93 |
greg |
1.2 |
} |
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 |
greg |
1.8 |
whiteval = lumens/area/(WHTEFFICACY*PI); |
177 |
greg |
1.2 |
|
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 |
greg |
1.6 |
getd(name, dp, help) /* get a positive double from stdin */ |
186 |
|
|
char *name; |
187 |
greg |
1.2 |
double *dp; |
188 |
greg |
1.6 |
char *help; |
189 |
greg |
1.2 |
{ |
190 |
|
|
char buf[32]; |
191 |
greg |
1.6 |
again: |
192 |
|
|
printf("%s [%g]: ", name, *dp); |
193 |
greg |
1.2 |
if (gets(buf) == NULL) |
194 |
|
|
return(0); |
195 |
greg |
1.6 |
if (buf[0] == '?') { |
196 |
|
|
puts(help); |
197 |
|
|
goto again; |
198 |
|
|
} |
199 |
greg |
1.2 |
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 |
greg |
1.9 |
static double parea = 1.0; |
209 |
greg |
1.5 |
|
210 |
greg |
1.9 |
getd("Polygon area", &parea, |
211 |
greg |
1.6 |
"Enter the total radiating area of the polygon."); |
212 |
greg |
1.9 |
area = unit2meter*unit2meter * parea; |
213 |
greg |
1.2 |
return(1); |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
|
217 |
|
|
getsphere() /* get projected area for a sphere */ |
218 |
|
|
{ |
219 |
greg |
1.5 |
static double radius = 1.0; |
220 |
greg |
1.2 |
|
221 |
greg |
1.6 |
getd("Sphere radius", &radius, |
222 |
|
|
"Enter the distance from the sphere's center to its surface."); |
223 |
greg |
1.7 |
area = 4.*PI*unit2meter*unit2meter * radius*radius; |
224 |
greg |
1.2 |
return(1); |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
|
228 |
|
|
getcylinder() /* get projected area for a cylinder */ |
229 |
|
|
{ |
230 |
greg |
1.5 |
static double length = 1.0, radius = 0.1; |
231 |
greg |
1.2 |
|
232 |
greg |
1.6 |
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 |
greg |
1.7 |
area = 2.*PI*unit2meter*unit2meter * radius*length; |
237 |
greg |
1.2 |
return(1); |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
|
241 |
|
|
getring() /* get projected area for a ring */ |
242 |
|
|
{ |
243 |
greg |
1.5 |
static double radius = 1.0; |
244 |
greg |
1.2 |
|
245 |
greg |
1.6 |
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 |
greg |
1.7 |
area = PI*unit2meter*unit2meter * radius*radius; |
249 |
greg |
1.2 |
return(1); |
250 |
greg |
1.1 |
} |