doom3-bfg/neo/idlib/bv/Sphere.cpp

171 lines
3.9 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition 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 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
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.
===========================================================================
*/
#pragma hdrstop
#include "../precompiled.h"
idSphere sphere_zero( vec3_zero, 0.0f );
/*
================
idSphere::PlaneDistance
================
*/
float idSphere::PlaneDistance( const idPlane& plane ) const
{
2012-11-26 18:58:24 +00:00
float d;
2012-11-26 18:58:24 +00:00
d = plane.Distance( origin );
if( d > radius )
{
2012-11-26 18:58:24 +00:00
return d - radius;
}
if( d < -radius )
{
2012-11-26 18:58:24 +00:00
return d + radius;
}
return 0.0f;
}
/*
================
idSphere::PlaneSide
================
*/
int idSphere::PlaneSide( const idPlane& plane, const float epsilon ) const
{
2012-11-26 18:58:24 +00:00
float d;
2012-11-26 18:58:24 +00:00
d = plane.Distance( origin );
if( d > radius + epsilon )
{
2012-11-26 18:58:24 +00:00
return PLANESIDE_FRONT;
}
if( d < -radius - epsilon )
{
2012-11-26 18:58:24 +00:00
return PLANESIDE_BACK;
}
return PLANESIDE_CROSS;
}
/*
============
idSphere::LineIntersection
Returns true if the line intersects the sphere between the start and end point.
============
*/
bool idSphere::LineIntersection( const idVec3& start, const idVec3& end ) const
{
2012-11-26 18:58:24 +00:00
idVec3 r, s, e;
float a;
2012-11-26 18:58:24 +00:00
s = start - origin;
e = end - origin;
r = e - s;
a = -s * r;
if( a <= 0 )
{
2012-11-26 18:58:24 +00:00
return ( s * s < radius * radius );
}
else if( a >= r * r )
{
2012-11-26 18:58:24 +00:00
return ( e * e < radius * radius );
}
else
{
2012-11-26 18:58:24 +00:00
r = s + ( a / ( r * r ) ) * r;
return ( r * r < radius * radius );
}
}
/*
============
idSphere::RayIntersection
Returns true if the ray intersects the sphere.
The ray can intersect the sphere in both directions from the start point.
If start is inside the sphere then scale1 < 0 and scale2 > 0.
============
*/
bool idSphere::RayIntersection( const idVec3& start, const idVec3& dir, float& scale1, float& scale2 ) const
{
2012-11-26 18:58:24 +00:00
double a, b, c, d, sqrtd;
idVec3 p;
2012-11-26 18:58:24 +00:00
p = start - origin;
a = dir * dir;
b = dir * p;
c = p * p - radius * radius;
d = b * b - c * a;
if( d < 0.0f )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
sqrtd = idMath::Sqrt( d );
a = 1.0f / a;
2012-11-26 18:58:24 +00:00
scale1 = ( -b + sqrtd ) * a;
scale2 = ( -b - sqrtd ) * a;
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idSphere::FromPoints
Tight sphere for a point set.
============
*/
void idSphere::FromPoints( const idVec3* points, const int numPoints )
{
2012-11-26 18:58:24 +00:00
int i;
float radiusSqr, dist;
idVec3 mins, maxs;
2012-11-26 18:58:24 +00:00
SIMDProcessor->MinMax( mins, maxs, points, numPoints );
2012-11-26 18:58:24 +00:00
origin = ( mins + maxs ) * 0.5f;
2012-11-26 18:58:24 +00:00
radiusSqr = 0.0f;
for( i = 0; i < numPoints; i++ )
{
2012-11-26 18:58:24 +00:00
dist = ( points[i] - origin ).LengthSqr();
if( dist > radiusSqr )
{
2012-11-26 18:58:24 +00:00
radiusSqr = dist;
}
}
radius = idMath::Sqrt( radiusSqr );
}