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

Comparing ray/src/cv/obj2rad.c (file contents):
Revision 2.8 by greg, Tue Jun 14 14:30:38 1994 UTC vs.
Revision 2.11 by greg, Wed Jun 15 12:50:37 1994 UTC

# Line 20 | Line 20 | static char SCCSid[] = "$SunId$ LBL";
20   #include <ctype.h>
21  
22   #define TCALNAME        "tmesh.cal"     /* triangle interp. file */
23 #define QCALNAME        "surf.cal"      /* quad interp. file */
23   #define PATNAME         "M-pat"         /* mesh pattern name (reused) */
24   #define TEXNAME         "M-nor"         /* mesh texture name (reused) */
25   #define DEFOBJ          "unnamed"       /* default object name */
# Line 37 | Line 36 | int    nvns;
36   FLOAT   (*vtlist)[2];           /* map vertex list */
37   int     nvts;
38  
39 < typedef FLOAT   BARYCCM[3][4];  /* barycentric coordinate system */
39 > typedef struct {
40 >        int     ax;             /* major axis */
41 >        FLOAT   tm[2][3];       /* transformation */
42 > } BARYCCM;
43  
44   typedef int     VNDX[3];        /* vertex index (point,map,normal) */
45  
# Line 130 | Line 132 | char   *argv[];
132          }
133          exit(0);
134   userr:
135 <        fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n] [file.obj]\n",
135 >        fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n",
136                          argv[0]);
137          exit(1);
138   }
# Line 237 | Line 239 | FILE   *fp;
239                                  if (!puttri(argv[1], argv[2], argv[3]))
240                                          syntax("Bad triangle");
241                                  break;
240                        case 4:
241                                if (!putquad(argv[1], argv[2],
242                                                argv[3], argv[4]))
243                                        syntax("Bad quad");
244                                break;
242                          default:
243                                  if (!putface(argc-1, argv+1))
244                                          syntax("Bad face");
# Line 440 | Line 437 | register char  *vs;
437                  if (vi[0]-- > nvs)
438                          return(0);
439          } else if (vi[0] < 0) {
440 <                vi[0] = nvs + vi[0];
440 >                vi[0] += nvs;
441                  if (vi[0] < 0)
442                          return(0);
443          } else
# Line 454 | Line 451 | register char  *vs;
451                  if (vi[1]-- > nvts)
452                          return(0);
453          } else if (vi[1] < 0) {
454 <                vi[1] = nvts + vi[1];
454 >                vi[1] += nvts;
455                  if (vi[1] < 0)
456                          return(0);
457          } else
# Line 468 | Line 465 | register char  *vs;
465                  if (vi[2]-- > nvns)
466                          return(0);
467          } else if (vi[2] < 0) {
468 <                vi[2] = nvns + vi[2];
468 >                vi[2] += nvns;
469                  if (vi[2] < 0)
470                          return(0);
471          } else
# Line 477 | Line 474 | register char  *vs;
474   }
475  
476  
477 < nonplanar(ac, av)                       /* are vertices are non-planar? */
477 > nonplanar(ac, av)                       /* are vertices non-planar? */
478   register int    ac;
479   register char   **av;
480   {
# Line 536 | Line 533 | register char  **av;
533          char    *cp;
534          register int    i;
535  
536 <        if (nonplanar(ac, av)) {        /* break into quads and triangles */
537 <                while (ac > 3) {
538 <                        if (!putquad(av[0], av[1], av[2], av[3]))
536 >        if (nonplanar(ac, av)) {        /* break into triangles */
537 >                while (ac > 2) {
538 >                        if (!puttri(av[0], av[1], av[2]))
539                                  return(0);
540 <                        ac -= 2;        /* remove two vertices & rotate */
540 >                        ac--;           /* remove vertex & rotate */
541                          cp = av[0];
542                          for (i = 0; i < ac-1; i++)
543 <                                av[i] = av[i+3];
543 >                                av[i] = av[i+2];
544                          av[i] = cp;
545                  }
549                if (ac == 3 && !puttri(av[0], av[1], av[2]))
550                        return(0);
546                  return(1);
547          }
548          if ((cp = getmtl()) == NULL)
# Line 584 | Line 579 | char   *v1, *v2, *v3;
579          patOK = 0;
580   #endif
581          if (texOK | patOK)
582 <                if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]],
582 >                if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]],
583                                  vlist[v3i[0]]) < 0)
584                          return(-1);
585                                          /* put out texture (if any) */
# Line 592 | Line 587 | char   *v1, *v2, *v3;
587                  printf("\n%s texfunc %s\n", mod, TEXNAME);
588                  mod = TEXNAME;
589                  printf("4 dx dy dz %s\n", TCALNAME);
590 <                printf("0\n21\n");
591 <                put_baryc(bvecs);
590 >                printf("0\n16 ");
591 >                put_baryc(&bvecs);
592                  printf("\t%14.12g %14.12g %14.12g\n",
593                                  vnlist[v1i[2]][0], vnlist[v2i[2]][0],
594                                  vnlist[v3i[2]][0]);
# Line 610 | Line 605 | char   *v1, *v2, *v3;
605                  printf("\n%s colorpict %s\n", mod, PATNAME);
606                  mod = PATNAME;
607                  printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME);
608 <                printf("0\n18\n");
609 <                put_baryc(bvecs);
608 >                printf("0\n13 ");
609 >                put_baryc(&bvecs);
610                  printf("\t%f %f %f\n", vtlist[v1i[1]][0],
611                                  vtlist[v2i[1]][0], vtlist[v3i[1]][0]);
612                  printf("\t%f %f %f\n", vtlist[v1i[1]][1],
# Line 631 | Line 626 | char   *v1, *v2, *v3;
626  
627   int
628   comp_baryc(bcm, v1, v2, v3)             /* compute barycentric vectors */
629 < register BARYCCM        bcm;
629 > register BARYCCM        *bcm;
630   FLOAT   *v1, *v2, *v3;
631   {
632          FLOAT   *vt;
633          FVECT   va, vab, vcb;
634          double  d;
635 +        int     ax0, ax1;
636          register int    i, j;
637 <
638 <        for (j = 0; j < 3; j++) {
639 <                for (i = 0; i < 3; i++) {
640 <                        vab[i] = v1[i] - v2[i];
641 <                        vcb[i] = v3[i] - v2[i];
642 <                }
643 <                d = DOT(vcb,vcb);
637 >                                        /* compute major axis */
638 >        for (i = 0; i < 3; i++) {
639 >                vab[i] = v1[i] - v2[i];
640 >                vcb[i] = v3[i] - v2[i];
641 >        }
642 >        fcross(va, vab, vcb);
643 >        bcm->ax = ABS(va[0]) > ABS(va[1]) ? 0 : 1;
644 >        bcm->ax = ABS(va[bcm->ax]) > ABS(va[2]) ? bcm->ax : 2;
645 >        ax0 = (bcm->ax + 1) % 3;
646 >        ax1 = (bcm->ax + 2) % 3;
647 >        for (j = 0; j < 2; j++) {
648 >                vab[0] = v1[ax0] - v2[ax0];
649 >                vcb[0] = v3[ax0] - v2[ax0];
650 >                vab[1] = v1[ax1] - v2[ax1];
651 >                vcb[1] = v3[ax1] - v2[ax1];
652 >                d = vcb[0]*vcb[0] + vcb[1]*vcb[1];
653                  if (d <= FTINY)
654                          return(-1);
655 <                d = DOT(vcb,vab)/d;
656 <                for (i = 0; i < 3; i++)
657 <                        va[i] = vab[i] - vcb[i]*d;
658 <                d = DOT(va,va);
655 >                d = (vcb[0]*vab[0]+vcb[1]*vab[1])/d;
656 >                va[0] = vab[0] - vcb[0]*d;
657 >                va[1] = vab[1] - vcb[1]*d;
658 >                d = va[0]*va[0] + va[1]*va[1];
659                  if (d <= FTINY)
660                          return(-1);
661 <                for (i = 0; i < 3; i++) {
662 <                        va[i] /= d;
663 <                        bcm[j][i] = va[i];
659 <                }
660 <                bcm[j][3] = -DOT(v2,va);
661 >                bcm->tm[j][0] = va[0] /= d;
662 >                bcm->tm[j][1] = va[1] /= d;
663 >                bcm->tm[j][2] = -(v2[ax0]*va[0]+v2[ax1]*va[1]);
664                                          /* rotate vertices */
665                  vt = v1;
666                  v1 = v2;
# Line 669 | Line 672 | FLOAT  *v1, *v2, *v3;
672  
673  
674   put_baryc(bcm)                          /* put barycentric coord. vectors */
675 < register BARYCCM        bcm;
675 > register BARYCCM        *bcm;
676   {
677 <        register int    i;
678 <
679 <        for (i = 0; i < 3; i++)
680 <                printf("%14.8f %14.8f %14.8f %14.8f\n",
681 <                                bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]);
679 < }
680 <
681 <
682 < putquad(p0, p1, p3, p2)                 /* put out a quadrilateral */
683 < char  *p0, *p1, *p3, *p2;               /* names correspond to binary pos. */
684 < {
685 <        VNDX  p0i, p1i, p2i, p3i;
686 <        FVECT  norm[4];
687 <        char  *mod, *name;
688 <        int  axis;
689 <        FVECT  v1, v2, vc1, vc2;
690 <        int  ok1, ok2;
691 <
692 < #ifdef TEXMAPS
693 <        /* also should output texture index coordinates,
694 <         * which will require new .cal file
695 <         */
696 < #endif
697 <        if ((mod = getmtl()) == NULL)
698 <                return(-1);
699 <        name = getonm();
700 <                                        /* get actual indices */
701 <        if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) ||
702 <                        !cvtndx(p2i,p2) || !cvtndx(p3i,p3))
703 <                return(0);
704 <                                        /* compute exact normals */
705 <        fvsum(v1, vlist[p1i[0]], vlist[p0i[0]], -1.0);
706 <        fvsum(v2, vlist[p2i[0]], vlist[p0i[0]], -1.0);
707 <        fcross(vc1, v1, v2);
708 <        ok1 = normalize(vc1) != 0.0;
709 <        fvsum(v1, vlist[p2i[0]], vlist[p3i[0]], -1.0);
710 <        fvsum(v2, vlist[p1i[0]], vlist[p3i[0]], -1.0);
711 <        fcross(vc2, v1, v2);
712 <        ok2 = normalize(vc2) != 0.0;
713 <        if (!(ok1 | ok2))
714 <                return(-1);
715 <                                        /* compute normal interpolation */
716 <        axis = norminterp(norm, p0i, p1i, p2i, p3i);
717 <
718 <                                        /* put out quadrilateral? */
719 <        if (ok1 & ok2 && fabs(fdot(vc1,vc2)) >= 1.0-FTINY) {
720 <                printf("\n%s ", mod);
721 <                if (axis != -1) {
722 <                        printf("texfunc %s\n", TEXNAME);
723 <                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
724 <                        printf("0\n13\t%d\n", axis);
725 <                        pvect(norm[0]);
726 <                        pvect(norm[1]);
727 <                        pvect(norm[2]);
728 <                        fvsum(v1, norm[3], vc1, -0.5);
729 <                        fvsum(v1, v1, vc2, -0.5);
730 <                        pvect(v1);
731 <                        printf("\n%s ", TEXNAME);
732 <                }
733 <                printf("polygon %s.%d\n", name, faceno);
734 <                printf("0\n0\n12\n");
735 <                pvect(vlist[p0i[0]]);
736 <                pvect(vlist[p1i[0]]);
737 <                pvect(vlist[p3i[0]]);
738 <                pvect(vlist[p2i[0]]);
739 <                return(1);
740 <        }
741 <                                        /* put out triangles? */
742 <        if (ok1) {
743 <                printf("\n%s ", mod);
744 <                if (axis != -1) {
745 <                        printf("texfunc %s\n", TEXNAME);
746 <                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
747 <                        printf("0\n13\t%d\n", axis);
748 <                        pvect(norm[0]);
749 <                        pvect(norm[1]);
750 <                        pvect(norm[2]);
751 <                        fvsum(v1, norm[3], vc1, -1.0);
752 <                        pvect(v1);
753 <                        printf("\n%s ", TEXNAME);
754 <                }
755 <                printf("polygon %s.%da\n", name, faceno);
756 <                printf("0\n0\n9\n");
757 <                pvect(vlist[p0i[0]]);
758 <                pvect(vlist[p1i[0]]);
759 <                pvect(vlist[p2i[0]]);
760 <        }
761 <        if (ok2) {
762 <                printf("\n%s ", mod);
763 <                if (axis != -1) {
764 <                        printf("texfunc %s\n", TEXNAME);
765 <                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
766 <                        printf("0\n13\t%d\n", axis);
767 <                        pvect(norm[0]);
768 <                        pvect(norm[1]);
769 <                        pvect(norm[2]);
770 <                        fvsum(v2, norm[3], vc2, -1.0);
771 <                        pvect(v2);
772 <                        printf("\n%s ", TEXNAME);
773 <                }
774 <                printf("polygon %s.%db\n", name, faceno);
775 <                printf("0\n0\n9\n");
776 <                pvect(vlist[p2i[0]]);
777 <                pvect(vlist[p1i[0]]);
778 <                pvect(vlist[p3i[0]]);
779 <        }
780 <        return(1);
781 < }
782 <
783 <
784 < int
785 < norminterp(resmat, p0i, p1i, p2i, p3i)  /* compute normal interpolation */
786 < register FVECT  resmat[4];
787 < register VNDX  p0i, p1i, p2i, p3i;
788 < {
789 < #define u  ((ax+1)%3)
790 < #define v  ((ax+2)%3)
791 <
792 <        register int  ax;
793 <        MAT4  eqnmat;
794 <        FVECT  v1;
795 <        register int  i, j;
796 <
797 < #ifdef TEXMAPS
798 <        /* also check for texture indices */
799 < #endif
800 <        if (flatten || !(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0))
801 <                return(-1);
802 <                                        /* find dominant axis */
803 <        VCOPY(v1, vnlist[p0i[2]]);
804 <        fvsum(v1, v1, vnlist[p1i[2]], 1.0);
805 <        fvsum(v1, v1, vnlist[p2i[2]], 1.0);
806 <        fvsum(v1, v1, vnlist[p3i[2]], 1.0);
807 <        ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1;
808 <        ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2;
809 <                                        /* assign equation matrix */
810 <        eqnmat[0][0] = vlist[p0i[0]][u]*vlist[p0i[0]][v];
811 <        eqnmat[0][1] = vlist[p0i[0]][u];
812 <        eqnmat[0][2] = vlist[p0i[0]][v];
813 <        eqnmat[0][3] = 1.0;
814 <        eqnmat[1][0] = vlist[p1i[0]][u]*vlist[p1i[0]][v];
815 <        eqnmat[1][1] = vlist[p1i[0]][u];
816 <        eqnmat[1][2] = vlist[p1i[0]][v];
817 <        eqnmat[1][3] = 1.0;
818 <        eqnmat[2][0] = vlist[p2i[0]][u]*vlist[p2i[0]][v];
819 <        eqnmat[2][1] = vlist[p2i[0]][u];
820 <        eqnmat[2][2] = vlist[p2i[0]][v];
821 <        eqnmat[2][3] = 1.0;
822 <        eqnmat[3][0] = vlist[p3i[0]][u]*vlist[p3i[0]][v];
823 <        eqnmat[3][1] = vlist[p3i[0]][u];
824 <        eqnmat[3][2] = vlist[p3i[0]][v];
825 <        eqnmat[3][3] = 1.0;
826 <                                        /* invert matrix (solve system) */
827 <        if (!invmat4(eqnmat, eqnmat))
828 <                return(-1);                     /* no solution */
829 <                                        /* compute result matrix */
830 <        for (j = 0; j < 4; j++)
831 <                for (i = 0; i < 3; i++)
832 <                        resmat[j][i] =  eqnmat[j][0]*vnlist[p0i[2]][i] +
833 <                                        eqnmat[j][1]*vnlist[p1i[2]][i] +
834 <                                        eqnmat[j][2]*vnlist[p2i[2]][i] +
835 <                                        eqnmat[j][3]*vnlist[p3i[2]][i];
836 < #ifdef TEXMAPS
837 <        /* compute result matrix for texture indices */
838 < #endif
839 <        return(ax);
840 <
841 < #undef u
842 < #undef v
677 >        printf("\t%d\n", bcm->ax);
678 >        printf("%14.8f %14.8f %14.8f\n",
679 >                                bcm->tm[0][0], bcm->tm[0][1], bcm->tm[0][2]);
680 >        printf("%14.8f %14.8f %14.8f\n",
681 >                                bcm->tm[1][0], bcm->tm[1][1], bcm->tm[1][2]);
682   }
683  
684  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines