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.1 by greg, Tue Mar 11 17:08:55 2003 UTC vs.
Revision 2.9 by schorsch, Mon Jun 30 14:59: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 <        int4            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 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 135 | Line 137 | int    flags;
137  
138  
139   int
140 < getmeshtrivid(tvid, mp, ti)             /* get triangle vertex ID's */
141 < int4            tvid[3];
142 < register MESH   *mp;
143 < OBJECT          ti;
140 > getmeshtrivid(tvid, mo, mp, ti)         /* get triangle vertex ID's */
141 > int32   tvid[3];
142 > OBJECT  *mo;
143 > MESH    *mp;
144 > OBJECT  ti;
145   {
146          int             pn = ti >> 10;
147 +        MESHPATCH       *pp;
148  
149          if (pn >= mp->npatches)
150                  return(0);
151 +        pp = &mp->patch[pn];
152          ti &= 0x3ff;
153          if (!(ti & 0x200)) {            /* local triangle */
154                  struct PTri     *tp;
155 <                if (ti >= mp->patch[pn].ntris)
155 >                if (ti >= pp->ntris)
156                          return(0);
157 <                tp = &mp->patch[pn].tri[ti];
157 >                tp = &pp->tri[ti];
158                  tvid[0] = tvid[1] = tvid[2] = pn << 8;
159                  tvid[0] |= tp->v1;
160                  tvid[1] |= tp->v2;
161                  tvid[2] |= tp->v3;
162 +                if (pp->trimat != NULL)
163 +                        *mo = pp->trimat[ti];
164 +                else
165 +                        *mo = pp->solemat;
166 +                if (*mo != OVOID)
167 +                        *mo += mp->mat0;
168                  return(1);
169          }
170          ti &= ~0x200;
171          if (!(ti & 0x100)) {            /* single link vertex */
172                  struct PJoin1   *tp1;
173 <                if (ti >= mp->patch[pn].nj1tris)
173 >                if (ti >= pp->nj1tris)
174                          return(0);
175 <                tp1 = &mp->patch[pn].j1tri[ti];
175 >                tp1 = &pp->j1tri[ti];
176                  tvid[0] = tp1->v1j;
177                  tvid[1] = tvid[2] = pn << 8;
178                  tvid[1] |= tp1->v2;
179                  tvid[2] |= tp1->v3;
180 +                if ((*mo = tp1->mat) != OVOID)
181 +                        *mo += mp->mat0;
182                  return(1);
183          }
184          ti &= ~0x100;
185          {                               /* double link vertex */
186                  struct PJoin2   *tp2;
187 <                if (ti >= mp->patch[pn].nj2tris)
187 >                if (ti >= pp->nj2tris)
188                          return(0);
189 <                tp2 = &mp->patch[pn].j2tri[ti];
189 >                tp2 = &pp->j2tri[ti];
190                  tvid[0] = tp2->v1j;
191                  tvid[1] = tp2->v2j;
192                  tvid[2] = pn << 8 | tp2->v3;
193 +                if ((*mo = tp2->mat) != OVOID)
194 +                        *mo += mp->mat0;
195          }
196          return(1);
197   }
# Line 185 | Line 200 | OBJECT         ti;
200   int
201   getmeshvert(vp, mp, vid, what)  /* get triangle vertex from ID */
202   MESHVERT        *vp;
203 < register MESH   *mp;
204 < int4            vid;
203 > MESH            *mp;
204 > int32           vid;
205   int             what;
206   {
207          int             pn = vid >> 8;
# Line 217 | Line 232 | int            what;
232                                          /* get (u,v) */
233          if (what & MT_UV && pp->uv != NULL && pp->uv[vid][0]) {
234                  for (i = 0; i < 2; i++)
235 <                        vp->uv[i] = mp->uvlim[i][0] +
236 <                                (mp->uvlim[i][1] - mp->uvlim[i][0])*
235 >                        vp->uv[i] = mp->uvlim[0][i] +
236 >                                (mp->uvlim[1][i] - mp->uvlim[0][i])*
237                                  (pp->uv[vid][i] + .5)*(1./4294967296.);
238                  vp->fl |= MT_UV;
239          }
# Line 226 | Line 241 | int            what;
241   }
242  
243  
244 + OBJREC *
245 + getmeshpseudo(mp, mo)           /* get mesh pseudo object for material */
246 + MESH    *mp;
247 + OBJECT  mo;
248 + {
249 +        if (mo < mp->mat0 || mo >= mp->mat0 + mp->nmats)
250 +                error(INTERNAL, "modifier out of range in getmeshpseudo");
251 +        if (mp->pseudo == NULL) {
252 +                register int    i;
253 +                mp->pseudo = (OBJREC *)calloc(mp->nmats, sizeof(OBJREC));
254 +                if (mp->pseudo == NULL)
255 +                        error(SYSTEM, "out of memory in getmeshpseudo");
256 +                for (i = mp->nmats; i--; ) {
257 +                        mp->pseudo[i].omod = mp->mat0 + i;
258 +                        mp->pseudo[i].otype = OBJ_FACE;
259 +                        mp->pseudo[i].oname = "M-Tri";
260 +                }
261 +        }
262 +        return(&mp->pseudo[mo - mp->mat0]);
263 + }
264 +
265 +
266   int
267 < getmeshtri(tv, mp, ti, what)    /* get triangle vertices */
267 > getmeshtri(tv, mo, mp, ti, wha) /* get triangle vertices */
268   MESHVERT        tv[3];
269 + OBJECT          *mo;
270   MESH            *mp;
271   OBJECT          ti;
272 < int             what;
272 > int             wha;
273   {
274 <        int4    tvid[3];
274 >        int32   tvid[3];
275  
276 <        if (!getmeshtrivid(tvid, mp, ti))
276 >        if (!getmeshtrivid(tvid, mo, mp, ti))
277                  return(0);
278  
279 <        getmeshvert(&tv[0], mp, tvid[0], what);
280 <        getmeshvert(&tv[1], mp, tvid[1], what);
281 <        getmeshvert(&tv[2], mp, tvid[2], what);
279 >        getmeshvert(&tv[0], mp, tvid[0], wha);
280 >        getmeshvert(&tv[1], mp, tvid[1], wha);
281 >        getmeshvert(&tv[2], mp, tvid[2], wha);
282  
283          return(tv[0].fl & tv[1].fl & tv[2].fl);
284   }
285  
286  
287 < int4
287 > int32
288   addmeshvert(mp, vp)             /* find/add a mesh vertex */
289   register MESH   *mp;
290   MESHVERT        *vp;
291   {
254        LUTAB           *ltp;
292          LUENT           *lvp;
293          MCVERT          cv;
294          register int    i;
# Line 264 | Line 301 | MESHVERT       *vp;
301                          return(-1);
302                  if (vp->v[i] >= mp->mcube.cuorg[i] + mp->mcube.cusize)
303                          return(-1);
304 <                cv.xyz[i] = (uint4)(4294967296. *
304 >                cv.xyz[i] = (uint32)(4294967296. *
305                                  (vp->v[i] - mp->mcube.cuorg[i]) /
306                                  mp->mcube.cusize);
307          }
# Line 272 | Line 309 | MESHVERT       *vp;
309                  cv.norm = encodedir(vp->n);
310          if (vp->fl & MT_UV)
311                  for (i = 0; i < 2; i++) {
312 <                        if (vp->uv[i] <= mp->uvlim[i][0])
312 >                        if (vp->uv[i] <= mp->uvlim[0][i])
313                                  return(-1);
314 <                        if (vp->uv[i] >= mp->uvlim[i][1])
314 >                        if (vp->uv[i] >= mp->uvlim[1][i])
315                                  return(-1);
316 <                        cv.uv[i] = (uint4)(4294967296. *
317 <                                        (vp->uv[i] - mp->uvlim[i][0]) /
318 <                                        (mp->uvlim[i][1] - mp->uvlim[i][0]));
316 >                        cv.uv[i] = (uint32)(4294967296. *
317 >                                        (vp->uv[i] - mp->uvlim[0][i]) /
318 >                                        (mp->uvlim[1][i] - mp->uvlim[0][i]));
319                  }
320          cv.fl = vp->fl;
321 <        ltp = (LUTAB *)mp->cdata;       /* get lookup table */
322 <        if (ltp == NULL) {
323 <                ltp = (LUTAB *)calloc(1, sizeof(LUTAB));
324 <                if (ltp == NULL)
321 >        if (mp->lut.tsiz == 0) {
322 >                mp->lut.hashf = cvhash;
323 >                mp->lut.keycmp = cvcmp;
324 >                mp->lut.freek = free;
325 >                if (!lu_init(&mp->lut, 50000))
326                          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;
327          }
328                                          /* find entry */
329 <        lvp = lu_find(ltp, (char *)&cv);
329 >        lvp = lu_find(&mp->lut, (char *)&cv);
330          if (lvp == NULL)
331                  goto nomem;
332          if (lvp->key == NULL) {
333 <                lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int4));
334 <                bcopy((void *)&cv, (void *)lvp->key, sizeof(MCVERT));
333 >                lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int32));
334 >                memcpy((void *)lvp->key, (void *)&cv, sizeof(MCVERT));
335          }
336          if (lvp->data == NULL) {        /* new vertex */
337                  register MESHPATCH      *pp;
# Line 315 | Line 347 | MESHVERT       *vp;
347                                                  (void *)mp->patch,
348                                          (mp->npatches + MPATCHBLKSIZ)*
349                                                  sizeof(MESHPATCH));
350 <                                bzero((void *)(mp->patch + mp->npatches),
350 >                                memset((void *)(mp->patch + mp->npatches), '\0',
351                                          MPATCHBLKSIZ*sizeof(MESHPATCH));
352                          }
353 <                        if (mp->npatches++ >= 1<<20)
353 >                        if (mp->npatches++ >= 1L<<22)
354                                  error(INTERNAL, "too many mesh patches");
355                  }
356                  pp = &mp->patch[mp->npatches-1];
357                  if (pp->xyz == NULL) {
358 <                        pp->xyz = (uint4 (*)[3])calloc(256, 3*sizeof(int4));
358 >                        pp->xyz = (uint32 (*)[3])calloc(256, 3*sizeof(int32));
359                          if (pp->xyz == NULL)
360                                  goto nomem;
361                  }
# Line 331 | Line 363 | MESHVERT       *vp;
363                          pp->xyz[pp->nverts][i] = cv.xyz[i];
364                  if (cv.fl & MT_N) {
365                          if (pp->norm == NULL) {
366 <                                pp->norm = (int4 *)calloc(256, sizeof(int4));
366 >                                pp->norm = (int32 *)calloc(256, sizeof(int32));
367                                  if (pp->norm == NULL)
368                                          goto nomem;
369                          }
# Line 339 | Line 371 | MESHVERT       *vp;
371                  }
372                  if (cv.fl & MT_UV) {
373                          if (pp->uv == NULL) {
374 <                                pp->uv = (uint4 (*)[2])calloc(256,
375 <                                                2*sizeof(uint4));
374 >                                pp->uv = (uint32 (*)[2])calloc(256,
375 >                                                2*sizeof(uint32));
376                                  if (pp->uv == NULL)
377                                          goto nomem;
378                          }
# Line 349 | Line 381 | MESHVERT       *vp;
381                  }
382                  pp->nverts++;
383                  lvp->data = lvp->key + sizeof(MCVERT);
384 <                *(int4 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1);
384 >                *(int32 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1);
385          }
386 <        return(*(int4 *)lvp->data);
386 >        return(*(int32 *)lvp->data);
387   nomem:
388          error(SYSTEM, "out of memory in addmeshvert");
389          return(-1);
# Line 359 | Line 391 | nomem:
391  
392  
393   OBJECT
394 < addmeshtri(mp, tv)              /* add a new mesh triangle */
394 > addmeshtri(mp, tv, mo)          /* add a new mesh triangle */
395   MESH            *mp;
396   MESHVERT        tv[3];
397 + OBJECT          mo;
398   {
399 <        int4                    vid[3], t;
399 >        int32                   vid[3], t;
400          int                     pn[3], i;
401          register MESHPATCH      *pp;
402  
# Line 375 | Line 408 | MESHVERT       tv[3];
408                          return(OVOID);
409                  pn[i] = vid[i] >> 8;
410          }
411 +                                /* normalize material index */
412 +        if (mo != OVOID)
413 +                if ((mo -= mp->mat0) >= mp->nmats)
414 +                        mp->nmats = mo+1;
415 +                else if (mo < 0)
416 +                        error(INTERNAL, "modifier range error in addmeshtri");
417                                  /* assign triangle */
418          if (pn[0] == pn[1] && pn[1] == pn[2]) { /* local case */
419                  pp = &mp->patch[pn[0]];
# Line 384 | Line 423 | MESHVERT       tv[3];
423                          if (pp->tri == NULL)
424                                  goto nomem;
425                  }
426 <                if (pp->ntris >= 512)
427 <                        goto toomany;
428 <                pp->tri[pp->ntris].v1 = vid[0] & 0xff;
429 <                pp->tri[pp->ntris].v2 = vid[1] & 0xff;
430 <                pp->tri[pp->ntris].v3 = vid[2] & 0xff;
431 <
432 <                return(pn[0] << 10 | pp->ntris++);
426 >                if (pp->ntris < 512) {
427 >                        pp->tri[pp->ntris].v1 = vid[0] & 0xff;
428 >                        pp->tri[pp->ntris].v2 = vid[1] & 0xff;
429 >                        pp->tri[pp->ntris].v3 = vid[2] & 0xff;
430 >                        if (pp->ntris == 0)
431 >                                pp->solemat = mo;
432 >                        else if (pp->trimat == NULL && mo != pp->solemat) {
433 >                                pp->trimat = (int16 *)malloc(
434 >                                                512*sizeof(int16));
435 >                                if (pp->trimat == NULL)
436 >                                        goto nomem;
437 >                                for (i = pp->ntris; i--; )
438 >                                        pp->trimat[i] = pp->solemat;
439 >                        }
440 >                        if (pp->trimat != NULL)
441 >                                pp->trimat[pp->ntris] = mo;
442 >                        return(pn[0] << 10 | pp->ntris++);
443 >                }
444          }
445          if (pn[0] == pn[1]) {
446 <                t = vid[2]; vid[2] = vid[0]; vid[0] = t;
447 <                i = pn[2]; pn[2] = pn[0]; pn[0] = i;
446 >                t = vid[2]; vid[2] = vid[1]; vid[1] = vid[0]; vid[0] = t;
447 >                i = pn[2]; pn[2] = pn[1]; pn[1] = pn[0]; pn[0] = i;
448          } else if (pn[0] == pn[2]) {
449 <                t = vid[0]; vid[0] = vid[1]; vid[1] = t;
450 <                i = pn[0]; pn[0] = pn[1]; pn[1] = i;
449 >                t = vid[0]; vid[0] = vid[1]; vid[1] = vid[2]; vid[2] = t;
450 >                i = pn[0]; pn[0] = pn[1]; pn[1] = pn[2]; pn[2] = i;
451          }
452          if (pn[1] == pn[2]) {                   /* single link */
453                  pp = &mp->patch[pn[1]];
# Line 407 | Line 457 | MESHVERT       tv[3];
457                          if (pp->j1tri == NULL)
458                                  goto nomem;
459                  }
460 <                if (pp->nj1tris >= 256)
461 <                        goto toomany;
462 <                pp->j1tri[pp->nj1tris].v1j = pn[0] << 8 | (vid[0] & 0xff);
463 <                pp->j1tri[pp->nj1tris].v2 = vid[1] & 0xff;
464 <                pp->j1tri[pp->nj1tris].v3 = vid[2] & 0xff;
465 <                
466 <                return(pn[1] << 10 | 0x200 | pp->nj1tris++);
460 >                if (pp->nj1tris < 256) {
461 >                        pp->j1tri[pp->nj1tris].v1j = vid[0];
462 >                        pp->j1tri[pp->nj1tris].v2 = vid[1] & 0xff;
463 >                        pp->j1tri[pp->nj1tris].v3 = vid[2] & 0xff;
464 >                        pp->j1tri[pp->nj1tris].mat = mo;
465 >                        return(pn[1] << 10 | 0x200 | pp->nj1tris++);
466 >                }
467          }
468                                                  /* double link */
469          pp = &mp->patch[pn[2]];
# Line 424 | Line 474 | MESHVERT       tv[3];
474                          goto nomem;
475          }
476          if (pp->nj2tris >= 256)
477 <                goto toomany;
478 <        pp->j2tri[pp->nj2tris].v1j = pn[0] << 8 | (vid[0] & 0xff);
479 <        pp->j2tri[pp->nj2tris].v2j = pn[1] << 8 | (vid[1] & 0xff);
477 >                error(INTERNAL, "too many patch triangles in addmeshtri");
478 >        pp->j2tri[pp->nj2tris].v1j = vid[0];
479 >        pp->j2tri[pp->nj2tris].v2j = vid[1];
480          pp->j2tri[pp->nj2tris].v3 = vid[2] & 0xff;
481 <        
481 >        pp->j2tri[pp->nj2tris].mat = mo;
482          return(pn[2] << 10 | 0x300 | pp->nj2tris++);
483   nomem:
484          error(SYSTEM, "out of memory in addmeshtri");
485          return(OVOID);
436 toomany:
437        error(CONSISTENCY, "too many patch triangles in addmeshtri");
438        return(OVOID);
486   }
487  
488  
489 + char *
490 + checkmesh(mp)                           /* validate mesh data */
491 + register MESH   *mp;
492 + {
493 +        static char     embuf[128];
494 +        int             nouvbounds = 1;
495 +        register int    i;
496 +                                        /* basic checks */
497 +        if (mp == NULL)
498 +                return("NULL mesh pointer");
499 +        if (!mp->ldflags)
500 +                return("unassigned mesh");
501 +        if (mp->name == NULL)
502 +                return("missing mesh name");
503 +        if (mp->nref <= 0)
504 +                return("unreferenced mesh");
505 +                                        /* check boundaries */
506 +        if (mp->ldflags & IO_BOUNDS) {
507 +                if (mp->mcube.cusize <= FTINY)
508 +                        return("illegal octree bounds in mesh");
509 +                nouvbounds = (mp->uvlim[1][0] - mp->uvlim[0][0] <= FTINY ||
510 +                                mp->uvlim[1][1] - mp->uvlim[0][1] <= FTINY);
511 +        }
512 +                                        /* check octree */
513 +        if (mp->ldflags & IO_TREE) {
514 +                if (isempty(mp->mcube.cutree))
515 +                        error(WARNING, "empty mesh octree");
516 +        }
517 +                                        /* check scene data */
518 +        if (mp->ldflags & IO_SCENE) {
519 +                if (!(mp->ldflags & IO_BOUNDS))
520 +                        return("unbounded scene in mesh");
521 +                if (mp->mat0 < 0 || mp->mat0+mp->nmats > nobjects)
522 +                        return("bad mesh modifier range");
523 +                for (i = mp->mat0+mp->nmats; i-- > mp->mat0; ) {
524 +                        int     otyp = objptr(i)->otype;
525 +                        if (!ismodifier(otyp)) {
526 +                                sprintf(embuf,
527 +                                        "non-modifier in mesh (%s \"%s\")",
528 +                                        ofun[otyp].funame, objptr(i)->oname);
529 +                                return(embuf);
530 +                        }
531 +                }
532 +                if (mp->npatches <= 0)
533 +                        error(WARNING, "no patches in mesh");
534 +                for (i = 0; i < mp->npatches; i++) {
535 +                        register MESHPATCH      *pp = &mp->patch[i];
536 +                        if (pp->nverts <= 0)
537 +                                error(WARNING, "no vertices in patch");
538 +                        else {
539 +                                if (pp->xyz == NULL)
540 +                                        return("missing patch vertex list");
541 +                                if (nouvbounds && pp->uv != NULL)
542 +                                        return("unreferenced uv coordinates");
543 +                        }
544 +                        if (pp->ntris + pp->nj1tris + pp->nj2tris <= 0)
545 +                                error(WARNING, "no triangles in patch");
546 +                        if (pp->ntris > 0 && pp->tri == NULL)
547 +                                return("missing patch triangle list");
548 +                        if (pp->nj1tris > 0 && pp->j1tri == NULL)
549 +                                return("missing patch joiner triangle list");
550 +                        if (pp->nj2tris > 0 && pp->j2tri == NULL)
551 +                                return("missing patch double-joiner list");
552 +                }
553 +        }
554 +        return(NULL);                   /* seems OK */
555 + }
556 +
557 +
558   static void
559   tallyoctree(ot, ecp, lcp, ocp)  /* tally octree size */
560   OCTREE  ot;
# Line 494 | Line 610 | FILE   *fp;
610                  t2cnt += pp->nj2tris;
611          }
612          fprintf(fp, "Mesh statistics:\n");
613 +        fprintf(fp, "\t%d materials\n", ms->nmats);
614          fprintf(fp, "\t%d patches (%.2f MBytes)\n", ms->npatches,
615                          (ms->npatches*sizeof(MESHPATCH) +
616 <                        vcnt*3*sizeof(uint4) +
617 <                        nscnt*sizeof(int4) +
618 <                        uvscnt*2*sizeof(uint4) +
616 >                        vcnt*3*sizeof(uint32) +
617 >                        nscnt*sizeof(int32) +
618 >                        uvscnt*2*sizeof(uint32) +
619                          tcnt*sizeof(struct PTri) +
620                          t1cnt*sizeof(struct PJoin1) +
621                          t2cnt*sizeof(struct PJoin2))/(1024.*1024.));
# Line 517 | Line 634 | FILE   *fp;
634  
635   void
636   freemesh(ms)                    /* free mesh data */
637 < MESH    *ms;
637 > register MESH   *ms;
638   {
639          MESH    mhead;
640          MESH    *msp;
# Line 543 | Line 660 | MESH   *ms;
660                                  /* free mesh data */
661          freestr(ms->name);
662          octfree(ms->mcube.cutree);
663 <        if (ms->cdata != NULL)
547 <                lu_done((LUTAB *)ms->cdata);
663 >        lu_done(&ms->lut);
664          if (ms->npatches > 0) {
665                  register MESHPATCH      *pp = ms->patch + ms->npatches;
666                  while (pp-- > ms->patch) {
# Line 563 | Line 679 | MESH   *ms;
679                  }
680                  free((void *)ms->patch);
681          }
682 +        if (ms->pseudo != NULL)
683 +                free((void *)ms->pseudo);
684          free((void *)ms);
685   }
686  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines