ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/ot/sphere.c
Revision: 2.4
Committed: Sat Mar 27 12:41:45 2004 UTC (20 years, 8 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.3: +7 -4 lines
Log Message:
Continued ANSIfication. Renamed local initotypes() to ot_initotypes().

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.4 static const char RCSid[] = "$Id: sphere.c,v 2.3 2003/06/26 00:58:10 schorsch 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 schorsch 2.4 /* XXX o_sphere() is extern, but not declared in any header file */
54     int
55     o_sphere( /* determine if sphere intersects cube */
56     OBJREC *o,
57     register CUBE *cu
58     )
59 greg 1.1 {
60     FVECT v1;
61     double d1, d2;
62 schorsch 2.3 register RREAL *fa;
63 greg 1.1 register int i;
64     #define cent fa
65     #define rad fa[3]
66     /* get arguments */
67 greg 1.3 if (o->oargs.nfargs != 4)
68     objerror(o, USER, "bad # arguments");
69 greg 1.1 fa = o->oargs.farg;
70 greg 1.3 if (rad < -FTINY) {
71     objerror(o, WARNING, "negative radius");
72     o->otype = o->otype == OBJ_SPHERE ?
73     OBJ_BUBBLE : OBJ_SPHERE;
74     rad = -rad;
75     } else if (rad <= FTINY)
76     objerror(o, USER, "zero radius");
77 greg 1.1
78     d1 = ROOT3/2.0 * cu->cusize; /* bounding radius for cube */
79    
80     d2 = cu->cusize * 0.5; /* get distance between centers */
81     for (i = 0; i < 3; i++)
82     v1[i] = cu->cuorg[i] + d2 - cent[i];
83     d2 = DOT(v1,v1);
84    
85     if (d2 > (rad+d1+FTINY)*(rad+d1+FTINY)) /* quick test */
86 greg 1.2 return(O_MISS); /* cube outside */
87 greg 1.1
88     /* check sphere interior */
89     if (d1 < rad) {
90     if (d2 < (rad-d1-FTINY)*(rad-d1-FTINY))
91 greg 1.2 return(O_MISS); /* cube inside sphere */
92 greg 1.1 if (d2 < (rad+FTINY)*(rad+FTINY))
93 greg 1.2 return(O_HIT); /* cube center inside */
94 greg 1.1 }
95     /* find closest distance */
96     for (i = 0; i < 3; i++)
97     if (cent[i] < cu->cuorg[i])
98     v1[i] = cu->cuorg[i] - cent[i];
99     else if (cent[i] > cu->cuorg[i] + cu->cusize)
100     v1[i] = cent[i] - (cu->cuorg[i] + cu->cusize);
101     else
102     v1[i] = 0;
103     /* final intersection check */
104     if (DOT(v1,v1) <= (rad+FTINY)*(rad+FTINY))
105 greg 1.2 return(O_HIT);
106 greg 1.1 else
107 greg 1.2 return(O_MISS);
108 greg 1.1 }