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.1 by greg, Tue Jun 21 14:45:46 1994 UTC vs.
Revision 1.16 by greg, Tue Apr 18 15:53:26 1995 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1994 Regents of the University of California */
1 > /* Copyright (c) 1995 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 56 | Line 56 | int    mg_nqcdivs = MG_NQCD;   /* number of divisions per q
56                                  /* alternate handler routines */
57  
58   static int      e_any_toss(),           /* discard unneeded entity */
59 <                e_ies();                /* IES luminaire file */
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 */
65                  e_cyl(),                /* cylinder */
66                  e_cone(),               /* cone */
67 +                e_prism(),              /* prism */
68                  e_ring(),               /* ring */
69                  e_torus();              /* torus */
70  
# Line 103 | Line 107 | mg_init()                      /* initialize alternate entity handlers */
107                  ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX;
108          } else
109                  uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF;
110 +        if (mg_ehand[MG_E_PRISM] == NULL) {
111 +                mg_ehand[MG_E_PRISM] = e_prism;
112 +                ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
113 +        } else
114 +                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
115          if (mg_ehand[MG_E_TORUS] == NULL) {
116                  mg_ehand[MG_E_TORUS] = e_torus;
117                  ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX;
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) {
122 +                        mg_ehand[MG_E_CMIX] = e_cmix;
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 131 | 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 138 | Line 174 | mg_init()                      /* initialize alternate entity handlers */
174   }
175  
176  
141
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 160 | 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 183 | Line 218 | mg_open(ctx, fn)                       /* open new input file */
218   register MG_FCTXT       *ctx;
219   char    *fn;
220   {
221 <        int     olen;
221 >        static int      nfids;
222          register char   *cp;
223  
224 +        ctx->fid = ++nfids;
225          ctx->lineno = 0;
226          if (fn == NULL) {
227 <                ctx->fname = "<stdin>";
227 >                strcpy(ctx->fname, "<stdin>");
228                  ctx->fp = stdin;
229                  ctx->prev = mg_file;
230                  mg_file = ctx;
231                  return(MG_OK);
232          }
233                                          /* get name relative to this context */
234 <        if (mg_file != NULL &&
199 <                        (cp = strrchr(mg_file->fname, '/')) != NULL)
200 <                olen = cp - mg_file->fname + 1;
201 <        else
202 <                olen = 0;
203 <        ctx->fname = (char *)malloc(olen+strlen(fn)+1);
204 <        if (ctx->fname == NULL)
205 <                return(MG_EMEM);
206 <        if (olen)
234 >        if (mg_file != NULL && (cp = strrchr(mg_file->fname, '/')) != NULL) {
235                  strcpy(ctx->fname, mg_file->fname);
236 <        strcpy(ctx->fname+olen, fn);
236 >                strcpy(ctx->fname+(cp-mg_file->fname+1), fn);
237 >        } else
238 >                strcpy(ctx->fname, fn);
239          ctx->fp = fopen(ctx->fname, "r");
240 <        if (ctx->fp == NULL) {
211 <                free((MEM_PTR)ctx->fname);
240 >        if (ctx->fp == NULL)
241                  return(MG_ENOFILE);
213        }
242          ctx->prev = mg_file;            /* establish new context */
243          mg_file = ctx;
244          return(MG_OK);
# Line 226 | Line 254 | mg_close()                     /* close input file */
254          if (ctx->fp == stdin)
255                  return;                 /* don't close standard input */
256          fclose(ctx->fp);
229        free((MEM_PTR)ctx->fname);
257   }
258  
259  
260 + void
261 + mg_fgetpos(pos)                 /* get current position in input file */
262 + register MG_FPOS        *pos;
263 + {
264 +        extern long     ftell();
265 +
266 +        pos->fid = mg_file->fid;
267 +        pos->lineno = mg_file->lineno;
268 +        pos->offset = ftell(mg_file->fp);
269 + }
270 +
271 +
272   int
273 < mg_rewind()                     /* rewind input file */
273 > mg_fgoto(pos)                   /* reposition input file pointer */
274 > register MG_FPOS        *pos;
275   {
276 <        if (mg_file->lineno == 0)
276 >        if (pos->fid != mg_file->fid)
277 >                return(MG_ESEEK);
278 >        if (pos->lineno == mg_file->lineno)
279                  return(MG_OK);
280          if (mg_file->fp == stdin)
281                  return(MG_ESEEK);       /* cannot seek on standard input */
282 <        if (fseek(mg_file->fp, 0L, 0) == EOF)
282 >        if (fseek(mg_file->fp, pos->offset, 0) == EOF)
283                  return(MG_ESEEK);
284 <        mg_file->lineno = 0;
284 >        mg_file->lineno = pos->lineno;
285          return(MG_OK);
286   }
287  
# Line 288 | Line 330 | mg_parse()                     /* parse current input line */
330                  return(MG_OK);          /* no words in line */
331          *ap = NULL;
332                                          /* else handle it */
333 <        return(handle_it(-1, ap-argv, argv));
333 >        return(mg_handle(-1, ap-argv, argv));
334   }
335  
336  
# Line 300 | Line 342 | char   *fn;
342          int     rval;
343  
344          if ((rval = mg_open(&cntxt, fn)) != MG_OK) {
345 <                fprintf("%s: %s\n", fn, mg_err[rval]);
345 >                fprintf(stderr, "%s: %s\n", fn, mg_err[rval]);
346                  return(rval);
347          }
348          while (mg_read())               /* parse each line */
# Line 323 | Line 365 | mg_clear()                     /* clear parser history */
365   }
366  
367  
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] = "-i";
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 = mg_iterate(ac-i, argv, f)) != MG_OK)
360                                return(rval);
361                }
362        } else if ((rval = (*f)()) != MG_OK)    /* else do this instance */
363                        return(rval);
364        if (i) {                        /* reset the transform */
365                argv[0] = mg_ename[MG_E_XF];
366                argv[1] = NULL;
367                (void)handle_it(MG_E_XF, 1, argv);
368        }
369        return(MG_OK);
370 }
371
372
368   /****************************************************************************
369   *      The following routines handle unsupported entities
370   */
# Line 385 | Line 380 | char   **av;
380  
381  
382   static int
388 reload_file()                   /* reload current MGF file */
389 {
390        register int    rval;
391
392        if ((rval = mg_rewind()) != MG_OK)
393                return(rval);
394        while (mg_read())
395                if ((rval = mg_parse()) != MG_OK)
396                        return(rval);
397        return(MG_OK);
398 }
399
400
401 static int
383   e_include(ac, av)               /* include file */
384   int     ac;
385   char    **av;
386   {
387 +        char    *xfarg[MG_MAXARGC];
388          MG_FCTXT        ictx;
389 +        XF_SPEC *xf_orig = xf_context;
390          int     rv;
391  
392          if (ac < 2)
393                  return(MG_EARGC);
394          if ((rv = mg_open(&ictx, av[1])) != MG_OK)
395                  return(rv);
396 <        if ((rv = mg_iterate(ac-2, av+2, reload_file)) != MG_OK) {
397 <                fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname,
398 <                                ictx.lineno, mg_err[rv], ictx.inpline);
399 <                mg_close();
400 <                return(MG_EINCL);
396 >        if (ac > 2) {
397 >                register int    i;
398 >
399 >                xfarg[0] = mg_ename[MG_E_XF];
400 >                for (i = 1; i < ac-1; i++)
401 >                        xfarg[i] = av[i+1];
402 >                xfarg[ac-1] = NULL;
403 >                if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK)
404 >                        return(rv);
405          }
406 +        do {
407 +                while (mg_read())
408 +                        if ((rv = mg_parse()) != MG_OK) {
409 +                                fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname,
410 +                                                ictx.lineno, mg_err[rv],
411 +                                                ictx.inpline);
412 +                                mg_close();
413 +                                return(MG_EINCL);
414 +                        }
415 +                if (ac > 2)
416 +                        if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK)
417 +                                return(rv);
418 +        } while (xf_context != xf_orig);
419          mg_close();
420          return(MG_OK);
421   }
# Line 463 | Line 463 | char   **av;
463          rad = atof(av[2]);
464                                          /* initialize */
465          warpconends = 1;
466 <        if ((rval = handle_it(MG_E_VERTEX, 3, v2ent)) != MG_OK)
466 >        if ((rval = mg_handle(MG_E_VERTEX, 3, v2ent)) != MG_OK)
467                  return(rval);
468          sprintf(p2x, FLTFMT, cv->p[0]);
469          sprintf(p2y, FLTFMT, cv->p[1]);
470          sprintf(p2z, FLTFMT, cv->p[2]+rad);
471 <        if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
471 >        if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
472                  return(rval);
473          r2[0] = '0'; r2[1] = '\0';
474          for (i = 1; i <= 2*mg_nqcdivs; i++) {
475                  theta = i*(PI/2)/mg_nqcdivs;
476 <                if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
476 >                if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
477                          return(rval);
478                  sprintf(p2z, FLTFMT, cv->p[2]+rad*cos(theta));
479 <                if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK)
479 >                if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
480                          return(rval);
481 <                if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
481 >                if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
482                          return(rval);
483                  strcpy(r1, r2);
484                  sprintf(r2, FLTFMT, rad*sin(theta));
485 <                if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK)
485 >                if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
486                          return(rval);
487          }
488          warpconends = 0;
# Line 511 | Line 511 | char   **av;
511                  return(MG_EARGC);
512          if ((cv = c_getvert(av[1])) == NULL)
513                  return(MG_EUNDEF);
514 <        if (cv->n[0]==0. && cv->n[1]==0. && cv->n[2]==0.)
514 >        if (is0vect(cv->n))
515                  return(MG_EILL);
516          if (!isflt(av[2]) || !isflt(av[3]))
517                  return(MG_ETYPE);
518          minrad = atof(av[2]);
519 +        round0(minrad);
520          maxrad = atof(av[3]);
521                                          /* check orientation */
522          if (minrad > 0.)
# Line 536 | Line 537 | char   **av;
537          for (j = 0; j < 3; j++)
538                  sprintf(p2[j], FLTFMT, cv->p[j] +
539                                  .5*sgn*(maxrad-minrad)*cv->n[j]);
540 <        if ((rval = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
540 >        if ((rval = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
541                  return(rval);
542 <        if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
542 >        if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
543                  return(rval);
544          sprintf(r2, FLTFMT, avgrad=.5*(minrad+maxrad));
545                                          /* run outer section */
546          for (i = 1; i <= 2*mg_nqcdivs; i++) {
547                  theta = i*(PI/2)/mg_nqcdivs;
548 <                if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
548 >                if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
549                          return(rval);
550                  for (j = 0; j < 3; j++)
551                          sprintf(p2[j], FLTFMT, cv->p[j] +
552                                  .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
553 <                if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK)
553 >                if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
554                          return(rval);
555 <                if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
555 >                if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
556                          return(rval);
557                  strcpy(r1, r2);
558                  sprintf(r2, FLTFMT, avgrad + .5*(maxrad-minrad)*sin(theta));
559 <                if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK)
559 >                if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
560                          return(rval);
561          }
562                                          /* run inner section */
# Line 565 | Line 566 | char   **av;
566                  for (j = 0; j < 3; j++)
567                          sprintf(p2[j], FLTFMT, cv->p[j] +
568                                  .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]);
569 <                if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
569 >                if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
570                          return(rval);
571 <                if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK)
571 >                if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK)
572                          return(rval);
573 <                if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK)
573 >                if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK)
574                          return(rval);
575                  strcpy(r1, r2);
576                  sprintf(r2, FLTFMT, -avgrad - .5*(maxrad-minrad)*sin(theta));
577 <                if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK)
577 >                if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK)
578                          return(rval);
579          }
580          warpconends = 0;
# Line 594 | Line 595 | char   **av;
595          avnew[2] = av[2];
596          avnew[3] = av[3];
597          avnew[4] = av[2];
598 <        return(handle_it(MG_E_CONE, 5, avnew));
598 >        return(mg_handle(MG_E_CONE, 5, avnew));
599   }
600  
601  
# Line 623 | Line 624 | char   **av;
624                  return(MG_EARGC);
625          if ((cv = c_getvert(av[1])) == NULL)
626                  return(MG_EUNDEF);
627 <        if (cv->n[0]==0. && cv->n[1]==0. && cv->n[2]==0.)
627 >        if (is0vect(cv->n))
628                  return(MG_EILL);
629          if (!isflt(av[2]) || !isflt(av[3]))
630                  return(MG_ETYPE);
631          minrad = atof(av[2]);
632 +        round0(minrad);
633          maxrad = atof(av[3]);
634          if (minrad < 0. || maxrad <= minrad)
635                  return(MG_EILL);
# Line 635 | Line 637 | char   **av;
637          make_axes(u, v, cv->n);
638          for (j = 0; j < 3; j++)
639                  sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*u[j]);
640 <        if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK)
640 >        if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
641                  return(rv);
642 <        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
642 >        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
643                  return(rv);
644          if (minrad == 0.) {             /* closed */
645                  v1ent[3] = av[1];
646 <                if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
646 >                if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
647                          return(rv);
648 <                if ((rv = handle_it(MG_E_NORMAL, 4, nzent)) != MG_OK)
648 >                if ((rv = mg_handle(MG_E_NORMAL, 4, nzent)) != MG_OK)
649                          return(rv);
650                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
651                          theta = i*(PI/2)/mg_nqcdivs;
652 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
652 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
653                                  return(rv);
654                          for (j = 0; j < 3; j++)
655                                  sprintf(p3[j], FLTFMT, cv->p[j] +
656                                                  maxrad*u[j]*cos(theta) +
657                                                  maxrad*v[j]*sin(theta));
658 <                        if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK)
658 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
659                                  return(rv);
660 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
660 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
661                                  return(rv);
662 <                        if ((rv = handle_it(MG_E_FACE, 4, fent)) != MG_OK)
662 >                        if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK)
663                                  return(rv);
664                  }
665          } else {                        /* open */
666 <                if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK)
666 >                if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
667                          return(rv);
668                  for (j = 0; j < 3; j++)
669                          sprintf(p4[j], FLTFMT, cv->p[j] + minrad*u[j]);
670 <                if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
670 >                if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
671                          return(rv);
672                  v1ent[3] = "_rv4";
673                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
674                          theta = i*(PI/2)/mg_nqcdivs;
675 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
675 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
676                                  return(rv);
677 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
677 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
678                                  return(rv);
679                          for (j = 0; j < 3; j++) {
680                                  d = u[j]*cos(theta) + v[j]*sin(theta);
681                                  sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*d);
682                                  sprintf(p4[j], FLTFMT, cv->p[j] + minrad*d);
683                          }
684 <                        if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK)
684 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
685                                  return(rv);
686 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
686 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
687                                  return(rv);
688 <                        if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK)
688 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
689                                  return(rv);
690 <                        if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
690 >                        if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
691                                  return(rv);
692 <                        if ((rv = handle_it(MG_E_FACE, 5, fent)) != MG_OK)
692 >                        if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
693                                  return(rv);
694                  }
695          }
# Line 728 | Line 730 | char   **av;
730          if (!isflt(av[2]) || !isflt(av[4]))
731                  return(MG_ETYPE);
732          rad1 = atof(av[2]);
733 +        round0(rad1);
734          rad2 = atof(av[4]);
735 +        round0(rad2);
736          if (rad1 == 0.) {
737                  if (rad2 == 0.)
738                          return(MG_EILL);
# Line 752 | Line 756 | char   **av;
756          if ((d = normalize(w)) == 0.)
757                  return(MG_EILL);
758          n1off = n2off = (rad2 - rad1)/d;
759 <        if (warpconends)                /* hack for e_sph and e_torus */
760 <                n2off = tan(atan(n2off)-(PI/4)/mg_nqcdivs);
761 <        n2off = sgn*n2off;
759 >        if (warpconends) {              /* hack for e_sph and e_torus */
760 >                d = atan(n2off) - (PI/4)/mg_nqcdivs;
761 >                if (d <= -PI/2+FTINY)
762 >                        n2off = -FHUGE;
763 >                else
764 >                        n2off = tan(d);
765 >        }
766          make_axes(u, v, w);
767          for (j = 0; j < 3; j++) {
768                  sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*u[j]);
769 <                sprintf(n3[j], FLTFMT, u[j] + w[j]*n2off);
769 >                if (n2off <= -FHUGE)
770 >                        sprintf(n3[j], FLTFMT, -w[j]);
771 >                else
772 >                        sprintf(n3[j], FLTFMT, u[j] + w[j]*n2off);
773          }
774 <        if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK)
774 >        if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK)
775                  return(rv);
776 <        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
776 >        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
777                  return(rv);
778 <        if ((rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
778 >        if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
779                  return(rv);
780          if (rad1 == 0.) {               /* triangles */
781                  v1ent[3] = av[1];
782 <                if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
782 >                if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
783                          return(rv);
784                  for (j = 0; j < 3; j++)
785                          sprintf(n4[j], FLTFMT, w[j]);
786 <                if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
786 >                if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
787                          return(rv);
788                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
789                          theta = sgn*i*(PI/2)/mg_nqcdivs;
790 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
790 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
791                                  return(rv);
792                          for (j = 0; j < 3; j++) {
793                                  d = u[j]*cos(theta) + v[j]*sin(theta);
794                                  sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d);
795 <                                sprintf(n3[j], FLTFMT, d + w[j]*n2off);
795 >                                if (n2off > -FHUGE)
796 >                                        sprintf(n3[j], FLTFMT, d + w[j]*n2off);
797                          }
798 <                        if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK)
798 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
799                                  return(rv);
800 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
800 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
801                                  return(rv);
802 <                        if ((rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
802 >                        if (n2off > -FHUGE &&
803 >                        (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
804                                  return(rv);
805 <                        if ((rv = handle_it(MG_E_FACE, 4, fent)) != MG_OK)
805 >                        if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK)
806                                  return(rv);
807                  }
808          } else {                        /* quads */
809                  v1ent[3] = "_cv4";
810 <                if (warpconends)                /* hack for e_sph and e_torus */
811 <                        n1off = tan(atan(n1off)+(PI/4)/mg_nqcdivs);
812 <                n1off = sgn*n1off;
810 >                if (warpconends) {              /* hack for e_sph and e_torus */
811 >                        d = atan(n1off) + (PI/4)/mg_nqcdivs;
812 >                        if (d >= PI/2-FTINY)
813 >                                n1off = FHUGE;
814 >                        else
815 >                                n1off = tan(atan(n1off)+(PI/4)/mg_nqcdivs);
816 >                }
817                  for (j = 0; j < 3; j++) {
818                          sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*u[j]);
819 <                        sprintf(n4[j], FLTFMT, u[j] + w[j]*n1off);
819 >                        if (n1off >= FHUGE)
820 >                                sprintf(n4[j], FLTFMT, w[j]);
821 >                        else
822 >                                sprintf(n4[j], FLTFMT, u[j] + w[j]*n1off);
823                  }
824 <                if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK)
824 >                if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK)
825                          return(rv);
826 <                if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
826 >                if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
827                          return(rv);
828 <                if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
828 >                if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
829                          return(rv);
830                  for (i = 1; i <= 4*mg_nqcdivs; i++) {
831                          theta = sgn*i*(PI/2)/mg_nqcdivs;
832 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK)
832 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
833                                  return(rv);
834 <                        if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK)
834 >                        if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK)
835                                  return(rv);
836                          for (j = 0; j < 3; j++) {
837                                  d = u[j]*cos(theta) + v[j]*sin(theta);
838                                  sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d);
839 <                                sprintf(n3[j], FLTFMT, d + w[j]*n2off);
839 >                                if (n2off > -FHUGE)
840 >                                        sprintf(n3[j], FLTFMT, d + w[j]*n2off);
841                                  sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*d);
842 <                                sprintf(n4[j], FLTFMT, d + w[j]*n1off);
842 >                                if (n1off < FHUGE)
843 >                                        sprintf(n4[j], FLTFMT, d + w[j]*n1off);
844                          }
845 <                        if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK)
845 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK)
846                                  return(rv);
847 <                        if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK)
847 >                        if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK)
848                                  return(rv);
849 <                        if ((rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
849 >                        if (n2off > -FHUGE &&
850 >                        (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
851                                  return(rv);
852 <                        if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK)
852 >                        if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK)
853                                  return(rv);
854 <                        if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK)
854 >                        if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK)
855                                  return(rv);
856 <                        if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
856 >                        if (n1off < FHUGE &&
857 >                        (rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK)
858                                  return(rv);
859 <                        if ((rv = handle_it(MG_E_FACE, 5, fent)) != MG_OK)
859 >                        if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK)
860                                  return(rv);
861                  }
862          }
863 +        return(MG_OK);
864 + }
865 +
866 +
867 + static int
868 + e_prism(ac, av)                 /* turn a prism into polygons */
869 + int     ac;
870 + char    **av;
871 + {
872 +        static char     p[3][24];
873 +        static char     *vent[5] = {mg_ename[MG_E_VERTEX],NULL,"="};
874 +        static char     *pent[5] = {mg_ename[MG_E_POINT],p[0],p[1],p[2]};
875 +        static char     *znorm[5] = {mg_ename[MG_E_NORMAL],"0","0","0"};
876 +        char    *newav[MG_MAXARGC], nvn[MG_MAXARGC-1][8];
877 +        double  length;
878 +        int     hasnorm;
879 +        FVECT   v1, v2, v3, norm;
880 +        register C_VERTEX       *cv;
881 +        C_VERTEX        *cv0;
882 +        int     rv;
883 +        register int    i, j;
884 +                                                /* check arguments */
885 +        if (ac < 5)
886 +                return(MG_EARGC);
887 +        if (!isflt(av[ac-1]))
888 +                return(MG_ETYPE);
889 +        length = atof(av[ac-1]);
890 +        if (length <= FTINY && length >= -FTINY)
891 +                return(MG_EILL);
892 +                                                /* compute face normal */
893 +        if ((cv0 = c_getvert(av[1])) == NULL)
894 +                return(MG_EUNDEF);
895 +        hasnorm = 0;
896 +        norm[0] = norm[1] = norm[2] = 0.;
897 +        v1[0] = v1[1] = v1[2] = 0.;
898 +        for (i = 2; i < ac-1; i++) {
899 +                if ((cv = c_getvert(av[i])) == NULL)
900 +                        return(MG_EUNDEF);
901 +                hasnorm += !is0vect(cv->n);
902 +                v2[0] = cv->p[0] - cv0->p[0];
903 +                v2[1] = cv->p[1] - cv0->p[1];
904 +                v2[2] = cv->p[2] - cv0->p[2];
905 +                fcross(v3, v1, v2);
906 +                norm[0] += v3[0];
907 +                norm[1] += v3[1];
908 +                norm[2] += v3[2];
909 +                VCOPY(v1, v2);
910 +        }
911 +        if (normalize(norm) == 0.)
912 +                return(MG_EILL);
913 +                                                /* create moved vertices */
914 +        for (i = 1; i < ac-1; i++) {
915 +                sprintf(nvn[i-1], "_pv%d", i);
916 +                vent[1] = nvn[i-1];
917 +                vent[3] = av[i];
918 +                if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK)
919 +                        return(rv);
920 +                cv = c_getvert(av[i]);          /* checked above */
921 +                for (j = 0; j < 3; j++)
922 +                        sprintf(p[j], FLTFMT, cv->p[j] - length*norm[j]);
923 +                if ((rv = mg_handle(MG_E_POINT, 4, pent)) != MG_OK)
924 +                        return(rv);
925 +        }
926 +                                                /* make faces */
927 +        newav[0] = mg_ename[MG_E_FACE];
928 +                                                /* do the side faces */
929 +        newav[5] = NULL;
930 +        newav[3] = av[ac-2];
931 +        newav[4] = nvn[ac-3];
932 +        for (i = 1; i < ac-1; i++) {
933 +                newav[1] = nvn[i-1];
934 +                newav[2] = av[i];
935 +                if ((rv = mg_handle(MG_E_FACE, 5, newav)) != MG_OK)
936 +                        return(rv);
937 +                newav[3] = newav[2];
938 +                newav[4] = newav[1];
939 +        }
940 +                                                /* do top face */
941 +        for (i = 1; i < ac-1; i++) {
942 +                if (hasnorm) {                  /* zero normals */
943 +                        vent[1] = nvn[i-1];
944 +                        if ((rv = mg_handle(MG_E_VERTEX, 2, vent)) != MG_OK)
945 +                                return(rv);
946 +                        if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK)
947 +                                return(rv);
948 +                }
949 +                newav[ac-1-i] = nvn[i-1];       /* reverse */
950 +        }
951 +        if ((rv = mg_handle(MG_E_FACE, ac-1, newav)) != MG_OK)
952 +                return(rv);
953 +                                                /* do bottom face */
954 +        if (hasnorm)
955 +                for (i = 1; i < ac-1; i++) {
956 +                        vent[1] = nvn[i-1];
957 +                        vent[3] = av[i];
958 +                        if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK)
959 +                                return(rv);
960 +                        if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK)
961 +                                return(rv);
962 +                        newav[i] = nvn[i-1];
963 +                }
964 +        else
965 +                for (i = 1; i < ac-1; i++)
966 +                        newav[i] = av[i];
967 +        newav[i] = NULL;
968 +        if ((rv = mg_handle(MG_E_FACE, i, newav)) != MG_OK)
969 +                return(rv);
970 +        return(MG_OK);
971 + }
972 +
973 +
974 + static int
975 + put_cxy()                       /* put out current xy chromaticities */
976 + {
977 +        static char     xbuf[24], ybuf[24];
978 +        static char     *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf};
979 +        int     rv;
980 +
981 +        sprintf(xbuf, "%.4f", c_ccolor->cx);
982 +        sprintf(ybuf, "%.4f", c_ccolor->cy);
983 +        if ((rv = mg_handle(MG_E_CXY, 3, ccom)) != MG_OK)
984 +                return(rv);
985 +        return(MG_OK);
986 + }
987 +
988 +
989 + static int
990 + put_cspec()                     /* put out current color spectrum */
991 + {
992 +        char    wl[2][6], vbuf[C_CNSS][24];
993 +        char    *newav[C_CNSS+4];
994 +        double  sf;
995 +        register int    i;
996 +
997 +        if (mg_ehand[MG_E_CSPEC] != c_hcolor) {
998 +                sprintf(wl[0], "%d", C_CMINWL);
999 +                sprintf(wl[1], "%d", C_CMAXWL);
1000 +                newav[0] = mg_ename[MG_E_CSPEC];
1001 +                newav[1] = wl[0];
1002 +                newav[2] = wl[1];
1003 +                sf = (double)C_CNSS / c_ccolor->ssum;
1004 +                for (i = 0; i < C_CNSS; i++) {
1005 +                        sprintf(vbuf[i], "%.4f", sf*c_ccolor->ssamp[i]);
1006 +                        newav[i+3] = vbuf[i];
1007 +                }
1008 +                newav[C_CNSS+3] = NULL;
1009 +                if ((i = mg_handle(MG_E_CSPEC, C_CNSS+3, newav)) != MG_OK)
1010 +                        return(i);
1011 +        }
1012 +        return(MG_OK);
1013 + }
1014 +
1015 +
1016 + static int
1017 + e_cspec(ac, av)                 /* handle spectral color */
1018 + int     ac;
1019 + char    **av;
1020 + {
1021 +                                /* convert to xy chromaticity */
1022 +        c_ccvt(c_ccolor, C_CSXY);
1023 +                                /* if it's really their handler, use it */
1024 +        if (mg_ehand[MG_E_CXY] != c_hcolor)
1025 +                return(put_cxy());
1026 +        return(MG_OK);
1027 + }
1028 +
1029 +
1030 + static int
1031 + e_cmix(ac, av)                  /* handle mixing of colors */
1032 + int     ac;
1033 + char    **av;
1034 + {
1035 +        /*
1036 +         * Contorted logic works as follows:
1037 +         *      1. the colors are already mixed in c_hcolor() support function
1038 +         *      2. if we would handle a spectral result, make sure it's not
1039 +         *      3. if c_hcolor() would handle a spectral result, don't bother
1040 +         *      4. otherwise, make cspec entity and pass it to their handler
1041 +         *      5. if we have only xy results, handle it as c_spec() would
1042 +         */
1043 +        if (mg_ehand[MG_E_CSPEC] == e_cspec)
1044 +                c_ccvt(c_ccolor, C_CSXY);
1045 +        else if (c_ccolor->flags & C_CDSPEC)
1046 +                return(put_cspec());
1047 +        if (mg_ehand[MG_E_CXY] != c_hcolor)
1048 +                return(put_cxy());
1049 +        return(MG_OK);
1050 + }
1051 +
1052 +
1053 + static int
1054 + e_cct(ac, av)                   /* handle color temperature */
1055 + int     ac;
1056 + char    **av;
1057 + {
1058 +        /*
1059 +         * Logic is similar to e_cmix here.  Support handler has already
1060 +         * converted temperature to spectral color.  Put it out as such
1061 +         * if they support it, otherwise convert to xy chromaticity and
1062 +         * put it out if they handle it.
1063 +         */
1064 +        if (mg_ehand[MG_E_CSPEC] != e_cspec)
1065 +                return(put_cspec());
1066 +        c_ccvt(c_ccolor, C_CSXY);
1067 +        if (mg_ehand[MG_E_CXY] != c_hcolor)
1068 +                return(put_cxy());
1069          return(MG_OK);
1070   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines