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.6 by greg, Sun Jun 14 03:04:25 2020 UTC vs.
Revision 2.18 by greg, Fri Apr 9 15:26:41 2021 UTC

# Line 19 | Line 19 | static const char RCSid[] = "$Id$";
19   int
20   findName(const char *nm, const char **nmlist, int n)
21   {
22 <        int    i;
22 >        int     i;
23          
24          for (i = n; i-- > 0; )
25                  if (!strcmp(nmlist[i], nm))
# Line 31 | 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 44 | 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 62 | 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 80 | 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 90 | 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 103 | 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 123 | 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 146 | 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 169 | 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 180 | 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 192 | 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 203 | 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 248 | 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 259 | 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 290 | 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 315 | 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 */
# Line 360 | 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 412 | 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 432 | 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 460 | 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 467 | 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 486 | 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 547 | 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 573 | 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 585 | Line 604 | add2facelist(Scene *sc, Face *f, int i)
604          fp->v[j].fnext = f;                     /* append new face */
605   }
606  
588 /* Allocate an empty scene */
589 Scene *
590 newScene(void)
591 {
592        Scene   *sc = (Scene *)ecalloc(1, sizeof(Scene));
593                                                /* default group & material */
594        sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps);
595        sc->grpname[sc->ngrps++] = savqstr("DEFAULT_GROUP");
596        sc->matname = chunk_alloc(char *, sc->matname, sc->nmats);
597        sc->matname[sc->nmats++] = savqstr("DEFAULT_MATERIAL");
598
599        return(sc);
600 }
601
607   /* Add a vertex to a scene */
608   int
609   addVertex(Scene *sc, double x, double y, double z)
# Line 625 | Line 630 | addTexture(Scene *sc, double u, double v)
630   int
631   addNormal(Scene *sc, double xn, double yn, double zn)
632   {
633 <        FVECT   nrm;
633 >        FVECT   nrm;
634  
635          nrm[0] = xn; nrm[1] = yn; nrm[2] = zn;
636          if (normalize(nrm) == .0)
# Line 639 | Line 644 | addNormal(Scene *sc, double xn, double yn, double zn)
644   void
645   setGroup(Scene *sc, const char *nm)
646   {
647 <        sc->lastgrp = findName(nm, (const char **)sc->grpname, sc->ngrps);
647 >        sc->lastgrp = getGroupID(sc, nm);
648          if (sc->lastgrp >= 0)
649                  return;
650          sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps);
# Line 650 | Line 655 | setGroup(Scene *sc, const char *nm)
655   void
656   setMaterial(Scene *sc, const char *nm)
657   {
658 <        sc->lastmat = findName(nm, (const char **)sc->matname, sc->nmats);
658 >        sc->lastmat = getMaterialID(sc, nm);
659          if (sc->lastmat >= 0)
660                  return;
661          sc->matname = chunk_alloc(char *, sc->matname, sc->nmats);
# Line 661 | Line 666 | setMaterial(Scene *sc, const char *nm)
666   Face *
667   addFace(Scene *sc, VNDX vid[], int nv)
668   {
669 <        Face    *f;
670 <        int     i;
669 >        Face    *f;
670 >        int     i;
671          
672          if (nv < 3)
673                  return(NULL);
# Line 695 | Line 700 | addFace(Scene *sc, VNDX vid[], int nv)
700          return(f);
701   }
702  
703 < /* Duplicate a scene */
703 > /* Callback for growBoundingBox() */
704 > static int
705 > addBBox(Scene *sc, Face *f, void *p)
706 > {
707 >        double  (*bbox)[3] = (double (*)[3])p;
708 >        int     i, j;
709 >
710 >        for (i = f->nv; i-- > 0; ) {
711 >                double  *p3 = sc->vert[f->v[i].vid].p;
712 >                for (j = 3; j--; ) {
713 >                        if (p3[j] < bbox[0][j])
714 >                                bbox[0][j] = p3[j];
715 >                        if (p3[j] > bbox[1][j])
716 >                                bbox[1][j] = p3[j];
717 >                }
718 >        }
719 >        return(1);
720 > }
721 >
722 > /* Expand bounding box min & max (initialize bbox to all zeroes) */
723 > int
724 > growBoundingBox(Scene *sc, double bbox[2][3], int flreq, int flexc)
725 > {
726 >        if (sc == NULL || sc->nfaces <= 0 || bbox == NULL)
727 >                return(0);
728 >
729 >        if (VABSEQ(bbox[0], bbox[1])) {         /* first run */
730 >                bbox[0][0] = bbox[0][1] = bbox[0][2] = FHUGE;
731 >                bbox[1][0] = bbox[1][1] = bbox[1][2] = -FHUGE;
732 >        }
733 >        return(foreachFace(sc, addBBox, flreq, flexc, bbox));
734 > }
735 >
736 > /* Allocate an empty scene */
737   Scene *
738 < dupScene(const Scene *osc)
738 > newScene(void)
739   {
740 +        Scene   *sc = (Scene *)ecalloc(1, sizeof(Scene));
741 +                                                /* default group & material */
742 +        sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps);
743 +        sc->grpname[sc->ngrps++] = savqstr("DEFAULT_GROUP");
744 +        sc->matname = chunk_alloc(char *, sc->matname, sc->nmats);
745 +        sc->matname[sc->nmats++] = savqstr("DEFAULT_MATERIAL");
746 +
747 +        return(sc);
748 + }
749 +
750 + /* Duplicate a scene, optionally selecting faces */
751 + Scene *
752 + dupScene(const Scene *osc, int flreq, int flexc)
753 + {
754 +        int             fltest = flreq | flexc;
755          Scene           *sc;
756          const Face      *fo;
757          Face            *f;
# Line 709 | Line 762 | dupScene(const Scene *osc)
762          sc = newScene();
763          for (i = 0; i < osc->ndescr; i++)
764                  addComment(sc, osc->descr[i]);
765 <        if (osc->ngrps) {
766 <                sc->grpname = (char **)emalloc(sizeof(char *) *
767 <                                                (osc->ngrps+(CHUNKSIZ-1)));
768 <                for (i = 0; i < osc->ngrps; i++)
765 >        if (osc->ngrps > 1) {
766 >                sc->grpname = (char **)erealloc((char *)sc->grpname,
767 >                                sizeof(char *) * (osc->ngrps+(CHUNKSIZ-1)));
768 >                for (i = 1; i < osc->ngrps; i++)
769                          sc->grpname[i] = savqstr(osc->grpname[i]);
770                  sc->ngrps = osc->ngrps;
771          }
772 <        if (osc->nmats) {
773 <                sc->matname = (char **)emalloc(sizeof(char *) *
774 <                                                (osc->nmats+(CHUNKSIZ-1)));
775 <                for (i = 0; i < osc->nmats; i++)
772 >        if (osc->nmats > 1) {
773 >                sc->matname = (char **)erealloc((char *)sc->matname,
774 >                                sizeof(char *) * (osc->nmats+(CHUNKSIZ-1)));
775 >                for (i = 1; i < osc->nmats; i++)
776                          sc->matname[i] = savqstr(osc->matname[i]);
777                  sc->nmats = osc->nmats;
778          }
779          if (osc->nverts) {
780                  sc->vert = (Vertex *)emalloc(sizeof(Vertex) *
781                                                  (osc->nverts+(CHUNKSIZ-1)));
782 <                memcpy((void *)sc->vert, (const void *)osc->vert,
730 <                                sizeof(Vertex)*osc->nverts);
782 >                memcpy(sc->vert, osc->vert, sizeof(Vertex)*osc->nverts);
783                  sc->nverts = osc->nverts;
784                  for (i = 0; i < sc->nverts; i++)
785                          sc->vert[i].vflist = NULL;
# Line 735 | Line 787 | dupScene(const Scene *osc)
787          if (osc->ntex) {
788                  sc->tex = (TexCoord *)emalloc(sizeof(TexCoord) *
789                                                  (osc->ntex+(CHUNKSIZ-1)));
790 <                memcpy((void *)sc->tex, (const void *)osc->tex,
739 <                                sizeof(TexCoord)*osc->ntex);
790 >                memcpy(sc->tex, osc->tex, sizeof(TexCoord)*osc->ntex);
791                  sc->ntex = osc->ntex;
792          }
793          if (osc->nnorms) {
794                  sc->norm = (Normal *)emalloc(sizeof(Normal) *
795                                                  (osc->nnorms+CHUNKSIZ));
796 <                memcpy((void *)sc->norm, (const void *)osc->norm,
746 <                                sizeof(Normal)*osc->nnorms);
796 >                memcpy(sc->norm, osc->norm, sizeof(Normal)*osc->nnorms);
797                  sc->nnorms = osc->nnorms;
798          }
799          for (fo = osc->flist; fo != NULL; fo = fo->next) {
800 <                f = (Face *)emalloc(sizeof(Face) +
801 <                                sizeof(VertEnt)*(fo->nv-3));
802 <                memcpy((void *)f, (const void *)fo, sizeof(Face) +
803 <                                sizeof(VertEnt)*(fo->nv-3));
800 >                if ((fo->flags & fltest) != flreq)
801 >                        continue;
802 >                f = (Face *)emalloc(sizeof(Face) + sizeof(VertEnt)*(fo->nv-3));
803 >                memcpy(f, fo, sizeof(Face) + sizeof(VertEnt)*(fo->nv-3));
804                  for (i = 0; i < f->nv; i++)
805                          add2facelist(sc, f, i);
806                  f->next = sc->flist;
807                  sc->flist = f;
808                  sc->nfaces++;
809          }
810 +        deleteUnreferenced(sc);         /* jetsam */
811          return(sc);
812   }
813  
814 + /* Add one scene to another, not checking for redundancies */
815 + int
816 + addScene(Scene *scdst, const Scene *scsrc)
817 + {
818 +        VNDX            my_vlist[4];
819 +        int             *vert_map = NULL;
820 +        int             tex_off = 0;
821 +        int             norm_off = 0;
822 +        VNDX            *vlist = my_vlist;
823 +        int             vllen = sizeof(my_vlist)/sizeof(VNDX);
824 +        int             cur_mat = 0;
825 +        int             cur_grp = 0;
826 +        int             fcnt = 0;
827 +        const Face      *f;
828 +        int             i;
829 +
830 +        if ((scdst == NULL) | (scsrc == NULL))
831 +                return(-1);
832 +        if (scsrc->nfaces <= 0)
833 +                return(0);
834 +                                        /* map vertices */
835 +        vert_map = (int *)emalloc(sizeof(int)*scsrc->nverts);
836 +        for (i = 0; i < scsrc->nverts; i++) {
837 +                const Vertex    *v = scsrc->vert + i;
838 +                if (v->vflist == NULL) {
839 +                        vert_map[i] = -1;
840 +                        continue;
841 +                }
842 +                vert_map[i] = addVertex(scdst, v->p[0], v->p[1], v->p[2]);
843 +        }
844 +        tex_off = scdst->ntex;          /* append texture coords */
845 +        if (scsrc->ntex > 0) {
846 +                scdst->tex = (TexCoord *)erealloc((char *)scdst->tex,
847 +                        sizeof(TexCoord)*(tex_off+scsrc->ntex+(CHUNKSIZ-1)));
848 +                memcpy(scdst->tex+tex_off, scsrc->tex,
849 +                                sizeof(TexCoord)*scsrc->ntex);
850 +        }
851 +        norm_off = scdst->nnorms;       /* append normals */
852 +        if (scsrc->nnorms > 0) {
853 +                scdst->norm = (Normal *)erealloc((char *)scdst->norm,
854 +                        sizeof(Normal)*(norm_off+scsrc->nnorms+(CHUNKSIZ-1)));
855 +                memcpy(scdst->norm+norm_off, scsrc->norm,
856 +                                sizeof(Normal)*scsrc->nnorms);
857 +        }
858 +                                        /* add faces */
859 +        scdst->lastgrp = scdst->lastmat = 0;
860 +        for (f = scsrc->flist; f != NULL; f = f->next) {
861 +                if (f->grp != cur_grp)
862 +                        setGroup(scdst, scsrc->grpname[cur_grp = f->grp]);
863 +                if (f->mat != cur_mat)
864 +                        setMaterial(scdst, scsrc->matname[cur_mat = f->mat]);
865 +                if (f->nv > vllen) {
866 +                        vlist = (VNDX *)( vlist == my_vlist ?
867 +                                emalloc(sizeof(VNDX)*f->nv) :
868 +                                erealloc((char *)vlist, sizeof(VNDX)*f->nv) );
869 +                        vllen = f->nv;
870 +                }
871 +                memset(vlist, 0xff, sizeof(VNDX)*f->nv);
872 +                for (i = f->nv; i-- > 0; ) {
873 +                        if (f->v[i].vid >= 0)
874 +                                vlist[i][0] = vert_map[f->v[i].vid];
875 +                        if (f->v[i].tid >= 0)
876 +                                vlist[i][1] = f->v[i].tid + tex_off;
877 +                        if (f->v[i].nid >= 0)
878 +                                vlist[i][2] = f->v[i].nid + norm_off;
879 +                }
880 +                fcnt += (addFace(scdst, vlist, f->nv) != NULL);
881 +        }
882 +                                        /* clean up */
883 +        if (vlist != my_vlist) efree((char *)vlist);
884 +        efree((char *)vert_map);
885 +        return(fcnt);
886 + }
887 +
888 + #define MAXAC   100
889 +
890   /* Transform entire scene */
891   int
892   xfScene(Scene *sc, int xac, char *xav[])
893   {
894 +        char    comm[24+MAXAC*8];
895 +        char    *cp;
896          XF      myxf;
897          FVECT   vec;
898          int     i;
# Line 786 | Line 915 | xfScene(Scene *sc, int xac, char *xav[])
915                  vec[0] /= myxf.sca; vec[1] /= myxf.sca; vec[2] /= myxf.sca;
916                  VCOPY(sc->norm[i], vec);
917          }
918 +                                        /* add comment */
919 +        cp = strcpy(comm, "Transformed by:");
920 +        for (i = 0; i < xac; i++) {
921 +                while (*cp) cp++;
922 +                *cp++ = ' ';
923 +                strcpy(cp, xav[i]);
924 +        }
925 +        addComment(sc, comm);
926          return(xac);                    /* all done */
927   }
928  
929   /* Ditto, using transform string rather than pre-parsed words */
793 #define MAXAC   100
930   int
931   xfmScene(Scene *sc, const char *xfm)
932   {
# Line 805 | Line 941 | xfmScene(Scene *sc, const char *xfm)
941          if (!*xfm)
942                  return(0);
943                                          /* parse string into words */
944 <        xav[0] = strcpy((char *)malloc(strlen(xfm)+1), xfm);
944 >        xav[0] = strcpy((char *)emalloc(strlen(xfm)+1), xfm);
945          xac = 1; i = 0;
946          for ( ; ; ) {
947                  while (!isspace(xfm[++i]))
# Line 823 | Line 959 | xfmScene(Scene *sc, const char *xfm)
959          }
960          xav[xac] = NULL;
961          i = xfScene(sc, xac, xav);
962 <        free(xav[0]);
962 >        efree((char *)xav[0]);
963          return(i);
964   }
965   #undef MAXAC
966 +
967 + /* Delete unreferenced vertices, normals, texture coords */
968 + void
969 + deleteUnreferenced(Scene *sc)
970 + {
971 +        int     *vmap;
972 +        Face    *f;
973 +        int     nused, i;
974 +                                                /* allocate index map */
975 +        if (!sc->nverts)
976 +                return;
977 +        i = sc->nverts;
978 +        if (sc->ntex > i)
979 +                i = sc->ntex;
980 +        if (sc->nnorms > i)
981 +                i = sc->nnorms;
982 +        vmap = (int *)emalloc(sizeof(int)*i);
983 +                                                /* remap positions */
984 +        for (i = nused = 0; i < sc->nverts; i++) {
985 +                if (sc->vert[i].vflist == NULL) {
986 +                        vmap[i] = -1;
987 +                        continue;
988 +                }
989 +                if (nused != i)
990 +                        sc->vert[nused] = sc->vert[i];
991 +                vmap[i] = nused++;
992 +        }
993 +        if (nused == sc->nverts)
994 +                goto skip_pos;
995 +        sc->vert = (Vertex *)erealloc((char *)sc->vert,
996 +                                sizeof(Vertex)*(nused+(CHUNKSIZ-1)));
997 +        sc->nverts = nused;
998 +        for (f = sc->flist; f != NULL; f = f->next)
999 +                for (i = f->nv; i--; )
1000 +                        if ((f->v[i].vid = vmap[f->v[i].vid]) < 0)
1001 +                                error(CONSISTENCY,
1002 +                                        "Link error in del_unref_verts()");
1003 + skip_pos:
1004 +                                                /* remap texture coord's */
1005 +        if (!sc->ntex)
1006 +                goto skip_tex;
1007 +        memset((void *)vmap, 0, sizeof(int)*sc->ntex);
1008 +        for (f = sc->flist; f != NULL; f = f->next)
1009 +                for (i = f->nv; i--; )
1010 +                        if (f->v[i].tid >= 0)
1011 +                                vmap[f->v[i].tid] = 1;
1012 +        for (i = nused = 0; i < sc->ntex; i++) {
1013 +                if (!vmap[i])
1014 +                        continue;
1015 +                if (nused != i)
1016 +                        sc->tex[nused] = sc->tex[i];
1017 +                vmap[i] = nused++;
1018 +        }
1019 +        if (nused == sc->ntex)
1020 +                goto skip_tex;
1021 +        sc->tex = (TexCoord *)erealloc((char *)sc->tex,
1022 +                                sizeof(TexCoord)*(nused+(CHUNKSIZ-1)));
1023 +        sc->ntex = nused;
1024 +        for (f = sc->flist; f != NULL; f = f->next)
1025 +                for (i = f->nv; i--; )
1026 +                        if (f->v[i].tid >= 0)
1027 +                                f->v[i].tid = vmap[f->v[i].tid];
1028 + skip_tex:
1029 +                                                /* remap normals */
1030 +        if (!sc->nnorms)
1031 +                goto skip_norms;
1032 +        memset((void *)vmap, 0, sizeof(int)*sc->nnorms);
1033 +        for (f = sc->flist; f != NULL; f = f->next)
1034 +                for (i = f->nv; i--; )
1035 +                        if (f->v[i].nid >= 0)
1036 +                                vmap[f->v[i].nid] = 1;
1037 +        for (i = nused = 0; i < sc->nnorms; i++) {
1038 +                if (!vmap[i])
1039 +                        continue;
1040 +                if (nused != i)
1041 +                        memcpy(sc->norm[nused], sc->norm[i], sizeof(Normal));
1042 +                vmap[i] = nused++;
1043 +        }
1044 +        if (nused == sc->nnorms)
1045 +                goto skip_norms;
1046 +        sc->norm = (Normal *)erealloc((char *)sc->norm,
1047 +                                sizeof(Normal)*(nused+(CHUNKSIZ-1)));
1048 +        sc->nnorms = nused;
1049 +        for (f = sc->flist; f != NULL; f = f->next)
1050 +                for (i = f->nv; i--; )
1051 +                        if (f->v[i].nid >= 0)
1052 +                                f->v[i].nid = vmap[f->v[i].nid];
1053 + skip_norms:
1054 +                                                /* clean up */
1055 +        efree((char *)vmap);
1056 + }
1057  
1058   /* Free a scene */
1059   void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines