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.14 by greg, Wed Apr 7 03:02:00 2021 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 30 | Line 31 | findName(const char *nm, const char **nmlist, int n)
31   void
32   clearSelection(Scene *sc, int set)
33   {
34 <        Face    *f;
34 >        Face    *f;
35          
36          for (f = sc->flist; f != NULL; f = f->next)
37                  if (set)
# Line 43 | Line 44 | clearSelection(Scene *sc, int set)
44   void
45   selectGroup(Scene *sc, const char *gname, int invert)
46   {
47 <        int     gid = findName(gname, (const char **)sc->grpname, sc->ngrps);
48 <        Face    *f;
47 >        int     gid = getGroupID(sc, gname);
48 >        Face    *f;
49  
50          if (gid < 0)
51                  return;
# Line 61 | Line 62 | selectGroup(Scene *sc, const char *gname, int invert)
62   void
63   selectMaterial(Scene *sc, const char *mname, int invert)
64   {
65 <        int     mid = findName(mname, (const char **)sc->matname, sc->nmats);
66 <        Face    *f;
65 >        int     mid = getMaterialID(sc, mname);
66 >        Face    *f;
67          
68          if (mid < 0)
69                  return;
# Line 79 | Line 80 | selectMaterial(Scene *sc, const char *mname, int inver
80   void
81   invertSelection(Scene *sc)
82   {
83 <        Face    *f;
83 >        Face    *f;
84          
85          for (f = sc->flist; f != NULL; f = f->next)
86                  f->flags ^= FACE_SELECTED;
# Line 89 | Line 90 | invertSelection(Scene *sc)
90   int
91   numberSelected(Scene *sc)
92   {
93 <        int     nsel = 0;
94 <        Face    *f;
93 >        int     nsel = 0;
94 >        Face    *f;
95          
96          for (f = sc->flist; f != NULL; f = f->next)
97                  nsel += ((f->flags & FACE_SELECTED) != 0);
# Line 102 | Line 103 | int
103   foreachFace(Scene *sc, int (*cb)(Scene *, Face *, void *),
104                          int flreq, int flexc, void *c_data)
105   {
106 <        int     fltest = flreq | flexc;
107 <        int     sum = 0;
108 <        Face    *f;
106 >        const int       fltest = flreq | flexc;
107 >        int             sum = 0;
108 >        Face            *f;
109          
110          if ((sc == NULL) | (cb == NULL))
111                  return(0);
# Line 122 | Line 123 | foreachFace(Scene *sc, int (*cb)(Scene *, Face *, void
123   static int
124   remFaceTexture(Scene *sc, Face *f, void *dummy)
125   {
126 <        int     hadTexture = 0;
127 <        int     i;
126 >        int     hadTexture = 0;
127 >        int     i;
128          
129          for (i = f->nv; i-- > 0; ) {
130                  if (f->v[i].tid < 0)
# Line 145 | Line 146 | removeTexture(Scene *sc, int flreq, int flexc)
146   static int
147   remFaceNormal(Scene *sc, Face *f, void *dummy)
148   {
149 <        int     hadNormal = 0;
150 <        int     i;
149 >        int     hadNormal = 0;
150 >        int     i;
151          
152          for (i = f->nv; i-- > 0; ) {
153                  if (f->v[i].nid < 0)
# Line 168 | Line 169 | removeNormals(Scene *sc, int flreq, int flexc)
169   static int
170   chngFaceGroup(Scene *sc, Face *f, void *ptr)
171   {
172 <        int     grp = *(int *)ptr;
172 >        int     grp = *(int *)ptr;
173 >
174          if (f->grp == grp)
175                  return(0);
176          f->grp = grp;
# Line 179 | Line 181 | chngFaceGroup(Scene *sc, Face *f, void *ptr)
181   int
182   changeGroup(Scene *sc, const char *gname, int flreq, int flexc)
183   {
184 <        int     grp = findName(gname, (const char **)sc->grpname, sc->ngrps);
184 >        int     grp = getGroupID(sc, gname);
185 >
186          if (grp < 0) {
187                  sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps);
188                  sc->grpname[grp=sc->ngrps++] = savqstr((char *)gname);
# Line 191 | Line 194 | changeGroup(Scene *sc, const char *gname, int flreq, i
194   static int
195   chngFaceMaterial(Scene *sc, Face *f, void *ptr)
196   {
197 <        int     mat = *(int *)ptr;
197 >        int     mat = *(int *)ptr;
198 >
199          if (f->mat == mat)
200                  return(0);
201          f->mat = mat;
# Line 202 | Line 206 | chngFaceMaterial(Scene *sc, Face *f, void *ptr)
206   int
207   changeMaterial(Scene *sc, const char *mname, int flreq, int flexc)
208   {
209 <        int     mat = findName(mname, (const char **)sc->matname, sc->nmats);
209 >        int     mat = getMaterialID(sc, mname);
210 >
211          if (mat < 0) {
212                  sc->matname = chunk_alloc(char *, sc->matname, sc->nmats);
213                  sc->matname[mat=sc->nmats++] = savqstr((char *)mname);
# Line 247 | Line 252 | norm_diff(const Normal n1, const Normal n2, double eps
252   static int
253   replace_vertex(Scene *sc, int prev, int repl, double eps)
254   {
255 <        int     repl_tex[10];
256 <        int     repl_norm[10];
257 <        Face    *f, *flast=NULL;
258 <        int     i, j=0;
255 >        int     repl_tex[10];
256 >        int     repl_norm[10];
257 >        Face    *f, *flast=NULL;
258 >        int     i, j=0;
259                                          /* check to see if it's even used */
260          if (sc->vert[prev].vflist == NULL)
261                  return(0);
# Line 258 | Line 263 | replace_vertex(Scene *sc, int prev, int repl, double e
263          repl_tex[0] = -1;
264          for (f = sc->vert[repl].vflist; f != NULL; f = f->v[j].fnext) {
265                                          /* make sure prev isn't in there */
266 <                for (j = 0; j < f->nv; j++)
266 >                for (j = f->nv; j-- > 0; )
267                          if (f->v[j].vid == prev)
268                                  return(0);
269 <                for (j = 0; j < f->nv; j++)
269 >                for (j = f->nv; j-- > 0; )
270                          if (f->v[j].vid == repl)
271                                  break;
272 <                if (j >= f->nv)
272 >                if (j < 0)
273                          goto linkerr;
274                  if (f->v[j].tid < 0)
275                          continue;
# Line 289 | Line 294 | replace_vertex(Scene *sc, int prev, int repl, double e
294                                          /* get replacement normals */
295          repl_norm[0] = -1;
296          for (f = sc->vert[repl].vflist; f != NULL; f = f->v[j].fnext) {
297 <                for (j = 0; j < f->nv; j++)
297 >                for (j = f->nv; j-- > 0; )
298                          if (f->v[j].vid == repl)
299                                  break;
300                  if (f->v[j].nid < 0)
# Line 314 | Line 319 | replace_vertex(Scene *sc, int prev, int repl, double e
319          }
320                                          /* replace occurrences of vertex */
321          for (f = sc->vert[prev].vflist; f != NULL; f = f->v[j].fnext) {
322 <                for (j = 0; j < f->nv; j++)
322 >                for (j = f->nv; j-- > 0; )
323                          if (f->v[j].vid == prev)
324                                  break;
325 <                if (j >= f->nv)
325 >                if (j < 0)
326                          goto linkerr;
327                  /* XXX doesn't allow for multiple references to prev in face */
328                  f->v[j].vid = repl;     /* replace vertex itself */
329 <                if (faceArea(sc, f, NULL) <= FTINY)
329 >                if (faceArea(sc, f, NULL) <= FTINY*FTINY)
330                          f->flags |= FACE_DEGENERATE;
331                  if (f->v[j].tid >= 0)   /* replace texture if appropriate */
332                          for (i = 0; repl_tex[i] >= 0; i++) {
# Line 359 | Line 364 | linkerr:
364   int
365   coalesceVertices(Scene *sc, double eps)
366   {
367 <        int     nelim = 0;
368 <        LUTAB   myLookup;
369 <        LUENT   *le;
370 <        char    vertfmt[32], vertbuf[64];
371 <        double  d;
372 <        int     i;
367 >        int     nelim = 0;
368 >        LUTAB   myLookup;
369 >        LUENT   *le;
370 >        char    vertfmt[32], vertbuf[64];
371 >        double  d;
372 >        int     i;
373          
374          if (eps >= 1.)
375                  return(0);
# Line 411 | Line 416 | coalesceVertices(Scene *sc, double eps)
416   int
417   findDuplicateFaces(Scene *sc)
418   {
419 <        int     nchecked = 0;
420 <        int     nfound = 0;
421 <        Face    *f, *f1;
422 <        int     vid;
423 <        int     i, j;
419 >        int     nchecked = 0;
420 >        int     nfound = 0;
421 >        Face    *f, *f1;
422 >        int     vid;
423 >        int     i, j;
424                                                  /* start fresh */
425          for (f = sc->flist; f != NULL; f = f->next)
426                  f->flags &= ~FACE_DUPLICATE;
# Line 431 | Line 436 | findDuplicateFaces(Scene *sc)
436                                                  /* look for duplicates */
437                  for (f1 = sc->vert[vid].vflist; f1 != NULL;
438                                          f1 = f1->v[j].fnext) {
439 <                        for (j = 0; j < f1->nv; j++)
439 >                        for (j = f1->nv; j-- > 0; )
440                                  if (f1->v[j].vid == vid)
441                                          break;
442 <                        if (j >= f1->nv)
442 >                        if (j < 0)
443                                  break;          /* missing link! */
444                          if (f1 == f)
445                                  continue;       /* shouldn't happen */
# Line 459 | Line 464 | findDuplicateFaces(Scene *sc)
464                          ++nfound;
465                  }
466          }
467 +        if (verbose)
468 +                fprintf(stderr, "Found %d duplicate faces\n", nfound);
469          return(nfound);
470   }
471  
# Line 466 | Line 473 | findDuplicateFaces(Scene *sc)
473   int
474   deleteFaces(Scene *sc, int flreq, int flexc)
475   {
476 <        int     fltest = flreq | flexc;
477 <        int     orig_nfaces = sc->nfaces;
478 <        Face    fhead;
479 <        Face    *f, *ftst;
476 >        const int       fltest = flreq | flexc;
477 >        int             orig_nfaces = sc->nfaces;
478 >        Face            fhead;
479 >        Face            *f, *ftst;
480          
481          fhead.next = sc->flist;
482          f = &fhead;
# Line 485 | Line 492 | deleteFaces(Scene *sc, int flreq, int flexc)
492                                          continue;
493                                  }
494                                  while (vf != NULL) {
495 <                                        for (j = 0; j < vf->nv; j++)
495 >                                        for (j = vf->nv; j-- > 0; )
496                                                  if (vf->v[j].vid == vid)
497                                                          break;
498 <                                        if (j >= vf->nv)
498 >                                        if (j < 0)
499                                                  break;  /* error */
500                                          if (vf->v[j].fnext == ftst) {
501                                                  vf->v[j].fnext =
# Line 546 | Line 553 | addComment(Scene *sc, const char *comment)
553          sc->descr[sc->ndescr++] = savqstr((char *)comment);
554   }
555  
556 + /* Find index for comment containing the given string (starting from n) */
557 + int
558 + findComment(Scene *sc, const char *match, int n)
559 + {
560 +        if (n >= sc->ndescr)
561 +                return(-1);
562 +        n *= (n > 0);
563 +        while (n < sc->ndescr)
564 +                if (strstr(sc->descr[n], match) != NULL)
565 +                        return(n);
566 +        return(-1);
567 + }
568 +
569   /* Clear comments */
570   void
571   clearComments(Scene *sc)
# Line 572 | Line 592 | add2facelist(Scene *sc, Face *f, int i)
592          for ( ; ; ) {                           /* else find position */
593                  if (fp == f)
594                          return;                 /* already in list */
595 <                for (j = 0; j < fp->nv; j++)
595 >                for (j = fp->nv; j-- > 0; )
596                          if (fp->v[j].vid == vid)
597                                  break;
598 <                if (j >= fp->nv)
598 >                if (j < 0)
599                          error(CONSISTENCY, "Link error in add2facelist()");
600                  if (fp->v[j].fnext == NULL)
601                          break;                  /* reached the end */
# Line 588 | Line 608 | add2facelist(Scene *sc, Face *f, int i)
608   Scene *
609   newScene(void)
610   {
611 <        Scene   *sc = (Scene *)ecalloc(1, sizeof(Scene));
611 >        Scene   *sc = (Scene *)ecalloc(1, sizeof(Scene));
612                                                  /* default group & material */
613          sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps);
614          sc->grpname[sc->ngrps++] = savqstr("DEFAULT_GROUP");
# Line 598 | Line 618 | newScene(void)
618          return(sc);
619   }
620  
621 < /* Duplicate a scene */
621 > /* Add a vertex to a scene */
622 > int
623 > addVertex(Scene *sc, double x, double y, double z)
624 > {
625 >        sc->vert = chunk_alloc(Vertex, sc->vert, sc->nverts);
626 >        sc->vert[sc->nverts].p[0] = x;
627 >        sc->vert[sc->nverts].p[1] = y;
628 >        sc->vert[sc->nverts].p[2] = z;
629 >        sc->vert[sc->nverts].vflist = NULL;
630 >        return(sc->nverts++);
631 > }
632 >
633 > /* Add a texture coordinate to a scene */
634 > int
635 > addTexture(Scene *sc, double u, double v)
636 > {
637 >        sc->tex = chunk_alloc(TexCoord, sc->tex, sc->ntex);
638 >        sc->tex[sc->ntex].u = u;
639 >        sc->tex[sc->ntex].v = v;
640 >        return(sc->ntex++);
641 > }
642 >
643 > /* Add a surface normal to a scene */
644 > int
645 > addNormal(Scene *sc, double xn, double yn, double zn)
646 > {
647 >        FVECT   nrm;
648 >
649 >        nrm[0] = xn; nrm[1] = yn; nrm[2] = zn;
650 >        if (normalize(nrm) == .0)
651 >                return(-1);
652 >        sc->norm = chunk_alloc(Normal, sc->norm, sc->nnorms);
653 >        VCOPY(sc->norm[sc->nnorms], nrm);
654 >        return(sc->nnorms++);
655 > }
656 >
657 > /* Set current (last) group */
658 > void
659 > setGroup(Scene *sc, const char *nm)
660 > {
661 >        sc->lastgrp = getGroupID(sc, nm);
662 >        if (sc->lastgrp >= 0)
663 >                return;
664 >        sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps);
665 >        sc->grpname[sc->lastgrp=sc->ngrps++] = savqstr((char *)nm);
666 > }
667 >
668 > /* Set current (last) material */
669 > void
670 > setMaterial(Scene *sc, const char *nm)
671 > {
672 >        sc->lastmat = getMaterialID(sc, nm);
673 >        if (sc->lastmat >= 0)
674 >                return;
675 >        sc->matname = chunk_alloc(char *, sc->matname, sc->nmats);
676 >        sc->matname[sc->lastmat=sc->nmats++] = savqstr((char *)nm);
677 > }
678 >
679 > /* Add new face to a scene */
680 > Face *
681 > addFace(Scene *sc, VNDX vid[], int nv)
682 > {
683 >        Face    *f;
684 >        int     i;
685 >        
686 >        if (nv < 3)
687 >                return(NULL);
688 >        f = (Face *)emalloc(sizeof(Face)+sizeof(VertEnt)*(nv-3));
689 >        f->flags = 0;
690 >        f->nv = nv;
691 >        f->grp = sc->lastgrp;
692 >        f->mat = sc->lastmat;
693 >        for (i = 0; i < nv; i++) {              /* add each vertex */
694 >                int     j;
695 >                f->v[i].vid = vid[i][0];
696 >                f->v[i].tid = vid[i][1];
697 >                f->v[i].nid = vid[i][2];
698 >                f->v[i].fnext = NULL;
699 >                for (j = i; j-- > 0; )
700 >                        if (f->v[j].vid == vid[i][0])
701 >                                break;
702 >                if (j < 0) {                    /* first occurrence? */
703 >                        f->v[i].fnext = sc->vert[vid[i][0]].vflist;
704 >                        sc->vert[vid[i][0]].vflist = f;
705 >                } else if (nv == 3)             /* degenerate triangle? */
706 >                        f->flags |= FACE_DEGENERATE;
707 >        }
708 >        f->next = sc->flist;                    /* push onto face list */
709 >        sc->flist = f;
710 >        sc->nfaces++;
711 >                                                /* check face area */
712 >        if (!(f->flags & FACE_DEGENERATE) && faceArea(sc, f, NULL) <= FTINY*FTINY)
713 >                f->flags |= FACE_DEGENERATE;
714 >        return(f);
715 > }
716 >
717 > /* Duplicate a scene, optionally selecting faces */
718   Scene *
719 < dupScene(const Scene *osc)
719 > dupScene(const Scene *osc, int flreq, int flexc)
720   {
721 +        int             fltest = flreq | flexc;
722          Scene           *sc;
723          const Face      *fo;
724          Face            *f;
# Line 650 | Line 767 | dupScene(const Scene *osc)
767                  sc->nnorms = osc->nnorms;
768          }
769          for (fo = osc->flist; fo != NULL; fo = fo->next) {
770 +                if ((fo->flags & fltest) != flreq)
771 +                        continue;
772                  f = (Face *)emalloc(sizeof(Face) +
773                                  sizeof(VertEnt)*(fo->nv-3));
774                  memcpy((void *)f, (const void *)fo, sizeof(Face) +
# Line 660 | Line 779 | dupScene(const Scene *osc)
779                  sc->flist = f;
780                  sc->nfaces++;
781          }
782 +        deleteUnreferenced(sc);         /* jetsam */
783          return(sc);
784 + }
785 +
786 + /* Add one scene to another, not checking for redundancies */
787 + int
788 + addScene(Scene *scdst, const Scene *scsrc)
789 + {
790 +        VNDX            my_vlist[4];
791 +        int             *vert_map = NULL;
792 +        int             *norm_map = NULL;
793 +        int             *tex_map = NULL;
794 +        VNDX            *vlist = my_vlist;
795 +        int             vllen = sizeof(my_vlist)/sizeof(VNDX);
796 +        int             cur_mat = 0;
797 +        int             cur_grp = 0;
798 +        int             fcnt = 0;
799 +        const Face      *f;
800 +        int             i;
801 +
802 +        if ((scdst == NULL) | (scsrc == NULL))
803 +                return(-1);
804 +        if (scsrc->nfaces <= 0)
805 +                return(0);
806 +                                        /* map vertices */
807 +        vert_map = (int *)emalloc(sizeof(int *)*scsrc->nverts);
808 +        for (i = 0; i < scsrc->nverts; i++) {
809 +                const Vertex    *v = scsrc->vert + i;
810 +                if (v->vflist == NULL) {
811 +                        vert_map[i] = -1;
812 +                        continue;
813 +                }
814 +                vert_map[i] = addVertex(scdst, v->p[0], v->p[1], v->p[2]);
815 +        }
816 +        if (scsrc->ntex > 0)            /* map texture coords */
817 +                tex_map = (int *)emalloc(sizeof(int *)*scsrc->ntex);
818 +        for (i = 0; i < scsrc->ntex; i++) {
819 +                const TexCoord  *t = scsrc->tex + i;
820 +                tex_map[i] = addTexture(scdst, t->u, t->v);
821 +        }
822 +        if (scsrc->nnorms > 0)          /* map normals */
823 +                norm_map = (int *)emalloc(sizeof(int *)*scsrc->nnorms);
824 +        for (i = 0; i < scsrc->nnorms; i++) {
825 +                const float     *n = scsrc->norm[i];
826 +                norm_map[i] = addNormal(scdst, n[0], n[1], n[2]);
827 +        }
828 +                                        /* add faces */
829 +        scdst->lastgrp = scdst->lastmat = 0;
830 +        for (f = scsrc->flist; f != NULL; f = f->next) {
831 +                if (f->grp != cur_grp)
832 +                        setGroup(scdst, scsrc->grpname[cur_grp = f->grp]);
833 +                if (f->mat != cur_mat)
834 +                        setMaterial(scdst, scsrc->matname[cur_mat = f->mat]);
835 +                if (f->nv > vllen) {
836 +                        if (vlist == my_vlist)
837 +                                vlist = (VNDX *)emalloc(
838 +                                                sizeof(VNDX)*(vllen = f->nv));
839 +                        else
840 +                                vlist = (VNDX *)erealloc((char *)vlist,
841 +                                                sizeof(VNDX)*(vllen = f->nv));
842 +                }
843 +                memset(vlist, 0xff, sizeof(VNDX)*f->nv);
844 +                for (i = f->nv; i-- > 0; ) {
845 +                        if (f->v[i].vid >= 0)
846 +                                vlist[i][0] = vert_map[f->v[i].vid];
847 +                        if (f->v[i].tid >= 0)
848 +                                vlist[i][1] = tex_map[f->v[i].tid];
849 +                        if (f->v[i].nid >= 0)
850 +                                vlist[i][2] = norm_map[f->v[i].nid];
851 +                }
852 +                fcnt += (addFace(scdst, vlist, f->nv) != NULL);
853 +        }
854 +                                        /* clean up */
855 +        if (vlist != my_vlist) efree((char *)vlist);
856 +        if (norm_map != NULL) efree((char *)norm_map);
857 +        if (tex_map != NULL) efree((char *)tex_map);
858 +        efree((char *)vert_map);
859 +        return(fcnt);
860 + }
861 +
862 + #define MAXAC   100
863 +
864 + /* Transform entire scene */
865 + int
866 + xfScene(Scene *sc, int xac, char *xav[])
867 + {
868 +        char    comm[24+MAXAC*8];
869 +        char    *cp;
870 +        XF      myxf;
871 +        FVECT   vec;
872 +        int     i;
873 +
874 +        if ((sc == NULL) | (xac <= 0) | (xav == NULL))
875 +                return(0);
876 +                                        /* compute matrix */
877 +        if (xf(&myxf, xac, xav) < xac)
878 +                return(0);
879 +                                        /* transform vertices */
880 +        for (i = 0; i < sc->nverts; i++) {
881 +                VCOPY(vec, sc->vert[i].p);
882 +                multp3(vec, vec, myxf.xfm);
883 +                VCOPY(sc->vert[i].p, vec);
884 +        }
885 +                                        /* transform normals */
886 +        for (i = 0; i < sc->nnorms; i++) {
887 +                VCOPY(vec, sc->norm[i]);
888 +                multv3(vec, vec, myxf.xfm);
889 +                vec[0] /= myxf.sca; vec[1] /= myxf.sca; vec[2] /= myxf.sca;
890 +                VCOPY(sc->norm[i], vec);
891 +        }
892 +                                        /* add comment */
893 +        cp = strcpy(comm, "Transformed by:");
894 +        for (i = 0; i < xac; i++) {
895 +                while (*cp) cp++;
896 +                *cp++ = ' ';
897 +                strcpy(cp, xav[i]);
898 +        }
899 +        addComment(sc, comm);
900 +        return(xac);                    /* all done */
901 + }
902 +
903 + /* Ditto, using transform string rather than pre-parsed words */
904 + int
905 + xfmScene(Scene *sc, const char *xfm)
906 + {
907 +        char    *xav[MAXAC+1];
908 +        int     xac, i;
909 +
910 +        if ((sc == NULL) | (xfm == NULL))
911 +                return(0);
912 +                                        /* skip spaces at beginning */
913 +        while (isspace(*xfm))
914 +                xfm++;
915 +        if (!*xfm)
916 +                return(0);
917 +                                        /* parse string into words */
918 +        xav[0] = strcpy((char *)emalloc(strlen(xfm)+1), xfm);
919 +        xac = 1; i = 0;
920 +        for ( ; ; ) {
921 +                while (!isspace(xfm[++i]))
922 +                        if (!xfm[i])
923 +                                break;
924 +                while (isspace(xfm[i]))
925 +                        xav[0][i++] = '\0';
926 +                if (!xfm[i])
927 +                        break;
928 +                if (xac >= MAXAC-1) {
929 +                        free(xav[0]);
930 +                        return(0);
931 +                }
932 +                xav[xac++] = xav[0] + i;
933 +        }
934 +        xav[xac] = NULL;
935 +        i = xfScene(sc, xac, xav);
936 +        efree((char *)xav[0]);
937 +        return(i);
938 + }
939 + #undef MAXAC
940 +
941 + /* Delete unreferenced vertices, normals, texture coords */
942 + void
943 + deleteUnreferenced(Scene *sc)
944 + {
945 +        int     *vmap;
946 +        Face    *f;
947 +        int     nused, i;
948 +                                                /* allocate index map */
949 +        if (!sc->nverts)
950 +                return;
951 +        i = sc->nverts;
952 +        if (sc->ntex > i)
953 +                i = sc->ntex;
954 +        if (sc->nnorms > i)
955 +                i = sc->nnorms;
956 +        vmap = (int *)emalloc(sizeof(int)*i);
957 +                                                /* remap positions */
958 +        for (i = nused = 0; i < sc->nverts; i++) {
959 +                if (sc->vert[i].vflist == NULL) {
960 +                        vmap[i] = -1;
961 +                        continue;
962 +                }
963 +                if (nused != i)
964 +                        sc->vert[nused] = sc->vert[i];
965 +                vmap[i] = nused++;
966 +        }
967 +        if (nused == sc->nverts)
968 +                goto skip_pos;
969 +        sc->vert = (Vertex *)erealloc((char *)sc->vert,
970 +                                sizeof(Vertex)*(nused+(CHUNKSIZ-1)));
971 +        sc->nverts = nused;
972 +        for (f = sc->flist; f != NULL; f = f->next)
973 +                for (i = f->nv; i--; )
974 +                        if ((f->v[i].vid = vmap[f->v[i].vid]) < 0)
975 +                                error(CONSISTENCY,
976 +                                        "Link error in del_unref_verts()");
977 + skip_pos:
978 +                                                /* remap texture coord's */
979 +        if (!sc->ntex)
980 +                goto skip_tex;
981 +        memset((void *)vmap, 0, sizeof(int)*sc->ntex);
982 +        for (f = sc->flist; f != NULL; f = f->next)
983 +                for (i = f->nv; i--; )
984 +                        if (f->v[i].tid >= 0)
985 +                                vmap[f->v[i].tid] = 1;
986 +        for (i = nused = 0; i < sc->ntex; i++) {
987 +                if (!vmap[i])
988 +                        continue;
989 +                if (nused != i)
990 +                        sc->tex[nused] = sc->tex[i];
991 +                vmap[i] = nused++;
992 +        }
993 +        if (nused == sc->ntex)
994 +                goto skip_tex;
995 +        sc->tex = (TexCoord *)erealloc((char *)sc->tex,
996 +                                sizeof(TexCoord)*(nused+(CHUNKSIZ-1)));
997 +        sc->ntex = nused;
998 +        for (f = sc->flist; f != NULL; f = f->next)
999 +                for (i = f->nv; i--; )
1000 +                        if (f->v[i].tid >= 0)
1001 +                                f->v[i].tid = vmap[f->v[i].tid];
1002 + skip_tex:
1003 +                                                /* remap normals */
1004 +        if (!sc->nnorms)
1005 +                goto skip_norms;
1006 +        memset((void *)vmap, 0, sizeof(int)*sc->nnorms);
1007 +        for (f = sc->flist; f != NULL; f = f->next)
1008 +                for (i = f->nv; i--; )
1009 +                        if (f->v[i].nid >= 0)
1010 +                                vmap[f->v[i].nid] = 1;
1011 +        for (i = nused = 0; i < sc->nnorms; i++) {
1012 +                if (!vmap[i])
1013 +                        continue;
1014 +                if (nused != i)
1015 +                        memcpy((void *)sc->norm[nused],
1016 +                                        (void *)sc->norm[i],
1017 +                                        sizeof(Normal));
1018 +                vmap[i] = nused++;
1019 +        }
1020 +        if (nused == sc->nnorms)
1021 +                goto skip_norms;
1022 +        sc->norm = (Normal *)erealloc((char *)sc->norm,
1023 +                                sizeof(Normal)*(nused+(CHUNKSIZ-1)));
1024 +        sc->nnorms = nused;
1025 +        for (f = sc->flist; f != NULL; f = f->next)
1026 +                for (i = f->nv; i--; )
1027 +                        if (f->v[i].nid >= 0)
1028 +                                f->v[i].nid = vmap[f->v[i].nid];
1029 + skip_norms:
1030 +                                                /* clean up */
1031 +        efree((char *)vmap);
1032   }
1033  
1034   /* Free a scene */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines