ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/parser.c
Revision: 1.3
Committed: Wed Jun 22 17:15:49 1994 UTC (29 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +46 -22 lines
Log Message:
more bug fixes

File Contents

# User Rev Content
1 greg 1.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 greg 1.2 e_ies(), /* IES luminaire file */
60 greg 1.1 e_include(), /* include file */
61     e_sph(), /* sphere */
62     e_cyl(), /* cylinder */
63     e_cone(), /* cone */
64     e_ring(), /* ring */
65     e_torus(); /* torus */
66    
67     /* alternate handler support functions */
68    
69     static int (*e_supp[MG_NENTITIES])();
70    
71     static char FLTFMT[] = "%.12g";
72    
73     static int warpconends; /* hack for generating good normals */
74    
75    
76     void
77     mg_init() /* initialize alternate entity handlers */
78     {
79     unsigned long ineed = 0, uneed = 0;
80     register int i;
81     /* pick up slack */
82     if (mg_ehand[MG_E_IES] == NULL)
83     mg_ehand[MG_E_IES] = e_ies;
84     if (mg_ehand[MG_E_INCLUDE] == NULL)
85     mg_ehand[MG_E_INCLUDE] = e_include;
86     if (mg_ehand[MG_E_SPH] == NULL) {
87     mg_ehand[MG_E_SPH] = e_sph;
88     ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
89     } else
90     uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
91     if (mg_ehand[MG_E_CYL] == NULL) {
92     mg_ehand[MG_E_CYL] = e_cyl;
93     ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
94     } else
95     uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
96     if (mg_ehand[MG_E_CONE] == NULL) {
97     mg_ehand[MG_E_CONE] = e_cone;
98     ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
99     } else
100     uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
101     if (mg_ehand[MG_E_RING] == NULL) {
102     mg_ehand[MG_E_RING] = e_ring;
103     ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX;
104     } else
105     uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF;
106     if (mg_ehand[MG_E_TORUS] == NULL) {
107     mg_ehand[MG_E_TORUS] = e_torus;
108     ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX;
109     } else
110     uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF;
111     /* check for consistency */
112     if (mg_ehand[MG_E_FACE] != NULL)
113     uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
114     if (mg_ehand[MG_E_CXY] != NULL)
115     uneed |= 1<<MG_E_COLOR;
116     if (mg_ehand[MG_E_RD] != NULL || mg_ehand[MG_E_TD] != NULL ||
117     mg_ehand[MG_E_ED] != NULL ||
118     mg_ehand[MG_E_RS] != NULL ||
119     mg_ehand[MG_E_TS] != NULL)
120     uneed |= 1<<MG_E_MATERIAL;
121     for (i = 0; i < MG_NENTITIES; i++)
122     if (uneed & 1<<i && mg_ehand[i] == NULL) {
123     fprintf(stderr, "Missing support for \"%s\" entity\n",
124     mg_ename[i]);
125     exit(1);
126     }
127     /* add support as needed */
128     if (ineed & 1<<MG_E_VERTEX && mg_ehand[MG_E_VERTEX] != c_hvertex)
129     e_supp[MG_E_VERTEX] = c_hvertex;
130     if (ineed & 1<<MG_E_POINT && mg_ehand[MG_E_POINT] != c_hvertex)
131     e_supp[MG_E_POINT] = c_hvertex;
132     if (ineed & 1<<MG_E_NORMAL && mg_ehand[MG_E_NORMAL] != c_hvertex)
133     e_supp[MG_E_NORMAL] = c_hvertex;
134     /* discard remaining entities */
135     for (i = 0; i < MG_NENTITIES; i++)
136     if (mg_ehand[i] == NULL)
137     mg_ehand[i] = e_any_toss;
138     }
139    
140    
141    
142     int
143     mg_entity(name) /* get entity number from its name */
144     char *name;
145     {
146     static LUTAB ent_tab; /* entity lookup table */
147     register char *cp;
148    
149     if (!ent_tab.tsiz) { /* initialize hash table */
150     if (!lu_init(&ent_tab, MG_NENTITIES))
151     return(-1); /* what to do? */
152     for (cp = mg_ename[MG_NENTITIES-1]; cp >= mg_ename[0];
153     cp -= sizeof(mg_ename[0]))
154     lu_find(&ent_tab, cp)->key = cp;
155     }
156     cp = lu_find(&ent_tab, name)->key;
157     if (cp == NULL)
158     return(-1);
159     return((cp - mg_ename[0])/sizeof(mg_ename[0]));
160     }
161    
162    
163     static int
164     handle_it(en, ac, av) /* pass entity to appropriate handler */
165     register int en;
166     int ac;
167     char **av;
168     {
169     int rv;
170    
171     if (en < 0 && (en = mg_entity(av[0])) < 0)
172     return(MG_EUNK);
173     if (e_supp[en] != NULL) {
174     if ((rv = (*e_supp[en])(ac, av)) != MG_OK)
175     return(rv);
176     }
177     return((*mg_ehand[en])(ac, av));
178     }
179    
180    
181     int
182     mg_open(ctx, fn) /* open new input file */
183     register MG_FCTXT *ctx;
184     char *fn;
185     {
186     int olen;
187     register char *cp;
188    
189     ctx->lineno = 0;
190     if (fn == NULL) {
191     ctx->fname = "<stdin>";
192     ctx->fp = stdin;
193     ctx->prev = mg_file;
194     mg_file = ctx;
195     return(MG_OK);
196     }
197     /* get name relative to this context */
198     if (mg_file != NULL &&
199     (cp = strrchr(mg_file->fname, '/')) != NULL)
200     olen = cp - mg_file->fname + 1;
201     else
202     olen = 0;
203     ctx->fname = (char *)malloc(olen+strlen(fn)+1);
204     if (ctx->fname == NULL)
205     return(MG_EMEM);
206     if (olen)
207     strcpy(ctx->fname, mg_file->fname);
208     strcpy(ctx->fname+olen, fn);
209     ctx->fp = fopen(ctx->fname, "r");
210     if (ctx->fp == NULL) {
211     free((MEM_PTR)ctx->fname);
212     return(MG_ENOFILE);
213     }
214     ctx->prev = mg_file; /* establish new context */
215     mg_file = ctx;
216     return(MG_OK);
217     }
218    
219    
220     void
221     mg_close() /* close input file */
222     {
223     register MG_FCTXT *ctx = mg_file;
224    
225     mg_file = ctx->prev; /* restore enclosing context */
226     if (ctx->fp == stdin)
227     return; /* don't close standard input */
228     fclose(ctx->fp);
229     free((MEM_PTR)ctx->fname);
230     }
231    
232    
233     int
234     mg_rewind() /* rewind input file */
235     {
236     if (mg_file->lineno == 0)
237     return(MG_OK);
238     if (mg_file->fp == stdin)
239     return(MG_ESEEK); /* cannot seek on standard input */
240     if (fseek(mg_file->fp, 0L, 0) == EOF)
241     return(MG_ESEEK);
242     mg_file->lineno = 0;
243     return(MG_OK);
244     }
245    
246    
247     int
248     mg_read() /* read next line from file */
249     {
250     register int len = 0;
251    
252     do {
253     if (fgets(mg_file->inpline+len,
254     MG_MAXLINE-len, mg_file->fp) == NULL)
255     return(len);
256     mg_file->lineno++;
257     len += strlen(mg_file->inpline+len);
258     if (len > 1 && mg_file->inpline[len-2] == '\\')
259     mg_file->inpline[--len-1] = ' ';
260     } while (mg_file->inpline[len]);
261    
262     return(len);
263     }
264    
265    
266     int
267     mg_parse() /* parse current input line */
268     {
269     char abuf[MG_MAXLINE];
270     char *argv[MG_MAXARGC];
271     int en;
272     register char *cp, **ap;
273    
274     strcpy(cp=abuf, mg_file->inpline);
275     ap = argv; /* break into words */
276     for ( ; ; ) {
277     while (isspace(*cp))
278     *cp++ = '\0';
279     if (!*cp)
280     break;
281     if (ap - argv >= MG_MAXARGC-1)
282     return(MG_EARGC);
283     *ap++ = cp;
284     while (*++cp && !isspace(*cp))
285     ;
286     }
287     if (ap == argv)
288     return(MG_OK); /* no words in line */
289     *ap = NULL;
290     /* else handle it */
291     return(handle_it(-1, ap-argv, argv));
292     }
293    
294    
295     int
296     mg_load(fn) /* load an MGF file */
297     char *fn;
298     {
299     MG_FCTXT cntxt;
300     int rval;
301    
302     if ((rval = mg_open(&cntxt, fn)) != MG_OK) {
303 greg 1.2 fprintf(stderr, "%s: %s\n", fn, mg_err[rval]);
304 greg 1.1 return(rval);
305     }
306     while (mg_read()) /* parse each line */
307     if ((rval = mg_parse()) != MG_OK) {
308     fprintf(stderr, "%s: %d: %s:\n%s", cntxt.fname,
309     cntxt.lineno, mg_err[rval],
310     cntxt.inpline);
311     break;
312     }
313     mg_close();
314     return(rval);
315     }
316    
317    
318     void
319     mg_clear() /* clear parser history */
320     {
321     c_clearall(); /* clear context tables */
322     mg_file = NULL; /* reset our context */
323     }
324    
325    
326     int
327     mg_iterate(ac, av, f) /* iterate on statement */
328     int ac;
329     register char **av;
330     int (*f)();
331     {
332     int niter, rval;
333     register int i, j;
334     char *argv[MG_MAXARGC];
335     char cntbuf[10];
336     /* build partial transformation */
337     for (i = 0; i < ac; i++) {
338     if (av[i][0] == '-' && av[i][1] == 'a' && av[i][2] == '\0')
339     break;
340     argv[i+1] = av[i];
341     }
342     argv[i+1] = NULL;
343     if (i) { /* handle transformation */
344     argv[0] = mg_ename[MG_E_XF];
345     if ((rval = handle_it(MG_E_XF, i+1, argv)) != MG_OK)
346     return(rval);
347     }
348     if (i < ac) { /* run array */
349     if (i+1 >= ac || !isint(av[i+1]))
350     return(MG_ETYPE);
351     niter = atoi(av[i+1]);
352 greg 1.2 argv[0] = mg_ename[MG_E_OBJECT];
353 greg 1.1 argv[1] = cntbuf;
354     for (j = 2; j+i < ac; j++)
355     argv[j] = av[j+i];
356     argv[j] = NULL;
357     for (j = 0; j < niter; j++) {
358     sprintf(cntbuf, "%d", j);
359 greg 1.2 if ((rval = handle_it(MG_E_OBJECT, 2, argv)) != MG_OK)
360     return(rval);
361     argv[0] = "-i";
362 greg 1.1 if ((rval = mg_iterate(ac-i, argv, f)) != MG_OK)
363 greg 1.2 return(rval);
364     argv[0] = mg_ename[MG_E_OBJECT];
365     if ((rval = handle_it(MG_E_OBJECT, 1, argv)) != MG_OK)
366 greg 1.1 return(rval);
367     }
368     } else if ((rval = (*f)()) != MG_OK) /* else do this instance */
369     return(rval);
370     if (i) { /* reset the transform */
371     argv[0] = mg_ename[MG_E_XF];
372     argv[1] = NULL;
373     (void)handle_it(MG_E_XF, 1, argv);
374     }
375     return(MG_OK);
376     }
377    
378    
379     /****************************************************************************
380     * The following routines handle unsupported entities
381     */
382    
383    
384     static int
385     e_any_toss(ac, av) /* discard an unwanted entity */
386     int ac;
387     char **av;
388     {
389     return(MG_OK);
390     }
391    
392    
393     static int
394     reload_file() /* reload current MGF file */
395     {
396     register int rval;
397    
398     if ((rval = mg_rewind()) != MG_OK)
399     return(rval);
400     while (mg_read())
401     if ((rval = mg_parse()) != MG_OK)
402     return(rval);
403     return(MG_OK);
404     }
405    
406    
407     static int
408     e_include(ac, av) /* include file */
409     int ac;
410     char **av;
411     {
412     MG_FCTXT ictx;
413     int rv;
414    
415     if (ac < 2)
416     return(MG_EARGC);
417     if ((rv = mg_open(&ictx, av[1])) != MG_OK)
418     return(rv);
419     if ((rv = mg_iterate(ac-2, av+2, reload_file)) != MG_OK) {
420     fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname,
421     ictx.lineno, mg_err[rv], ictx.inpline);
422     mg_close();
423     return(MG_EINCL);
424     }
425     mg_close();
426     return(MG_OK);
427     }
428    
429    
430     static void
431     make_axes(u, v, w) /* compute u and v given w (normalized) */
432     FVECT u, v, w;
433     {
434     register int i;
435    
436     v[0] = v[1] = v[2] = 0.;
437     for (i = 0; i < 3; i++)
438     if (w[i] < .6 && w[i] > -.6)
439     break;
440     v[i] = 1.;
441     fcross(u, v, w);
442     normalize(u);
443     fcross(v, w, u);
444     }
445    
446    
447     static int
448     e_sph(ac, av) /* expand a sphere into cones */
449     int ac;
450     char **av;
451     {
452     static char p2x[24], p2y[24], p2z[24], r1[24], r2[24];
453     static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_sv1","=","_sv2"};
454     static char *v2ent[4] = {mg_ename[MG_E_VERTEX],"_sv2","="};
455     static char *p2ent[5] = {mg_ename[MG_E_POINT],p2x,p2y,p2z};
456     static char *conent[6] = {mg_ename[MG_E_CONE],"_sv1",r1,"_sv2",r2};
457     register C_VERTEX *cv;
458     register int i;
459     int rval;
460     double rad;
461     double theta;
462    
463     if (ac != 3)
464     return(MG_EARGC);
465     if ((cv = c_getvert(av[1])) == NULL)
466     return(MG_EUNDEF);
467     if (!isflt(av[2]))
468     return(MG_ETYPE);
469     rad = atof(av[2]);
470     /* initialize */
471     warpconends = 1;
472     if ((rval = handle_it(MG_E_VERTEX, 3, v2ent)) != MG_OK)
473     return(rval);
474     sprintf(p2x, FLTFMT, cv->p[0]);
475     sprintf(p2y, FLTFMT, cv->p[1]);
476     sprintf(p2z, FLTFMT, cv->p[2]+rad);
477     if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
478     return(rval);
479     r2[0] = '0'; r2[1] = '\0';
480     for (i = 1; i <= 2*mg_nqcdivs; i++) {
481     theta = i*(PI/2)/mg_nqcdivs;
482     if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
483     return(rval);
484     sprintf(p2z, FLTFMT, cv->p[2]+rad*cos(theta));
485     if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK)
486     return(rval);
487     if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
488     return(rval);
489     strcpy(r1, r2);
490     sprintf(r2, FLTFMT, rad*sin(theta));
491     if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK)
492     return(rval);
493     }
494     warpconends = 0;
495     return(MG_OK);
496     }
497    
498    
499     static int
500     e_torus(ac, av) /* expand a torus into cones */
501     int ac;
502     char **av;
503     {
504     static char p2[3][24], r1[24], r2[24];
505     static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_tv1","=","_tv2"};
506     static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_tv2","="};
507     static char *p2ent[5] = {mg_ename[MG_E_POINT],p2[0],p2[1],p2[2]};
508     static char *conent[6] = {mg_ename[MG_E_CONE],"_tv1",r1,"_tv2",r2};
509     register C_VERTEX *cv;
510     register int i, j;
511     int rval;
512     int sgn;
513     double minrad, maxrad, avgrad;
514     double theta;
515    
516     if (ac != 4)
517     return(MG_EARGC);
518     if ((cv = c_getvert(av[1])) == NULL)
519     return(MG_EUNDEF);
520 greg 1.3 if (is0vect(cv->n))
521 greg 1.1 return(MG_EILL);
522     if (!isflt(av[2]) || !isflt(av[3]))
523     return(MG_ETYPE);
524     minrad = atof(av[2]);
525 greg 1.3 round0(minrad);
526 greg 1.1 maxrad = atof(av[3]);
527     /* check orientation */
528     if (minrad > 0.)
529     sgn = 1;
530     else if (minrad < 0.)
531     sgn = -1;
532     else if (maxrad > 0.)
533     sgn = 1;
534     else if (maxrad < 0.)
535     sgn = -1;
536     else
537     return(MG_EILL);
538     if (sgn*(maxrad-minrad) <= 0.)
539     return(MG_EILL);
540     /* initialize */
541     warpconends = 1;
542     v2ent[3] = av[1];
543     for (j = 0; j < 3; j++)
544     sprintf(p2[j], FLTFMT, cv->p[j] +
545     .5*sgn*(maxrad-minrad)*cv->n[j]);
546     if ((rval = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
547     return(rval);
548     if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
549     return(rval);
550     sprintf(r2, FLTFMT, avgrad=.5*(minrad+maxrad));
551     /* run outer section */
552     for (i = 1; i <= 2*mg_nqcdivs; i++) {
553     theta = i*(PI/2)/mg_nqcdivs;
554     if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
555     return(rval);
556     for (j = 0; j < 3; j++)
557     sprintf(p2[j], FLTFMT, cv->p[j] +
558     .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
559     if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK)
560     return(rval);
561     if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
562     return(rval);
563     strcpy(r1, r2);
564     sprintf(r2, FLTFMT, avgrad + .5*(maxrad-minrad)*sin(theta));
565     if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK)
566     return(rval);
567     }
568     /* run inner section */
569     sprintf(r2, FLTFMT, -.5*(minrad+maxrad));
570     for ( ; i <= 4*mg_nqcdivs; i++) {
571     theta = i*(PI/2)/mg_nqcdivs;
572     for (j = 0; j < 3; j++)
573     sprintf(p2[j], FLTFMT, cv->p[j] +
574     .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
575     if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
576     return(rval);
577     if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK)
578     return(rval);
579     if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
580     return(rval);
581     strcpy(r1, r2);
582     sprintf(r2, FLTFMT, -avgrad - .5*(maxrad-minrad)*sin(theta));
583     if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK)
584     return(rval);
585     }
586     warpconends = 0;
587     return(MG_OK);
588     }
589    
590    
591     static int
592     e_cyl(ac, av) /* replace a cylinder with equivalent cone */
593     int ac;
594     char **av;
595     {
596     static char *avnew[6] = {mg_ename[MG_E_CONE]};
597    
598     if (ac != 4)
599     return(MG_EARGC);
600     avnew[1] = av[1];
601     avnew[2] = av[2];
602     avnew[3] = av[3];
603     avnew[4] = av[2];
604     return(handle_it(MG_E_CONE, 5, avnew));
605     }
606    
607    
608     static int
609     e_ring(ac, av) /* turn a ring into polygons */
610     int ac;
611     char **av;
612     {
613     static char p3[3][24], p4[3][24];
614     static char *nzent[5] = {mg_ename[MG_E_NORMAL],"0","0","0"};
615     static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_rv1","="};
616     static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_rv2","=","_rv3"};
617     static char *v3ent[4] = {mg_ename[MG_E_VERTEX],"_rv3","="};
618     static char *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]};
619     static char *v4ent[4] = {mg_ename[MG_E_VERTEX],"_rv4","="};
620     static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]};
621     static char *fent[6] = {mg_ename[MG_E_FACE],"_rv1","_rv2","_rv3","_rv4"};
622     register C_VERTEX *cv;
623     register int i, j;
624     FVECT u, v;
625     double minrad, maxrad;
626     int rv;
627     double theta, d;
628    
629     if (ac != 4)
630     return(MG_EARGC);
631     if ((cv = c_getvert(av[1])) == NULL)
632     return(MG_EUNDEF);
633 greg 1.3 if (is0vect(cv->n))
634 greg 1.1 return(MG_EILL);
635     if (!isflt(av[2]) || !isflt(av[3]))
636     return(MG_ETYPE);
637     minrad = atof(av[2]);
638 greg 1.3 round0(minrad);
639 greg 1.1 maxrad = atof(av[3]);
640     if (minrad < 0. || maxrad <= minrad)
641     return(MG_EILL);
642     /* initialize */
643     make_axes(u, v, cv->n);
644     for (j = 0; j < 3; j++)
645     sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*u[j]);
646     if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK)
647     return(rv);
648     if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
649     return(rv);
650     if (minrad == 0.) { /* closed */
651     v1ent[3] = av[1];
652     if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
653     return(rv);
654     if ((rv = handle_it(MG_E_NORMAL, 4, nzent)) != MG_OK)
655     return(rv);
656     for (i = 1; i <= 4*mg_nqcdivs; i++) {
657     theta = i*(PI/2)/mg_nqcdivs;
658     if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
659     return(rv);
660     for (j = 0; j < 3; j++)
661     sprintf(p3[j], FLTFMT, cv->p[j] +
662     maxrad*u[j]*cos(theta) +
663     maxrad*v[j]*sin(theta));
664 greg 1.3 if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
665 greg 1.1 return(rv);
666     if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
667     return(rv);
668     if ((rv = handle_it(MG_E_FACE, 4, fent)) != MG_OK)
669     return(rv);
670     }
671     } else { /* open */
672     if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK)
673     return(rv);
674     for (j = 0; j < 3; j++)
675     sprintf(p4[j], FLTFMT, cv->p[j] + minrad*u[j]);
676     if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
677     return(rv);
678     v1ent[3] = "_rv4";
679     for (i = 1; i <= 4*mg_nqcdivs; i++) {
680     theta = i*(PI/2)/mg_nqcdivs;
681     if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
682     return(rv);
683     if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
684     return(rv);
685     for (j = 0; j < 3; j++) {
686     d = u[j]*cos(theta) + v[j]*sin(theta);
687     sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*d);
688     sprintf(p4[j], FLTFMT, cv->p[j] + minrad*d);
689     }
690 greg 1.3 if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
691 greg 1.1 return(rv);
692     if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
693     return(rv);
694 greg 1.3 if ((rv = handle_it(MG_E_VERTEX, 2, v4ent)) != MG_OK)
695 greg 1.1 return(rv);
696     if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
697     return(rv);
698     if ((rv = handle_it(MG_E_FACE, 5, fent)) != MG_OK)
699     return(rv);
700     }
701     }
702     return(MG_OK);
703     }
704    
705    
706     static int
707     e_cone(ac, av) /* turn a cone into polygons */
708     int ac;
709     char **av;
710     {
711     static char p3[3][24], p4[3][24], n3[3][24], n4[3][24];
712     static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_cv1","="};
713     static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_cv2","=","_cv3"};
714     static char *v3ent[4] = {mg_ename[MG_E_VERTEX],"_cv3","="};
715     static char *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]};
716     static char *n3ent[5] = {mg_ename[MG_E_NORMAL],n3[0],n3[1],n3[2]};
717     static char *v4ent[4] = {mg_ename[MG_E_VERTEX],"_cv4","="};
718     static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]};
719     static char *n4ent[5] = {mg_ename[MG_E_NORMAL],n4[0],n4[1],n4[2]};
720     static char *fent[6] = {mg_ename[MG_E_FACE],"_cv1","_cv2","_cv3","_cv4"};
721     register C_VERTEX *cv1, *cv2;
722     register int i, j;
723     FVECT u, v, w;
724     double rad1, rad2;
725     int sgn;
726     double n1off, n2off;
727     double d;
728     int rv;
729     double theta;
730    
731     if (ac != 5)
732     return(MG_EARGC);
733     if ((cv1 = c_getvert(av[1])) == NULL ||
734     (cv2 = c_getvert(av[3])) == NULL)
735     return(MG_EUNDEF);
736     if (!isflt(av[2]) || !isflt(av[4]))
737     return(MG_ETYPE);
738     rad1 = atof(av[2]);
739 greg 1.3 round0(rad1);
740 greg 1.1 rad2 = atof(av[4]);
741 greg 1.3 round0(rad2);
742 greg 1.1 if (rad1 == 0.) {
743     if (rad2 == 0.)
744     return(MG_EILL);
745     } else if (rad2 != 0.) {
746     if (rad1 < 0. ^ rad2 < 0.)
747     return(MG_EILL);
748     } else { /* swap */
749     C_VERTEX *cv;
750    
751     cv = cv1;
752     cv1 = cv2;
753     cv2 = cv;
754     d = rad1;
755     rad1 = rad2;
756     rad2 = d;
757     }
758     sgn = rad2 < 0. ? -1 : 1;
759     /* initialize */
760     for (j = 0; j < 3; j++)
761     w[j] = cv1->p[j] - cv2->p[j];
762     if ((d = normalize(w)) == 0.)
763     return(MG_EILL);
764     n1off = n2off = (rad2 - rad1)/d;
765 greg 1.3 if (warpconends) { /* hack for e_sph and e_torus */
766     d = atan(n2off) - (PI/4)/mg_nqcdivs;
767     if (d <= -PI/2+FTINY)
768     n2off = -FHUGE;
769     else
770     n2off = tan(d);
771     }
772 greg 1.1 make_axes(u, v, w);
773     for (j = 0; j < 3; j++) {
774     sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*u[j]);
775 greg 1.3 if (n2off <= -FHUGE)
776     sprintf(n3[j], FLTFMT, -w[j]);
777     else
778     sprintf(n3[j], FLTFMT, u[j] + w[j]*n2off);
779 greg 1.1 }
780     if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK)
781     return(rv);
782     if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
783     return(rv);
784     if ((rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
785     return(rv);
786     if (rad1 == 0.) { /* triangles */
787     v1ent[3] = av[1];
788     if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
789     return(rv);
790     for (j = 0; j < 3; j++)
791     sprintf(n4[j], FLTFMT, w[j]);
792     if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
793     return(rv);
794     for (i = 1; i <= 4*mg_nqcdivs; i++) {
795     theta = sgn*i*(PI/2)/mg_nqcdivs;
796     if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
797     return(rv);
798     for (j = 0; j < 3; j++) {
799     d = u[j]*cos(theta) + v[j]*sin(theta);
800     sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d);
801 greg 1.3 if (n2off > -FHUGE)
802     sprintf(n3[j], FLTFMT, d + w[j]*n2off);
803 greg 1.1 }
804 greg 1.3 if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
805 greg 1.1 return(rv);
806     if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
807     return(rv);
808 greg 1.3 if (n2off > -FHUGE &&
809     (rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
810 greg 1.1 return(rv);
811     if ((rv = handle_it(MG_E_FACE, 4, fent)) != MG_OK)
812     return(rv);
813     }
814     } else { /* quads */
815     v1ent[3] = "_cv4";
816 greg 1.3 if (warpconends) { /* hack for e_sph and e_torus */
817     d = atan(n1off) + (PI/4)/mg_nqcdivs;
818     if (d >= PI/2-FTINY)
819     n1off = FHUGE;
820     else
821     n1off = tan(atan(n1off)+(PI/4)/mg_nqcdivs);
822     }
823 greg 1.1 for (j = 0; j < 3; j++) {
824     sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*u[j]);
825 greg 1.3 if (n1off >= FHUGE)
826     sprintf(n4[j], FLTFMT, w[j]);
827     else
828     sprintf(n4[j], FLTFMT, u[j] + w[j]*n1off);
829 greg 1.1 }
830     if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK)
831     return(rv);
832     if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
833     return(rv);
834     if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
835     return(rv);
836     for (i = 1; i <= 4*mg_nqcdivs; i++) {
837     theta = sgn*i*(PI/2)/mg_nqcdivs;
838     if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
839     return(rv);
840     if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
841     return(rv);
842     for (j = 0; j < 3; j++) {
843     d = u[j]*cos(theta) + v[j]*sin(theta);
844     sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d);
845 greg 1.3 if (n2off > -FHUGE)
846     sprintf(n3[j], FLTFMT, d + w[j]*n2off);
847 greg 1.1 sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*d);
848 greg 1.3 if (n1off < FHUGE)
849     sprintf(n4[j], FLTFMT, d + w[j]*n1off);
850 greg 1.1 }
851 greg 1.3 if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
852 greg 1.1 return(rv);
853     if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
854     return(rv);
855 greg 1.3 if (n2off > -FHUGE &&
856     (rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
857 greg 1.1 return(rv);
858 greg 1.3 if ((rv = handle_it(MG_E_VERTEX, 2, v4ent)) != MG_OK)
859 greg 1.1 return(rv);
860     if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
861     return(rv);
862 greg 1.3 if (n1off < FHUGE &&
863     (rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
864 greg 1.1 return(rv);
865     if ((rv = handle_it(MG_E_FACE, 5, fent)) != MG_OK)
866     return(rv);
867     }
868     }
869     return(MG_OK);
870     }