ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/parser.c
Revision: 1.10
Committed: Wed Jun 29 16:15:20 1994 UTC (29 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.9: +111 -143 lines
Log Message:
made the -a (array) option work within files as well

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 = LU_SINIT(NULL,NULL); /* 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 int
191 mg_handle(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 static int nfids;
214 int olen;
215 register char *cp;
216
217 ctx->fid = ++nfids;
218 ctx->lineno = 0;
219 if (fn == NULL) {
220 strcpy(ctx->fname, "<stdin>");
221 ctx->fp = stdin;
222 ctx->prev = mg_file;
223 mg_file = ctx;
224 return(MG_OK);
225 }
226 /* get name relative to this context */
227 if (mg_file != NULL &&
228 (cp = strrchr(mg_file->fname, '/')) != NULL)
229 olen = cp - mg_file->fname + 1;
230 else
231 olen = 0;
232 if (olen)
233 strcpy(ctx->fname, mg_file->fname);
234 strcpy(ctx->fname+olen, fn);
235 ctx->fp = fopen(ctx->fname, "r");
236 if (ctx->fp == NULL)
237 return(MG_ENOFILE);
238 ctx->prev = mg_file; /* establish new context */
239 mg_file = ctx;
240 return(MG_OK);
241 }
242
243
244 void
245 mg_close() /* close input file */
246 {
247 register MG_FCTXT *ctx = mg_file;
248
249 mg_file = ctx->prev; /* restore enclosing context */
250 if (ctx->fp == stdin)
251 return; /* don't close standard input */
252 fclose(ctx->fp);
253 }
254
255
256 void
257 mg_fgetpos(pos) /* get current position in input file */
258 register MG_FPOS *pos;
259 {
260 extern long ftell();
261
262 pos->fid = mg_file->fid;
263 pos->lineno = mg_file->lineno;
264 pos->offset = ftell(mg_file->fp);
265 }
266
267
268 int
269 mg_fgoto(pos) /* reposition input file pointer */
270 register MG_FPOS *pos;
271 {
272 if (pos->fid != mg_file->fid)
273 return(MG_ESEEK);
274 if (pos->lineno == mg_file->lineno)
275 return(MG_OK);
276 if (mg_file->fp == stdin)
277 return(MG_ESEEK); /* cannot seek on standard input */
278 if (fseek(mg_file->fp, pos->offset, 0) == EOF)
279 return(MG_ESEEK);
280 mg_file->lineno = pos->lineno;
281 return(MG_OK);
282 }
283
284
285 int
286 mg_read() /* read next line from file */
287 {
288 register int len = 0;
289
290 do {
291 if (fgets(mg_file->inpline+len,
292 MG_MAXLINE-len, mg_file->fp) == NULL)
293 return(len);
294 mg_file->lineno++;
295 len += strlen(mg_file->inpline+len);
296 if (len > 1 && mg_file->inpline[len-2] == '\\')
297 mg_file->inpline[--len-1] = ' ';
298 } while (mg_file->inpline[len]);
299
300 return(len);
301 }
302
303
304 int
305 mg_parse() /* parse current input line */
306 {
307 char abuf[MG_MAXLINE];
308 char *argv[MG_MAXARGC];
309 int en;
310 register char *cp, **ap;
311
312 strcpy(cp=abuf, mg_file->inpline);
313 ap = argv; /* break into words */
314 for ( ; ; ) {
315 while (isspace(*cp))
316 *cp++ = '\0';
317 if (!*cp)
318 break;
319 if (ap - argv >= MG_MAXARGC-1)
320 return(MG_EARGC);
321 *ap++ = cp;
322 while (*++cp && !isspace(*cp))
323 ;
324 }
325 if (ap == argv)
326 return(MG_OK); /* no words in line */
327 *ap = NULL;
328 /* else handle it */
329 return(mg_handle(-1, ap-argv, argv));
330 }
331
332
333 int
334 mg_load(fn) /* load an MGF file */
335 char *fn;
336 {
337 MG_FCTXT cntxt;
338 int rval;
339
340 if ((rval = mg_open(&cntxt, fn)) != MG_OK) {
341 fprintf(stderr, "%s: %s\n", fn, mg_err[rval]);
342 return(rval);
343 }
344 while (mg_read()) /* parse each line */
345 if ((rval = mg_parse()) != MG_OK) {
346 fprintf(stderr, "%s: %d: %s:\n%s", cntxt.fname,
347 cntxt.lineno, mg_err[rval],
348 cntxt.inpline);
349 break;
350 }
351 mg_close();
352 return(rval);
353 }
354
355
356 void
357 mg_clear() /* clear parser history */
358 {
359 c_clearall(); /* clear context tables */
360 mg_file = NULL; /* reset our context */
361 }
362
363
364 /****************************************************************************
365 * The following routines handle unsupported entities
366 */
367
368
369 static int
370 e_any_toss(ac, av) /* discard an unwanted entity */
371 int ac;
372 char **av;
373 {
374 return(MG_OK);
375 }
376
377
378 static int
379 e_include(ac, av) /* include file */
380 int ac;
381 char **av;
382 {
383 char *xfarg[MG_MAXARGC];
384 MG_FCTXT ictx;
385 int rv;
386
387 if (ac < 2)
388 return(MG_EARGC);
389 if ((rv = mg_open(&ictx, av[1])) != MG_OK)
390 return(rv);
391 if (ac > 2) {
392 register int i;
393
394 xfarg[0] = mg_ename[MG_E_XF];
395 for (i = 1; i < ac-1; i++)
396 xfarg[i] = av[i+1];
397 xfarg[ac-1] = NULL;
398 if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK)
399 return(rv);
400 }
401 while (!feof(mg_file->fp)) {
402 while (mg_read())
403 if ((rv = mg_parse()) != MG_OK) {
404 fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname,
405 ictx.lineno, mg_err[rv],
406 ictx.inpline);
407 mg_close();
408 return(MG_EINCL);
409 }
410 if (ac > 2)
411 if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK)
412 return(rv);
413 }
414 mg_close();
415 return(MG_OK);
416 }
417
418
419 static void
420 make_axes(u, v, w) /* compute u and v given w (normalized) */
421 FVECT u, v, w;
422 {
423 register int i;
424
425 v[0] = v[1] = v[2] = 0.;
426 for (i = 0; i < 3; i++)
427 if (w[i] < .6 && w[i] > -.6)
428 break;
429 v[i] = 1.;
430 fcross(u, v, w);
431 normalize(u);
432 fcross(v, w, u);
433 }
434
435
436 static int
437 e_sph(ac, av) /* expand a sphere into cones */
438 int ac;
439 char **av;
440 {
441 static char p2x[24], p2y[24], p2z[24], r1[24], r2[24];
442 static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_sv1","=","_sv2"};
443 static char *v2ent[4] = {mg_ename[MG_E_VERTEX],"_sv2","="};
444 static char *p2ent[5] = {mg_ename[MG_E_POINT],p2x,p2y,p2z};
445 static char *conent[6] = {mg_ename[MG_E_CONE],"_sv1",r1,"_sv2",r2};
446 register C_VERTEX *cv;
447 register int i;
448 int rval;
449 double rad;
450 double theta;
451
452 if (ac != 3)
453 return(MG_EARGC);
454 if ((cv = c_getvert(av[1])) == NULL)
455 return(MG_EUNDEF);
456 if (!isflt(av[2]))
457 return(MG_ETYPE);
458 rad = atof(av[2]);
459 /* initialize */
460 warpconends = 1;
461 if ((rval = mg_handle(MG_E_VERTEX, 3, v2ent)) != MG_OK)
462 return(rval);
463 sprintf(p2x, FLTFMT, cv->p[0]);
464 sprintf(p2y, FLTFMT, cv->p[1]);
465 sprintf(p2z, FLTFMT, cv->p[2]+rad);
466 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
467 return(rval);
468 r2[0] = '0'; r2[1] = '\0';
469 for (i = 1; i <= 2*mg_nqcdivs; i++) {
470 theta = i*(PI/2)/mg_nqcdivs;
471 if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
472 return(rval);
473 sprintf(p2z, FLTFMT, cv->p[2]+rad*cos(theta));
474 if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
475 return(rval);
476 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
477 return(rval);
478 strcpy(r1, r2);
479 sprintf(r2, FLTFMT, rad*sin(theta));
480 if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
481 return(rval);
482 }
483 warpconends = 0;
484 return(MG_OK);
485 }
486
487
488 static int
489 e_torus(ac, av) /* expand a torus into cones */
490 int ac;
491 char **av;
492 {
493 static char p2[3][24], r1[24], r2[24];
494 static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_tv1","=","_tv2"};
495 static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_tv2","="};
496 static char *p2ent[5] = {mg_ename[MG_E_POINT],p2[0],p2[1],p2[2]};
497 static char *conent[6] = {mg_ename[MG_E_CONE],"_tv1",r1,"_tv2",r2};
498 register C_VERTEX *cv;
499 register int i, j;
500 int rval;
501 int sgn;
502 double minrad, maxrad, avgrad;
503 double theta;
504
505 if (ac != 4)
506 return(MG_EARGC);
507 if ((cv = c_getvert(av[1])) == NULL)
508 return(MG_EUNDEF);
509 if (is0vect(cv->n))
510 return(MG_EILL);
511 if (!isflt(av[2]) || !isflt(av[3]))
512 return(MG_ETYPE);
513 minrad = atof(av[2]);
514 round0(minrad);
515 maxrad = atof(av[3]);
516 /* check orientation */
517 if (minrad > 0.)
518 sgn = 1;
519 else if (minrad < 0.)
520 sgn = -1;
521 else if (maxrad > 0.)
522 sgn = 1;
523 else if (maxrad < 0.)
524 sgn = -1;
525 else
526 return(MG_EILL);
527 if (sgn*(maxrad-minrad) <= 0.)
528 return(MG_EILL);
529 /* initialize */
530 warpconends = 1;
531 v2ent[3] = av[1];
532 for (j = 0; j < 3; j++)
533 sprintf(p2[j], FLTFMT, cv->p[j] +
534 .5*sgn*(maxrad-minrad)*cv->n[j]);
535 if ((rval = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
536 return(rval);
537 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
538 return(rval);
539 sprintf(r2, FLTFMT, avgrad=.5*(minrad+maxrad));
540 /* run outer section */
541 for (i = 1; i <= 2*mg_nqcdivs; i++) {
542 theta = i*(PI/2)/mg_nqcdivs;
543 if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
544 return(rval);
545 for (j = 0; j < 3; j++)
546 sprintf(p2[j], FLTFMT, cv->p[j] +
547 .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
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, avgrad + .5*(maxrad-minrad)*sin(theta));
554 if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
555 return(rval);
556 }
557 /* run inner section */
558 sprintf(r2, FLTFMT, -.5*(minrad+maxrad));
559 for ( ; i <= 4*mg_nqcdivs; i++) {
560 theta = i*(PI/2)/mg_nqcdivs;
561 for (j = 0; j < 3; j++)
562 sprintf(p2[j], FLTFMT, cv->p[j] +
563 .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
564 if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
565 return(rval);
566 if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
567 return(rval);
568 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
569 return(rval);
570 strcpy(r1, r2);
571 sprintf(r2, FLTFMT, -avgrad - .5*(maxrad-minrad)*sin(theta));
572 if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
573 return(rval);
574 }
575 warpconends = 0;
576 return(MG_OK);
577 }
578
579
580 static int
581 e_cyl(ac, av) /* replace a cylinder with equivalent cone */
582 int ac;
583 char **av;
584 {
585 static char *avnew[6] = {mg_ename[MG_E_CONE]};
586
587 if (ac != 4)
588 return(MG_EARGC);
589 avnew[1] = av[1];
590 avnew[2] = av[2];
591 avnew[3] = av[3];
592 avnew[4] = av[2];
593 return(mg_handle(MG_E_CONE, 5, avnew));
594 }
595
596
597 static int
598 e_ring(ac, av) /* turn a ring into polygons */
599 int ac;
600 char **av;
601 {
602 static char p3[3][24], p4[3][24];
603 static char *nzent[5] = {mg_ename[MG_E_NORMAL],"0","0","0"};
604 static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_rv1","="};
605 static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_rv2","=","_rv3"};
606 static char *v3ent[4] = {mg_ename[MG_E_VERTEX],"_rv3","="};
607 static char *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]};
608 static char *v4ent[4] = {mg_ename[MG_E_VERTEX],"_rv4","="};
609 static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]};
610 static char *fent[6] = {mg_ename[MG_E_FACE],"_rv1","_rv2","_rv3","_rv4"};
611 register C_VERTEX *cv;
612 register int i, j;
613 FVECT u, v;
614 double minrad, maxrad;
615 int rv;
616 double theta, d;
617
618 if (ac != 4)
619 return(MG_EARGC);
620 if ((cv = c_getvert(av[1])) == NULL)
621 return(MG_EUNDEF);
622 if (is0vect(cv->n))
623 return(MG_EILL);
624 if (!isflt(av[2]) || !isflt(av[3]))
625 return(MG_ETYPE);
626 minrad = atof(av[2]);
627 round0(minrad);
628 maxrad = atof(av[3]);
629 if (minrad < 0. || maxrad <= minrad)
630 return(MG_EILL);
631 /* initialize */
632 make_axes(u, v, cv->n);
633 for (j = 0; j < 3; j++)
634 sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*u[j]);
635 if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
636 return(rv);
637 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
638 return(rv);
639 if (minrad == 0.) { /* closed */
640 v1ent[3] = av[1];
641 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
642 return(rv);
643 if ((rv = mg_handle(MG_E_NORMAL, 4, nzent)) != MG_OK)
644 return(rv);
645 for (i = 1; i <= 4*mg_nqcdivs; i++) {
646 theta = i*(PI/2)/mg_nqcdivs;
647 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
648 return(rv);
649 for (j = 0; j < 3; j++)
650 sprintf(p3[j], FLTFMT, cv->p[j] +
651 maxrad*u[j]*cos(theta) +
652 maxrad*v[j]*sin(theta));
653 if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
654 return(rv);
655 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
656 return(rv);
657 if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK)
658 return(rv);
659 }
660 } else { /* open */
661 if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
662 return(rv);
663 for (j = 0; j < 3; j++)
664 sprintf(p4[j], FLTFMT, cv->p[j] + minrad*u[j]);
665 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
666 return(rv);
667 v1ent[3] = "_rv4";
668 for (i = 1; i <= 4*mg_nqcdivs; i++) {
669 theta = i*(PI/2)/mg_nqcdivs;
670 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
671 return(rv);
672 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
673 return(rv);
674 for (j = 0; j < 3; j++) {
675 d = u[j]*cos(theta) + v[j]*sin(theta);
676 sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*d);
677 sprintf(p4[j], FLTFMT, cv->p[j] + minrad*d);
678 }
679 if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
680 return(rv);
681 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
682 return(rv);
683 if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
684 return(rv);
685 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
686 return(rv);
687 if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
688 return(rv);
689 }
690 }
691 return(MG_OK);
692 }
693
694
695 static int
696 e_cone(ac, av) /* turn a cone into polygons */
697 int ac;
698 char **av;
699 {
700 static char p3[3][24], p4[3][24], n3[3][24], n4[3][24];
701 static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_cv1","="};
702 static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_cv2","=","_cv3"};
703 static char *v3ent[4] = {mg_ename[MG_E_VERTEX],"_cv3","="};
704 static char *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]};
705 static char *n3ent[5] = {mg_ename[MG_E_NORMAL],n3[0],n3[1],n3[2]};
706 static char *v4ent[4] = {mg_ename[MG_E_VERTEX],"_cv4","="};
707 static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]};
708 static char *n4ent[5] = {mg_ename[MG_E_NORMAL],n4[0],n4[1],n4[2]};
709 static char *fent[6] = {mg_ename[MG_E_FACE],"_cv1","_cv2","_cv3","_cv4"};
710 register C_VERTEX *cv1, *cv2;
711 register int i, j;
712 FVECT u, v, w;
713 double rad1, rad2;
714 int sgn;
715 double n1off, n2off;
716 double d;
717 int rv;
718 double theta;
719
720 if (ac != 5)
721 return(MG_EARGC);
722 if ((cv1 = c_getvert(av[1])) == NULL ||
723 (cv2 = c_getvert(av[3])) == NULL)
724 return(MG_EUNDEF);
725 if (!isflt(av[2]) || !isflt(av[4]))
726 return(MG_ETYPE);
727 rad1 = atof(av[2]);
728 round0(rad1);
729 rad2 = atof(av[4]);
730 round0(rad2);
731 if (rad1 == 0.) {
732 if (rad2 == 0.)
733 return(MG_EILL);
734 } else if (rad2 != 0.) {
735 if (rad1 < 0. ^ rad2 < 0.)
736 return(MG_EILL);
737 } else { /* swap */
738 C_VERTEX *cv;
739
740 cv = cv1;
741 cv1 = cv2;
742 cv2 = cv;
743 d = rad1;
744 rad1 = rad2;
745 rad2 = d;
746 }
747 sgn = rad2 < 0. ? -1 : 1;
748 /* initialize */
749 for (j = 0; j < 3; j++)
750 w[j] = cv1->p[j] - cv2->p[j];
751 if ((d = normalize(w)) == 0.)
752 return(MG_EILL);
753 n1off = n2off = (rad2 - rad1)/d;
754 if (warpconends) { /* hack for e_sph and e_torus */
755 d = atan(n2off) - (PI/4)/mg_nqcdivs;
756 if (d <= -PI/2+FTINY)
757 n2off = -FHUGE;
758 else
759 n2off = tan(d);
760 }
761 make_axes(u, v, w);
762 for (j = 0; j < 3; j++) {
763 sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*u[j]);
764 if (n2off <= -FHUGE)
765 sprintf(n3[j], FLTFMT, -w[j]);
766 else
767 sprintf(n3[j], FLTFMT, u[j] + w[j]*n2off);
768 }
769 if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
770 return(rv);
771 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
772 return(rv);
773 if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
774 return(rv);
775 if (rad1 == 0.) { /* triangles */
776 v1ent[3] = av[1];
777 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
778 return(rv);
779 for (j = 0; j < 3; j++)
780 sprintf(n4[j], FLTFMT, w[j]);
781 if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
782 return(rv);
783 for (i = 1; i <= 4*mg_nqcdivs; i++) {
784 theta = sgn*i*(PI/2)/mg_nqcdivs;
785 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
786 return(rv);
787 for (j = 0; j < 3; j++) {
788 d = u[j]*cos(theta) + v[j]*sin(theta);
789 sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d);
790 if (n2off > -FHUGE)
791 sprintf(n3[j], FLTFMT, d + w[j]*n2off);
792 }
793 if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
794 return(rv);
795 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
796 return(rv);
797 if (n2off > -FHUGE &&
798 (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
799 return(rv);
800 if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK)
801 return(rv);
802 }
803 } else { /* quads */
804 v1ent[3] = "_cv4";
805 if (warpconends) { /* hack for e_sph and e_torus */
806 d = atan(n1off) + (PI/4)/mg_nqcdivs;
807 if (d >= PI/2-FTINY)
808 n1off = FHUGE;
809 else
810 n1off = tan(atan(n1off)+(PI/4)/mg_nqcdivs);
811 }
812 for (j = 0; j < 3; j++) {
813 sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*u[j]);
814 if (n1off >= FHUGE)
815 sprintf(n4[j], FLTFMT, w[j]);
816 else
817 sprintf(n4[j], FLTFMT, u[j] + w[j]*n1off);
818 }
819 if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
820 return(rv);
821 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
822 return(rv);
823 if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
824 return(rv);
825 for (i = 1; i <= 4*mg_nqcdivs; i++) {
826 theta = sgn*i*(PI/2)/mg_nqcdivs;
827 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
828 return(rv);
829 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
830 return(rv);
831 for (j = 0; j < 3; j++) {
832 d = u[j]*cos(theta) + v[j]*sin(theta);
833 sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d);
834 if (n2off > -FHUGE)
835 sprintf(n3[j], FLTFMT, d + w[j]*n2off);
836 sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*d);
837 if (n1off < FHUGE)
838 sprintf(n4[j], FLTFMT, d + w[j]*n1off);
839 }
840 if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
841 return(rv);
842 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
843 return(rv);
844 if (n2off > -FHUGE &&
845 (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
846 return(rv);
847 if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
848 return(rv);
849 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
850 return(rv);
851 if (n1off < FHUGE &&
852 (rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
853 return(rv);
854 if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
855 return(rv);
856 }
857 }
858 return(MG_OK);
859 }
860
861
862 static int
863 e_prism(ac, av) /* turn a prism into polygons */
864 int ac;
865 char **av;
866 {
867 static char p[3][24];
868 static char *vent[4] = {mg_ename[MG_E_VERTEX],NULL,"="};
869 static char *pent[5] = {mg_ename[MG_E_POINT],p[0],p[1],p[2]};
870 char *newav[MG_MAXARGC], nvn[MG_MAXARGC-1][8];
871 double length;
872 FVECT v1, v2, v3, norm;
873 register C_VERTEX *cv;
874 C_VERTEX *cv0;
875 int rv;
876 register int i, j;
877
878 if (ac < 5)
879 return(MG_EARGC);
880 if (!isflt(av[ac-1]))
881 return(MG_ETYPE);
882 length = atof(av[ac-1]);
883 if (length <= FTINY && length >= -FTINY)
884 return(MG_EILL);
885 /* do bottom face */
886 newav[0] = mg_ename[MG_E_FACE];
887 for (i = 1; i < ac-1; i++)
888 newav[i] = av[i];
889 newav[i] = NULL;
890 if ((rv = mg_handle(MG_E_FACE, i, newav)) != MG_OK)
891 return(rv);
892 /* compute face normal */
893 if ((cv0 = c_getvert(av[1])) == NULL)
894 return(MG_EUNDEF);
895 norm[0] = norm[1] = norm[2] = 0.;
896 v1[0] = v1[1] = v1[2] = 0.;
897 for (i = 2; i < ac-1; i++) {
898 if ((cv = c_getvert(av[i])) == NULL)
899 return(MG_EUNDEF);
900 v2[0] = cv->p[0] - cv0->p[0];
901 v2[1] = cv->p[1] - cv0->p[1];
902 v2[2] = cv->p[2] - cv0->p[2];
903 fcross(v3, v1, v2);
904 norm[0] += v3[0];
905 norm[1] += v3[1];
906 norm[2] += v3[2];
907 VCOPY(v1, v2);
908 }
909 if (normalize(norm) == 0.)
910 return(MG_EILL);
911 /* create moved vertices */
912 for (i = 1; i < ac-1; i++) {
913 sprintf(nvn[i-1], "_pv%d", i);
914 vent[1] = nvn[i-1];
915 if ((rv = mg_handle(MG_E_VERTEX, 3, vent)) != MG_OK)
916 return(rv);
917 cv = c_getvert(av[i]); /* checked above */
918 for (j = 0; j < 3; j++)
919 sprintf(p[j], FLTFMT, cv->p[j] - length*norm[j]);
920 if ((rv = mg_handle(MG_E_POINT, 4, pent)) != MG_OK)
921 return(rv);
922 newav[ac-1-i] = nvn[i-1]; /* reverse */
923 }
924 /* do top face */
925 if ((rv = mg_handle(MG_E_FACE, ac-1, newav)) != MG_OK)
926 return(rv);
927 /* do the side faces */
928 newav[5] = NULL;
929 newav[3] = av[ac-2];
930 newav[4] = nvn[ac-3];
931 for (i = 1; i < ac-1; i++) {
932 newav[1] = nvn[i-1];
933 newav[2] = av[i];
934 if ((rv = mg_handle(MG_E_FACE, 5, newav)) != MG_OK)
935 return(rv);
936 newav[3] = newav[2];
937 newav[4] = newav[1];
938 }
939 return(MG_OK);
940 }
941
942
943 static int
944 e_cspec(ac, av) /* handle spectral color */
945 int ac;
946 char **av;
947 {
948 static char xbuf[24], ybuf[24];
949 static char *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf};
950 int rv;
951
952 c_ccvt(c_ccolor, C_CSXY);
953 /* if it's really their handler, use it */
954 if (mg_ehand[MG_E_CXY] != c_hcolor) {
955 sprintf(xbuf, "%.4f", c_ccolor->cx);
956 sprintf(ybuf, "%.4f", c_ccolor->cy);
957 if ((rv = mg_handle(MG_E_CXY, 3, ccom)) != MG_OK)
958 return(rv);
959 }
960 return(MG_OK);
961 }
962
963
964 static int
965 e_cmix(ac, av) /* handle mixing of colors */
966 int ac;
967 char **av;
968 {
969 char wl[2][6], vbuf[C_CNSS][24];
970 char *newav[C_CNSS+4];
971 int rv;
972 register int i;
973 /*
974 * Contorted logic works as follows:
975 * 1. the colors are already mixed in c_hcolor() support function
976 * 2. if we would handle a spectral result, make sure it's not
977 * 3. if c_hcolor() would handle a spectral result, don't bother
978 * 4. otherwise, make cspec entity and pass it to their handler
979 * 5. if we have only xy results, handle it as c_spec() would
980 */
981 if (mg_ehand[MG_E_CSPEC] == e_cspec)
982 c_ccvt(c_ccolor, C_CSXY);
983 else if (c_ccolor->flags & C_CDSPEC) {
984 if (mg_ehand[MG_E_CSPEC] != c_hcolor) {
985 sprintf(wl[0], "%d", C_CMINWL);
986 sprintf(wl[1], "%d", C_CMAXWL);
987 newav[0] = mg_ename[MG_E_CSPEC];
988 newav[1] = wl[0];
989 newav[2] = wl[1];
990 for (i = 0; i < C_CNSS; i++) {
991 sprintf(vbuf[i], "%.6f",
992 (double)c_ccolor->ssamp[i] /
993 c_ccolor->ssum);
994 newav[i+3] = vbuf[i];
995 }
996 newav[C_CNSS+3] = NULL;
997 if ((rv = mg_handle(MG_E_CSPEC, C_CNSS+3, newav)) != MG_OK)
998 return(rv);
999 }
1000 return(MG_OK);
1001 }
1002 if (mg_ehand[MG_E_CXY] != c_hcolor) {
1003 sprintf(vbuf[0], "%.4f", c_ccolor->cx);
1004 sprintf(vbuf[1], "%.4f", c_ccolor->cy);
1005 newav[0] = mg_ename[MG_E_CXY];
1006 newav[1] = vbuf[0];
1007 newav[2] = vbuf[1];
1008 newav[3] = NULL;
1009 if ((rv = mg_handle(MG_E_CXY, 3, newav)) != MG_OK)
1010 return(rv);
1011 }
1012 return(MG_OK);
1013 }