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