ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/parser.c
(Generate patch)

Comparing ray/src/cv/mgflib/parser.c (file contents):
Revision 1.5 by greg, Fri Jun 24 09:40:29 1994 UTC vs.
Revision 1.26 by gregl, Mon Dec 15 09:41:37 1997 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1994 Regents of the University of California */
1 > /* Copyright (c) 1996 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10  
11   #include <stdio.h>
12   #include <math.h>
13 + #include <stdlib.h>
14   #include <ctype.h>
15   #include <string.h>
16   #include "parser.h"
# Line 27 | Line 28 | char   mg_ename[MG_NENTITIES][MG_MAXELEN] = MG_NAMELIST;
28  
29   int     (*mg_ehand[MG_NENTITIES])();
30  
31 +                        /* Handler routine for unknown entities */
32 +
33 + int     (*mg_uhand)() = mg_defuhand;
34 +
35 + unsigned        mg_nunknown;    /* count of unknown entities */
36 +
37                          /* error messages */
38  
39   char    *mg_err[MG_NERRS] = MG_ERRLIST;
# Line 53 | Line 60 | int    mg_nqcdivs = MG_NQCD;   /* number of divisions per q
60  
61   /* temporary settings for testing */
62   #define e_ies e_any_toss
56 #define e_cmix e_any_toss
57 #define e_cspec e_any_toss
63                                  /* alternate handler routines */
64  
65   static int      e_any_toss(),           /* discard unneeded entity */
66                  e_ies(),                /* IES luminaire file */
67 <                e_include(),            /* include file */
63 <                e_sph(),                /* sphere */
67 >                e_cct(),                /* color temperature */
68                  e_cmix(),               /* color mixtures */
69                  e_cspec();              /* color spectra */
66                e_cyl(),                /* cylinder */
67                e_cone(),               /* cone */
68                e_prism(),              /* prism */
69                e_ring(),               /* ring */
70                e_torus();              /* torus */
70  
71                                  /* alternate handler support functions */
72  
# Line 90 | Line 89 | mg_init()                      /* initialize alternate entity handlers */
89                  mg_ehand[MG_E_INCLUDE] = e_include;
90          if (mg_ehand[MG_E_SPH] == NULL) {
91                  mg_ehand[MG_E_SPH] = e_sph;
92 <                ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
92 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
93          } else
94 <                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
94 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
95          if (mg_ehand[MG_E_CYL] == NULL) {
96                  mg_ehand[MG_E_CYL] = e_cyl;
97 <                ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
97 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
98          } else
99 <                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
99 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
100          if (mg_ehand[MG_E_CONE] == NULL) {
101                  mg_ehand[MG_E_CONE] = e_cone;
102 <                ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
102 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
103          } else
104 <                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
104 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
105          if (mg_ehand[MG_E_RING] == NULL) {
106                  mg_ehand[MG_E_RING] = e_ring;
107 <                ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX;
107 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX;
108          } else
109 <                uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF;
109 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX|1L<<MG_E_XF;
110          if (mg_ehand[MG_E_PRISM] == NULL) {
111                  mg_ehand[MG_E_PRISM] = e_prism;
112 <                ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
112 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
113          } else
114 <                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
114 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
115          if (mg_ehand[MG_E_TORUS] == NULL) {
116                  mg_ehand[MG_E_TORUS] = e_torus;
117 <                ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX;
117 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX;
118          } else
119 <                uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF;
119 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX|1L<<MG_E_XF;
120 >        if (mg_ehand[MG_E_FACE] == NULL)
121 >                mg_ehand[MG_E_FACE] = mg_ehand[MG_E_FACEH];
122 >        else if (mg_ehand[MG_E_FACEH] == NULL)
123 >                mg_ehand[MG_E_FACEH] = e_faceh;
124          if (mg_ehand[MG_E_COLOR] != NULL) {
125 <                if (mg_ehand[MG_E_CMIX] == NULL)
125 >                if (mg_ehand[MG_E_CMIX] == NULL) {
126                          mg_ehand[MG_E_CMIX] = e_cmix;
127 <                if (mg_ehand[MG_E_CSPEC] == NULL)
127 >                        ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT;
128 >                }
129 >                if (mg_ehand[MG_E_CSPEC] == NULL) {
130                          mg_ehand[MG_E_CSPEC] = e_cspec;
131 +                        ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT;
132 +                }
133 +                if (mg_ehand[MG_E_CCT] == NULL) {
134 +                        mg_ehand[MG_E_CCT] = e_cct;
135 +                        ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT;
136 +                }
137          }
138                                          /* check for consistency */
139          if (mg_ehand[MG_E_FACE] != NULL)
140 <                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
141 <        if (mg_ehand[MG_E_CXY] != NULL)
142 <                uneed |= 1<<MG_E_COLOR;
140 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
141 >        if (mg_ehand[MG_E_CXY] != NULL || mg_ehand[MG_E_CSPEC] != NULL ||
142 >                        mg_ehand[MG_E_CMIX] != NULL)
143 >                uneed |= 1L<<MG_E_COLOR;
144          if (mg_ehand[MG_E_RD] != NULL || mg_ehand[MG_E_TD] != NULL ||
145 +                        mg_ehand[MG_E_IR] != NULL ||
146                          mg_ehand[MG_E_ED] != NULL ||
147                          mg_ehand[MG_E_RS] != NULL ||
148 <                        mg_ehand[MG_E_TS] != NULL)
149 <                uneed |= 1<<MG_E_MATERIAL;
148 >                        mg_ehand[MG_E_TS] != NULL ||
149 >                        mg_ehand[MG_E_SIDES] != NULL)
150 >                uneed |= 1L<<MG_E_MATERIAL;
151          for (i = 0; i < MG_NENTITIES; i++)
152 <                if (uneed & 1<<i && mg_ehand[i] == NULL) {
152 >                if (uneed & 1L<<i && mg_ehand[i] == NULL) {
153                          fprintf(stderr, "Missing support for \"%s\" entity\n",
154                                          mg_ename[i]);
155                          exit(1);
156                  }
157                                          /* add support as needed */
158 <        if (ineed & 1<<MG_E_VERTEX && mg_ehand[MG_E_VERTEX] != c_hvertex)
158 >        if (ineed & 1L<<MG_E_VERTEX && mg_ehand[MG_E_VERTEX] != c_hvertex)
159                  e_supp[MG_E_VERTEX] = c_hvertex;
160 <        if (ineed & 1<<MG_E_POINT && mg_ehand[MG_E_POINT] != c_hvertex)
160 >        if (ineed & 1L<<MG_E_POINT && mg_ehand[MG_E_POINT] != c_hvertex)
161                  e_supp[MG_E_POINT] = c_hvertex;
162 <        if (ineed & 1<<MG_E_NORMAL && mg_ehand[MG_E_NORMAL] != c_hvertex)
162 >        if (ineed & 1L<<MG_E_NORMAL && mg_ehand[MG_E_NORMAL] != c_hvertex)
163                  e_supp[MG_E_NORMAL] = c_hvertex;
164 +        if (ineed & 1L<<MG_E_COLOR && mg_ehand[MG_E_COLOR] != c_hcolor)
165 +                e_supp[MG_E_COLOR] = c_hcolor;
166 +        if (ineed & 1L<<MG_E_CXY && mg_ehand[MG_E_CXY] != c_hcolor)
167 +                e_supp[MG_E_CXY] = c_hcolor;
168 +        if (ineed & 1L<<MG_E_CSPEC && mg_ehand[MG_E_CSPEC] != c_hcolor)
169 +                e_supp[MG_E_CSPEC] = c_hcolor;
170 +        if (ineed & 1L<<MG_E_CMIX && mg_ehand[MG_E_CMIX] != c_hcolor)
171 +                e_supp[MG_E_CMIX] = c_hcolor;
172 +        if (ineed & 1L<<MG_E_CCT && mg_ehand[MG_E_CCT] != c_hcolor)
173 +                e_supp[MG_E_CCT] = c_hcolor;
174                                          /* discard remaining entities */
175          for (i = 0; i < MG_NENTITIES; i++)
176                  if (mg_ehand[i] == NULL)
# Line 154 | Line 178 | mg_init()                      /* initialize alternate entity handlers */
178   }
179  
180  
157
181   int
182   mg_entity(name)                 /* get entity number from its name */
183   char    *name;
184   {
185 <        static LUTAB    ent_tab;        /* entity lookup table */
185 >        static LUTAB    ent_tab = LU_SINIT(NULL,NULL);  /* lookup table */
186          register char   *cp;
187  
188          if (!ent_tab.tsiz) {            /* initialize hash table */
# Line 176 | Line 199 | char   *name;
199   }
200  
201  
202 < static int
203 < handle_it(en, ac, av)           /* pass entity to appropriate handler */
202 > int
203 > mg_handle(en, ac, av)           /* pass entity to appropriate handler */
204   register int    en;
205   int     ac;
206   char    **av;
207   {
208          int     rv;
209  
210 <        if (en < 0 && (en = mg_entity(av[0])) < 0)
210 >        if (en < 0 && (en = mg_entity(av[0])) < 0) {    /* unknown entity */
211 >                if (mg_uhand != NULL)
212 >                        return((*mg_uhand)(ac, av));
213                  return(MG_EUNK);
214 <        if (e_supp[en] != NULL) {
214 >        }
215 >        if (e_supp[en] != NULL) {                       /* support handler */
216                  if ((rv = (*e_supp[en])(ac, av)) != MG_OK)
217                          return(rv);
218          }
219 <        return((*mg_ehand[en])(ac, av));
219 >        return((*mg_ehand[en])(ac, av));                /* assigned handler */
220   }
221  
222  
# Line 199 | Line 225 | mg_open(ctx, fn)                       /* open new input file */
225   register MG_FCTXT       *ctx;
226   char    *fn;
227   {
228 <        int     olen;
228 >        static int      nfids;
229          register char   *cp;
230  
231 +        ctx->fid = ++nfids;
232          ctx->lineno = 0;
233          if (fn == NULL) {
234 <                ctx->fname = "<stdin>";
234 >                strcpy(ctx->fname, "<stdin>");
235                  ctx->fp = stdin;
236                  ctx->prev = mg_file;
237                  mg_file = ctx;
238                  return(MG_OK);
239          }
240                                          /* get name relative to this context */
241 <        if (mg_file != NULL &&
215 <                        (cp = strrchr(mg_file->fname, '/')) != NULL)
216 <                olen = cp - mg_file->fname + 1;
217 <        else
218 <                olen = 0;
219 <        ctx->fname = (char *)malloc(olen+strlen(fn)+1);
220 <        if (ctx->fname == NULL)
221 <                return(MG_EMEM);
222 <        if (olen)
241 >        if (mg_file != NULL && (cp = strrchr(mg_file->fname, '/')) != NULL) {
242                  strcpy(ctx->fname, mg_file->fname);
243 <        strcpy(ctx->fname+olen, fn);
243 >                strcpy(ctx->fname+(cp-mg_file->fname+1), fn);
244 >        } else
245 >                strcpy(ctx->fname, fn);
246          ctx->fp = fopen(ctx->fname, "r");
247 <        if (ctx->fp == NULL) {
227 <                free((MEM_PTR)ctx->fname);
247 >        if (ctx->fp == NULL)
248                  return(MG_ENOFILE);
229        }
249          ctx->prev = mg_file;            /* establish new context */
250          mg_file = ctx;
251          return(MG_OK);
# Line 239 | Line 258 | mg_close()                     /* close input file */
258          register MG_FCTXT       *ctx = mg_file;
259  
260          mg_file = ctx->prev;            /* restore enclosing context */
261 <        if (ctx->fp == stdin)
262 <                return;                 /* don't close standard input */
244 <        fclose(ctx->fp);
245 <        free((MEM_PTR)ctx->fname);
261 >        if (ctx->fp != stdin)           /* close file if it's a file */
262 >                fclose(ctx->fp);
263   }
264  
265  
266 + void
267 + mg_fgetpos(pos)                 /* get current position in input file */
268 + register MG_FPOS        *pos;
269 + {
270 +        pos->fid = mg_file->fid;
271 +        pos->lineno = mg_file->lineno;
272 +        pos->offset = ftell(mg_file->fp);
273 + }
274 +
275 +
276   int
277 < mg_rewind()                     /* rewind input file */
277 > mg_fgoto(pos)                   /* reposition input file pointer */
278 > register MG_FPOS        *pos;
279   {
280 <        if (mg_file->lineno == 0)
280 >        if (pos->fid != mg_file->fid)
281 >                return(MG_ESEEK);
282 >        if (pos->lineno == mg_file->lineno)
283                  return(MG_OK);
284          if (mg_file->fp == stdin)
285                  return(MG_ESEEK);       /* cannot seek on standard input */
286 <        if (fseek(mg_file->fp, 0L, 0) == EOF)
286 >        if (fseek(mg_file->fp, pos->offset, 0) == EOF)
287                  return(MG_ESEEK);
288 <        mg_file->lineno = 0;
288 >        mg_file->lineno = pos->lineno;
289          return(MG_OK);
290   }
291  
# Line 269 | Line 299 | mg_read()                      /* read next line from file */
299                  if (fgets(mg_file->inpline+len,
300                                  MG_MAXLINE-len, mg_file->fp) == NULL)
301                          return(len);
272                mg_file->lineno++;
302                  len += strlen(mg_file->inpline+len);
303 <                if (len > 1 && mg_file->inpline[len-2] == '\\')
304 <                        mg_file->inpline[--len-1] = ' ';
305 <        } while (mg_file->inpline[len]);
303 >                if (len >= MG_MAXLINE-1)
304 >                        return(len);
305 >                mg_file->lineno++;
306 >        } while (len > 1 && mg_file->inpline[len-2] == '\\');
307  
308          return(len);
309   }
# Line 284 | Line 314 | mg_parse()                     /* parse current input line */
314   {
315          char    abuf[MG_MAXLINE];
316          char    *argv[MG_MAXARGC];
317 <        int     en;
318 <        register char   *cp, **ap;
319 <
320 <        strcpy(cp=abuf, mg_file->inpline);
321 <        ap = argv;                      /* break into words */
317 >        register char   *cp, *cp2, **ap;
318 >                                        /* copy line, removing escape chars */
319 >        cp = abuf; cp2 = mg_file->inpline;
320 >        while ((*cp++ = *cp2++))
321 >                if (cp2[0] == '\n' && cp2[-1] == '\\')
322 >                        cp--;
323 >        cp = abuf; ap = argv;           /* break into words */
324          for ( ; ; ) {
325                  while (isspace(*cp))
326                          *cp++ = '\0';
# Line 304 | Line 336 | mg_parse()                     /* parse current input line */
336                  return(MG_OK);          /* no words in line */
337          *ap = NULL;
338                                          /* else handle it */
339 <        return(handle_it(-1, ap-argv, argv));
339 >        return(mg_handle(-1, ap-argv, argv));
340   }
341  
342  
# Line 314 | Line 346 | char   *fn;
346   {
347          MG_FCTXT        cntxt;
348          int     rval;
349 +        register int    nbr;
350  
351          if ((rval = mg_open(&cntxt, fn)) != MG_OK) {
352                  fprintf(stderr, "%s: %s\n", fn, mg_err[rval]);
353                  return(rval);
354          }
355 <        while (mg_read())               /* parse each line */
355 >        while ((nbr = mg_read()) > 0) { /* parse each line */
356 >                if (nbr >= MG_MAXLINE-1) {
357 >                        fprintf(stderr, "%s: %d: %s\n", cntxt.fname,
358 >                                        cntxt.lineno, mg_err[rval=MG_ELINE]);
359 >                        break;
360 >                }
361                  if ((rval = mg_parse()) != MG_OK) {
362                          fprintf(stderr, "%s: %d: %s:\n%s", cntxt.fname,
363                                          cntxt.lineno, mg_err[rval],
364                                          cntxt.inpline);
365                          break;
366                  }
367 +        }
368          mg_close();
369          return(rval);
370   }
371  
372  
373 < void
374 < mg_clear()                      /* clear parser history */
373 > int
374 > mg_defuhand(ac, av)             /* default handler for unknown entities */
375 > int     ac;
376 > char    **av;
377   {
378 <        c_clearall();                   /* clear context tables */
379 <        mg_file = NULL;                 /* reset our context */
378 >        if (mg_nunknown++ == 0)         /* report first incident */
379 >                fprintf(stderr, "%s: %d: %s: %s\n", mg_file->fname,
380 >                                mg_file->lineno, mg_err[MG_EUNK], av[0]);
381 >        return(MG_OK);
382   }
383  
384  
385 < int
386 < mg_iterate(ac, av, f)           /* iterate on statement */
344 < int     ac;
345 < register char   **av;
346 < int     (*f)();
385 > void
386 > mg_clear()                      /* clear parser history */
387   {
388 <        int     niter, rval;
389 <        register int    i, j;
390 <        char    *argv[MG_MAXARGC];
351 <        char    cntbuf[10];
352 <                                        /* build partial transformation */
353 <        for (i = 0; i < ac; i++) {
354 <                if (av[i][0] == '-' && av[i][1] == 'a' && av[i][2] == '\0')
355 <                        break;
356 <                argv[i+1] = av[i];
357 <        }
358 <        argv[i+1] = NULL;
359 <        if (i) {                        /* handle transformation */
360 <                argv[0] = mg_ename[MG_E_XF];
361 <                if ((rval = handle_it(MG_E_XF, i+1, argv)) != MG_OK)
362 <                        return(rval);
363 <        }
364 <        if (i < ac) {                   /* run array */
365 <                if (i+1 >= ac || !isint(av[i+1]))
366 <                        return(MG_ETYPE);
367 <                niter = atoi(av[i+1]);
368 <                argv[0] = mg_ename[MG_E_OBJECT];
369 <                argv[1] = cntbuf;
370 <                for (j = 2; j+i < ac; j++)
371 <                        argv[j] = av[j+i];
372 <                argv[j] = NULL;
373 <                for (j = 0; j < niter; j++) {
374 <                        sprintf(cntbuf, "%d", j);
375 <                        if ((rval = handle_it(MG_E_OBJECT, 2, argv)) != MG_OK)
376 <                                return(rval);
377 <                        argv[0] = "-i";
378 <                        if ((rval = mg_iterate(ac-i, argv, f)) != MG_OK)
379 <                                return(rval);
380 <                        argv[0] = mg_ename[MG_E_OBJECT];
381 <                        if ((rval = handle_it(MG_E_OBJECT, 1, argv)) != MG_OK)
382 <                                return(rval);
383 <                }
384 <        } else if ((rval = (*f)()) != MG_OK)    /* else do this instance */
385 <                        return(rval);
386 <        if (i) {                        /* reset the transform */
387 <                argv[0] = mg_ename[MG_E_XF];
388 <                argv[1] = NULL;
389 <                (void)handle_it(MG_E_XF, 1, argv);
390 <        }
391 <        return(MG_OK);
388 >        c_clearall();                   /* clear context tables */
389 >        while (mg_file != NULL)         /* reset our file context */
390 >                mg_close();
391   }
392  
393  
# Line 406 | Line 405 | char   **av;
405   }
406  
407  
408 < static int
410 < reload_file()                   /* reload current MGF file */
411 < {
412 <        register int    rval;
413 <
414 <        if ((rval = mg_rewind()) != MG_OK)
415 <                return(rval);
416 <        while (mg_read())
417 <                if ((rval = mg_parse()) != MG_OK)
418 <                        return(rval);
419 <        return(MG_OK);
420 < }
421 <
422 <
423 < static int
408 > int
409   e_include(ac, av)               /* include file */
410   int     ac;
411   char    **av;
412   {
413 +        char    *xfarg[MG_MAXARGC];
414          MG_FCTXT        ictx;
415 <        int     rv;
415 >        XF_SPEC *xf_orig = xf_context;
416 >        register int    rv;
417  
418          if (ac < 2)
419                  return(MG_EARGC);
420          if ((rv = mg_open(&ictx, av[1])) != MG_OK)
421                  return(rv);
422 <        if ((rv = mg_iterate(ac-2, av+2, reload_file)) != MG_OK) {
423 <                fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname,
424 <                                ictx.lineno, mg_err[rv], ictx.inpline);
425 <                mg_close();
426 <                return(MG_EINCL);
422 >        if (ac > 2) {
423 >                register int    i;
424 >
425 >                xfarg[0] = mg_ename[MG_E_XF];
426 >                for (i = 1; i < ac-1; i++)
427 >                        xfarg[i] = av[i+1];
428 >                xfarg[ac-1] = NULL;
429 >                if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK) {
430 >                        mg_close();
431 >                        return(rv);
432 >                }
433          }
434 +        do {
435 +                while ((rv = mg_read()) > 0) {
436 +                        if (rv >= MG_MAXLINE-1) {
437 +                                fprintf(stderr, "%s: %d: %s\n", ictx.fname,
438 +                                                ictx.lineno, mg_err[MG_ELINE]);
439 +                                mg_close();
440 +                                return(MG_EINCL);
441 +                        }
442 +                        if ((rv = mg_parse()) != MG_OK) {
443 +                                fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname,
444 +                                                ictx.lineno, mg_err[rv],
445 +                                                ictx.inpline);
446 +                                mg_close();
447 +                                return(MG_EINCL);
448 +                        }
449 +                }
450 +                if (ac > 2)
451 +                        if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK) {
452 +                                mg_close();
453 +                                return(rv);
454 +                        }
455 +        } while (xf_context != xf_orig);
456          mg_close();
457          return(MG_OK);
458   }
459  
460  
461 + int
462 + e_faceh(ac, av)                 /* replace face+holes with single contour */
463 + int     ac;
464 + char    **av;
465 + {
466 +        char    *newav[MG_MAXARGC];
467 +        int     lastp = 0;
468 +        register int    i, j;
469 +
470 +        newav[0] = mg_ename[MG_E_FACE];
471 +        for (i = 1; i < ac; i++)
472 +                if (av[i][0] == '-') {
473 +                        if (i < 4)
474 +                                return(MG_EARGC);
475 +                        if (i >= ac-1)
476 +                                break;
477 +                        if (!lastp)
478 +                                lastp = i-1;
479 +                        for (j = i+1; j < ac-1 && av[j+1][0] != '-'; j++)
480 +                                ;
481 +                        if (j - i < 3)
482 +                                return(MG_EARGC);
483 +                        newav[i] = av[j];       /* connect hole loop */
484 +                } else
485 +                        newav[i] = av[i];       /* hole or perimeter vertex */
486 +        if (lastp)
487 +                newav[i++] = av[lastp];         /* finish seam to outside */
488 +        newav[i] = NULL;
489 +        return(mg_handle(MG_E_FACE, i, newav));
490 + }
491 +
492 +
493   static void
494   make_axes(u, v, w)              /* compute u and v given w (normalized) */
495   FVECT   u, v, w;
# Line 460 | Line 507 | FVECT  u, v, w;
507   }
508  
509  
510 < static int
510 > int
511   e_sph(ac, av)                   /* expand a sphere into cones */
512   int     ac;
513   char    **av;
# Line 485 | Line 532 | char   **av;
532          rad = atof(av[2]);
533                                          /* initialize */
534          warpconends = 1;
535 <        if ((rval = handle_it(MG_E_VERTEX, 3, v2ent)) != MG_OK)
535 >        if ((rval = mg_handle(MG_E_VERTEX, 3, v2ent)) != MG_OK)
536                  return(rval);
537          sprintf(p2x, FLTFMT, cv->p[0]);
538          sprintf(p2y, FLTFMT, cv->p[1]);
539          sprintf(p2z, FLTFMT, cv->p[2]+rad);
540 <        if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
540 >        if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
541                  return(rval);
542          r2[0] = '0'; r2[1] = '\0';
543          for (i = 1; i <= 2*mg_nqcdivs; i++) {
544                  theta = i*(PI/2)/mg_nqcdivs;
545 <                if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
545 >                if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
546                          return(rval);
547                  sprintf(p2z, FLTFMT, cv->p[2]+rad*cos(theta));
548 <                if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK)
548 >                if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
549                          return(rval);
550 <                if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
550 >                if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
551                          return(rval);
552                  strcpy(r1, r2);
553                  sprintf(r2, FLTFMT, rad*sin(theta));
554 <                if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK)
554 >                if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
555                          return(rval);
556          }
557          warpconends = 0;
# Line 512 | Line 559 | char   **av;
559   }
560  
561  
562 < static int
562 > int
563   e_torus(ac, av)                 /* expand a torus into cones */
564   int     ac;
565   char    **av;
# Line 559 | Line 606 | char   **av;
606          for (j = 0; j < 3; j++)
607                  sprintf(p2[j], FLTFMT, cv->p[j] +
608                                  .5*sgn*(maxrad-minrad)*cv->n[j]);
609 <        if ((rval = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
609 >        if ((rval = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
610                  return(rval);
611 <        if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
611 >        if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
612                  return(rval);
613          sprintf(r2, FLTFMT, avgrad=.5*(minrad+maxrad));
614                                          /* run outer section */
615          for (i = 1; i <= 2*mg_nqcdivs; i++) {
616                  theta = i*(PI/2)/mg_nqcdivs;
617 <                if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
617 >                if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
618                          return(rval);
619                  for (j = 0; j < 3; j++)
620                          sprintf(p2[j], FLTFMT, cv->p[j] +
621                                  .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
622 <                if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK)
622 >                if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
623                          return(rval);
624 <                if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
624 >                if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
625                          return(rval);
626                  strcpy(r1, r2);
627                  sprintf(r2, FLTFMT, avgrad + .5*(maxrad-minrad)*sin(theta));
628 <                if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK)
628 >                if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
629                          return(rval);
630          }
631                                          /* run inner section */
# Line 588 | Line 635 | char   **av;
635                  for (j = 0; j < 3; j++)
636                          sprintf(p2[j], FLTFMT, cv->p[j] +
637                                  .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
638 <                if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
638 >                if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
639                          return(rval);
640 <                if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK)
640 >                if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
641                          return(rval);
642 <                if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
642 >                if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
643                          return(rval);
644                  strcpy(r1, r2);
645                  sprintf(r2, FLTFMT, -avgrad - .5*(maxrad-minrad)*sin(theta));
646 <                if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK)
646 >                if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
647                          return(rval);
648          }
649          warpconends = 0;
# Line 604 | Line 651 | char   **av;
651   }
652  
653  
654 < static int
654 > int
655   e_cyl(ac, av)                   /* replace a cylinder with equivalent cone */
656   int     ac;
657   char    **av;
# Line 617 | Line 664 | char   **av;
664          avnew[2] = av[2];
665          avnew[3] = av[3];
666          avnew[4] = av[2];
667 <        return(handle_it(MG_E_CONE, 5, avnew));
667 >        return(mg_handle(MG_E_CONE, 5, avnew));
668   }
669  
670  
671 < static int
671 > int
672   e_ring(ac, av)                  /* turn a ring into polygons */
673   int     ac;
674   char    **av;
# Line 659 | Line 706 | char   **av;
706          make_axes(u, v, cv->n);
707          for (j = 0; j < 3; j++)
708                  sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*u[j]);
709 <        if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK)
709 >        if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
710                  return(rv);
711 <        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
711 >        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
712                  return(rv);
713          if (minrad == 0.) {             /* closed */
714                  v1ent[3] = av[1];
715 <                if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
715 >                if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
716                          return(rv);
717 <                if ((rv = handle_it(MG_E_NORMAL, 4, nzent)) != MG_OK)
717 >                if ((rv = mg_handle(MG_E_NORMAL, 4, nzent)) != MG_OK)
718                          return(rv);
719                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
720                          theta = i*(PI/2)/mg_nqcdivs;
721 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
721 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
722                                  return(rv);
723                          for (j = 0; j < 3; j++)
724                                  sprintf(p3[j], FLTFMT, cv->p[j] +
725                                                  maxrad*u[j]*cos(theta) +
726                                                  maxrad*v[j]*sin(theta));
727 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
727 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
728                                  return(rv);
729 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
729 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
730                                  return(rv);
731 <                        if ((rv = handle_it(MG_E_FACE, 4, fent)) != MG_OK)
731 >                        if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK)
732                                  return(rv);
733                  }
734          } else {                        /* open */
735 <                if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK)
735 >                if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
736                          return(rv);
737                  for (j = 0; j < 3; j++)
738                          sprintf(p4[j], FLTFMT, cv->p[j] + minrad*u[j]);
739 <                if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
739 >                if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
740                          return(rv);
741                  v1ent[3] = "_rv4";
742                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
743                          theta = i*(PI/2)/mg_nqcdivs;
744 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
744 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
745                                  return(rv);
746 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
746 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
747                                  return(rv);
748                          for (j = 0; j < 3; j++) {
749                                  d = u[j]*cos(theta) + v[j]*sin(theta);
750                                  sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*d);
751                                  sprintf(p4[j], FLTFMT, cv->p[j] + minrad*d);
752                          }
753 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
753 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
754                                  return(rv);
755 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
755 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
756                                  return(rv);
757 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v4ent)) != MG_OK)
757 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
758                                  return(rv);
759 <                        if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
759 >                        if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
760                                  return(rv);
761 <                        if ((rv = handle_it(MG_E_FACE, 5, fent)) != MG_OK)
761 >                        if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
762                                  return(rv);
763                  }
764          }
# Line 719 | Line 766 | char   **av;
766   }
767  
768  
769 < static int
769 > int
770   e_cone(ac, av)                  /* turn a cone into polygons */
771   int     ac;
772   char    **av;
# Line 734 | Line 781 | char   **av;
781          static char     *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]};
782          static char     *n4ent[5] = {mg_ename[MG_E_NORMAL],n4[0],n4[1],n4[2]};
783          static char     *fent[6] = {mg_ename[MG_E_FACE],"_cv1","_cv2","_cv3","_cv4"};
784 +        char    *v1n;
785          register C_VERTEX       *cv1, *cv2;
786          register int    i, j;
787          FVECT   u, v, w;
# Line 749 | Line 797 | char   **av;
797          if ((cv1 = c_getvert(av[1])) == NULL ||
798                          (cv2 = c_getvert(av[3])) == NULL)
799                  return(MG_EUNDEF);
800 +        v1n = av[1];
801          if (!isflt(av[2]) || !isflt(av[4]))
802                  return(MG_ETYPE);
803          rad1 = atof(av[2]);
# Line 759 | Line 808 | char   **av;
808                  if (rad2 == 0.)
809                          return(MG_EILL);
810          } else if (rad2 != 0.) {
811 <                if (rad1 < 0. ^ rad2 < 0.)
811 >                if ((rad1 < 0.) ^ (rad2 < 0.))
812                          return(MG_EILL);
813          } else {                        /* swap */
814                  C_VERTEX        *cv;
# Line 767 | Line 816 | char   **av;
816                  cv = cv1;
817                  cv1 = cv2;
818                  cv2 = cv;
819 +                v1n = av[3];
820                  d = rad1;
821                  rad1 = rad2;
822                  rad2 = d;
# Line 793 | Line 843 | char   **av;
843                  else
844                          sprintf(n3[j], FLTFMT, u[j] + w[j]*n2off);
845          }
846 <        if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK)
846 >        if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
847                  return(rv);
848 <        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
848 >        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
849                  return(rv);
850 <        if ((rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
850 >        if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
851                  return(rv);
852          if (rad1 == 0.) {               /* triangles */
853 <                v1ent[3] = av[1];
854 <                if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
853 >                v1ent[3] = v1n;
854 >                if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
855                          return(rv);
856                  for (j = 0; j < 3; j++)
857                          sprintf(n4[j], FLTFMT, w[j]);
858 <                if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
858 >                if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
859                          return(rv);
860                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
861                          theta = sgn*i*(PI/2)/mg_nqcdivs;
862 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
862 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
863                                  return(rv);
864                          for (j = 0; j < 3; j++) {
865                                  d = u[j]*cos(theta) + v[j]*sin(theta);
# Line 817 | Line 867 | char   **av;
867                                  if (n2off > -FHUGE)
868                                          sprintf(n3[j], FLTFMT, d + w[j]*n2off);
869                          }
870 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
870 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
871                                  return(rv);
872 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
872 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
873                                  return(rv);
874                          if (n2off > -FHUGE &&
875 <                        (rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
875 >                        (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
876                                  return(rv);
877 <                        if ((rv = handle_it(MG_E_FACE, 4, fent)) != MG_OK)
877 >                        if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK)
878                                  return(rv);
879                  }
880          } else {                        /* quads */
# Line 843 | Line 893 | char   **av;
893                          else
894                                  sprintf(n4[j], FLTFMT, u[j] + w[j]*n1off);
895                  }
896 <                if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK)
896 >                if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
897                          return(rv);
898 <                if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
898 >                if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
899                          return(rv);
900 <                if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
900 >                if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
901                          return(rv);
902                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
903                          theta = sgn*i*(PI/2)/mg_nqcdivs;
904 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
904 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
905                                  return(rv);
906 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
906 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
907                                  return(rv);
908                          for (j = 0; j < 3; j++) {
909                                  d = u[j]*cos(theta) + v[j]*sin(theta);
# Line 864 | Line 914 | char   **av;
914                                  if (n1off < FHUGE)
915                                          sprintf(n4[j], FLTFMT, d + w[j]*n1off);
916                          }
917 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
917 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
918                                  return(rv);
919 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
919 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
920                                  return(rv);
921                          if (n2off > -FHUGE &&
922 <                        (rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
922 >                        (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
923                                  return(rv);
924 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v4ent)) != MG_OK)
924 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
925                                  return(rv);
926 <                        if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
926 >                        if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
927                                  return(rv);
928                          if (n1off < FHUGE &&
929 <                        (rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
929 >                        (rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
930                                  return(rv);
931 <                        if ((rv = handle_it(MG_E_FACE, 5, fent)) != MG_OK)
931 >                        if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
932                                  return(rv);
933                  }
934          }
# Line 886 | Line 936 | char   **av;
936   }
937  
938  
939 < static int
939 > int
940   e_prism(ac, av)                 /* turn a prism into polygons */
941   int     ac;
942   char    **av;
943   {
944          static char     p[3][24];
945 <        static char     *vent[4] = {mg_ename[MG_E_VERTEX],NULL,"="};
945 >        static char     *vent[5] = {mg_ename[MG_E_VERTEX],NULL,"="};
946          static char     *pent[5] = {mg_ename[MG_E_POINT],p[0],p[1],p[2]};
947 +        static char     *znorm[5] = {mg_ename[MG_E_NORMAL],"0","0","0"};
948          char    *newav[MG_MAXARGC], nvn[MG_MAXARGC-1][8];
949          double  length;
950 +        int     hasnorm;
951          FVECT   v1, v2, v3, norm;
952          register C_VERTEX       *cv;
953          C_VERTEX        *cv0;
954          int     rv;
955          register int    i, j;
956 <
956 >                                                /* check arguments */
957          if (ac < 5)
958                  return(MG_EARGC);
959          if (!isflt(av[ac-1]))
# Line 909 | Line 961 | char   **av;
961          length = atof(av[ac-1]);
962          if (length <= FTINY && length >= -FTINY)
963                  return(MG_EILL);
964 <                                        /* do bottom face */
965 <        newav[0] = mg_ename[MG_E_FACE];
914 <        for (i = 1; i < ac-1; i++)
915 <                newav[i] = av[i];
916 <        newav[i] = NULL;
917 <        if ((rv = handle_it(MG_E_FACE, i, newav)) != MG_OK)
918 <                return(rv);
919 <                                        /* compute face normal */
920 <        if ((cv0 = c_getvert(av[2])) == NULL)
964 >                                                /* compute face normal */
965 >        if ((cv0 = c_getvert(av[1])) == NULL)
966                  return(MG_EUNDEF);
967 +        hasnorm = 0;
968          norm[0] = norm[1] = norm[2] = 0.;
969          v1[0] = v1[1] = v1[2] = 0.;
970          for (i = 2; i < ac-1; i++) {
971                  if ((cv = c_getvert(av[i])) == NULL)
972                          return(MG_EUNDEF);
973 +                hasnorm += !is0vect(cv->n);
974                  v2[0] = cv->p[0] - cv0->p[0];
975                  v2[1] = cv->p[1] - cv0->p[1];
976                  v2[2] = cv->p[2] - cv0->p[2];
# Line 935 | Line 982 | char   **av;
982          }
983          if (normalize(norm) == 0.)
984                  return(MG_EILL);
985 <                                        /* create moved vertices */
985 >                                                /* create moved vertices */
986          for (i = 1; i < ac-1; i++) {
987                  sprintf(nvn[i-1], "_pv%d", i);
988                  vent[1] = nvn[i-1];
989 <                if ((rv = handle_it(MG_E_VERTEX, 3, vent)) != MG_OK)
989 >                vent[3] = av[i];
990 >                if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK)
991                          return(rv);
992                  cv = c_getvert(av[i]);          /* checked above */
993                  for (j = 0; j < 3; j++)
994                          sprintf(p[j], FLTFMT, cv->p[j] - length*norm[j]);
995 <                if ((rv = handle_it(MG_E_POINT, 4, pent)) != MG_OK)
995 >                if ((rv = mg_handle(MG_E_POINT, 4, pent)) != MG_OK)
996                          return(rv);
949                newav[ac-1-i] = nvn[i-1];       /* reverse */
997          }
998 <                                                /* do top face */
999 <        if ((rv = handle_it(MG_E_FACE, ac-1, newav)) != MG_OK)
953 <                return(rv);
998 >                                                /* make faces */
999 >        newav[0] = mg_ename[MG_E_FACE];
1000                                                  /* do the side faces */
1001          newav[5] = NULL;
1002          newav[3] = av[ac-2];
# Line 958 | Line 1004 | char   **av;
1004          for (i = 1; i < ac-1; i++) {
1005                  newav[1] = nvn[i-1];
1006                  newav[2] = av[i];
1007 <                if ((rv = handle_it(MG_E_FACE, 5, newav)) != MG_OK)
1007 >                if ((rv = mg_handle(MG_E_FACE, 5, newav)) != MG_OK)
1008                          return(rv);
1009                  newav[3] = newav[2];
1010                  newav[4] = newav[1];
1011          }
1012 +                                                /* do top face */
1013 +        for (i = 1; i < ac-1; i++) {
1014 +                if (hasnorm) {                  /* zero normals */
1015 +                        vent[1] = nvn[i-1];
1016 +                        if ((rv = mg_handle(MG_E_VERTEX, 2, vent)) != MG_OK)
1017 +                                return(rv);
1018 +                        if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK)
1019 +                                return(rv);
1020 +                }
1021 +                newav[ac-1-i] = nvn[i-1];       /* reverse */
1022 +        }
1023 +        if ((rv = mg_handle(MG_E_FACE, ac-1, newav)) != MG_OK)
1024 +                return(rv);
1025 +                                                /* do bottom face */
1026 +        if (hasnorm)
1027 +                for (i = 1; i < ac-1; i++) {
1028 +                        vent[1] = nvn[i-1];
1029 +                        vent[3] = av[i];
1030 +                        if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK)
1031 +                                return(rv);
1032 +                        if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK)
1033 +                                return(rv);
1034 +                        newav[i] = nvn[i-1];
1035 +                }
1036 +        else
1037 +                for (i = 1; i < ac-1; i++)
1038 +                        newav[i] = av[i];
1039 +        newav[i] = NULL;
1040 +        if ((rv = mg_handle(MG_E_FACE, i, newav)) != MG_OK)
1041 +                return(rv);
1042 +        return(MG_OK);
1043 + }
1044 +
1045 +
1046 + static int
1047 + put_cxy()                       /* put out current xy chromaticities */
1048 + {
1049 +        static char     xbuf[24], ybuf[24];
1050 +        static char     *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf};
1051 +
1052 +        sprintf(xbuf, "%.4f", c_ccolor->cx);
1053 +        sprintf(ybuf, "%.4f", c_ccolor->cy);
1054 +        return(mg_handle(MG_E_CXY, 3, ccom));
1055 + }
1056 +
1057 +
1058 + static int
1059 + put_cspec()                     /* put out current color spectrum */
1060 + {
1061 +        char    wl[2][6], vbuf[C_CNSS][24];
1062 +        char    *newav[C_CNSS+4];
1063 +        double  sf;
1064 +        register int    i;
1065 +
1066 +        if (mg_ehand[MG_E_CSPEC] != c_hcolor) {
1067 +                sprintf(wl[0], "%d", C_CMINWL);
1068 +                sprintf(wl[1], "%d", C_CMAXWL);
1069 +                newav[0] = mg_ename[MG_E_CSPEC];
1070 +                newav[1] = wl[0];
1071 +                newav[2] = wl[1];
1072 +                sf = (double)C_CNSS / c_ccolor->ssum;
1073 +                for (i = 0; i < C_CNSS; i++) {
1074 +                        sprintf(vbuf[i], "%.4f", sf*c_ccolor->ssamp[i]);
1075 +                        newav[i+3] = vbuf[i];
1076 +                }
1077 +                newav[C_CNSS+3] = NULL;
1078 +                if ((i = mg_handle(MG_E_CSPEC, C_CNSS+3, newav)) != MG_OK)
1079 +                        return(i);
1080 +        }
1081 +        return(MG_OK);
1082 + }
1083 +
1084 +
1085 + static int
1086 + e_cspec(ac, av)                 /* handle spectral color */
1087 + int     ac;
1088 + char    **av;
1089 + {
1090 +                                /* convert to xy chromaticity */
1091 +        c_ccvt(c_ccolor, C_CSXY);
1092 +                                /* if it's really their handler, use it */
1093 +        if (mg_ehand[MG_E_CXY] != c_hcolor)
1094 +                return(put_cxy());
1095 +        return(MG_OK);
1096 + }
1097 +
1098 +
1099 + static int
1100 + e_cmix(ac, av)                  /* handle mixing of colors */
1101 + int     ac;
1102 + char    **av;
1103 + {
1104 +        /*
1105 +         * Contorted logic works as follows:
1106 +         *      1. the colors are already mixed in c_hcolor() support function
1107 +         *      2. if we would handle a spectral result, make sure it's not
1108 +         *      3. if c_hcolor() would handle a spectral result, don't bother
1109 +         *      4. otherwise, make cspec entity and pass it to their handler
1110 +         *      5. if we have only xy results, handle it as c_spec() would
1111 +         */
1112 +        if (mg_ehand[MG_E_CSPEC] == e_cspec)
1113 +                c_ccvt(c_ccolor, C_CSXY);
1114 +        else if (c_ccolor->flags & C_CDSPEC)
1115 +                return(put_cspec());
1116 +        if (mg_ehand[MG_E_CXY] != c_hcolor)
1117 +                return(put_cxy());
1118 +        return(MG_OK);
1119 + }
1120 +
1121 +
1122 + static int
1123 + e_cct(ac, av)                   /* handle color temperature */
1124 + int     ac;
1125 + char    **av;
1126 + {
1127 +        /*
1128 +         * Logic is similar to e_cmix here.  Support handler has already
1129 +         * converted temperature to spectral color.  Put it out as such
1130 +         * if they support it, otherwise convert to xy chromaticity and
1131 +         * put it out if they handle it.
1132 +         */
1133 +        if (mg_ehand[MG_E_CSPEC] != e_cspec)
1134 +                return(put_cspec());
1135 +        c_ccvt(c_ccolor, C_CSXY);
1136 +        if (mg_ehand[MG_E_CXY] != c_hcolor)
1137 +                return(put_cxy());
1138          return(MG_OK);
1139   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines