ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/sphere.c
Revision: 1.5
Committed: Wed Oct 23 13:43:48 1991 UTC (32 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +1 -1 lines
Log Message:
added FLOAT definition to better control size of structures

File Contents

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