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

Comparing ray/src/common/mesh.c (file contents):
Revision 2.8 by greg, Fri Jun 20 00:25:49 2003 UTC vs.
Revision 2.26 by greg, Tue Nov 6 17:12:35 2012 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   * Mesh support routines
6   */
7  
8 < #include "standard.h"
8 > #include <string.h>
9 >
10 > #include "rtio.h"
11 > #include "rtmath.h"
12 > #include "rterror.h"
13 > #include "paths.h"
14   #include "octree.h"
15   #include "object.h"
16   #include "otypes.h"
# Line 27 | Line 32 | static MESH    *mlist = NULL;          /* list of loaded meshes *
32  
33  
34   static unsigned long
35 < cvhash(cvp)                             /* hash an encoded vertex */
31 < MCVERT  *cvp;
35 > cvhash(const char *p)                   /* hash an encoded vertex */
36   {
37 +        const MCVERT    *cvp = (const MCVERT *)p;
38          unsigned long   hval;
39          
40          if (!(cvp->fl & MT_V))
# Line 44 | Line 49 | MCVERT *cvp;
49  
50  
51   static int
52 < cvcmp(v1, v2)                           /* compare encoded vertices */
48 < register MCVERT *v1, *v2;
52 > cvcmp(const char *vv1, const char *vv2)         /* compare encoded vertices */
53   {
54 +        const MCVERT    *v1 = (const MCVERT *)vv1, *v2 = (const MCVERT *)vv2;
55          if (v1->fl != v2->fl)
56                  return(1);
57          if (v1->xyz[0] != v2->xyz[0])
# Line 68 | Line 73 | register MCVERT        *v1, *v2;
73  
74  
75   MESH *
76 < getmesh(mname, flags)                   /* get mesh data */
77 < char    *mname;
78 < int     flags;
76 > getmesh(                                /* get new mesh data reference */
77 >        char    *mname,
78 >        int     flags
79 > )
80   {
81          char  *pathname;
82 <        register MESH  *ms;
82 >        MESH  *ms;
83  
84          flags &= IO_LEGAL;
85          for (ms = mlist; ms != NULL; ms = ms->next)
86                  if (!strcmp(mname, ms->name)) {
87 <                        if ((ms->ldflags & flags) == flags) {
88 <                                ms->nref++;
83 <                                return(ms);             /* loaded */
84 <                        }
85 <                        break;                  /* load the rest */
87 >                        ms->nref++;     /* increase reference count */
88 >                        break;
89                  }
90 <        if (ms == NULL) {
90 >        if (ms == NULL) {               /* load first time */
91                  ms = (MESH *)calloc(1, sizeof(MESH));
92                  if (ms == NULL)
93                          error(SYSTEM, "out of memory in getmesh");
# Line 106 | Line 109 | int    flags;
109  
110  
111   MESHINST *
112 < getmeshinst(o, flags)                   /* create mesh instance */
113 < OBJREC  *o;
114 < int     flags;
112 > getmeshinst(                            /* create mesh instance */
113 >        OBJREC  *o,
114 >        int     flags
115 > )
116   {
117 <        register MESHINST  *ins;
117 >        MESHINST  *ins;
118  
119          flags &= IO_LEGAL;
120          if ((ins = (MESHINST *)o->os) == NULL) {
# Line 128 | Line 132 | int    flags;
132                  ins->msh = NULL;
133                  o->os = (char *)ins;
134          }
135 <        if (ins->msh == NULL || (ins->msh->ldflags & flags) != flags)
135 >        if (ins->msh == NULL)
136                  ins->msh = getmesh(o->oargs.sarg[0], flags);
137 +        else if ((flags &= ~ins->msh->ldflags))
138 +                readmesh(ins->msh,
139 +                        getpath(o->oargs.sarg[0], getrlibpath(), R_OK),
140 +                                flags);
141          return(ins);
142   }
143  
144  
145   int
146 < getmeshtrivid(tvid, mo, mp, ti)         /* get triangle vertex ID's */
147 < int32   tvid[3];
148 < OBJECT  *mo;
149 < MESH    *mp;
142 < OBJECT  ti;
146 > nextmeshtri(                            /* get next triangle ID */
147 >        OBJECT *tip,
148 >        MESH *mp
149 > )
150   {
151 +        int             pn;
152 +        MESHPATCH       *pp;
153 +
154 +        pn = ++(*tip) >> 10;                    /* next triangle (OVOID init) */
155 +        while (pn < mp->npatches) {
156 +                pp = &mp->patch[pn];
157 +                if (!(*tip & 0x200)) {          /* local triangle? */
158 +                        if ((*tip & 0x1ff) < pp->ntris)
159 +                                return(1);
160 +                        *tip &= ~0x1ff;         /* move on to single-joiners */
161 +                        *tip |= 0x200;
162 +                }
163 +                if (!(*tip & 0x100)) {          /* single joiner? */
164 +                        if ((*tip & 0xff) < pp->nj1tris)
165 +                                return(1);
166 +                        *tip &= ~0xff;          /* move on to double-joiners */
167 +                        *tip |= 0x100;
168 +                }
169 +                if ((*tip & 0xff) < pp->nj2tris)
170 +                        return(1);
171 +                *tip = ++pn << 10;              /* first in next patch */
172 +        }
173 +        return(0);                              /* out of patches */
174 + }
175 +
176 + int
177 + getmeshtrivid(                          /* get triangle vertex ID's */
178 +        int32   tvid[3],
179 +        OBJECT  *mo,
180 +        MESH    *mp,
181 +        OBJECT  ti
182 + )
183 + {
184          int             pn = ti >> 10;
185          MESHPATCH       *pp;
186  
# Line 196 | Line 236 | OBJECT ti;
236  
237  
238   int
239 < getmeshvert(vp, mp, vid, what)  /* get triangle vertex from ID */
240 < MESHVERT        *vp;
241 < MESH            *mp;
242 < int32           vid;
243 < int             what;
239 > getmeshvert(                            /* get triangle vertex from ID */
240 >        MESHVERT        *vp,
241 >        MESH            *mp,
242 >        int32           vid,
243 >        int             what
244 > )
245   {
246          int             pn = vid >> 8;
247          MESHPATCH       *pp;
248          double          vres;
249 <        register int    i;
249 >        int     i;
250          
251          vp->fl = 0;
252          if (pn >= mp->npatches)
# Line 240 | Line 281 | int            what;
281  
282  
283   OBJREC *
284 < getmeshpseudo(mp, mo)           /* get mesh pseudo object for material */
285 < MESH    *mp;
286 < OBJECT  mo;
284 > getmeshpseudo(                  /* get mesh pseudo object for material */
285 >        MESH    *mp,
286 >        OBJECT  mo
287 > )
288   {
289          if (mo < mp->mat0 || mo >= mp->mat0 + mp->nmats)
290                  error(INTERNAL, "modifier out of range in getmeshpseudo");
291          if (mp->pseudo == NULL) {
292 <                register int    i;
292 >                int     i;
293                  mp->pseudo = (OBJREC *)calloc(mp->nmats, sizeof(OBJREC));
294                  if (mp->pseudo == NULL)
295                          error(SYSTEM, "out of memory in getmeshpseudo");
# Line 262 | Line 304 | OBJECT mo;
304  
305  
306   int
307 < getmeshtri(tv, mo, mp, ti, wha) /* get triangle vertices */
308 < MESHVERT        tv[3];
309 < OBJECT          *mo;
310 < MESH            *mp;
311 < OBJECT          ti;
312 < int             wha;
307 > getmeshtri(                     /* get triangle vertices */
308 >        MESHVERT        tv[3],
309 >        OBJECT          *mo,
310 >        MESH            *mp,
311 >        OBJECT          ti,
312 >        int             wha
313 > )
314   {
315          int32   tvid[3];
316  
# Line 283 | Line 326 | int            wha;
326  
327  
328   int32
329 < addmeshvert(mp, vp)             /* find/add a mesh vertex */
330 < register MESH   *mp;
331 < MESHVERT        *vp;
329 > addmeshvert(                    /* find/add a mesh vertex */
330 >        MESH    *mp,
331 >        MESHVERT        *vp
332 > )
333   {
334          LUENT           *lvp;
335          MCVERT          cv;
336 <        register int    i;
336 >        int     i;
337  
338          if (!(vp->fl & MT_V))
339                  return(-1);
# Line 303 | Line 347 | MESHVERT       *vp;
347                                  (vp->v[i] - mp->mcube.cuorg[i]) /
348                                  mp->mcube.cusize);
349          }
350 <        if (vp->fl & MT_N)
350 >        if (vp->fl & MT_N)              /* assumes normalized! */
351                  cv.norm = encodedir(vp->n);
352          if (vp->fl & MT_UV)
353                  for (i = 0; i < 2; i++) {
# Line 329 | Line 373 | MESHVERT       *vp;
373                  goto nomem;
374          if (lvp->key == NULL) {
375                  lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int32));
376 <                bcopy((void *)&cv, (void *)lvp->key, sizeof(MCVERT));
376 >                memcpy((void *)lvp->key, (void *)&cv, sizeof(MCVERT));
377          }
378          if (lvp->data == NULL) {        /* new vertex */
379 <                register MESHPATCH      *pp;
379 >                MESHPATCH       *pp;
380                  if (mp->npatches <= 0) {
381                          mp->patch = (MESHPATCH *)calloc(MPATCHBLKSIZ,
382                                          sizeof(MESHPATCH));
# Line 345 | Line 389 | MESHVERT       *vp;
389                                                  (void *)mp->patch,
390                                          (mp->npatches + MPATCHBLKSIZ)*
391                                                  sizeof(MESHPATCH));
392 <                                bzero((void *)(mp->patch + mp->npatches),
392 >                                memset((void *)(mp->patch + mp->npatches), '\0',
393                                          MPATCHBLKSIZ*sizeof(MESHPATCH));
394                          }
395                          if (mp->npatches++ >= 1L<<22)
# Line 389 | Line 433 | nomem:
433  
434  
435   OBJECT
436 < addmeshtri(mp, tv, mo)          /* add a new mesh triangle */
437 < MESH            *mp;
438 < MESHVERT        tv[3];
439 < OBJECT          mo;
436 > addmeshtri(                     /* add a new mesh triangle */
437 >        MESH            *mp,
438 >        MESHVERT        tv[3],
439 >        OBJECT          mo
440 > )
441   {
442          int32                   vid[3], t;
443          int                     pn[3], i;
444 <        register MESHPATCH      *pp;
444 >        MESHPATCH       *pp;
445  
446          if (!(tv[0].fl & tv[1].fl & tv[2].fl & MT_V))
447                  return(OVOID);
# Line 407 | Line 452 | OBJECT         mo;
452                  pn[i] = vid[i] >> 8;
453          }
454                                  /* normalize material index */
455 <        if (mo != OVOID)
455 >        if (mo != OVOID) {
456                  if ((mo -= mp->mat0) >= mp->nmats)
457                          mp->nmats = mo+1;
458                  else if (mo < 0)
459                          error(INTERNAL, "modifier range error in addmeshtri");
460 +        }
461                                  /* assign triangle */
462          if (pn[0] == pn[1] && pn[1] == pn[2]) { /* local case */
463                  pp = &mp->patch[pn[0]];
# Line 485 | Line 531 | nomem:
531  
532  
533   char *
534 < checkmesh(mp)                           /* validate mesh data */
489 < register MESH   *mp;
534 > checkmesh(MESH *mp)                     /* validate mesh data */
535   {
536          static char     embuf[128];
537          int             nouvbounds = 1;
538 <        register int    i;
538 >        int     i;
539                                          /* basic checks */
540          if (mp == NULL)
541                  return("NULL mesh pointer");
# Line 530 | Line 575 | register MESH  *mp;
575                  if (mp->npatches <= 0)
576                          error(WARNING, "no patches in mesh");
577                  for (i = 0; i < mp->npatches; i++) {
578 <                        register MESHPATCH      *pp = &mp->patch[i];
578 >                        MESHPATCH       *pp = &mp->patch[i];
579                          if (pp->nverts <= 0)
580                                  error(WARNING, "no vertices in patch");
581                          else {
# Line 539 | Line 584 | register MESH  *mp;
584                                  if (nouvbounds && pp->uv != NULL)
585                                          return("unreferenced uv coordinates");
586                          }
542                        if (pp->ntris + pp->nj1tris + pp->nj2tris <= 0)
543                                error(WARNING, "no triangles in patch");
587                          if (pp->ntris > 0 && pp->tri == NULL)
588                                  return("missing patch triangle list");
589                          if (pp->nj1tris > 0 && pp->j1tri == NULL)
# Line 554 | Line 597 | register MESH  *mp;
597  
598  
599   static void
600 < tallyoctree(ot, ecp, lcp, ocp)  /* tally octree size */
601 < OCTREE  ot;
602 < int     *ecp, *lcp, *ocp;
600 > tallyoctree(                    /* tally octree size */
601 >        OCTREE  ot,
602 >        int     *ecp,
603 >        int     *lcp,
604 >        int     *ocp
605 > )
606   {
607          int     i;
608  
# Line 577 | Line 623 | int    *ecp, *lcp, *ocp;
623  
624  
625   void
626 < printmeshstats(ms, fp)          /* print out mesh statistics */
627 < MESH    *ms;
628 < FILE    *fp;
626 > printmeshstats(                 /* print out mesh statistics */
627 >        MESH    *ms,
628 >        FILE    *fp
629 > )
630   {
631          int     lfcnt=0, lecnt=0, locnt=0;
632          int     vcnt=0, ncnt=0, uvcnt=0;
# Line 589 | Line 636 | FILE   *fp;
636          
637          tallyoctree(ms->mcube.cutree, &lecnt, &lfcnt, &locnt);
638          for (i = 0; i < ms->npatches; i++) {
639 <                register MESHPATCH      *pp = &ms->patch[i];
639 >                MESHPATCH       *pp = &ms->patch[i];
640                  vcnt += pp->nverts;
641                  if (pp->norm != NULL) {
642                          for (j = pp->nverts; j--; )
# Line 608 | Line 655 | FILE   *fp;
655                  t2cnt += pp->nj2tris;
656          }
657          fprintf(fp, "Mesh statistics:\n");
658 <        fprintf(fp, "\t%d materials\n", ms->nmats);
658 >        fprintf(fp, "\t%ld materials\n", (long)ms->nmats);
659          fprintf(fp, "\t%d patches (%.2f MBytes)\n", ms->npatches,
660                          (ms->npatches*sizeof(MESHPATCH) +
661                          vcnt*3*sizeof(uint32) +
# Line 631 | Line 678 | FILE   *fp;
678  
679  
680   void
681 < freemesh(ms)                    /* free mesh data */
635 < register MESH   *ms;
681 > freemesh(MESH *ms)              /* free mesh data */
682   {
683          MESH    mhead;
684          MESH    *msp;
# Line 660 | Line 706 | register MESH  *ms;
706          octfree(ms->mcube.cutree);
707          lu_done(&ms->lut);
708          if (ms->npatches > 0) {
709 <                register MESHPATCH      *pp = ms->patch + ms->npatches;
709 >                MESHPATCH       *pp = ms->patch + ms->npatches;
710                  while (pp-- > ms->patch) {
711                          if (pp->j2tri != NULL)
712                                  free((void *)pp->j2tri);
# Line 684 | Line 730 | register MESH  *ms;
730  
731  
732   void
733 < freemeshinst(o)                 /* free mesh instance */
688 < OBJREC  *o;
733 > freemeshinst(OBJREC *o)         /* free mesh instance */
734   {
735          if (o->os == NULL)
736                  return;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines