ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/parser.c
Revision: 1.22
Committed: Fri Dec 1 11:03:57 1995 UTC (28 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.21: +13 -10 lines
Log Message:
made it so the stored input line includes escaped newlines

File Contents

# Content
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 len += strlen(mg_file->inpline+len);
308 if (len >= MG_MAXLINE-1)
309 return(len);
310 mg_file->lineno++;
311 } while (len > 1 && mg_file->inpline[len-2] == '\\');
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, *cp2, **ap;
324 /* copy line, removing escape chars */
325 cp = abuf; cp2 = mg_file->inpline;
326 while ((*cp++ = *cp2++))
327 if (cp2[0] == '\n' && cp2[-1] == '\\')
328 cp--;
329 cp = abuf; ap = argv; /* break into words */
330 for ( ; ; ) {
331 while (isspace(*cp))
332 *cp++ = '\0';
333 if (!*cp)
334 break;
335 if (ap - argv >= MG_MAXARGC-1)
336 return(MG_EARGC);
337 *ap++ = cp;
338 while (*++cp && !isspace(*cp))
339 ;
340 }
341 if (ap == argv)
342 return(MG_OK); /* no words in line */
343 *ap = NULL;
344 /* else handle it */
345 return(mg_handle(-1, ap-argv, argv));
346 }
347
348
349 int
350 mg_load(fn) /* load an MGF file */
351 char *fn;
352 {
353 MG_FCTXT cntxt;
354 int rval;
355 register int nbr;
356
357 if ((rval = mg_open(&cntxt, fn)) != MG_OK) {
358 fprintf(stderr, "%s: %s\n", fn, mg_err[rval]);
359 return(rval);
360 }
361 while ((nbr = mg_read()) > 0) { /* parse each line */
362 if (nbr >= MG_MAXLINE-1) {
363 fprintf(stderr, "%s: %d: %s\n", cntxt.fname,
364 cntxt.lineno, mg_err[rval=MG_ELINE]);
365 break;
366 }
367 if ((rval = mg_parse()) != MG_OK) {
368 fprintf(stderr, "%s: %d: %s:\n%s", cntxt.fname,
369 cntxt.lineno, mg_err[rval],
370 cntxt.inpline);
371 break;
372 }
373 }
374 mg_close();
375 return(rval);
376 }
377
378
379 int
380 mg_defuhand(ac, av) /* default handler for unknown entities */
381 int ac;
382 char **av;
383 {
384 if (mg_nunknown++ == 0) /* report first incident */
385 fprintf(stderr, "%s: %d: %s: %s\n", mg_file->fname,
386 mg_file->lineno, mg_err[MG_EUNK], av[0]);
387 return(MG_OK);
388 }
389
390
391 void
392 mg_clear() /* clear parser history */
393 {
394 c_clearall(); /* clear context tables */
395 mg_file = NULL; /* reset our context */
396 }
397
398
399 /****************************************************************************
400 * The following routines handle unsupported entities
401 */
402
403
404 static int
405 e_any_toss(ac, av) /* discard an unwanted entity */
406 int ac;
407 char **av;
408 {
409 return(MG_OK);
410 }
411
412
413 static int
414 e_include(ac, av) /* include file */
415 int ac;
416 char **av;
417 {
418 char *xfarg[MG_MAXARGC];
419 MG_FCTXT ictx;
420 XF_SPEC *xf_orig = xf_context;
421 register int rv;
422
423 if (ac < 2)
424 return(MG_EARGC);
425 if ((rv = mg_open(&ictx, av[1])) != MG_OK)
426 return(rv);
427 if (ac > 2) {
428 register int i;
429
430 xfarg[0] = mg_ename[MG_E_XF];
431 for (i = 1; i < ac-1; i++)
432 xfarg[i] = av[i+1];
433 xfarg[ac-1] = NULL;
434 if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK) {
435 mg_close();
436 return(rv);
437 }
438 }
439 do {
440 while ((rv = mg_read()) > 0) {
441 if (rv >= MG_MAXLINE-1) {
442 fprintf(stderr, "%s: %d: %s\n", ictx.fname,
443 ictx.lineno, mg_err[MG_ELINE]);
444 mg_close();
445 return(MG_EINCL);
446 }
447 if ((rv = mg_parse()) != MG_OK) {
448 fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname,
449 ictx.lineno, mg_err[rv],
450 ictx.inpline);
451 mg_close();
452 return(MG_EINCL);
453 }
454 }
455 if (ac > 2)
456 if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK) {
457 mg_close();
458 return(rv);
459 }
460 } while (xf_context != xf_orig);
461 mg_close();
462 return(MG_OK);
463 }
464
465
466 static void
467 make_axes(u, v, w) /* compute u and v given w (normalized) */
468 FVECT u, v, w;
469 {
470 register int i;
471
472 v[0] = v[1] = v[2] = 0.;
473 for (i = 0; i < 3; i++)
474 if (w[i] < .6 && w[i] > -.6)
475 break;
476 v[i] = 1.;
477 fcross(u, v, w);
478 normalize(u);
479 fcross(v, w, u);
480 }
481
482
483 static int
484 e_sph(ac, av) /* expand a sphere into cones */
485 int ac;
486 char **av;
487 {
488 static char p2x[24], p2y[24], p2z[24], r1[24], r2[24];
489 static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_sv1","=","_sv2"};
490 static char *v2ent[4] = {mg_ename[MG_E_VERTEX],"_sv2","="};
491 static char *p2ent[5] = {mg_ename[MG_E_POINT],p2x,p2y,p2z};
492 static char *conent[6] = {mg_ename[MG_E_CONE],"_sv1",r1,"_sv2",r2};
493 register C_VERTEX *cv;
494 register int i;
495 int rval;
496 double rad;
497 double theta;
498
499 if (ac != 3)
500 return(MG_EARGC);
501 if ((cv = c_getvert(av[1])) == NULL)
502 return(MG_EUNDEF);
503 if (!isflt(av[2]))
504 return(MG_ETYPE);
505 rad = atof(av[2]);
506 /* initialize */
507 warpconends = 1;
508 if ((rval = mg_handle(MG_E_VERTEX, 3, v2ent)) != MG_OK)
509 return(rval);
510 sprintf(p2x, FLTFMT, cv->p[0]);
511 sprintf(p2y, FLTFMT, cv->p[1]);
512 sprintf(p2z, FLTFMT, cv->p[2]+rad);
513 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
514 return(rval);
515 r2[0] = '0'; r2[1] = '\0';
516 for (i = 1; i <= 2*mg_nqcdivs; i++) {
517 theta = i*(PI/2)/mg_nqcdivs;
518 if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
519 return(rval);
520 sprintf(p2z, FLTFMT, cv->p[2]+rad*cos(theta));
521 if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
522 return(rval);
523 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
524 return(rval);
525 strcpy(r1, r2);
526 sprintf(r2, FLTFMT, rad*sin(theta));
527 if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
528 return(rval);
529 }
530 warpconends = 0;
531 return(MG_OK);
532 }
533
534
535 static int
536 e_torus(ac, av) /* expand a torus into cones */
537 int ac;
538 char **av;
539 {
540 static char p2[3][24], r1[24], r2[24];
541 static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_tv1","=","_tv2"};
542 static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_tv2","="};
543 static char *p2ent[5] = {mg_ename[MG_E_POINT],p2[0],p2[1],p2[2]};
544 static char *conent[6] = {mg_ename[MG_E_CONE],"_tv1",r1,"_tv2",r2};
545 register C_VERTEX *cv;
546 register int i, j;
547 int rval;
548 int sgn;
549 double minrad, maxrad, avgrad;
550 double theta;
551
552 if (ac != 4)
553 return(MG_EARGC);
554 if ((cv = c_getvert(av[1])) == NULL)
555 return(MG_EUNDEF);
556 if (is0vect(cv->n))
557 return(MG_EILL);
558 if (!isflt(av[2]) || !isflt(av[3]))
559 return(MG_ETYPE);
560 minrad = atof(av[2]);
561 round0(minrad);
562 maxrad = atof(av[3]);
563 /* check orientation */
564 if (minrad > 0.)
565 sgn = 1;
566 else if (minrad < 0.)
567 sgn = -1;
568 else if (maxrad > 0.)
569 sgn = 1;
570 else if (maxrad < 0.)
571 sgn = -1;
572 else
573 return(MG_EILL);
574 if (sgn*(maxrad-minrad) <= 0.)
575 return(MG_EILL);
576 /* initialize */
577 warpconends = 1;
578 v2ent[3] = av[1];
579 for (j = 0; j < 3; j++)
580 sprintf(p2[j], FLTFMT, cv->p[j] +
581 .5*sgn*(maxrad-minrad)*cv->n[j]);
582 if ((rval = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
583 return(rval);
584 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
585 return(rval);
586 sprintf(r2, FLTFMT, avgrad=.5*(minrad+maxrad));
587 /* run outer section */
588 for (i = 1; i <= 2*mg_nqcdivs; i++) {
589 theta = i*(PI/2)/mg_nqcdivs;
590 if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
591 return(rval);
592 for (j = 0; j < 3; j++)
593 sprintf(p2[j], FLTFMT, cv->p[j] +
594 .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
595 if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
596 return(rval);
597 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
598 return(rval);
599 strcpy(r1, r2);
600 sprintf(r2, FLTFMT, avgrad + .5*(maxrad-minrad)*sin(theta));
601 if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
602 return(rval);
603 }
604 /* run inner section */
605 sprintf(r2, FLTFMT, -.5*(minrad+maxrad));
606 for ( ; i <= 4*mg_nqcdivs; i++) {
607 theta = i*(PI/2)/mg_nqcdivs;
608 for (j = 0; j < 3; j++)
609 sprintf(p2[j], FLTFMT, cv->p[j] +
610 .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
611 if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
612 return(rval);
613 if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
614 return(rval);
615 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
616 return(rval);
617 strcpy(r1, r2);
618 sprintf(r2, FLTFMT, -avgrad - .5*(maxrad-minrad)*sin(theta));
619 if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
620 return(rval);
621 }
622 warpconends = 0;
623 return(MG_OK);
624 }
625
626
627 static int
628 e_cyl(ac, av) /* replace a cylinder with equivalent cone */
629 int ac;
630 char **av;
631 {
632 static char *avnew[6] = {mg_ename[MG_E_CONE]};
633
634 if (ac != 4)
635 return(MG_EARGC);
636 avnew[1] = av[1];
637 avnew[2] = av[2];
638 avnew[3] = av[3];
639 avnew[4] = av[2];
640 return(mg_handle(MG_E_CONE, 5, avnew));
641 }
642
643
644 static int
645 e_ring(ac, av) /* turn a ring into polygons */
646 int ac;
647 char **av;
648 {
649 static char p3[3][24], p4[3][24];
650 static char *nzent[5] = {mg_ename[MG_E_NORMAL],"0","0","0"};
651 static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_rv1","="};
652 static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_rv2","=","_rv3"};
653 static char *v3ent[4] = {mg_ename[MG_E_VERTEX],"_rv3","="};
654 static char *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]};
655 static char *v4ent[4] = {mg_ename[MG_E_VERTEX],"_rv4","="};
656 static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]};
657 static char *fent[6] = {mg_ename[MG_E_FACE],"_rv1","_rv2","_rv3","_rv4"};
658 register C_VERTEX *cv;
659 register int i, j;
660 FVECT u, v;
661 double minrad, maxrad;
662 int rv;
663 double theta, d;
664
665 if (ac != 4)
666 return(MG_EARGC);
667 if ((cv = c_getvert(av[1])) == NULL)
668 return(MG_EUNDEF);
669 if (is0vect(cv->n))
670 return(MG_EILL);
671 if (!isflt(av[2]) || !isflt(av[3]))
672 return(MG_ETYPE);
673 minrad = atof(av[2]);
674 round0(minrad);
675 maxrad = atof(av[3]);
676 if (minrad < 0. || maxrad <= minrad)
677 return(MG_EILL);
678 /* initialize */
679 make_axes(u, v, cv->n);
680 for (j = 0; j < 3; j++)
681 sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*u[j]);
682 if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
683 return(rv);
684 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
685 return(rv);
686 if (minrad == 0.) { /* closed */
687 v1ent[3] = av[1];
688 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
689 return(rv);
690 if ((rv = mg_handle(MG_E_NORMAL, 4, nzent)) != MG_OK)
691 return(rv);
692 for (i = 1; i <= 4*mg_nqcdivs; i++) {
693 theta = i*(PI/2)/mg_nqcdivs;
694 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
695 return(rv);
696 for (j = 0; j < 3; j++)
697 sprintf(p3[j], FLTFMT, cv->p[j] +
698 maxrad*u[j]*cos(theta) +
699 maxrad*v[j]*sin(theta));
700 if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
701 return(rv);
702 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
703 return(rv);
704 if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK)
705 return(rv);
706 }
707 } else { /* open */
708 if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
709 return(rv);
710 for (j = 0; j < 3; j++)
711 sprintf(p4[j], FLTFMT, cv->p[j] + minrad*u[j]);
712 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
713 return(rv);
714 v1ent[3] = "_rv4";
715 for (i = 1; i <= 4*mg_nqcdivs; i++) {
716 theta = i*(PI/2)/mg_nqcdivs;
717 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
718 return(rv);
719 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
720 return(rv);
721 for (j = 0; j < 3; j++) {
722 d = u[j]*cos(theta) + v[j]*sin(theta);
723 sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*d);
724 sprintf(p4[j], FLTFMT, cv->p[j] + minrad*d);
725 }
726 if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
727 return(rv);
728 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
729 return(rv);
730 if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
731 return(rv);
732 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
733 return(rv);
734 if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
735 return(rv);
736 }
737 }
738 return(MG_OK);
739 }
740
741
742 static int
743 e_cone(ac, av) /* turn a cone into polygons */
744 int ac;
745 char **av;
746 {
747 static char p3[3][24], p4[3][24], n3[3][24], n4[3][24];
748 static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_cv1","="};
749 static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_cv2","=","_cv3"};
750 static char *v3ent[4] = {mg_ename[MG_E_VERTEX],"_cv3","="};
751 static char *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]};
752 static char *n3ent[5] = {mg_ename[MG_E_NORMAL],n3[0],n3[1],n3[2]};
753 static char *v4ent[4] = {mg_ename[MG_E_VERTEX],"_cv4","="};
754 static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]};
755 static char *n4ent[5] = {mg_ename[MG_E_NORMAL],n4[0],n4[1],n4[2]};
756 static char *fent[6] = {mg_ename[MG_E_FACE],"_cv1","_cv2","_cv3","_cv4"};
757 char *v1n;
758 register C_VERTEX *cv1, *cv2;
759 register int i, j;
760 FVECT u, v, w;
761 double rad1, rad2;
762 int sgn;
763 double n1off, n2off;
764 double d;
765 int rv;
766 double theta;
767
768 if (ac != 5)
769 return(MG_EARGC);
770 if ((cv1 = c_getvert(av[1])) == NULL ||
771 (cv2 = c_getvert(av[3])) == NULL)
772 return(MG_EUNDEF);
773 v1n = av[1];
774 if (!isflt(av[2]) || !isflt(av[4]))
775 return(MG_ETYPE);
776 rad1 = atof(av[2]);
777 round0(rad1);
778 rad2 = atof(av[4]);
779 round0(rad2);
780 if (rad1 == 0.) {
781 if (rad2 == 0.)
782 return(MG_EILL);
783 } else if (rad2 != 0.) {
784 if (rad1 < 0. ^ rad2 < 0.)
785 return(MG_EILL);
786 } else { /* swap */
787 C_VERTEX *cv;
788
789 cv = cv1;
790 cv1 = cv2;
791 cv2 = cv;
792 v1n = av[3];
793 d = rad1;
794 rad1 = rad2;
795 rad2 = d;
796 }
797 sgn = rad2 < 0. ? -1 : 1;
798 /* initialize */
799 for (j = 0; j < 3; j++)
800 w[j] = cv1->p[j] - cv2->p[j];
801 if ((d = normalize(w)) == 0.)
802 return(MG_EILL);
803 n1off = n2off = (rad2 - rad1)/d;
804 if (warpconends) { /* hack for e_sph and e_torus */
805 d = atan(n2off) - (PI/4)/mg_nqcdivs;
806 if (d <= -PI/2+FTINY)
807 n2off = -FHUGE;
808 else
809 n2off = tan(d);
810 }
811 make_axes(u, v, w);
812 for (j = 0; j < 3; j++) {
813 sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*u[j]);
814 if (n2off <= -FHUGE)
815 sprintf(n3[j], FLTFMT, -w[j]);
816 else
817 sprintf(n3[j], FLTFMT, u[j] + w[j]*n2off);
818 }
819 if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
820 return(rv);
821 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
822 return(rv);
823 if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
824 return(rv);
825 if (rad1 == 0.) { /* triangles */
826 v1ent[3] = v1n;
827 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
828 return(rv);
829 for (j = 0; j < 3; j++)
830 sprintf(n4[j], FLTFMT, w[j]);
831 if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
832 return(rv);
833 for (i = 1; i <= 4*mg_nqcdivs; i++) {
834 theta = sgn*i*(PI/2)/mg_nqcdivs;
835 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
836 return(rv);
837 for (j = 0; j < 3; j++) {
838 d = u[j]*cos(theta) + v[j]*sin(theta);
839 sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d);
840 if (n2off > -FHUGE)
841 sprintf(n3[j], FLTFMT, d + w[j]*n2off);
842 }
843 if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
844 return(rv);
845 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
846 return(rv);
847 if (n2off > -FHUGE &&
848 (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
849 return(rv);
850 if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK)
851 return(rv);
852 }
853 } else { /* quads */
854 v1ent[3] = "_cv4";
855 if (warpconends) { /* hack for e_sph and e_torus */
856 d = atan(n1off) + (PI/4)/mg_nqcdivs;
857 if (d >= PI/2-FTINY)
858 n1off = FHUGE;
859 else
860 n1off = tan(atan(n1off)+(PI/4)/mg_nqcdivs);
861 }
862 for (j = 0; j < 3; j++) {
863 sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*u[j]);
864 if (n1off >= FHUGE)
865 sprintf(n4[j], FLTFMT, w[j]);
866 else
867 sprintf(n4[j], FLTFMT, u[j] + w[j]*n1off);
868 }
869 if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
870 return(rv);
871 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
872 return(rv);
873 if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
874 return(rv);
875 for (i = 1; i <= 4*mg_nqcdivs; i++) {
876 theta = sgn*i*(PI/2)/mg_nqcdivs;
877 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
878 return(rv);
879 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
880 return(rv);
881 for (j = 0; j < 3; j++) {
882 d = u[j]*cos(theta) + v[j]*sin(theta);
883 sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d);
884 if (n2off > -FHUGE)
885 sprintf(n3[j], FLTFMT, d + w[j]*n2off);
886 sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*d);
887 if (n1off < FHUGE)
888 sprintf(n4[j], FLTFMT, d + w[j]*n1off);
889 }
890 if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
891 return(rv);
892 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
893 return(rv);
894 if (n2off > -FHUGE &&
895 (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
896 return(rv);
897 if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
898 return(rv);
899 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
900 return(rv);
901 if (n1off < FHUGE &&
902 (rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
903 return(rv);
904 if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
905 return(rv);
906 }
907 }
908 return(MG_OK);
909 }
910
911
912 static int
913 e_prism(ac, av) /* turn a prism into polygons */
914 int ac;
915 char **av;
916 {
917 static char p[3][24];
918 static char *vent[5] = {mg_ename[MG_E_VERTEX],NULL,"="};
919 static char *pent[5] = {mg_ename[MG_E_POINT],p[0],p[1],p[2]};
920 static char *znorm[5] = {mg_ename[MG_E_NORMAL],"0","0","0"};
921 char *newav[MG_MAXARGC], nvn[MG_MAXARGC-1][8];
922 double length;
923 int hasnorm;
924 FVECT v1, v2, v3, norm;
925 register C_VERTEX *cv;
926 C_VERTEX *cv0;
927 int rv;
928 register int i, j;
929 /* check arguments */
930 if (ac < 5)
931 return(MG_EARGC);
932 if (!isflt(av[ac-1]))
933 return(MG_ETYPE);
934 length = atof(av[ac-1]);
935 if (length <= FTINY && length >= -FTINY)
936 return(MG_EILL);
937 /* compute face normal */
938 if ((cv0 = c_getvert(av[1])) == NULL)
939 return(MG_EUNDEF);
940 hasnorm = 0;
941 norm[0] = norm[1] = norm[2] = 0.;
942 v1[0] = v1[1] = v1[2] = 0.;
943 for (i = 2; i < ac-1; i++) {
944 if ((cv = c_getvert(av[i])) == NULL)
945 return(MG_EUNDEF);
946 hasnorm += !is0vect(cv->n);
947 v2[0] = cv->p[0] - cv0->p[0];
948 v2[1] = cv->p[1] - cv0->p[1];
949 v2[2] = cv->p[2] - cv0->p[2];
950 fcross(v3, v1, v2);
951 norm[0] += v3[0];
952 norm[1] += v3[1];
953 norm[2] += v3[2];
954 VCOPY(v1, v2);
955 }
956 if (normalize(norm) == 0.)
957 return(MG_EILL);
958 /* create moved vertices */
959 for (i = 1; i < ac-1; i++) {
960 sprintf(nvn[i-1], "_pv%d", i);
961 vent[1] = nvn[i-1];
962 vent[3] = av[i];
963 if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK)
964 return(rv);
965 cv = c_getvert(av[i]); /* checked above */
966 for (j = 0; j < 3; j++)
967 sprintf(p[j], FLTFMT, cv->p[j] - length*norm[j]);
968 if ((rv = mg_handle(MG_E_POINT, 4, pent)) != MG_OK)
969 return(rv);
970 }
971 /* make faces */
972 newav[0] = mg_ename[MG_E_FACE];
973 /* do the side faces */
974 newav[5] = NULL;
975 newav[3] = av[ac-2];
976 newav[4] = nvn[ac-3];
977 for (i = 1; i < ac-1; i++) {
978 newav[1] = nvn[i-1];
979 newav[2] = av[i];
980 if ((rv = mg_handle(MG_E_FACE, 5, newav)) != MG_OK)
981 return(rv);
982 newav[3] = newav[2];
983 newav[4] = newav[1];
984 }
985 /* do top face */
986 for (i = 1; i < ac-1; i++) {
987 if (hasnorm) { /* zero normals */
988 vent[1] = nvn[i-1];
989 if ((rv = mg_handle(MG_E_VERTEX, 2, vent)) != MG_OK)
990 return(rv);
991 if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK)
992 return(rv);
993 }
994 newav[ac-1-i] = nvn[i-1]; /* reverse */
995 }
996 if ((rv = mg_handle(MG_E_FACE, ac-1, newav)) != MG_OK)
997 return(rv);
998 /* do bottom face */
999 if (hasnorm)
1000 for (i = 1; i < ac-1; i++) {
1001 vent[1] = nvn[i-1];
1002 vent[3] = av[i];
1003 if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK)
1004 return(rv);
1005 if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK)
1006 return(rv);
1007 newav[i] = nvn[i-1];
1008 }
1009 else
1010 for (i = 1; i < ac-1; i++)
1011 newav[i] = av[i];
1012 newav[i] = NULL;
1013 if ((rv = mg_handle(MG_E_FACE, i, newav)) != MG_OK)
1014 return(rv);
1015 return(MG_OK);
1016 }
1017
1018
1019 static int
1020 put_cxy() /* put out current xy chromaticities */
1021 {
1022 static char xbuf[24], ybuf[24];
1023 static char *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf};
1024 int rv;
1025
1026 sprintf(xbuf, "%.4f", c_ccolor->cx);
1027 sprintf(ybuf, "%.4f", c_ccolor->cy);
1028 if ((rv = mg_handle(MG_E_CXY, 3, ccom)) != MG_OK)
1029 return(rv);
1030 return(MG_OK);
1031 }
1032
1033
1034 static int
1035 put_cspec() /* put out current color spectrum */
1036 {
1037 char wl[2][6], vbuf[C_CNSS][24];
1038 char *newav[C_CNSS+4];
1039 double sf;
1040 register int i;
1041
1042 if (mg_ehand[MG_E_CSPEC] != c_hcolor) {
1043 sprintf(wl[0], "%d", C_CMINWL);
1044 sprintf(wl[1], "%d", C_CMAXWL);
1045 newav[0] = mg_ename[MG_E_CSPEC];
1046 newav[1] = wl[0];
1047 newav[2] = wl[1];
1048 sf = (double)C_CNSS / c_ccolor->ssum;
1049 for (i = 0; i < C_CNSS; i++) {
1050 sprintf(vbuf[i], "%.4f", sf*c_ccolor->ssamp[i]);
1051 newav[i+3] = vbuf[i];
1052 }
1053 newav[C_CNSS+3] = NULL;
1054 if ((i = mg_handle(MG_E_CSPEC, C_CNSS+3, newav)) != MG_OK)
1055 return(i);
1056 }
1057 return(MG_OK);
1058 }
1059
1060
1061 static int
1062 e_cspec(ac, av) /* handle spectral color */
1063 int ac;
1064 char **av;
1065 {
1066 /* convert to xy chromaticity */
1067 c_ccvt(c_ccolor, C_CSXY);
1068 /* if it's really their handler, use it */
1069 if (mg_ehand[MG_E_CXY] != c_hcolor)
1070 return(put_cxy());
1071 return(MG_OK);
1072 }
1073
1074
1075 static int
1076 e_cmix(ac, av) /* handle mixing of colors */
1077 int ac;
1078 char **av;
1079 {
1080 /*
1081 * Contorted logic works as follows:
1082 * 1. the colors are already mixed in c_hcolor() support function
1083 * 2. if we would handle a spectral result, make sure it's not
1084 * 3. if c_hcolor() would handle a spectral result, don't bother
1085 * 4. otherwise, make cspec entity and pass it to their handler
1086 * 5. if we have only xy results, handle it as c_spec() would
1087 */
1088 if (mg_ehand[MG_E_CSPEC] == e_cspec)
1089 c_ccvt(c_ccolor, C_CSXY);
1090 else if (c_ccolor->flags & C_CDSPEC)
1091 return(put_cspec());
1092 if (mg_ehand[MG_E_CXY] != c_hcolor)
1093 return(put_cxy());
1094 return(MG_OK);
1095 }
1096
1097
1098 static int
1099 e_cct(ac, av) /* handle color temperature */
1100 int ac;
1101 char **av;
1102 {
1103 /*
1104 * Logic is similar to e_cmix here. Support handler has already
1105 * converted temperature to spectral color. Put it out as such
1106 * if they support it, otherwise convert to xy chromaticity and
1107 * put it out if they handle it.
1108 */
1109 if (mg_ehand[MG_E_CSPEC] != e_cspec)
1110 return(put_cspec());
1111 c_ccvt(c_ccolor, C_CSXY);
1112 if (mg_ehand[MG_E_CXY] != c_hcolor)
1113 return(put_cxy());
1114 return(MG_OK);
1115 }