1 |
– |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* sphere.c - compute ray intersection with spheres. |
9 |
– |
* |
10 |
– |
* 8/19/85 |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include "copyright.h" |
9 |
+ |
|
10 |
|
#include "ray.h" |
11 |
|
|
12 |
|
#include "otypes.h" |
20 |
|
double root[2]; /* quadratic roots */ |
21 |
|
int nroots; |
22 |
|
double t; |
23 |
< |
register double *ap; |
23 |
> |
register FLOAT *ap; |
24 |
|
register int i; |
25 |
|
|
26 |
< |
if (so->oargs.nfargs != 4 || so->oargs.farg[3] <= FTINY) |
27 |
< |
objerror(so, USER, "bad arguments"); |
31 |
< |
|
26 |
> |
if (so->oargs.nfargs != 4) |
27 |
> |
objerror(so, USER, "bad # arguments"); |
28 |
|
ap = so->oargs.farg; |
29 |
+ |
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 |
|
|
37 |
|
/* |
38 |
|
* We compute the intersection by substituting into |
40 |
|
* quadratic equation in t is then solved for the |
41 |
|
* smallest positive root, which is our point of |
42 |
|
* intersection. |
43 |
< |
* Because the ray direction is normalized, a is always 1. |
43 |
> |
* Since the ray is normalized, a should always be |
44 |
> |
* one. We compute it here to prevent instability in the |
45 |
> |
* intersection calculation. |
46 |
|
*/ |
47 |
< |
|
48 |
< |
a = 1.0; /* compute quadratic coefficients */ |
44 |
< |
b = c = 0.0; |
47 |
> |
/* compute quadratic coefficients */ |
48 |
> |
a = b = c = 0.0; |
49 |
|
for (i = 0; i < 3; i++) { |
50 |
+ |
a += r->rdir[i]*r->rdir[i]; |
51 |
|
t = r->rorg[i] - ap[i]; |
52 |
|
b += 2.0*r->rdir[i]*t; |
53 |
|
c += t*t; |
62 |
|
if (i >= nroots) |
63 |
|
return(0); /* no positive root */ |
64 |
|
|
65 |
< |
if (t < r->rot) { /* found closer intersection */ |
66 |
< |
r->ro = so; |
67 |
< |
r->rot = t; |
68 |
< |
/* compute normal */ |
69 |
< |
a = ap[3]; |
70 |
< |
if (so->otype == OBJ_BUBBLE) |
71 |
< |
a = -a; /* reverse */ |
72 |
< |
for (i = 0; i < 3; i++) { |
73 |
< |
r->rop[i] = r->rorg[i] + r->rdir[i]*t; |
74 |
< |
r->ron[i] = (r->rop[i] - ap[i]) / a; |
75 |
< |
} |
76 |
< |
r->rod = -DOT(r->rdir, r->ron); |
65 |
> |
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 |
|
} |
78 |
< |
return(1); |
78 |
> |
r->rod = -DOT(r->rdir, r->ron); |
79 |
> |
r->rox = NULL; |
80 |
> |
|
81 |
> |
return(1); /* hit */ |
82 |
|
} |