1 |
greg |
3.1 |
#ifndef lint |
2 |
|
|
static const char RCSid[] = "$Id: context.c,v 1.30 2011/02/03 19:36:10 greg Exp $"; |
3 |
|
|
#endif |
4 |
|
|
/* |
5 |
|
|
* Context handlers |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include <stdio.h> |
9 |
|
|
#include <stdlib.h> |
10 |
|
|
#include <string.h> |
11 |
|
|
#include "mgf_parser.h" |
12 |
|
|
#include "lookup.h" |
13 |
|
|
|
14 |
|
|
/* default context values */ |
15 |
|
|
static C_MATERIAL c_dfmaterial = C_DEFMATERIAL; |
16 |
|
|
static C_VERTEX c_dfvertex = C_DEFVERTEX; |
17 |
|
|
|
18 |
|
|
/* the unnamed contexts */ |
19 |
|
|
static C_COLOR c_uncolor = C_DEFCOLOR; |
20 |
|
|
static C_MATERIAL c_unmaterial = C_DEFMATERIAL; |
21 |
|
|
static C_VERTEX c_unvertex = C_DEFVERTEX; |
22 |
|
|
|
23 |
|
|
/* the current contexts */ |
24 |
|
|
C_COLOR *c_ccolor = &c_uncolor; |
25 |
|
|
char *c_ccname = NULL; |
26 |
|
|
C_MATERIAL *c_cmaterial = &c_unmaterial; |
27 |
|
|
char *c_cmname = NULL; |
28 |
|
|
C_VERTEX *c_cvertex = &c_unvertex; |
29 |
|
|
char *c_cvname = NULL; |
30 |
|
|
|
31 |
|
|
static LUTAB clr_tab = LU_SINIT(free,free); /* color lookup table */ |
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); |
37 |
|
|
|
38 |
|
|
|
39 |
|
|
int |
40 |
|
|
c_hcolor(int ac, char **av) /* handle color entity */ |
41 |
|
|
{ |
42 |
|
|
double w, wsum; |
43 |
|
|
register int i; |
44 |
|
|
register LUENT *lp; |
45 |
|
|
|
46 |
|
|
switch (mg_entity(av[0])) { |
47 |
|
|
case MG_E_COLOR: /* get/set color context */ |
48 |
|
|
if (ac > 4) |
49 |
|
|
return(MG_EARGC); |
50 |
|
|
if (ac == 1) { /* set unnamed color context */ |
51 |
|
|
c_uncolor = c_dfcolor; |
52 |
|
|
c_ccolor = &c_uncolor; |
53 |
|
|
c_ccname = NULL; |
54 |
|
|
return(MG_OK); |
55 |
|
|
} |
56 |
|
|
if (!isname(av[1])) |
57 |
|
|
return(MG_EILL); |
58 |
|
|
lp = lu_find(&clr_tab, av[1]); /* lookup context */ |
59 |
|
|
if (lp == NULL) |
60 |
|
|
return(MG_EMEM); |
61 |
|
|
c_ccname = lp->key; |
62 |
|
|
c_ccolor = (C_COLOR *)lp->data; |
63 |
|
|
if (ac == 2) { /* reestablish previous context */ |
64 |
|
|
if (c_ccolor == NULL) |
65 |
|
|
return(MG_EUNDEF); |
66 |
|
|
return(MG_OK); |
67 |
|
|
} |
68 |
|
|
if (av[2][0] != '=' || av[2][1]) |
69 |
|
|
return(MG_ETYPE); |
70 |
|
|
if (c_ccolor == NULL) { /* create new color context */ |
71 |
|
|
lp->key = (char *)malloc(strlen(av[1])+1); |
72 |
|
|
if (lp->key == NULL) |
73 |
|
|
return(MG_EMEM); |
74 |
|
|
strcpy(lp->key, av[1]); |
75 |
|
|
lp->data = (char *)malloc(sizeof(C_COLOR)); |
76 |
|
|
if (lp->data == NULL) |
77 |
|
|
return(MG_EMEM); |
78 |
|
|
c_ccname = lp->key; |
79 |
|
|
c_ccolor = (C_COLOR *)lp->data; |
80 |
|
|
c_ccolor->clock = 0; |
81 |
|
|
c_ccolor->client_data = NULL; |
82 |
|
|
} |
83 |
|
|
i = c_ccolor->clock; |
84 |
|
|
if (ac == 3) { /* use default template */ |
85 |
|
|
*c_ccolor = c_dfcolor; |
86 |
|
|
c_ccolor->clock = i + 1; |
87 |
|
|
return(MG_OK); |
88 |
|
|
} |
89 |
|
|
lp = lu_find(&clr_tab, av[3]); /* lookup template */ |
90 |
|
|
if (lp == NULL) |
91 |
|
|
return(MG_EMEM); |
92 |
|
|
if (lp->data == NULL) |
93 |
|
|
return(MG_EUNDEF); |
94 |
|
|
*c_ccolor = *(C_COLOR *)lp->data; |
95 |
|
|
c_ccolor->clock = i + 1; |
96 |
|
|
return(MG_OK); |
97 |
|
|
case MG_E_CXY: /* assign CIE XY value */ |
98 |
|
|
if (ac != 3) |
99 |
|
|
return(MG_EARGC); |
100 |
|
|
if (!isflt(av[1]) | !isflt(av[2])) |
101 |
|
|
return(MG_ETYPE); |
102 |
|
|
c_ccolor->cx = atof(av[1]); |
103 |
|
|
c_ccolor->cy = atof(av[2]); |
104 |
|
|
c_ccolor->flags = C_CDXY|C_CSXY; |
105 |
|
|
if ((c_ccolor->cx < 0.) | (c_ccolor->cy < 0.) | |
106 |
|
|
(c_ccolor->cx + c_ccolor->cy > 1.)) |
107 |
|
|
return(MG_EILL); |
108 |
|
|
c_ccolor->clock++; |
109 |
|
|
return(MG_OK); |
110 |
|
|
case MG_E_CSPEC: /* assign spectral values */ |
111 |
|
|
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)); |
117 |
|
|
case MG_E_CCT: /* assign black body spectrum */ |
118 |
|
|
if (ac != 2) |
119 |
|
|
return(MG_EARGC); |
120 |
|
|
if (!isflt(av[1])) |
121 |
|
|
return(MG_ETYPE); |
122 |
|
|
if (!c_bbtemp(c_ccolor, atof(av[1]))) |
123 |
|
|
return(MG_EILL); |
124 |
|
|
c_ccolor->clock++; |
125 |
|
|
return(MG_OK); |
126 |
|
|
case MG_E_CMIX: /* mix colors */ |
127 |
|
|
if (ac < 5 || (ac-1)%2) |
128 |
|
|
return(MG_EARGC); |
129 |
|
|
if (!isflt(av[1])) |
130 |
|
|
return(MG_ETYPE); |
131 |
|
|
wsum = atof(av[1]); |
132 |
|
|
if ((lp = lu_find(&clr_tab, av[2])) == NULL) |
133 |
|
|
return(MG_EMEM); |
134 |
|
|
if (lp->data == NULL) |
135 |
|
|
return(MG_EUNDEF); |
136 |
|
|
*c_ccolor = *(C_COLOR *)lp->data; |
137 |
|
|
for (i = 3; i < ac; i += 2) { |
138 |
|
|
if (!isflt(av[i])) |
139 |
|
|
return(MG_ETYPE); |
140 |
|
|
w = atof(av[i]); |
141 |
|
|
if ((lp = lu_find(&clr_tab, av[i+1])) == NULL) |
142 |
|
|
return(MG_EMEM); |
143 |
|
|
if (lp->data == NULL) |
144 |
|
|
return(MG_EUNDEF); |
145 |
|
|
c_cmix(c_ccolor, wsum, c_ccolor, |
146 |
|
|
w, (C_COLOR *)lp->data); |
147 |
|
|
wsum += w; |
148 |
|
|
} |
149 |
|
|
if (wsum <= 0.) |
150 |
|
|
return(MG_EILL); |
151 |
|
|
c_ccolor->clock++; |
152 |
|
|
return(MG_OK); |
153 |
|
|
} |
154 |
|
|
return(MG_EUNK); |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
|
158 |
|
|
int |
159 |
|
|
c_hmaterial(int ac, char **av) /* handle material entity */ |
160 |
|
|
{ |
161 |
|
|
int i; |
162 |
|
|
register LUENT *lp; |
163 |
|
|
|
164 |
|
|
switch (mg_entity(av[0])) { |
165 |
|
|
case MG_E_MATERIAL: /* get/set material context */ |
166 |
|
|
if (ac > 4) |
167 |
|
|
return(MG_EARGC); |
168 |
|
|
if (ac == 1) { /* set unnamed material context */ |
169 |
|
|
c_unmaterial = c_dfmaterial; |
170 |
|
|
c_cmaterial = &c_unmaterial; |
171 |
|
|
c_cmname = NULL; |
172 |
|
|
return(MG_OK); |
173 |
|
|
} |
174 |
|
|
if (!isname(av[1])) |
175 |
|
|
return(MG_EILL); |
176 |
|
|
lp = lu_find(&mat_tab, av[1]); /* lookup context */ |
177 |
|
|
if (lp == NULL) |
178 |
|
|
return(MG_EMEM); |
179 |
|
|
c_cmname = lp->key; |
180 |
|
|
c_cmaterial = (C_MATERIAL *)lp->data; |
181 |
|
|
if (ac == 2) { /* reestablish previous context */ |
182 |
|
|
if (c_cmaterial == NULL) |
183 |
|
|
return(MG_EUNDEF); |
184 |
|
|
return(MG_OK); |
185 |
|
|
} |
186 |
|
|
if (av[2][0] != '=' || av[2][1]) |
187 |
|
|
return(MG_ETYPE); |
188 |
|
|
if (c_cmaterial == NULL) { /* create new material */ |
189 |
|
|
lp->key = (char *)malloc(strlen(av[1])+1); |
190 |
|
|
if (lp->key == NULL) |
191 |
|
|
return(MG_EMEM); |
192 |
|
|
strcpy(lp->key, av[1]); |
193 |
|
|
lp->data = (char *)malloc(sizeof(C_MATERIAL)); |
194 |
|
|
if (lp->data == NULL) |
195 |
|
|
return(MG_EMEM); |
196 |
|
|
c_cmname = lp->key; |
197 |
|
|
c_cmaterial = (C_MATERIAL *)lp->data; |
198 |
|
|
c_cmaterial->clock = 0; |
199 |
|
|
c_cmaterial->client_data = NULL; |
200 |
|
|
} |
201 |
|
|
i = c_cmaterial->clock; |
202 |
|
|
if (ac == 3) { /* use default template */ |
203 |
|
|
*c_cmaterial = c_dfmaterial; |
204 |
|
|
c_cmaterial->clock = i + 1; |
205 |
|
|
return(MG_OK); |
206 |
|
|
} |
207 |
|
|
lp = lu_find(&mat_tab, av[3]); /* lookup template */ |
208 |
|
|
if (lp == NULL) |
209 |
|
|
return(MG_EMEM); |
210 |
|
|
if (lp->data == NULL) |
211 |
|
|
return(MG_EUNDEF); |
212 |
|
|
*c_cmaterial = *(C_MATERIAL *)lp->data; |
213 |
|
|
c_cmaterial->clock = i + 1; |
214 |
|
|
return(MG_OK); |
215 |
|
|
case MG_E_IR: /* set index of refraction */ |
216 |
|
|
if (ac != 3) |
217 |
|
|
return(MG_EARGC); |
218 |
|
|
if (!isflt(av[1]) | !isflt(av[2])) |
219 |
|
|
return(MG_ETYPE); |
220 |
|
|
c_cmaterial->nr = atof(av[1]); |
221 |
|
|
c_cmaterial->ni = atof(av[2]); |
222 |
|
|
if (c_cmaterial->nr <= FTINY) |
223 |
|
|
return(MG_EILL); |
224 |
|
|
c_cmaterial->clock++; |
225 |
|
|
return(MG_OK); |
226 |
|
|
case MG_E_RD: /* set diffuse reflectance */ |
227 |
|
|
if (ac != 2) |
228 |
|
|
return(MG_EARGC); |
229 |
|
|
if (!isflt(av[1])) |
230 |
|
|
return(MG_ETYPE); |
231 |
|
|
c_cmaterial->rd = atof(av[1]); |
232 |
|
|
if ((c_cmaterial->rd < 0.) | (c_cmaterial->rd > 1.)) |
233 |
|
|
return(MG_EILL); |
234 |
|
|
c_cmaterial->rd_c = *c_ccolor; |
235 |
|
|
c_cmaterial->clock++; |
236 |
|
|
return(MG_OK); |
237 |
|
|
case MG_E_ED: /* set diffuse emittance */ |
238 |
|
|
if (ac != 2) |
239 |
|
|
return(MG_EARGC); |
240 |
|
|
if (!isflt(av[1])) |
241 |
|
|
return(MG_ETYPE); |
242 |
|
|
c_cmaterial->ed = atof(av[1]); |
243 |
|
|
if (c_cmaterial->ed < 0.) |
244 |
|
|
return(MG_EILL); |
245 |
|
|
c_cmaterial->ed_c = *c_ccolor; |
246 |
|
|
c_cmaterial->clock++; |
247 |
|
|
return(MG_OK); |
248 |
|
|
case MG_E_TD: /* set diffuse transmittance */ |
249 |
|
|
if (ac != 2) |
250 |
|
|
return(MG_EARGC); |
251 |
|
|
if (!isflt(av[1])) |
252 |
|
|
return(MG_ETYPE); |
253 |
|
|
c_cmaterial->td = atof(av[1]); |
254 |
|
|
if ((c_cmaterial->td < 0.) | (c_cmaterial->td > 1.)) |
255 |
|
|
return(MG_EILL); |
256 |
|
|
c_cmaterial->td_c = *c_ccolor; |
257 |
|
|
c_cmaterial->clock++; |
258 |
|
|
return(MG_OK); |
259 |
|
|
case MG_E_RS: /* set specular reflectance */ |
260 |
|
|
if (ac != 3) |
261 |
|
|
return(MG_EARGC); |
262 |
|
|
if (!isflt(av[1]) | !isflt(av[2])) |
263 |
|
|
return(MG_ETYPE); |
264 |
|
|
c_cmaterial->rs = atof(av[1]); |
265 |
|
|
c_cmaterial->rs_a = atof(av[2]); |
266 |
|
|
if ((c_cmaterial->rs < 0.) | (c_cmaterial->rs > 1.) | |
267 |
|
|
(c_cmaterial->rs_a < 0.)) |
268 |
|
|
return(MG_EILL); |
269 |
|
|
c_cmaterial->rs_c = *c_ccolor; |
270 |
|
|
c_cmaterial->clock++; |
271 |
|
|
return(MG_OK); |
272 |
|
|
case MG_E_TS: /* set specular transmittance */ |
273 |
|
|
if (ac != 3) |
274 |
|
|
return(MG_EARGC); |
275 |
|
|
if (!isflt(av[1]) | !isflt(av[2])) |
276 |
|
|
return(MG_ETYPE); |
277 |
|
|
c_cmaterial->ts = atof(av[1]); |
278 |
|
|
c_cmaterial->ts_a = atof(av[2]); |
279 |
|
|
if ((c_cmaterial->ts < 0.) | (c_cmaterial->ts > 1.) | |
280 |
|
|
(c_cmaterial->ts_a < 0.)) |
281 |
|
|
return(MG_EILL); |
282 |
|
|
c_cmaterial->ts_c = *c_ccolor; |
283 |
|
|
c_cmaterial->clock++; |
284 |
|
|
return(MG_OK); |
285 |
|
|
case MG_E_SIDES: /* set number of sides */ |
286 |
|
|
if (ac != 2) |
287 |
|
|
return(MG_EARGC); |
288 |
|
|
if (!isint(av[1])) |
289 |
|
|
return(MG_ETYPE); |
290 |
|
|
i = atoi(av[1]); |
291 |
|
|
if (i == 1) |
292 |
|
|
c_cmaterial->sided = 1; |
293 |
|
|
else if (i == 2) |
294 |
|
|
c_cmaterial->sided = 0; |
295 |
|
|
else |
296 |
|
|
return(MG_EILL); |
297 |
|
|
c_cmaterial->clock++; |
298 |
|
|
return(MG_OK); |
299 |
|
|
} |
300 |
|
|
return(MG_EUNK); |
301 |
|
|
} |
302 |
|
|
|
303 |
|
|
|
304 |
|
|
int |
305 |
|
|
c_hvertex(int ac, char **av) /* handle a vertex entity */ |
306 |
|
|
{ |
307 |
|
|
int i; |
308 |
|
|
register LUENT *lp; |
309 |
|
|
|
310 |
|
|
switch (mg_entity(av[0])) { |
311 |
|
|
case MG_E_VERTEX: /* get/set vertex context */ |
312 |
|
|
if (ac > 4) |
313 |
|
|
return(MG_EARGC); |
314 |
|
|
if (ac == 1) { /* set unnamed vertex context */ |
315 |
|
|
c_unvertex = c_dfvertex; |
316 |
|
|
c_cvertex = &c_unvertex; |
317 |
|
|
c_cvname = NULL; |
318 |
|
|
return(MG_OK); |
319 |
|
|
} |
320 |
|
|
if (!isname(av[1])) |
321 |
|
|
return(MG_EILL); |
322 |
|
|
lp = lu_find(&vtx_tab, av[1]); /* lookup context */ |
323 |
|
|
if (lp == NULL) |
324 |
|
|
return(MG_EMEM); |
325 |
|
|
c_cvname = lp->key; |
326 |
|
|
c_cvertex = (C_VERTEX *)lp->data; |
327 |
|
|
if (ac == 2) { /* reestablish previous context */ |
328 |
|
|
if (c_cvertex == NULL) |
329 |
|
|
return(MG_EUNDEF); |
330 |
|
|
return(MG_OK); |
331 |
|
|
} |
332 |
|
|
if (av[2][0] != '=' || av[2][1]) |
333 |
|
|
return(MG_ETYPE); |
334 |
|
|
if (c_cvertex == NULL) { /* create new vertex context */ |
335 |
|
|
lp->key = (char *)malloc(strlen(av[1])+1); |
336 |
|
|
if (lp->key == NULL) |
337 |
|
|
return(MG_EMEM); |
338 |
|
|
strcpy(lp->key, av[1]); |
339 |
|
|
lp->data = (char *)malloc(sizeof(C_VERTEX)); |
340 |
|
|
if (lp->data == NULL) |
341 |
|
|
return(MG_EMEM); |
342 |
|
|
c_cvname = lp->key; |
343 |
|
|
c_cvertex = (C_VERTEX *)lp->data; |
344 |
|
|
c_cvertex->clock = 0; |
345 |
|
|
c_cvertex->client_data = NULL; |
346 |
|
|
} |
347 |
|
|
i = c_cvertex->clock; |
348 |
|
|
if (ac == 3) { /* use default template */ |
349 |
|
|
*c_cvertex = c_dfvertex; |
350 |
|
|
c_cvertex->clock = i + 1; |
351 |
|
|
return(MG_OK); |
352 |
|
|
} |
353 |
|
|
lp = lu_find(&vtx_tab, av[3]); /* lookup template */ |
354 |
|
|
if (lp == NULL) |
355 |
|
|
return(MG_EMEM); |
356 |
|
|
if (lp->data == NULL) |
357 |
|
|
return(MG_EUNDEF); |
358 |
|
|
*c_cvertex = *(C_VERTEX *)lp->data; |
359 |
|
|
c_cvertex->clock = i + 1; |
360 |
|
|
return(MG_OK); |
361 |
|
|
case MG_E_POINT: /* set point */ |
362 |
|
|
if (ac != 4) |
363 |
|
|
return(MG_EARGC); |
364 |
|
|
if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3])) |
365 |
|
|
return(MG_ETYPE); |
366 |
|
|
c_cvertex->p[0] = atof(av[1]); |
367 |
|
|
c_cvertex->p[1] = atof(av[2]); |
368 |
|
|
c_cvertex->p[2] = atof(av[3]); |
369 |
|
|
c_cvertex->clock++; |
370 |
|
|
return(MG_OK); |
371 |
|
|
case MG_E_NORMAL: /* set normal */ |
372 |
|
|
if (ac != 4) |
373 |
|
|
return(MG_EARGC); |
374 |
|
|
if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3])) |
375 |
|
|
return(MG_ETYPE); |
376 |
|
|
c_cvertex->n[0] = atof(av[1]); |
377 |
|
|
c_cvertex->n[1] = atof(av[2]); |
378 |
|
|
c_cvertex->n[2] = atof(av[3]); |
379 |
|
|
(void)normalize(c_cvertex->n); |
380 |
|
|
c_cvertex->clock++; |
381 |
|
|
return(MG_OK); |
382 |
|
|
} |
383 |
|
|
return(MG_EUNK); |
384 |
|
|
} |
385 |
|
|
|
386 |
|
|
|
387 |
|
|
void |
388 |
|
|
c_clearall(void) /* empty context tables */ |
389 |
|
|
{ |
390 |
|
|
c_uncolor = c_dfcolor; |
391 |
|
|
c_ccolor = &c_uncolor; |
392 |
|
|
c_ccname = NULL; |
393 |
|
|
lu_done(&clr_tab); |
394 |
|
|
c_unmaterial = c_dfmaterial; |
395 |
|
|
c_cmaterial = &c_unmaterial; |
396 |
|
|
c_cmname = NULL; |
397 |
|
|
lu_done(&mat_tab); |
398 |
|
|
c_unvertex = c_dfvertex; |
399 |
|
|
c_cvertex = &c_unvertex; |
400 |
|
|
c_cvname = NULL; |
401 |
|
|
lu_done(&vtx_tab); |
402 |
|
|
} |
403 |
|
|
|
404 |
|
|
|
405 |
|
|
C_MATERIAL * |
406 |
|
|
c_getmaterial(char *name) /* get a named material */ |
407 |
|
|
{ |
408 |
|
|
register LUENT *lp; |
409 |
|
|
|
410 |
|
|
if ((lp = lu_find(&mat_tab, name)) == NULL) |
411 |
|
|
return(NULL); |
412 |
|
|
return((C_MATERIAL *)lp->data); |
413 |
|
|
} |
414 |
|
|
|
415 |
|
|
|
416 |
|
|
C_VERTEX * |
417 |
|
|
c_getvert(char *name) /* get a named vertex */ |
418 |
|
|
{ |
419 |
|
|
register LUENT *lp; |
420 |
|
|
|
421 |
|
|
if ((lp = lu_find(&vtx_tab, name)) == NULL) |
422 |
|
|
return(NULL); |
423 |
|
|
return((C_VERTEX *)lp->data); |
424 |
|
|
} |
425 |
|
|
|
426 |
|
|
|
427 |
|
|
C_COLOR * |
428 |
|
|
c_getcolor(char *name) /* get a named color */ |
429 |
|
|
{ |
430 |
|
|
register LUENT *lp; |
431 |
|
|
|
432 |
|
|
if ((lp = lu_find(&clr_tab, name)) == NULL) |
433 |
|
|
return(NULL); |
434 |
|
|
return((C_COLOR *)lp->data); |
435 |
|
|
} |
436 |
|
|
|
437 |
|
|
|
438 |
|
|
static int |
439 |
|
|
setspectrum( /* convert a spectrum */ |
440 |
|
|
register C_COLOR *clr, |
441 |
|
|
double wlmin, |
442 |
|
|
double wlmax, |
443 |
|
|
int ac, |
444 |
|
|
char **av |
445 |
|
|
) |
446 |
|
|
{ |
447 |
|
|
double scale; |
448 |
|
|
float va[C_CNSS]; |
449 |
|
|
register int i, pos; |
450 |
|
|
int n, imax; |
451 |
|
|
int wl; |
452 |
|
|
double wl0, wlstep; |
453 |
|
|
double boxpos, boxstep; |
454 |
|
|
/* check bounds */ |
455 |
|
|
if ((wlmax <= C_CMINWL) | (wlmax <= wlmin) | (wlmin >= C_CMAXWL)) |
456 |
|
|
return(MG_EILL); |
457 |
|
|
wlstep = (wlmax - wlmin)/(ac-1); |
458 |
|
|
while (wlmin < C_CMINWL) { |
459 |
|
|
wlmin += wlstep; |
460 |
|
|
ac--; av++; |
461 |
|
|
} |
462 |
|
|
while (wlmax > C_CMAXWL) { |
463 |
|
|
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) |
494 |
|
|
return(MG_EILL); |
495 |
|
|
scale = C_CMAXV / scale; |
496 |
|
|
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; |
516 |
|
|
clr->clock++; |
517 |
|
|
return(MG_OK); |
518 |
|
|
} |