1 |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Parse an MGF file, converting or discarding unsupported entities |
9 |
*/ |
10 |
|
11 |
#include <stdio.h> |
12 |
#include <math.h> |
13 |
#include <ctype.h> |
14 |
#include <string.h> |
15 |
#include "parser.h" |
16 |
#include "lookup.h" |
17 |
#include "messages.h" |
18 |
|
19 |
/* |
20 |
* Global definitions of variables declared in parser.h |
21 |
*/ |
22 |
/* entity names */ |
23 |
|
24 |
char mg_ename[MG_NENTITIES][MG_MAXELEN] = MG_NAMELIST; |
25 |
|
26 |
/* Handler routines for each entity */ |
27 |
|
28 |
int (*mg_ehand[MG_NENTITIES])(); |
29 |
|
30 |
/* Handler routine for unknown entities */ |
31 |
|
32 |
int (*mg_uhand)() = mg_defuhand; |
33 |
|
34 |
unsigned mg_nunknown; /* count of unknown entities */ |
35 |
|
36 |
/* error messages */ |
37 |
|
38 |
char *mg_err[MG_NERRS] = MG_ERRLIST; |
39 |
|
40 |
MG_FCTXT *mg_file; /* current file context pointer */ |
41 |
|
42 |
int mg_nqcdivs = MG_NQCD; /* number of divisions per quarter circle */ |
43 |
|
44 |
/* |
45 |
* The idea with this parser is to compensate for any missing entries in |
46 |
* mg_ehand with alternate handlers that express these entities in terms |
47 |
* of others that the calling program can handle. |
48 |
* |
49 |
* In some cases, no alternate handler is possible because the entity |
50 |
* has no approximate equivalent. These entities are simply discarded. |
51 |
* |
52 |
* Certain entities are dependent on others, and mg_init() will fail |
53 |
* if the supported entities are not consistent. |
54 |
* |
55 |
* Some alternate entity handlers require that earlier entities be |
56 |
* noted in some fashion, and we therefore keep another array of |
57 |
* parallel support handlers to assist in this effort. |
58 |
*/ |
59 |
|
60 |
/* temporary settings for testing */ |
61 |
#define e_ies e_any_toss |
62 |
/* alternate handler routines */ |
63 |
|
64 |
static int e_any_toss(), /* discard unneeded entity */ |
65 |
e_ies(), /* IES luminaire file */ |
66 |
e_include(), /* include file */ |
67 |
e_sph(), /* sphere */ |
68 |
e_cct(), /* color temperature */ |
69 |
e_cmix(), /* color mixtures */ |
70 |
e_cspec(), /* color spectra */ |
71 |
e_cyl(), /* cylinder */ |
72 |
e_cone(), /* cone */ |
73 |
e_prism(), /* prism */ |
74 |
e_ring(), /* ring */ |
75 |
e_torus(); /* torus */ |
76 |
|
77 |
/* alternate handler support functions */ |
78 |
|
79 |
static int (*e_supp[MG_NENTITIES])(); |
80 |
|
81 |
static char FLTFMT[] = "%.12g"; |
82 |
|
83 |
static int warpconends; /* hack for generating good normals */ |
84 |
|
85 |
|
86 |
void |
87 |
mg_init() /* initialize alternate entity handlers */ |
88 |
{ |
89 |
unsigned long ineed = 0, uneed = 0; |
90 |
register int i; |
91 |
/* pick up slack */ |
92 |
if (mg_ehand[MG_E_IES] == NULL) |
93 |
mg_ehand[MG_E_IES] = e_ies; |
94 |
if (mg_ehand[MG_E_INCLUDE] == NULL) |
95 |
mg_ehand[MG_E_INCLUDE] = e_include; |
96 |
if (mg_ehand[MG_E_SPH] == NULL) { |
97 |
mg_ehand[MG_E_SPH] = e_sph; |
98 |
ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX; |
99 |
} else |
100 |
uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF; |
101 |
if (mg_ehand[MG_E_CYL] == NULL) { |
102 |
mg_ehand[MG_E_CYL] = e_cyl; |
103 |
ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX; |
104 |
} else |
105 |
uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF; |
106 |
if (mg_ehand[MG_E_CONE] == NULL) { |
107 |
mg_ehand[MG_E_CONE] = e_cone; |
108 |
ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX; |
109 |
} else |
110 |
uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF; |
111 |
if (mg_ehand[MG_E_RING] == NULL) { |
112 |
mg_ehand[MG_E_RING] = e_ring; |
113 |
ineed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX; |
114 |
} else |
115 |
uneed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX|1L<<MG_E_XF; |
116 |
if (mg_ehand[MG_E_PRISM] == NULL) { |
117 |
mg_ehand[MG_E_PRISM] = e_prism; |
118 |
ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX; |
119 |
} else |
120 |
uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF; |
121 |
if (mg_ehand[MG_E_TORUS] == NULL) { |
122 |
mg_ehand[MG_E_TORUS] = e_torus; |
123 |
ineed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX; |
124 |
} else |
125 |
uneed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX|1L<<MG_E_XF; |
126 |
if (mg_ehand[MG_E_COLOR] != NULL) { |
127 |
if (mg_ehand[MG_E_CMIX] == NULL) { |
128 |
mg_ehand[MG_E_CMIX] = e_cmix; |
129 |
ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT; |
130 |
} |
131 |
if (mg_ehand[MG_E_CSPEC] == NULL) { |
132 |
mg_ehand[MG_E_CSPEC] = e_cspec; |
133 |
ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT; |
134 |
} |
135 |
if (mg_ehand[MG_E_CCT] == NULL) { |
136 |
mg_ehand[MG_E_CCT] = e_cct; |
137 |
ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT; |
138 |
} |
139 |
} |
140 |
/* check for consistency */ |
141 |
if (mg_ehand[MG_E_FACE] != NULL) |
142 |
uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF; |
143 |
if (mg_ehand[MG_E_CXY] != NULL || mg_ehand[MG_E_CSPEC] != NULL || |
144 |
mg_ehand[MG_E_CMIX] != NULL) |
145 |
uneed |= 1L<<MG_E_COLOR; |
146 |
if (mg_ehand[MG_E_RD] != NULL || mg_ehand[MG_E_TD] != NULL || |
147 |
mg_ehand[MG_E_IR] != NULL || |
148 |
mg_ehand[MG_E_ED] != NULL || |
149 |
mg_ehand[MG_E_RS] != NULL || |
150 |
mg_ehand[MG_E_TS] != NULL || |
151 |
mg_ehand[MG_E_SIDES] != NULL) |
152 |
uneed |= 1L<<MG_E_MATERIAL; |
153 |
for (i = 0; i < MG_NENTITIES; i++) |
154 |
if (uneed & 1L<<i && mg_ehand[i] == NULL) { |
155 |
fprintf(stderr, "Missing support for \"%s\" entity\n", |
156 |
mg_ename[i]); |
157 |
exit(1); |
158 |
} |
159 |
/* add support as needed */ |
160 |
if (ineed & 1L<<MG_E_VERTEX && mg_ehand[MG_E_VERTEX] != c_hvertex) |
161 |
e_supp[MG_E_VERTEX] = c_hvertex; |
162 |
if (ineed & 1L<<MG_E_POINT && mg_ehand[MG_E_POINT] != c_hvertex) |
163 |
e_supp[MG_E_POINT] = c_hvertex; |
164 |
if (ineed & 1L<<MG_E_NORMAL && mg_ehand[MG_E_NORMAL] != c_hvertex) |
165 |
e_supp[MG_E_NORMAL] = c_hvertex; |
166 |
if (ineed & 1L<<MG_E_COLOR && mg_ehand[MG_E_COLOR] != c_hcolor) |
167 |
e_supp[MG_E_COLOR] = c_hcolor; |
168 |
if (ineed & 1L<<MG_E_CXY && mg_ehand[MG_E_CXY] != c_hcolor) |
169 |
e_supp[MG_E_CXY] = c_hcolor; |
170 |
if (ineed & 1L<<MG_E_CSPEC && mg_ehand[MG_E_CSPEC] != c_hcolor) |
171 |
e_supp[MG_E_CSPEC] = c_hcolor; |
172 |
if (ineed & 1L<<MG_E_CMIX && mg_ehand[MG_E_CMIX] != c_hcolor) |
173 |
e_supp[MG_E_CMIX] = c_hcolor; |
174 |
if (ineed & 1L<<MG_E_CCT && mg_ehand[MG_E_CCT] != c_hcolor) |
175 |
e_supp[MG_E_CCT] = c_hcolor; |
176 |
/* discard remaining entities */ |
177 |
for (i = 0; i < MG_NENTITIES; i++) |
178 |
if (mg_ehand[i] == NULL) |
179 |
mg_ehand[i] = e_any_toss; |
180 |
} |
181 |
|
182 |
|
183 |
int |
184 |
mg_entity(name) /* get entity number from its name */ |
185 |
char *name; |
186 |
{ |
187 |
static LUTAB ent_tab = LU_SINIT(NULL,NULL); /* lookup table */ |
188 |
register char *cp; |
189 |
|
190 |
if (!ent_tab.tsiz) { /* initialize hash table */ |
191 |
if (!lu_init(&ent_tab, MG_NENTITIES)) |
192 |
return(-1); /* what to do? */ |
193 |
for (cp = mg_ename[MG_NENTITIES-1]; cp >= mg_ename[0]; |
194 |
cp -= sizeof(mg_ename[0])) |
195 |
lu_find(&ent_tab, cp)->key = cp; |
196 |
} |
197 |
cp = lu_find(&ent_tab, name)->key; |
198 |
if (cp == NULL) |
199 |
return(-1); |
200 |
return((cp - mg_ename[0])/sizeof(mg_ename[0])); |
201 |
} |
202 |
|
203 |
|
204 |
int |
205 |
mg_handle(en, ac, av) /* pass entity to appropriate handler */ |
206 |
register int en; |
207 |
int ac; |
208 |
char **av; |
209 |
{ |
210 |
int rv; |
211 |
|
212 |
if (en < 0 && (en = mg_entity(av[0])) < 0) { /* unknown entity */ |
213 |
if (mg_uhand != NULL) |
214 |
return((*mg_uhand)(ac, av)); |
215 |
return(MG_EUNK); |
216 |
} |
217 |
if (e_supp[en] != NULL) { /* support handler */ |
218 |
if ((rv = (*e_supp[en])(ac, av)) != MG_OK) |
219 |
return(rv); |
220 |
} |
221 |
return((*mg_ehand[en])(ac, av)); /* assigned handler */ |
222 |
} |
223 |
|
224 |
|
225 |
int |
226 |
mg_open(ctx, fn) /* open new input file */ |
227 |
register MG_FCTXT *ctx; |
228 |
char *fn; |
229 |
{ |
230 |
static int nfids; |
231 |
register char *cp; |
232 |
|
233 |
ctx->fid = ++nfids; |
234 |
ctx->lineno = 0; |
235 |
if (fn == NULL) { |
236 |
strcpy(ctx->fname, "<stdin>"); |
237 |
ctx->fp = stdin; |
238 |
ctx->prev = mg_file; |
239 |
mg_file = ctx; |
240 |
return(MG_OK); |
241 |
} |
242 |
/* get name relative to this context */ |
243 |
if (mg_file != NULL && (cp = strrchr(mg_file->fname, '/')) != NULL) { |
244 |
strcpy(ctx->fname, mg_file->fname); |
245 |
strcpy(ctx->fname+(cp-mg_file->fname+1), fn); |
246 |
} else |
247 |
strcpy(ctx->fname, fn); |
248 |
ctx->fp = fopen(ctx->fname, "r"); |
249 |
if (ctx->fp == NULL) |
250 |
return(MG_ENOFILE); |
251 |
ctx->prev = mg_file; /* establish new context */ |
252 |
mg_file = ctx; |
253 |
return(MG_OK); |
254 |
} |
255 |
|
256 |
|
257 |
void |
258 |
mg_close() /* close input file */ |
259 |
{ |
260 |
register MG_FCTXT *ctx = mg_file; |
261 |
|
262 |
mg_file = ctx->prev; /* restore enclosing context */ |
263 |
if (ctx->fp == stdin) |
264 |
return; /* don't close standard input */ |
265 |
fclose(ctx->fp); |
266 |
} |
267 |
|
268 |
|
269 |
void |
270 |
mg_fgetpos(pos) /* get current position in input file */ |
271 |
register MG_FPOS *pos; |
272 |
{ |
273 |
extern long ftell(); |
274 |
|
275 |
pos->fid = mg_file->fid; |
276 |
pos->lineno = mg_file->lineno; |
277 |
pos->offset = ftell(mg_file->fp); |
278 |
} |
279 |
|
280 |
|
281 |
int |
282 |
mg_fgoto(pos) /* reposition input file pointer */ |
283 |
register MG_FPOS *pos; |
284 |
{ |
285 |
if (pos->fid != mg_file->fid) |
286 |
return(MG_ESEEK); |
287 |
if (pos->lineno == mg_file->lineno) |
288 |
return(MG_OK); |
289 |
if (mg_file->fp == stdin) |
290 |
return(MG_ESEEK); /* cannot seek on standard input */ |
291 |
if (fseek(mg_file->fp, pos->offset, 0) == EOF) |
292 |
return(MG_ESEEK); |
293 |
mg_file->lineno = pos->lineno; |
294 |
return(MG_OK); |
295 |
} |
296 |
|
297 |
|
298 |
int |
299 |
mg_read() /* read next line from file */ |
300 |
{ |
301 |
register int len = 0; |
302 |
|
303 |
do { |
304 |
if (fgets(mg_file->inpline+len, |
305 |
MG_MAXLINE-len, mg_file->fp) == NULL) |
306 |
return(len); |
307 |
mg_file->lineno++; |
308 |
len += strlen(mg_file->inpline+len); |
309 |
if (len > 1 && mg_file->inpline[len-2] == '\\') |
310 |
mg_file->inpline[--len-1] = ' '; |
311 |
} while (mg_file->inpline[len]); |
312 |
|
313 |
return(len); |
314 |
} |
315 |
|
316 |
|
317 |
int |
318 |
mg_parse() /* parse current input line */ |
319 |
{ |
320 |
char abuf[MG_MAXLINE]; |
321 |
char *argv[MG_MAXARGC]; |
322 |
int en; |
323 |
register char *cp, **ap; |
324 |
|
325 |
strcpy(cp=abuf, mg_file->inpline); |
326 |
ap = argv; /* break into words */ |
327 |
for ( ; ; ) { |
328 |
while (isspace(*cp)) |
329 |
*cp++ = '\0'; |
330 |
if (!*cp) |
331 |
break; |
332 |
if (ap - argv >= MG_MAXARGC-1) |
333 |
return(MG_EARGC); |
334 |
*ap++ = cp; |
335 |
while (*++cp && !isspace(*cp)) |
336 |
; |
337 |
} |
338 |
if (ap == argv) |
339 |
return(MG_OK); /* no words in line */ |
340 |
*ap = NULL; |
341 |
/* else handle it */ |
342 |
return(mg_handle(-1, ap-argv, argv)); |
343 |
} |
344 |
|
345 |
|
346 |
int |
347 |
mg_load(fn) /* load an MGF file */ |
348 |
char *fn; |
349 |
{ |
350 |
MG_FCTXT cntxt; |
351 |
int rval; |
352 |
|
353 |
if ((rval = mg_open(&cntxt, fn)) != MG_OK) { |
354 |
fprintf(stderr, "%s: %s\n", fn, mg_err[rval]); |
355 |
return(rval); |
356 |
} |
357 |
while (mg_read()) /* parse each line */ |
358 |
if ((rval = mg_parse()) != MG_OK) { |
359 |
fprintf(stderr, "%s: %d: %s:\n%s", cntxt.fname, |
360 |
cntxt.lineno, mg_err[rval], |
361 |
cntxt.inpline); |
362 |
break; |
363 |
} |
364 |
mg_close(); |
365 |
return(rval); |
366 |
} |
367 |
|
368 |
|
369 |
int |
370 |
mg_defuhand(ac, av) /* default handler for unknown entities */ |
371 |
int ac; |
372 |
char **av; |
373 |
{ |
374 |
if (mg_nunknown++ == 0) /* report first incident */ |
375 |
fprintf(stderr, "%s: %d: %s: %s\n", mg_file->fname, |
376 |
mg_file->lineno, mg_err[MG_EUNK], av[0]); |
377 |
return(MG_OK); |
378 |
} |
379 |
|
380 |
|
381 |
void |
382 |
mg_clear() /* clear parser history */ |
383 |
{ |
384 |
c_clearall(); /* clear context tables */ |
385 |
mg_file = NULL; /* reset our context */ |
386 |
} |
387 |
|
388 |
|
389 |
/**************************************************************************** |
390 |
* The following routines handle unsupported entities |
391 |
*/ |
392 |
|
393 |
|
394 |
static int |
395 |
e_any_toss(ac, av) /* discard an unwanted entity */ |
396 |
int ac; |
397 |
char **av; |
398 |
{ |
399 |
return(MG_OK); |
400 |
} |
401 |
|
402 |
|
403 |
static int |
404 |
e_include(ac, av) /* include file */ |
405 |
int ac; |
406 |
char **av; |
407 |
{ |
408 |
char *xfarg[MG_MAXARGC]; |
409 |
MG_FCTXT ictx; |
410 |
XF_SPEC *xf_orig = xf_context; |
411 |
int rv; |
412 |
|
413 |
if (ac < 2) |
414 |
return(MG_EARGC); |
415 |
if ((rv = mg_open(&ictx, av[1])) != MG_OK) |
416 |
return(rv); |
417 |
if (ac > 2) { |
418 |
register int i; |
419 |
|
420 |
xfarg[0] = mg_ename[MG_E_XF]; |
421 |
for (i = 1; i < ac-1; i++) |
422 |
xfarg[i] = av[i+1]; |
423 |
xfarg[ac-1] = NULL; |
424 |
if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK) |
425 |
return(rv); |
426 |
} |
427 |
do { |
428 |
while (mg_read()) |
429 |
if ((rv = mg_parse()) != MG_OK) { |
430 |
fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname, |
431 |
ictx.lineno, mg_err[rv], |
432 |
ictx.inpline); |
433 |
mg_close(); |
434 |
return(MG_EINCL); |
435 |
} |
436 |
if (ac > 2) |
437 |
if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK) |
438 |
return(rv); |
439 |
} while (xf_context != xf_orig); |
440 |
mg_close(); |
441 |
return(MG_OK); |
442 |
} |
443 |
|
444 |
|
445 |
static void |
446 |
make_axes(u, v, w) /* compute u and v given w (normalized) */ |
447 |
FVECT u, v, w; |
448 |
{ |
449 |
register int i; |
450 |
|
451 |
v[0] = v[1] = v[2] = 0.; |
452 |
for (i = 0; i < 3; i++) |
453 |
if (w[i] < .6 && w[i] > -.6) |
454 |
break; |
455 |
v[i] = 1.; |
456 |
fcross(u, v, w); |
457 |
normalize(u); |
458 |
fcross(v, w, u); |
459 |
} |
460 |
|
461 |
|
462 |
static int |
463 |
e_sph(ac, av) /* expand a sphere into cones */ |
464 |
int ac; |
465 |
char **av; |
466 |
{ |
467 |
static char p2x[24], p2y[24], p2z[24], r1[24], r2[24]; |
468 |
static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_sv1","=","_sv2"}; |
469 |
static char *v2ent[4] = {mg_ename[MG_E_VERTEX],"_sv2","="}; |
470 |
static char *p2ent[5] = {mg_ename[MG_E_POINT],p2x,p2y,p2z}; |
471 |
static char *conent[6] = {mg_ename[MG_E_CONE],"_sv1",r1,"_sv2",r2}; |
472 |
register C_VERTEX *cv; |
473 |
register int i; |
474 |
int rval; |
475 |
double rad; |
476 |
double theta; |
477 |
|
478 |
if (ac != 3) |
479 |
return(MG_EARGC); |
480 |
if ((cv = c_getvert(av[1])) == NULL) |
481 |
return(MG_EUNDEF); |
482 |
if (!isflt(av[2])) |
483 |
return(MG_ETYPE); |
484 |
rad = atof(av[2]); |
485 |
/* initialize */ |
486 |
warpconends = 1; |
487 |
if ((rval = mg_handle(MG_E_VERTEX, 3, v2ent)) != MG_OK) |
488 |
return(rval); |
489 |
sprintf(p2x, FLTFMT, cv->p[0]); |
490 |
sprintf(p2y, FLTFMT, cv->p[1]); |
491 |
sprintf(p2z, FLTFMT, cv->p[2]+rad); |
492 |
if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK) |
493 |
return(rval); |
494 |
r2[0] = '0'; r2[1] = '\0'; |
495 |
for (i = 1; i <= 2*mg_nqcdivs; i++) { |
496 |
theta = i*(PI/2)/mg_nqcdivs; |
497 |
if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
498 |
return(rval); |
499 |
sprintf(p2z, FLTFMT, cv->p[2]+rad*cos(theta)); |
500 |
if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK) |
501 |
return(rval); |
502 |
if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK) |
503 |
return(rval); |
504 |
strcpy(r1, r2); |
505 |
sprintf(r2, FLTFMT, rad*sin(theta)); |
506 |
if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK) |
507 |
return(rval); |
508 |
} |
509 |
warpconends = 0; |
510 |
return(MG_OK); |
511 |
} |
512 |
|
513 |
|
514 |
static int |
515 |
e_torus(ac, av) /* expand a torus into cones */ |
516 |
int ac; |
517 |
char **av; |
518 |
{ |
519 |
static char p2[3][24], r1[24], r2[24]; |
520 |
static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_tv1","=","_tv2"}; |
521 |
static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_tv2","="}; |
522 |
static char *p2ent[5] = {mg_ename[MG_E_POINT],p2[0],p2[1],p2[2]}; |
523 |
static char *conent[6] = {mg_ename[MG_E_CONE],"_tv1",r1,"_tv2",r2}; |
524 |
register C_VERTEX *cv; |
525 |
register int i, j; |
526 |
int rval; |
527 |
int sgn; |
528 |
double minrad, maxrad, avgrad; |
529 |
double theta; |
530 |
|
531 |
if (ac != 4) |
532 |
return(MG_EARGC); |
533 |
if ((cv = c_getvert(av[1])) == NULL) |
534 |
return(MG_EUNDEF); |
535 |
if (is0vect(cv->n)) |
536 |
return(MG_EILL); |
537 |
if (!isflt(av[2]) || !isflt(av[3])) |
538 |
return(MG_ETYPE); |
539 |
minrad = atof(av[2]); |
540 |
round0(minrad); |
541 |
maxrad = atof(av[3]); |
542 |
/* check orientation */ |
543 |
if (minrad > 0.) |
544 |
sgn = 1; |
545 |
else if (minrad < 0.) |
546 |
sgn = -1; |
547 |
else if (maxrad > 0.) |
548 |
sgn = 1; |
549 |
else if (maxrad < 0.) |
550 |
sgn = -1; |
551 |
else |
552 |
return(MG_EILL); |
553 |
if (sgn*(maxrad-minrad) <= 0.) |
554 |
return(MG_EILL); |
555 |
/* initialize */ |
556 |
warpconends = 1; |
557 |
v2ent[3] = av[1]; |
558 |
for (j = 0; j < 3; j++) |
559 |
sprintf(p2[j], FLTFMT, cv->p[j] + |
560 |
.5*sgn*(maxrad-minrad)*cv->n[j]); |
561 |
if ((rval = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
562 |
return(rval); |
563 |
if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK) |
564 |
return(rval); |
565 |
sprintf(r2, FLTFMT, avgrad=.5*(minrad+maxrad)); |
566 |
/* run outer section */ |
567 |
for (i = 1; i <= 2*mg_nqcdivs; i++) { |
568 |
theta = i*(PI/2)/mg_nqcdivs; |
569 |
if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
570 |
return(rval); |
571 |
for (j = 0; j < 3; j++) |
572 |
sprintf(p2[j], FLTFMT, cv->p[j] + |
573 |
.5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]); |
574 |
if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK) |
575 |
return(rval); |
576 |
if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK) |
577 |
return(rval); |
578 |
strcpy(r1, r2); |
579 |
sprintf(r2, FLTFMT, avgrad + .5*(maxrad-minrad)*sin(theta)); |
580 |
if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK) |
581 |
return(rval); |
582 |
} |
583 |
/* run inner section */ |
584 |
sprintf(r2, FLTFMT, -.5*(minrad+maxrad)); |
585 |
for ( ; i <= 4*mg_nqcdivs; i++) { |
586 |
theta = i*(PI/2)/mg_nqcdivs; |
587 |
for (j = 0; j < 3; j++) |
588 |
sprintf(p2[j], FLTFMT, cv->p[j] + |
589 |
.5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]); |
590 |
if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
591 |
return(rval); |
592 |
if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK) |
593 |
return(rval); |
594 |
if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK) |
595 |
return(rval); |
596 |
strcpy(r1, r2); |
597 |
sprintf(r2, FLTFMT, -avgrad - .5*(maxrad-minrad)*sin(theta)); |
598 |
if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK) |
599 |
return(rval); |
600 |
} |
601 |
warpconends = 0; |
602 |
return(MG_OK); |
603 |
} |
604 |
|
605 |
|
606 |
static int |
607 |
e_cyl(ac, av) /* replace a cylinder with equivalent cone */ |
608 |
int ac; |
609 |
char **av; |
610 |
{ |
611 |
static char *avnew[6] = {mg_ename[MG_E_CONE]}; |
612 |
|
613 |
if (ac != 4) |
614 |
return(MG_EARGC); |
615 |
avnew[1] = av[1]; |
616 |
avnew[2] = av[2]; |
617 |
avnew[3] = av[3]; |
618 |
avnew[4] = av[2]; |
619 |
return(mg_handle(MG_E_CONE, 5, avnew)); |
620 |
} |
621 |
|
622 |
|
623 |
static int |
624 |
e_ring(ac, av) /* turn a ring into polygons */ |
625 |
int ac; |
626 |
char **av; |
627 |
{ |
628 |
static char p3[3][24], p4[3][24]; |
629 |
static char *nzent[5] = {mg_ename[MG_E_NORMAL],"0","0","0"}; |
630 |
static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_rv1","="}; |
631 |
static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_rv2","=","_rv3"}; |
632 |
static char *v3ent[4] = {mg_ename[MG_E_VERTEX],"_rv3","="}; |
633 |
static char *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]}; |
634 |
static char *v4ent[4] = {mg_ename[MG_E_VERTEX],"_rv4","="}; |
635 |
static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]}; |
636 |
static char *fent[6] = {mg_ename[MG_E_FACE],"_rv1","_rv2","_rv3","_rv4"}; |
637 |
register C_VERTEX *cv; |
638 |
register int i, j; |
639 |
FVECT u, v; |
640 |
double minrad, maxrad; |
641 |
int rv; |
642 |
double theta, d; |
643 |
|
644 |
if (ac != 4) |
645 |
return(MG_EARGC); |
646 |
if ((cv = c_getvert(av[1])) == NULL) |
647 |
return(MG_EUNDEF); |
648 |
if (is0vect(cv->n)) |
649 |
return(MG_EILL); |
650 |
if (!isflt(av[2]) || !isflt(av[3])) |
651 |
return(MG_ETYPE); |
652 |
minrad = atof(av[2]); |
653 |
round0(minrad); |
654 |
maxrad = atof(av[3]); |
655 |
if (minrad < 0. || maxrad <= minrad) |
656 |
return(MG_EILL); |
657 |
/* initialize */ |
658 |
make_axes(u, v, cv->n); |
659 |
for (j = 0; j < 3; j++) |
660 |
sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*u[j]); |
661 |
if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK) |
662 |
return(rv); |
663 |
if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) |
664 |
return(rv); |
665 |
if (minrad == 0.) { /* closed */ |
666 |
v1ent[3] = av[1]; |
667 |
if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
668 |
return(rv); |
669 |
if ((rv = mg_handle(MG_E_NORMAL, 4, nzent)) != MG_OK) |
670 |
return(rv); |
671 |
for (i = 1; i <= 4*mg_nqcdivs; i++) { |
672 |
theta = i*(PI/2)/mg_nqcdivs; |
673 |
if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
674 |
return(rv); |
675 |
for (j = 0; j < 3; j++) |
676 |
sprintf(p3[j], FLTFMT, cv->p[j] + |
677 |
maxrad*u[j]*cos(theta) + |
678 |
maxrad*v[j]*sin(theta)); |
679 |
if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK) |
680 |
return(rv); |
681 |
if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) |
682 |
return(rv); |
683 |
if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK) |
684 |
return(rv); |
685 |
} |
686 |
} else { /* open */ |
687 |
if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK) |
688 |
return(rv); |
689 |
for (j = 0; j < 3; j++) |
690 |
sprintf(p4[j], FLTFMT, cv->p[j] + minrad*u[j]); |
691 |
if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK) |
692 |
return(rv); |
693 |
v1ent[3] = "_rv4"; |
694 |
for (i = 1; i <= 4*mg_nqcdivs; i++) { |
695 |
theta = i*(PI/2)/mg_nqcdivs; |
696 |
if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
697 |
return(rv); |
698 |
if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
699 |
return(rv); |
700 |
for (j = 0; j < 3; j++) { |
701 |
d = u[j]*cos(theta) + v[j]*sin(theta); |
702 |
sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*d); |
703 |
sprintf(p4[j], FLTFMT, cv->p[j] + minrad*d); |
704 |
} |
705 |
if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK) |
706 |
return(rv); |
707 |
if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) |
708 |
return(rv); |
709 |
if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK) |
710 |
return(rv); |
711 |
if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK) |
712 |
return(rv); |
713 |
if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK) |
714 |
return(rv); |
715 |
} |
716 |
} |
717 |
return(MG_OK); |
718 |
} |
719 |
|
720 |
|
721 |
static int |
722 |
e_cone(ac, av) /* turn a cone into polygons */ |
723 |
int ac; |
724 |
char **av; |
725 |
{ |
726 |
static char p3[3][24], p4[3][24], n3[3][24], n4[3][24]; |
727 |
static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_cv1","="}; |
728 |
static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_cv2","=","_cv3"}; |
729 |
static char *v3ent[4] = {mg_ename[MG_E_VERTEX],"_cv3","="}; |
730 |
static char *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]}; |
731 |
static char *n3ent[5] = {mg_ename[MG_E_NORMAL],n3[0],n3[1],n3[2]}; |
732 |
static char *v4ent[4] = {mg_ename[MG_E_VERTEX],"_cv4","="}; |
733 |
static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]}; |
734 |
static char *n4ent[5] = {mg_ename[MG_E_NORMAL],n4[0],n4[1],n4[2]}; |
735 |
static char *fent[6] = {mg_ename[MG_E_FACE],"_cv1","_cv2","_cv3","_cv4"}; |
736 |
char *v1n; |
737 |
register C_VERTEX *cv1, *cv2; |
738 |
register int i, j; |
739 |
FVECT u, v, w; |
740 |
double rad1, rad2; |
741 |
int sgn; |
742 |
double n1off, n2off; |
743 |
double d; |
744 |
int rv; |
745 |
double theta; |
746 |
|
747 |
if (ac != 5) |
748 |
return(MG_EARGC); |
749 |
if ((cv1 = c_getvert(av[1])) == NULL || |
750 |
(cv2 = c_getvert(av[3])) == NULL) |
751 |
return(MG_EUNDEF); |
752 |
v1n = av[1]; |
753 |
if (!isflt(av[2]) || !isflt(av[4])) |
754 |
return(MG_ETYPE); |
755 |
rad1 = atof(av[2]); |
756 |
round0(rad1); |
757 |
rad2 = atof(av[4]); |
758 |
round0(rad2); |
759 |
if (rad1 == 0.) { |
760 |
if (rad2 == 0.) |
761 |
return(MG_EILL); |
762 |
} else if (rad2 != 0.) { |
763 |
if (rad1 < 0. ^ rad2 < 0.) |
764 |
return(MG_EILL); |
765 |
} else { /* swap */ |
766 |
C_VERTEX *cv; |
767 |
|
768 |
cv = cv1; |
769 |
cv1 = cv2; |
770 |
cv2 = cv; |
771 |
v1n = av[3]; |
772 |
d = rad1; |
773 |
rad1 = rad2; |
774 |
rad2 = d; |
775 |
} |
776 |
sgn = rad2 < 0. ? -1 : 1; |
777 |
/* initialize */ |
778 |
for (j = 0; j < 3; j++) |
779 |
w[j] = cv1->p[j] - cv2->p[j]; |
780 |
if ((d = normalize(w)) == 0.) |
781 |
return(MG_EILL); |
782 |
n1off = n2off = (rad2 - rad1)/d; |
783 |
if (warpconends) { /* hack for e_sph and e_torus */ |
784 |
d = atan(n2off) - (PI/4)/mg_nqcdivs; |
785 |
if (d <= -PI/2+FTINY) |
786 |
n2off = -FHUGE; |
787 |
else |
788 |
n2off = tan(d); |
789 |
} |
790 |
make_axes(u, v, w); |
791 |
for (j = 0; j < 3; j++) { |
792 |
sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*u[j]); |
793 |
if (n2off <= -FHUGE) |
794 |
sprintf(n3[j], FLTFMT, -w[j]); |
795 |
else |
796 |
sprintf(n3[j], FLTFMT, u[j] + w[j]*n2off); |
797 |
} |
798 |
if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK) |
799 |
return(rv); |
800 |
if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) |
801 |
return(rv); |
802 |
if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK) |
803 |
return(rv); |
804 |
if (rad1 == 0.) { /* triangles */ |
805 |
v1ent[3] = v1n; |
806 |
if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
807 |
return(rv); |
808 |
for (j = 0; j < 3; j++) |
809 |
sprintf(n4[j], FLTFMT, w[j]); |
810 |
if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK) |
811 |
return(rv); |
812 |
for (i = 1; i <= 4*mg_nqcdivs; i++) { |
813 |
theta = sgn*i*(PI/2)/mg_nqcdivs; |
814 |
if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
815 |
return(rv); |
816 |
for (j = 0; j < 3; j++) { |
817 |
d = u[j]*cos(theta) + v[j]*sin(theta); |
818 |
sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d); |
819 |
if (n2off > -FHUGE) |
820 |
sprintf(n3[j], FLTFMT, d + w[j]*n2off); |
821 |
} |
822 |
if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK) |
823 |
return(rv); |
824 |
if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) |
825 |
return(rv); |
826 |
if (n2off > -FHUGE && |
827 |
(rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK) |
828 |
return(rv); |
829 |
if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK) |
830 |
return(rv); |
831 |
} |
832 |
} else { /* quads */ |
833 |
v1ent[3] = "_cv4"; |
834 |
if (warpconends) { /* hack for e_sph and e_torus */ |
835 |
d = atan(n1off) + (PI/4)/mg_nqcdivs; |
836 |
if (d >= PI/2-FTINY) |
837 |
n1off = FHUGE; |
838 |
else |
839 |
n1off = tan(atan(n1off)+(PI/4)/mg_nqcdivs); |
840 |
} |
841 |
for (j = 0; j < 3; j++) { |
842 |
sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*u[j]); |
843 |
if (n1off >= FHUGE) |
844 |
sprintf(n4[j], FLTFMT, w[j]); |
845 |
else |
846 |
sprintf(n4[j], FLTFMT, u[j] + w[j]*n1off); |
847 |
} |
848 |
if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK) |
849 |
return(rv); |
850 |
if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK) |
851 |
return(rv); |
852 |
if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK) |
853 |
return(rv); |
854 |
for (i = 1; i <= 4*mg_nqcdivs; i++) { |
855 |
theta = sgn*i*(PI/2)/mg_nqcdivs; |
856 |
if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
857 |
return(rv); |
858 |
if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
859 |
return(rv); |
860 |
for (j = 0; j < 3; j++) { |
861 |
d = u[j]*cos(theta) + v[j]*sin(theta); |
862 |
sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d); |
863 |
if (n2off > -FHUGE) |
864 |
sprintf(n3[j], FLTFMT, d + w[j]*n2off); |
865 |
sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*d); |
866 |
if (n1off < FHUGE) |
867 |
sprintf(n4[j], FLTFMT, d + w[j]*n1off); |
868 |
} |
869 |
if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK) |
870 |
return(rv); |
871 |
if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) |
872 |
return(rv); |
873 |
if (n2off > -FHUGE && |
874 |
(rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK) |
875 |
return(rv); |
876 |
if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK) |
877 |
return(rv); |
878 |
if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK) |
879 |
return(rv); |
880 |
if (n1off < FHUGE && |
881 |
(rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK) |
882 |
return(rv); |
883 |
if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK) |
884 |
return(rv); |
885 |
} |
886 |
} |
887 |
return(MG_OK); |
888 |
} |
889 |
|
890 |
|
891 |
static int |
892 |
e_prism(ac, av) /* turn a prism into polygons */ |
893 |
int ac; |
894 |
char **av; |
895 |
{ |
896 |
static char p[3][24]; |
897 |
static char *vent[5] = {mg_ename[MG_E_VERTEX],NULL,"="}; |
898 |
static char *pent[5] = {mg_ename[MG_E_POINT],p[0],p[1],p[2]}; |
899 |
static char *znorm[5] = {mg_ename[MG_E_NORMAL],"0","0","0"}; |
900 |
char *newav[MG_MAXARGC], nvn[MG_MAXARGC-1][8]; |
901 |
double length; |
902 |
int hasnorm; |
903 |
FVECT v1, v2, v3, norm; |
904 |
register C_VERTEX *cv; |
905 |
C_VERTEX *cv0; |
906 |
int rv; |
907 |
register int i, j; |
908 |
/* check arguments */ |
909 |
if (ac < 5) |
910 |
return(MG_EARGC); |
911 |
if (!isflt(av[ac-1])) |
912 |
return(MG_ETYPE); |
913 |
length = atof(av[ac-1]); |
914 |
if (length <= FTINY && length >= -FTINY) |
915 |
return(MG_EILL); |
916 |
/* compute face normal */ |
917 |
if ((cv0 = c_getvert(av[1])) == NULL) |
918 |
return(MG_EUNDEF); |
919 |
hasnorm = 0; |
920 |
norm[0] = norm[1] = norm[2] = 0.; |
921 |
v1[0] = v1[1] = v1[2] = 0.; |
922 |
for (i = 2; i < ac-1; i++) { |
923 |
if ((cv = c_getvert(av[i])) == NULL) |
924 |
return(MG_EUNDEF); |
925 |
hasnorm += !is0vect(cv->n); |
926 |
v2[0] = cv->p[0] - cv0->p[0]; |
927 |
v2[1] = cv->p[1] - cv0->p[1]; |
928 |
v2[2] = cv->p[2] - cv0->p[2]; |
929 |
fcross(v3, v1, v2); |
930 |
norm[0] += v3[0]; |
931 |
norm[1] += v3[1]; |
932 |
norm[2] += v3[2]; |
933 |
VCOPY(v1, v2); |
934 |
} |
935 |
if (normalize(norm) == 0.) |
936 |
return(MG_EILL); |
937 |
/* create moved vertices */ |
938 |
for (i = 1; i < ac-1; i++) { |
939 |
sprintf(nvn[i-1], "_pv%d", i); |
940 |
vent[1] = nvn[i-1]; |
941 |
vent[3] = av[i]; |
942 |
if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK) |
943 |
return(rv); |
944 |
cv = c_getvert(av[i]); /* checked above */ |
945 |
for (j = 0; j < 3; j++) |
946 |
sprintf(p[j], FLTFMT, cv->p[j] - length*norm[j]); |
947 |
if ((rv = mg_handle(MG_E_POINT, 4, pent)) != MG_OK) |
948 |
return(rv); |
949 |
} |
950 |
/* make faces */ |
951 |
newav[0] = mg_ename[MG_E_FACE]; |
952 |
/* do the side faces */ |
953 |
newav[5] = NULL; |
954 |
newav[3] = av[ac-2]; |
955 |
newav[4] = nvn[ac-3]; |
956 |
for (i = 1; i < ac-1; i++) { |
957 |
newav[1] = nvn[i-1]; |
958 |
newav[2] = av[i]; |
959 |
if ((rv = mg_handle(MG_E_FACE, 5, newav)) != MG_OK) |
960 |
return(rv); |
961 |
newav[3] = newav[2]; |
962 |
newav[4] = newav[1]; |
963 |
} |
964 |
/* do top face */ |
965 |
for (i = 1; i < ac-1; i++) { |
966 |
if (hasnorm) { /* zero normals */ |
967 |
vent[1] = nvn[i-1]; |
968 |
if ((rv = mg_handle(MG_E_VERTEX, 2, vent)) != MG_OK) |
969 |
return(rv); |
970 |
if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK) |
971 |
return(rv); |
972 |
} |
973 |
newav[ac-1-i] = nvn[i-1]; /* reverse */ |
974 |
} |
975 |
if ((rv = mg_handle(MG_E_FACE, ac-1, newav)) != MG_OK) |
976 |
return(rv); |
977 |
/* do bottom face */ |
978 |
if (hasnorm) |
979 |
for (i = 1; i < ac-1; i++) { |
980 |
vent[1] = nvn[i-1]; |
981 |
vent[3] = av[i]; |
982 |
if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK) |
983 |
return(rv); |
984 |
if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK) |
985 |
return(rv); |
986 |
newav[i] = nvn[i-1]; |
987 |
} |
988 |
else |
989 |
for (i = 1; i < ac-1; i++) |
990 |
newav[i] = av[i]; |
991 |
newav[i] = NULL; |
992 |
if ((rv = mg_handle(MG_E_FACE, i, newav)) != MG_OK) |
993 |
return(rv); |
994 |
return(MG_OK); |
995 |
} |
996 |
|
997 |
|
998 |
static int |
999 |
put_cxy() /* put out current xy chromaticities */ |
1000 |
{ |
1001 |
static char xbuf[24], ybuf[24]; |
1002 |
static char *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf}; |
1003 |
int rv; |
1004 |
|
1005 |
sprintf(xbuf, "%.4f", c_ccolor->cx); |
1006 |
sprintf(ybuf, "%.4f", c_ccolor->cy); |
1007 |
if ((rv = mg_handle(MG_E_CXY, 3, ccom)) != MG_OK) |
1008 |
return(rv); |
1009 |
return(MG_OK); |
1010 |
} |
1011 |
|
1012 |
|
1013 |
static int |
1014 |
put_cspec() /* put out current color spectrum */ |
1015 |
{ |
1016 |
char wl[2][6], vbuf[C_CNSS][24]; |
1017 |
char *newav[C_CNSS+4]; |
1018 |
double sf; |
1019 |
register int i; |
1020 |
|
1021 |
if (mg_ehand[MG_E_CSPEC] != c_hcolor) { |
1022 |
sprintf(wl[0], "%d", C_CMINWL); |
1023 |
sprintf(wl[1], "%d", C_CMAXWL); |
1024 |
newav[0] = mg_ename[MG_E_CSPEC]; |
1025 |
newav[1] = wl[0]; |
1026 |
newav[2] = wl[1]; |
1027 |
sf = (double)C_CNSS / c_ccolor->ssum; |
1028 |
for (i = 0; i < C_CNSS; i++) { |
1029 |
sprintf(vbuf[i], "%.4f", sf*c_ccolor->ssamp[i]); |
1030 |
newav[i+3] = vbuf[i]; |
1031 |
} |
1032 |
newav[C_CNSS+3] = NULL; |
1033 |
if ((i = mg_handle(MG_E_CSPEC, C_CNSS+3, newav)) != MG_OK) |
1034 |
return(i); |
1035 |
} |
1036 |
return(MG_OK); |
1037 |
} |
1038 |
|
1039 |
|
1040 |
static int |
1041 |
e_cspec(ac, av) /* handle spectral color */ |
1042 |
int ac; |
1043 |
char **av; |
1044 |
{ |
1045 |
/* convert to xy chromaticity */ |
1046 |
c_ccvt(c_ccolor, C_CSXY); |
1047 |
/* if it's really their handler, use it */ |
1048 |
if (mg_ehand[MG_E_CXY] != c_hcolor) |
1049 |
return(put_cxy()); |
1050 |
return(MG_OK); |
1051 |
} |
1052 |
|
1053 |
|
1054 |
static int |
1055 |
e_cmix(ac, av) /* handle mixing of colors */ |
1056 |
int ac; |
1057 |
char **av; |
1058 |
{ |
1059 |
/* |
1060 |
* Contorted logic works as follows: |
1061 |
* 1. the colors are already mixed in c_hcolor() support function |
1062 |
* 2. if we would handle a spectral result, make sure it's not |
1063 |
* 3. if c_hcolor() would handle a spectral result, don't bother |
1064 |
* 4. otherwise, make cspec entity and pass it to their handler |
1065 |
* 5. if we have only xy results, handle it as c_spec() would |
1066 |
*/ |
1067 |
if (mg_ehand[MG_E_CSPEC] == e_cspec) |
1068 |
c_ccvt(c_ccolor, C_CSXY); |
1069 |
else if (c_ccolor->flags & C_CDSPEC) |
1070 |
return(put_cspec()); |
1071 |
if (mg_ehand[MG_E_CXY] != c_hcolor) |
1072 |
return(put_cxy()); |
1073 |
return(MG_OK); |
1074 |
} |
1075 |
|
1076 |
|
1077 |
static int |
1078 |
e_cct(ac, av) /* handle color temperature */ |
1079 |
int ac; |
1080 |
char **av; |
1081 |
{ |
1082 |
/* |
1083 |
* Logic is similar to e_cmix here. Support handler has already |
1084 |
* converted temperature to spectral color. Put it out as such |
1085 |
* if they support it, otherwise convert to xy chromaticity and |
1086 |
* put it out if they handle it. |
1087 |
*/ |
1088 |
if (mg_ehand[MG_E_CSPEC] != e_cspec) |
1089 |
return(put_cspec()); |
1090 |
c_ccvt(c_ccolor, C_CSXY); |
1091 |
if (mg_ehand[MG_E_CXY] != c_hcolor) |
1092 |
return(put_cxy()); |
1093 |
return(MG_OK); |
1094 |
} |