Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / Opcode / OPC_SphereAABBOverlap.h
1 /*
2  *      OPCODE - Optimized Collision Detection
3  * http://www.codercorner.com/Opcode.htm
4  * 
5  * Copyright (c) 2001-2008 Pierre Terdiman,  pierre@codercorner.com
6
7 This software is provided 'as-is', without any express or implied warranty.
8 In no event will the authors be held liable for any damages arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose, 
10 including commercial applications, and to alter it and redistribute it freely, 
11 subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
15 3. This notice may not be removed or altered from any source distribution.
16 */
17
18 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
19 /**
20  *      Sphere-AABB overlap test, based on Jim Arvo's code.
21  *      \param          center          [in] box center
22  *      \param          extents         [in] box extents
23  *      \return         TRUE on overlap
24  */
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26 inline_ BOOL SphereCollider::SphereAABBOverlap(const Point& center, const Point& extents)
27
28         // Stats
29         mNbVolumeBVTests++;
30
31         float d = 0.0f;
32
33         //find the square of the distance
34         //from the sphere to the box
35 #ifdef OLDIES
36         for(udword i=0;i<3;i++)
37         {
38                 float tmp = mCenter[i] - center[i];
39                 float s = tmp + extents[i];
40
41                 if(s<0.0f)      d += s*s;
42                 else
43                 {
44                         s = tmp - extents[i];
45                         if(s>0.0f)      d += s*s;
46                 }
47         }
48 #endif
49
50 //#ifdef NEW_TEST
51
52 //      float tmp = mCenter.x - center.x;
53 //      float s = tmp + extents.x;
54
55         float tmp,s;
56
57         tmp = mCenter.x - center.x;
58         s = tmp + extents.x;
59
60         if(s<0.0f)
61         {
62                 d += s*s;
63                 if(d>mRadius2)  return FALSE;
64         }
65         else
66         {
67                 s = tmp - extents.x;
68                 if(s>0.0f)
69                 {
70                         d += s*s;
71                         if(d>mRadius2)  return FALSE;
72                 }
73         }
74
75         tmp = mCenter.y - center.y;
76         s = tmp + extents.y;
77
78         if(s<0.0f)
79         {
80                 d += s*s;
81                 if(d>mRadius2)  return FALSE;
82         }
83         else
84         {
85                 s = tmp - extents.y;
86                 if(s>0.0f)
87                 {
88                         d += s*s;
89                         if(d>mRadius2)  return FALSE;
90                 }
91         }
92
93         tmp = mCenter.z - center.z;
94         s = tmp + extents.z;
95
96         if(s<0.0f)
97         {
98                 d += s*s;
99                 if(d>mRadius2)  return FALSE;
100         }
101         else
102         {
103                 s = tmp - extents.z;
104                 if(s>0.0f)
105                 {
106                         d += s*s;
107                         if(d>mRadius2)  return FALSE;
108                 }
109         }
110 //#endif
111
112 #ifdef OLDIES
113 //      Point Min = center - extents;
114 //      Point Max = center + extents;
115
116         float d = 0.0f;
117
118         //find the square of the distance
119         //from the sphere to the box
120         for(udword i=0;i<3;i++)
121         {
122 float Min = center[i] - extents[i];
123
124 //              if(mCenter[i]<Min[i])
125                 if(mCenter[i]<Min)
126                 {
127 //                      float s = mCenter[i] - Min[i];
128                         float s = mCenter[i] - Min;
129                         d += s*s;
130                 }
131                 else
132                 {
133 float Max = center[i] + extents[i];
134
135 //                      if(mCenter[i]>Max[i])
136                         if(mCenter[i]>Max)
137                         {
138                                 float s = mCenter[i] - Max;
139                                 d += s*s;
140                         }
141                 }
142         }
143 #endif
144         return d <= mRadius2;
145 }