ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/parser.c
Revision: 1.8
Committed: Sat Jun 25 09:48:03 1994 UTC (29 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.7: +2 -8 lines
Log Message:
minor fixes and enhancements

File Contents

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