ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/parser.c
Revision: 1.20
Committed: Wed Nov 8 09:40:22 1995 UTC (29 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.19: +17 -4 lines
Log Message:
added check for input line exceeding MG_MAXLINE

File Contents

# User Rev Content
1 greg 1.16 /* Copyright (c) 1995 Regents of the University of California */
2 greg 1.1
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 greg 1.18 /* Handler routine for unknown entities */
31    
32     int (*mg_uhand)() = mg_defuhand;
33    
34     unsigned mg_nunknown; /* count of unknown entities */
35    
36 greg 1.1 /* 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 greg 1.2 e_ies(), /* IES luminaire file */
66 greg 1.1 e_include(), /* include file */
67     e_sph(), /* sphere */
68 greg 1.13 e_cct(), /* color temperature */
69 greg 1.4 e_cmix(), /* color mixtures */
70 greg 1.6 e_cspec(), /* color spectra */
71 greg 1.1 e_cyl(), /* cylinder */
72     e_cone(), /* cone */
73 greg 1.4 e_prism(), /* prism */
74 greg 1.1 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 greg 1.17 ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
99 greg 1.1 } else
100 greg 1.17 uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
101 greg 1.1 if (mg_ehand[MG_E_CYL] == NULL) {
102     mg_ehand[MG_E_CYL] = e_cyl;
103 greg 1.17 ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
104 greg 1.1 } else
105 greg 1.17 uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
106 greg 1.1 if (mg_ehand[MG_E_CONE] == NULL) {
107     mg_ehand[MG_E_CONE] = e_cone;
108 greg 1.17 ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
109 greg 1.1 } else
110 greg 1.17 uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
111 greg 1.1 if (mg_ehand[MG_E_RING] == NULL) {
112     mg_ehand[MG_E_RING] = e_ring;
113 greg 1.17 ineed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX;
114 greg 1.1 } else
115 greg 1.17 uneed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX|1L<<MG_E_XF;
116 greg 1.4 if (mg_ehand[MG_E_PRISM] == NULL) {
117     mg_ehand[MG_E_PRISM] = e_prism;
118 greg 1.17 ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
119 greg 1.4 } else
120 greg 1.17 uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
121 greg 1.1 if (mg_ehand[MG_E_TORUS] == NULL) {
122     mg_ehand[MG_E_TORUS] = e_torus;
123 greg 1.17 ineed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX;
124 greg 1.1 } else
125 greg 1.17 uneed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX|1L<<MG_E_XF;
126 greg 1.4 if (mg_ehand[MG_E_COLOR] != NULL) {
127 greg 1.6 if (mg_ehand[MG_E_CMIX] == NULL) {
128 greg 1.4 mg_ehand[MG_E_CMIX] = e_cmix;
129 greg 1.17 ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT;
130 greg 1.6 }
131     if (mg_ehand[MG_E_CSPEC] == NULL) {
132 greg 1.4 mg_ehand[MG_E_CSPEC] = e_cspec;
133 greg 1.17 ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT;
134 greg 1.6 }
135 greg 1.13 if (mg_ehand[MG_E_CCT] == NULL) {
136     mg_ehand[MG_E_CCT] = e_cct;
137 greg 1.17 ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT;
138 greg 1.13 }
139 greg 1.4 }
140 greg 1.1 /* check for consistency */
141     if (mg_ehand[MG_E_FACE] != NULL)
142 greg 1.17 uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
143 greg 1.6 if (mg_ehand[MG_E_CXY] != NULL || mg_ehand[MG_E_CSPEC] != NULL ||
144     mg_ehand[MG_E_CMIX] != NULL)
145 greg 1.17 uneed |= 1L<<MG_E_COLOR;
146 greg 1.1 if (mg_ehand[MG_E_RD] != NULL || mg_ehand[MG_E_TD] != NULL ||
147 greg 1.15 mg_ehand[MG_E_IR] != NULL ||
148 greg 1.1 mg_ehand[MG_E_ED] != NULL ||
149     mg_ehand[MG_E_RS] != NULL ||
150 greg 1.11 mg_ehand[MG_E_TS] != NULL ||
151     mg_ehand[MG_E_SIDES] != NULL)
152 greg 1.17 uneed |= 1L<<MG_E_MATERIAL;
153 greg 1.1 for (i = 0; i < MG_NENTITIES; i++)
154 greg 1.17 if (uneed & 1L<<i && mg_ehand[i] == NULL) {
155 greg 1.1 fprintf(stderr, "Missing support for \"%s\" entity\n",
156     mg_ename[i]);
157     exit(1);
158     }
159     /* add support as needed */
160 greg 1.17 if (ineed & 1L<<MG_E_VERTEX && mg_ehand[MG_E_VERTEX] != c_hvertex)
161 greg 1.1 e_supp[MG_E_VERTEX] = c_hvertex;
162 greg 1.17 if (ineed & 1L<<MG_E_POINT && mg_ehand[MG_E_POINT] != c_hvertex)
163 greg 1.1 e_supp[MG_E_POINT] = c_hvertex;
164 greg 1.17 if (ineed & 1L<<MG_E_NORMAL && mg_ehand[MG_E_NORMAL] != c_hvertex)
165 greg 1.1 e_supp[MG_E_NORMAL] = c_hvertex;
166 greg 1.17 if (ineed & 1L<<MG_E_COLOR && mg_ehand[MG_E_COLOR] != c_hcolor)
167 greg 1.6 e_supp[MG_E_COLOR] = c_hcolor;
168 greg 1.17 if (ineed & 1L<<MG_E_CXY && mg_ehand[MG_E_CXY] != c_hcolor)
169 greg 1.6 e_supp[MG_E_CXY] = c_hcolor;
170 greg 1.17 if (ineed & 1L<<MG_E_CSPEC && mg_ehand[MG_E_CSPEC] != c_hcolor)
171 greg 1.6 e_supp[MG_E_CSPEC] = c_hcolor;
172 greg 1.17 if (ineed & 1L<<MG_E_CMIX && mg_ehand[MG_E_CMIX] != c_hcolor)
173 greg 1.6 e_supp[MG_E_CMIX] = c_hcolor;
174 greg 1.17 if (ineed & 1L<<MG_E_CCT && mg_ehand[MG_E_CCT] != c_hcolor)
175 greg 1.13 e_supp[MG_E_CCT] = c_hcolor;
176 greg 1.1 /* 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 greg 1.9 static LUTAB ent_tab = LU_SINIT(NULL,NULL); /* lookup table */
188 greg 1.1 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 greg 1.10 int
205     mg_handle(en, ac, av) /* pass entity to appropriate handler */
206 greg 1.1 register int en;
207     int ac;
208     char **av;
209     {
210     int rv;
211    
212 greg 1.18 if (en < 0 && (en = mg_entity(av[0])) < 0) { /* unknown entity */
213     if (mg_uhand != NULL)
214     return((*mg_uhand)(ac, av));
215 greg 1.1 return(MG_EUNK);
216 greg 1.18 }
217     if (e_supp[en] != NULL) { /* support handler */
218 greg 1.1 if ((rv = (*e_supp[en])(ac, av)) != MG_OK)
219     return(rv);
220     }
221 greg 1.18 return((*mg_ehand[en])(ac, av)); /* assigned handler */
222 greg 1.1 }
223    
224    
225     int
226     mg_open(ctx, fn) /* open new input file */
227     register MG_FCTXT *ctx;
228     char *fn;
229     {
230 greg 1.10 static int nfids;
231 greg 1.1 register char *cp;
232    
233 greg 1.10 ctx->fid = ++nfids;
234 greg 1.1 ctx->lineno = 0;
235     if (fn == NULL) {
236 greg 1.8 strcpy(ctx->fname, "<stdin>");
237 greg 1.1 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 greg 1.16 if (mg_file != NULL && (cp = strrchr(mg_file->fname, '/')) != NULL) {
244 greg 1.1 strcpy(ctx->fname, mg_file->fname);
245 greg 1.16 strcpy(ctx->fname+(cp-mg_file->fname+1), fn);
246     } else
247     strcpy(ctx->fname, fn);
248 greg 1.1 ctx->fp = fopen(ctx->fname, "r");
249 greg 1.8 if (ctx->fp == NULL)
250 greg 1.1 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 greg 1.10 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 greg 1.1 int
282 greg 1.10 mg_fgoto(pos) /* reposition input file pointer */
283     register MG_FPOS *pos;
284 greg 1.1 {
285 greg 1.10 if (pos->fid != mg_file->fid)
286     return(MG_ESEEK);
287     if (pos->lineno == mg_file->lineno)
288 greg 1.1 return(MG_OK);
289     if (mg_file->fp == stdin)
290     return(MG_ESEEK); /* cannot seek on standard input */
291 greg 1.10 if (fseek(mg_file->fp, pos->offset, 0) == EOF)
292 greg 1.1 return(MG_ESEEK);
293 greg 1.10 mg_file->lineno = pos->lineno;
294 greg 1.1 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 greg 1.10 return(mg_handle(-1, ap-argv, argv));
343 greg 1.1 }
344    
345    
346     int
347     mg_load(fn) /* load an MGF file */
348     char *fn;
349     {
350     MG_FCTXT cntxt;
351 greg 1.20 register int rval;
352 greg 1.1
353     if ((rval = mg_open(&cntxt, fn)) != MG_OK) {
354 greg 1.2 fprintf(stderr, "%s: %s\n", fn, mg_err[rval]);
355 greg 1.1 return(rval);
356     }
357 greg 1.20 while ((rval = mg_read()) > 0) { /* parse each line */
358     if (rval >= MG_MAXLINE-1 && cntxt.inpline[rval-1] != '\n') {
359     fprintf(stderr, "%s: %d: %s\n", cntxt.fname,
360     cntxt.lineno, mg_err[rval=MG_ELINE]);
361     break;
362     }
363 greg 1.1 if ((rval = mg_parse()) != MG_OK) {
364     fprintf(stderr, "%s: %d: %s:\n%s", cntxt.fname,
365     cntxt.lineno, mg_err[rval],
366     cntxt.inpline);
367     break;
368     }
369 greg 1.20 }
370 greg 1.1 mg_close();
371     return(rval);
372 greg 1.18 }
373    
374    
375     int
376     mg_defuhand(ac, av) /* default handler for unknown entities */
377     int ac;
378     char **av;
379     {
380     if (mg_nunknown++ == 0) /* report first incident */
381     fprintf(stderr, "%s: %d: %s: %s\n", mg_file->fname,
382     mg_file->lineno, mg_err[MG_EUNK], av[0]);
383     return(MG_OK);
384 greg 1.1 }
385    
386    
387     void
388     mg_clear() /* clear parser history */
389     {
390     c_clearall(); /* clear context tables */
391     mg_file = NULL; /* reset our context */
392     }
393    
394    
395     /****************************************************************************
396     * The following routines handle unsupported entities
397     */
398    
399    
400     static int
401     e_any_toss(ac, av) /* discard an unwanted entity */
402     int ac;
403     char **av;
404     {
405     return(MG_OK);
406     }
407    
408    
409     static int
410     e_include(ac, av) /* include file */
411     int ac;
412     char **av;
413     {
414 greg 1.10 char *xfarg[MG_MAXARGC];
415 greg 1.1 MG_FCTXT ictx;
416 greg 1.16 XF_SPEC *xf_orig = xf_context;
417 greg 1.20 register int rv;
418 greg 1.1
419     if (ac < 2)
420     return(MG_EARGC);
421     if ((rv = mg_open(&ictx, av[1])) != MG_OK)
422     return(rv);
423 greg 1.10 if (ac > 2) {
424     register int i;
425    
426     xfarg[0] = mg_ename[MG_E_XF];
427     for (i = 1; i < ac-1; i++)
428     xfarg[i] = av[i+1];
429     xfarg[ac-1] = NULL;
430     if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK)
431     return(rv);
432 greg 1.1 }
433 greg 1.16 do {
434 greg 1.20 while ((rv = mg_read()) > 0) {
435     if (rv >= MG_MAXLINE-1 && ictx.inpline[rv-1] != '\n') {
436     fprintf(stderr, "%s: %d: %s\n", ictx.fname,
437     ictx.lineno, mg_err[MG_ELINE]);
438     mg_close();
439     return(MG_EINCL);
440     }
441 greg 1.10 if ((rv = mg_parse()) != MG_OK) {
442     fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname,
443     ictx.lineno, mg_err[rv],
444     ictx.inpline);
445     mg_close();
446     return(MG_EINCL);
447     }
448 greg 1.20 }
449 greg 1.10 if (ac > 2)
450     if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK)
451     return(rv);
452 greg 1.16 } while (xf_context != xf_orig);
453 greg 1.1 mg_close();
454     return(MG_OK);
455     }
456    
457    
458     static void
459     make_axes(u, v, w) /* compute u and v given w (normalized) */
460     FVECT u, v, w;
461     {
462     register int i;
463    
464     v[0] = v[1] = v[2] = 0.;
465     for (i = 0; i < 3; i++)
466     if (w[i] < .6 && w[i] > -.6)
467     break;
468     v[i] = 1.;
469     fcross(u, v, w);
470     normalize(u);
471     fcross(v, w, u);
472     }
473    
474    
475     static int
476     e_sph(ac, av) /* expand a sphere into cones */
477     int ac;
478     char **av;
479     {
480     static char p2x[24], p2y[24], p2z[24], r1[24], r2[24];
481     static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_sv1","=","_sv2"};
482     static char *v2ent[4] = {mg_ename[MG_E_VERTEX],"_sv2","="};
483     static char *p2ent[5] = {mg_ename[MG_E_POINT],p2x,p2y,p2z};
484     static char *conent[6] = {mg_ename[MG_E_CONE],"_sv1",r1,"_sv2",r2};
485     register C_VERTEX *cv;
486     register int i;
487     int rval;
488     double rad;
489     double theta;
490    
491     if (ac != 3)
492     return(MG_EARGC);
493     if ((cv = c_getvert(av[1])) == NULL)
494     return(MG_EUNDEF);
495     if (!isflt(av[2]))
496     return(MG_ETYPE);
497     rad = atof(av[2]);
498     /* initialize */
499     warpconends = 1;
500 greg 1.10 if ((rval = mg_handle(MG_E_VERTEX, 3, v2ent)) != MG_OK)
501 greg 1.1 return(rval);
502     sprintf(p2x, FLTFMT, cv->p[0]);
503     sprintf(p2y, FLTFMT, cv->p[1]);
504     sprintf(p2z, FLTFMT, cv->p[2]+rad);
505 greg 1.10 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
506 greg 1.1 return(rval);
507     r2[0] = '0'; r2[1] = '\0';
508     for (i = 1; i <= 2*mg_nqcdivs; i++) {
509     theta = i*(PI/2)/mg_nqcdivs;
510 greg 1.10 if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
511 greg 1.1 return(rval);
512     sprintf(p2z, FLTFMT, cv->p[2]+rad*cos(theta));
513 greg 1.10 if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
514 greg 1.1 return(rval);
515 greg 1.10 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
516 greg 1.1 return(rval);
517     strcpy(r1, r2);
518     sprintf(r2, FLTFMT, rad*sin(theta));
519 greg 1.10 if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
520 greg 1.1 return(rval);
521     }
522     warpconends = 0;
523     return(MG_OK);
524     }
525    
526    
527     static int
528     e_torus(ac, av) /* expand a torus into cones */
529     int ac;
530     char **av;
531     {
532     static char p2[3][24], r1[24], r2[24];
533     static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_tv1","=","_tv2"};
534     static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_tv2","="};
535     static char *p2ent[5] = {mg_ename[MG_E_POINT],p2[0],p2[1],p2[2]};
536     static char *conent[6] = {mg_ename[MG_E_CONE],"_tv1",r1,"_tv2",r2};
537     register C_VERTEX *cv;
538     register int i, j;
539     int rval;
540     int sgn;
541     double minrad, maxrad, avgrad;
542     double theta;
543    
544     if (ac != 4)
545     return(MG_EARGC);
546     if ((cv = c_getvert(av[1])) == NULL)
547     return(MG_EUNDEF);
548 greg 1.3 if (is0vect(cv->n))
549 greg 1.1 return(MG_EILL);
550     if (!isflt(av[2]) || !isflt(av[3]))
551     return(MG_ETYPE);
552     minrad = atof(av[2]);
553 greg 1.3 round0(minrad);
554 greg 1.1 maxrad = atof(av[3]);
555     /* check orientation */
556     if (minrad > 0.)
557     sgn = 1;
558     else if (minrad < 0.)
559     sgn = -1;
560     else if (maxrad > 0.)
561     sgn = 1;
562     else if (maxrad < 0.)
563     sgn = -1;
564     else
565     return(MG_EILL);
566     if (sgn*(maxrad-minrad) <= 0.)
567     return(MG_EILL);
568     /* initialize */
569     warpconends = 1;
570     v2ent[3] = av[1];
571     for (j = 0; j < 3; j++)
572     sprintf(p2[j], FLTFMT, cv->p[j] +
573     .5*sgn*(maxrad-minrad)*cv->n[j]);
574 greg 1.10 if ((rval = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
575 greg 1.1 return(rval);
576 greg 1.10 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
577 greg 1.1 return(rval);
578     sprintf(r2, FLTFMT, avgrad=.5*(minrad+maxrad));
579     /* run outer section */
580     for (i = 1; i <= 2*mg_nqcdivs; i++) {
581     theta = i*(PI/2)/mg_nqcdivs;
582 greg 1.10 if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
583 greg 1.1 return(rval);
584     for (j = 0; j < 3; j++)
585     sprintf(p2[j], FLTFMT, cv->p[j] +
586     .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
587 greg 1.10 if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
588 greg 1.1 return(rval);
589 greg 1.10 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
590 greg 1.1 return(rval);
591     strcpy(r1, r2);
592     sprintf(r2, FLTFMT, avgrad + .5*(maxrad-minrad)*sin(theta));
593 greg 1.10 if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
594 greg 1.1 return(rval);
595     }
596     /* run inner section */
597     sprintf(r2, FLTFMT, -.5*(minrad+maxrad));
598     for ( ; i <= 4*mg_nqcdivs; i++) {
599     theta = i*(PI/2)/mg_nqcdivs;
600     for (j = 0; j < 3; j++)
601     sprintf(p2[j], FLTFMT, cv->p[j] +
602     .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
603 greg 1.10 if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
604 greg 1.1 return(rval);
605 greg 1.10 if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
606 greg 1.1 return(rval);
607 greg 1.10 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
608 greg 1.1 return(rval);
609     strcpy(r1, r2);
610     sprintf(r2, FLTFMT, -avgrad - .5*(maxrad-minrad)*sin(theta));
611 greg 1.10 if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
612 greg 1.1 return(rval);
613     }
614     warpconends = 0;
615     return(MG_OK);
616     }
617    
618    
619     static int
620     e_cyl(ac, av) /* replace a cylinder with equivalent cone */
621     int ac;
622     char **av;
623     {
624     static char *avnew[6] = {mg_ename[MG_E_CONE]};
625    
626     if (ac != 4)
627     return(MG_EARGC);
628     avnew[1] = av[1];
629     avnew[2] = av[2];
630     avnew[3] = av[3];
631     avnew[4] = av[2];
632 greg 1.10 return(mg_handle(MG_E_CONE, 5, avnew));
633 greg 1.1 }
634    
635    
636     static int
637     e_ring(ac, av) /* turn a ring into polygons */
638     int ac;
639     char **av;
640     {
641     static char p3[3][24], p4[3][24];
642     static char *nzent[5] = {mg_ename[MG_E_NORMAL],"0","0","0"};
643     static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_rv1","="};
644     static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_rv2","=","_rv3"};
645     static char *v3ent[4] = {mg_ename[MG_E_VERTEX],"_rv3","="};
646     static char *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]};
647     static char *v4ent[4] = {mg_ename[MG_E_VERTEX],"_rv4","="};
648     static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]};
649     static char *fent[6] = {mg_ename[MG_E_FACE],"_rv1","_rv2","_rv3","_rv4"};
650     register C_VERTEX *cv;
651     register int i, j;
652     FVECT u, v;
653     double minrad, maxrad;
654     int rv;
655     double theta, d;
656    
657     if (ac != 4)
658     return(MG_EARGC);
659     if ((cv = c_getvert(av[1])) == NULL)
660     return(MG_EUNDEF);
661 greg 1.3 if (is0vect(cv->n))
662 greg 1.1 return(MG_EILL);
663     if (!isflt(av[2]) || !isflt(av[3]))
664     return(MG_ETYPE);
665     minrad = atof(av[2]);
666 greg 1.3 round0(minrad);
667 greg 1.1 maxrad = atof(av[3]);
668     if (minrad < 0. || maxrad <= minrad)
669     return(MG_EILL);
670     /* initialize */
671     make_axes(u, v, cv->n);
672     for (j = 0; j < 3; j++)
673     sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*u[j]);
674 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
675 greg 1.1 return(rv);
676 greg 1.10 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
677 greg 1.1 return(rv);
678     if (minrad == 0.) { /* closed */
679     v1ent[3] = av[1];
680 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
681 greg 1.1 return(rv);
682 greg 1.10 if ((rv = mg_handle(MG_E_NORMAL, 4, nzent)) != MG_OK)
683 greg 1.1 return(rv);
684     for (i = 1; i <= 4*mg_nqcdivs; i++) {
685     theta = i*(PI/2)/mg_nqcdivs;
686 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
687 greg 1.1 return(rv);
688     for (j = 0; j < 3; j++)
689     sprintf(p3[j], FLTFMT, cv->p[j] +
690     maxrad*u[j]*cos(theta) +
691     maxrad*v[j]*sin(theta));
692 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
693 greg 1.1 return(rv);
694 greg 1.10 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
695 greg 1.1 return(rv);
696 greg 1.10 if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK)
697 greg 1.1 return(rv);
698     }
699     } else { /* open */
700 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
701 greg 1.1 return(rv);
702     for (j = 0; j < 3; j++)
703     sprintf(p4[j], FLTFMT, cv->p[j] + minrad*u[j]);
704 greg 1.10 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
705 greg 1.1 return(rv);
706     v1ent[3] = "_rv4";
707     for (i = 1; i <= 4*mg_nqcdivs; i++) {
708     theta = i*(PI/2)/mg_nqcdivs;
709 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
710 greg 1.1 return(rv);
711 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
712 greg 1.1 return(rv);
713     for (j = 0; j < 3; j++) {
714     d = u[j]*cos(theta) + v[j]*sin(theta);
715     sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*d);
716     sprintf(p4[j], FLTFMT, cv->p[j] + minrad*d);
717     }
718 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
719 greg 1.1 return(rv);
720 greg 1.10 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
721 greg 1.1 return(rv);
722 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
723 greg 1.1 return(rv);
724 greg 1.10 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
725 greg 1.1 return(rv);
726 greg 1.10 if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
727 greg 1.1 return(rv);
728     }
729     }
730     return(MG_OK);
731     }
732    
733    
734     static int
735     e_cone(ac, av) /* turn a cone into polygons */
736     int ac;
737     char **av;
738     {
739     static char p3[3][24], p4[3][24], n3[3][24], n4[3][24];
740     static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_cv1","="};
741     static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_cv2","=","_cv3"};
742     static char *v3ent[4] = {mg_ename[MG_E_VERTEX],"_cv3","="};
743     static char *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]};
744     static char *n3ent[5] = {mg_ename[MG_E_NORMAL],n3[0],n3[1],n3[2]};
745     static char *v4ent[4] = {mg_ename[MG_E_VERTEX],"_cv4","="};
746     static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]};
747     static char *n4ent[5] = {mg_ename[MG_E_NORMAL],n4[0],n4[1],n4[2]};
748     static char *fent[6] = {mg_ename[MG_E_FACE],"_cv1","_cv2","_cv3","_cv4"};
749 greg 1.19 char *v1n;
750 greg 1.1 register C_VERTEX *cv1, *cv2;
751     register int i, j;
752     FVECT u, v, w;
753     double rad1, rad2;
754     int sgn;
755     double n1off, n2off;
756     double d;
757     int rv;
758     double theta;
759    
760     if (ac != 5)
761     return(MG_EARGC);
762     if ((cv1 = c_getvert(av[1])) == NULL ||
763     (cv2 = c_getvert(av[3])) == NULL)
764     return(MG_EUNDEF);
765 greg 1.19 v1n = av[1];
766 greg 1.1 if (!isflt(av[2]) || !isflt(av[4]))
767     return(MG_ETYPE);
768     rad1 = atof(av[2]);
769 greg 1.3 round0(rad1);
770 greg 1.1 rad2 = atof(av[4]);
771 greg 1.3 round0(rad2);
772 greg 1.1 if (rad1 == 0.) {
773     if (rad2 == 0.)
774     return(MG_EILL);
775     } else if (rad2 != 0.) {
776     if (rad1 < 0. ^ rad2 < 0.)
777     return(MG_EILL);
778     } else { /* swap */
779     C_VERTEX *cv;
780    
781     cv = cv1;
782     cv1 = cv2;
783     cv2 = cv;
784 greg 1.19 v1n = av[3];
785 greg 1.1 d = rad1;
786     rad1 = rad2;
787     rad2 = d;
788     }
789     sgn = rad2 < 0. ? -1 : 1;
790     /* initialize */
791     for (j = 0; j < 3; j++)
792     w[j] = cv1->p[j] - cv2->p[j];
793     if ((d = normalize(w)) == 0.)
794     return(MG_EILL);
795     n1off = n2off = (rad2 - rad1)/d;
796 greg 1.3 if (warpconends) { /* hack for e_sph and e_torus */
797     d = atan(n2off) - (PI/4)/mg_nqcdivs;
798     if (d <= -PI/2+FTINY)
799     n2off = -FHUGE;
800     else
801     n2off = tan(d);
802     }
803 greg 1.1 make_axes(u, v, w);
804     for (j = 0; j < 3; j++) {
805     sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*u[j]);
806 greg 1.3 if (n2off <= -FHUGE)
807     sprintf(n3[j], FLTFMT, -w[j]);
808     else
809     sprintf(n3[j], FLTFMT, u[j] + w[j]*n2off);
810 greg 1.1 }
811 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
812 greg 1.1 return(rv);
813 greg 1.10 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
814 greg 1.1 return(rv);
815 greg 1.10 if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
816 greg 1.1 return(rv);
817     if (rad1 == 0.) { /* triangles */
818 greg 1.19 v1ent[3] = v1n;
819 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
820 greg 1.1 return(rv);
821     for (j = 0; j < 3; j++)
822     sprintf(n4[j], FLTFMT, w[j]);
823 greg 1.10 if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
824 greg 1.1 return(rv);
825     for (i = 1; i <= 4*mg_nqcdivs; i++) {
826     theta = sgn*i*(PI/2)/mg_nqcdivs;
827 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
828 greg 1.1 return(rv);
829     for (j = 0; j < 3; j++) {
830     d = u[j]*cos(theta) + v[j]*sin(theta);
831     sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d);
832 greg 1.3 if (n2off > -FHUGE)
833     sprintf(n3[j], FLTFMT, d + w[j]*n2off);
834 greg 1.1 }
835 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
836 greg 1.1 return(rv);
837 greg 1.10 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
838 greg 1.1 return(rv);
839 greg 1.3 if (n2off > -FHUGE &&
840 greg 1.10 (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
841 greg 1.1 return(rv);
842 greg 1.10 if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK)
843 greg 1.1 return(rv);
844     }
845     } else { /* quads */
846     v1ent[3] = "_cv4";
847 greg 1.3 if (warpconends) { /* hack for e_sph and e_torus */
848     d = atan(n1off) + (PI/4)/mg_nqcdivs;
849     if (d >= PI/2-FTINY)
850     n1off = FHUGE;
851     else
852     n1off = tan(atan(n1off)+(PI/4)/mg_nqcdivs);
853     }
854 greg 1.1 for (j = 0; j < 3; j++) {
855     sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*u[j]);
856 greg 1.3 if (n1off >= FHUGE)
857     sprintf(n4[j], FLTFMT, w[j]);
858     else
859     sprintf(n4[j], FLTFMT, u[j] + w[j]*n1off);
860 greg 1.1 }
861 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
862 greg 1.1 return(rv);
863 greg 1.10 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
864 greg 1.1 return(rv);
865 greg 1.10 if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
866 greg 1.1 return(rv);
867     for (i = 1; i <= 4*mg_nqcdivs; i++) {
868     theta = sgn*i*(PI/2)/mg_nqcdivs;
869 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
870 greg 1.1 return(rv);
871 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
872 greg 1.1 return(rv);
873     for (j = 0; j < 3; j++) {
874     d = u[j]*cos(theta) + v[j]*sin(theta);
875     sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d);
876 greg 1.3 if (n2off > -FHUGE)
877     sprintf(n3[j], FLTFMT, d + w[j]*n2off);
878 greg 1.1 sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*d);
879 greg 1.3 if (n1off < FHUGE)
880     sprintf(n4[j], FLTFMT, d + w[j]*n1off);
881 greg 1.1 }
882 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
883 greg 1.1 return(rv);
884 greg 1.10 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
885 greg 1.1 return(rv);
886 greg 1.3 if (n2off > -FHUGE &&
887 greg 1.10 (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
888 greg 1.1 return(rv);
889 greg 1.10 if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
890 greg 1.1 return(rv);
891 greg 1.10 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
892 greg 1.1 return(rv);
893 greg 1.3 if (n1off < FHUGE &&
894 greg 1.10 (rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
895 greg 1.1 return(rv);
896 greg 1.10 if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
897 greg 1.1 return(rv);
898     }
899 greg 1.4 }
900     return(MG_OK);
901     }
902    
903    
904     static int
905     e_prism(ac, av) /* turn a prism into polygons */
906     int ac;
907     char **av;
908     {
909     static char p[3][24];
910 greg 1.12 static char *vent[5] = {mg_ename[MG_E_VERTEX],NULL,"="};
911 greg 1.4 static char *pent[5] = {mg_ename[MG_E_POINT],p[0],p[1],p[2]};
912 greg 1.12 static char *znorm[5] = {mg_ename[MG_E_NORMAL],"0","0","0"};
913 greg 1.4 char *newav[MG_MAXARGC], nvn[MG_MAXARGC-1][8];
914     double length;
915 greg 1.12 int hasnorm;
916 greg 1.4 FVECT v1, v2, v3, norm;
917     register C_VERTEX *cv;
918     C_VERTEX *cv0;
919     int rv;
920     register int i, j;
921 greg 1.12 /* check arguments */
922 greg 1.4 if (ac < 5)
923     return(MG_EARGC);
924 greg 1.5 if (!isflt(av[ac-1]))
925 greg 1.4 return(MG_ETYPE);
926 greg 1.5 length = atof(av[ac-1]);
927 greg 1.4 if (length <= FTINY && length >= -FTINY)
928     return(MG_EILL);
929 greg 1.12 /* compute face normal */
930 greg 1.7 if ((cv0 = c_getvert(av[1])) == NULL)
931 greg 1.4 return(MG_EUNDEF);
932 greg 1.12 hasnorm = 0;
933 greg 1.4 norm[0] = norm[1] = norm[2] = 0.;
934     v1[0] = v1[1] = v1[2] = 0.;
935     for (i = 2; i < ac-1; i++) {
936 greg 1.5 if ((cv = c_getvert(av[i])) == NULL)
937 greg 1.4 return(MG_EUNDEF);
938 greg 1.12 hasnorm += !is0vect(cv->n);
939 greg 1.4 v2[0] = cv->p[0] - cv0->p[0];
940     v2[1] = cv->p[1] - cv0->p[1];
941     v2[2] = cv->p[2] - cv0->p[2];
942     fcross(v3, v1, v2);
943     norm[0] += v3[0];
944     norm[1] += v3[1];
945     norm[2] += v3[2];
946     VCOPY(v1, v2);
947     }
948     if (normalize(norm) == 0.)
949     return(MG_EILL);
950 greg 1.12 /* create moved vertices */
951 greg 1.4 for (i = 1; i < ac-1; i++) {
952     sprintf(nvn[i-1], "_pv%d", i);
953     vent[1] = nvn[i-1];
954 greg 1.12 vent[3] = av[i];
955     if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK)
956 greg 1.4 return(rv);
957 greg 1.5 cv = c_getvert(av[i]); /* checked above */
958 greg 1.4 for (j = 0; j < 3; j++)
959     sprintf(p[j], FLTFMT, cv->p[j] - length*norm[j]);
960 greg 1.10 if ((rv = mg_handle(MG_E_POINT, 4, pent)) != MG_OK)
961 greg 1.4 return(rv);
962     }
963 greg 1.12 /* make faces */
964     newav[0] = mg_ename[MG_E_FACE];
965 greg 1.4 /* do the side faces */
966     newav[5] = NULL;
967 greg 1.5 newav[3] = av[ac-2];
968 greg 1.4 newav[4] = nvn[ac-3];
969     for (i = 1; i < ac-1; i++) {
970     newav[1] = nvn[i-1];
971 greg 1.5 newav[2] = av[i];
972 greg 1.10 if ((rv = mg_handle(MG_E_FACE, 5, newav)) != MG_OK)
973 greg 1.4 return(rv);
974     newav[3] = newav[2];
975     newav[4] = newav[1];
976 greg 1.6 }
977 greg 1.12 /* do top face */
978     for (i = 1; i < ac-1; i++) {
979     if (hasnorm) { /* zero normals */
980     vent[1] = nvn[i-1];
981     if ((rv = mg_handle(MG_E_VERTEX, 2, vent)) != MG_OK)
982     return(rv);
983     if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK)
984     return(rv);
985     }
986     newav[ac-1-i] = nvn[i-1]; /* reverse */
987     }
988     if ((rv = mg_handle(MG_E_FACE, ac-1, newav)) != MG_OK)
989     return(rv);
990     /* do bottom face */
991     if (hasnorm)
992     for (i = 1; i < ac-1; i++) {
993     vent[1] = nvn[i-1];
994     vent[3] = av[i];
995     if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK)
996     return(rv);
997     if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK)
998     return(rv);
999     newav[i] = nvn[i-1];
1000     }
1001     else
1002     for (i = 1; i < ac-1; i++)
1003     newav[i] = av[i];
1004     newav[i] = NULL;
1005     if ((rv = mg_handle(MG_E_FACE, i, newav)) != MG_OK)
1006     return(rv);
1007 greg 1.6 return(MG_OK);
1008     }
1009    
1010    
1011     static int
1012 greg 1.13 put_cxy() /* put out current xy chromaticities */
1013 greg 1.6 {
1014     static char xbuf[24], ybuf[24];
1015     static char *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf};
1016     int rv;
1017    
1018 greg 1.13 sprintf(xbuf, "%.4f", c_ccolor->cx);
1019     sprintf(ybuf, "%.4f", c_ccolor->cy);
1020     if ((rv = mg_handle(MG_E_CXY, 3, ccom)) != MG_OK)
1021     return(rv);
1022     return(MG_OK);
1023     }
1024    
1025    
1026     static int
1027     put_cspec() /* put out current color spectrum */
1028     {
1029     char wl[2][6], vbuf[C_CNSS][24];
1030     char *newav[C_CNSS+4];
1031     double sf;
1032     register int i;
1033    
1034     if (mg_ehand[MG_E_CSPEC] != c_hcolor) {
1035     sprintf(wl[0], "%d", C_CMINWL);
1036     sprintf(wl[1], "%d", C_CMAXWL);
1037     newav[0] = mg_ename[MG_E_CSPEC];
1038     newav[1] = wl[0];
1039     newav[2] = wl[1];
1040     sf = (double)C_CNSS / c_ccolor->ssum;
1041     for (i = 0; i < C_CNSS; i++) {
1042 greg 1.14 sprintf(vbuf[i], "%.4f", sf*c_ccolor->ssamp[i]);
1043 greg 1.13 newav[i+3] = vbuf[i];
1044     }
1045     newav[C_CNSS+3] = NULL;
1046     if ((i = mg_handle(MG_E_CSPEC, C_CNSS+3, newav)) != MG_OK)
1047     return(i);
1048     }
1049     return(MG_OK);
1050     }
1051    
1052    
1053     static int
1054     e_cspec(ac, av) /* handle spectral color */
1055     int ac;
1056     char **av;
1057     {
1058     /* convert to xy chromaticity */
1059 greg 1.6 c_ccvt(c_ccolor, C_CSXY);
1060     /* if it's really their handler, use it */
1061 greg 1.13 if (mg_ehand[MG_E_CXY] != c_hcolor)
1062     return(put_cxy());
1063 greg 1.6 return(MG_OK);
1064     }
1065    
1066    
1067     static int
1068     e_cmix(ac, av) /* handle mixing of colors */
1069     int ac;
1070     char **av;
1071     {
1072     /*
1073     * Contorted logic works as follows:
1074     * 1. the colors are already mixed in c_hcolor() support function
1075     * 2. if we would handle a spectral result, make sure it's not
1076     * 3. if c_hcolor() would handle a spectral result, don't bother
1077     * 4. otherwise, make cspec entity and pass it to their handler
1078     * 5. if we have only xy results, handle it as c_spec() would
1079     */
1080     if (mg_ehand[MG_E_CSPEC] == e_cspec)
1081     c_ccvt(c_ccolor, C_CSXY);
1082 greg 1.13 else if (c_ccolor->flags & C_CDSPEC)
1083     return(put_cspec());
1084     if (mg_ehand[MG_E_CXY] != c_hcolor)
1085     return(put_cxy());
1086     return(MG_OK);
1087     }
1088    
1089    
1090     static int
1091     e_cct(ac, av) /* handle color temperature */
1092     int ac;
1093     char **av;
1094     {
1095     /*
1096     * Logic is similar to e_cmix here. Support handler has already
1097     * converted temperature to spectral color. Put it out as such
1098     * if they support it, otherwise convert to xy chromaticity and
1099     * put it out if they handle it.
1100     */
1101     if (mg_ehand[MG_E_CSPEC] != e_cspec)
1102     return(put_cspec());
1103     c_ccvt(c_ccolor, C_CSXY);
1104     if (mg_ehand[MG_E_CXY] != c_hcolor)
1105     return(put_cxy());
1106 greg 1.1 return(MG_OK);
1107     }