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.4 by greg, Fri Jun 24 09:32:51 1994 UTC vs.
Revision 1.15 by greg, Thu Apr 13 12:11:30 1995 UTC

# Line 53 | Line 53 | int    mg_nqcdivs = MG_NQCD;   /* number of divisions per q
53  
54   /* temporary settings for testing */
55   #define e_ies e_any_toss
56 #define e_cmix e_any_toss
57 #define e_cspec e_any_toss
56                                  /* alternate handler routines */
57  
58   static int      e_any_toss(),           /* discard unneeded entity */
59                  e_ies(),                /* IES luminaire file */
60                  e_include(),            /* include file */
61                  e_sph(),                /* sphere */
62 +                e_cct(),                /* color temperature */
63                  e_cmix(),               /* color mixtures */
64 <                e_cspec();              /* color spectra */
64 >                e_cspec(),              /* color spectra */
65                  e_cyl(),                /* cylinder */
66                  e_cone(),               /* cone */
67                  e_prism(),              /* prism */
# Line 119 | Line 118 | mg_init()                      /* initialize alternate entity handlers */
118          } else
119                  uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF;
120          if (mg_ehand[MG_E_COLOR] != NULL) {
121 <                if (mg_ehand[MG_E_CMIX] == NULL)
121 >                if (mg_ehand[MG_E_CMIX] == NULL) {
122                          mg_ehand[MG_E_CMIX] = e_cmix;
123 <                if (mg_ehand[MG_E_CSPEC] == NULL)
123 >                        ineed |= 1<<MG_E_COLOR|1<<MG_E_CXY|1<<MG_E_CSPEC|1<<MG_E_CMIX|1<<MG_E_CCT;
124 >                }
125 >                if (mg_ehand[MG_E_CSPEC] == NULL) {
126                          mg_ehand[MG_E_CSPEC] = e_cspec;
127 +                        ineed |= 1<<MG_E_COLOR|1<<MG_E_CXY|1<<MG_E_CSPEC|1<<MG_E_CMIX|1<<MG_E_CCT;
128 +                }
129 +                if (mg_ehand[MG_E_CCT] == NULL) {
130 +                        mg_ehand[MG_E_CCT] = e_cct;
131 +                        ineed |= 1<<MG_E_COLOR|1<<MG_E_CXY|1<<MG_E_CSPEC|1<<MG_E_CMIX|1<<MG_E_CCT;
132 +                }
133          }
134                                          /* check for consistency */
135          if (mg_ehand[MG_E_FACE] != NULL)
136                  uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
137 <        if (mg_ehand[MG_E_CXY] != NULL)
137 >        if (mg_ehand[MG_E_CXY] != NULL || mg_ehand[MG_E_CSPEC] != NULL ||
138 >                        mg_ehand[MG_E_CMIX] != NULL)
139                  uneed |= 1<<MG_E_COLOR;
140          if (mg_ehand[MG_E_RD] != NULL || mg_ehand[MG_E_TD] != NULL ||
141 +                        mg_ehand[MG_E_IR] != NULL ||
142                          mg_ehand[MG_E_ED] != NULL ||
143                          mg_ehand[MG_E_RS] != NULL ||
144 <                        mg_ehand[MG_E_TS] != NULL)
144 >                        mg_ehand[MG_E_TS] != NULL ||
145 >                        mg_ehand[MG_E_SIDES] != NULL)
146                  uneed |= 1<<MG_E_MATERIAL;
147          for (i = 0; i < MG_NENTITIES; i++)
148                  if (uneed & 1<<i && mg_ehand[i] == NULL) {
# Line 147 | Line 157 | mg_init()                      /* initialize alternate entity handlers */
157                  e_supp[MG_E_POINT] = c_hvertex;
158          if (ineed & 1<<MG_E_NORMAL && mg_ehand[MG_E_NORMAL] != c_hvertex)
159                  e_supp[MG_E_NORMAL] = c_hvertex;
160 +        if (ineed & 1<<MG_E_COLOR && mg_ehand[MG_E_COLOR] != c_hcolor)
161 +                e_supp[MG_E_COLOR] = c_hcolor;
162 +        if (ineed & 1<<MG_E_CXY && mg_ehand[MG_E_CXY] != c_hcolor)
163 +                e_supp[MG_E_CXY] = c_hcolor;
164 +        if (ineed & 1<<MG_E_CSPEC && mg_ehand[MG_E_CSPEC] != c_hcolor)
165 +                e_supp[MG_E_CSPEC] = c_hcolor;
166 +        if (ineed & 1<<MG_E_CMIX && mg_ehand[MG_E_CMIX] != c_hcolor)
167 +                e_supp[MG_E_CMIX] = c_hcolor;
168 +        if (ineed & 1<<MG_E_CCT && mg_ehand[MG_E_CCT] != c_hcolor)
169 +                e_supp[MG_E_CCT] = c_hcolor;
170                                          /* discard remaining entities */
171          for (i = 0; i < MG_NENTITIES; i++)
172                  if (mg_ehand[i] == NULL)
# Line 154 | Line 174 | mg_init()                      /* initialize alternate entity handlers */
174   }
175  
176  
157
177   int
178   mg_entity(name)                 /* get entity number from its name */
179   char    *name;
180   {
181 <        static LUTAB    ent_tab;        /* entity lookup table */
181 >        static LUTAB    ent_tab = LU_SINIT(NULL,NULL);  /* lookup table */
182          register char   *cp;
183  
184          if (!ent_tab.tsiz) {            /* initialize hash table */
# Line 176 | Line 195 | char   *name;
195   }
196  
197  
198 < static int
199 < handle_it(en, ac, av)           /* pass entity to appropriate handler */
198 > int
199 > mg_handle(en, ac, av)           /* pass entity to appropriate handler */
200   register int    en;
201   int     ac;
202   char    **av;
# Line 199 | Line 218 | mg_open(ctx, fn)                       /* open new input file */
218   register MG_FCTXT       *ctx;
219   char    *fn;
220   {
221 +        static int      nfids;
222          int     olen;
223          register char   *cp;
224  
225 +        ctx->fid = ++nfids;
226          ctx->lineno = 0;
227          if (fn == NULL) {
228 <                ctx->fname = "<stdin>";
228 >                strcpy(ctx->fname, "<stdin>");
229                  ctx->fp = stdin;
230                  ctx->prev = mg_file;
231                  mg_file = ctx;
# Line 216 | Line 237 | char   *fn;
237                  olen = cp - mg_file->fname + 1;
238          else
239                  olen = 0;
219        ctx->fname = (char *)malloc(olen+strlen(fn)+1);
220        if (ctx->fname == NULL)
221                return(MG_EMEM);
240          if (olen)
241                  strcpy(ctx->fname, mg_file->fname);
242          strcpy(ctx->fname+olen, fn);
243          ctx->fp = fopen(ctx->fname, "r");
244 <        if (ctx->fp == NULL) {
227 <                free((MEM_PTR)ctx->fname);
244 >        if (ctx->fp == NULL)
245                  return(MG_ENOFILE);
229        }
246          ctx->prev = mg_file;            /* establish new context */
247          mg_file = ctx;
248          return(MG_OK);
# Line 242 | Line 258 | mg_close()                     /* close input file */
258          if (ctx->fp == stdin)
259                  return;                 /* don't close standard input */
260          fclose(ctx->fp);
245        free((MEM_PTR)ctx->fname);
261   }
262  
263  
264 + void
265 + mg_fgetpos(pos)                 /* get current position in input file */
266 + register MG_FPOS        *pos;
267 + {
268 +        extern long     ftell();
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 304 | Line 334 | mg_parse()                     /* parse current input line */
334                  return(MG_OK);          /* no words in line */
335          *ap = NULL;
336                                          /* else handle it */
337 <        return(handle_it(-1, ap-argv, argv));
337 >        return(mg_handle(-1, ap-argv, argv));
338   }
339  
340  
# Line 339 | Line 369 | mg_clear()                     /* clear parser history */
369   }
370  
371  
342 int
343 mg_iterate(ac, av, f)           /* iterate on statement */
344 int     ac;
345 register char   **av;
346 int     (*f)();
347 {
348        int     niter, rval;
349        register int    i, j;
350        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);
392 }
393
394
372   /****************************************************************************
373   *      The following routines handle unsupported entities
374   */
# Line 407 | Line 384 | char   **av;
384  
385  
386   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
387   e_include(ac, av)               /* include file */
388   int     ac;
389   char    **av;
390   {
391 +        char    *xfarg[MG_MAXARGC];
392          MG_FCTXT        ictx;
393          int     rv;
394  
# Line 432 | Line 396 | char   **av;
396                  return(MG_EARGC);
397          if ((rv = mg_open(&ictx, av[1])) != MG_OK)
398                  return(rv);
399 <        if ((rv = mg_iterate(ac-2, av+2, reload_file)) != MG_OK) {
400 <                fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname,
401 <                                ictx.lineno, mg_err[rv], ictx.inpline);
402 <                mg_close();
403 <                return(MG_EINCL);
399 >        if (ac > 2) {
400 >                register int    i;
401 >
402 >                xfarg[0] = mg_ename[MG_E_XF];
403 >                for (i = 1; i < ac-1; i++)
404 >                        xfarg[i] = av[i+1];
405 >                xfarg[ac-1] = NULL;
406 >                if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK)
407 >                        return(rv);
408          }
409 +        while (!feof(mg_file->fp)) {
410 +                while (mg_read())
411 +                        if ((rv = mg_parse()) != MG_OK) {
412 +                                fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname,
413 +                                                ictx.lineno, mg_err[rv],
414 +                                                ictx.inpline);
415 +                                mg_close();
416 +                                return(MG_EINCL);
417 +                        }
418 +                if (ac > 2)
419 +                        if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK)
420 +                                return(rv);
421 +        }
422          mg_close();
423          return(MG_OK);
424   }
# Line 485 | Line 466 | char   **av;
466          rad = atof(av[2]);
467                                          /* initialize */
468          warpconends = 1;
469 <        if ((rval = handle_it(MG_E_VERTEX, 3, v2ent)) != MG_OK)
469 >        if ((rval = mg_handle(MG_E_VERTEX, 3, v2ent)) != MG_OK)
470                  return(rval);
471          sprintf(p2x, FLTFMT, cv->p[0]);
472          sprintf(p2y, FLTFMT, cv->p[1]);
473          sprintf(p2z, FLTFMT, cv->p[2]+rad);
474 <        if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
474 >        if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
475                  return(rval);
476          r2[0] = '0'; r2[1] = '\0';
477          for (i = 1; i <= 2*mg_nqcdivs; i++) {
478                  theta = i*(PI/2)/mg_nqcdivs;
479 <                if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
479 >                if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
480                          return(rval);
481                  sprintf(p2z, FLTFMT, cv->p[2]+rad*cos(theta));
482 <                if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK)
482 >                if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
483                          return(rval);
484 <                if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
484 >                if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
485                          return(rval);
486                  strcpy(r1, r2);
487                  sprintf(r2, FLTFMT, rad*sin(theta));
488 <                if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK)
488 >                if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
489                          return(rval);
490          }
491          warpconends = 0;
# Line 559 | Line 540 | char   **av;
540          for (j = 0; j < 3; j++)
541                  sprintf(p2[j], FLTFMT, cv->p[j] +
542                                  .5*sgn*(maxrad-minrad)*cv->n[j]);
543 <        if ((rval = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
543 >        if ((rval = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
544                  return(rval);
545 <        if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
545 >        if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
546                  return(rval);
547          sprintf(r2, FLTFMT, avgrad=.5*(minrad+maxrad));
548                                          /* run outer section */
549          for (i = 1; i <= 2*mg_nqcdivs; i++) {
550                  theta = i*(PI/2)/mg_nqcdivs;
551 <                if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
551 >                if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
552                          return(rval);
553                  for (j = 0; j < 3; j++)
554                          sprintf(p2[j], FLTFMT, cv->p[j] +
555                                  .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
556 <                if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK)
556 >                if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
557                          return(rval);
558 <                if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
558 >                if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
559                          return(rval);
560                  strcpy(r1, r2);
561                  sprintf(r2, FLTFMT, avgrad + .5*(maxrad-minrad)*sin(theta));
562 <                if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK)
562 >                if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
563                          return(rval);
564          }
565                                          /* run inner section */
# Line 588 | Line 569 | char   **av;
569                  for (j = 0; j < 3; j++)
570                          sprintf(p2[j], FLTFMT, cv->p[j] +
571                                  .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
572 <                if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
572 >                if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
573                          return(rval);
574 <                if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK)
574 >                if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
575                          return(rval);
576 <                if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
576 >                if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
577                          return(rval);
578                  strcpy(r1, r2);
579                  sprintf(r2, FLTFMT, -avgrad - .5*(maxrad-minrad)*sin(theta));
580 <                if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK)
580 >                if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
581                          return(rval);
582          }
583          warpconends = 0;
# Line 617 | Line 598 | char   **av;
598          avnew[2] = av[2];
599          avnew[3] = av[3];
600          avnew[4] = av[2];
601 <        return(handle_it(MG_E_CONE, 5, avnew));
601 >        return(mg_handle(MG_E_CONE, 5, avnew));
602   }
603  
604  
# Line 659 | Line 640 | char   **av;
640          make_axes(u, v, cv->n);
641          for (j = 0; j < 3; j++)
642                  sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*u[j]);
643 <        if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK)
643 >        if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
644                  return(rv);
645 <        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
645 >        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
646                  return(rv);
647          if (minrad == 0.) {             /* closed */
648                  v1ent[3] = av[1];
649 <                if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
649 >                if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
650                          return(rv);
651 <                if ((rv = handle_it(MG_E_NORMAL, 4, nzent)) != MG_OK)
651 >                if ((rv = mg_handle(MG_E_NORMAL, 4, nzent)) != MG_OK)
652                          return(rv);
653                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
654                          theta = i*(PI/2)/mg_nqcdivs;
655 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
655 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
656                                  return(rv);
657                          for (j = 0; j < 3; j++)
658                                  sprintf(p3[j], FLTFMT, cv->p[j] +
659                                                  maxrad*u[j]*cos(theta) +
660                                                  maxrad*v[j]*sin(theta));
661 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
661 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
662                                  return(rv);
663 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
663 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
664                                  return(rv);
665 <                        if ((rv = handle_it(MG_E_FACE, 4, fent)) != MG_OK)
665 >                        if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK)
666                                  return(rv);
667                  }
668          } else {                        /* open */
669 <                if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK)
669 >                if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
670                          return(rv);
671                  for (j = 0; j < 3; j++)
672                          sprintf(p4[j], FLTFMT, cv->p[j] + minrad*u[j]);
673 <                if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
673 >                if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
674                          return(rv);
675                  v1ent[3] = "_rv4";
676                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
677                          theta = i*(PI/2)/mg_nqcdivs;
678 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
678 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
679                                  return(rv);
680 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
680 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
681                                  return(rv);
682                          for (j = 0; j < 3; j++) {
683                                  d = u[j]*cos(theta) + v[j]*sin(theta);
684                                  sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*d);
685                                  sprintf(p4[j], FLTFMT, cv->p[j] + minrad*d);
686                          }
687 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
687 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
688                                  return(rv);
689 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
689 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
690                                  return(rv);
691 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v4ent)) != MG_OK)
691 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
692                                  return(rv);
693 <                        if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
693 >                        if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
694                                  return(rv);
695 <                        if ((rv = handle_it(MG_E_FACE, 5, fent)) != MG_OK)
695 >                        if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
696                                  return(rv);
697                  }
698          }
# Line 793 | Line 774 | char   **av;
774                  else
775                          sprintf(n3[j], FLTFMT, u[j] + w[j]*n2off);
776          }
777 <        if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK)
777 >        if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
778                  return(rv);
779 <        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
779 >        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
780                  return(rv);
781 <        if ((rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
781 >        if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
782                  return(rv);
783          if (rad1 == 0.) {               /* triangles */
784                  v1ent[3] = av[1];
785 <                if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
785 >                if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
786                          return(rv);
787                  for (j = 0; j < 3; j++)
788                          sprintf(n4[j], FLTFMT, w[j]);
789 <                if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
789 >                if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
790                          return(rv);
791                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
792                          theta = sgn*i*(PI/2)/mg_nqcdivs;
793 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
793 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
794                                  return(rv);
795                          for (j = 0; j < 3; j++) {
796                                  d = u[j]*cos(theta) + v[j]*sin(theta);
# Line 817 | Line 798 | char   **av;
798                                  if (n2off > -FHUGE)
799                                          sprintf(n3[j], FLTFMT, d + w[j]*n2off);
800                          }
801 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
801 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
802                                  return(rv);
803 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
803 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
804                                  return(rv);
805                          if (n2off > -FHUGE &&
806 <                        (rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
806 >                        (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
807                                  return(rv);
808 <                        if ((rv = handle_it(MG_E_FACE, 4, fent)) != MG_OK)
808 >                        if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK)
809                                  return(rv);
810                  }
811          } else {                        /* quads */
# Line 843 | Line 824 | char   **av;
824                          else
825                                  sprintf(n4[j], FLTFMT, u[j] + w[j]*n1off);
826                  }
827 <                if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK)
827 >                if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
828                          return(rv);
829 <                if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
829 >                if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
830                          return(rv);
831 <                if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
831 >                if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
832                          return(rv);
833                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
834                          theta = sgn*i*(PI/2)/mg_nqcdivs;
835 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
835 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
836                                  return(rv);
837 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
837 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
838                                  return(rv);
839                          for (j = 0; j < 3; j++) {
840                                  d = u[j]*cos(theta) + v[j]*sin(theta);
# Line 864 | Line 845 | char   **av;
845                                  if (n1off < FHUGE)
846                                          sprintf(n4[j], FLTFMT, d + w[j]*n1off);
847                          }
848 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
848 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
849                                  return(rv);
850 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
850 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
851                                  return(rv);
852                          if (n2off > -FHUGE &&
853 <                        (rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
853 >                        (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
854                                  return(rv);
855 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v4ent)) != MG_OK)
855 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
856                                  return(rv);
857 <                        if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
857 >                        if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
858                                  return(rv);
859                          if (n1off < FHUGE &&
860 <                        (rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
860 >                        (rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
861                                  return(rv);
862 <                        if ((rv = handle_it(MG_E_FACE, 5, fent)) != MG_OK)
862 >                        if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
863                                  return(rv);
864                  }
865          }
# Line 892 | Line 873 | int    ac;
873   char    **av;
874   {
875          static char     p[3][24];
876 <        static char     *vent[4] = {mg_ename[MG_E_VERTEX],NULL,"="};
876 >        static char     *vent[5] = {mg_ename[MG_E_VERTEX],NULL,"="};
877          static char     *pent[5] = {mg_ename[MG_E_POINT],p[0],p[1],p[2]};
878 +        static char     *znorm[5] = {mg_ename[MG_E_NORMAL],"0","0","0"};
879          char    *newav[MG_MAXARGC], nvn[MG_MAXARGC-1][8];
880          double  length;
881 +        int     hasnorm;
882          FVECT   v1, v2, v3, norm;
883          register C_VERTEX       *cv;
884          C_VERTEX        *cv0;
885          int     rv;
886          register int    i, j;
887 <
887 >                                                /* check arguments */
888          if (ac < 5)
889                  return(MG_EARGC);
890 <        if (!isflt(av[1]))
890 >        if (!isflt(av[ac-1]))
891                  return(MG_ETYPE);
892 <        length = atof(av[1]);
892 >        length = atof(av[ac-1]);
893          if (length <= FTINY && length >= -FTINY)
894                  return(MG_EILL);
895 <                                        /* do bottom face */
896 <        newav[0] = mg_ename[MG_E_FACE];
914 <        for (i = 1; i < ac-1; i++)
915 <                newav[i] = av[i+1];
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)
895 >                                                /* compute face normal */
896 >        if ((cv0 = c_getvert(av[1])) == NULL)
897                  return(MG_EUNDEF);
898 +        hasnorm = 0;
899          norm[0] = norm[1] = norm[2] = 0.;
900          v1[0] = v1[1] = v1[2] = 0.;
901          for (i = 2; i < ac-1; i++) {
902 <                if ((cv = c_getvert(av[i+1])) == NULL)
902 >                if ((cv = c_getvert(av[i])) == NULL)
903                          return(MG_EUNDEF);
904 +                hasnorm += !is0vect(cv->n);
905                  v2[0] = cv->p[0] - cv0->p[0];
906                  v2[1] = cv->p[1] - cv0->p[1];
907                  v2[2] = cv->p[2] - cv0->p[2];
# Line 935 | Line 913 | char   **av;
913          }
914          if (normalize(norm) == 0.)
915                  return(MG_EILL);
916 <                                        /* create moved vertices */
916 >                                                /* create moved vertices */
917          for (i = 1; i < ac-1; i++) {
918                  sprintf(nvn[i-1], "_pv%d", i);
919                  vent[1] = nvn[i-1];
920 <                if ((rv = handle_it(MG_E_VERTEX, 3, vent)) != MG_OK)
920 >                vent[3] = av[i];
921 >                if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK)
922                          return(rv);
923 <                cv = c_getvert(av[i+1]);        /* checked above */
923 >                cv = c_getvert(av[i]);          /* checked above */
924                  for (j = 0; j < 3; j++)
925                          sprintf(p[j], FLTFMT, cv->p[j] - length*norm[j]);
926 <                if ((rv = handle_it(MG_E_POINT, 4, pent)) != MG_OK)
926 >                if ((rv = mg_handle(MG_E_POINT, 4, pent)) != MG_OK)
927                          return(rv);
949                newav[ac-1-i] = nvn[i-1];       /* reverse */
928          }
929 <                                                /* do top face */
930 <        if ((rv = handle_it(MG_E_FACE, ac-1, newav)) != MG_OK)
953 <                return(rv);
929 >                                                /* make faces */
930 >        newav[0] = mg_ename[MG_E_FACE];
931                                                  /* do the side faces */
932          newav[5] = NULL;
933 <        newav[3] = av[ac-1];
933 >        newav[3] = av[ac-2];
934          newav[4] = nvn[ac-3];
935          for (i = 1; i < ac-1; i++) {
936                  newav[1] = nvn[i-1];
937 <                newav[2] = av[i+1];
938 <                if ((rv = handle_it(MG_E_FACE, 5, newav)) != MG_OK)
937 >                newav[2] = av[i];
938 >                if ((rv = mg_handle(MG_E_FACE, 5, newav)) != MG_OK)
939                          return(rv);
940                  newav[3] = newav[2];
941                  newav[4] = newav[1];
942          }
943 +                                                /* do top face */
944 +        for (i = 1; i < ac-1; i++) {
945 +                if (hasnorm) {                  /* zero normals */
946 +                        vent[1] = nvn[i-1];
947 +                        if ((rv = mg_handle(MG_E_VERTEX, 2, vent)) != MG_OK)
948 +                                return(rv);
949 +                        if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK)
950 +                                return(rv);
951 +                }
952 +                newav[ac-1-i] = nvn[i-1];       /* reverse */
953 +        }
954 +        if ((rv = mg_handle(MG_E_FACE, ac-1, newav)) != MG_OK)
955 +                return(rv);
956 +                                                /* do bottom face */
957 +        if (hasnorm)
958 +                for (i = 1; i < ac-1; i++) {
959 +                        vent[1] = nvn[i-1];
960 +                        vent[3] = av[i];
961 +                        if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK)
962 +                                return(rv);
963 +                        if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK)
964 +                                return(rv);
965 +                        newav[i] = nvn[i-1];
966 +                }
967 +        else
968 +                for (i = 1; i < ac-1; i++)
969 +                        newav[i] = av[i];
970 +        newav[i] = NULL;
971 +        if ((rv = mg_handle(MG_E_FACE, i, newav)) != MG_OK)
972 +                return(rv);
973 +        return(MG_OK);
974 + }
975 +
976 +
977 + static int
978 + put_cxy()                       /* put out current xy chromaticities */
979 + {
980 +        static char     xbuf[24], ybuf[24];
981 +        static char     *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf};
982 +        int     rv;
983 +
984 +        sprintf(xbuf, "%.4f", c_ccolor->cx);
985 +        sprintf(ybuf, "%.4f", c_ccolor->cy);
986 +        if ((rv = mg_handle(MG_E_CXY, 3, ccom)) != MG_OK)
987 +                return(rv);
988 +        return(MG_OK);
989 + }
990 +
991 +
992 + static int
993 + put_cspec()                     /* put out current color spectrum */
994 + {
995 +        char    wl[2][6], vbuf[C_CNSS][24];
996 +        char    *newav[C_CNSS+4];
997 +        double  sf;
998 +        register int    i;
999 +
1000 +        if (mg_ehand[MG_E_CSPEC] != c_hcolor) {
1001 +                sprintf(wl[0], "%d", C_CMINWL);
1002 +                sprintf(wl[1], "%d", C_CMAXWL);
1003 +                newav[0] = mg_ename[MG_E_CSPEC];
1004 +                newav[1] = wl[0];
1005 +                newav[2] = wl[1];
1006 +                sf = (double)C_CNSS / c_ccolor->ssum;
1007 +                for (i = 0; i < C_CNSS; i++) {
1008 +                        sprintf(vbuf[i], "%.4f", sf*c_ccolor->ssamp[i]);
1009 +                        newav[i+3] = vbuf[i];
1010 +                }
1011 +                newav[C_CNSS+3] = NULL;
1012 +                if ((i = mg_handle(MG_E_CSPEC, C_CNSS+3, newav)) != MG_OK)
1013 +                        return(i);
1014 +        }
1015 +        return(MG_OK);
1016 + }
1017 +
1018 +
1019 + static int
1020 + e_cspec(ac, av)                 /* handle spectral color */
1021 + int     ac;
1022 + char    **av;
1023 + {
1024 +                                /* convert to xy chromaticity */
1025 +        c_ccvt(c_ccolor, C_CSXY);
1026 +                                /* if it's really their handler, use it */
1027 +        if (mg_ehand[MG_E_CXY] != c_hcolor)
1028 +                return(put_cxy());
1029 +        return(MG_OK);
1030 + }
1031 +
1032 +
1033 + static int
1034 + e_cmix(ac, av)                  /* handle mixing of colors */
1035 + int     ac;
1036 + char    **av;
1037 + {
1038 +        /*
1039 +         * Contorted logic works as follows:
1040 +         *      1. the colors are already mixed in c_hcolor() support function
1041 +         *      2. if we would handle a spectral result, make sure it's not
1042 +         *      3. if c_hcolor() would handle a spectral result, don't bother
1043 +         *      4. otherwise, make cspec entity and pass it to their handler
1044 +         *      5. if we have only xy results, handle it as c_spec() would
1045 +         */
1046 +        if (mg_ehand[MG_E_CSPEC] == e_cspec)
1047 +                c_ccvt(c_ccolor, C_CSXY);
1048 +        else if (c_ccolor->flags & C_CDSPEC)
1049 +                return(put_cspec());
1050 +        if (mg_ehand[MG_E_CXY] != c_hcolor)
1051 +                return(put_cxy());
1052 +        return(MG_OK);
1053 + }
1054 +
1055 +
1056 + static int
1057 + e_cct(ac, av)                   /* handle color temperature */
1058 + int     ac;
1059 + char    **av;
1060 + {
1061 +        /*
1062 +         * Logic is similar to e_cmix here.  Support handler has already
1063 +         * converted temperature to spectral color.  Put it out as such
1064 +         * if they support it, otherwise convert to xy chromaticity and
1065 +         * put it out if they handle it.
1066 +         */
1067 +        if (mg_ehand[MG_E_CSPEC] != e_cspec)
1068 +                return(put_cspec());
1069 +        c_ccvt(c_ccolor, C_CSXY);
1070 +        if (mg_ehand[MG_E_CXY] != c_hcolor)
1071 +                return(put_cxy());
1072          return(MG_OK);
1073   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines