ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/parser.c
Revision: 1.27
Committed: Fri Feb 28 20:11:29 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 1.26: +4 -6 lines
Log Message:
Updates for 3.5 release

File Contents

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