ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/parser.c
Revision: 1.6
Committed: Fri Jun 24 16:28:18 1994 UTC (29 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.5: +90 -6 lines
Log Message:
added cspec and cmix entities

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