ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/parser.c
Revision: 1.18
Committed: Thu May 11 20:17:33 1995 UTC (28 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.17: +24 -3 lines
Log Message:
added default handling of undefined entities

File Contents

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