ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/o_cone.c
Revision: 2.9
Committed: Thu Apr 21 00:40:35 2016 UTC (8 years ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2, rad5R1, rad5R3
Changes since 2.8: +3 -1 lines
Log Message:
Made zero cone and ring radii non-fatal (degenerate -> ignore)

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: o_cone.c,v 2.8 2014/07/08 18:25:00 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 if (co == NULL)
32 objerror(o, INTERNAL, "unexpected illegal");
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 VSUM(r->rop, r->rorg, r->rdir, r->rot);
86 VCOPY(r->ron, co->ad);
87 r->rod = -rdx[2];
88 r->rox = NULL;
89 return(1); /* good */
90 }
91 /* roots for cone, cup, cyl., tube */
92 nroots = quadratic(root, a, b, c);
93
94 for (rn = 0; rn < nroots; rn++) { /* check real roots */
95 if (root[rn] <= FTINY)
96 continue; /* too small */
97 if (root[rn] >= r->rot)
98 break; /* too big */
99 /* check endpoints */
100 VSUM(rox, r->rorg, r->rdir, root[rn]);
101 VSUB(rdx, rox, CO_P0(co));
102 b = DOT(rdx, co->ad);
103 if (b < 0.0)
104 continue; /* before p0 */
105 if (b > co->al)
106 continue; /* after p1 */
107 r->ro = o;
108 r->rot = root[rn];
109 VCOPY(r->rop, rox);
110 /* get normal */
111 if (o->otype == OBJ_CYLINDER)
112 a = CO_R0(co);
113 else if (o->otype == OBJ_TUBE)
114 a = -CO_R0(co);
115 else { /* OBJ_CONE || OBJ_CUP */
116 c = CO_R1(co) - CO_R0(co);
117 a = CO_R0(co) + b*c/co->al;
118 if (o->otype == OBJ_CUP) {
119 c = -c;
120 a = -a;
121 }
122 }
123 for (i = 0; i < 3; i++)
124 r->ron[i] = (rdx[i] - b*co->ad[i])/a;
125 if (o->otype == OBJ_CONE || o->otype == OBJ_CUP)
126 for (i = 0; i < 3; i++)
127 r->ron[i] = (co->al*r->ron[i] - c*co->ad[i])
128 / co->sl;
129 a = DOT(r->ron, r->ron);
130 if (a > 1.+FTINY || a < 1.-FTINY) {
131 c = 1./(.5 + .5*a); /* avoid numerical error */
132 r->ron[0] *= c; r->ron[1] *= c; r->ron[2] *= c;
133 }
134 r->rod = -DOT(r->rdir, r->ron);
135 r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
136 r->uv[0] = r->uv[1] = 0.0;
137 r->rox = NULL;
138 return(1); /* good */
139 }
140 return(0);
141 }