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