ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/o_cone.c
Revision: 2.8
Committed: Tue Jul 8 18:25:00 2014 UTC (9 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad4R2, rad4R2P1
Changes since 2.7: +5 -5 lines
Log Message:
Eliminated unnecessary "extern" and "register" modifiers

File Contents

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