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.10 by greg, Wed Jun 29 16:15:20 1994 UTC vs.
Revision 1.28 by schorsch, Sat Nov 15 17:54:06 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1994 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Parse an MGF file, converting or discarding unsupported entities
6   */
7  
8   #include <stdio.h>
9 + #include <stdlib.h>
10   #include <math.h>
11   #include <ctype.h>
12   #include <string.h>
# Line 27 | Line 25 | char   mg_ename[MG_NENTITIES][MG_MAXELEN] = MG_NAMELIST;
25  
26   int     (*mg_ehand[MG_NENTITIES])();
27  
28 +                        /* Handler routine for unknown entities */
29 +
30 + int     (*mg_uhand)() = mg_defuhand;
31 +
32 + unsigned        mg_nunknown;    /* count of unknown entities */
33 +
34                          /* error messages */
35  
36   char    *mg_err[MG_NERRS] = MG_ERRLIST;
# Line 55 | Line 59 | int    mg_nqcdivs = MG_NQCD;   /* number of divisions per q
59   #define e_ies e_any_toss
60                                  /* alternate handler routines */
61  
62 < static int      e_any_toss(),           /* discard unneeded entity */
63 <                e_ies(),                /* IES luminaire file */
64 <                e_include(),            /* include file */
61 <                e_sph(),                /* sphere */
62 <                e_cmix(),               /* color mixtures */
63 <                e_cspec(),              /* color spectra */
64 <                e_cyl(),                /* cylinder */
65 <                e_cone(),               /* cone */
66 <                e_prism(),              /* prism */
67 <                e_ring(),               /* ring */
68 <                e_torus();              /* torus */
62 > static void make_axes(FVECT u, FVECT v, FVECT w);
63 > static int put_cxy(void);
64 > static int put_cspec(void);
65  
66 + static int e_any_toss(int ac, char **av); /* discard an unwanted entity */
67 + static int e_cspec(int ac, char **av); /* handle spectral color */
68 + static int e_cmix(int ac, char **av); /* handle mixing of colors */
69 + static int e_cct(int ac, char **av); /* handle color temperature */
70 +
71 +
72                                  /* alternate handler support functions */
73  
74 < static int      (*e_supp[MG_NENTITIES])();
74 > static int      (*e_supp[MG_NENTITIES])(int ac, char **av);
75  
76   static char     FLTFMT[] = "%.12g";
77  
# Line 77 | Line 79 | static int     warpconends;            /* hack for generating good n
79  
80  
81   void
82 < mg_init()                       /* initialize alternate entity handlers */
82 > mg_init(void)                   /* initialize alternate entity handlers */
83   {
84          unsigned long   ineed = 0, uneed = 0;
85          register int    i;
# Line 88 | Line 90 | mg_init()                      /* initialize alternate entity handlers */
90                  mg_ehand[MG_E_INCLUDE] = e_include;
91          if (mg_ehand[MG_E_SPH] == NULL) {
92                  mg_ehand[MG_E_SPH] = e_sph;
93 <                ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
93 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
94          } else
95 <                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
95 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
96          if (mg_ehand[MG_E_CYL] == NULL) {
97                  mg_ehand[MG_E_CYL] = e_cyl;
98 <                ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
98 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
99          } else
100 <                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
100 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
101          if (mg_ehand[MG_E_CONE] == NULL) {
102                  mg_ehand[MG_E_CONE] = e_cone;
103 <                ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
103 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
104          } else
105 <                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
105 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
106          if (mg_ehand[MG_E_RING] == NULL) {
107                  mg_ehand[MG_E_RING] = e_ring;
108 <                ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX;
108 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX;
109          } else
110 <                uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF;
110 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX|1L<<MG_E_XF;
111          if (mg_ehand[MG_E_PRISM] == NULL) {
112                  mg_ehand[MG_E_PRISM] = e_prism;
113 <                ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
113 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
114          } else
115 <                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
115 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
116          if (mg_ehand[MG_E_TORUS] == NULL) {
117                  mg_ehand[MG_E_TORUS] = e_torus;
118 <                ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX;
118 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX;
119          } else
120 <                uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF;
120 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX|1L<<MG_E_XF;
121 >        if (mg_ehand[MG_E_FACE] == NULL)
122 >                mg_ehand[MG_E_FACE] = mg_ehand[MG_E_FACEH];
123 >        else if (mg_ehand[MG_E_FACEH] == NULL)
124 >                mg_ehand[MG_E_FACEH] = e_faceh;
125          if (mg_ehand[MG_E_COLOR] != NULL) {
126                  if (mg_ehand[MG_E_CMIX] == NULL) {
127                          mg_ehand[MG_E_CMIX] = e_cmix;
128 <                        ineed |= 1<<MG_E_COLOR|1<<MG_E_CXY|1<<MG_E_CSPEC|1<<MG_E_CMIX;
128 >                        ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT;
129                  }
130                  if (mg_ehand[MG_E_CSPEC] == NULL) {
131                          mg_ehand[MG_E_CSPEC] = e_cspec;
132 <                        ineed |= 1<<MG_E_COLOR|1<<MG_E_CXY|1<<MG_E_CSPEC|1<<MG_E_CMIX;
132 >                        ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT;
133                  }
134 +                if (mg_ehand[MG_E_CCT] == NULL) {
135 +                        mg_ehand[MG_E_CCT] = e_cct;
136 +                        ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT;
137 +                }
138          }
139                                          /* check for consistency */
140          if (mg_ehand[MG_E_FACE] != NULL)
141 <                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
141 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
142          if (mg_ehand[MG_E_CXY] != NULL || mg_ehand[MG_E_CSPEC] != NULL ||
143                          mg_ehand[MG_E_CMIX] != NULL)
144 <                uneed |= 1<<MG_E_COLOR;
144 >                uneed |= 1L<<MG_E_COLOR;
145          if (mg_ehand[MG_E_RD] != NULL || mg_ehand[MG_E_TD] != NULL ||
146 +                        mg_ehand[MG_E_IR] != NULL ||
147                          mg_ehand[MG_E_ED] != NULL ||
148                          mg_ehand[MG_E_RS] != NULL ||
149 <                        mg_ehand[MG_E_TS] != NULL)
150 <                uneed |= 1<<MG_E_MATERIAL;
149 >                        mg_ehand[MG_E_TS] != NULL ||
150 >                        mg_ehand[MG_E_SIDES] != NULL)
151 >                uneed |= 1L<<MG_E_MATERIAL;
152          for (i = 0; i < MG_NENTITIES; i++)
153 <                if (uneed & 1<<i && mg_ehand[i] == NULL) {
153 >                if (uneed & 1L<<i && mg_ehand[i] == NULL) {
154                          fprintf(stderr, "Missing support for \"%s\" entity\n",
155                                          mg_ename[i]);
156                          exit(1);
157                  }
158                                          /* add support as needed */
159 <        if (ineed & 1<<MG_E_VERTEX && mg_ehand[MG_E_VERTEX] != c_hvertex)
159 >        if (ineed & 1L<<MG_E_VERTEX && mg_ehand[MG_E_VERTEX] != c_hvertex)
160                  e_supp[MG_E_VERTEX] = c_hvertex;
161 <        if (ineed & 1<<MG_E_POINT && mg_ehand[MG_E_POINT] != c_hvertex)
161 >        if (ineed & 1L<<MG_E_POINT && mg_ehand[MG_E_POINT] != c_hvertex)
162                  e_supp[MG_E_POINT] = c_hvertex;
163 <        if (ineed & 1<<MG_E_NORMAL && mg_ehand[MG_E_NORMAL] != c_hvertex)
163 >        if (ineed & 1L<<MG_E_NORMAL && mg_ehand[MG_E_NORMAL] != c_hvertex)
164                  e_supp[MG_E_NORMAL] = c_hvertex;
165 <        if (ineed & 1<<MG_E_COLOR && mg_ehand[MG_E_COLOR] != c_hcolor)
165 >        if (ineed & 1L<<MG_E_COLOR && mg_ehand[MG_E_COLOR] != c_hcolor)
166                  e_supp[MG_E_COLOR] = c_hcolor;
167 <        if (ineed & 1<<MG_E_CXY && mg_ehand[MG_E_CXY] != c_hcolor)
167 >        if (ineed & 1L<<MG_E_CXY && mg_ehand[MG_E_CXY] != c_hcolor)
168                  e_supp[MG_E_CXY] = c_hcolor;
169 <        if (ineed & 1<<MG_E_CSPEC && mg_ehand[MG_E_CSPEC] != c_hcolor)
169 >        if (ineed & 1L<<MG_E_CSPEC && mg_ehand[MG_E_CSPEC] != c_hcolor)
170                  e_supp[MG_E_CSPEC] = c_hcolor;
171 <        if (ineed & 1<<MG_E_CMIX && mg_ehand[MG_E_CMIX] != c_hcolor)
171 >        if (ineed & 1L<<MG_E_CMIX && mg_ehand[MG_E_CMIX] != c_hcolor)
172                  e_supp[MG_E_CMIX] = c_hcolor;
173 +        if (ineed & 1L<<MG_E_CCT && mg_ehand[MG_E_CCT] != c_hcolor)
174 +                e_supp[MG_E_CCT] = c_hcolor;
175                                          /* discard remaining entities */
176          for (i = 0; i < MG_NENTITIES; i++)
177                  if (mg_ehand[i] == NULL)
# Line 165 | Line 179 | mg_init()                      /* initialize alternate entity handlers */
179   }
180  
181  
168
182   int
183 < mg_entity(name)                 /* get entity number from its name */
184 < char    *name;
183 > mg_entity(                      /* get entity number from its name */
184 >        char    *name
185 > )
186   {
187          static LUTAB    ent_tab = LU_SINIT(NULL,NULL);  /* lookup table */
188          register char   *cp;
# Line 188 | Line 202 | char   *name;
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;
205 > mg_handle(              /* pass entity to appropriate handler */
206 >        register int    en,
207 >        int     ac,
208 >        char    **av
209 > )
210   {
211          int     rv;
212  
213 <        if (en < 0 && (en = mg_entity(av[0])) < 0)
213 >        if (en < 0 && (en = mg_entity(av[0])) < 0) {    /* unknown entity */
214 >                if (mg_uhand != NULL)
215 >                        return((*mg_uhand)(ac, av));
216                  return(MG_EUNK);
217 <        if (e_supp[en] != NULL) {
217 >        }
218 >        if (e_supp[en] != NULL) {                       /* support handler */
219                  if ((rv = (*e_supp[en])(ac, av)) != MG_OK)
220                          return(rv);
221          }
222 <        return((*mg_ehand[en])(ac, av));
222 >        return((*mg_ehand[en])(ac, av));                /* assigned handler */
223   }
224  
225  
226   int
227 < mg_open(ctx, fn)                        /* open new input file */
228 < register MG_FCTXT       *ctx;
229 < char    *fn;
227 > mg_open(                        /* open new input file */
228 >        register MG_FCTXT       *ctx,
229 >        char    *fn
230 > )
231   {
232          static int      nfids;
214        int     olen;
233          register char   *cp;
234  
235          ctx->fid = ++nfids;
# Line 224 | Line 242 | char   *fn;
242                  return(MG_OK);
243          }
244                                          /* get name relative to this context */
245 <        if (mg_file != NULL &&
246 <                        (cp = strrchr(mg_file->fname, '/')) != NULL)
229 <                olen = cp - mg_file->fname + 1;
230 <        else
231 <                olen = 0;
232 <        if (olen)
245 >        if (fn[0] != '/' && mg_file != NULL &&
246 >                        (cp = strrchr(mg_file->fname, '/')) != NULL) {
247                  strcpy(ctx->fname, mg_file->fname);
248 <        strcpy(ctx->fname+olen, fn);
248 >                strcpy(ctx->fname+(cp-mg_file->fname+1), fn);
249 >        } else
250 >                strcpy(ctx->fname, fn);
251          ctx->fp = fopen(ctx->fname, "r");
252          if (ctx->fp == NULL)
253                  return(MG_ENOFILE);
# Line 242 | Line 258 | char   *fn;
258  
259  
260   void
261 < mg_close()                      /* close input file */
261 > mg_close(void)                  /* close input file */
262   {
263          register MG_FCTXT       *ctx = mg_file;
264  
265          mg_file = ctx->prev;            /* restore enclosing context */
266 <        if (ctx->fp == stdin)
267 <                return;                 /* don't close standard input */
252 <        fclose(ctx->fp);
266 >        if (ctx->fp != stdin)           /* close file if it's a file */
267 >                fclose(ctx->fp);
268   }
269  
270  
271   void
272 < mg_fgetpos(pos)                 /* get current position in input file */
273 < register MG_FPOS        *pos;
272 > mg_fgetpos(                     /* get current position in input file */
273 >        register MG_FPOS        *pos
274 > )
275   {
260        extern long     ftell();
261
276          pos->fid = mg_file->fid;
277          pos->lineno = mg_file->lineno;
278          pos->offset = ftell(mg_file->fp);
# Line 266 | Line 280 | register MG_FPOS       *pos;
280  
281  
282   int
283 < mg_fgoto(pos)                   /* reposition input file pointer */
284 < register MG_FPOS        *pos;
283 > mg_fgoto(                       /* reposition input file pointer */
284 >        register MG_FPOS        *pos
285 > )
286   {
287          if (pos->fid != mg_file->fid)
288                  return(MG_ESEEK);
# Line 283 | Line 298 | register MG_FPOS       *pos;
298  
299  
300   int
301 < mg_read()                       /* read next line from file */
301 > mg_read(void)                   /* read next line from file */
302   {
303          register int    len = 0;
304  
# Line 291 | Line 306 | mg_read()                      /* read next line from file */
306                  if (fgets(mg_file->inpline+len,
307                                  MG_MAXLINE-len, mg_file->fp) == NULL)
308                          return(len);
294                mg_file->lineno++;
309                  len += strlen(mg_file->inpline+len);
310 <                if (len > 1 && mg_file->inpline[len-2] == '\\')
311 <                        mg_file->inpline[--len-1] = ' ';
312 <        } while (mg_file->inpline[len]);
310 >                if (len >= MG_MAXLINE-1)
311 >                        return(len);
312 >                mg_file->lineno++;
313 >        } while (len > 1 && mg_file->inpline[len-2] == '\\');
314  
315          return(len);
316   }
317  
318  
319   int
320 < mg_parse()                      /* parse current input line */
320 > mg_parse(void)                  /* parse current input line */
321   {
322          char    abuf[MG_MAXLINE];
323          char    *argv[MG_MAXARGC];
324 <        int     en;
325 <        register char   *cp, **ap;
326 <
327 <        strcpy(cp=abuf, mg_file->inpline);
328 <        ap = argv;                      /* break into words */
324 >        register char   *cp, *cp2, **ap;
325 >                                        /* copy line, removing escape chars */
326 >        cp = abuf; cp2 = mg_file->inpline;
327 >        while ((*cp++ = *cp2++))
328 >                if (cp2[0] == '\n' && cp2[-1] == '\\')
329 >                        cp--;
330 >        cp = abuf; ap = argv;           /* break into words */
331          for ( ; ; ) {
332                  while (isspace(*cp))
333                          *cp++ = '\0';
# Line 331 | Line 348 | mg_parse()                     /* parse current input line */
348  
349  
350   int
351 < mg_load(fn)                     /* load an MGF file */
352 < char    *fn;
351 > mg_load(                        /* load an MGF file */
352 >        char    *fn
353 > )
354   {
355          MG_FCTXT        cntxt;
356          int     rval;
357 +        register int    nbr;
358  
359          if ((rval = mg_open(&cntxt, fn)) != MG_OK) {
360                  fprintf(stderr, "%s: %s\n", fn, mg_err[rval]);
361                  return(rval);
362          }
363 <        while (mg_read())               /* parse each line */
363 >        while ((nbr = mg_read()) > 0) { /* parse each line */
364 >                if (nbr >= MG_MAXLINE-1) {
365 >                        fprintf(stderr, "%s: %d: %s\n", cntxt.fname,
366 >                                        cntxt.lineno, mg_err[rval=MG_ELINE]);
367 >                        break;
368 >                }
369                  if ((rval = mg_parse()) != MG_OK) {
370                          fprintf(stderr, "%s: %d: %s:\n%s", cntxt.fname,
371                                          cntxt.lineno, mg_err[rval],
372                                          cntxt.inpline);
373                          break;
374                  }
375 +        }
376          mg_close();
377          return(rval);
378   }
379  
380  
381 + int
382 + mg_defuhand(            /* default handler for unknown entities */
383 +        int     ac,
384 +        char    **av
385 + )
386 + {
387 +        if (mg_nunknown++ == 0)         /* report first incident */
388 +                fprintf(stderr, "%s: %d: %s: %s\n", mg_file->fname,
389 +                                mg_file->lineno, mg_err[MG_EUNK], av[0]);
390 +        return(MG_OK);
391 + }
392 +
393 +
394   void
395 < mg_clear()                      /* clear parser history */
395 > mg_clear(void)                  /* clear parser history */
396   {
397          c_clearall();                   /* clear context tables */
398 <        mg_file = NULL;                 /* reset our context */
398 >        while (mg_file != NULL)         /* reset our file context */
399 >                mg_close();
400   }
401  
402  
# Line 367 | Line 406 | mg_clear()                     /* clear parser history */
406  
407  
408   static int
409 < e_any_toss(ac, av)              /* discard an unwanted entity */
410 < int     ac;
411 < char    **av;
409 > e_any_toss(             /* discard an unwanted entity */
410 >        int     ac,
411 >        char    **av
412 > )
413   {
414          return(MG_OK);
415   }
416  
417  
418 < static int
419 < e_include(ac, av)               /* include file */
420 < int     ac;
421 < char    **av;
418 > int
419 > e_include(              /* include file */
420 >        int     ac,
421 >        char    **av
422 > )
423   {
424          char    *xfarg[MG_MAXARGC];
425          MG_FCTXT        ictx;
426 <        int     rv;
426 >        XF_SPEC *xf_orig = xf_context;
427 >        register int    rv;
428  
429          if (ac < 2)
430                  return(MG_EARGC);
# Line 395 | Line 437 | char   **av;
437                  for (i = 1; i < ac-1; i++)
438                          xfarg[i] = av[i+1];
439                  xfarg[ac-1] = NULL;
440 <                if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK)
440 >                if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK) {
441 >                        mg_close();
442                          return(rv);
443 +                }
444          }
445 <        while (!feof(mg_file->fp)) {
446 <                while (mg_read())
445 >        do {
446 >                while ((rv = mg_read()) > 0) {
447 >                        if (rv >= MG_MAXLINE-1) {
448 >                                fprintf(stderr, "%s: %d: %s\n", ictx.fname,
449 >                                                ictx.lineno, mg_err[MG_ELINE]);
450 >                                mg_close();
451 >                                return(MG_EINCL);
452 >                        }
453                          if ((rv = mg_parse()) != MG_OK) {
454                                  fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname,
455                                                  ictx.lineno, mg_err[rv],
# Line 407 | Line 457 | char   **av;
457                                  mg_close();
458                                  return(MG_EINCL);
459                          }
460 +                }
461                  if (ac > 2)
462 <                        if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK)
462 >                        if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK) {
463 >                                mg_close();
464                                  return(rv);
465 <        }
465 >                        }
466 >        } while (xf_context != xf_orig);
467          mg_close();
468          return(MG_OK);
469   }
470  
471  
472 + int
473 + e_faceh(                        /* replace face+holes with single contour */
474 +        int     ac,
475 +        char    **av
476 + )
477 + {
478 +        char    *newav[MG_MAXARGC];
479 +        int     lastp = 0;
480 +        register int    i, j;
481 +
482 +        newav[0] = mg_ename[MG_E_FACE];
483 +        for (i = 1; i < ac; i++)
484 +                if (av[i][0] == '-') {
485 +                        if (i < 4)
486 +                                return(MG_EARGC);
487 +                        if (i >= ac-1)
488 +                                break;
489 +                        if (!lastp)
490 +                                lastp = i-1;
491 +                        for (j = i+1; j < ac-1 && av[j+1][0] != '-'; j++)
492 +                                ;
493 +                        if (j - i < 3)
494 +                                return(MG_EARGC);
495 +                        newav[i] = av[j];       /* connect hole loop */
496 +                } else
497 +                        newav[i] = av[i];       /* hole or perimeter vertex */
498 +        if (lastp)
499 +                newav[i++] = av[lastp];         /* finish seam to outside */
500 +        newav[i] = NULL;
501 +        return(mg_handle(MG_E_FACE, i, newav));
502 + }
503 +
504 +
505   static void
506 < make_axes(u, v, w)              /* compute u and v given w (normalized) */
507 < FVECT   u, v, w;
506 > make_axes(              /* compute u and v given w (normalized) */
507 >        FVECT   u,
508 >        FVECT   v,
509 >        FVECT   w
510 > )
511   {
512          register int    i;
513  
# Line 433 | Line 522 | FVECT  u, v, w;
522   }
523  
524  
525 < static int
526 < e_sph(ac, av)                   /* expand a sphere into cones */
527 < int     ac;
528 < char    **av;
525 > int
526 > e_sph(                  /* expand a sphere into cones */
527 >        int     ac,
528 >        char    **av
529 > )
530   {
531          static char     p2x[24], p2y[24], p2z[24], r1[24], r2[24];
532          static char     *v1ent[5] = {mg_ename[MG_E_VERTEX],"_sv1","=","_sv2"};
# Line 485 | Line 575 | char   **av;
575   }
576  
577  
578 < static int
579 < e_torus(ac, av)                 /* expand a torus into cones */
580 < int     ac;
581 < char    **av;
578 > int
579 > e_torus(                        /* expand a torus into cones */
580 >        int     ac,
581 >        char    **av
582 > )
583   {
584          static char     p2[3][24], r1[24], r2[24];
585          static char     *v1ent[5] = {mg_ename[MG_E_VERTEX],"_tv1","=","_tv2"};
# Line 577 | Line 668 | char   **av;
668   }
669  
670  
671 < static int
672 < e_cyl(ac, av)                   /* replace a cylinder with equivalent cone */
673 < int     ac;
674 < char    **av;
671 > int
672 > e_cyl(                  /* replace a cylinder with equivalent cone */
673 >        int     ac,
674 >        char    **av
675 > )
676   {
677          static char     *avnew[6] = {mg_ename[MG_E_CONE]};
678  
# Line 594 | Line 686 | char   **av;
686   }
687  
688  
689 < static int
690 < e_ring(ac, av)                  /* turn a ring into polygons */
691 < int     ac;
692 < char    **av;
689 > int
690 > e_ring(                 /* turn a ring into polygons */
691 >        int     ac,
692 >        char    **av
693 > )
694   {
695          static char     p3[3][24], p4[3][24];
696          static char     *nzent[5] = {mg_ename[MG_E_NORMAL],"0","0","0"};
# Line 692 | Line 785 | char   **av;
785   }
786  
787  
788 < static int
789 < e_cone(ac, av)                  /* turn a cone into polygons */
790 < int     ac;
791 < char    **av;
788 > int
789 > e_cone(                 /* turn a cone into polygons */
790 >        int     ac,
791 >        char    **av
792 > )
793   {
794          static char     p3[3][24], p4[3][24], n3[3][24], n4[3][24];
795          static char     *v1ent[5] = {mg_ename[MG_E_VERTEX],"_cv1","="};
# Line 707 | Line 801 | char   **av;
801          static char     *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]};
802          static char     *n4ent[5] = {mg_ename[MG_E_NORMAL],n4[0],n4[1],n4[2]};
803          static char     *fent[6] = {mg_ename[MG_E_FACE],"_cv1","_cv2","_cv3","_cv4"};
804 +        char    *v1n;
805          register C_VERTEX       *cv1, *cv2;
806          register int    i, j;
807          FVECT   u, v, w;
# Line 722 | Line 817 | char   **av;
817          if ((cv1 = c_getvert(av[1])) == NULL ||
818                          (cv2 = c_getvert(av[3])) == NULL)
819                  return(MG_EUNDEF);
820 +        v1n = av[1];
821          if (!isflt(av[2]) || !isflt(av[4]))
822                  return(MG_ETYPE);
823          rad1 = atof(av[2]);
# Line 732 | Line 828 | char   **av;
828                  if (rad2 == 0.)
829                          return(MG_EILL);
830          } else if (rad2 != 0.) {
831 <                if (rad1 < 0. ^ rad2 < 0.)
831 >                if ((rad1 < 0.) ^ (rad2 < 0.))
832                          return(MG_EILL);
833          } else {                        /* swap */
834                  C_VERTEX        *cv;
# Line 740 | Line 836 | char   **av;
836                  cv = cv1;
837                  cv1 = cv2;
838                  cv2 = cv;
839 +                v1n = av[3];
840                  d = rad1;
841                  rad1 = rad2;
842                  rad2 = d;
# Line 773 | Line 870 | char   **av;
870          if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
871                  return(rv);
872          if (rad1 == 0.) {               /* triangles */
873 <                v1ent[3] = av[1];
873 >                v1ent[3] = v1n;
874                  if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
875                          return(rv);
876                  for (j = 0; j < 3; j++)
# Line 859 | Line 956 | char   **av;
956   }
957  
958  
959 < static int
960 < e_prism(ac, av)                 /* turn a prism into polygons */
961 < int     ac;
962 < char    **av;
959 > int
960 > e_prism(                        /* turn a prism into polygons */
961 >        int     ac,
962 >        char    **av
963 > )
964   {
965          static char     p[3][24];
966 <        static char     *vent[4] = {mg_ename[MG_E_VERTEX],NULL,"="};
966 >        static char     *vent[5] = {mg_ename[MG_E_VERTEX],NULL,"="};
967          static char     *pent[5] = {mg_ename[MG_E_POINT],p[0],p[1],p[2]};
968 +        static char     *znorm[5] = {mg_ename[MG_E_NORMAL],"0","0","0"};
969          char    *newav[MG_MAXARGC], nvn[MG_MAXARGC-1][8];
970          double  length;
971 +        int     hasnorm;
972          FVECT   v1, v2, v3, norm;
973          register C_VERTEX       *cv;
974          C_VERTEX        *cv0;
975          int     rv;
976          register int    i, j;
977 <
977 >                                                /* check arguments */
978          if (ac < 5)
979                  return(MG_EARGC);
980          if (!isflt(av[ac-1]))
# Line 882 | Line 982 | char   **av;
982          length = atof(av[ac-1]);
983          if (length <= FTINY && length >= -FTINY)
984                  return(MG_EILL);
985 <                                        /* do bottom face */
886 <        newav[0] = mg_ename[MG_E_FACE];
887 <        for (i = 1; i < ac-1; i++)
888 <                newav[i] = av[i];
889 <        newav[i] = NULL;
890 <        if ((rv = mg_handle(MG_E_FACE, i, newav)) != MG_OK)
891 <                return(rv);
892 <                                        /* compute face normal */
985 >                                                /* compute face normal */
986          if ((cv0 = c_getvert(av[1])) == NULL)
987                  return(MG_EUNDEF);
988 +        hasnorm = 0;
989          norm[0] = norm[1] = norm[2] = 0.;
990          v1[0] = v1[1] = v1[2] = 0.;
991          for (i = 2; i < ac-1; i++) {
992                  if ((cv = c_getvert(av[i])) == NULL)
993                          return(MG_EUNDEF);
994 +                hasnorm += !is0vect(cv->n);
995                  v2[0] = cv->p[0] - cv0->p[0];
996                  v2[1] = cv->p[1] - cv0->p[1];
997                  v2[2] = cv->p[2] - cv0->p[2];
# Line 908 | Line 1003 | char   **av;
1003          }
1004          if (normalize(norm) == 0.)
1005                  return(MG_EILL);
1006 <                                        /* create moved vertices */
1006 >                                                /* create moved vertices */
1007          for (i = 1; i < ac-1; i++) {
1008                  sprintf(nvn[i-1], "_pv%d", i);
1009                  vent[1] = nvn[i-1];
1010 <                if ((rv = mg_handle(MG_E_VERTEX, 3, vent)) != MG_OK)
1010 >                vent[3] = av[i];
1011 >                if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK)
1012                          return(rv);
1013                  cv = c_getvert(av[i]);          /* checked above */
1014                  for (j = 0; j < 3; j++)
1015                          sprintf(p[j], FLTFMT, cv->p[j] - length*norm[j]);
1016                  if ((rv = mg_handle(MG_E_POINT, 4, pent)) != MG_OK)
1017                          return(rv);
922                newav[ac-1-i] = nvn[i-1];       /* reverse */
1018          }
1019 <                                                /* do top face */
1020 <        if ((rv = mg_handle(MG_E_FACE, ac-1, newav)) != MG_OK)
926 <                return(rv);
1019 >                                                /* make faces */
1020 >        newav[0] = mg_ename[MG_E_FACE];
1021                                                  /* do the side faces */
1022          newav[5] = NULL;
1023          newav[3] = av[ac-2];
# Line 936 | Line 1030 | char   **av;
1030                  newav[3] = newav[2];
1031                  newav[4] = newav[1];
1032          }
1033 +                                                /* do top face */
1034 +        for (i = 1; i < ac-1; i++) {
1035 +                if (hasnorm) {                  /* zero normals */
1036 +                        vent[1] = nvn[i-1];
1037 +                        if ((rv = mg_handle(MG_E_VERTEX, 2, vent)) != MG_OK)
1038 +                                return(rv);
1039 +                        if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK)
1040 +                                return(rv);
1041 +                }
1042 +                newav[ac-1-i] = nvn[i-1];       /* reverse */
1043 +        }
1044 +        if ((rv = mg_handle(MG_E_FACE, ac-1, newav)) != MG_OK)
1045 +                return(rv);
1046 +                                                /* do bottom face */
1047 +        if (hasnorm)
1048 +                for (i = 1; i < ac-1; i++) {
1049 +                        vent[1] = nvn[i-1];
1050 +                        vent[3] = av[i];
1051 +                        if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK)
1052 +                                return(rv);
1053 +                        if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK)
1054 +                                return(rv);
1055 +                        newav[i] = nvn[i-1];
1056 +                }
1057 +        else
1058 +                for (i = 1; i < ac-1; i++)
1059 +                        newav[i] = av[i];
1060 +        newav[i] = NULL;
1061 +        if ((rv = mg_handle(MG_E_FACE, i, newav)) != MG_OK)
1062 +                return(rv);
1063          return(MG_OK);
1064   }
1065  
1066  
1067   static int
1068 < e_cspec(ac, av)                 /* handle spectral color */
945 < int     ac;
946 < char    **av;
1068 > put_cxy(void)                   /* put out current xy chromaticities */
1069   {
1070          static char     xbuf[24], ybuf[24];
1071          static char     *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf};
950        int     rv;
1072  
1073 <        c_ccvt(c_ccolor, C_CSXY);
1074 <                                /* if it's really their handler, use it */
1075 <        if (mg_ehand[MG_E_CXY] != c_hcolor) {
955 <                sprintf(xbuf, "%.4f", c_ccolor->cx);
956 <                sprintf(ybuf, "%.4f", c_ccolor->cy);
957 <                if ((rv = mg_handle(MG_E_CXY, 3, ccom)) != MG_OK)
958 <                        return(rv);
959 <        }
960 <        return(MG_OK);
1073 >        sprintf(xbuf, "%.4f", c_ccolor->cx);
1074 >        sprintf(ybuf, "%.4f", c_ccolor->cy);
1075 >        return(mg_handle(MG_E_CXY, 3, ccom));
1076   }
1077  
1078  
1079   static int
1080 < e_cmix(ac, av)                  /* handle mixing of colors */
966 < int     ac;
967 < char    **av;
1080 > put_cspec(void)                 /* put out current color spectrum */
1081   {
1082          char    wl[2][6], vbuf[C_CNSS][24];
1083          char    *newav[C_CNSS+4];
1084 <        int     rv;
1084 >        double  sf;
1085          register int    i;
1086 +
1087 +        if (mg_ehand[MG_E_CSPEC] != c_hcolor) {
1088 +                sprintf(wl[0], "%d", C_CMINWL);
1089 +                sprintf(wl[1], "%d", C_CMAXWL);
1090 +                newav[0] = mg_ename[MG_E_CSPEC];
1091 +                newav[1] = wl[0];
1092 +                newav[2] = wl[1];
1093 +                sf = (double)C_CNSS / c_ccolor->ssum;
1094 +                for (i = 0; i < C_CNSS; i++) {
1095 +                        sprintf(vbuf[i], "%.4f", sf*c_ccolor->ssamp[i]);
1096 +                        newav[i+3] = vbuf[i];
1097 +                }
1098 +                newav[C_CNSS+3] = NULL;
1099 +                if ((i = mg_handle(MG_E_CSPEC, C_CNSS+3, newav)) != MG_OK)
1100 +                        return(i);
1101 +        }
1102 +        return(MG_OK);
1103 + }
1104 +
1105 +
1106 + static int
1107 + e_cspec(                        /* handle spectral color */
1108 +        int     ac,
1109 +        char    **av
1110 + )
1111 + {
1112 +                                /* convert to xy chromaticity */
1113 +        c_ccvt(c_ccolor, C_CSXY);
1114 +                                /* if it's really their handler, use it */
1115 +        if (mg_ehand[MG_E_CXY] != c_hcolor)
1116 +                return(put_cxy());
1117 +        return(MG_OK);
1118 + }
1119 +
1120 +
1121 + static int
1122 + e_cmix(                 /* handle mixing of colors */
1123 +        int     ac,
1124 +        char    **av
1125 + )
1126 + {
1127          /*
1128           * Contorted logic works as follows:
1129           *      1. the colors are already mixed in c_hcolor() support function
# Line 980 | Line 1134 | char   **av;
1134           */
1135          if (mg_ehand[MG_E_CSPEC] == e_cspec)
1136                  c_ccvt(c_ccolor, C_CSXY);
1137 <        else if (c_ccolor->flags & C_CDSPEC) {
1138 <                if (mg_ehand[MG_E_CSPEC] != c_hcolor) {
1139 <                        sprintf(wl[0], "%d", C_CMINWL);
1140 <                        sprintf(wl[1], "%d", C_CMAXWL);
1141 <                        newav[0] = mg_ename[MG_E_CSPEC];
1142 <                        newav[1] = wl[0];
1143 <                        newav[2] = wl[1];
1144 <                        for (i = 0; i < C_CNSS; i++) {
1145 <                                sprintf(vbuf[i], "%.6f",
1146 <                                                (double)c_ccolor->ssamp[i] /
1147 <                                                c_ccolor->ssum);
1148 <                                newav[i+3] = vbuf[i];
1149 <                        }
1150 <                        newav[C_CNSS+3] = NULL;
1151 <                        if ((rv = mg_handle(MG_E_CSPEC, C_CNSS+3, newav)) != MG_OK)
1152 <                                return(rv);
1153 <                }
1154 <                return(MG_OK);
1155 <        }
1156 <        if (mg_ehand[MG_E_CXY] != c_hcolor) {
1157 <                sprintf(vbuf[0], "%.4f", c_ccolor->cx);
1158 <                sprintf(vbuf[1], "%.4f", c_ccolor->cy);
1159 <                newav[0] = mg_ename[MG_E_CXY];
1160 <                newav[1] = vbuf[0];
1161 <                newav[2] = vbuf[1];
1008 <                newav[3] = NULL;
1009 <                if ((rv = mg_handle(MG_E_CXY, 3, newav)) != MG_OK)
1010 <                        return(rv);
1011 <        }
1137 >        else if (c_ccolor->flags & C_CDSPEC)
1138 >                return(put_cspec());
1139 >        if (mg_ehand[MG_E_CXY] != c_hcolor)
1140 >                return(put_cxy());
1141 >        return(MG_OK);
1142 > }
1143 >
1144 >
1145 > static int
1146 > e_cct(                  /* handle color temperature */
1147 >        int     ac,
1148 >        char    **av
1149 > )
1150 > {
1151 >        /*
1152 >         * Logic is similar to e_cmix here.  Support handler has already
1153 >         * converted temperature to spectral color.  Put it out as such
1154 >         * if they support it, otherwise convert to xy chromaticity and
1155 >         * put it out if they handle it.
1156 >         */
1157 >        if (mg_ehand[MG_E_CSPEC] != e_cspec)
1158 >                return(put_cspec());
1159 >        c_ccvt(c_ccolor, C_CSXY);
1160 >        if (mg_ehand[MG_E_CXY] != c_hcolor)
1161 >                return(put_cxy());
1162          return(MG_OK);
1163   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines