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.9 by greg, Sat Jun 25 16:33:09 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 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_cct(),                /* color temperature */
63                  e_cmix(),               /* color mixtures */
64                  e_cspec(),              /* color spectra */
65                  e_cyl(),                /* cylinder */
# Line 119 | Line 120 | mg_init()                      /* initialize alternate entity handlers */
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;
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;
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)
# Line 133 | Line 138 | mg_init()                      /* initialize alternate entity handlers */
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 158 | Line 165 | mg_init()                      /* initialize alternate entity handlers */
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 165 | Line 174 | mg_init()                      /* initialize alternate entity handlers */
174   }
175  
176  
168
177   int
178   mg_entity(name)                 /* get entity number from its name */
179   char    *name;
# Line 187 | 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 210 | 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                  strcpy(ctx->fname, "<stdin>");
# Line 222 | Line 231 | char   *fn;
231                  return(MG_OK);
232          }
233                                          /* get name relative to this context */
234 <        if (mg_file != NULL &&
226 <                        (cp = strrchr(mg_file->fname, '/')) != NULL)
227 <                olen = cp - mg_file->fname + 1;
228 <        else
229 <                olen = 0;
230 <        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)
241                  return(MG_ENOFILE);
# Line 251 | Line 257 | mg_close()                     /* close input file */
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 309 | 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 344 | Line 365 | mg_clear()                     /* clear parser history */
365   }
366  
367  
347 int
348 mg_iterate(ac, av, f)           /* iterate on statement */
349 int     ac;
350 register char   **av;
351 int     (*f)();
352 {
353        int     niter, rval;
354        register int    i, j;
355        char    *argv[MG_MAXARGC];
356        char    cntbuf[10];
357                                        /* build partial transformation */
358        for (i = 0; i < ac; i++) {
359                if (av[i][0] == '-' && av[i][1] == 'a' && av[i][2] == '\0')
360                        break;
361                argv[i+1] = av[i];
362        }
363        argv[i+1] = NULL;
364        if (i) {                        /* handle transformation */
365                argv[0] = mg_ename[MG_E_XF];
366                if ((rval = handle_it(MG_E_XF, i+1, argv)) != MG_OK)
367                        return(rval);
368        }
369        if (i < ac) {                   /* run array */
370                if (i+1 >= ac || !isint(av[i+1]))
371                        return(MG_ETYPE);
372                niter = atoi(av[i+1]);
373                argv[0] = mg_ename[MG_E_OBJECT];
374                argv[1] = cntbuf;
375                for (j = 2; j+i < ac; j++)
376                        argv[j] = av[j+i];
377                argv[j] = NULL;
378                for (j = 0; j < niter; j++) {
379                        sprintf(cntbuf, "%d", j);
380                        if ((rval = handle_it(MG_E_OBJECT, 2, argv)) != MG_OK)
381                                return(rval);
382                        argv[0] = "-i";
383                        if ((rval = mg_iterate(ac-i, argv, f)) != MG_OK)
384                                return(rval);
385                        argv[0] = mg_ename[MG_E_OBJECT];
386                        if ((rval = handle_it(MG_E_OBJECT, 1, argv)) != MG_OK)
387                                return(rval);
388                }
389        } else if ((rval = (*f)()) != MG_OK)    /* else do this instance */
390                        return(rval);
391        if (i) {                        /* reset the transform */
392                argv[0] = mg_ename[MG_E_XF];
393                argv[1] = NULL;
394                (void)handle_it(MG_E_XF, 1, argv);
395        }
396        return(MG_OK);
397 }
398
399
368   /****************************************************************************
369   *      The following routines handle unsupported entities
370   */
# Line 412 | Line 380 | char   **av;
380  
381  
382   static int
415 reload_file()                   /* reload current MGF file */
416 {
417        register int    rval;
418
419        if ((rval = mg_rewind()) != MG_OK)
420                return(rval);
421        while (mg_read())
422                if ((rval = mg_parse()) != MG_OK)
423                        return(rval);
424        return(MG_OK);
425 }
426
427
428 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 490 | 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 564 | 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 593 | 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 622 | 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 664 | 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, 2, 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, 2, 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, 2, 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 798 | Line 771 | char   **av;
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);
# Line 822 | Line 795 | char   **av;
795                                  if (n2off > -FHUGE)
796                                          sprintf(n3[j], FLTFMT, d + w[j]*n2off);
797                          }
798 <                        if ((rv = handle_it(MG_E_VERTEX, 2, 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 (n2off > -FHUGE &&
803 <                        (rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
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 */
# Line 848 | Line 821 | char   **av;
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);
# Line 869 | Line 842 | char   **av;
842                                  if (n1off < FHUGE)
843                                          sprintf(n4[j], FLTFMT, d + w[j]*n1off);
844                          }
845 <                        if ((rv = handle_it(MG_E_VERTEX, 2, 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 (n2off > -FHUGE &&
850 <                        (rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK)
850 >                        (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
851                                  return(rv);
852 <                        if ((rv = handle_it(MG_E_VERTEX, 2, 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 (n1off < FHUGE &&
857 <                        (rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK)
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          }
# Line 897 | Line 870 | int    ac;
870   char    **av;
871   {
872          static char     p[3][24];
873 <        static char     *vent[4] = {mg_ename[MG_E_VERTEX],NULL,"="};
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 <
884 >                                                /* check arguments */
885          if (ac < 5)
886                  return(MG_EARGC);
887          if (!isflt(av[ac-1]))
# Line 914 | Line 889 | char   **av;
889          length = atof(av[ac-1]);
890          if (length <= FTINY && length >= -FTINY)
891                  return(MG_EILL);
892 <                                        /* do bottom face */
918 <        newav[0] = mg_ename[MG_E_FACE];
919 <        for (i = 1; i < ac-1; i++)
920 <                newav[i] = av[i];
921 <        newav[i] = NULL;
922 <        if ((rv = handle_it(MG_E_FACE, i, newav)) != MG_OK)
923 <                return(rv);
924 <                                        /* compute face normal */
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];
# Line 940 | Line 910 | char   **av;
910          }
911          if (normalize(norm) == 0.)
912                  return(MG_EILL);
913 <                                        /* create moved vertices */
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 <                if ((rv = handle_it(MG_E_VERTEX, 3, vent)) != MG_OK)
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 = handle_it(MG_E_POINT, 4, pent)) != MG_OK)
923 >                if ((rv = mg_handle(MG_E_POINT, 4, pent)) != MG_OK)
924                          return(rv);
954                newav[ac-1-i] = nvn[i-1];       /* reverse */
925          }
926 <                                                /* do top face */
927 <        if ((rv = handle_it(MG_E_FACE, ac-1, newav)) != MG_OK)
958 <                return(rv);
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];
# Line 963 | Line 932 | char   **av;
932          for (i = 1; i < ac-1; i++) {
933                  newav[1] = nvn[i-1];
934                  newav[2] = av[i];
935 <                if ((rv = handle_it(MG_E_FACE, 5, newav)) != MG_OK)
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 < e_cspec(ac, av)                 /* handle spectral color */
977 < int     ac;
978 < char    **av;
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 <                sprintf(xbuf, "%.4f", c_ccolor->cx);
988 <                sprintf(ybuf, "%.4f", c_ccolor->cy);
989 <                if ((rv = handle_it(MG_E_CXY, 3, ccom)) != MG_OK)
990 <                        return(rv);
991 <        }
1024 >        if (mg_ehand[MG_E_CXY] != c_hcolor)
1025 >                return(put_cxy());
1026          return(MG_OK);
1027   }
1028  
# Line 998 | Line 1032 | e_cmix(ac, av)                 /* handle mixing of colors */
1032   int     ac;
1033   char    **av;
1034   {
1001        char    wl[2][6], vbuf[C_CNSS][24];
1002        char    *newav[C_CNSS+4];
1003        int     rv;
1004        register int    i;
1035          /*
1036           * Contorted logic works as follows:
1037           *      1. the colors are already mixed in c_hcolor() support function
# Line 1012 | Line 1042 | char   **av;
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 <                if (mg_ehand[MG_E_CSPEC] != c_hcolor) {
1047 <                        sprintf(wl[0], "%d", C_CMINWL);
1048 <                        sprintf(wl[1], "%d", C_CMAXWL);
1049 <                        newav[0] = mg_ename[MG_E_CSPEC];
1050 <                        newav[1] = wl[0];
1051 <                        newav[2] = wl[1];
1052 <                        for (i = 0; i < C_CNSS; i++) {
1053 <                                sprintf(vbuf[i], "%.6f",
1054 <                                                (double)c_ccolor->ssamp[i] /
1055 <                                                c_ccolor->ssum);
1056 <                                newav[i+3] = vbuf[i];
1057 <                        }
1058 <                        newav[C_CNSS+3] = NULL;
1059 <                        if ((rv = handle_it(MG_E_CSPEC, C_CNSS+3, newav)) != MG_OK)
1060 <                                return(rv);
1061 <                }
1062 <                return(MG_OK);
1063 <        }
1064 <        if (mg_ehand[MG_E_CXY] != c_hcolor) {
1065 <                sprintf(vbuf[0], "%.4f", c_ccolor->cx);
1066 <                sprintf(vbuf[1], "%.4f", c_ccolor->cy);
1067 <                newav[0] = mg_ename[MG_E_CXY];
1068 <                newav[1] = vbuf[0];
1039 <                newav[2] = vbuf[1];
1040 <                newav[3] = NULL;
1041 <                if ((rv = handle_it(MG_E_CXY, 3, newav)) != MG_OK)
1042 <                        return(rv);
1043 <        }
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