1 |
– |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Context handlers |
6 |
|
*/ |
7 |
|
|
8 |
|
#include <stdio.h> |
9 |
< |
#include <math.h> |
9 |
> |
#include <stdlib.h> |
10 |
|
#include <string.h> |
11 |
|
#include "parser.h" |
12 |
|
#include "lookup.h" |
13 |
|
|
14 |
|
/* default context values */ |
18 |
– |
static C_COLOR c_dfcolor = C_DEFCOLOR; |
15 |
|
static C_MATERIAL c_dfmaterial = C_DEFMATERIAL; |
16 |
|
static C_VERTEX c_dfvertex = C_DEFVERTEX; |
17 |
|
|
32 |
|
static LUTAB mat_tab = LU_SINIT(free,free); /* material lookup table */ |
33 |
|
static LUTAB vtx_tab = LU_SINIT(free,free); /* vertex lookup table */ |
34 |
|
|
39 |
– |
/* CIE 1931 Standard Observer */ |
40 |
– |
static C_COLOR cie_xf = C_CIEX; |
41 |
– |
static C_COLOR cie_yf = C_CIEY; |
42 |
– |
static C_COLOR cie_zf = C_CIEZ; |
43 |
– |
|
35 |
|
static int setspectrum(); |
45 |
– |
static void mixcolors(); |
36 |
|
|
37 |
|
|
38 |
|
int |
54 |
|
c_ccname = NULL; |
55 |
|
return(MG_OK); |
56 |
|
} |
57 |
+ |
if (!isname(av[1])) |
58 |
+ |
return(MG_EILL); |
59 |
|
lp = lu_find(&clr_tab, av[1]); /* lookup context */ |
60 |
|
if (lp == NULL) |
61 |
|
return(MG_EMEM); |
79 |
|
c_ccname = lp->key; |
80 |
|
c_ccolor = (C_COLOR *)lp->data; |
81 |
|
c_ccolor->clock = 0; |
82 |
+ |
c_ccolor->client_data = NULL; |
83 |
|
} |
84 |
|
i = c_ccolor->clock; |
85 |
|
if (ac == 3) { /* use default template */ |
98 |
|
case MG_E_CXY: /* assign CIE XY value */ |
99 |
|
if (ac != 3) |
100 |
|
return(MG_EARGC); |
101 |
< |
if (!isflt(av[1]) || !isflt(av[2])) |
101 |
> |
if (!isflt(av[1]) | !isflt(av[2])) |
102 |
|
return(MG_ETYPE); |
103 |
|
c_ccolor->cx = atof(av[1]); |
104 |
|
c_ccolor->cy = atof(av[2]); |
105 |
|
c_ccolor->flags = C_CDXY|C_CSXY; |
106 |
< |
if (c_ccolor->cx < 0. | c_ccolor->cy < 0. | |
107 |
< |
c_ccolor->cx + c_ccolor->cy > 1.) |
106 |
> |
if ((c_ccolor->cx < 0.) | (c_ccolor->cy < 0.) | |
107 |
> |
(c_ccolor->cx + c_ccolor->cy > 1.)) |
108 |
|
return(MG_EILL); |
109 |
|
c_ccolor->clock++; |
110 |
|
return(MG_OK); |
111 |
|
case MG_E_CSPEC: /* assign spectral values */ |
112 |
|
if (ac < 5) |
113 |
|
return(MG_EARGC); |
114 |
< |
if (!isint(av[1]) || !isint(av[2])) |
114 |
> |
if (!isflt(av[1]) | !isflt(av[2])) |
115 |
|
return(MG_ETYPE); |
116 |
< |
return(setspectrum(c_ccolor, atoi(av[1]), atoi(av[2]), |
116 |
> |
return(setspectrum(c_ccolor, atof(av[1]), atof(av[2]), |
117 |
|
ac-3, av+3)); |
118 |
+ |
case MG_E_CCT: /* assign black body spectrum */ |
119 |
+ |
if (ac != 2) |
120 |
+ |
return(MG_EARGC); |
121 |
+ |
if (!isflt(av[1])) |
122 |
+ |
return(MG_ETYPE); |
123 |
+ |
if (!c_bbtemp(c_ccolor, atof(av[1]))) |
124 |
+ |
return(MG_EILL); |
125 |
+ |
c_ccolor->clock++; |
126 |
+ |
return(MG_OK); |
127 |
|
case MG_E_CMIX: /* mix colors */ |
128 |
|
if (ac < 5 || (ac-1)%2) |
129 |
|
return(MG_EARGC); |
130 |
|
if (!isflt(av[1])) |
131 |
|
return(MG_ETYPE); |
132 |
|
wsum = atof(av[1]); |
131 |
– |
if (wsum < 0.) |
132 |
– |
return(MG_EILL); |
133 |
|
if ((lp = lu_find(&clr_tab, av[2])) == NULL) |
134 |
|
return(MG_EMEM); |
135 |
|
if (lp->data == NULL) |
139 |
|
if (!isflt(av[i])) |
140 |
|
return(MG_ETYPE); |
141 |
|
w = atof(av[i]); |
142 |
– |
if (w < 0.) |
143 |
– |
return(MG_EILL); |
142 |
|
if ((lp = lu_find(&clr_tab, av[i+1])) == NULL) |
143 |
|
return(MG_EMEM); |
144 |
|
if (lp->data == NULL) |
145 |
|
return(MG_EUNDEF); |
146 |
< |
mixcolors(c_ccolor, wsum, c_ccolor, |
146 |
> |
c_cmix(c_ccolor, wsum, c_ccolor, |
147 |
|
w, (C_COLOR *)lp->data); |
148 |
|
wsum += w; |
149 |
|
} |
150 |
+ |
if (wsum <= 0.) |
151 |
+ |
return(MG_EILL); |
152 |
|
c_ccolor->clock++; |
153 |
|
return(MG_OK); |
154 |
|
} |
174 |
|
c_cmname = NULL; |
175 |
|
return(MG_OK); |
176 |
|
} |
177 |
+ |
if (!isname(av[1])) |
178 |
+ |
return(MG_EILL); |
179 |
|
lp = lu_find(&mat_tab, av[1]); /* lookup context */ |
180 |
|
if (lp == NULL) |
181 |
|
return(MG_EMEM); |
199 |
|
c_cmname = lp->key; |
200 |
|
c_cmaterial = (C_MATERIAL *)lp->data; |
201 |
|
c_cmaterial->clock = 0; |
202 |
+ |
c_cmaterial->client_data = NULL; |
203 |
|
} |
204 |
|
i = c_cmaterial->clock; |
205 |
|
if (ac == 3) { /* use default template */ |
215 |
|
*c_cmaterial = *(C_MATERIAL *)lp->data; |
216 |
|
c_cmaterial->clock = i + 1; |
217 |
|
return(MG_OK); |
218 |
+ |
case MG_E_IR: /* set index of refraction */ |
219 |
+ |
if (ac != 3) |
220 |
+ |
return(MG_EARGC); |
221 |
+ |
if (!isflt(av[1]) | !isflt(av[2])) |
222 |
+ |
return(MG_ETYPE); |
223 |
+ |
c_cmaterial->nr = atof(av[1]); |
224 |
+ |
c_cmaterial->ni = atof(av[2]); |
225 |
+ |
if (c_cmaterial->nr <= FTINY) |
226 |
+ |
return(MG_EILL); |
227 |
+ |
c_cmaterial->clock++; |
228 |
+ |
return(MG_OK); |
229 |
|
case MG_E_RD: /* set diffuse reflectance */ |
230 |
|
if (ac != 2) |
231 |
|
return(MG_EARGC); |
232 |
|
if (!isflt(av[1])) |
233 |
|
return(MG_ETYPE); |
234 |
|
c_cmaterial->rd = atof(av[1]); |
235 |
< |
if (c_cmaterial->rd < 0. | c_cmaterial->rd > 1.) |
235 |
> |
if ((c_cmaterial->rd < 0.) | (c_cmaterial->rd > 1.)) |
236 |
|
return(MG_EILL); |
237 |
|
c_cmaterial->rd_c = *c_ccolor; |
238 |
|
c_cmaterial->clock++; |
254 |
|
if (!isflt(av[1])) |
255 |
|
return(MG_ETYPE); |
256 |
|
c_cmaterial->td = atof(av[1]); |
257 |
< |
if (c_cmaterial->td < 0. | c_cmaterial->td > 1.) |
257 |
> |
if ((c_cmaterial->td < 0.) | (c_cmaterial->td > 1.)) |
258 |
|
return(MG_EILL); |
259 |
|
c_cmaterial->td_c = *c_ccolor; |
260 |
|
c_cmaterial->clock++; |
262 |
|
case MG_E_RS: /* set specular reflectance */ |
263 |
|
if (ac != 3) |
264 |
|
return(MG_EARGC); |
265 |
< |
if (!isflt(av[1]) || !isflt(av[2])) |
265 |
> |
if (!isflt(av[1]) | !isflt(av[2])) |
266 |
|
return(MG_ETYPE); |
267 |
|
c_cmaterial->rs = atof(av[1]); |
268 |
|
c_cmaterial->rs_a = atof(av[2]); |
269 |
< |
if (c_cmaterial->rs < 0. | c_cmaterial->rs > 1. | |
270 |
< |
c_cmaterial->rs_a < 0.) |
269 |
> |
if ((c_cmaterial->rs < 0.) | (c_cmaterial->rs > 1.) | |
270 |
> |
(c_cmaterial->rs_a < 0.)) |
271 |
|
return(MG_EILL); |
272 |
|
c_cmaterial->rs_c = *c_ccolor; |
273 |
|
c_cmaterial->clock++; |
275 |
|
case MG_E_TS: /* set specular transmittance */ |
276 |
|
if (ac != 3) |
277 |
|
return(MG_EARGC); |
278 |
< |
if (!isflt(av[1]) || !isflt(av[2])) |
278 |
> |
if (!isflt(av[1]) | !isflt(av[2])) |
279 |
|
return(MG_ETYPE); |
280 |
|
c_cmaterial->ts = atof(av[1]); |
281 |
|
c_cmaterial->ts_a = atof(av[2]); |
282 |
< |
if (c_cmaterial->ts < 0. | c_cmaterial->ts > 1. | |
283 |
< |
c_cmaterial->ts_a < 0.) |
282 |
> |
if ((c_cmaterial->ts < 0.) | (c_cmaterial->ts > 1.) | |
283 |
> |
(c_cmaterial->ts_a < 0.)) |
284 |
|
return(MG_EILL); |
285 |
|
c_cmaterial->ts_c = *c_ccolor; |
286 |
|
c_cmaterial->clock++; |
322 |
|
c_cvname = NULL; |
323 |
|
return(MG_OK); |
324 |
|
} |
325 |
+ |
if (!isname(av[1])) |
326 |
+ |
return(MG_EILL); |
327 |
|
lp = lu_find(&vtx_tab, av[1]); /* lookup context */ |
328 |
|
if (lp == NULL) |
329 |
|
return(MG_EMEM); |
346 |
|
return(MG_EMEM); |
347 |
|
c_cvname = lp->key; |
348 |
|
c_cvertex = (C_VERTEX *)lp->data; |
349 |
+ |
c_cvertex->clock = 0; |
350 |
+ |
c_cvertex->client_data = NULL; |
351 |
|
} |
352 |
|
i = c_cvertex->clock; |
353 |
|
if (ac == 3) { /* use default template */ |
366 |
|
case MG_E_POINT: /* set point */ |
367 |
|
if (ac != 4) |
368 |
|
return(MG_EARGC); |
369 |
< |
if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3])) |
369 |
> |
if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3])) |
370 |
|
return(MG_ETYPE); |
371 |
|
c_cvertex->p[0] = atof(av[1]); |
372 |
|
c_cvertex->p[1] = atof(av[2]); |
376 |
|
case MG_E_NORMAL: /* set normal */ |
377 |
|
if (ac != 4) |
378 |
|
return(MG_EARGC); |
379 |
< |
if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3])) |
379 |
> |
if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3])) |
380 |
|
return(MG_ETYPE); |
381 |
|
c_cvertex->n[0] = atof(av[1]); |
382 |
|
c_cvertex->n[1] = atof(av[2]); |
443 |
|
} |
444 |
|
|
445 |
|
|
428 |
– |
int |
429 |
– |
c_isgrey(clr) /* check if color is grey */ |
430 |
– |
register C_COLOR *clr; |
431 |
– |
{ |
432 |
– |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) |
433 |
– |
return(1); /* no settings == grey */ |
434 |
– |
c_ccvt(clr, C_CSXY); |
435 |
– |
return(clr->cx >= .323 && clr->cx <= .343 && |
436 |
– |
clr->cy >= .323 && clr->cy <= .343); |
437 |
– |
} |
438 |
– |
|
439 |
– |
|
440 |
– |
void |
441 |
– |
c_ccvt(clr, fl) /* convert color representations */ |
442 |
– |
register C_COLOR *clr; |
443 |
– |
int fl; |
444 |
– |
{ |
445 |
– |
double x, y, z; |
446 |
– |
register int i; |
447 |
– |
|
448 |
– |
if (clr->flags & fl) /* already done */ |
449 |
– |
return; |
450 |
– |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) /* nothing set! */ |
451 |
– |
*clr = c_dfcolor; |
452 |
– |
else if (fl & C_CSXY) { /* cspec -> cxy */ |
453 |
– |
x = y = z = 0.; |
454 |
– |
for (i = 0; i < C_CNSS; i++) { |
455 |
– |
x += cie_xf.ssamp[i] * clr->ssamp[i]; |
456 |
– |
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
457 |
– |
z += cie_zf.ssamp[i] * clr->ssamp[i]; |
458 |
– |
} |
459 |
– |
z += x + y; |
460 |
– |
clr->cx = x / z; |
461 |
– |
clr->cy = y / z; |
462 |
– |
clr->flags |= C_CSXY; |
463 |
– |
} else { /* cxy -> cspec */ |
464 |
– |
z = (cie_xf.ssum + cie_yf.ssum + cie_zf.ssum) / 3.; |
465 |
– |
x = clr->cx * z / cie_xf.ssum; |
466 |
– |
y = clr->cy * z / cie_yf.ssum; |
467 |
– |
z = (1. - clr->cx - clr->cy) * z / cie_zf.ssum; |
468 |
– |
clr->ssum = 0; |
469 |
– |
for (i = 0; i < C_CNSS; i++) |
470 |
– |
clr->ssum += clr->ssamp[i] = |
471 |
– |
x * cie_xf.ssamp[i] + |
472 |
– |
y * cie_yf.ssamp[i] + |
473 |
– |
z * cie_zf.ssamp[i] ; |
474 |
– |
clr->flags |= C_CSSPEC; |
475 |
– |
} |
476 |
– |
} |
477 |
– |
|
478 |
– |
|
446 |
|
static int |
447 |
|
setspectrum(clr, wlmin, wlmax, ac, av) /* convert a spectrum */ |
448 |
|
register C_COLOR *clr; |
449 |
< |
int wlmin, wlmax; |
449 |
> |
double wlmin, wlmax; |
450 |
|
int ac; |
451 |
|
char **av; |
452 |
|
{ |
453 |
|
double scale; |
454 |
< |
float *va; |
455 |
< |
register int i; |
456 |
< |
int wl, pos; |
454 |
> |
float va[C_CNSS]; |
455 |
> |
register int i, pos; |
456 |
> |
int n, imax; |
457 |
> |
int wl; |
458 |
|
double wl0, wlstep; |
459 |
< |
|
460 |
< |
if (wlmin < C_CMINWL || wlmin >= wlmax || wlmax > C_CMAXWL) |
459 |
> |
double boxpos, boxstep; |
460 |
> |
/* check bounds */ |
461 |
> |
if ((wlmax <= C_CMINWL) | (wlmax <= wlmin) | (wlmin >= C_CMAXWL)) |
462 |
|
return(MG_EILL); |
463 |
< |
if ((va = (float *)malloc(ac*sizeof(float))) == NULL) |
464 |
< |
return(MG_EMEM); |
463 |
> |
wlstep = (wlmax - wlmin)/(ac-1); |
464 |
> |
while (wlmin < C_CMINWL) { |
465 |
> |
wlmin += wlstep; |
466 |
> |
ac--; av++; |
467 |
> |
} |
468 |
> |
while (wlmax > C_CMAXWL) { |
469 |
> |
wlmax -= wlstep; |
470 |
> |
ac--; |
471 |
> |
} |
472 |
> |
imax = ac; /* box filter if necessary */ |
473 |
> |
boxpos = 0; |
474 |
> |
boxstep = 1; |
475 |
> |
if (wlstep < C_CWLI) { |
476 |
> |
imax = (wlmax - wlmin)/C_CWLI + (1-FTINY); |
477 |
> |
boxpos = (wlmin - C_CMINWL)/C_CWLI; |
478 |
> |
boxstep = wlstep/C_CWLI; |
479 |
> |
wlstep = C_CWLI; |
480 |
> |
} |
481 |
|
scale = 0.; /* get values and maximum */ |
482 |
< |
for (i = 0; i < ac; i++) { |
483 |
< |
if (!isflt(av[i])) |
484 |
< |
return(MG_ETYPE); |
485 |
< |
va[i] = atof(av[i]); |
486 |
< |
if (va[i] < 0.) |
487 |
< |
return(MG_EILL); |
482 |
> |
pos = 0; |
483 |
> |
for (i = 0; i < imax; i++) { |
484 |
> |
va[i] = 0.; n = 0; |
485 |
> |
while (boxpos < i+.5 && pos < ac) { |
486 |
> |
if (!isflt(av[pos])) |
487 |
> |
return(MG_ETYPE); |
488 |
> |
va[i] += atof(av[pos++]); |
489 |
> |
n++; |
490 |
> |
boxpos += boxstep; |
491 |
> |
} |
492 |
> |
if (n > 1) |
493 |
> |
va[i] /= (double)n; |
494 |
|
if (va[i] > scale) |
495 |
|
scale = va[i]; |
496 |
+ |
else if (va[i] < -scale) |
497 |
+ |
scale = -va[i]; |
498 |
|
} |
499 |
< |
if (scale == 0.) |
499 |
> |
if (scale <= FTINY) |
500 |
|
return(MG_EILL); |
501 |
|
scale = C_CMAXV / scale; |
502 |
|
clr->ssum = 0; /* convert to our spacing */ |
503 |
|
wl0 = wlmin; |
511 |
– |
wlstep = (double)(wlmax - wlmin)/(ac-1); |
504 |
|
pos = 0; |
505 |
|
for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI) |
506 |
< |
if (wl < wlmin || wl > wlmax) |
506 |
> |
if ((wl < wlmin) | (wl > wlmax)) |
507 |
|
clr->ssamp[i] = 0; |
508 |
|
else { |
509 |
|
while (wl0 + wlstep < wl+FTINY) { |
510 |
|
wl0 += wlstep; |
511 |
|
pos++; |
512 |
|
} |
513 |
< |
if (wl+FTINY >= wl0 && wl-FTINY <= wl0) |
514 |
< |
clr->ssamp[i] = scale*va[pos]; |
513 |
> |
if ((wl+FTINY >= wl0) & (wl-FTINY <= wl0)) |
514 |
> |
clr->ssamp[i] = scale*va[pos] + .5; |
515 |
|
else /* interpolate if necessary */ |
516 |
< |
clr->ssamp[i] = scale / wlstep * |
516 |
> |
clr->ssamp[i] = .5 + scale / wlstep * |
517 |
|
( va[pos]*(wl0+wlstep - wl) + |
518 |
|
va[pos+1]*(wl - wl0) ); |
519 |
|
clr->ssum += clr->ssamp[i]; |
520 |
|
} |
521 |
|
clr->flags = C_CDSPEC|C_CSSPEC; |
522 |
|
clr->clock++; |
531 |
– |
free((MEM_PTR)va); |
523 |
|
return(MG_OK); |
533 |
– |
} |
534 |
– |
|
535 |
– |
|
536 |
– |
static void |
537 |
– |
mixcolors(cres, w1, c1, w2, c2) /* mix two colors according to weights given */ |
538 |
– |
register C_COLOR *cres, *c1, *c2; |
539 |
– |
double w1, w2; |
540 |
– |
{ |
541 |
– |
double scale; |
542 |
– |
float cmix[C_CNSS]; |
543 |
– |
register int i; |
544 |
– |
|
545 |
– |
if ((c1->flags|c2->flags) & C_CDSPEC) { /* spectral mixing */ |
546 |
– |
c_ccvt(c1, C_CSSPEC); |
547 |
– |
c_ccvt(c2, C_CSSPEC); |
548 |
– |
w1 /= (double)c1->ssum; |
549 |
– |
w2 /= (double)c2->ssum; |
550 |
– |
scale = 0.; |
551 |
– |
for (i = 0; i < C_CNSS; i++) { |
552 |
– |
cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i]; |
553 |
– |
if (cmix[i] > scale) |
554 |
– |
scale = cmix[i]; |
555 |
– |
} |
556 |
– |
scale = C_CMAXV / scale; |
557 |
– |
cres->ssum = 0; |
558 |
– |
for (i = 0; i < C_CNSS; i++) |
559 |
– |
cres->ssum += cres->ssamp[i] = scale*cmix[i] + .5; |
560 |
– |
cres->flags = C_CDSPEC|C_CSSPEC; |
561 |
– |
} else { /* CIE xy mixing */ |
562 |
– |
c_ccvt(c1, C_CSXY); |
563 |
– |
c_ccvt(c2, C_CSXY); |
564 |
– |
scale = 1. / (w1/c1->cy + w2/c2->cy); |
565 |
– |
cres->cx = (c1->cx*w1/c1->cy + c2->cx*w2/c2->cy) * scale; |
566 |
– |
cres->cy = (w1 + w2) * scale; |
567 |
– |
cres->flags = C_CDXY|C_CSXY; |
568 |
– |
} |
524 |
|
} |