--- ray/src/rt/o_cone.c 1991/11/12 17:09:26 2.1 +++ ray/src/rt/o_cone.c 2023/03/16 00:25:24 2.12 @@ -1,35 +1,35 @@ -/* 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.12 2023/03/16 00:25:24 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); + if (co == NULL) + objerror(o, INTERNAL, "unexpected illegal"); /* * To intersect a ray with a cone, we transform the @@ -70,11 +70,11 @@ register RAY *r; } else { /* OBJ_RING */ - if (rdx[2] <= FTINY && rdx[2] >= -FTINY) + if ((rdx[2] <= FTINY) & (rdx[2] >= -FTINY)) return(0); /* parallel */ root[0] = -rox[2]/rdx[2]; - if (root[0] <= FTINY || root[0] >= r->rot) - return(0); /* distance check */ + if (rayreject(o, r, root[0], -rdx[2])) + return(0); /* have better */ b = root[0]*rdx[0] + rox[0]; c = root[0]*rdx[1] + rox[1]; a = b*b + c*c; @@ -82,10 +82,11 @@ 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->pert[0] = r->pert[1] = r->pert[2] = 0.0; + r->uv[0] = r->uv[1] = 0.0; r->rox = NULL; return(1); /* good */ } @@ -95,18 +96,18 @@ register RAY *r; for (rn = 0; rn < nroots; rn++) { /* check real roots */ if (root[rn] <= FTINY) continue; /* too small */ - if (root[rn] >= r->rot) + if (root[rn] > r->rot + FTINY) 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 */ if (b > co->al) continue; /* after p1 */ + if (rayreject(o, r, root[rn], 0)) + break; /* previous hit better */ r->ro = o; r->rot = root[rn]; VCOPY(r->rop, rox); @@ -128,8 +129,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 */ }