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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines