ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/sphere.c
Revision: 2.4
Committed: Tue Feb 25 02:47:23 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.3 static const char RCSid[] = "$Id$";
3 greg 1.1 #endif
4     /*
5     * sphere.c - compute ray intersection with spheres.
6 greg 2.3 */
7    
8 greg 2.4 #include "copyright.h"
9 greg 1.1
10     #include "ray.h"
11    
12     #include "otypes.h"
13    
14    
15     o_sphere(so, r) /* compute intersection with sphere */
16     OBJREC *so;
17     register RAY *r;
18     {
19     double a, b, c; /* coefficients for quadratic equation */
20     double root[2]; /* quadratic roots */
21     int nroots;
22     double t;
23 greg 1.5 register FLOAT *ap;
24 greg 1.1 register int i;
25    
26 greg 1.4 if (so->oargs.nfargs != 4)
27     objerror(so, USER, "bad # arguments");
28 greg 1.1 ap = so->oargs.farg;
29 greg 1.4 if (ap[3] < -FTINY) {
30     objerror(so, WARNING, "negative radius");
31     so->otype = so->otype == OBJ_SPHERE ?
32     OBJ_BUBBLE : OBJ_SPHERE;
33     ap[3] = -ap[3];
34     } else if (ap[3] <= FTINY)
35     objerror(so, USER, "zero radius");
36 greg 1.1
37     /*
38     * We compute the intersection by substituting into
39     * the surface equation for the sphere. The resulting
40     * quadratic equation in t is then solved for the
41     * smallest positive root, which is our point of
42     * intersection.
43 greg 2.2 * Since the ray is normalized, a should always be
44     * one. We compute it here to prevent instability in the
45     * intersection calculation.
46 greg 1.1 */
47 greg 2.2 /* compute quadratic coefficients */
48     a = b = c = 0.0;
49 greg 1.1 for (i = 0; i < 3; i++) {
50 greg 2.2 a += r->rdir[i]*r->rdir[i];
51 greg 1.1 t = r->rorg[i] - ap[i];
52     b += 2.0*r->rdir[i]*t;
53     c += t*t;
54     }
55     c -= ap[3] * ap[3];
56    
57     nroots = quadratic(root, a, b, c); /* solve quadratic */
58    
59     for (i = 0; i < nroots; i++) /* get smallest positive */
60     if ((t = root[i]) > FTINY)
61     break;
62     if (i >= nroots)
63     return(0); /* no positive root */
64    
65 greg 1.2 if (t >= r->rot)
66     return(0); /* other is closer */
67    
68     r->ro = so;
69     r->rot = t;
70     /* compute normal */
71     a = ap[3];
72     if (so->otype == OBJ_BUBBLE)
73     a = -a; /* reverse */
74     for (i = 0; i < 3; i++) {
75     r->rop[i] = r->rorg[i] + r->rdir[i]*t;
76     r->ron[i] = (r->rop[i] - ap[i]) / a;
77 greg 1.1 }
78 greg 1.2 r->rod = -DOT(r->rdir, r->ron);
79 greg 1.3 r->rox = NULL;
80 greg 1.2
81     return(1); /* hit */
82 greg 1.1 }