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

Comparing ray/src/common/objutil.c (file contents):
Revision 2.1 by greg, Mon Mar 30 18:28:35 2020 UTC vs.
Revision 2.8 by greg, Tue Jun 23 19:29:40 2020 UTC

# Line 8 | Line 8 | static const char RCSid[] = "$Id$";
8   */
9  
10   #include <stdlib.h>
11 + #include <ctype.h>
12   #include "rtio.h"
13   #include "rtmath.h"
14   #include "rterror.h"
# Line 18 | Line 19 | static const char RCSid[] = "$Id$";
19   int
20   findName(const char *nm, const char **nmlist, int n)
21   {
22 <        register int    i;
22 >        int    i;
23          
24          for (i = n; i-- > 0; )
25                  if (!strcmp(nmlist[i], nm))
# Line 321 | Line 322 | replace_vertex(Scene *sc, int prev, int repl, double e
322                          goto linkerr;
323                  /* XXX doesn't allow for multiple references to prev in face */
324                  f->v[j].vid = repl;     /* replace vertex itself */
325 <                if (faceArea(sc, f, NULL) <= FTINY)
325 >                if (faceArea(sc, f, NULL) <= FTINY*FTINY)
326                          f->flags |= FACE_DEGENERATE;
327                  if (f->v[j].tid >= 0)   /* replace texture if appropriate */
328                          for (i = 0; repl_tex[i] >= 0; i++) {
# Line 546 | Line 547 | addComment(Scene *sc, const char *comment)
547          sc->descr[sc->ndescr++] = savqstr((char *)comment);
548   }
549  
550 + /* Find index for comment containing the given string (starting from n) */
551 + int
552 + findComment(Scene *sc, const char *match, int n)
553 + {
554 +        if (n >= sc->ndescr)
555 +                return(-1);
556 +        n *= (n > 0);
557 +        while (n < sc->ndescr)
558 +                if (strcasestr(sc->descr[n], match) != NULL)
559 +                        return(n);
560 +        return(-1);
561 + }
562 +
563   /* Clear comments */
564   void
565   clearComments(Scene *sc)
# Line 598 | Line 612 | newScene(void)
612          return(sc);
613   }
614  
615 + /* Add a vertex to a scene */
616 + int
617 + addVertex(Scene *sc, double x, double y, double z)
618 + {
619 +        sc->vert = chunk_alloc(Vertex, sc->vert, sc->nverts);
620 +        sc->vert[sc->nverts].p[0] = x;
621 +        sc->vert[sc->nverts].p[1] = y;
622 +        sc->vert[sc->nverts].p[2] = z;
623 +        sc->vert[sc->nverts].vflist = NULL;
624 +        return(sc->nverts++);
625 + }
626 +
627 + /* Add a texture coordinate to a scene */
628 + int
629 + addTexture(Scene *sc, double u, double v)
630 + {
631 +        sc->tex = chunk_alloc(TexCoord, sc->tex, sc->ntex);
632 +        sc->tex[sc->ntex].u = u;
633 +        sc->tex[sc->ntex].v = v;
634 +        return(sc->ntex++);
635 + }
636 +
637 + /* Add a surface normal to a scene */
638 + int
639 + addNormal(Scene *sc, double xn, double yn, double zn)
640 + {
641 +        FVECT   nrm;
642 +
643 +        nrm[0] = xn; nrm[1] = yn; nrm[2] = zn;
644 +        if (normalize(nrm) == .0)
645 +                return(-1);
646 +        sc->norm = chunk_alloc(Normal, sc->norm, sc->nnorms);
647 +        VCOPY(sc->norm[sc->nnorms], nrm);
648 +        return(sc->nnorms++);
649 + }
650 +
651 + /* Set current (last) group */
652 + void
653 + setGroup(Scene *sc, const char *nm)
654 + {
655 +        sc->lastgrp = findName(nm, (const char **)sc->grpname, sc->ngrps);
656 +        if (sc->lastgrp >= 0)
657 +                return;
658 +        sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps);
659 +        sc->grpname[sc->lastgrp=sc->ngrps++] = savqstr((char *)nm);
660 + }
661 +
662 + /* Set current (last) material */
663 + void
664 + setMaterial(Scene *sc, const char *nm)
665 + {
666 +        sc->lastmat = findName(nm, (const char **)sc->matname, sc->nmats);
667 +        if (sc->lastmat >= 0)
668 +                return;
669 +        sc->matname = chunk_alloc(char *, sc->matname, sc->nmats);
670 +        sc->matname[sc->lastmat=sc->nmats++] = savqstr((char *)nm);
671 + }
672 +
673 + /* Add new face to a scene */
674 + Face *
675 + addFace(Scene *sc, VNDX vid[], int nv)
676 + {
677 +        Face    *f;
678 +        int     i;
679 +        
680 +        if (nv < 3)
681 +                return(NULL);
682 +        f = (Face *)emalloc(sizeof(Face)+sizeof(VertEnt)*(nv-3));
683 +        f->flags = 0;
684 +        f->nv = nv;
685 +        f->grp = sc->lastgrp;
686 +        f->mat = sc->lastmat;
687 +        for (i = 0; i < nv; i++) {              /* add each vertex */
688 +                int     j;
689 +                f->v[i].vid = vid[i][0];
690 +                f->v[i].tid = vid[i][1];
691 +                f->v[i].nid = vid[i][2];
692 +                f->v[i].fnext = NULL;
693 +                for (j = i; j-- > 0; )
694 +                        if (f->v[j].vid == vid[i][0])
695 +                                break;
696 +                if (j < 0) {                    /* first occurrence? */
697 +                        f->v[i].fnext = sc->vert[vid[i][0]].vflist;
698 +                        sc->vert[vid[i][0]].vflist = f;
699 +                } else if (nv == 3)             /* degenerate triangle? */
700 +                        f->flags |= FACE_DEGENERATE;
701 +        }
702 +        f->next = sc->flist;                    /* push onto face list */
703 +        sc->flist = f;
704 +        sc->nfaces++;
705 +                                                /* check face area */
706 +        if (!(f->flags & FACE_DEGENERATE) && faceArea(sc, f, NULL) <= FTINY*FTINY)
707 +                f->flags |= FACE_DEGENERATE;
708 +        return(f);
709 + }
710 +
711   /* Duplicate a scene */
712   Scene *
713   dupScene(const Scene *osc)
# Line 662 | Line 772 | dupScene(const Scene *osc)
772          }
773          return(sc);
774   }
775 +
776 + #define MAXAC   100
777 +
778 + /* Transform entire scene */
779 + int
780 + xfScene(Scene *sc, int xac, char *xav[])
781 + {
782 +        char    comm[24+MAXAC*8];
783 +        char    *cp;
784 +        XF      myxf;
785 +        FVECT   vec;
786 +        int     i;
787 +
788 +        if ((sc == NULL) | (xac <= 0) | (xav == NULL))
789 +                return(0);
790 +                                        /* compute matrix */
791 +        if (xf(&myxf, xac, xav) < xac)
792 +                return(0);
793 +                                        /* transform vertices */
794 +        for (i = 0; i < sc->nverts; i++) {
795 +                VCOPY(vec, sc->vert[i].p);
796 +                multp3(vec, vec, myxf.xfm);
797 +                VCOPY(sc->vert[i].p, vec);
798 +        }
799 +                                        /* transform normals */
800 +        for (i = 0; i < sc->nnorms; i++) {
801 +                VCOPY(vec, sc->norm[i]);
802 +                multv3(vec, vec, myxf.xfm);
803 +                vec[0] /= myxf.sca; vec[1] /= myxf.sca; vec[2] /= myxf.sca;
804 +                VCOPY(sc->norm[i], vec);
805 +        }
806 +                                        /* add comment */
807 +        cp = strcpy(comm, "Transformed by:");
808 +        for (i = 0; i < xac; i++) {
809 +                while (*cp) cp++;
810 +                *cp++ = ' ';
811 +                strcpy(cp, xav[i]);
812 +        }
813 +        addComment(sc, comm);
814 +        return(xac);                    /* all done */
815 + }
816 +
817 + /* Ditto, using transform string rather than pre-parsed words */
818 + int
819 + xfmScene(Scene *sc, const char *xfm)
820 + {
821 +        char    *xav[MAXAC+1];
822 +        int     xac, i;
823 +
824 +        if ((sc == NULL) | (xfm == NULL))
825 +                return(0);
826 +                                        /* skip spaces at beginning */
827 +        while (isspace(*xfm))
828 +                xfm++;
829 +        if (!*xfm)
830 +                return(0);
831 +                                        /* parse string into words */
832 +        xav[0] = strcpy((char *)malloc(strlen(xfm)+1), xfm);
833 +        xac = 1; i = 0;
834 +        for ( ; ; ) {
835 +                while (!isspace(xfm[++i]))
836 +                        if (!xfm[i])
837 +                                break;
838 +                while (isspace(xfm[i]))
839 +                        xav[0][i++] = '\0';
840 +                if (!xfm[i])
841 +                        break;
842 +                if (xac >= MAXAC-1) {
843 +                        free(xav[0]);
844 +                        return(0);
845 +                }
846 +                xav[xac++] = xav[0] + i;
847 +        }
848 +        xav[xac] = NULL;
849 +        i = xfScene(sc, xac, xav);
850 +        free(xav[0]);
851 +        return(i);
852 + }
853 + #undef MAXAC
854  
855   /* Free a scene */
856   void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines