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.11 by greg, Fri Feb 12 01:57:49 2021 UTC vs.
Revision 2.14 by greg, Wed Apr 7 03:02:00 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 469 | 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 488 | 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 588 | 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 604 | 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 640 | Line 644 | addTexture(Scene *sc, double u, double v)
644   int
645   addNormal(Scene *sc, double xn, double yn, double zn)
646   {
647 <        FVECT   nrm;
647 >        FVECT   nrm;
648  
649          nrm[0] = xn; nrm[1] = yn; nrm[2] = zn;
650          if (normalize(nrm) == .0)
# Line 654 | Line 658 | addNormal(Scene *sc, double xn, double yn, double zn)
658   void
659   setGroup(Scene *sc, const char *nm)
660   {
661 <        sc->lastgrp = findName(nm, (const char **)sc->grpname, sc->ngrps);
661 >        sc->lastgrp = getGroupID(sc, nm);
662          if (sc->lastgrp >= 0)
663                  return;
664          sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps);
# Line 665 | Line 669 | setGroup(Scene *sc, const char *nm)
669   void
670   setMaterial(Scene *sc, const char *nm)
671   {
672 <        sc->lastmat = findName(nm, (const char **)sc->matname, sc->nmats);
672 >        sc->lastmat = getMaterialID(sc, nm);
673          if (sc->lastmat >= 0)
674                  return;
675          sc->matname = chunk_alloc(char *, sc->matname, sc->nmats);
# Line 676 | Line 680 | setMaterial(Scene *sc, const char *nm)
680   Face *
681   addFace(Scene *sc, VNDX vid[], int nv)
682   {
683 <        Face    *f;
684 <        int     i;
683 >        Face    *f;
684 >        int     i;
685          
686          if (nv < 3)
687                  return(NULL);
# Line 779 | Line 783 | dupScene(const Scene *osc, int flreq, int flexc)
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 */
# Line 835 | Line 915 | xfmScene(Scene *sc, const char *xfm)
915          if (!*xfm)
916                  return(0);
917                                          /* parse string into words */
918 <        xav[0] = strcpy((char *)malloc(strlen(xfm)+1), xfm);
918 >        xav[0] = strcpy((char *)emalloc(strlen(xfm)+1), xfm);
919          xac = 1; i = 0;
920          for ( ; ; ) {
921                  while (!isspace(xfm[++i]))
# Line 853 | Line 933 | xfmScene(Scene *sc, const char *xfm)
933          }
934          xav[xac] = NULL;
935          i = xfScene(sc, xac, xav);
936 <        free(xav[0]);
936 >        efree((char *)xav[0]);
937          return(i);
938   }
939   #undef MAXAC

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines