1 |
greg |
1.1 |
#ifndef lint |
2 |
schorsch |
2.3 |
static const char RCSid[] = "$Id: sphere.c,v 2.2 2003/02/22 02:07:26 greg Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* sphere.c - routines for creating octrees for spheres. |
6 |
|
|
* |
7 |
|
|
* 7/28/85 |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#include "standard.h" |
11 |
|
|
|
12 |
|
|
#include "octree.h" |
13 |
|
|
|
14 |
|
|
#include "object.h" |
15 |
|
|
|
16 |
|
|
#include "otypes.h" |
17 |
|
|
|
18 |
|
|
#define ROOT3 1.732050808 |
19 |
|
|
|
20 |
|
|
/* |
21 |
|
|
* Regrettably, the algorithm for determining a cube's location |
22 |
|
|
* with respect to a sphere is not simple. First, a quick test is |
23 |
|
|
* made to determine if the sphere and the bounding sphere of the cube |
24 |
|
|
* are disjoint. This of course means no intersection. Failing this, |
25 |
|
|
* we determine if the cube lies inside the sphere. The cube is |
26 |
|
|
* entirely inside if the bounding sphere on the cube is |
27 |
|
|
* contained within our sphere. This means no intersection. Otherwise, |
28 |
|
|
* if the cube radius is smaller than the sphere's and the cube center is |
29 |
|
|
* inside the sphere, we assume intersection. If these tests fail, |
30 |
|
|
* we proceed as follows. |
31 |
|
|
* The sphere center is located in relation to the 6 cube faces, |
32 |
|
|
* and one of four things is done depending on the number of |
33 |
|
|
* planes the center lies between: |
34 |
|
|
* |
35 |
|
|
* 0: The sphere is closest to a cube corner, find the |
36 |
|
|
* distance to that corner. |
37 |
|
|
* |
38 |
|
|
* 1: The sphere is closest to a cube edge, find this |
39 |
|
|
* distance. |
40 |
|
|
* |
41 |
|
|
* 2: The sphere is closest to a cube face, find the distance. |
42 |
|
|
* |
43 |
|
|
* 3: The sphere has its center inside the cube. |
44 |
|
|
* |
45 |
|
|
* In cases 0-2, if the closest part of the cube is within |
46 |
|
|
* the radius distance from the sphere center, we have intersection. |
47 |
|
|
* If it is not, the cube must be outside the sphere. |
48 |
|
|
* In case 3, there must be intersection, and no further |
49 |
|
|
* tests are necessary. |
50 |
|
|
*/ |
51 |
|
|
|
52 |
|
|
|
53 |
|
|
o_sphere(o, cu) /* determine if sphere intersects cube */ |
54 |
|
|
OBJREC *o; |
55 |
|
|
register CUBE *cu; |
56 |
|
|
{ |
57 |
|
|
FVECT v1; |
58 |
|
|
double d1, d2; |
59 |
schorsch |
2.3 |
register RREAL *fa; |
60 |
greg |
1.1 |
register int i; |
61 |
|
|
#define cent fa |
62 |
|
|
#define rad fa[3] |
63 |
|
|
/* get arguments */ |
64 |
greg |
1.3 |
if (o->oargs.nfargs != 4) |
65 |
|
|
objerror(o, USER, "bad # arguments"); |
66 |
greg |
1.1 |
fa = o->oargs.farg; |
67 |
greg |
1.3 |
if (rad < -FTINY) { |
68 |
|
|
objerror(o, WARNING, "negative radius"); |
69 |
|
|
o->otype = o->otype == OBJ_SPHERE ? |
70 |
|
|
OBJ_BUBBLE : OBJ_SPHERE; |
71 |
|
|
rad = -rad; |
72 |
|
|
} else if (rad <= FTINY) |
73 |
|
|
objerror(o, USER, "zero radius"); |
74 |
greg |
1.1 |
|
75 |
|
|
d1 = ROOT3/2.0 * cu->cusize; /* bounding radius for cube */ |
76 |
|
|
|
77 |
|
|
d2 = cu->cusize * 0.5; /* get distance between centers */ |
78 |
|
|
for (i = 0; i < 3; i++) |
79 |
|
|
v1[i] = cu->cuorg[i] + d2 - cent[i]; |
80 |
|
|
d2 = DOT(v1,v1); |
81 |
|
|
|
82 |
|
|
if (d2 > (rad+d1+FTINY)*(rad+d1+FTINY)) /* quick test */ |
83 |
greg |
1.2 |
return(O_MISS); /* cube outside */ |
84 |
greg |
1.1 |
|
85 |
|
|
/* check sphere interior */ |
86 |
|
|
if (d1 < rad) { |
87 |
|
|
if (d2 < (rad-d1-FTINY)*(rad-d1-FTINY)) |
88 |
greg |
1.2 |
return(O_MISS); /* cube inside sphere */ |
89 |
greg |
1.1 |
if (d2 < (rad+FTINY)*(rad+FTINY)) |
90 |
greg |
1.2 |
return(O_HIT); /* cube center inside */ |
91 |
greg |
1.1 |
} |
92 |
|
|
/* find closest distance */ |
93 |
|
|
for (i = 0; i < 3; i++) |
94 |
|
|
if (cent[i] < cu->cuorg[i]) |
95 |
|
|
v1[i] = cu->cuorg[i] - cent[i]; |
96 |
|
|
else if (cent[i] > cu->cuorg[i] + cu->cusize) |
97 |
|
|
v1[i] = cent[i] - (cu->cuorg[i] + cu->cusize); |
98 |
|
|
else |
99 |
|
|
v1[i] = 0; |
100 |
|
|
/* final intersection check */ |
101 |
|
|
if (DOT(v1,v1) <= (rad+FTINY)*(rad+FTINY)) |
102 |
greg |
1.2 |
return(O_HIT); |
103 |
greg |
1.1 |
else |
104 |
greg |
1.2 |
return(O_MISS); |
105 |
greg |
1.1 |
} |