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.10 by greg, Wed Jun 29 16:15:20 1994 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 */
# Line 62 | Line 60 | static int     e_any_toss(),           /* discard unneeded entity *
60                  e_include(),            /* include file */
61                  e_sph(),                /* sphere */
62                  e_cmix(),               /* color mixtures */
63 <                e_cspec();              /* color spectra */
63 >                e_cspec(),              /* color spectra */
64                  e_cyl(),                /* cylinder */
65                  e_cone(),               /* cone */
66                  e_prism(),              /* prism */
# Line 119 | Line 117 | mg_init()                      /* initialize alternate entity handlers */
117          } else
118                  uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF;
119          if (mg_ehand[MG_E_COLOR] != NULL) {
120 <                if (mg_ehand[MG_E_CMIX] == NULL)
120 >                if (mg_ehand[MG_E_CMIX] == NULL) {
121                          mg_ehand[MG_E_CMIX] = e_cmix;
122 <                if (mg_ehand[MG_E_CSPEC] == NULL)
122 >                        ineed |= 1<<MG_E_COLOR|1<<MG_E_CXY|1<<MG_E_CSPEC|1<<MG_E_CMIX;
123 >                }
124 >                if (mg_ehand[MG_E_CSPEC] == NULL) {
125                          mg_ehand[MG_E_CSPEC] = e_cspec;
126 +                        ineed |= 1<<MG_E_COLOR|1<<MG_E_CXY|1<<MG_E_CSPEC|1<<MG_E_CMIX;
127 +                }
128          }
129                                          /* check for consistency */
130          if (mg_ehand[MG_E_FACE] != NULL)
131                  uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
132 <        if (mg_ehand[MG_E_CXY] != NULL)
132 >        if (mg_ehand[MG_E_CXY] != NULL || mg_ehand[MG_E_CSPEC] != NULL ||
133 >                        mg_ehand[MG_E_CMIX] != NULL)
134                  uneed |= 1<<MG_E_COLOR;
135          if (mg_ehand[MG_E_RD] != NULL || mg_ehand[MG_E_TD] != NULL ||
136                          mg_ehand[MG_E_ED] != NULL ||
# Line 147 | Line 150 | mg_init()                      /* initialize alternate entity handlers */
150                  e_supp[MG_E_POINT] = c_hvertex;
151          if (ineed & 1<<MG_E_NORMAL && mg_ehand[MG_E_NORMAL] != c_hvertex)
152                  e_supp[MG_E_NORMAL] = c_hvertex;
153 +        if (ineed & 1<<MG_E_COLOR && mg_ehand[MG_E_COLOR] != c_hcolor)
154 +                e_supp[MG_E_COLOR] = c_hcolor;
155 +        if (ineed & 1<<MG_E_CXY && mg_ehand[MG_E_CXY] != c_hcolor)
156 +                e_supp[MG_E_CXY] = c_hcolor;
157 +        if (ineed & 1<<MG_E_CSPEC && mg_ehand[MG_E_CSPEC] != c_hcolor)
158 +                e_supp[MG_E_CSPEC] = c_hcolor;
159 +        if (ineed & 1<<MG_E_CMIX && mg_ehand[MG_E_CMIX] != c_hcolor)
160 +                e_supp[MG_E_CMIX] = c_hcolor;
161                                          /* discard remaining entities */
162          for (i = 0; i < MG_NENTITIES; i++)
163                  if (mg_ehand[i] == NULL)
# Line 159 | Line 170 | int
170   mg_entity(name)                 /* get entity number from its name */
171   char    *name;
172   {
173 <        static LUTAB    ent_tab;        /* entity lookup table */
173 >        static LUTAB    ent_tab = LU_SINIT(NULL,NULL);  /* lookup table */
174          register char   *cp;
175  
176          if (!ent_tab.tsiz) {            /* initialize hash table */
# Line 176 | Line 187 | char   *name;
187   }
188  
189  
190 < static int
191 < handle_it(en, ac, av)           /* pass entity to appropriate handler */
190 > int
191 > mg_handle(en, ac, av)           /* pass entity to appropriate handler */
192   register int    en;
193   int     ac;
194   char    **av;
# Line 199 | Line 210 | mg_open(ctx, fn)                       /* open new input file */
210   register MG_FCTXT       *ctx;
211   char    *fn;
212   {
213 +        static int      nfids;
214          int     olen;
215          register char   *cp;
216  
217 +        ctx->fid = ++nfids;
218          ctx->lineno = 0;
219          if (fn == NULL) {
220 <                ctx->fname = "<stdin>";
220 >                strcpy(ctx->fname, "<stdin>");
221                  ctx->fp = stdin;
222                  ctx->prev = mg_file;
223                  mg_file = ctx;
# Line 216 | Line 229 | char   *fn;
229                  olen = cp - mg_file->fname + 1;
230          else
231                  olen = 0;
219        ctx->fname = (char *)malloc(olen+strlen(fn)+1);
220        if (ctx->fname == NULL)
221                return(MG_EMEM);
232          if (olen)
233                  strcpy(ctx->fname, mg_file->fname);
234          strcpy(ctx->fname+olen, fn);
235          ctx->fp = fopen(ctx->fname, "r");
236 <        if (ctx->fp == NULL) {
227 <                free((MEM_PTR)ctx->fname);
236 >        if (ctx->fp == NULL)
237                  return(MG_ENOFILE);
229        }
238          ctx->prev = mg_file;            /* establish new context */
239          mg_file = ctx;
240          return(MG_OK);
# Line 242 | Line 250 | mg_close()                     /* close input file */
250          if (ctx->fp == stdin)
251                  return;                 /* don't close standard input */
252          fclose(ctx->fp);
245        free((MEM_PTR)ctx->fname);
253   }
254  
255  
256 + void
257 + mg_fgetpos(pos)                 /* get current position in input file */
258 + register MG_FPOS        *pos;
259 + {
260 +        extern long     ftell();
261 +
262 +        pos->fid = mg_file->fid;
263 +        pos->lineno = mg_file->lineno;
264 +        pos->offset = ftell(mg_file->fp);
265 + }
266 +
267 +
268   int
269 < mg_rewind()                     /* rewind input file */
269 > mg_fgoto(pos)                   /* reposition input file pointer */
270 > register MG_FPOS        *pos;
271   {
272 <        if (mg_file->lineno == 0)
272 >        if (pos->fid != mg_file->fid)
273 >                return(MG_ESEEK);
274 >        if (pos->lineno == mg_file->lineno)
275                  return(MG_OK);
276          if (mg_file->fp == stdin)
277                  return(MG_ESEEK);       /* cannot seek on standard input */
278 <        if (fseek(mg_file->fp, 0L, 0) == EOF)
278 >        if (fseek(mg_file->fp, pos->offset, 0) == EOF)
279                  return(MG_ESEEK);
280 <        mg_file->lineno = 0;
280 >        mg_file->lineno = pos->lineno;
281          return(MG_OK);
282   }
283  
# Line 304 | Line 326 | mg_parse()                     /* parse current input line */
326                  return(MG_OK);          /* no words in line */
327          *ap = NULL;
328                                          /* else handle it */
329 <        return(handle_it(-1, ap-argv, argv));
329 >        return(mg_handle(-1, ap-argv, argv));
330   }
331  
332  
# Line 339 | Line 361 | mg_clear()                     /* clear parser history */
361   }
362  
363  
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
364   /****************************************************************************
365   *      The following routines handle unsupported entities
366   */
# Line 407 | Line 376 | char   **av;
376  
377  
378   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
379   e_include(ac, av)               /* include file */
380   int     ac;
381   char    **av;
382   {
383 +        char    *xfarg[MG_MAXARGC];
384          MG_FCTXT        ictx;
385          int     rv;
386  
# Line 432 | Line 388 | char   **av;
388                  return(MG_EARGC);
389          if ((rv = mg_open(&ictx, av[1])) != MG_OK)
390                  return(rv);
391 <        if ((rv = mg_iterate(ac-2, av+2, reload_file)) != MG_OK) {
392 <                fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname,
393 <                                ictx.lineno, mg_err[rv], ictx.inpline);
394 <                mg_close();
395 <                return(MG_EINCL);
391 >        if (ac > 2) {
392 >                register int    i;
393 >
394 >                xfarg[0] = mg_ename[MG_E_XF];
395 >                for (i = 1; i < ac-1; i++)
396 >                        xfarg[i] = av[i+1];
397 >                xfarg[ac-1] = NULL;
398 >                if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK)
399 >                        return(rv);
400          }
401 +        while (!feof(mg_file->fp)) {
402 +                while (mg_read())
403 +                        if ((rv = mg_parse()) != MG_OK) {
404 +                                fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname,
405 +                                                ictx.lineno, mg_err[rv],
406 +                                                ictx.inpline);
407 +                                mg_close();
408 +                                return(MG_EINCL);
409 +                        }
410 +                if (ac > 2)
411 +                        if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK)
412 +                                return(rv);
413 +        }
414          mg_close();
415          return(MG_OK);
416   }
# Line 485 | Line 458 | char   **av;
458          rad = atof(av[2]);
459                                          /* initialize */
460          warpconends = 1;
461 <        if ((rval = handle_it(MG_E_VERTEX, 3, v2ent)) != MG_OK)
461 >        if ((rval = mg_handle(MG_E_VERTEX, 3, v2ent)) != MG_OK)
462                  return(rval);
463          sprintf(p2x, FLTFMT, cv->p[0]);
464          sprintf(p2y, FLTFMT, cv->p[1]);
465          sprintf(p2z, FLTFMT, cv->p[2]+rad);
466 <        if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
466 >        if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
467                  return(rval);
468          r2[0] = '0'; r2[1] = '\0';
469          for (i = 1; i <= 2*mg_nqcdivs; i++) {
470                  theta = i*(PI/2)/mg_nqcdivs;
471 <                if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
471 >                if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
472                          return(rval);
473                  sprintf(p2z, FLTFMT, cv->p[2]+rad*cos(theta));
474 <                if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK)
474 >                if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
475                          return(rval);
476 <                if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
476 >                if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
477                          return(rval);
478                  strcpy(r1, r2);
479                  sprintf(r2, FLTFMT, rad*sin(theta));
480 <                if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK)
480 >                if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
481                          return(rval);
482          }
483          warpconends = 0;
# Line 559 | Line 532 | char   **av;
532          for (j = 0; j < 3; j++)
533                  sprintf(p2[j], FLTFMT, cv->p[j] +
534                                  .5*sgn*(maxrad-minrad)*cv->n[j]);
535 <        if ((rval = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
535 >        if ((rval = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
536                  return(rval);
537 <        if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
537 >        if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
538                  return(rval);
539          sprintf(r2, FLTFMT, avgrad=.5*(minrad+maxrad));
540                                          /* run outer section */
541          for (i = 1; i <= 2*mg_nqcdivs; i++) {
542                  theta = i*(PI/2)/mg_nqcdivs;
543 <                if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
543 >                if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
544                          return(rval);
545                  for (j = 0; j < 3; j++)
546                          sprintf(p2[j], FLTFMT, cv->p[j] +
547                                  .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
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, avgrad + .5*(maxrad-minrad)*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                                          /* run inner section */
# Line 588 | Line 561 | char   **av;
561                  for (j = 0; j < 3; j++)
562                          sprintf(p2[j], FLTFMT, cv->p[j] +
563                                  .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
564 <                if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
564 >                if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
565                          return(rval);
566 <                if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK)
566 >                if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
567                          return(rval);
568 <                if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
568 >                if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
569                          return(rval);
570                  strcpy(r1, r2);
571                  sprintf(r2, FLTFMT, -avgrad - .5*(maxrad-minrad)*sin(theta));
572 <                if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK)
572 >                if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
573                          return(rval);
574          }
575          warpconends = 0;
# Line 617 | Line 590 | char   **av;
590          avnew[2] = av[2];
591          avnew[3] = av[3];
592          avnew[4] = av[2];
593 <        return(handle_it(MG_E_CONE, 5, avnew));
593 >        return(mg_handle(MG_E_CONE, 5, avnew));
594   }
595  
596  
# Line 659 | Line 632 | char   **av;
632          make_axes(u, v, cv->n);
633          for (j = 0; j < 3; j++)
634                  sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*u[j]);
635 <        if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK)
635 >        if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
636                  return(rv);
637 <        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
637 >        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
638                  return(rv);
639          if (minrad == 0.) {             /* closed */
640                  v1ent[3] = av[1];
641 <                if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
641 >                if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
642                          return(rv);
643 <                if ((rv = handle_it(MG_E_NORMAL, 4, nzent)) != MG_OK)
643 >                if ((rv = mg_handle(MG_E_NORMAL, 4, nzent)) != MG_OK)
644                          return(rv);
645                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
646                          theta = i*(PI/2)/mg_nqcdivs;
647 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
647 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
648                                  return(rv);
649                          for (j = 0; j < 3; j++)
650                                  sprintf(p3[j], FLTFMT, cv->p[j] +
651                                                  maxrad*u[j]*cos(theta) +
652                                                  maxrad*v[j]*sin(theta));
653 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
653 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
654                                  return(rv);
655 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
655 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
656                                  return(rv);
657 <                        if ((rv = handle_it(MG_E_FACE, 4, fent)) != MG_OK)
657 >                        if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK)
658                                  return(rv);
659                  }
660          } else {                        /* open */
661 <                if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK)
661 >                if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
662                          return(rv);
663                  for (j = 0; j < 3; j++)
664                          sprintf(p4[j], FLTFMT, cv->p[j] + minrad*u[j]);
665 <                if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
665 >                if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
666                          return(rv);
667                  v1ent[3] = "_rv4";
668                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
669                          theta = i*(PI/2)/mg_nqcdivs;
670 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
670 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
671                                  return(rv);
672 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
672 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
673                                  return(rv);
674                          for (j = 0; j < 3; j++) {
675                                  d = u[j]*cos(theta) + v[j]*sin(theta);
676                                  sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*d);
677                                  sprintf(p4[j], FLTFMT, cv->p[j] + minrad*d);
678                          }
679 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
679 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
680                                  return(rv);
681 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
681 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
682                                  return(rv);
683 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v4ent)) != MG_OK)
683 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
684                                  return(rv);
685 <                        if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
685 >                        if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
686                                  return(rv);
687 <                        if ((rv = handle_it(MG_E_FACE, 5, fent)) != MG_OK)
687 >                        if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
688                                  return(rv);
689                  }
690          }
# Line 793 | Line 766 | char   **av;
766                  else
767                          sprintf(n3[j], FLTFMT, u[j] + w[j]*n2off);
768          }
769 <        if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK)
769 >        if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
770                  return(rv);
771 <        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
771 >        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
772                  return(rv);
773 <        if ((rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
773 >        if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
774                  return(rv);
775          if (rad1 == 0.) {               /* triangles */
776                  v1ent[3] = av[1];
777 <                if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
777 >                if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
778                          return(rv);
779                  for (j = 0; j < 3; j++)
780                          sprintf(n4[j], FLTFMT, w[j]);
781 <                if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
781 >                if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
782                          return(rv);
783                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
784                          theta = sgn*i*(PI/2)/mg_nqcdivs;
785 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
785 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
786                                  return(rv);
787                          for (j = 0; j < 3; j++) {
788                                  d = u[j]*cos(theta) + v[j]*sin(theta);
# Line 817 | Line 790 | char   **av;
790                                  if (n2off > -FHUGE)
791                                          sprintf(n3[j], FLTFMT, d + w[j]*n2off);
792                          }
793 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
793 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
794                                  return(rv);
795 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
795 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
796                                  return(rv);
797                          if (n2off > -FHUGE &&
798 <                        (rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
798 >                        (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
799                                  return(rv);
800 <                        if ((rv = handle_it(MG_E_FACE, 4, fent)) != MG_OK)
800 >                        if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK)
801                                  return(rv);
802                  }
803          } else {                        /* quads */
# Line 843 | Line 816 | char   **av;
816                          else
817                                  sprintf(n4[j], FLTFMT, u[j] + w[j]*n1off);
818                  }
819 <                if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK)
819 >                if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
820                          return(rv);
821 <                if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
821 >                if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
822                          return(rv);
823 <                if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
823 >                if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
824                          return(rv);
825                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
826                          theta = sgn*i*(PI/2)/mg_nqcdivs;
827 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
827 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
828                                  return(rv);
829 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
829 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
830                                  return(rv);
831                          for (j = 0; j < 3; j++) {
832                                  d = u[j]*cos(theta) + v[j]*sin(theta);
# Line 864 | Line 837 | char   **av;
837                                  if (n1off < FHUGE)
838                                          sprintf(n4[j], FLTFMT, d + w[j]*n1off);
839                          }
840 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK)
840 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
841                                  return(rv);
842 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
842 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
843                                  return(rv);
844                          if (n2off > -FHUGE &&
845 <                        (rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
845 >                        (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
846                                  return(rv);
847 <                        if ((rv = handle_it(MG_E_VERTEX, 2, v4ent)) != MG_OK)
847 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
848                                  return(rv);
849 <                        if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
849 >                        if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
850                                  return(rv);
851                          if (n1off < FHUGE &&
852 <                        (rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
852 >                        (rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
853                                  return(rv);
854 <                        if ((rv = handle_it(MG_E_FACE, 5, fent)) != MG_OK)
854 >                        if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
855                                  return(rv);
856                  }
857          }
# Line 904 | Line 877 | char   **av;
877  
878          if (ac < 5)
879                  return(MG_EARGC);
880 <        if (!isflt(av[1]))
880 >        if (!isflt(av[ac-1]))
881                  return(MG_ETYPE);
882 <        length = atof(av[1]);
882 >        length = atof(av[ac-1]);
883          if (length <= FTINY && length >= -FTINY)
884                  return(MG_EILL);
885                                          /* do bottom face */
886          newav[0] = mg_ename[MG_E_FACE];
887          for (i = 1; i < ac-1; i++)
888 <                newav[i] = av[i+1];
888 >                newav[i] = av[i];
889          newav[i] = NULL;
890 <        if ((rv = handle_it(MG_E_FACE, i, newav)) != MG_OK)
890 >        if ((rv = mg_handle(MG_E_FACE, i, newav)) != MG_OK)
891                  return(rv);
892                                          /* compute face normal */
893 <        if ((cv0 = c_getvert(av[2])) == NULL)
893 >        if ((cv0 = c_getvert(av[1])) == NULL)
894                  return(MG_EUNDEF);
895          norm[0] = norm[1] = norm[2] = 0.;
896          v1[0] = v1[1] = v1[2] = 0.;
897          for (i = 2; i < ac-1; i++) {
898 <                if ((cv = c_getvert(av[i+1])) == NULL)
898 >                if ((cv = c_getvert(av[i])) == NULL)
899                          return(MG_EUNDEF);
900                  v2[0] = cv->p[0] - cv0->p[0];
901                  v2[1] = cv->p[1] - cv0->p[1];
# Line 939 | Line 912 | char   **av;
912          for (i = 1; i < ac-1; i++) {
913                  sprintf(nvn[i-1], "_pv%d", i);
914                  vent[1] = nvn[i-1];
915 <                if ((rv = handle_it(MG_E_VERTEX, 3, vent)) != MG_OK)
915 >                if ((rv = mg_handle(MG_E_VERTEX, 3, vent)) != MG_OK)
916                          return(rv);
917 <                cv = c_getvert(av[i+1]);        /* checked above */
917 >                cv = c_getvert(av[i]);          /* checked above */
918                  for (j = 0; j < 3; j++)
919                          sprintf(p[j], FLTFMT, cv->p[j] - length*norm[j]);
920 <                if ((rv = handle_it(MG_E_POINT, 4, pent)) != MG_OK)
920 >                if ((rv = mg_handle(MG_E_POINT, 4, pent)) != MG_OK)
921                          return(rv);
922                  newav[ac-1-i] = nvn[i-1];       /* reverse */
923          }
924                                                  /* do top face */
925 <        if ((rv = handle_it(MG_E_FACE, ac-1, newav)) != MG_OK)
925 >        if ((rv = mg_handle(MG_E_FACE, ac-1, newav)) != MG_OK)
926                  return(rv);
927                                                  /* do the side faces */
928          newav[5] = NULL;
929 <        newav[3] = av[ac-1];
929 >        newav[3] = av[ac-2];
930          newav[4] = nvn[ac-3];
931          for (i = 1; i < ac-1; i++) {
932                  newav[1] = nvn[i-1];
933 <                newav[2] = av[i+1];
934 <                if ((rv = handle_it(MG_E_FACE, 5, newav)) != MG_OK)
933 >                newav[2] = av[i];
934 >                if ((rv = mg_handle(MG_E_FACE, 5, newav)) != MG_OK)
935                          return(rv);
936                  newav[3] = newav[2];
937                  newav[4] = newav[1];
938 +        }
939 +        return(MG_OK);
940 + }
941 +
942 +
943 + static int
944 + e_cspec(ac, av)                 /* handle spectral color */
945 + int     ac;
946 + char    **av;
947 + {
948 +        static char     xbuf[24], ybuf[24];
949 +        static char     *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf};
950 +        int     rv;
951 +
952 +        c_ccvt(c_ccolor, C_CSXY);
953 +                                /* if it's really their handler, use it */
954 +        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);
961 + }
962 +
963 +
964 + static int
965 + e_cmix(ac, av)                  /* handle mixing of colors */
966 + int     ac;
967 + char    **av;
968 + {
969 +        char    wl[2][6], vbuf[C_CNSS][24];
970 +        char    *newav[C_CNSS+4];
971 +        int     rv;
972 +        register int    i;
973 +        /*
974 +         * Contorted logic works as follows:
975 +         *      1. the colors are already mixed in c_hcolor() support function
976 +         *      2. if we would handle a spectral result, make sure it's not
977 +         *      3. if c_hcolor() would handle a spectral result, don't bother
978 +         *      4. otherwise, make cspec entity and pass it to their handler
979 +         *      5. if we have only xy results, handle it as c_spec() would
980 +         */
981 +        if (mg_ehand[MG_E_CSPEC] == e_cspec)
982 +                c_ccvt(c_ccolor, C_CSXY);
983 +        else if (c_ccolor->flags & C_CDSPEC) {
984 +                if (mg_ehand[MG_E_CSPEC] != c_hcolor) {
985 +                        sprintf(wl[0], "%d", C_CMINWL);
986 +                        sprintf(wl[1], "%d", C_CMAXWL);
987 +                        newav[0] = mg_ename[MG_E_CSPEC];
988 +                        newav[1] = wl[0];
989 +                        newav[2] = wl[1];
990 +                        for (i = 0; i < C_CNSS; i++) {
991 +                                sprintf(vbuf[i], "%.6f",
992 +                                                (double)c_ccolor->ssamp[i] /
993 +                                                c_ccolor->ssum);
994 +                                newav[i+3] = vbuf[i];
995 +                        }
996 +                        newav[C_CNSS+3] = NULL;
997 +                        if ((rv = mg_handle(MG_E_CSPEC, C_CNSS+3, newav)) != MG_OK)
998 +                                return(rv);
999 +                }
1000 +                return(MG_OK);
1001 +        }
1002 +        if (mg_ehand[MG_E_CXY] != c_hcolor) {
1003 +                sprintf(vbuf[0], "%.4f", c_ccolor->cx);
1004 +                sprintf(vbuf[1], "%.4f", c_ccolor->cy);
1005 +                newav[0] = mg_ename[MG_E_CXY];
1006 +                newav[1] = vbuf[0];
1007 +                newav[2] = vbuf[1];
1008 +                newav[3] = NULL;
1009 +                if ((rv = mg_handle(MG_E_CXY, 3, newav)) != MG_OK)
1010 +                        return(rv);
1011          }
1012          return(MG_OK);
1013   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines