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