| 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 |
|
|
| 35 |
< |
static int setspectrum(C_COLOR *, double wlmin, double wlmax, |
| 36 |
< |
int ac, char **av); |
| 35 |
> |
static int setspectrum(C_COLOR *, int ac, char **av); |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
int |
| 39 |
|
c_hcolor(int ac, char **av) /* handle color entity */ |
| 40 |
|
{ |
| 41 |
|
double w, wsum; |
| 42 |
< |
register int i; |
| 43 |
< |
register LUENT *lp; |
| 42 |
> |
int i; |
| 43 |
> |
LUENT *lp; |
| 44 |
|
|
| 45 |
|
switch (mg_entity(av[0])) { |
| 46 |
|
case MG_E_COLOR: /* get/set color context */ |
| 59 |
|
return(MG_EMEM); |
| 60 |
|
c_ccname = lp->key; |
| 61 |
|
c_ccolor = (C_COLOR *)lp->data; |
| 62 |
< |
if (ac == 2) { /* reestablish previous context */ |
| 63 |
< |
if (c_ccolor == NULL) |
| 62 |
> |
if (ac == 2 || (ac == 4 && !strcmp(av[1], av[3]))) { |
| 63 |
> |
if (c_ccolor == NULL) /* reestablish context */ |
| 64 |
|
return(MG_EUNDEF); |
| 65 |
|
return(MG_OK); |
| 66 |
|
} |
| 107 |
|
c_ccolor->clock++; |
| 108 |
|
return(MG_OK); |
| 109 |
|
case MG_E_CSPEC: /* assign spectral values */ |
| 110 |
< |
if (ac < 5) |
| 112 |
< |
return(MG_EARGC); |
| 113 |
< |
if (!isflt(av[1]) | !isflt(av[2])) |
| 114 |
< |
return(MG_ETYPE); |
| 115 |
< |
return(setspectrum(c_ccolor, atof(av[1]), atof(av[2]), |
| 116 |
< |
ac-3, av+3)); |
| 110 |
> |
return(setspectrum(c_ccolor, ac-1, av+1)); |
| 111 |
|
case MG_E_CCT: /* assign black body spectrum */ |
| 112 |
|
if (ac != 2) |
| 113 |
|
return(MG_EARGC); |
| 153 |
|
c_hmaterial(int ac, char **av) /* handle material entity */ |
| 154 |
|
{ |
| 155 |
|
int i; |
| 156 |
< |
register LUENT *lp; |
| 156 |
> |
LUENT *lp; |
| 157 |
|
|
| 158 |
|
switch (mg_entity(av[0])) { |
| 159 |
|
case MG_E_MATERIAL: /* get/set material context */ |
| 172 |
|
return(MG_EMEM); |
| 173 |
|
c_cmname = lp->key; |
| 174 |
|
c_cmaterial = (C_MATERIAL *)lp->data; |
| 175 |
< |
if (ac == 2) { /* reestablish previous context */ |
| 176 |
< |
if (c_cmaterial == NULL) |
| 175 |
> |
if (ac == 2 || (ac == 4 && !strcmp(av[1], av[3]))) { |
| 176 |
> |
if (c_cmaterial == NULL) /* reestablish context */ |
| 177 |
|
return(MG_EUNDEF); |
| 178 |
|
return(MG_OK); |
| 179 |
|
} |
| 299 |
|
c_hvertex(int ac, char **av) /* handle a vertex entity */ |
| 300 |
|
{ |
| 301 |
|
int i; |
| 302 |
< |
register LUENT *lp; |
| 302 |
> |
LUENT *lp; |
| 303 |
|
|
| 304 |
|
switch (mg_entity(av[0])) { |
| 305 |
|
case MG_E_VERTEX: /* get/set vertex context */ |
| 318 |
|
return(MG_EMEM); |
| 319 |
|
c_cvname = lp->key; |
| 320 |
|
c_cvertex = (C_VERTEX *)lp->data; |
| 321 |
< |
if (ac == 2) { /* reestablish previous context */ |
| 322 |
< |
if (c_cvertex == NULL) |
| 321 |
> |
if (ac == 2 || (ac == 4 && !strcmp(av[1], av[3]))) { |
| 322 |
> |
if (c_cvertex == NULL) /* reestablish context */ |
| 323 |
|
return(MG_EUNDEF); |
| 324 |
|
return(MG_OK); |
| 325 |
|
} |
| 399 |
|
C_MATERIAL * |
| 400 |
|
c_getmaterial(char *name) /* get a named material */ |
| 401 |
|
{ |
| 402 |
< |
register LUENT *lp; |
| 402 |
> |
LUENT *lp; |
| 403 |
|
|
| 404 |
|
if ((lp = lu_find(&mat_tab, name)) == NULL) |
| 405 |
|
return(NULL); |
| 410 |
|
C_VERTEX * |
| 411 |
|
c_getvert(char *name) /* get a named vertex */ |
| 412 |
|
{ |
| 413 |
< |
register LUENT *lp; |
| 413 |
> |
LUENT *lp; |
| 414 |
|
|
| 415 |
|
if ((lp = lu_find(&vtx_tab, name)) == NULL) |
| 416 |
|
return(NULL); |
| 421 |
|
C_COLOR * |
| 422 |
|
c_getcolor(char *name) /* get a named color */ |
| 423 |
|
{ |
| 424 |
< |
register LUENT *lp; |
| 424 |
> |
LUENT *lp; |
| 425 |
|
|
| 426 |
|
if ((lp = lu_find(&clr_tab, name)) == NULL) |
| 427 |
|
return(NULL); |
| 430 |
|
|
| 431 |
|
|
| 432 |
|
static int |
| 433 |
< |
setspectrum( /* convert a spectrum */ |
| 434 |
< |
register C_COLOR *clr, |
| 441 |
< |
double wlmin, |
| 442 |
< |
double wlmax, |
| 433 |
> |
setspectrum( /* convert a spectrum */ |
| 434 |
> |
C_COLOR *clr, |
| 435 |
|
int ac, |
| 436 |
|
char **av |
| 437 |
|
) |
| 438 |
|
{ |
| 439 |
< |
double scale; |
| 440 |
< |
float va[C_CNSS]; |
| 441 |
< |
register int i, pos; |
| 442 |
< |
int n, imax; |
| 443 |
< |
int wl; |
| 444 |
< |
double wl0, wlstep; |
| 445 |
< |
double boxpos, boxstep; |
| 446 |
< |
/* check bounds */ |
| 447 |
< |
if ((wlmax <= C_CMINWL) | (wlmax <= wlmin) | (wlmin >= C_CMAXWL)) |
| 448 |
< |
return(MG_EILL); |
| 449 |
< |
wlstep = (wlmax - wlmin)/(ac-1); |
| 450 |
< |
while (wlmin < C_CMINWL) { |
| 451 |
< |
wlmin += wlstep; |
| 452 |
< |
ac--; av++; |
| 439 |
> |
double wlmin, wlmax; |
| 440 |
> |
int i; |
| 441 |
> |
float *va; |
| 442 |
> |
/* get bounds */ |
| 443 |
> |
if (ac < 4) |
| 444 |
> |
return(MG_EARGC); |
| 445 |
> |
if (!isflt(av[0]) || !isflt(av[1])) |
| 446 |
> |
return(MG_ETYPE); |
| 447 |
> |
wlmin = atof(av[0]); |
| 448 |
> |
wlmax = atof(av[1]); |
| 449 |
> |
ac -= 2; av += 2; |
| 450 |
> |
va = (float *)malloc(sizeof(float)*ac); |
| 451 |
> |
if (va == NULL) |
| 452 |
> |
return(MG_EMEM); |
| 453 |
> |
for (i = ac; i--; ) { |
| 454 |
> |
if (!isflt(av[i])) |
| 455 |
> |
return(MG_ETYPE); |
| 456 |
> |
va[i] = atof(av[i]); |
| 457 |
|
} |
| 458 |
< |
while (wlmax > C_CMAXWL) { |
| 459 |
< |
wlmax -= wlstep; |
| 464 |
< |
ac--; |
| 465 |
< |
} |
| 466 |
< |
imax = ac; /* box filter if necessary */ |
| 467 |
< |
boxpos = 0; |
| 468 |
< |
boxstep = 1; |
| 469 |
< |
if (wlstep < C_CWLI) { |
| 470 |
< |
imax = (wlmax - wlmin)/C_CWLI + (1-FTINY); |
| 471 |
< |
boxpos = (wlmin - C_CMINWL)/C_CWLI; |
| 472 |
< |
boxstep = wlstep/C_CWLI; |
| 473 |
< |
wlstep = C_CWLI; |
| 474 |
< |
} |
| 475 |
< |
scale = 0.; /* get values and maximum */ |
| 476 |
< |
pos = 0; |
| 477 |
< |
for (i = 0; i < imax; i++) { |
| 478 |
< |
va[i] = 0.; n = 0; |
| 479 |
< |
while (boxpos < i+.5 && pos < ac) { |
| 480 |
< |
if (!isflt(av[pos])) |
| 481 |
< |
return(MG_ETYPE); |
| 482 |
< |
va[i] += atof(av[pos++]); |
| 483 |
< |
n++; |
| 484 |
< |
boxpos += boxstep; |
| 485 |
< |
} |
| 486 |
< |
if (n > 1) |
| 487 |
< |
va[i] /= (double)n; |
| 488 |
< |
if (va[i] > scale) |
| 489 |
< |
scale = va[i]; |
| 490 |
< |
else if (va[i] < -scale) |
| 491 |
< |
scale = -va[i]; |
| 492 |
< |
} |
| 493 |
< |
if (scale <= FTINY) |
| 458 |
> |
if (c_sset(clr, wlmin, wlmax, va, ac) <= FTINY) { |
| 459 |
> |
free(va); |
| 460 |
|
return(MG_EILL); |
| 461 |
< |
scale = C_CMAXV / scale; |
| 462 |
< |
clr->ssum = 0; /* convert to our spacing */ |
| 497 |
< |
wl0 = wlmin; |
| 498 |
< |
pos = 0; |
| 499 |
< |
for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI) |
| 500 |
< |
if ((wl < wlmin) | (wl > wlmax)) |
| 501 |
< |
clr->ssamp[i] = 0; |
| 502 |
< |
else { |
| 503 |
< |
while (wl0 + wlstep < wl+FTINY) { |
| 504 |
< |
wl0 += wlstep; |
| 505 |
< |
pos++; |
| 506 |
< |
} |
| 507 |
< |
if ((wl+FTINY >= wl0) & (wl-FTINY <= wl0)) |
| 508 |
< |
clr->ssamp[i] = scale*va[pos] + .5; |
| 509 |
< |
else /* interpolate if necessary */ |
| 510 |
< |
clr->ssamp[i] = .5 + scale / wlstep * |
| 511 |
< |
( va[pos]*(wl0+wlstep - wl) + |
| 512 |
< |
va[pos+1]*(wl - wl0) ); |
| 513 |
< |
clr->ssum += clr->ssamp[i]; |
| 514 |
< |
} |
| 515 |
< |
clr->flags = C_CDSPEC|C_CSSPEC; |
| 461 |
> |
} |
| 462 |
> |
free(va); |
| 463 |
|
clr->clock++; |
| 464 |
|
return(MG_OK); |
| 465 |
|
} |