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.16 by greg, Fri Jan 30 00:08:31 2004 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 "octree.h"
14   #include "object.h"
15 + #include "otypes.h"
16   #include "mesh.h"
12 #include "lookup.h"
17  
18   /* An encoded mesh vertex */
19   typedef struct {
20          int             fl;
21 <        uint4           xyz[3];
22 <        int4            norm;
23 <        uint4           uv[2];
21 >        uint32          xyz[3];
22 >        int32           norm;
23 >        uint32          uv[2];
24   } MCVERT;
25  
26   #define  MPATCHBLKSIZ   128             /* patch allocation block size */
# Line 68 | Line 72 | register MCVERT        *v1, *v2;
72  
73  
74   MESH *
75 < getmesh(mname, flags)                   /* get mesh data */
75 > getmesh(mname, flags)                   /* get new mesh data reference */
76   char    *mname;
77   int     flags;
78   {
# Line 78 | Line 82 | int    flags;
82          flags &= IO_LEGAL;
83          for (ms = mlist; ms != NULL; ms = ms->next)
84                  if (!strcmp(mname, ms->name)) {
85 <                        if ((ms->ldflags & flags) == flags) {
86 <                                ms->nref++;
83 <                                return(ms);             /* loaded */
84 <                        }
85 <                        break;                  /* load the rest */
85 >                        ms->nref++;     /* increase reference count */
86 >                        break;
87                  }
88 <        if (ms == NULL) {
88 >        if (ms == NULL) {               /* load first time */
89                  ms = (MESH *)calloc(1, sizeof(MESH));
90                  if (ms == NULL)
91                          error(SYSTEM, "out of memory in getmesh");
# Line 94 | Line 95 | int    flags;
95                  ms->next = mlist;
96                  mlist = ms;
97          }
98 <        if ((pathname = getpath(mname, getlibpath(), R_OK)) == NULL) {
98 >        if ((pathname = getpath(mname, getrlibpath(), R_OK)) == NULL) {
99                  sprintf(errmsg, "cannot find mesh file \"%s\"", mname);
100                  error(USER, errmsg);
101          }
# Line 128 | Line 129 | int    flags;
129                  ins->msh = NULL;
130                  o->os = (char *)ins;
131          }
132 <        if (ins->msh == NULL || (ins->msh->ldflags & flags) != flags)
132 >        if (ins->msh == NULL)
133                  ins->msh = getmesh(o->oargs.sarg[0], flags);
134 +        else if ((flags &= ~ins->msh->ldflags))
135 +                readmesh(ins->msh,
136 +                        getpath(o->oargs.sarg[0], getrlibpath(), R_OK),
137 +                                flags);
138          return(ins);
139   }
140  
141  
142   int
143 < getmeshtrivid(tvid, mp, ti)             /* get triangle vertex ID's */
144 < int4            tvid[3];
145 < register MESH   *mp;
146 < OBJECT          ti;
143 > getmeshtrivid(tvid, mo, mp, ti)         /* get triangle vertex ID's */
144 > int32   tvid[3];
145 > OBJECT  *mo;
146 > MESH    *mp;
147 > OBJECT  ti;
148   {
149          int             pn = ti >> 10;
150 +        MESHPATCH       *pp;
151  
152          if (pn >= mp->npatches)
153                  return(0);
154 +        pp = &mp->patch[pn];
155          ti &= 0x3ff;
156          if (!(ti & 0x200)) {            /* local triangle */
157                  struct PTri     *tp;
158 <                if (ti >= mp->patch[pn].ntris)
158 >                if (ti >= pp->ntris)
159                          return(0);
160 <                tp = &mp->patch[pn].tri[ti];
160 >                tp = &pp->tri[ti];
161                  tvid[0] = tvid[1] = tvid[2] = pn << 8;
162                  tvid[0] |= tp->v1;
163                  tvid[1] |= tp->v2;
164                  tvid[2] |= tp->v3;
165 +                if (pp->trimat != NULL)
166 +                        *mo = pp->trimat[ti];
167 +                else
168 +                        *mo = pp->solemat;
169 +                if (*mo != OVOID)
170 +                        *mo += mp->mat0;
171                  return(1);
172          }
173          ti &= ~0x200;
174          if (!(ti & 0x100)) {            /* single link vertex */
175                  struct PJoin1   *tp1;
176 <                if (ti >= mp->patch[pn].nj1tris)
176 >                if (ti >= pp->nj1tris)
177                          return(0);
178 <                tp1 = &mp->patch[pn].j1tri[ti];
178 >                tp1 = &pp->j1tri[ti];
179                  tvid[0] = tp1->v1j;
180                  tvid[1] = tvid[2] = pn << 8;
181                  tvid[1] |= tp1->v2;
182                  tvid[2] |= tp1->v3;
183 +                if ((*mo = tp1->mat) != OVOID)
184 +                        *mo += mp->mat0;
185                  return(1);
186          }
187          ti &= ~0x100;
188          {                               /* double link vertex */
189                  struct PJoin2   *tp2;
190 <                if (ti >= mp->patch[pn].nj2tris)
190 >                if (ti >= pp->nj2tris)
191                          return(0);
192 <                tp2 = &mp->patch[pn].j2tri[ti];
192 >                tp2 = &pp->j2tri[ti];
193                  tvid[0] = tp2->v1j;
194                  tvid[1] = tp2->v2j;
195                  tvid[2] = pn << 8 | tp2->v3;
196 +                if ((*mo = tp2->mat) != OVOID)
197 +                        *mo += mp->mat0;
198          }
199          return(1);
200   }
# Line 185 | Line 203 | OBJECT         ti;
203   int
204   getmeshvert(vp, mp, vid, what)  /* get triangle vertex from ID */
205   MESHVERT        *vp;
206 < register MESH   *mp;
207 < int4            vid;
206 > MESH            *mp;
207 > int32           vid;
208   int             what;
209   {
210          int             pn = vid >> 8;
# Line 226 | Line 244 | int            what;
244   }
245  
246  
247 + OBJREC *
248 + getmeshpseudo(mp, mo)           /* get mesh pseudo object for material */
249 + MESH    *mp;
250 + OBJECT  mo;
251 + {
252 +        if (mo < mp->mat0 || mo >= mp->mat0 + mp->nmats)
253 +                error(INTERNAL, "modifier out of range in getmeshpseudo");
254 +        if (mp->pseudo == NULL) {
255 +                register int    i;
256 +                mp->pseudo = (OBJREC *)calloc(mp->nmats, sizeof(OBJREC));
257 +                if (mp->pseudo == NULL)
258 +                        error(SYSTEM, "out of memory in getmeshpseudo");
259 +                for (i = mp->nmats; i--; ) {
260 +                        mp->pseudo[i].omod = mp->mat0 + i;
261 +                        mp->pseudo[i].otype = OBJ_FACE;
262 +                        mp->pseudo[i].oname = "M-Tri";
263 +                }
264 +        }
265 +        return(&mp->pseudo[mo - mp->mat0]);
266 + }
267 +
268 +
269   int
270 < getmeshtri(tv, mp, ti, what)    /* get triangle vertices */
270 > getmeshtri(tv, mo, mp, ti, wha) /* get triangle vertices */
271   MESHVERT        tv[3];
272 + OBJECT          *mo;
273   MESH            *mp;
274   OBJECT          ti;
275 < int             what;
275 > int             wha;
276   {
277 <        int4    tvid[3];
277 >        int32   tvid[3];
278  
279 <        if (!getmeshtrivid(tvid, mp, ti))
279 >        if (!getmeshtrivid(tvid, mo, mp, ti))
280                  return(0);
281  
282 <        getmeshvert(&tv[0], mp, tvid[0], what);
283 <        getmeshvert(&tv[1], mp, tvid[1], what);
284 <        getmeshvert(&tv[2], mp, tvid[2], what);
282 >        getmeshvert(&tv[0], mp, tvid[0], wha);
283 >        getmeshvert(&tv[1], mp, tvid[1], wha);
284 >        getmeshvert(&tv[2], mp, tvid[2], wha);
285  
286          return(tv[0].fl & tv[1].fl & tv[2].fl);
287   }
288  
289  
290 < int4
290 > int32
291   addmeshvert(mp, vp)             /* find/add a mesh vertex */
292   register MESH   *mp;
293   MESHVERT        *vp;
294   {
254        LUTAB           *ltp;
295          LUENT           *lvp;
296          MCVERT          cv;
297          register int    i;
# Line 264 | Line 304 | MESHVERT       *vp;
304                          return(-1);
305                  if (vp->v[i] >= mp->mcube.cuorg[i] + mp->mcube.cusize)
306                          return(-1);
307 <                cv.xyz[i] = (uint4)(4294967296. *
307 >                cv.xyz[i] = (uint32)(4294967296. *
308                                  (vp->v[i] - mp->mcube.cuorg[i]) /
309                                  mp->mcube.cusize);
310          }
311 <        if (vp->fl & MT_N)
311 >        if (vp->fl & MT_N)              /* assumes normalized! */
312                  cv.norm = encodedir(vp->n);
313          if (vp->fl & MT_UV)
314                  for (i = 0; i < 2; i++) {
# Line 276 | Line 316 | MESHVERT       *vp;
316                                  return(-1);
317                          if (vp->uv[i] >= mp->uvlim[1][i])
318                                  return(-1);
319 <                        cv.uv[i] = (uint4)(4294967296. *
319 >                        cv.uv[i] = (uint32)(4294967296. *
320                                          (vp->uv[i] - mp->uvlim[0][i]) /
321                                          (mp->uvlim[1][i] - mp->uvlim[0][i]));
322                  }
323          cv.fl = vp->fl;
324 <        ltp = (LUTAB *)mp->cdata;       /* get lookup table */
325 <        if (ltp == NULL) {
326 <                ltp = (LUTAB *)calloc(1, sizeof(LUTAB));
327 <                if (ltp == NULL)
324 >        if (mp->lut.tsiz == 0) {
325 >                mp->lut.hashf = cvhash;
326 >                mp->lut.keycmp = cvcmp;
327 >                mp->lut.freek = free;
328 >                if (!lu_init(&mp->lut, 50000))
329                          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;
330          }
331                                          /* find entry */
332 <        lvp = lu_find(ltp, (char *)&cv);
332 >        lvp = lu_find(&mp->lut, (char *)&cv);
333          if (lvp == NULL)
334                  goto nomem;
335          if (lvp->key == NULL) {
336 <                lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int4));
337 <                bcopy((void *)&cv, (void *)lvp->key, sizeof(MCVERT));
336 >                lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int32));
337 >                memcpy((void *)lvp->key, (void *)&cv, sizeof(MCVERT));
338          }
339          if (lvp->data == NULL) {        /* new vertex */
340                  register MESHPATCH      *pp;
# Line 315 | Line 350 | MESHVERT       *vp;
350                                                  (void *)mp->patch,
351                                          (mp->npatches + MPATCHBLKSIZ)*
352                                                  sizeof(MESHPATCH));
353 <                                bzero((void *)(mp->patch + mp->npatches),
353 >                                memset((void *)(mp->patch + mp->npatches), '\0',
354                                          MPATCHBLKSIZ*sizeof(MESHPATCH));
355                          }
356                          if (mp->npatches++ >= 1L<<22)
# Line 323 | Line 358 | MESHVERT       *vp;
358                  }
359                  pp = &mp->patch[mp->npatches-1];
360                  if (pp->xyz == NULL) {
361 <                        pp->xyz = (uint4 (*)[3])calloc(256, 3*sizeof(int4));
361 >                        pp->xyz = (uint32 (*)[3])calloc(256, 3*sizeof(int32));
362                          if (pp->xyz == NULL)
363                                  goto nomem;
364                  }
# Line 331 | Line 366 | MESHVERT       *vp;
366                          pp->xyz[pp->nverts][i] = cv.xyz[i];
367                  if (cv.fl & MT_N) {
368                          if (pp->norm == NULL) {
369 <                                pp->norm = (int4 *)calloc(256, sizeof(int4));
369 >                                pp->norm = (int32 *)calloc(256, sizeof(int32));
370                                  if (pp->norm == NULL)
371                                          goto nomem;
372                          }
# Line 339 | Line 374 | MESHVERT       *vp;
374                  }
375                  if (cv.fl & MT_UV) {
376                          if (pp->uv == NULL) {
377 <                                pp->uv = (uint4 (*)[2])calloc(256,
378 <                                                2*sizeof(uint4));
377 >                                pp->uv = (uint32 (*)[2])calloc(256,
378 >                                                2*sizeof(uint32));
379                                  if (pp->uv == NULL)
380                                          goto nomem;
381                          }
# Line 349 | Line 384 | MESHVERT       *vp;
384                  }
385                  pp->nverts++;
386                  lvp->data = lvp->key + sizeof(MCVERT);
387 <                *(int4 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1);
387 >                *(int32 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1);
388          }
389 <        return(*(int4 *)lvp->data);
389 >        return(*(int32 *)lvp->data);
390   nomem:
391          error(SYSTEM, "out of memory in addmeshvert");
392          return(-1);
# Line 359 | Line 394 | nomem:
394  
395  
396   OBJECT
397 < addmeshtri(mp, tv)              /* add a new mesh triangle */
397 > addmeshtri(mp, tv, mo)          /* add a new mesh triangle */
398   MESH            *mp;
399   MESHVERT        tv[3];
400 + OBJECT          mo;
401   {
402 <        int4                    vid[3], t;
402 >        int32                   vid[3], t;
403          int                     pn[3], i;
404          register MESHPATCH      *pp;
405  
# Line 375 | Line 411 | MESHVERT       tv[3];
411                          return(OVOID);
412                  pn[i] = vid[i] >> 8;
413          }
414 +                                /* normalize material index */
415 +        if (mo != OVOID) {
416 +                if ((mo -= mp->mat0) >= mp->nmats)
417 +                        mp->nmats = mo+1;
418 +                else if (mo < 0)
419 +                        error(INTERNAL, "modifier range error in addmeshtri");
420 +        }
421                                  /* assign triangle */
422          if (pn[0] == pn[1] && pn[1] == pn[2]) { /* local case */
423                  pp = &mp->patch[pn[0]];
# Line 388 | Line 431 | MESHVERT       tv[3];
431                          pp->tri[pp->ntris].v1 = vid[0] & 0xff;
432                          pp->tri[pp->ntris].v2 = vid[1] & 0xff;
433                          pp->tri[pp->ntris].v3 = vid[2] & 0xff;
434 +                        if (pp->ntris == 0)
435 +                                pp->solemat = mo;
436 +                        else if (pp->trimat == NULL && mo != pp->solemat) {
437 +                                pp->trimat = (int16 *)malloc(
438 +                                                512*sizeof(int16));
439 +                                if (pp->trimat == NULL)
440 +                                        goto nomem;
441 +                                for (i = pp->ntris; i--; )
442 +                                        pp->trimat[i] = pp->solemat;
443 +                        }
444 +                        if (pp->trimat != NULL)
445 +                                pp->trimat[pp->ntris] = mo;
446                          return(pn[0] << 10 | pp->ntris++);
447                  }
448          }
# Line 410 | Line 465 | MESHVERT       tv[3];
465                          pp->j1tri[pp->nj1tris].v1j = vid[0];
466                          pp->j1tri[pp->nj1tris].v2 = vid[1] & 0xff;
467                          pp->j1tri[pp->nj1tris].v3 = vid[2] & 0xff;
468 +                        pp->j1tri[pp->nj1tris].mat = mo;
469                          return(pn[1] << 10 | 0x200 | pp->nj1tris++);
470                  }
471          }
# Line 426 | Line 482 | MESHVERT       tv[3];
482          pp->j2tri[pp->nj2tris].v1j = vid[0];
483          pp->j2tri[pp->nj2tris].v2j = vid[1];
484          pp->j2tri[pp->nj2tris].v3 = vid[2] & 0xff;
485 +        pp->j2tri[pp->nj2tris].mat = mo;
486          return(pn[2] << 10 | 0x300 | pp->nj2tris++);
487   nomem:
488          error(SYSTEM, "out of memory in addmeshtri");
# Line 433 | Line 490 | nomem:
490   }
491  
492  
493 + char *
494 + checkmesh(mp)                           /* validate mesh data */
495 + register MESH   *mp;
496 + {
497 +        static char     embuf[128];
498 +        int             nouvbounds = 1;
499 +        register int    i;
500 +                                        /* basic checks */
501 +        if (mp == NULL)
502 +                return("NULL mesh pointer");
503 +        if (!mp->ldflags)
504 +                return("unassigned mesh");
505 +        if (mp->name == NULL)
506 +                return("missing mesh name");
507 +        if (mp->nref <= 0)
508 +                return("unreferenced mesh");
509 +                                        /* check boundaries */
510 +        if (mp->ldflags & IO_BOUNDS) {
511 +                if (mp->mcube.cusize <= FTINY)
512 +                        return("illegal octree bounds in mesh");
513 +                nouvbounds = (mp->uvlim[1][0] - mp->uvlim[0][0] <= FTINY ||
514 +                                mp->uvlim[1][1] - mp->uvlim[0][1] <= FTINY);
515 +        }
516 +                                        /* check octree */
517 +        if (mp->ldflags & IO_TREE) {
518 +                if (isempty(mp->mcube.cutree))
519 +                        error(WARNING, "empty mesh octree");
520 +        }
521 +                                        /* check scene data */
522 +        if (mp->ldflags & IO_SCENE) {
523 +                if (!(mp->ldflags & IO_BOUNDS))
524 +                        return("unbounded scene in mesh");
525 +                if (mp->mat0 < 0 || mp->mat0+mp->nmats > nobjects)
526 +                        return("bad mesh modifier range");
527 +                for (i = mp->mat0+mp->nmats; i-- > mp->mat0; ) {
528 +                        int     otyp = objptr(i)->otype;
529 +                        if (!ismodifier(otyp)) {
530 +                                sprintf(embuf,
531 +                                        "non-modifier in mesh (%s \"%s\")",
532 +                                        ofun[otyp].funame, objptr(i)->oname);
533 +                                return(embuf);
534 +                        }
535 +                }
536 +                if (mp->npatches <= 0)
537 +                        error(WARNING, "no patches in mesh");
538 +                for (i = 0; i < mp->npatches; i++) {
539 +                        register MESHPATCH      *pp = &mp->patch[i];
540 +                        if (pp->nverts <= 0)
541 +                                error(WARNING, "no vertices in patch");
542 +                        else {
543 +                                if (pp->xyz == NULL)
544 +                                        return("missing patch vertex list");
545 +                                if (nouvbounds && pp->uv != NULL)
546 +                                        return("unreferenced uv coordinates");
547 +                        }
548 +                        if (pp->ntris + pp->nj1tris + pp->nj2tris <= 0)
549 +                                error(WARNING, "no triangles in patch");
550 +                        if (pp->ntris > 0 && pp->tri == NULL)
551 +                                return("missing patch triangle list");
552 +                        if (pp->nj1tris > 0 && pp->j1tri == NULL)
553 +                                return("missing patch joiner triangle list");
554 +                        if (pp->nj2tris > 0 && pp->j2tri == NULL)
555 +                                return("missing patch double-joiner list");
556 +                }
557 +        }
558 +        return(NULL);                   /* seems OK */
559 + }
560 +
561 +
562   static void
563   tallyoctree(ot, ecp, lcp, ocp)  /* tally octree size */
564   OCTREE  ot;
# Line 488 | Line 614 | FILE   *fp;
614                  t2cnt += pp->nj2tris;
615          }
616          fprintf(fp, "Mesh statistics:\n");
617 +        fprintf(fp, "\t%ld materials\n", ms->nmats);
618          fprintf(fp, "\t%d patches (%.2f MBytes)\n", ms->npatches,
619                          (ms->npatches*sizeof(MESHPATCH) +
620 <                        vcnt*3*sizeof(uint4) +
621 <                        nscnt*sizeof(int4) +
622 <                        uvscnt*2*sizeof(uint4) +
620 >                        vcnt*3*sizeof(uint32) +
621 >                        nscnt*sizeof(int32) +
622 >                        uvscnt*2*sizeof(uint32) +
623                          tcnt*sizeof(struct PTri) +
624                          t1cnt*sizeof(struct PJoin1) +
625                          t2cnt*sizeof(struct PJoin2))/(1024.*1024.));
# Line 511 | Line 638 | FILE   *fp;
638  
639   void
640   freemesh(ms)                    /* free mesh data */
641 < MESH    *ms;
641 > register MESH   *ms;
642   {
643          MESH    mhead;
644          MESH    *msp;
# Line 537 | Line 664 | MESH   *ms;
664                                  /* free mesh data */
665          freestr(ms->name);
666          octfree(ms->mcube.cutree);
667 <        if (ms->cdata != NULL)
541 <                lu_done((LUTAB *)ms->cdata);
667 >        lu_done(&ms->lut);
668          if (ms->npatches > 0) {
669                  register MESHPATCH      *pp = ms->patch + ms->npatches;
670                  while (pp-- > ms->patch) {
# Line 557 | Line 683 | MESH   *ms;
683                  }
684                  free((void *)ms->patch);
685          }
686 +        if (ms->pseudo != NULL)
687 +                free((void *)ms->pseudo);
688          free((void *)ms);
689   }
690  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines