ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/parser.c
Revision: 1.21
Committed: Wed Nov 29 19:14:49 1995 UTC (28 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.20: +10 -5 lines
Log Message:
added error for unmatched xf or o context close
fixed potential bug in mg_load() in case MG_OK != 0 in future

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 register int nbr;
353
354 if ((rval = mg_open(&cntxt, fn)) != MG_OK) {
355 fprintf(stderr, "%s: %s\n", fn, mg_err[rval]);
356 return(rval);
357 }
358 while ((nbr = mg_read()) > 0) { /* parse each line */
359 if (nbr >= MG_MAXLINE-1 && cntxt.inpline[nbr-1] != '\n') {
360 fprintf(stderr, "%s: %d: %s\n", cntxt.fname,
361 cntxt.lineno, mg_err[rval=MG_ELINE]);
362 break;
363 }
364 if ((rval = mg_parse()) != MG_OK) {
365 fprintf(stderr, "%s: %d: %s:\n%s", cntxt.fname,
366 cntxt.lineno, mg_err[rval],
367 cntxt.inpline);
368 break;
369 }
370 }
371 mg_close();
372 return(rval);
373 }
374
375
376 int
377 mg_defuhand(ac, av) /* default handler for unknown entities */
378 int ac;
379 char **av;
380 {
381 if (mg_nunknown++ == 0) /* report first incident */
382 fprintf(stderr, "%s: %d: %s: %s\n", mg_file->fname,
383 mg_file->lineno, mg_err[MG_EUNK], av[0]);
384 return(MG_OK);
385 }
386
387
388 void
389 mg_clear() /* clear parser history */
390 {
391 c_clearall(); /* clear context tables */
392 mg_file = NULL; /* reset our context */
393 }
394
395
396 /****************************************************************************
397 * The following routines handle unsupported entities
398 */
399
400
401 static int
402 e_any_toss(ac, av) /* discard an unwanted entity */
403 int ac;
404 char **av;
405 {
406 return(MG_OK);
407 }
408
409
410 static int
411 e_include(ac, av) /* include file */
412 int ac;
413 char **av;
414 {
415 char *xfarg[MG_MAXARGC];
416 MG_FCTXT ictx;
417 XF_SPEC *xf_orig = xf_context;
418 register int rv;
419
420 if (ac < 2)
421 return(MG_EARGC);
422 if ((rv = mg_open(&ictx, av[1])) != MG_OK)
423 return(rv);
424 if (ac > 2) {
425 register int i;
426
427 xfarg[0] = mg_ename[MG_E_XF];
428 for (i = 1; i < ac-1; i++)
429 xfarg[i] = av[i+1];
430 xfarg[ac-1] = NULL;
431 if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK) {
432 mg_close();
433 return(rv);
434 }
435 }
436 do {
437 while ((rv = mg_read()) > 0) {
438 if (rv >= MG_MAXLINE-1 && ictx.inpline[rv-1] != '\n') {
439 fprintf(stderr, "%s: %d: %s\n", ictx.fname,
440 ictx.lineno, mg_err[MG_ELINE]);
441 mg_close();
442 return(MG_EINCL);
443 }
444 if ((rv = mg_parse()) != MG_OK) {
445 fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname,
446 ictx.lineno, mg_err[rv],
447 ictx.inpline);
448 mg_close();
449 return(MG_EINCL);
450 }
451 }
452 if (ac > 2)
453 if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK) {
454 mg_close();
455 return(rv);
456 }
457 } while (xf_context != xf_orig);
458 mg_close();
459 return(MG_OK);
460 }
461
462
463 static void
464 make_axes(u, v, w) /* compute u and v given w (normalized) */
465 FVECT u, v, w;
466 {
467 register int i;
468
469 v[0] = v[1] = v[2] = 0.;
470 for (i = 0; i < 3; i++)
471 if (w[i] < .6 && w[i] > -.6)
472 break;
473 v[i] = 1.;
474 fcross(u, v, w);
475 normalize(u);
476 fcross(v, w, u);
477 }
478
479
480 static int
481 e_sph(ac, av) /* expand a sphere into cones */
482 int ac;
483 char **av;
484 {
485 static char p2x[24], p2y[24], p2z[24], r1[24], r2[24];
486 static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_sv1","=","_sv2"};
487 static char *v2ent[4] = {mg_ename[MG_E_VERTEX],"_sv2","="};
488 static char *p2ent[5] = {mg_ename[MG_E_POINT],p2x,p2y,p2z};
489 static char *conent[6] = {mg_ename[MG_E_CONE],"_sv1",r1,"_sv2",r2};
490 register C_VERTEX *cv;
491 register int i;
492 int rval;
493 double rad;
494 double theta;
495
496 if (ac != 3)
497 return(MG_EARGC);
498 if ((cv = c_getvert(av[1])) == NULL)
499 return(MG_EUNDEF);
500 if (!isflt(av[2]))
501 return(MG_ETYPE);
502 rad = atof(av[2]);
503 /* initialize */
504 warpconends = 1;
505 if ((rval = mg_handle(MG_E_VERTEX, 3, v2ent)) != MG_OK)
506 return(rval);
507 sprintf(p2x, FLTFMT, cv->p[0]);
508 sprintf(p2y, FLTFMT, cv->p[1]);
509 sprintf(p2z, FLTFMT, cv->p[2]+rad);
510 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
511 return(rval);
512 r2[0] = '0'; r2[1] = '\0';
513 for (i = 1; i <= 2*mg_nqcdivs; i++) {
514 theta = i*(PI/2)/mg_nqcdivs;
515 if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
516 return(rval);
517 sprintf(p2z, FLTFMT, cv->p[2]+rad*cos(theta));
518 if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
519 return(rval);
520 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
521 return(rval);
522 strcpy(r1, r2);
523 sprintf(r2, FLTFMT, rad*sin(theta));
524 if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
525 return(rval);
526 }
527 warpconends = 0;
528 return(MG_OK);
529 }
530
531
532 static int
533 e_torus(ac, av) /* expand a torus into cones */
534 int ac;
535 char **av;
536 {
537 static char p2[3][24], r1[24], r2[24];
538 static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_tv1","=","_tv2"};
539 static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_tv2","="};
540 static char *p2ent[5] = {mg_ename[MG_E_POINT],p2[0],p2[1],p2[2]};
541 static char *conent[6] = {mg_ename[MG_E_CONE],"_tv1",r1,"_tv2",r2};
542 register C_VERTEX *cv;
543 register int i, j;
544 int rval;
545 int sgn;
546 double minrad, maxrad, avgrad;
547 double theta;
548
549 if (ac != 4)
550 return(MG_EARGC);
551 if ((cv = c_getvert(av[1])) == NULL)
552 return(MG_EUNDEF);
553 if (is0vect(cv->n))
554 return(MG_EILL);
555 if (!isflt(av[2]) || !isflt(av[3]))
556 return(MG_ETYPE);
557 minrad = atof(av[2]);
558 round0(minrad);
559 maxrad = atof(av[3]);
560 /* check orientation */
561 if (minrad > 0.)
562 sgn = 1;
563 else if (minrad < 0.)
564 sgn = -1;
565 else if (maxrad > 0.)
566 sgn = 1;
567 else if (maxrad < 0.)
568 sgn = -1;
569 else
570 return(MG_EILL);
571 if (sgn*(maxrad-minrad) <= 0.)
572 return(MG_EILL);
573 /* initialize */
574 warpconends = 1;
575 v2ent[3] = av[1];
576 for (j = 0; j < 3; j++)
577 sprintf(p2[j], FLTFMT, cv->p[j] +
578 .5*sgn*(maxrad-minrad)*cv->n[j]);
579 if ((rval = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
580 return(rval);
581 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
582 return(rval);
583 sprintf(r2, FLTFMT, avgrad=.5*(minrad+maxrad));
584 /* run outer section */
585 for (i = 1; i <= 2*mg_nqcdivs; i++) {
586 theta = i*(PI/2)/mg_nqcdivs;
587 if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
588 return(rval);
589 for (j = 0; j < 3; j++)
590 sprintf(p2[j], FLTFMT, cv->p[j] +
591 .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
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 /* run inner section */
602 sprintf(r2, FLTFMT, -.5*(minrad+maxrad));
603 for ( ; i <= 4*mg_nqcdivs; i++) {
604 theta = i*(PI/2)/mg_nqcdivs;
605 for (j = 0; j < 3; j++)
606 sprintf(p2[j], FLTFMT, cv->p[j] +
607 .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
608 if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
609 return(rval);
610 if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
611 return(rval);
612 if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
613 return(rval);
614 strcpy(r1, r2);
615 sprintf(r2, FLTFMT, -avgrad - .5*(maxrad-minrad)*sin(theta));
616 if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
617 return(rval);
618 }
619 warpconends = 0;
620 return(MG_OK);
621 }
622
623
624 static int
625 e_cyl(ac, av) /* replace a cylinder with equivalent cone */
626 int ac;
627 char **av;
628 {
629 static char *avnew[6] = {mg_ename[MG_E_CONE]};
630
631 if (ac != 4)
632 return(MG_EARGC);
633 avnew[1] = av[1];
634 avnew[2] = av[2];
635 avnew[3] = av[3];
636 avnew[4] = av[2];
637 return(mg_handle(MG_E_CONE, 5, avnew));
638 }
639
640
641 static int
642 e_ring(ac, av) /* turn a ring into polygons */
643 int ac;
644 char **av;
645 {
646 static char p3[3][24], p4[3][24];
647 static char *nzent[5] = {mg_ename[MG_E_NORMAL],"0","0","0"};
648 static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_rv1","="};
649 static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_rv2","=","_rv3"};
650 static char *v3ent[4] = {mg_ename[MG_E_VERTEX],"_rv3","="};
651 static char *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]};
652 static char *v4ent[4] = {mg_ename[MG_E_VERTEX],"_rv4","="};
653 static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]};
654 static char *fent[6] = {mg_ename[MG_E_FACE],"_rv1","_rv2","_rv3","_rv4"};
655 register C_VERTEX *cv;
656 register int i, j;
657 FVECT u, v;
658 double minrad, maxrad;
659 int rv;
660 double theta, d;
661
662 if (ac != 4)
663 return(MG_EARGC);
664 if ((cv = c_getvert(av[1])) == NULL)
665 return(MG_EUNDEF);
666 if (is0vect(cv->n))
667 return(MG_EILL);
668 if (!isflt(av[2]) || !isflt(av[3]))
669 return(MG_ETYPE);
670 minrad = atof(av[2]);
671 round0(minrad);
672 maxrad = atof(av[3]);
673 if (minrad < 0. || maxrad <= minrad)
674 return(MG_EILL);
675 /* initialize */
676 make_axes(u, v, cv->n);
677 for (j = 0; j < 3; j++)
678 sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*u[j]);
679 if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
680 return(rv);
681 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
682 return(rv);
683 if (minrad == 0.) { /* closed */
684 v1ent[3] = av[1];
685 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
686 return(rv);
687 if ((rv = mg_handle(MG_E_NORMAL, 4, nzent)) != MG_OK)
688 return(rv);
689 for (i = 1; i <= 4*mg_nqcdivs; i++) {
690 theta = i*(PI/2)/mg_nqcdivs;
691 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
692 return(rv);
693 for (j = 0; j < 3; j++)
694 sprintf(p3[j], FLTFMT, cv->p[j] +
695 maxrad*u[j]*cos(theta) +
696 maxrad*v[j]*sin(theta));
697 if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
698 return(rv);
699 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
700 return(rv);
701 if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK)
702 return(rv);
703 }
704 } else { /* open */
705 if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
706 return(rv);
707 for (j = 0; j < 3; j++)
708 sprintf(p4[j], FLTFMT, cv->p[j] + minrad*u[j]);
709 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
710 return(rv);
711 v1ent[3] = "_rv4";
712 for (i = 1; i <= 4*mg_nqcdivs; i++) {
713 theta = i*(PI/2)/mg_nqcdivs;
714 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
715 return(rv);
716 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
717 return(rv);
718 for (j = 0; j < 3; j++) {
719 d = u[j]*cos(theta) + v[j]*sin(theta);
720 sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*d);
721 sprintf(p4[j], FLTFMT, cv->p[j] + minrad*d);
722 }
723 if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
724 return(rv);
725 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
726 return(rv);
727 if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
728 return(rv);
729 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
730 return(rv);
731 if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
732 return(rv);
733 }
734 }
735 return(MG_OK);
736 }
737
738
739 static int
740 e_cone(ac, av) /* turn a cone into polygons */
741 int ac;
742 char **av;
743 {
744 static char p3[3][24], p4[3][24], n3[3][24], n4[3][24];
745 static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_cv1","="};
746 static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_cv2","=","_cv3"};
747 static char *v3ent[4] = {mg_ename[MG_E_VERTEX],"_cv3","="};
748 static char *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]};
749 static char *n3ent[5] = {mg_ename[MG_E_NORMAL],n3[0],n3[1],n3[2]};
750 static char *v4ent[4] = {mg_ename[MG_E_VERTEX],"_cv4","="};
751 static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]};
752 static char *n4ent[5] = {mg_ename[MG_E_NORMAL],n4[0],n4[1],n4[2]};
753 static char *fent[6] = {mg_ename[MG_E_FACE],"_cv1","_cv2","_cv3","_cv4"};
754 char *v1n;
755 register C_VERTEX *cv1, *cv2;
756 register int i, j;
757 FVECT u, v, w;
758 double rad1, rad2;
759 int sgn;
760 double n1off, n2off;
761 double d;
762 int rv;
763 double theta;
764
765 if (ac != 5)
766 return(MG_EARGC);
767 if ((cv1 = c_getvert(av[1])) == NULL ||
768 (cv2 = c_getvert(av[3])) == NULL)
769 return(MG_EUNDEF);
770 v1n = av[1];
771 if (!isflt(av[2]) || !isflt(av[4]))
772 return(MG_ETYPE);
773 rad1 = atof(av[2]);
774 round0(rad1);
775 rad2 = atof(av[4]);
776 round0(rad2);
777 if (rad1 == 0.) {
778 if (rad2 == 0.)
779 return(MG_EILL);
780 } else if (rad2 != 0.) {
781 if (rad1 < 0. ^ rad2 < 0.)
782 return(MG_EILL);
783 } else { /* swap */
784 C_VERTEX *cv;
785
786 cv = cv1;
787 cv1 = cv2;
788 cv2 = cv;
789 v1n = av[3];
790 d = rad1;
791 rad1 = rad2;
792 rad2 = d;
793 }
794 sgn = rad2 < 0. ? -1 : 1;
795 /* initialize */
796 for (j = 0; j < 3; j++)
797 w[j] = cv1->p[j] - cv2->p[j];
798 if ((d = normalize(w)) == 0.)
799 return(MG_EILL);
800 n1off = n2off = (rad2 - rad1)/d;
801 if (warpconends) { /* hack for e_sph and e_torus */
802 d = atan(n2off) - (PI/4)/mg_nqcdivs;
803 if (d <= -PI/2+FTINY)
804 n2off = -FHUGE;
805 else
806 n2off = tan(d);
807 }
808 make_axes(u, v, w);
809 for (j = 0; j < 3; j++) {
810 sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*u[j]);
811 if (n2off <= -FHUGE)
812 sprintf(n3[j], FLTFMT, -w[j]);
813 else
814 sprintf(n3[j], FLTFMT, u[j] + w[j]*n2off);
815 }
816 if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
817 return(rv);
818 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
819 return(rv);
820 if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
821 return(rv);
822 if (rad1 == 0.) { /* triangles */
823 v1ent[3] = v1n;
824 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
825 return(rv);
826 for (j = 0; j < 3; j++)
827 sprintf(n4[j], FLTFMT, w[j]);
828 if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
829 return(rv);
830 for (i = 1; i <= 4*mg_nqcdivs; i++) {
831 theta = sgn*i*(PI/2)/mg_nqcdivs;
832 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
833 return(rv);
834 for (j = 0; j < 3; j++) {
835 d = u[j]*cos(theta) + v[j]*sin(theta);
836 sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d);
837 if (n2off > -FHUGE)
838 sprintf(n3[j], FLTFMT, d + w[j]*n2off);
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_FACE, 4, fent)) != MG_OK)
848 return(rv);
849 }
850 } else { /* quads */
851 v1ent[3] = "_cv4";
852 if (warpconends) { /* hack for e_sph and e_torus */
853 d = atan(n1off) + (PI/4)/mg_nqcdivs;
854 if (d >= PI/2-FTINY)
855 n1off = FHUGE;
856 else
857 n1off = tan(atan(n1off)+(PI/4)/mg_nqcdivs);
858 }
859 for (j = 0; j < 3; j++) {
860 sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*u[j]);
861 if (n1off >= FHUGE)
862 sprintf(n4[j], FLTFMT, w[j]);
863 else
864 sprintf(n4[j], FLTFMT, u[j] + w[j]*n1off);
865 }
866 if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
867 return(rv);
868 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
869 return(rv);
870 if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
871 return(rv);
872 for (i = 1; i <= 4*mg_nqcdivs; i++) {
873 theta = sgn*i*(PI/2)/mg_nqcdivs;
874 if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
875 return(rv);
876 if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
877 return(rv);
878 for (j = 0; j < 3; j++) {
879 d = u[j]*cos(theta) + v[j]*sin(theta);
880 sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d);
881 if (n2off > -FHUGE)
882 sprintf(n3[j], FLTFMT, d + w[j]*n2off);
883 sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*d);
884 if (n1off < FHUGE)
885 sprintf(n4[j], FLTFMT, d + w[j]*n1off);
886 }
887 if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
888 return(rv);
889 if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
890 return(rv);
891 if (n2off > -FHUGE &&
892 (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
893 return(rv);
894 if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
895 return(rv);
896 if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
897 return(rv);
898 if (n1off < FHUGE &&
899 (rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
900 return(rv);
901 if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
902 return(rv);
903 }
904 }
905 return(MG_OK);
906 }
907
908
909 static int
910 e_prism(ac, av) /* turn a prism into polygons */
911 int ac;
912 char **av;
913 {
914 static char p[3][24];
915 static char *vent[5] = {mg_ename[MG_E_VERTEX],NULL,"="};
916 static char *pent[5] = {mg_ename[MG_E_POINT],p[0],p[1],p[2]};
917 static char *znorm[5] = {mg_ename[MG_E_NORMAL],"0","0","0"};
918 char *newav[MG_MAXARGC], nvn[MG_MAXARGC-1][8];
919 double length;
920 int hasnorm;
921 FVECT v1, v2, v3, norm;
922 register C_VERTEX *cv;
923 C_VERTEX *cv0;
924 int rv;
925 register int i, j;
926 /* check arguments */
927 if (ac < 5)
928 return(MG_EARGC);
929 if (!isflt(av[ac-1]))
930 return(MG_ETYPE);
931 length = atof(av[ac-1]);
932 if (length <= FTINY && length >= -FTINY)
933 return(MG_EILL);
934 /* compute face normal */
935 if ((cv0 = c_getvert(av[1])) == NULL)
936 return(MG_EUNDEF);
937 hasnorm = 0;
938 norm[0] = norm[1] = norm[2] = 0.;
939 v1[0] = v1[1] = v1[2] = 0.;
940 for (i = 2; i < ac-1; i++) {
941 if ((cv = c_getvert(av[i])) == NULL)
942 return(MG_EUNDEF);
943 hasnorm += !is0vect(cv->n);
944 v2[0] = cv->p[0] - cv0->p[0];
945 v2[1] = cv->p[1] - cv0->p[1];
946 v2[2] = cv->p[2] - cv0->p[2];
947 fcross(v3, v1, v2);
948 norm[0] += v3[0];
949 norm[1] += v3[1];
950 norm[2] += v3[2];
951 VCOPY(v1, v2);
952 }
953 if (normalize(norm) == 0.)
954 return(MG_EILL);
955 /* create moved vertices */
956 for (i = 1; i < ac-1; i++) {
957 sprintf(nvn[i-1], "_pv%d", i);
958 vent[1] = nvn[i-1];
959 vent[3] = av[i];
960 if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK)
961 return(rv);
962 cv = c_getvert(av[i]); /* checked above */
963 for (j = 0; j < 3; j++)
964 sprintf(p[j], FLTFMT, cv->p[j] - length*norm[j]);
965 if ((rv = mg_handle(MG_E_POINT, 4, pent)) != MG_OK)
966 return(rv);
967 }
968 /* make faces */
969 newav[0] = mg_ename[MG_E_FACE];
970 /* do the side faces */
971 newav[5] = NULL;
972 newav[3] = av[ac-2];
973 newav[4] = nvn[ac-3];
974 for (i = 1; i < ac-1; i++) {
975 newav[1] = nvn[i-1];
976 newav[2] = av[i];
977 if ((rv = mg_handle(MG_E_FACE, 5, newav)) != MG_OK)
978 return(rv);
979 newav[3] = newav[2];
980 newav[4] = newav[1];
981 }
982 /* do top face */
983 for (i = 1; i < ac-1; i++) {
984 if (hasnorm) { /* zero normals */
985 vent[1] = nvn[i-1];
986 if ((rv = mg_handle(MG_E_VERTEX, 2, vent)) != MG_OK)
987 return(rv);
988 if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK)
989 return(rv);
990 }
991 newav[ac-1-i] = nvn[i-1]; /* reverse */
992 }
993 if ((rv = mg_handle(MG_E_FACE, ac-1, newav)) != MG_OK)
994 return(rv);
995 /* do bottom face */
996 if (hasnorm)
997 for (i = 1; i < ac-1; i++) {
998 vent[1] = nvn[i-1];
999 vent[3] = av[i];
1000 if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK)
1001 return(rv);
1002 if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK)
1003 return(rv);
1004 newav[i] = nvn[i-1];
1005 }
1006 else
1007 for (i = 1; i < ac-1; i++)
1008 newav[i] = av[i];
1009 newav[i] = NULL;
1010 if ((rv = mg_handle(MG_E_FACE, i, newav)) != MG_OK)
1011 return(rv);
1012 return(MG_OK);
1013 }
1014
1015
1016 static int
1017 put_cxy() /* put out current xy chromaticities */
1018 {
1019 static char xbuf[24], ybuf[24];
1020 static char *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf};
1021 int rv;
1022
1023 sprintf(xbuf, "%.4f", c_ccolor->cx);
1024 sprintf(ybuf, "%.4f", c_ccolor->cy);
1025 if ((rv = mg_handle(MG_E_CXY, 3, ccom)) != MG_OK)
1026 return(rv);
1027 return(MG_OK);
1028 }
1029
1030
1031 static int
1032 put_cspec() /* put out current color spectrum */
1033 {
1034 char wl[2][6], vbuf[C_CNSS][24];
1035 char *newav[C_CNSS+4];
1036 double sf;
1037 register int i;
1038
1039 if (mg_ehand[MG_E_CSPEC] != c_hcolor) {
1040 sprintf(wl[0], "%d", C_CMINWL);
1041 sprintf(wl[1], "%d", C_CMAXWL);
1042 newav[0] = mg_ename[MG_E_CSPEC];
1043 newav[1] = wl[0];
1044 newav[2] = wl[1];
1045 sf = (double)C_CNSS / c_ccolor->ssum;
1046 for (i = 0; i < C_CNSS; i++) {
1047 sprintf(vbuf[i], "%.4f", sf*c_ccolor->ssamp[i]);
1048 newav[i+3] = vbuf[i];
1049 }
1050 newav[C_CNSS+3] = NULL;
1051 if ((i = mg_handle(MG_E_CSPEC, C_CNSS+3, newav)) != MG_OK)
1052 return(i);
1053 }
1054 return(MG_OK);
1055 }
1056
1057
1058 static int
1059 e_cspec(ac, av) /* handle spectral color */
1060 int ac;
1061 char **av;
1062 {
1063 /* convert to xy chromaticity */
1064 c_ccvt(c_ccolor, C_CSXY);
1065 /* if it's really their handler, use it */
1066 if (mg_ehand[MG_E_CXY] != c_hcolor)
1067 return(put_cxy());
1068 return(MG_OK);
1069 }
1070
1071
1072 static int
1073 e_cmix(ac, av) /* handle mixing of colors */
1074 int ac;
1075 char **av;
1076 {
1077 /*
1078 * Contorted logic works as follows:
1079 * 1. the colors are already mixed in c_hcolor() support function
1080 * 2. if we would handle a spectral result, make sure it's not
1081 * 3. if c_hcolor() would handle a spectral result, don't bother
1082 * 4. otherwise, make cspec entity and pass it to their handler
1083 * 5. if we have only xy results, handle it as c_spec() would
1084 */
1085 if (mg_ehand[MG_E_CSPEC] == e_cspec)
1086 c_ccvt(c_ccolor, C_CSXY);
1087 else if (c_ccolor->flags & C_CDSPEC)
1088 return(put_cspec());
1089 if (mg_ehand[MG_E_CXY] != c_hcolor)
1090 return(put_cxy());
1091 return(MG_OK);
1092 }
1093
1094
1095 static int
1096 e_cct(ac, av) /* handle color temperature */
1097 int ac;
1098 char **av;
1099 {
1100 /*
1101 * Logic is similar to e_cmix here. Support handler has already
1102 * converted temperature to spectral color. Put it out as such
1103 * if they support it, otherwise convert to xy chromaticity and
1104 * put it out if they handle it.
1105 */
1106 if (mg_ehand[MG_E_CSPEC] != e_cspec)
1107 return(put_cspec());
1108 c_ccvt(c_ccolor, C_CSXY);
1109 if (mg_ehand[MG_E_CXY] != c_hcolor)
1110 return(put_cxy());
1111 return(MG_OK);
1112 }