ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/parser.c
Revision: 1.26
Committed: Mon Dec 15 09:41:37 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 1.25: +1 -2 lines
Log Message:
added stdlib.h for better reliability on some systems

File Contents

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