1 |
greg |
1.1 |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
|
|
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* Context handlers |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
#include <stdio.h> |
12 |
greg |
1.2 |
#include <math.h> |
13 |
greg |
1.1 |
#include <string.h> |
14 |
|
|
#include "parser.h" |
15 |
|
|
#include "lookup.h" |
16 |
|
|
|
17 |
|
|
/* default context values */ |
18 |
|
|
static C_COLOR c_dfcolor = C_DEFCOLOR; |
19 |
|
|
static C_MATERIAL c_dfmaterial = C_DEFMATERIAL; |
20 |
|
|
static C_VERTEX c_dfvertex = C_DEFVERTEX; |
21 |
|
|
|
22 |
|
|
/* the unnamed contexts */ |
23 |
|
|
static C_COLOR c_uncolor = C_DEFCOLOR; |
24 |
|
|
static C_MATERIAL c_unmaterial = C_DEFMATERIAL; |
25 |
|
|
static C_VERTEX c_unvertex = C_DEFVERTEX; |
26 |
|
|
|
27 |
|
|
/* the current contexts */ |
28 |
|
|
C_COLOR *c_ccolor = &c_uncolor; |
29 |
greg |
1.12 |
char *c_ccname = NULL; |
30 |
greg |
1.1 |
C_MATERIAL *c_cmaterial = &c_unmaterial; |
31 |
greg |
1.12 |
char *c_cmname = NULL; |
32 |
greg |
1.1 |
C_VERTEX *c_cvertex = &c_unvertex; |
33 |
greg |
1.12 |
char *c_cvname = NULL; |
34 |
greg |
1.1 |
|
35 |
greg |
1.3 |
static LUTAB clr_tab = LU_SINIT(free,free); /* color lookup table */ |
36 |
|
|
static LUTAB mat_tab = LU_SINIT(free,free); /* material lookup table */ |
37 |
|
|
static LUTAB vtx_tab = LU_SINIT(free,free); /* vertex lookup table */ |
38 |
greg |
1.1 |
|
39 |
greg |
1.19 |
/* CIE 1931 Standard Observer curves */ |
40 |
|
|
static C_COLOR cie_xf = { 1, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, |
41 |
|
|
{14,42,143,435,1344,2839,3483,3362,2908,1954,956, |
42 |
|
|
320,49,93,633,1655,2904,4334,5945,7621,9163,10263, |
43 |
|
|
10622,10026,8544,6424,4479,2835,1649,874,468,227, |
44 |
|
|
114,58,29,14,7,3,2,1,0}, 106836L, .467, .368, 362.230 |
45 |
|
|
}; |
46 |
|
|
static C_COLOR cie_yf = { 1, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, |
47 |
|
|
{0,1,4,12,40,116,230,380,600,910,1390,2080,3230, |
48 |
|
|
5030,7100,8620,9540,9950,9950,9520,8700,7570,6310, |
49 |
|
|
5030,3810,2650,1750,1070,610,320,170,82,41,21,10, |
50 |
|
|
5,2,1,1,0,0}, 106856L, .398, .542, 493.525 |
51 |
|
|
}; |
52 |
|
|
static C_COLOR cie_zf = { 1, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, |
53 |
|
|
{65,201,679,2074,6456,13856,17471,17721,16692, |
54 |
|
|
12876,8130,4652,2720,1582,782,422,203,87,39,21,17, |
55 |
|
|
11,8,3,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, |
56 |
|
|
106770L, .147, .077, 54.363 |
57 |
|
|
}; |
58 |
|
|
/* Derived CIE 1931 Primaries (imaginary) */ |
59 |
|
|
static C_COLOR cie_xp = { 1, C_CDSPEC|C_CSSPEC|C_CSXY, |
60 |
|
|
{-174,-198,-195,-197,-202,-213,-235,-272,-333, |
61 |
|
|
-444,-688,-1232,-2393,-4497,-6876,-6758,-5256, |
62 |
|
|
-3100,-815,1320,3200,4782,5998,6861,7408,7754, |
63 |
|
|
7980,8120,8199,8240,8271,8292,8309,8283,8469, |
64 |
|
|
8336,8336,8336,8336,8336,8336}, |
65 |
|
|
127424L, 1., .0, |
66 |
|
|
}; |
67 |
|
|
static C_COLOR cie_yp = { 1, C_CDSPEC|C_CSSPEC|C_CSXY, |
68 |
|
|
{-451,-431,-431,-430,-427,-417,-399,-366,-312, |
69 |
|
|
-204,57,691,2142,4990,8810,9871,9122,7321,5145, |
70 |
|
|
3023,1123,-473,-1704,-2572,-3127,-3474,-3704, |
71 |
|
|
-3846,-3927,-3968,-3999,-4021,-4038,-4012,-4201, |
72 |
|
|
-4066,-4066,-4066,-4066,-4066,-4066}, |
73 |
|
|
-23035L, .0, 1., |
74 |
|
|
}; |
75 |
|
|
static C_COLOR cie_zp = { 1, C_CDSPEC|C_CSSPEC|C_CSXY, |
76 |
|
|
{4051,4054,4052,4053,4054,4056,4059,4064,4071, |
77 |
|
|
4074,4056,3967,3677,2933,1492,313,-440,-795, |
78 |
|
|
-904,-918,-898,-884,-869,-863,-855,-855,-851, |
79 |
|
|
-848,-847,-846,-846,-846,-845,-846,-843,-845, |
80 |
|
|
-845,-845,-845,-845,-845}, |
81 |
|
|
36057L, .0, .0, |
82 |
|
|
}; |
83 |
greg |
1.1 |
|
84 |
greg |
1.5 |
static int setspectrum(); |
85 |
greg |
1.18 |
static int setbbtemp(); |
86 |
greg |
1.5 |
static void mixcolors(); |
87 |
|
|
|
88 |
|
|
|
89 |
greg |
1.1 |
int |
90 |
|
|
c_hcolor(ac, av) /* handle color entity */ |
91 |
|
|
int ac; |
92 |
|
|
register char **av; |
93 |
|
|
{ |
94 |
greg |
1.5 |
double w, wsum; |
95 |
|
|
register int i; |
96 |
greg |
1.1 |
register LUENT *lp; |
97 |
|
|
|
98 |
|
|
switch (mg_entity(av[0])) { |
99 |
|
|
case MG_E_COLOR: /* get/set color context */ |
100 |
greg |
1.4 |
if (ac > 4) |
101 |
|
|
return(MG_EARGC); |
102 |
greg |
1.1 |
if (ac == 1) { /* set unnamed color context */ |
103 |
|
|
c_uncolor = c_dfcolor; |
104 |
|
|
c_ccolor = &c_uncolor; |
105 |
greg |
1.12 |
c_ccname = NULL; |
106 |
greg |
1.1 |
return(MG_OK); |
107 |
|
|
} |
108 |
greg |
1.22 |
if (!isname(av[1])) |
109 |
|
|
return(MG_EILL); |
110 |
greg |
1.1 |
lp = lu_find(&clr_tab, av[1]); /* lookup context */ |
111 |
|
|
if (lp == NULL) |
112 |
|
|
return(MG_EMEM); |
113 |
greg |
1.14 |
c_ccname = lp->key; |
114 |
greg |
1.9 |
c_ccolor = (C_COLOR *)lp->data; |
115 |
greg |
1.1 |
if (ac == 2) { /* reestablish previous context */ |
116 |
greg |
1.9 |
if (c_ccolor == NULL) |
117 |
greg |
1.1 |
return(MG_EUNDEF); |
118 |
|
|
return(MG_OK); |
119 |
|
|
} |
120 |
|
|
if (av[2][0] != '=' || av[2][1]) |
121 |
|
|
return(MG_ETYPE); |
122 |
greg |
1.9 |
if (c_ccolor == NULL) { /* create new color context */ |
123 |
greg |
1.1 |
lp->key = (char *)malloc(strlen(av[1])+1); |
124 |
|
|
if (lp->key == NULL) |
125 |
|
|
return(MG_EMEM); |
126 |
|
|
strcpy(lp->key, av[1]); |
127 |
|
|
lp->data = (char *)malloc(sizeof(C_COLOR)); |
128 |
|
|
if (lp->data == NULL) |
129 |
|
|
return(MG_EMEM); |
130 |
greg |
1.14 |
c_ccname = lp->key; |
131 |
greg |
1.9 |
c_ccolor = (C_COLOR *)lp->data; |
132 |
|
|
c_ccolor->clock = 0; |
133 |
greg |
1.1 |
} |
134 |
greg |
1.10 |
i = c_ccolor->clock; |
135 |
greg |
1.1 |
if (ac == 3) { /* use default template */ |
136 |
|
|
*c_ccolor = c_dfcolor; |
137 |
greg |
1.9 |
c_ccolor->clock = i + 1; |
138 |
greg |
1.1 |
return(MG_OK); |
139 |
|
|
} |
140 |
|
|
lp = lu_find(&clr_tab, av[3]); /* lookup template */ |
141 |
|
|
if (lp == NULL) |
142 |
|
|
return(MG_EMEM); |
143 |
|
|
if (lp->data == NULL) |
144 |
|
|
return(MG_EUNDEF); |
145 |
|
|
*c_ccolor = *(C_COLOR *)lp->data; |
146 |
greg |
1.9 |
c_ccolor->clock = i + 1; |
147 |
greg |
1.1 |
return(MG_OK); |
148 |
|
|
case MG_E_CXY: /* assign CIE XY value */ |
149 |
|
|
if (ac != 3) |
150 |
|
|
return(MG_EARGC); |
151 |
greg |
1.18 |
if (!isflt(av[1]) | !isflt(av[2])) |
152 |
greg |
1.1 |
return(MG_ETYPE); |
153 |
|
|
c_ccolor->cx = atof(av[1]); |
154 |
|
|
c_ccolor->cy = atof(av[2]); |
155 |
greg |
1.5 |
c_ccolor->flags = C_CDXY|C_CSXY; |
156 |
greg |
1.1 |
if (c_ccolor->cx < 0. | c_ccolor->cy < 0. | |
157 |
|
|
c_ccolor->cx + c_ccolor->cy > 1.) |
158 |
|
|
return(MG_EILL); |
159 |
greg |
1.8 |
c_ccolor->clock++; |
160 |
greg |
1.1 |
return(MG_OK); |
161 |
greg |
1.5 |
case MG_E_CSPEC: /* assign spectral values */ |
162 |
|
|
if (ac < 5) |
163 |
|
|
return(MG_EARGC); |
164 |
greg |
1.18 |
if (!isflt(av[1]) | !isflt(av[2])) |
165 |
greg |
1.5 |
return(MG_ETYPE); |
166 |
greg |
1.17 |
return(setspectrum(c_ccolor, atof(av[1]), atof(av[2]), |
167 |
greg |
1.5 |
ac-3, av+3)); |
168 |
greg |
1.18 |
case MG_E_CCT: /* assign black body spectrum */ |
169 |
|
|
if (ac != 2) |
170 |
|
|
return(MG_EARGC); |
171 |
|
|
if (!isflt(av[1])) |
172 |
|
|
return(MG_ETYPE); |
173 |
|
|
return(setbbtemp(c_ccolor, atof(av[1]))); |
174 |
greg |
1.5 |
case MG_E_CMIX: /* mix colors */ |
175 |
|
|
if (ac < 5 || (ac-1)%2) |
176 |
|
|
return(MG_EARGC); |
177 |
|
|
if (!isflt(av[1])) |
178 |
|
|
return(MG_ETYPE); |
179 |
|
|
wsum = atof(av[1]); |
180 |
|
|
if (wsum < 0.) |
181 |
|
|
return(MG_EILL); |
182 |
|
|
if ((lp = lu_find(&clr_tab, av[2])) == NULL) |
183 |
|
|
return(MG_EMEM); |
184 |
|
|
if (lp->data == NULL) |
185 |
|
|
return(MG_EUNDEF); |
186 |
|
|
*c_ccolor = *(C_COLOR *)lp->data; |
187 |
|
|
for (i = 3; i < ac; i += 2) { |
188 |
|
|
if (!isflt(av[i])) |
189 |
|
|
return(MG_ETYPE); |
190 |
|
|
w = atof(av[i]); |
191 |
|
|
if (w < 0.) |
192 |
|
|
return(MG_EILL); |
193 |
|
|
if ((lp = lu_find(&clr_tab, av[i+1])) == NULL) |
194 |
|
|
return(MG_EMEM); |
195 |
|
|
if (lp->data == NULL) |
196 |
|
|
return(MG_EUNDEF); |
197 |
|
|
mixcolors(c_ccolor, wsum, c_ccolor, |
198 |
|
|
w, (C_COLOR *)lp->data); |
199 |
|
|
wsum += w; |
200 |
|
|
} |
201 |
greg |
1.10 |
c_ccolor->clock++; |
202 |
greg |
1.5 |
return(MG_OK); |
203 |
greg |
1.1 |
} |
204 |
|
|
return(MG_EUNK); |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
|
208 |
|
|
int |
209 |
|
|
c_hmaterial(ac, av) /* handle material entity */ |
210 |
|
|
int ac; |
211 |
|
|
register char **av; |
212 |
|
|
{ |
213 |
greg |
1.9 |
int i; |
214 |
greg |
1.1 |
register LUENT *lp; |
215 |
|
|
|
216 |
|
|
switch (mg_entity(av[0])) { |
217 |
|
|
case MG_E_MATERIAL: /* get/set material context */ |
218 |
greg |
1.4 |
if (ac > 4) |
219 |
|
|
return(MG_EARGC); |
220 |
greg |
1.1 |
if (ac == 1) { /* set unnamed material context */ |
221 |
|
|
c_unmaterial = c_dfmaterial; |
222 |
|
|
c_cmaterial = &c_unmaterial; |
223 |
greg |
1.12 |
c_cmname = NULL; |
224 |
greg |
1.1 |
return(MG_OK); |
225 |
|
|
} |
226 |
greg |
1.22 |
if (!isname(av[1])) |
227 |
|
|
return(MG_EILL); |
228 |
greg |
1.1 |
lp = lu_find(&mat_tab, av[1]); /* lookup context */ |
229 |
|
|
if (lp == NULL) |
230 |
|
|
return(MG_EMEM); |
231 |
greg |
1.14 |
c_cmname = lp->key; |
232 |
greg |
1.9 |
c_cmaterial = (C_MATERIAL *)lp->data; |
233 |
greg |
1.1 |
if (ac == 2) { /* reestablish previous context */ |
234 |
greg |
1.9 |
if (c_cmaterial == NULL) |
235 |
greg |
1.1 |
return(MG_EUNDEF); |
236 |
|
|
return(MG_OK); |
237 |
|
|
} |
238 |
|
|
if (av[2][0] != '=' || av[2][1]) |
239 |
|
|
return(MG_ETYPE); |
240 |
greg |
1.9 |
if (c_cmaterial == NULL) { /* create new material */ |
241 |
greg |
1.1 |
lp->key = (char *)malloc(strlen(av[1])+1); |
242 |
|
|
if (lp->key == NULL) |
243 |
|
|
return(MG_EMEM); |
244 |
|
|
strcpy(lp->key, av[1]); |
245 |
|
|
lp->data = (char *)malloc(sizeof(C_MATERIAL)); |
246 |
|
|
if (lp->data == NULL) |
247 |
|
|
return(MG_EMEM); |
248 |
greg |
1.14 |
c_cmname = lp->key; |
249 |
greg |
1.9 |
c_cmaterial = (C_MATERIAL *)lp->data; |
250 |
|
|
c_cmaterial->clock = 0; |
251 |
greg |
1.1 |
} |
252 |
greg |
1.10 |
i = c_cmaterial->clock; |
253 |
greg |
1.1 |
if (ac == 3) { /* use default template */ |
254 |
|
|
*c_cmaterial = c_dfmaterial; |
255 |
greg |
1.9 |
c_cmaterial->clock = i + 1; |
256 |
greg |
1.1 |
return(MG_OK); |
257 |
|
|
} |
258 |
|
|
lp = lu_find(&mat_tab, av[3]); /* lookup template */ |
259 |
|
|
if (lp == NULL) |
260 |
|
|
return(MG_EMEM); |
261 |
|
|
if (lp->data == NULL) |
262 |
|
|
return(MG_EUNDEF); |
263 |
|
|
*c_cmaterial = *(C_MATERIAL *)lp->data; |
264 |
greg |
1.9 |
c_cmaterial->clock = i + 1; |
265 |
greg |
1.1 |
return(MG_OK); |
266 |
greg |
1.21 |
case MG_E_IR: /* set index of refraction */ |
267 |
|
|
if (ac != 3) |
268 |
|
|
return(MG_EARGC); |
269 |
|
|
if (!isflt(av[1]) | !isflt(av[2])) |
270 |
|
|
return(MG_ETYPE); |
271 |
|
|
c_cmaterial->nr = atof(av[1]); |
272 |
|
|
c_cmaterial->ni = atof(av[2]); |
273 |
|
|
if (c_cmaterial->nr <= FTINY) |
274 |
|
|
return(MG_EILL); |
275 |
|
|
c_cmaterial->clock++; |
276 |
|
|
return(MG_OK); |
277 |
greg |
1.1 |
case MG_E_RD: /* set diffuse reflectance */ |
278 |
|
|
if (ac != 2) |
279 |
|
|
return(MG_EARGC); |
280 |
|
|
if (!isflt(av[1])) |
281 |
|
|
return(MG_ETYPE); |
282 |
|
|
c_cmaterial->rd = atof(av[1]); |
283 |
|
|
if (c_cmaterial->rd < 0. | c_cmaterial->rd > 1.) |
284 |
|
|
return(MG_EILL); |
285 |
|
|
c_cmaterial->rd_c = *c_ccolor; |
286 |
greg |
1.2 |
c_cmaterial->clock++; |
287 |
greg |
1.1 |
return(MG_OK); |
288 |
|
|
case MG_E_ED: /* set diffuse emittance */ |
289 |
|
|
if (ac != 2) |
290 |
|
|
return(MG_EARGC); |
291 |
|
|
if (!isflt(av[1])) |
292 |
|
|
return(MG_ETYPE); |
293 |
|
|
c_cmaterial->ed = atof(av[1]); |
294 |
|
|
if (c_cmaterial->ed < 0.) |
295 |
|
|
return(MG_EILL); |
296 |
|
|
c_cmaterial->ed_c = *c_ccolor; |
297 |
greg |
1.2 |
c_cmaterial->clock++; |
298 |
greg |
1.1 |
return(MG_OK); |
299 |
|
|
case MG_E_TD: /* set diffuse transmittance */ |
300 |
|
|
if (ac != 2) |
301 |
|
|
return(MG_EARGC); |
302 |
|
|
if (!isflt(av[1])) |
303 |
|
|
return(MG_ETYPE); |
304 |
|
|
c_cmaterial->td = atof(av[1]); |
305 |
|
|
if (c_cmaterial->td < 0. | c_cmaterial->td > 1.) |
306 |
|
|
return(MG_EILL); |
307 |
|
|
c_cmaterial->td_c = *c_ccolor; |
308 |
greg |
1.8 |
c_cmaterial->clock++; |
309 |
greg |
1.1 |
return(MG_OK); |
310 |
|
|
case MG_E_RS: /* set specular reflectance */ |
311 |
|
|
if (ac != 3) |
312 |
|
|
return(MG_EARGC); |
313 |
greg |
1.18 |
if (!isflt(av[1]) | !isflt(av[2])) |
314 |
greg |
1.1 |
return(MG_ETYPE); |
315 |
|
|
c_cmaterial->rs = atof(av[1]); |
316 |
|
|
c_cmaterial->rs_a = atof(av[2]); |
317 |
|
|
if (c_cmaterial->rs < 0. | c_cmaterial->rs > 1. | |
318 |
|
|
c_cmaterial->rs_a < 0.) |
319 |
|
|
return(MG_EILL); |
320 |
|
|
c_cmaterial->rs_c = *c_ccolor; |
321 |
greg |
1.2 |
c_cmaterial->clock++; |
322 |
greg |
1.1 |
return(MG_OK); |
323 |
|
|
case MG_E_TS: /* set specular transmittance */ |
324 |
|
|
if (ac != 3) |
325 |
|
|
return(MG_EARGC); |
326 |
greg |
1.18 |
if (!isflt(av[1]) | !isflt(av[2])) |
327 |
greg |
1.1 |
return(MG_ETYPE); |
328 |
|
|
c_cmaterial->ts = atof(av[1]); |
329 |
|
|
c_cmaterial->ts_a = atof(av[2]); |
330 |
|
|
if (c_cmaterial->ts < 0. | c_cmaterial->ts > 1. | |
331 |
|
|
c_cmaterial->ts_a < 0.) |
332 |
|
|
return(MG_EILL); |
333 |
|
|
c_cmaterial->ts_c = *c_ccolor; |
334 |
greg |
1.2 |
c_cmaterial->clock++; |
335 |
greg |
1.1 |
return(MG_OK); |
336 |
greg |
1.13 |
case MG_E_SIDES: /* set number of sides */ |
337 |
|
|
if (ac != 2) |
338 |
|
|
return(MG_EARGC); |
339 |
|
|
if (!isint(av[1])) |
340 |
|
|
return(MG_ETYPE); |
341 |
|
|
i = atoi(av[1]); |
342 |
|
|
if (i == 1) |
343 |
|
|
c_cmaterial->sided = 1; |
344 |
|
|
else if (i == 2) |
345 |
|
|
c_cmaterial->sided = 0; |
346 |
|
|
else |
347 |
|
|
return(MG_EILL); |
348 |
|
|
c_cmaterial->clock++; |
349 |
|
|
return(MG_OK); |
350 |
greg |
1.1 |
} |
351 |
|
|
return(MG_EUNK); |
352 |
|
|
} |
353 |
|
|
|
354 |
|
|
|
355 |
|
|
int |
356 |
|
|
c_hvertex(ac, av) /* handle a vertex entity */ |
357 |
|
|
int ac; |
358 |
|
|
register char **av; |
359 |
|
|
{ |
360 |
greg |
1.9 |
int i; |
361 |
greg |
1.1 |
register LUENT *lp; |
362 |
|
|
|
363 |
|
|
switch (mg_entity(av[0])) { |
364 |
|
|
case MG_E_VERTEX: /* get/set vertex context */ |
365 |
greg |
1.4 |
if (ac > 4) |
366 |
|
|
return(MG_EARGC); |
367 |
greg |
1.1 |
if (ac == 1) { /* set unnamed vertex context */ |
368 |
|
|
c_unvertex = c_dfvertex; |
369 |
|
|
c_cvertex = &c_unvertex; |
370 |
greg |
1.12 |
c_cvname = NULL; |
371 |
greg |
1.1 |
return(MG_OK); |
372 |
|
|
} |
373 |
greg |
1.22 |
if (!isname(av[1])) |
374 |
|
|
return(MG_EILL); |
375 |
greg |
1.1 |
lp = lu_find(&vtx_tab, av[1]); /* lookup context */ |
376 |
|
|
if (lp == NULL) |
377 |
|
|
return(MG_EMEM); |
378 |
greg |
1.14 |
c_cvname = lp->key; |
379 |
greg |
1.9 |
c_cvertex = (C_VERTEX *)lp->data; |
380 |
greg |
1.1 |
if (ac == 2) { /* reestablish previous context */ |
381 |
greg |
1.9 |
if (c_cvertex == NULL) |
382 |
greg |
1.1 |
return(MG_EUNDEF); |
383 |
|
|
return(MG_OK); |
384 |
|
|
} |
385 |
|
|
if (av[2][0] != '=' || av[2][1]) |
386 |
|
|
return(MG_ETYPE); |
387 |
greg |
1.9 |
if (c_cvertex == NULL) { /* create new vertex context */ |
388 |
greg |
1.1 |
lp->key = (char *)malloc(strlen(av[1])+1); |
389 |
|
|
if (lp->key == NULL) |
390 |
|
|
return(MG_EMEM); |
391 |
|
|
strcpy(lp->key, av[1]); |
392 |
|
|
lp->data = (char *)malloc(sizeof(C_VERTEX)); |
393 |
|
|
if (lp->data == NULL) |
394 |
|
|
return(MG_EMEM); |
395 |
greg |
1.14 |
c_cvname = lp->key; |
396 |
greg |
1.9 |
c_cvertex = (C_VERTEX *)lp->data; |
397 |
greg |
1.1 |
} |
398 |
greg |
1.10 |
i = c_cvertex->clock; |
399 |
greg |
1.1 |
if (ac == 3) { /* use default template */ |
400 |
|
|
*c_cvertex = c_dfvertex; |
401 |
greg |
1.9 |
c_cvertex->clock = i + 1; |
402 |
greg |
1.1 |
return(MG_OK); |
403 |
|
|
} |
404 |
|
|
lp = lu_find(&vtx_tab, av[3]); /* lookup template */ |
405 |
|
|
if (lp == NULL) |
406 |
|
|
return(MG_EMEM); |
407 |
|
|
if (lp->data == NULL) |
408 |
|
|
return(MG_EUNDEF); |
409 |
|
|
*c_cvertex = *(C_VERTEX *)lp->data; |
410 |
greg |
1.9 |
c_cvertex->clock = i + 1; |
411 |
greg |
1.1 |
return(MG_OK); |
412 |
|
|
case MG_E_POINT: /* set point */ |
413 |
|
|
if (ac != 4) |
414 |
|
|
return(MG_EARGC); |
415 |
greg |
1.18 |
if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3])) |
416 |
greg |
1.1 |
return(MG_ETYPE); |
417 |
|
|
c_cvertex->p[0] = atof(av[1]); |
418 |
|
|
c_cvertex->p[1] = atof(av[2]); |
419 |
|
|
c_cvertex->p[2] = atof(av[3]); |
420 |
greg |
1.8 |
c_cvertex->clock++; |
421 |
greg |
1.1 |
return(MG_OK); |
422 |
|
|
case MG_E_NORMAL: /* set normal */ |
423 |
|
|
if (ac != 4) |
424 |
|
|
return(MG_EARGC); |
425 |
greg |
1.18 |
if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3])) |
426 |
greg |
1.1 |
return(MG_ETYPE); |
427 |
|
|
c_cvertex->n[0] = atof(av[1]); |
428 |
|
|
c_cvertex->n[1] = atof(av[2]); |
429 |
|
|
c_cvertex->n[2] = atof(av[3]); |
430 |
|
|
(void)normalize(c_cvertex->n); |
431 |
greg |
1.8 |
c_cvertex->clock++; |
432 |
greg |
1.1 |
return(MG_OK); |
433 |
|
|
} |
434 |
|
|
return(MG_EUNK); |
435 |
|
|
} |
436 |
|
|
|
437 |
|
|
|
438 |
|
|
void |
439 |
|
|
c_clearall() /* empty context tables */ |
440 |
|
|
{ |
441 |
|
|
c_uncolor = c_dfcolor; |
442 |
|
|
c_ccolor = &c_uncolor; |
443 |
greg |
1.15 |
c_ccname = NULL; |
444 |
greg |
1.3 |
lu_done(&clr_tab); |
445 |
greg |
1.1 |
c_unmaterial = c_dfmaterial; |
446 |
|
|
c_cmaterial = &c_unmaterial; |
447 |
greg |
1.15 |
c_cmname = NULL; |
448 |
greg |
1.3 |
lu_done(&mat_tab); |
449 |
greg |
1.1 |
c_unvertex = c_dfvertex; |
450 |
|
|
c_cvertex = &c_unvertex; |
451 |
greg |
1.15 |
c_cvname = NULL; |
452 |
greg |
1.3 |
lu_done(&vtx_tab); |
453 |
greg |
1.1 |
} |
454 |
|
|
|
455 |
|
|
|
456 |
greg |
1.11 |
C_MATERIAL * |
457 |
|
|
c_getmaterial(name) /* get a named material */ |
458 |
|
|
char *name; |
459 |
|
|
{ |
460 |
|
|
register LUENT *lp; |
461 |
|
|
|
462 |
|
|
if ((lp = lu_find(&mat_tab, name)) == NULL) |
463 |
|
|
return(NULL); |
464 |
|
|
return((C_MATERIAL *)lp->data); |
465 |
|
|
} |
466 |
|
|
|
467 |
|
|
|
468 |
greg |
1.1 |
C_VERTEX * |
469 |
|
|
c_getvert(name) /* get a named vertex */ |
470 |
|
|
char *name; |
471 |
|
|
{ |
472 |
|
|
register LUENT *lp; |
473 |
|
|
|
474 |
|
|
if ((lp = lu_find(&vtx_tab, name)) == NULL) |
475 |
|
|
return(NULL); |
476 |
|
|
return((C_VERTEX *)lp->data); |
477 |
greg |
1.5 |
} |
478 |
|
|
|
479 |
|
|
|
480 |
greg |
1.7 |
C_COLOR * |
481 |
|
|
c_getcolor(name) /* get a named color */ |
482 |
|
|
char *name; |
483 |
|
|
{ |
484 |
|
|
register LUENT *lp; |
485 |
|
|
|
486 |
|
|
if ((lp = lu_find(&clr_tab, name)) == NULL) |
487 |
|
|
return(NULL); |
488 |
|
|
return((C_COLOR *)lp->data); |
489 |
|
|
} |
490 |
|
|
|
491 |
|
|
|
492 |
greg |
1.5 |
int |
493 |
|
|
c_isgrey(clr) /* check if color is grey */ |
494 |
|
|
register C_COLOR *clr; |
495 |
|
|
{ |
496 |
greg |
1.8 |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) |
497 |
greg |
1.5 |
return(1); /* no settings == grey */ |
498 |
|
|
c_ccvt(clr, C_CSXY); |
499 |
|
|
return(clr->cx >= .323 && clr->cx <= .343 && |
500 |
|
|
clr->cy >= .323 && clr->cy <= .343); |
501 |
|
|
} |
502 |
|
|
|
503 |
|
|
|
504 |
|
|
void |
505 |
|
|
c_ccvt(clr, fl) /* convert color representations */ |
506 |
|
|
register C_COLOR *clr; |
507 |
|
|
int fl; |
508 |
|
|
{ |
509 |
|
|
double x, y, z; |
510 |
|
|
register int i; |
511 |
|
|
|
512 |
greg |
1.16 |
fl &= ~clr->flags; /* ignore what's done */ |
513 |
|
|
if (!fl) /* everything's done! */ |
514 |
greg |
1.5 |
return; |
515 |
greg |
1.8 |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) /* nothing set! */ |
516 |
greg |
1.5 |
*clr = c_dfcolor; |
517 |
greg |
1.16 |
if (fl & C_CSXY) { /* cspec -> cxy */ |
518 |
greg |
1.5 |
x = y = z = 0.; |
519 |
|
|
for (i = 0; i < C_CNSS; i++) { |
520 |
|
|
x += cie_xf.ssamp[i] * clr->ssamp[i]; |
521 |
|
|
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
522 |
|
|
z += cie_zf.ssamp[i] * clr->ssamp[i]; |
523 |
|
|
} |
524 |
greg |
1.17 |
x /= (double)cie_xf.ssum; |
525 |
|
|
y /= (double)cie_yf.ssum; |
526 |
|
|
z /= (double)cie_zf.ssum; |
527 |
greg |
1.5 |
z += x + y; |
528 |
|
|
clr->cx = x / z; |
529 |
|
|
clr->cy = y / z; |
530 |
|
|
clr->flags |= C_CSXY; |
531 |
greg |
1.16 |
} else if (fl & C_CSSPEC) { /* cxy -> cspec */ |
532 |
greg |
1.19 |
x = clr->cx; |
533 |
|
|
y = clr->cy; |
534 |
|
|
z = 1. - x - y; |
535 |
greg |
1.5 |
clr->ssum = 0; |
536 |
greg |
1.19 |
for (i = 0; i < C_CNSS; i++) { |
537 |
|
|
clr->ssamp[i] = x*cie_xp.ssamp[i] + y*cie_yp.ssamp[i] |
538 |
|
|
+ z*cie_zp.ssamp[i] + .5; |
539 |
|
|
if (clr->ssamp[i] < 0) /* out of gamut! */ |
540 |
|
|
clr->ssamp[i] = 0; |
541 |
|
|
else |
542 |
|
|
clr->ssum += clr->ssamp[i]; |
543 |
|
|
} |
544 |
greg |
1.5 |
clr->flags |= C_CSSPEC; |
545 |
|
|
} |
546 |
greg |
1.16 |
if (fl & C_CSEFF) { /* compute efficacy */ |
547 |
greg |
1.19 |
if (clr->flags & C_CSSPEC) { /* from spectrum */ |
548 |
greg |
1.16 |
y = 0.; |
549 |
|
|
for (i = 0; i < C_CNSS; i++) |
550 |
|
|
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
551 |
|
|
clr->eff = C_CLPWM * y / clr->ssum; |
552 |
greg |
1.19 |
} else /* clr->flags & C_CSXY */ { /* from (x,y) */ |
553 |
greg |
1.16 |
clr->eff = clr->cx*cie_xf.eff + clr->cy*cie_yf.eff + |
554 |
|
|
(1. - clr->cx - clr->cy)*cie_zf.eff; |
555 |
|
|
} |
556 |
|
|
clr->flags |= C_CSEFF; |
557 |
|
|
} |
558 |
greg |
1.5 |
} |
559 |
|
|
|
560 |
|
|
|
561 |
|
|
static int |
562 |
|
|
setspectrum(clr, wlmin, wlmax, ac, av) /* convert a spectrum */ |
563 |
|
|
register C_COLOR *clr; |
564 |
greg |
1.17 |
double wlmin, wlmax; |
565 |
greg |
1.5 |
int ac; |
566 |
|
|
char **av; |
567 |
|
|
{ |
568 |
|
|
double scale; |
569 |
greg |
1.17 |
float va[C_CNSS]; |
570 |
|
|
register int i, pos; |
571 |
|
|
int n, imax; |
572 |
|
|
int wl; |
573 |
greg |
1.5 |
double wl0, wlstep; |
574 |
greg |
1.20 |
double boxpos, boxstep; |
575 |
greg |
1.17 |
/* check bounds */ |
576 |
|
|
if (wlmax <= C_CMINWL | wlmax <= wlmin | wlmin >= C_CMAXWL) |
577 |
greg |
1.5 |
return(MG_EILL); |
578 |
greg |
1.17 |
wlstep = (wlmax - wlmin)/(ac-1); |
579 |
|
|
while (wlmin < C_CMINWL) { |
580 |
|
|
wlmin += wlstep; |
581 |
|
|
ac--; av++; |
582 |
|
|
} |
583 |
|
|
while (wlmax > C_CMAXWL) { |
584 |
|
|
wlmax -= wlstep; |
585 |
|
|
ac--; |
586 |
|
|
} |
587 |
|
|
imax = ac; /* box filter if necessary */ |
588 |
greg |
1.20 |
boxpos = 0; |
589 |
|
|
boxstep = 1; |
590 |
greg |
1.17 |
if (wlstep < C_CWLI) { |
591 |
greg |
1.20 |
imax = (wlmax - wlmin)/C_CWLI + (1-FTINY); |
592 |
|
|
boxpos = (wlmin - C_CMINWL)/C_CWLI; |
593 |
|
|
boxstep = wlstep/C_CWLI; |
594 |
greg |
1.17 |
wlstep = C_CWLI; |
595 |
|
|
} |
596 |
greg |
1.5 |
scale = 0.; /* get values and maximum */ |
597 |
greg |
1.17 |
pos = 0; |
598 |
|
|
for (i = 0; i < imax; i++) { |
599 |
|
|
va[i] = 0.; n = 0; |
600 |
greg |
1.20 |
while (boxpos < i+.5 && pos < ac) { |
601 |
greg |
1.17 |
if (!isflt(av[pos])) |
602 |
|
|
return(MG_ETYPE); |
603 |
|
|
va[i] += atof(av[pos++]); |
604 |
|
|
n++; |
605 |
greg |
1.20 |
boxpos += boxstep; |
606 |
greg |
1.17 |
} |
607 |
|
|
if (n > 1) |
608 |
|
|
va[i] /= (double)n; |
609 |
greg |
1.5 |
if (va[i] > scale) |
610 |
|
|
scale = va[i]; |
611 |
greg |
1.23 |
else if (va[i] < -scale) |
612 |
|
|
scale = -va[i]; |
613 |
greg |
1.5 |
} |
614 |
greg |
1.20 |
if (scale <= FTINY) |
615 |
greg |
1.5 |
return(MG_EILL); |
616 |
|
|
scale = C_CMAXV / scale; |
617 |
|
|
clr->ssum = 0; /* convert to our spacing */ |
618 |
|
|
wl0 = wlmin; |
619 |
|
|
pos = 0; |
620 |
|
|
for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI) |
621 |
greg |
1.17 |
if (wl < wlmin | wl > wlmax) |
622 |
greg |
1.5 |
clr->ssamp[i] = 0; |
623 |
|
|
else { |
624 |
|
|
while (wl0 + wlstep < wl+FTINY) { |
625 |
|
|
wl0 += wlstep; |
626 |
|
|
pos++; |
627 |
|
|
} |
628 |
greg |
1.17 |
if (wl+FTINY >= wl0 & wl-FTINY <= wl0) |
629 |
greg |
1.18 |
clr->ssamp[i] = scale*va[pos] + .5; |
630 |
greg |
1.5 |
else /* interpolate if necessary */ |
631 |
greg |
1.18 |
clr->ssamp[i] = .5 + scale / wlstep * |
632 |
greg |
1.5 |
( va[pos]*(wl0+wlstep - wl) + |
633 |
|
|
va[pos+1]*(wl - wl0) ); |
634 |
|
|
clr->ssum += clr->ssamp[i]; |
635 |
|
|
} |
636 |
|
|
clr->flags = C_CDSPEC|C_CSSPEC; |
637 |
greg |
1.8 |
clr->clock++; |
638 |
greg |
1.5 |
return(MG_OK); |
639 |
|
|
} |
640 |
|
|
|
641 |
|
|
|
642 |
|
|
static void |
643 |
|
|
mixcolors(cres, w1, c1, w2, c2) /* mix two colors according to weights given */ |
644 |
|
|
register C_COLOR *cres, *c1, *c2; |
645 |
|
|
double w1, w2; |
646 |
|
|
{ |
647 |
|
|
double scale; |
648 |
|
|
float cmix[C_CNSS]; |
649 |
|
|
register int i; |
650 |
|
|
|
651 |
|
|
if ((c1->flags|c2->flags) & C_CDSPEC) { /* spectral mixing */ |
652 |
greg |
1.16 |
c_ccvt(c1, C_CSSPEC|C_CSEFF); |
653 |
|
|
c_ccvt(c2, C_CSSPEC|C_CSEFF); |
654 |
|
|
w1 /= c1->eff*c1->ssum; |
655 |
greg |
1.17 |
w2 /= c2->eff*c2->ssum; |
656 |
greg |
1.5 |
scale = 0.; |
657 |
|
|
for (i = 0; i < C_CNSS; i++) { |
658 |
|
|
cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i]; |
659 |
|
|
if (cmix[i] > scale) |
660 |
|
|
scale = cmix[i]; |
661 |
|
|
} |
662 |
|
|
scale = C_CMAXV / scale; |
663 |
|
|
cres->ssum = 0; |
664 |
|
|
for (i = 0; i < C_CNSS; i++) |
665 |
|
|
cres->ssum += cres->ssamp[i] = scale*cmix[i] + .5; |
666 |
|
|
cres->flags = C_CDSPEC|C_CSSPEC; |
667 |
|
|
} else { /* CIE xy mixing */ |
668 |
|
|
c_ccvt(c1, C_CSXY); |
669 |
|
|
c_ccvt(c2, C_CSXY); |
670 |
|
|
scale = 1. / (w1/c1->cy + w2/c2->cy); |
671 |
|
|
cres->cx = (c1->cx*w1/c1->cy + c2->cx*w2/c2->cy) * scale; |
672 |
|
|
cres->cy = (w1 + w2) * scale; |
673 |
|
|
cres->flags = C_CDXY|C_CSXY; |
674 |
|
|
} |
675 |
greg |
1.1 |
} |
676 |
greg |
1.18 |
|
677 |
|
|
|
678 |
|
|
#define C1 3.741832e-16 /* W-m^2 */ |
679 |
|
|
#define C2 1.4388e-2 /* m-K */ |
680 |
|
|
|
681 |
|
|
#define bbsp(l,t) (C1/((l)*(l)*(l)*(l)*(l)*(exp(C2/((t)*(l)))-1.))) |
682 |
|
|
#define bblm(t) (C2/5./(t)) |
683 |
|
|
|
684 |
|
|
static int |
685 |
|
|
setbbtemp(clr, tk) /* set black body spectrum */ |
686 |
|
|
register C_COLOR *clr; |
687 |
|
|
double tk; |
688 |
|
|
{ |
689 |
|
|
double sf, wl; |
690 |
|
|
register int i; |
691 |
|
|
|
692 |
|
|
if (tk < 1000) |
693 |
|
|
return(MG_EILL); |
694 |
|
|
wl = bblm(tk); /* scalefactor based on peak */ |
695 |
|
|
if (wl < C_CMINWL*1e-9) |
696 |
|
|
wl = C_CMINWL*1e-9; |
697 |
|
|
else if (wl > C_CMAXWL*1e-9) |
698 |
|
|
wl = C_CMAXWL*1e-9; |
699 |
|
|
sf = C_CMAXV/bbsp(wl,tk); |
700 |
|
|
clr->ssum = 0; |
701 |
|
|
for (i = 0; i < C_CNSS; i++) { |
702 |
|
|
wl = (C_CMINWL + i*C_CWLI)*1e-9; |
703 |
|
|
clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + .5; |
704 |
|
|
} |
705 |
|
|
clr->flags = C_CDSPEC|C_CSSPEC; |
706 |
|
|
clr->clock++; |
707 |
|
|
return(MG_OK); |
708 |
|
|
} |
709 |
|
|
|
710 |
|
|
#undef C1 |
711 |
|
|
#undef C2 |
712 |
|
|
#undef bbsp |
713 |
|
|
#undef bblm |