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.2 by greg, Wed Jun 22 15:33:47 1994 UTC vs.
Revision 1.10 by greg, Wed Jun 29 16:15:20 1994 UTC

# Line 59 | Line 59 | 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_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 */
69  
# Line 103 | Line 106 | mg_init()                      /* initialize alternate entity handlers */
106                  ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX;
107          } else
108                  uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF;
109 +        if (mg_ehand[MG_E_PRISM] == NULL) {
110 +                mg_ehand[MG_E_PRISM] = e_prism;
111 +                ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
112 +        } else
113 +                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
114          if (mg_ehand[MG_E_TORUS] == NULL) {
115                  mg_ehand[MG_E_TORUS] = e_torus;
116                  ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX;
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) {
121 +                        mg_ehand[MG_E_CMIX] = e_cmix;
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 131 | 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 143 | 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 160 | 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 183 | 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 200 | Line 229 | char   *fn;
229                  olen = cp - mg_file->fname + 1;
230          else
231                  olen = 0;
203        ctx->fname = (char *)malloc(olen+strlen(fn)+1);
204        if (ctx->fname == NULL)
205                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) {
211 <                free((MEM_PTR)ctx->fname);
236 >        if (ctx->fp == NULL)
237                  return(MG_ENOFILE);
213        }
238          ctx->prev = mg_file;            /* establish new context */
239          mg_file = ctx;
240          return(MG_OK);
# Line 226 | Line 250 | mg_close()                     /* close input file */
250          if (ctx->fp == stdin)
251                  return;                 /* don't close standard input */
252          fclose(ctx->fp);
229        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 288 | 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 323 | Line 361 | mg_clear()                     /* clear parser history */
361   }
362  
363  
326 int
327 mg_iterate(ac, av, f)           /* iterate on statement */
328 int     ac;
329 register char   **av;
330 int     (*f)();
331 {
332        int     niter, rval;
333        register int    i, j;
334        char    *argv[MG_MAXARGC];
335        char    cntbuf[10];
336                                        /* build partial transformation */
337        for (i = 0; i < ac; i++) {
338                if (av[i][0] == '-' && av[i][1] == 'a' && av[i][2] == '\0')
339                        break;
340                argv[i+1] = av[i];
341        }
342        argv[i+1] = NULL;
343        if (i) {                        /* handle transformation */
344                argv[0] = mg_ename[MG_E_XF];
345                if ((rval = handle_it(MG_E_XF, i+1, argv)) != MG_OK)
346                        return(rval);
347        }
348        if (i < ac) {                   /* run array */
349                if (i+1 >= ac || !isint(av[i+1]))
350                        return(MG_ETYPE);
351                niter = atoi(av[i+1]);
352                argv[0] = mg_ename[MG_E_OBJECT];
353                argv[1] = cntbuf;
354                for (j = 2; j+i < ac; j++)
355                        argv[j] = av[j+i];
356                argv[j] = NULL;
357                for (j = 0; j < niter; j++) {
358                        sprintf(cntbuf, "%d", j);
359                        if ((rval = handle_it(MG_E_OBJECT, 2, argv)) != MG_OK)
360                                return(rval);
361                        argv[0] = "-i";
362                        if ((rval = mg_iterate(ac-i, argv, f)) != MG_OK)
363                                return(rval);
364                        argv[0] = mg_ename[MG_E_OBJECT];
365                        if ((rval = handle_it(MG_E_OBJECT, 1, argv)) != MG_OK)
366                                return(rval);
367                }
368        } else if ((rval = (*f)()) != MG_OK)    /* else do this instance */
369                        return(rval);
370        if (i) {                        /* reset the transform */
371                argv[0] = mg_ename[MG_E_XF];
372                argv[1] = NULL;
373                (void)handle_it(MG_E_XF, 1, argv);
374        }
375        return(MG_OK);
376 }
377
378
364   /****************************************************************************
365   *      The following routines handle unsupported entities
366   */
# Line 391 | Line 376 | char   **av;
376  
377  
378   static int
394 reload_file()                   /* reload current MGF file */
395 {
396        register int    rval;
397
398        if ((rval = mg_rewind()) != MG_OK)
399                return(rval);
400        while (mg_read())
401                if ((rval = mg_parse()) != MG_OK)
402                        return(rval);
403        return(MG_OK);
404 }
405
406
407 static int
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 416 | 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 469 | 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 517 | Line 506 | char   **av;
506                  return(MG_EARGC);
507          if ((cv = c_getvert(av[1])) == NULL)
508                  return(MG_EUNDEF);
509 <        if (cv->n[0]==0. && cv->n[1]==0. && cv->n[2]==0.)
509 >        if (is0vect(cv->n))
510                  return(MG_EILL);
511          if (!isflt(av[2]) || !isflt(av[3]))
512                  return(MG_ETYPE);
513          minrad = atof(av[2]);
514 +        round0(minrad);
515          maxrad = atof(av[3]);
516                                          /* check orientation */
517          if (minrad > 0.)
# Line 542 | 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 571 | 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 600 | 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 629 | Line 619 | char   **av;
619                  return(MG_EARGC);
620          if ((cv = c_getvert(av[1])) == NULL)
621                  return(MG_EUNDEF);
622 <        if (cv->n[0]==0. && cv->n[1]==0. && cv->n[2]==0.)
622 >        if (is0vect(cv->n))
623                  return(MG_EILL);
624          if (!isflt(av[2]) || !isflt(av[3]))
625                  return(MG_ETYPE);
626          minrad = atof(av[2]);
627 +        round0(minrad);
628          maxrad = atof(av[3]);
629          if (minrad < 0. || maxrad <= minrad)
630                  return(MG_EILL);
# Line 641 | 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, 3, 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, 3, 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, 3, 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 734 | Line 725 | char   **av;
725          if (!isflt(av[2]) || !isflt(av[4]))
726                  return(MG_ETYPE);
727          rad1 = atof(av[2]);
728 +        round0(rad1);
729          rad2 = atof(av[4]);
730 +        round0(rad2);
731          if (rad1 == 0.) {
732                  if (rad2 == 0.)
733                          return(MG_EILL);
# Line 758 | Line 751 | char   **av;
751          if ((d = normalize(w)) == 0.)
752                  return(MG_EILL);
753          n1off = n2off = (rad2 - rad1)/d;
754 <        if (warpconends)                /* hack for e_sph and e_torus */
755 <                n2off = tan(atan(n2off)-(PI/4)/mg_nqcdivs);
756 <        n2off = sgn*n2off;
754 >        if (warpconends) {              /* hack for e_sph and e_torus */
755 >                d = atan(n2off) - (PI/4)/mg_nqcdivs;
756 >                if (d <= -PI/2+FTINY)
757 >                        n2off = -FHUGE;
758 >                else
759 >                        n2off = tan(d);
760 >        }
761          make_axes(u, v, w);
762          for (j = 0; j < 3; j++) {
763                  sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*u[j]);
764 <                sprintf(n3[j], FLTFMT, u[j] + w[j]*n2off);
764 >                if (n2off <= -FHUGE)
765 >                        sprintf(n3[j], FLTFMT, -w[j]);
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);
789                                  sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d);
790 <                                sprintf(n3[j], FLTFMT, d + w[j]*n2off);
790 >                                if (n2off > -FHUGE)
791 >                                        sprintf(n3[j], FLTFMT, d + w[j]*n2off);
792                          }
793 <                        if ((rv = handle_it(MG_E_VERTEX, 3, 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 ((rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
797 >                        if (n2off > -FHUGE &&
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 */
804                  v1ent[3] = "_cv4";
805 <                if (warpconends)                /* hack for e_sph and e_torus */
806 <                        n1off = tan(atan(n1off)+(PI/4)/mg_nqcdivs);
807 <                n1off = sgn*n1off;
805 >                if (warpconends) {              /* hack for e_sph and e_torus */
806 >                        d = atan(n1off) + (PI/4)/mg_nqcdivs;
807 >                        if (d >= PI/2-FTINY)
808 >                                n1off = FHUGE;
809 >                        else
810 >                                n1off = tan(atan(n1off)+(PI/4)/mg_nqcdivs);
811 >                }
812                  for (j = 0; j < 3; j++) {
813                          sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*u[j]);
814 <                        sprintf(n4[j], FLTFMT, u[j] + w[j]*n1off);
814 >                        if (n1off >= FHUGE)
815 >                                sprintf(n4[j], FLTFMT, w[j]);
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);
833                                  sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d);
834 <                                sprintf(n3[j], FLTFMT, d + w[j]*n2off);
834 >                                if (n2off > -FHUGE)
835 >                                        sprintf(n3[j], FLTFMT, d + w[j]*n2off);
836                                  sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*d);
837 <                                sprintf(n4[j], FLTFMT, d + w[j]*n1off);
837 >                                if (n1off < FHUGE)
838 >                                        sprintf(n4[j], FLTFMT, d + w[j]*n1off);
839                          }
840 <                        if ((rv = handle_it(MG_E_VERTEX, 3, 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 ((rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
844 >                        if (n2off > -FHUGE &&
845 >                        (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
846                                  return(rv);
847 <                        if ((rv = handle_it(MG_E_VERTEX, 3, 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 ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
851 >                        if (n1off < FHUGE &&
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 +        }
858 +        return(MG_OK);
859 + }
860 +
861 +
862 + static int
863 + e_prism(ac, av)                 /* turn a prism into polygons */
864 + int     ac;
865 + char    **av;
866 + {
867 +        static char     p[3][24];
868 +        static char     *vent[4] = {mg_ename[MG_E_VERTEX],NULL,"="};
869 +        static char     *pent[5] = {mg_ename[MG_E_POINT],p[0],p[1],p[2]};
870 +        char    *newav[MG_MAXARGC], nvn[MG_MAXARGC-1][8];
871 +        double  length;
872 +        FVECT   v1, v2, v3, norm;
873 +        register C_VERTEX       *cv;
874 +        C_VERTEX        *cv0;
875 +        int     rv;
876 +        register int    i, j;
877 +
878 +        if (ac < 5)
879 +                return(MG_EARGC);
880 +        if (!isflt(av[ac-1]))
881 +                return(MG_ETYPE);
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];
889 +        newav[i] = NULL;
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[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])) == NULL)
899 +                        return(MG_EUNDEF);
900 +                v2[0] = cv->p[0] - cv0->p[0];
901 +                v2[1] = cv->p[1] - cv0->p[1];
902 +                v2[2] = cv->p[2] - cv0->p[2];
903 +                fcross(v3, v1, v2);
904 +                norm[0] += v3[0];
905 +                norm[1] += v3[1];
906 +                norm[2] += v3[2];
907 +                VCOPY(v1, v2);
908 +        }
909 +        if (normalize(norm) == 0.)
910 +                return(MG_EILL);
911 +                                        /* create moved vertices */
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 = mg_handle(MG_E_VERTEX, 3, vent)) != MG_OK)
916 +                        return(rv);
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 = 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 = 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-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];
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