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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines