--- ray/src/rt/o_cone.c 1990/12/15 15:03:28 1.3 +++ ray/src/rt/o_cone.c 2014/07/08 18:25:00 2.8 @@ -1,32 +1,30 @@ -/* Copyright (c) 1990 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: o_cone.c,v 2.8 2014/07/08 18:25:00 greg Exp $"; #endif - /* * o_cone.c - routine to determine ray intersection with cones. - * - * 2/13/86 */ -#include "ray.h" +#include "copyright.h" +#include "ray.h" #include "otypes.h" - +#include "rtotypes.h" #include "cone.h" -o_cone(o, r) /* intersect ray with cone */ -OBJREC *o; -register RAY *r; +int +o_cone( /* intersect ray with cone */ + OBJREC *o, + RAY *r +) { FVECT rox, rdx; double a, b, c; double root[2]; int nroots, rn; - register CONE *co; - register int i; + CONE *co; + int i; /* get cone structure */ co = getcone(o, 1); @@ -82,8 +80,7 @@ register RAY *r; return(0); /* outside radii */ r->ro = o; r->rot = root[0]; - for (i = 0; i < 3; i++) - r->rop[i] = r->rorg[i] + r->rdir[i]*r->rot; + VSUM(r->rop, r->rorg, r->rdir, r->rot); VCOPY(r->ron, co->ad); r->rod = -rdx[2]; r->rox = NULL; @@ -98,10 +95,8 @@ register RAY *r; if (root[rn] >= r->rot) break; /* too big */ /* check endpoints */ - for (i = 0; i < 3; i++) { - rox[i] = r->rorg[i] + root[rn]*r->rdir[i]; - rdx[i] = rox[i] - CO_P0(co)[i]; - } + VSUM(rox, r->rorg, r->rdir, root[rn]); + VSUB(rdx, rox, CO_P0(co)); b = DOT(rdx, co->ad); if (b < 0.0) continue; /* before p0 */ @@ -128,8 +123,15 @@ register RAY *r; if (o->otype == OBJ_CONE || o->otype == OBJ_CUP) for (i = 0; i < 3; i++) r->ron[i] = (co->al*r->ron[i] - c*co->ad[i]) - /co->sl; + / co->sl; + a = DOT(r->ron, r->ron); + if (a > 1.+FTINY || a < 1.-FTINY) { + c = 1./(.5 + .5*a); /* avoid numerical error */ + r->ron[0] *= c; r->ron[1] *= c; r->ron[2] *= c; + } r->rod = -DOT(r->rdir, r->ron); + r->pert[0] = r->pert[1] = r->pert[2] = 0.0; + r->uv[0] = r->uv[1] = 0.0; r->rox = NULL; return(1); /* good */ }