ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/mgf2inv.c
(Generate patch)

Comparing ray/src/cv/mgflib/mgf2inv.c (file contents):
Revision 1.2 by greg, Mon Dec 4 12:01:31 1995 UTC vs.
Revision 1.6 by greg, Tue Mar 18 11:05:43 1997 UTC

# Line 16 | Line 16 | static char SCCSid[] = "$SunId$ LBL";
16  
17   #include <ctype.h>
18  
19 + #include <string.h>
20 +
21   #include "parser.h"
22  
23 + #include "lookup.h"
24 +
25   #define O_INV1          1       /* Inventor 1.0 output */
26   #define O_INV2          2       /* Inventor 2.0 output */
27   #define O_VRML          3       /* VRML output */
28  
29 + #define MAXID           48      /* maximum identifier length */
30 +
31 + #define VERTFMT         "%+16.9e %+16.9e %+16.9e\n%+6.3f %+6.3f %+6.3f"
32 + #define VZVECT          "+0.000 +0.000 +0.000"
33 + #define VFSEPPOS        50      /* position of newline in above */
34 + #define VFLEN           72      /* total vertex string length */
35 + #define MAXVERT         10240   /* maximum cached vertices */
36 +
37 + #define setvkey(k,v)    sprintf(k,VERTFMT,(v)->p[0],(v)->p[1],(v)->p[2],\
38 +                                        (v)->n[0],(v)->n[1],(v)->n[2]);
39 +
40 + char    vlist[MAXVERT][VFLEN];  /* our vertex cache */
41 + int     nverts;                 /* current cache size */
42 +
43 + LUTAB   vert_tab = LU_SINIT(NULL,NULL);
44 +
45 + struct face {
46 +        struct face     *next;          /* next face in list */
47 +        short           nv;             /* number of vertices */
48 +        short           vl[3];          /* vertex index list (variable) */
49 + }       *flist, *flast;         /* our face cache */
50 +
51 + #define newface(n)      (struct face *)malloc(sizeof(struct face) + \
52 +                                ((n) > 3 ? (n)-3 : 0)*sizeof(short))
53 + #define freeface(f)     free((MEM_PTR)f)
54 +
55   #define TABSTOP         8       /* assumed number of characters per tab */
56   #define SHIFTW          2       /* nesting shift width */
57   #define MAXIND          15      /* maximum indent level */
58  
59   char    tabs[MAXIND*SHIFTW+1];  /* current tab-in string */
60  
61 + #define curmatname      (c_cmname == NULL ? "mat" : to_id(c_cmname))
62 +
63   int     outtype = O_INV2;       /* output format */
64  
65   extern int      i_comment(), i_object(), i_xf(),
# Line 114 | Line 146 | char   *argv[];
146                          printf("## %s %s: %u unknown entities\n",
147                                          argv[0], argv[i], mg_nunknown);
148          }
149 <        printf("}\n");                          /* close root node */
149 >        flush_cache();          /* flush face cache, just in case */
150 >        printf("}\n");          /* close root node */
151          exit(0);
152   userr:
153          fprintf(stderr, "%s: [-1|-2|-vrml] [file] ..\n", argv[0]);
# Line 165 | Line 198 | char   **av;
198   {
199          static int      objnest;
200  
201 +        flush_cache();                  /* flush cached objects */
202          if (ac == 2) {                          /* start group */
203                  printf("%sDEF %s Group {\n", tabs, to_id(av[1]));
204                  indent(1);
# Line 191 | Line 225 | char   **av;
225          static long     xfid;
226          register XF_SPEC        *spec;
227  
228 +        flush_cache();                  /* flush cached objects */
229          if (ac == 1) {                  /* end of transform */
230                  if ((spec = xf_context) == NULL)
231                          return(MG_ECNTXT);
# Line 284 | Line 319 | register XF_SPEC       *spec;
319   int
320   put_material()                  /* put out current material */
321   {
322 <        char    *mname = "mat";
322 >        char    *mname = curmatname;
323          float   rgbval[3];
324  
290        if (c_cmname != NULL)
291                mname = c_cmname;
325          if (!c_cmaterial->clock) {      /* current, just use it */
326 <                printf("%sUSE %s\n", tabs, to_id(mname));
326 >                printf("%sUSE %s\n", tabs, mname);
327                  return(0);
328          }
329                                  /* else update definition */
330 <        printf("%sDEF %s Group {\n", tabs, to_id(mname));
330 >        printf("%sDEF %s Group {\n", tabs, mname);
331          indent(1);
332          printf("%sMaterial {\n", tabs);
333          indent(1);
# Line 307 | Line 340 | put_material()                 /* put out current material */
340                  mgf2rgb(&c_cmaterial->rs_c, c_cmaterial->rs, rgbval);
341                  printf("%sspecularColor %.4f %.4f %.4f\n", tabs,
342                                  rgbval[0], rgbval[1], rgbval[2]);
343 <                printf("%sshininess %.3f\n", tabs, 1.-c_cmaterial->rs_a);
343 >                printf("%sshininess %.3f\n", tabs, 1.-sqrt(c_cmaterial->rs_a));
344          }
345          if (c_cmaterial->ed > FTINY) {
346                  mgf2rgb(&c_cmaterial->ed_c, 1.0, rgbval);
# Line 334 | Line 367 | i_face(ac, av)                 /* translate an N-sided face */
367   int     ac;
368   char    **av;
369   {
370 <        C_VERTEX        *vl[MG_MAXARGC-1];
371 <        int     donorms = 1;
370 >        static char     lastmat[MAXID];
371 >        struct face     *newf;
372 >        register C_VERTEX       *vp;
373 >        register LUENT  *lp;
374          register int    i;
375  
376          if (ac < 4)
377                  return(MG_EARGC);
378 <        printf("%sSeparator {\n", tabs);
379 <        indent(1);
380 <                                /* put out current material */
381 <        if (put_material() < 0)
382 <                return(MG_EBADMAT);
378 >        if ( strcmp(lastmat, curmatname) || c_cmaterial->clock ||
379 >                        nverts == 0 || nverts+ac-1 >= MAXVERT) {
380 >                flush_cache();                  /* new cache */
381 >                lu_init(&vert_tab, MAXVERT);
382 >                printf("%sSeparator {\n", tabs);
383 >                indent(1);
384 >                if (put_material() < 0)         /* put out material */
385 >                        return(MG_EBADMAT);
386 >                (void)strcpy(lastmat, curmatname);
387 >        }
388 >                                /* allocate new face */
389 >        if ((newf = newface(ac-1)) == NULL)
390 >                return(MG_EMEM);
391 >        newf->nv = ac-1;
392                                  /* get vertex references */
393 <        for (i = 0; i < ac-1; i++) {
394 <                if ((vl[i] = c_getvert(av[i+1])) == NULL)
393 >        for (i = 0; i < newf->nv; i++) {
394 >                if ((vp = c_getvert(av[i+1])) == NULL)
395                          return(MG_EUNDEF);
396 <                if (is0vect(vl[i]->n))
397 <                        donorms = 0;
396 >                setvkey(vlist[nverts], vp);
397 >                lp = lu_find(&vert_tab, vlist[nverts]);
398 >                if (lp == NULL)
399 >                        return(MG_EMEM);
400 >                if (lp->key == NULL)
401 >                        lp->key = (char *)vlist[nverts++];
402 >                newf->vl[i] = ((char (*)[VFLEN])lp->key - vlist);
403          }
404 <                                /* put out normal coordinates */
405 <        if (donorms) {
406 <                printf("%sNormal {\n", tabs);
407 <                indent(1);
408 <                printf("%svector [ %5.3g %5.3g %5.3g", tabs,
409 <                        vl[0]->n[0], vl[0]->n[1], vl[0]->n[2]);
410 <                for (i = 1; i < ac-1; i++)
411 <                        printf(",\n%s         %5.3g %5.3g %5.3g", tabs,
363 <                                        vl[i]->n[0], vl[i]->n[1], vl[i]->n[2]);
364 <                indent(0);
365 <                printf(" ]\n%s}\n", tabs);
366 <        } else
367 <                printf("%sNormal { }\n", tabs);
368 <                                /* put out vertex coordinates */
369 <        printf("%sCoordinate3 {\n", tabs);
370 <        indent(1);
371 <        printf("%spoint [ %13.9g %13.9g %13.9g", tabs,
372 <                        vl[0]->p[0], vl[0]->p[1], vl[0]->p[2]);
373 <        for (i = 1; i < ac-1; i++)
374 <                printf(",\n%s        %13.9g %13.9g %13.9g", tabs,
375 <                        vl[i]->p[0], vl[i]->p[1], vl[i]->p[2]);
376 <        indent(0);
377 <        printf(" ]\n%s}\n", tabs);
378 <                                /* put out actual face */
379 <        printf("%sIndexedFaceSet {\n", tabs);
380 <        indent(1);
381 <        if (donorms) {
382 <                printf("%snormalIndex [ 0", tabs);
383 <                for (i = 1; i < ac-1; i++)
384 <                        printf(", %d", i);
385 <                printf(" ]\n");
386 <        }
387 <        printf("%scoordIndex [ 0", tabs);
388 <        for (i = 1; i < ac-1; i++)
389 <                printf(", %d", i);
390 <        printf(" ]\n");
391 <        indent(0);
392 <        printf("%s}\n", tabs);
393 <        indent(0);
394 <        printf("%s}\n", tabs);
395 <        return(MG_OK);
404 >                                /* add to face list */
405 >        newf->next = NULL;
406 >        if (flist == NULL)
407 >                flist = newf;
408 >        else
409 >                flast->next = newf;
410 >        flast = newf;
411 >        return(MG_OK);          /* we'll actually put it out later */
412   }
413  
414  
# Line 405 | Line 421 | char   **av;
421  
422          if (ac != 3)
423                  return(MG_EARGC);
424 +        flush_cache();          /* flush vertex cache */
425          printf("%sSeparator {\n", tabs);
426          indent(1);
427                                  /* put out current material */
# Line 436 | Line 453 | char   **av;
453  
454          if (ac != 4)
455                  return(MG_EARGC);
456 +        flush_cache();          /* flush vertex cache */
457          printf("%sSeparator {\n", tabs);
458          indent(1);
459                                  /* put out current material */
# Line 452 | Line 470 | char   **av;
470          va[1] = v2->p[1] - v1->p[1];
471          va[2] = v2->p[2] - v1->p[2];
472          length = sqrt(DOT(va,va));
473 <        angle = acos(va[1]/length);
473 >        if (va[1] >= length)
474 >                angle = 0.;
475 >        else if (va[1] <= -length)
476 >                angle = PI;
477 >        else
478 >                angle = acos(va[1]/length);
479          printf("%sTranslation { translation %13.9g %13.9g %13.9g }\n", tabs,
480                          .5*(v1->p[0]+v2->p[0]), .5*(v1->p[1]+v2->p[1]),
481                          .5*(v1->p[2]+v2->p[2]));
# Line 471 | Line 494 | char *
494   to_id(name)                     /* make sure a name is a valid Inventor ID */
495   register char   *name;
496   {
497 <        static char     id[32];
497 >        static char     id[MAXID];
498          register char   *cp;
499  
500 <        for (cp = id; cp < id+sizeof(id)-1 && *name; name++)
500 >        for (cp = id; *name && cp < MAXID-1+id; name++)
501                  if (isalnum(*name) || *name == '_')
502                          *cp++ = *name;
503                  else
504                          *cp++ = '_';
505          *cp = '\0';
506          return(id);
507 + }
508 +
509 +
510 + flush_cache()                   /* put out cached faces */
511 + {
512 +        int     donorms = 0;
513 +        register struct face    *f;
514 +        register int    i;
515 +
516 +        if (nverts == 0)
517 +                return;
518 +                                        /* put out coordinates */
519 +        printf("%sCoordinate3 {\n", tabs);
520 +        indent(1);
521 +        vlist[0][VFSEPPOS] = '\0';
522 +        printf("%spoint [ %s", tabs, vlist[0]);
523 +        for (i = 1; i < nverts; i++) {
524 +                vlist[i][VFSEPPOS] = '\0';
525 +                printf(",\n%s        %s", tabs, vlist[i]);
526 +                if (strcmp(VFSEPPOS+1+vlist[i], VZVECT))
527 +                        donorms++;
528 +        }
529 +        indent(0);
530 +        printf(" ]\n%s}\n", tabs);
531 +        if (donorms) {                  /* put out normals */
532 +                printf("%sNormal {\n", tabs);
533 +                indent(1);
534 +                printf("%svector [ %s", tabs, VFSEPPOS+1+vlist[0]);
535 +                for (i = 1; i < nverts; i++)
536 +                        printf(",\n%s         %s", tabs, VFSEPPOS+1+vlist[i]);
537 +                indent(0);
538 +                printf(" ]\n%s}\n", tabs);
539 +        }
540 +                                        /* put out faces */
541 +        printf("%sIndexedFaceSet {\n", tabs);
542 +        indent(1);
543 +        f = flist;                      /* coordinate indices */
544 +        printf("%scoordIndex [ %d", tabs, f->vl[0]);
545 +        for (i = 1; i < f->nv; i++)
546 +                printf(", %d", f->vl[i]);
547 +        for (f = f->next; f != NULL; f = f->next) {
548 +                printf(", -1,\n%s             %d", tabs, f->vl[0]);
549 +                for (i = 1; i < f->nv; i++)
550 +                        printf(", %d", f->vl[i]);
551 +        }
552 +        printf(" ]\n");
553 +        if (donorms) {
554 +                f = flist;                      /* normal indices */
555 +                printf("%snormalIndex [ %d", tabs, f->vl[0]);
556 +                for (i = 1; i < f->nv; i++)
557 +                        printf(", %d", f->vl[i]);
558 +                for (f = f->next; f != NULL; f = f->next) {
559 +                        printf(", -1,\n%s              %d", tabs, f->vl[0]);
560 +                        for (i = 1; i < f->nv; i++)
561 +                                printf(", %d", f->vl[i]);
562 +                }
563 +                printf(" ]\n");
564 +        }
565 +        indent(0);                      /* close IndexedFaceSet */
566 +        printf("%s}\n", tabs);
567 +        indent(0);                      /* close face group */
568 +        printf("%s}\n", tabs);
569 +        while ((f = flist) != NULL) {   /* free face list */
570 +                flist = f->next;
571 +                freeface(f);
572 +        }
573 +        lu_done(&vert_tab);             /* clear lookup table */
574 +        nverts = 0;
575   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines