--- ray/src/rt/o_mesh.c 2003/03/14 21:27:46 2.5 +++ ray/src/rt/o_mesh.c 2012/10/23 05:03:52 2.13 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: o_mesh.c,v 2.5 2003/03/14 21:27:46 greg Exp $"; +static const char RCSid[] = "$Id: o_mesh.c,v 2.13 2012/10/23 05:03:52 greg Exp $"; #endif /* * Routines for computing ray intersections with meshes. @@ -21,11 +21,12 @@ static const char RCSid[] = "$Id: o_mesh.c,v 2.5 2003/ #include "copyright.h" -#include "ray.h" +#include +#include "ray.h" #include "mesh.h" - #include "tmesh.h" +#include "rtotypes.h" #define EDGE_CACHE_SIZ 251 /* length of mesh edge cache */ @@ -39,7 +40,7 @@ struct EdgeCache { OBJREC *o; /* mesh object */ MESHINST *mi; /* current mesh instance */ struct EdgeSide { - int4 v1i, v2i; /* vertex indices (lowest first) */ + int32 v1i, v2i; /* vertex indices (lowest first) */ short signum; /* signed volume */ } cache[EDGE_CACHE_SIZ]; } edge_cache; @@ -52,31 +53,31 @@ OBJREC *o; /* get mesh instance */ edge_cache.mi = getmeshinst(edge_cache.o = o, IO_ALL); /* clear edge cache */ - bzero((void *)edge_cache.cache, sizeof(edge_cache.cache)); + memset((void *)edge_cache.cache, '\0', sizeof(edge_cache.cache)); } static int -signed_volume(r, v1, v2) /* get signed volume for ray and edge */ +volume_sign(r, v1, v2) /* get signed volume for ray and edge */ register RAY *r; -int4 v1, v2; +int32 v1, v2; { int reversed = 0; register struct EdgeSide *ecp; if (v1 > v2) { - int4 t = v2; v2 = v1; v1 = t; + int32 t = v2; v2 = v1; v1 = t; reversed = 1; } ecp = &edge_cache.cache[((v2<<11 ^ v1) & 0x7fffffff) % EDGE_CACHE_SIZ]; - if (ecp->v1i != v1 || ecp->v2i != v2) { + if ((ecp->v1i != v1) | (ecp->v2i != v2)) { MESHVERT tv1, tv2; /* compute signed volume */ FVECT v2d; double vol; - if (!getmeshvert(&tv1, edge_cache.mi->msh, v1, MT_V) || + if (!getmeshvert(&tv1, edge_cache.mi->msh, v1, MT_V) | !getmeshvert(&tv2, edge_cache.mi->msh, v2, MT_V)) objerror(edge_cache.o, INTERNAL, - "missing mesh vertex in signed_volume"); + "missing mesh vertex in volume_sign"); VSUB(v2d, tv2.v, r->rorg); vol = (tv1.v[0] - r->rorg[0]) * (v2d[1]*r->rdir[2] - v2d[2]*r->rdir[1]); @@ -84,12 +85,8 @@ int4 v1, v2; (v2d[2]*r->rdir[0] - v2d[0]*r->rdir[2]); vol += (tv1.v[2] - r->rorg[2]) * (v2d[0]*r->rdir[1] - v2d[1]*r->rdir[0]); - if (vol > .0) - ecp->signum = 1; - else if (vol < .0) - ecp->signum = -1; - else - ecp->signum = 0; + /* don't generate 0 */ + ecp->signum = vol > .0 ? 1 : -1; ecp->v1i = v1; ecp->v2i = v2; } @@ -102,7 +99,7 @@ mesh_hit(oset, r) /* intersect ray with mesh triangle OBJECT *oset; RAY *r; { - int4 tvi[3]; + int32 tvi[3]; int sv1, sv2, sv3; MESHVERT tv[3]; OBJECT tmod; @@ -114,12 +111,13 @@ RAY *r; if (!getmeshtrivid(tvi, &tmod, curmsh, oset[i])) objerror(edge_cache.o, INTERNAL, "missing triangle vertices in mesh_hit"); - sv1 = signed_volume(r, tvi[0], tvi[1]); - sv2 = signed_volume(r, tvi[1], tvi[2]); - sv3 = signed_volume(r, tvi[2], tvi[0]); - if (sv1 != sv2 || sv2 != sv3) /* compare volume signs */ - if (sv1 && sv2 && sv3) - continue; + sv1 = volume_sign(r, tvi[0], tvi[1]); + sv2 = volume_sign(r, tvi[1], tvi[2]); + if (sv1 != sv2) /* compare volume signs */ + continue; + sv3 = volume_sign(r, tvi[2], tvi[0]); + if (sv2 != sv3) + continue; /* compute intersection */ getmeshvert(&tv[0], curmsh, tvi[0], MT_V); getmeshvert(&tv[1], curmsh, tvi[1], MT_V); @@ -144,21 +142,22 @@ RAY *r; } -int -o_mesh(o, r) /* compute ray intersection with a mesh */ -OBJREC *o; -register RAY *r; +extern int +o_mesh( /* compute ray intersection with a mesh */ + OBJREC *o, + register RAY *r +) { RAY rcont; int flags; MESHVERT tv[3]; OBJECT tmod; - FLOAT wt[3]; + RREAL wt[3]; int i; /* get the mesh instance */ prep_edge_cache(o); /* copy and transform ray */ - copystruct(&rcont, r); + rcont = *r; multp3(rcont.rorg, r->rorg, curmi->x.b.xfm); multv3(rcont.rdir, r->rdir, curmi->x.b.xfm); for (i = 0; i < 3; i++) @@ -200,8 +199,7 @@ register RAY *r; wt[2]*tv[2].n[i]; multv3(r->pert, rcont.pert, curmi->x.f.xfm); if (normalize(r->pert) != 0.0) - for (i = 0; i < 3; i++) - r->pert[i] -= r->ron[i]; + VSUB(r->pert, r->pert, r->ron); } else r->pert[0] = r->pert[1] = r->pert[2] = .0;