-
Notifications
You must be signed in to change notification settings - Fork 267
/
BoxSphere.c
104 lines (94 loc) · 3.72 KB
/
BoxSphere.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
A Simple Method for Box-Sphere Intersection Testing
by Jim Arvo
from "Graphics Gems", Academic Press, 1990
*/
#include "GraphicsGems.h"
/*
* This routine tests for intersection between an n-dimensional
* axis-aligned box and an n-dimensional sphere. The mode argument
* indicates whether the objects are to be regarded as surfaces or
* solids. The values are:
*
* mode
*
* 0 Hollow Box, Hollow Sphere
* 1 Hollow Box, Solid Sphere
* 2 Solid Box, Hollow Sphere
* 3 Solid Box, Solid Sphere
*
*/
int Box_Sphere_Intersect( int n, float Bmin[], float Bmax[], float C[], float r, int mode )
/* int n; The dimension of the space. */
/* float Bmin[]; The minimum of the box for each axis. */
/* float Bmax[]; The maximum of the box for each axis. */
/* float C[]; The sphere center in n-space. */
/* float r; The radius of the sphere. */
/* int mode; Selects hollow or solid. */
{
float a, b;
float dmin, dmax;
float r2 = (float)SQR( r );
int i, face;
switch( mode )
{
case 0: /* Hollow Box - Hollow Sphere */
dmin = 0;
dmax = 0;
face = FALSE;
for( i = 0; i < n; i++ ) {
a = SQR( C[i] - Bmin[i] );
b = SQR( C[i] - Bmax[i] );
dmax += MAX( a, b );
if( C[i] < Bmin[i] ) {
face = TRUE;
dmin += a;
}
else if( C[i] > Bmax[i] ) {
face = TRUE;
dmin += b;
}
else if( MIN( a, b ) <= r2 ) face = TRUE;
}
if(face && ( dmin <= r2 ) && ( r2 <= dmax)) return(TRUE);
break;
case 1: /* Hollow Box - Solid Sphere */
dmin = 0;
face = FALSE;
for( i = 0; i < n; i++ ) {
if( C[i] < Bmin[i] ) {
face = TRUE;
dmin += SQR( C[i] - Bmin[i] );
}
else if( C[i] > Bmax[i] ) {
face = TRUE;
dmin += SQR( C[i] - Bmax[i] );
}
else if( C[i] - Bmin[i] <= r ) face = TRUE;
else if( Bmax[i] - C[i] <= r ) face = TRUE;
}
if( face && ( dmin <= r2 ) ) return( TRUE );
break;
case 2: /* Solid Box - Hollow Sphere */
dmax = 0;
dmin = 0;
for( i = 0; i < n; i++ ) {
a = SQR( C[i] - Bmin[i] );
b = SQR( C[i] - Bmax[i] );
dmax += MAX( a, b );
if( C[i] < Bmin[i] ) dmin += a; else
if( C[i] > Bmax[i] ) dmin += b;
}
if( dmin <= r2 && r2 <= dmax ) return( TRUE );
break;
case 3: /* Solid Box - Solid Sphere */
dmin = 0;
for( i = 0; i < n; i++ ) {
if( C[i] < Bmin[i] ) dmin += SQR(C[i] - Bmin[i] ); else
if( C[i] > Bmax[i] ) dmin += SQR( C[i] - Bmax[i] );
}
if( dmin <= r2 ) return( TRUE );
break;
} /* end switch */
return( FALSE );
}