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.2 by greg, Wed Mar 12 04:59:04 2003 UTC vs.
Revision 2.10 by greg, Thu Jul 10 03:30:11 2003 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   * Mesh support routines
6   */
7  
8 + #include <string.h>
9 +
10   #include "standard.h"
11   #include "octree.h"
12   #include "object.h"
13 + #include "otypes.h"
14   #include "mesh.h"
12 #include "lookup.h"
15  
16   /* An encoded mesh vertex */
17   typedef struct {
18          int             fl;
19 <        uint4           xyz[3];
20 <        int4            norm;
21 <        uint4           uv[2];
19 >        uint32          xyz[3];
20 >        int32           norm;
21 >        uint32          uv[2];
22   } MCVERT;
23  
24   #define  MPATCHBLKSIZ   128             /* patch allocation block size */
# Line 68 | Line 70 | register MCVERT        *v1, *v2;
70  
71  
72   MESH *
73 < getmesh(mname, flags)                   /* get mesh data */
73 > getmesh(mname, flags)                   /* get new mesh data reference */
74   char    *mname;
75   int     flags;
76   {
# Line 77 | Line 79 | int    flags;
79  
80          flags &= IO_LEGAL;
81          for (ms = mlist; ms != NULL; ms = ms->next)
82 <                if (!strcmp(mname, ms->name)) {
83 <                        if ((ms->ldflags & flags) == flags) {
84 <                                ms->nref++;
83 <                                return(ms);             /* loaded */
84 <                        }
85 <                        break;                  /* load the rest */
86 <                }
87 <        if (ms == NULL) {
82 >                if (!strcmp(mname, ms->name))
83 >                        break;
84 >        if (ms == NULL) {               /* load first time */
85                  ms = (MESH *)calloc(1, sizeof(MESH));
86                  if (ms == NULL)
87                          error(SYSTEM, "out of memory in getmesh");
88                  ms->name = savestr(mname);
92                ms->nref = 1;
89                  ms->mcube.cutree = EMPTY;
90                  ms->next = mlist;
91                  mlist = ms;
92          }
93 <        if ((pathname = getpath(mname, getlibpath(), R_OK)) == NULL) {
93 >        if ((pathname = getpath(mname, getrlibpath(), R_OK)) == NULL) {
94                  sprintf(errmsg, "cannot find mesh file \"%s\"", mname);
95                  error(USER, errmsg);
96          }
97          flags &= ~ms->ldflags;
98          if (flags)
99                  readmesh(ms, pathname, flags);
100 +        ms->nref++;                     /* increase reference count */
101          return(ms);
102   }
103  
# Line 128 | Line 125 | int    flags;
125                  ins->msh = NULL;
126                  o->os = (char *)ins;
127          }
128 <        if (ins->msh == NULL || (ins->msh->ldflags & flags) != flags)
128 >        if (ins->msh == NULL)
129                  ins->msh = getmesh(o->oargs.sarg[0], flags);
130 +        else if ((flags &= ~ins->msh->ldflags))
131 +                readmesh(ins->msh,
132 +                        getpath(o->oargs.sarg[0], getrlibpath(), R_OK),
133 +                                flags);
134          return(ins);
135   }
136  
137  
138   int
139 < getmeshtrivid(tvid, mp, ti)             /* get triangle vertex ID's */
140 < int4            tvid[3];
141 < register MESH   *mp;
142 < OBJECT          ti;
139 > getmeshtrivid(tvid, mo, mp, ti)         /* get triangle vertex ID's */
140 > int32   tvid[3];
141 > OBJECT  *mo;
142 > MESH    *mp;
143 > OBJECT  ti;
144   {
145          int             pn = ti >> 10;
146 +        MESHPATCH       *pp;
147  
148          if (pn >= mp->npatches)
149                  return(0);
150 +        pp = &mp->patch[pn];
151          ti &= 0x3ff;
152          if (!(ti & 0x200)) {            /* local triangle */
153                  struct PTri     *tp;
154 <                if (ti >= mp->patch[pn].ntris)
154 >                if (ti >= pp->ntris)
155                          return(0);
156 <                tp = &mp->patch[pn].tri[ti];
156 >                tp = &pp->tri[ti];
157                  tvid[0] = tvid[1] = tvid[2] = pn << 8;
158                  tvid[0] |= tp->v1;
159                  tvid[1] |= tp->v2;
160                  tvid[2] |= tp->v3;
161 +                if (pp->trimat != NULL)
162 +                        *mo = pp->trimat[ti];
163 +                else
164 +                        *mo = pp->solemat;
165 +                if (*mo != OVOID)
166 +                        *mo += mp->mat0;
167                  return(1);
168          }
169          ti &= ~0x200;
170          if (!(ti & 0x100)) {            /* single link vertex */
171                  struct PJoin1   *tp1;
172 <                if (ti >= mp->patch[pn].nj1tris)
172 >                if (ti >= pp->nj1tris)
173                          return(0);
174 <                tp1 = &mp->patch[pn].j1tri[ti];
174 >                tp1 = &pp->j1tri[ti];
175                  tvid[0] = tp1->v1j;
176                  tvid[1] = tvid[2] = pn << 8;
177                  tvid[1] |= tp1->v2;
178                  tvid[2] |= tp1->v3;
179 +                if ((*mo = tp1->mat) != OVOID)
180 +                        *mo += mp->mat0;
181                  return(1);
182          }
183          ti &= ~0x100;
184          {                               /* double link vertex */
185                  struct PJoin2   *tp2;
186 <                if (ti >= mp->patch[pn].nj2tris)
186 >                if (ti >= pp->nj2tris)
187                          return(0);
188 <                tp2 = &mp->patch[pn].j2tri[ti];
188 >                tp2 = &pp->j2tri[ti];
189                  tvid[0] = tp2->v1j;
190                  tvid[1] = tp2->v2j;
191                  tvid[2] = pn << 8 | tp2->v3;
192 +                if ((*mo = tp2->mat) != OVOID)
193 +                        *mo += mp->mat0;
194          }
195          return(1);
196   }
# Line 185 | Line 199 | OBJECT         ti;
199   int
200   getmeshvert(vp, mp, vid, what)  /* get triangle vertex from ID */
201   MESHVERT        *vp;
202 < register MESH   *mp;
203 < int4            vid;
202 > MESH            *mp;
203 > int32           vid;
204   int             what;
205   {
206          int             pn = vid >> 8;
# Line 226 | Line 240 | int            what;
240   }
241  
242  
243 + OBJREC *
244 + getmeshpseudo(mp, mo)           /* get mesh pseudo object for material */
245 + MESH    *mp;
246 + OBJECT  mo;
247 + {
248 +        if (mo < mp->mat0 || mo >= mp->mat0 + mp->nmats)
249 +                error(INTERNAL, "modifier out of range in getmeshpseudo");
250 +        if (mp->pseudo == NULL) {
251 +                register int    i;
252 +                mp->pseudo = (OBJREC *)calloc(mp->nmats, sizeof(OBJREC));
253 +                if (mp->pseudo == NULL)
254 +                        error(SYSTEM, "out of memory in getmeshpseudo");
255 +                for (i = mp->nmats; i--; ) {
256 +                        mp->pseudo[i].omod = mp->mat0 + i;
257 +                        mp->pseudo[i].otype = OBJ_FACE;
258 +                        mp->pseudo[i].oname = "M-Tri";
259 +                }
260 +        }
261 +        return(&mp->pseudo[mo - mp->mat0]);
262 + }
263 +
264 +
265   int
266 < getmeshtri(tv, mp, ti, what)    /* get triangle vertices */
266 > getmeshtri(tv, mo, mp, ti, wha) /* get triangle vertices */
267   MESHVERT        tv[3];
268 + OBJECT          *mo;
269   MESH            *mp;
270   OBJECT          ti;
271 < int             what;
271 > int             wha;
272   {
273 <        int4    tvid[3];
273 >        int32   tvid[3];
274  
275 <        if (!getmeshtrivid(tvid, mp, ti))
275 >        if (!getmeshtrivid(tvid, mo, mp, ti))
276                  return(0);
277  
278 <        getmeshvert(&tv[0], mp, tvid[0], what);
279 <        getmeshvert(&tv[1], mp, tvid[1], what);
280 <        getmeshvert(&tv[2], mp, tvid[2], what);
278 >        getmeshvert(&tv[0], mp, tvid[0], wha);
279 >        getmeshvert(&tv[1], mp, tvid[1], wha);
280 >        getmeshvert(&tv[2], mp, tvid[2], wha);
281  
282          return(tv[0].fl & tv[1].fl & tv[2].fl);
283   }
284  
285  
286 < int4
286 > int32
287   addmeshvert(mp, vp)             /* find/add a mesh vertex */
288   register MESH   *mp;
289   MESHVERT        *vp;
290   {
254        LUTAB           *ltp;
291          LUENT           *lvp;
292          MCVERT          cv;
293          register int    i;
# Line 264 | Line 300 | MESHVERT       *vp;
300                          return(-1);
301                  if (vp->v[i] >= mp->mcube.cuorg[i] + mp->mcube.cusize)
302                          return(-1);
303 <                cv.xyz[i] = (uint4)(4294967296. *
303 >                cv.xyz[i] = (uint32)(4294967296. *
304                                  (vp->v[i] - mp->mcube.cuorg[i]) /
305                                  mp->mcube.cusize);
306          }
# Line 276 | Line 312 | MESHVERT       *vp;
312                                  return(-1);
313                          if (vp->uv[i] >= mp->uvlim[1][i])
314                                  return(-1);
315 <                        cv.uv[i] = (uint4)(4294967296. *
315 >                        cv.uv[i] = (uint32)(4294967296. *
316                                          (vp->uv[i] - mp->uvlim[0][i]) /
317                                          (mp->uvlim[1][i] - mp->uvlim[0][i]));
318                  }
319          cv.fl = vp->fl;
320 <        ltp = (LUTAB *)mp->cdata;       /* get lookup table */
321 <        if (ltp == NULL) {
322 <                ltp = (LUTAB *)calloc(1, sizeof(LUTAB));
323 <                if (ltp == NULL)
320 >        if (mp->lut.tsiz == 0) {
321 >                mp->lut.hashf = cvhash;
322 >                mp->lut.keycmp = cvcmp;
323 >                mp->lut.freek = free;
324 >                if (!lu_init(&mp->lut, 50000))
325                          goto nomem;
289                ltp->hashf = cvhash;
290                ltp->keycmp = cvcmp;
291                ltp->freek = free;
292                if (!lu_init(ltp, 50000))
293                        goto nomem;
294                mp->cdata = (char *)ltp;
326          }
327                                          /* find entry */
328 <        lvp = lu_find(ltp, (char *)&cv);
328 >        lvp = lu_find(&mp->lut, (char *)&cv);
329          if (lvp == NULL)
330                  goto nomem;
331          if (lvp->key == NULL) {
332 <                lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int4));
333 <                bcopy((void *)&cv, (void *)lvp->key, sizeof(MCVERT));
332 >                lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int32));
333 >                memcpy((void *)lvp->key, (void *)&cv, sizeof(MCVERT));
334          }
335          if (lvp->data == NULL) {        /* new vertex */
336                  register MESHPATCH      *pp;
# Line 315 | Line 346 | MESHVERT       *vp;
346                                                  (void *)mp->patch,
347                                          (mp->npatches + MPATCHBLKSIZ)*
348                                                  sizeof(MESHPATCH));
349 <                                bzero((void *)(mp->patch + mp->npatches),
349 >                                memset((void *)(mp->patch + mp->npatches), '\0',
350                                          MPATCHBLKSIZ*sizeof(MESHPATCH));
351                          }
352                          if (mp->npatches++ >= 1L<<22)
# Line 323 | Line 354 | MESHVERT       *vp;
354                  }
355                  pp = &mp->patch[mp->npatches-1];
356                  if (pp->xyz == NULL) {
357 <                        pp->xyz = (uint4 (*)[3])calloc(256, 3*sizeof(int4));
357 >                        pp->xyz = (uint32 (*)[3])calloc(256, 3*sizeof(int32));
358                          if (pp->xyz == NULL)
359                                  goto nomem;
360                  }
# Line 331 | Line 362 | MESHVERT       *vp;
362                          pp->xyz[pp->nverts][i] = cv.xyz[i];
363                  if (cv.fl & MT_N) {
364                          if (pp->norm == NULL) {
365 <                                pp->norm = (int4 *)calloc(256, sizeof(int4));
365 >                                pp->norm = (int32 *)calloc(256, sizeof(int32));
366                                  if (pp->norm == NULL)
367                                          goto nomem;
368                          }
# Line 339 | Line 370 | MESHVERT       *vp;
370                  }
371                  if (cv.fl & MT_UV) {
372                          if (pp->uv == NULL) {
373 <                                pp->uv = (uint4 (*)[2])calloc(256,
374 <                                                2*sizeof(uint4));
373 >                                pp->uv = (uint32 (*)[2])calloc(256,
374 >                                                2*sizeof(uint32));
375                                  if (pp->uv == NULL)
376                                          goto nomem;
377                          }
# Line 349 | Line 380 | MESHVERT       *vp;
380                  }
381                  pp->nverts++;
382                  lvp->data = lvp->key + sizeof(MCVERT);
383 <                *(int4 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1);
383 >                *(int32 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1);
384          }
385 <        return(*(int4 *)lvp->data);
385 >        return(*(int32 *)lvp->data);
386   nomem:
387          error(SYSTEM, "out of memory in addmeshvert");
388          return(-1);
# Line 359 | Line 390 | nomem:
390  
391  
392   OBJECT
393 < addmeshtri(mp, tv)              /* add a new mesh triangle */
393 > addmeshtri(mp, tv, mo)          /* add a new mesh triangle */
394   MESH            *mp;
395   MESHVERT        tv[3];
396 + OBJECT          mo;
397   {
398 <        int4                    vid[3], t;
398 >        int32                   vid[3], t;
399          int                     pn[3], i;
400          register MESHPATCH      *pp;
401  
# Line 375 | Line 407 | MESHVERT       tv[3];
407                          return(OVOID);
408                  pn[i] = vid[i] >> 8;
409          }
410 +                                /* normalize material index */
411 +        if (mo != OVOID)
412 +                if ((mo -= mp->mat0) >= mp->nmats)
413 +                        mp->nmats = mo+1;
414 +                else if (mo < 0)
415 +                        error(INTERNAL, "modifier range error in addmeshtri");
416                                  /* assign triangle */
417          if (pn[0] == pn[1] && pn[1] == pn[2]) { /* local case */
418                  pp = &mp->patch[pn[0]];
# Line 388 | Line 426 | MESHVERT       tv[3];
426                          pp->tri[pp->ntris].v1 = vid[0] & 0xff;
427                          pp->tri[pp->ntris].v2 = vid[1] & 0xff;
428                          pp->tri[pp->ntris].v3 = vid[2] & 0xff;
429 +                        if (pp->ntris == 0)
430 +                                pp->solemat = mo;
431 +                        else if (pp->trimat == NULL && mo != pp->solemat) {
432 +                                pp->trimat = (int16 *)malloc(
433 +                                                512*sizeof(int16));
434 +                                if (pp->trimat == NULL)
435 +                                        goto nomem;
436 +                                for (i = pp->ntris; i--; )
437 +                                        pp->trimat[i] = pp->solemat;
438 +                        }
439 +                        if (pp->trimat != NULL)
440 +                                pp->trimat[pp->ntris] = mo;
441                          return(pn[0] << 10 | pp->ntris++);
442                  }
443          }
# Line 410 | Line 460 | MESHVERT       tv[3];
460                          pp->j1tri[pp->nj1tris].v1j = vid[0];
461                          pp->j1tri[pp->nj1tris].v2 = vid[1] & 0xff;
462                          pp->j1tri[pp->nj1tris].v3 = vid[2] & 0xff;
463 +                        pp->j1tri[pp->nj1tris].mat = mo;
464                          return(pn[1] << 10 | 0x200 | pp->nj1tris++);
465                  }
466          }
# Line 426 | Line 477 | MESHVERT       tv[3];
477          pp->j2tri[pp->nj2tris].v1j = vid[0];
478          pp->j2tri[pp->nj2tris].v2j = vid[1];
479          pp->j2tri[pp->nj2tris].v3 = vid[2] & 0xff;
480 +        pp->j2tri[pp->nj2tris].mat = mo;
481          return(pn[2] << 10 | 0x300 | pp->nj2tris++);
482   nomem:
483          error(SYSTEM, "out of memory in addmeshtri");
# Line 433 | Line 485 | nomem:
485   }
486  
487  
488 + char *
489 + checkmesh(mp)                           /* validate mesh data */
490 + register MESH   *mp;
491 + {
492 +        static char     embuf[128];
493 +        int             nouvbounds = 1;
494 +        register int    i;
495 +                                        /* basic checks */
496 +        if (mp == NULL)
497 +                return("NULL mesh pointer");
498 +        if (!mp->ldflags)
499 +                return("unassigned mesh");
500 +        if (mp->name == NULL)
501 +                return("missing mesh name");
502 +        if (mp->nref <= 0)
503 +                return("unreferenced mesh");
504 +                                        /* check boundaries */
505 +        if (mp->ldflags & IO_BOUNDS) {
506 +                if (mp->mcube.cusize <= FTINY)
507 +                        return("illegal octree bounds in mesh");
508 +                nouvbounds = (mp->uvlim[1][0] - mp->uvlim[0][0] <= FTINY ||
509 +                                mp->uvlim[1][1] - mp->uvlim[0][1] <= FTINY);
510 +        }
511 +                                        /* check octree */
512 +        if (mp->ldflags & IO_TREE) {
513 +                if (isempty(mp->mcube.cutree))
514 +                        error(WARNING, "empty mesh octree");
515 +        }
516 +                                        /* check scene data */
517 +        if (mp->ldflags & IO_SCENE) {
518 +                if (!(mp->ldflags & IO_BOUNDS))
519 +                        return("unbounded scene in mesh");
520 +                if (mp->mat0 < 0 || mp->mat0+mp->nmats > nobjects)
521 +                        return("bad mesh modifier range");
522 +                for (i = mp->mat0+mp->nmats; i-- > mp->mat0; ) {
523 +                        int     otyp = objptr(i)->otype;
524 +                        if (!ismodifier(otyp)) {
525 +                                sprintf(embuf,
526 +                                        "non-modifier in mesh (%s \"%s\")",
527 +                                        ofun[otyp].funame, objptr(i)->oname);
528 +                                return(embuf);
529 +                        }
530 +                }
531 +                if (mp->npatches <= 0)
532 +                        error(WARNING, "no patches in mesh");
533 +                for (i = 0; i < mp->npatches; i++) {
534 +                        register MESHPATCH      *pp = &mp->patch[i];
535 +                        if (pp->nverts <= 0)
536 +                                error(WARNING, "no vertices in patch");
537 +                        else {
538 +                                if (pp->xyz == NULL)
539 +                                        return("missing patch vertex list");
540 +                                if (nouvbounds && pp->uv != NULL)
541 +                                        return("unreferenced uv coordinates");
542 +                        }
543 +                        if (pp->ntris + pp->nj1tris + pp->nj2tris <= 0)
544 +                                error(WARNING, "no triangles in patch");
545 +                        if (pp->ntris > 0 && pp->tri == NULL)
546 +                                return("missing patch triangle list");
547 +                        if (pp->nj1tris > 0 && pp->j1tri == NULL)
548 +                                return("missing patch joiner triangle list");
549 +                        if (pp->nj2tris > 0 && pp->j2tri == NULL)
550 +                                return("missing patch double-joiner list");
551 +                }
552 +        }
553 +        return(NULL);                   /* seems OK */
554 + }
555 +
556 +
557   static void
558   tallyoctree(ot, ecp, lcp, ocp)  /* tally octree size */
559   OCTREE  ot;
# Line 488 | Line 609 | FILE   *fp;
609                  t2cnt += pp->nj2tris;
610          }
611          fprintf(fp, "Mesh statistics:\n");
612 +        fprintf(fp, "\t%d materials\n", ms->nmats);
613          fprintf(fp, "\t%d patches (%.2f MBytes)\n", ms->npatches,
614                          (ms->npatches*sizeof(MESHPATCH) +
615 <                        vcnt*3*sizeof(uint4) +
616 <                        nscnt*sizeof(int4) +
617 <                        uvscnt*2*sizeof(uint4) +
615 >                        vcnt*3*sizeof(uint32) +
616 >                        nscnt*sizeof(int32) +
617 >                        uvscnt*2*sizeof(uint32) +
618                          tcnt*sizeof(struct PTri) +
619                          t1cnt*sizeof(struct PJoin1) +
620                          t2cnt*sizeof(struct PJoin2))/(1024.*1024.));
# Line 511 | Line 633 | FILE   *fp;
633  
634   void
635   freemesh(ms)                    /* free mesh data */
636 < MESH    *ms;
636 > register MESH   *ms;
637   {
638          MESH    mhead;
639          MESH    *msp;
# Line 537 | Line 659 | MESH   *ms;
659                                  /* free mesh data */
660          freestr(ms->name);
661          octfree(ms->mcube.cutree);
662 <        if (ms->cdata != NULL)
541 <                lu_done((LUTAB *)ms->cdata);
662 >        lu_done(&ms->lut);
663          if (ms->npatches > 0) {
664                  register MESHPATCH      *pp = ms->patch + ms->npatches;
665                  while (pp-- > ms->patch) {
# Line 557 | Line 678 | MESH   *ms;
678                  }
679                  free((void *)ms->patch);
680          }
681 +        if (ms->pseudo != NULL)
682 +                free((void *)ms->pseudo);
683          free((void *)ms);
684   }
685  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines