| 1 |
greg |
1.1 |
/* Copyright (c) 1986 Regents of the University of California */ |
| 2 |
|
|
|
| 3 |
|
|
#ifndef lint |
| 4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
| 5 |
|
|
#endif |
| 6 |
|
|
|
| 7 |
|
|
/* |
| 8 |
|
|
* o_cone.c - routine to determine ray intersection with cones. |
| 9 |
|
|
* |
| 10 |
|
|
* 2/13/86 |
| 11 |
|
|
*/ |
| 12 |
|
|
|
| 13 |
|
|
#include "ray.h" |
| 14 |
|
|
|
| 15 |
|
|
#include "otypes.h" |
| 16 |
|
|
|
| 17 |
|
|
#include "cone.h" |
| 18 |
|
|
|
| 19 |
|
|
|
| 20 |
|
|
o_cone(o, r) /* intersect ray with cone */ |
| 21 |
|
|
OBJREC *o; |
| 22 |
|
|
register RAY *r; |
| 23 |
|
|
{ |
| 24 |
|
|
FVECT rox, rdx; |
| 25 |
|
|
double a, b, c; |
| 26 |
|
|
double root[2]; |
| 27 |
|
|
int nroots, rn; |
| 28 |
|
|
register CONE *co; |
| 29 |
|
|
register int i; |
| 30 |
|
|
|
| 31 |
|
|
/* get cone structure */ |
| 32 |
|
|
co = getcone(o, 1); |
| 33 |
|
|
|
| 34 |
|
|
/* |
| 35 |
|
|
* To intersect a ray with a cone, we transform the |
| 36 |
|
|
* ray into the cone's normalized space. This greatly |
| 37 |
|
|
* simplifies the computation. |
| 38 |
|
|
* For a cone or cup, normalization results in the |
| 39 |
|
|
* equation: |
| 40 |
|
|
* |
| 41 |
|
|
* x*x + y*y - z*z == 0 |
| 42 |
|
|
* |
| 43 |
|
|
* For a cylinder or tube, the normalized equation is: |
| 44 |
|
|
* |
| 45 |
|
|
* x*x + y*y - r*r == 0 |
| 46 |
|
|
* |
| 47 |
|
|
* A normalized ring obeys the following set of equations: |
| 48 |
|
|
* |
| 49 |
|
|
* z == 0 && |
| 50 |
|
|
* x*x + y*y >= r0*r0 && |
| 51 |
|
|
* x*x + y*y <= r1*r1 |
| 52 |
|
|
*/ |
| 53 |
|
|
|
| 54 |
|
|
/* transform ray */ |
| 55 |
|
|
multp3(rox, r->rorg, co->tm); |
| 56 |
|
|
multv3(rdx, r->rdir, co->tm); |
| 57 |
|
|
/* compute intersection */ |
| 58 |
|
|
|
| 59 |
|
|
if (o->otype == OBJ_CONE || o->otype == OBJ_CUP) { |
| 60 |
|
|
|
| 61 |
|
|
a = rdx[0]*rdx[0] + rdx[1]*rdx[1] - rdx[2]*rdx[2]; |
| 62 |
|
|
b = 2.0*(rdx[0]*rox[0] + rdx[1]*rox[1] - rdx[2]*rox[2]); |
| 63 |
|
|
c = rox[0]*rox[0] + rox[1]*rox[1] - rox[2]*rox[2]; |
| 64 |
|
|
|
| 65 |
|
|
} else if (o->otype == OBJ_CYLINDER || o->otype == OBJ_TUBE) { |
| 66 |
|
|
|
| 67 |
|
|
a = rdx[0]*rdx[0] + rdx[1]*rdx[1]; |
| 68 |
|
|
b = 2.0*(rdx[0]*rox[0] + rdx[1]*rox[1]); |
| 69 |
|
|
c = rox[0]*rox[0] + rox[1]*rox[1] - CO_R0(co)*CO_R0(co); |
| 70 |
|
|
|
| 71 |
|
|
} else { /* OBJ_RING */ |
| 72 |
|
|
|
| 73 |
|
|
if (rdx[2] <= FTINY && rdx[2] >= -FTINY) |
| 74 |
|
|
return(0); /* parallel */ |
| 75 |
|
|
root[0] = -rox[2]/rdx[2]; |
| 76 |
|
|
if (root[0] <= FTINY || root[0] >= r->rot) |
| 77 |
|
|
return(0); /* distance check */ |
| 78 |
|
|
b = root[0]*rdx[0] + rox[0]; |
| 79 |
|
|
c = root[0]*rdx[1] + rox[1]; |
| 80 |
|
|
a = b*b + c*c; |
| 81 |
|
|
if (a < CO_R0(co)*CO_R0(co) || a > CO_R1(co)*CO_R1(co)) |
| 82 |
|
|
return(0); /* outside radii */ |
| 83 |
|
|
r->ro = o; |
| 84 |
|
|
r->rot = root[0]; |
| 85 |
|
|
for (i = 0; i < 3; i++) |
| 86 |
|
|
r->rop[i] = r->rorg[i] + r->rdir[i]*r->rot; |
| 87 |
|
|
VCOPY(r->ron, co->ad); |
| 88 |
|
|
r->rod = -rdx[2]; |
| 89 |
greg |
1.2 |
r->rofs = 1.0; setident4(r->rofx); |
| 90 |
|
|
r->robs = 1.0; setident4(r->robx); |
| 91 |
greg |
1.1 |
return(1); /* good */ |
| 92 |
|
|
} |
| 93 |
|
|
/* roots for cone, cup, cyl., tube */ |
| 94 |
|
|
nroots = quadratic(root, a, b, c); |
| 95 |
|
|
|
| 96 |
|
|
for (rn = 0; rn < nroots; rn++) { /* check real roots */ |
| 97 |
|
|
if (root[rn] <= FTINY) |
| 98 |
|
|
continue; /* too small */ |
| 99 |
|
|
if (root[rn] >= r->rot) |
| 100 |
|
|
break; /* too big */ |
| 101 |
|
|
/* check endpoints */ |
| 102 |
|
|
for (i = 0; i < 3; i++) { |
| 103 |
|
|
rox[i] = r->rorg[i] + root[rn]*r->rdir[i]; |
| 104 |
|
|
rdx[i] = rox[i] - CO_P0(co)[i]; |
| 105 |
|
|
} |
| 106 |
|
|
b = DOT(rdx, co->ad); |
| 107 |
|
|
if (b < 0.0) |
| 108 |
|
|
continue; /* before p0 */ |
| 109 |
|
|
if (b > co->al) |
| 110 |
|
|
continue; /* after p1 */ |
| 111 |
|
|
r->ro = o; |
| 112 |
|
|
r->rot = root[rn]; |
| 113 |
|
|
VCOPY(r->rop, rox); |
| 114 |
|
|
/* get normal */ |
| 115 |
|
|
if (o->otype == OBJ_CYLINDER) |
| 116 |
|
|
a = CO_R0(co); |
| 117 |
|
|
else if (o->otype == OBJ_TUBE) |
| 118 |
|
|
a = -CO_R0(co); |
| 119 |
|
|
else { /* OBJ_CONE || OBJ_CUP */ |
| 120 |
|
|
c = CO_R1(co) - CO_R0(co); |
| 121 |
|
|
a = CO_R0(co) + b*c/co->al; |
| 122 |
|
|
if (o->otype == OBJ_CUP) { |
| 123 |
|
|
c = -c; |
| 124 |
|
|
a = -a; |
| 125 |
|
|
} |
| 126 |
|
|
} |
| 127 |
|
|
for (i = 0; i < 3; i++) |
| 128 |
|
|
r->ron[i] = (rdx[i] - b*co->ad[i])/a; |
| 129 |
|
|
if (o->otype == OBJ_CONE || o->otype == OBJ_CUP) |
| 130 |
|
|
for (i = 0; i < 3; i++) |
| 131 |
|
|
r->ron[i] = (co->al*r->ron[i] - c*co->ad[i]) |
| 132 |
|
|
/co->sl; |
| 133 |
|
|
r->rod = -DOT(r->rdir, r->ron); |
| 134 |
greg |
1.2 |
r->rofs = 1.0; setident4(r->rofx); |
| 135 |
|
|
r->robs = 1.0; setident4(r->robx); |
| 136 |
greg |
1.1 |
return(1); /* good */ |
| 137 |
|
|
} |
| 138 |
|
|
return(0); |
| 139 |
|
|
} |