ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/o_cone.c
Revision: 2.12
Committed: Thu Mar 16 00:25:24 2023 UTC (14 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, HEAD
Changes since 2.11: +3 -3 lines
Log Message:
feat: Added test for which side of flat surface is seen in case of coincident surfaces

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.12 static const char RCSid[] = "$Id: o_cone.c,v 2.11 2021/12/16 21:37:21 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * o_cone.c - routine to determine ray intersection with cones.
6 greg 2.2 */
7    
8 greg 2.3 #include "copyright.h"
9 greg 1.1
10     #include "ray.h"
11     #include "otypes.h"
12 schorsch 2.5 #include "rtotypes.h"
13 greg 1.1 #include "cone.h"
14    
15    
16 greg 2.8 int
17 schorsch 2.5 o_cone( /* intersect ray with cone */
18     OBJREC *o,
19 greg 2.8 RAY *r
20 schorsch 2.5 )
21 greg 1.1 {
22     FVECT rox, rdx;
23     double a, b, c;
24     double root[2];
25     int nroots, rn;
26 greg 2.8 CONE *co;
27     int i;
28 greg 1.1
29     /* get cone structure */
30     co = getcone(o, 1);
31 greg 2.9 if (co == NULL)
32     objerror(o, INTERNAL, "unexpected illegal");
33 greg 1.1
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 greg 2.10 if ((rdx[2] <= FTINY) & (rdx[2] >= -FTINY))
74 greg 1.1 return(0); /* parallel */
75     root[0] = -rox[2]/rdx[2];
76 greg 2.12 if (rayreject(o, r, root[0], -rdx[2]))
77 greg 2.10 return(0); /* have better */
78 greg 1.1 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 greg 2.7 VSUM(r->rop, r->rorg, r->rdir, r->rot);
86 greg 1.1 VCOPY(r->ron, co->ad);
87     r->rod = -rdx[2];
88 greg 2.11 r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
89     r->uv[0] = r->uv[1] = 0.0;
90 greg 1.3 r->rox = NULL;
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 greg 2.10 if (root[rn] > r->rot + FTINY)
100 greg 1.1 break; /* too big */
101     /* check endpoints */
102 greg 2.7 VSUM(rox, r->rorg, r->rdir, root[rn]);
103     VSUB(rdx, rox, CO_P0(co));
104 greg 1.1 b = DOT(rdx, co->ad);
105     if (b < 0.0)
106     continue; /* before p0 */
107     if (b > co->al)
108     continue; /* after p1 */
109 greg 2.12 if (rayreject(o, r, root[rn], 0))
110 greg 2.10 break; /* previous hit better */
111 greg 1.1 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 greg 2.7 / co->sl;
133 greg 2.6 a = DOT(r->ron, r->ron);
134     if (a > 1.+FTINY || a < 1.-FTINY) {
135     c = 1./(.5 + .5*a); /* avoid numerical error */
136     r->ron[0] *= c; r->ron[1] *= c; r->ron[2] *= c;
137     }
138 greg 1.1 r->rod = -DOT(r->rdir, r->ron);
139 greg 2.4 r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
140     r->uv[0] = r->uv[1] = 0.0;
141 greg 1.3 r->rox = NULL;
142 greg 1.1 return(1); /* good */
143     }
144     return(0);
145     }