doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Sphere.cpp
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #include "../precompiled.h"
30 #pragma hdrstop
31 
32 
34 
35 
36 /*
37 ================
38 idSphere::PlaneDistance
39 ================
40 */
41 float idSphere::PlaneDistance( const idPlane &plane ) const {
42  float d;
43 
44  d = plane.Distance( origin );
45  if ( d > radius ) {
46  return d - radius;
47  }
48  if ( d < -radius ) {
49  return d + radius;
50  }
51  return 0.0f;
52 }
53 
54 /*
55 ================
56 idSphere::PlaneSide
57 ================
58 */
59 int idSphere::PlaneSide( const idPlane &plane, const float epsilon ) const {
60  float d;
61 
62  d = plane.Distance( origin );
63  if ( d > radius + epsilon ) {
64  return PLANESIDE_FRONT;
65  }
66  if ( d < -radius - epsilon ) {
67  return PLANESIDE_BACK;
68  }
69  return PLANESIDE_CROSS;
70 }
71 
72 /*
73 ============
74 idSphere::LineIntersection
75 
76  Returns true if the line intersects the sphere between the start and end point.
77 ============
78 */
79 bool idSphere::LineIntersection( const idVec3 &start, const idVec3 &end ) const {
80  idVec3 r, s, e;
81  float a;
82 
83  s = start - origin;
84  e = end - origin;
85  r = e - s;
86  a = -s * r;
87  if ( a <= 0 ) {
88  return ( s * s < radius * radius );
89  }
90  else if ( a >= r * r ) {
91  return ( e * e < radius * radius );
92  }
93  else {
94  r = s + ( a / ( r * r ) ) * r;
95  return ( r * r < radius * radius );
96  }
97 }
98 
99 /*
100 ============
101 idSphere::RayIntersection
102 
103  Returns true if the ray intersects the sphere.
104  The ray can intersect the sphere in both directions from the start point.
105  If start is inside the sphere then scale1 < 0 and scale2 > 0.
106 ============
107 */
108 bool idSphere::RayIntersection( const idVec3 &start, const idVec3 &dir, float &scale1, float &scale2 ) const {
109  double a, b, c, d, sqrtd;
110  idVec3 p;
111 
112  p = start - origin;
113  a = dir * dir;
114  b = dir * p;
115  c = p * p - radius * radius;
116  d = b * b - c * a;
117 
118  if ( d < 0.0f ) {
119  return false;
120  }
121 
122  sqrtd = idMath::Sqrt( d );
123  a = 1.0f / a;
124 
125  scale1 = ( -b + sqrtd ) * a;
126  scale2 = ( -b - sqrtd ) * a;
127 
128  return true;
129 }
130 
131 /*
132 ============
133 idSphere::FromPoints
134 
135  Tight sphere for a point set.
136 ============
137 */
138 void idSphere::FromPoints( const idVec3 *points, const int numPoints ) {
139  int i;
140  float radiusSqr, dist;
141  idVec3 mins, maxs;
142 
143  SIMDProcessor->MinMax( mins, maxs, points, numPoints );
144 
145  origin = ( mins + maxs ) * 0.5f;
146 
147  radiusSqr = 0.0f;
148  for ( i = 0; i < numPoints; i++ ) {
149  dist = ( points[i] - origin ).LengthSqr();
150  if ( dist > radiusSqr ) {
151  radiusSqr = dist;
152  }
153  }
154  radius = idMath::Sqrt( radiusSqr );
155 }
GLsizei const GLfloat * points
Definition: glext.h:3884
float Distance(const idVec3 &v) const
Definition: Plane.h:324
Definition: Vector.h:316
static float Sqrt(float x)
Definition: Math.h:302
GLdouble s
Definition: glext.h:2935
float PlaneDistance(const idPlane &plane) const
Definition: Sphere.cpp:41
idVec3 origin
Definition: Sphere.h:95
int i
Definition: process.py:33
#define PLANESIDE_FRONT
Definition: Plane.h:53
virtual void VPCALL MinMax(float &min, float &max, const float *src, const int count)=0
const GLubyte * c
Definition: glext.h:4677
#define vec3_zero
Definition: Vector.h:390
GLuint GLuint end
Definition: glext.h:2845
#define PLANESIDE_BACK
Definition: Plane.h:54
idSphere sphere_zero(vec3_zero, 0.0f)
Definition: Plane.h:71
int PlaneSide(const idPlane &plane, const float epsilon=ON_EPSILON) const
Definition: Sphere.cpp:59
GLubyte GLubyte GLubyte a
Definition: glext.h:4662
bool LineIntersection(const idVec3 &start, const idVec3 &end) const
Definition: Sphere.cpp:79
GLubyte GLubyte b
Definition: glext.h:4662
GLdouble GLdouble GLdouble r
Definition: glext.h:2951
tuple f
Definition: idal.py:89
void FromPoints(const idVec3 *points, const int numPoints)
Definition: Sphere.cpp:138
#define PLANESIDE_CROSS
Definition: Plane.h:56
float radius
Definition: Sphere.h:96
GLfloat GLfloat p
Definition: glext.h:4674
GLuint start
Definition: glext.h:2845
bool RayIntersection(const idVec3 &start, const idVec3 &dir, float &scale1, float &scale2) const
Definition: Sphere.cpp:108
idSIMDProcessor * SIMDProcessor
Definition: Simd.cpp:43