2012-11-26 18:58:24 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
|
|
|
|
Doom 3 BFG Edition GPL Source Code
|
2012-11-28 15:47:07 +00:00
|
|
|
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +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.
|
|
|
|
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __BV_SPHERE_H__
|
|
|
|
#define __BV_SPHERE_H__
|
|
|
|
|
|
|
|
/*
|
|
|
|
===============================================================================
|
|
|
|
|
|
|
|
Sphere
|
|
|
|
|
|
|
|
===============================================================================
|
|
|
|
*/
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
class idSphere
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
public:
|
2012-11-28 15:47:07 +00:00
|
|
|
idSphere();
|
|
|
|
explicit idSphere( const idVec3& point );
|
|
|
|
explicit idSphere( const idVec3& point, const float r );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
float operator[]( const int index ) const;
|
2012-11-28 15:47:07 +00:00
|
|
|
float& operator[]( const int index );
|
|
|
|
idSphere operator+( const idVec3& t ) const; // returns tranlated sphere
|
|
|
|
idSphere& operator+=( const idVec3& t ); // translate the sphere
|
|
|
|
idSphere operator+( const idSphere& s ) const;
|
|
|
|
idSphere& operator+=( const idSphere& s );
|
|
|
|
|
|
|
|
bool Compare( const idSphere& a ) const; // exact compare, no epsilon
|
|
|
|
bool Compare( const idSphere& a, const float epsilon ) const; // compare with epsilon
|
|
|
|
bool operator==( const idSphere& a ) const; // exact compare, no epsilon
|
|
|
|
bool operator!=( const idSphere& a ) const; // exact compare, no epsilon
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
void Clear(); // inside out sphere
|
|
|
|
void Zero(); // single point at origin
|
2012-11-28 15:47:07 +00:00
|
|
|
void SetOrigin( const idVec3& o ); // set origin of sphere
|
2012-11-26 18:58:24 +00:00
|
|
|
void SetRadius( const float r ); // set square radius
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
const idVec3& GetOrigin() const; // returns origin of sphere
|
2012-11-26 18:58:24 +00:00
|
|
|
float GetRadius() const; // returns sphere radius
|
|
|
|
bool IsCleared() const; // returns true if sphere is inside out
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
bool AddPoint( const idVec3& p ); // add the point, returns true if the sphere expanded
|
|
|
|
bool AddSphere( const idSphere& s ); // add the sphere, returns true if the sphere expanded
|
2012-11-26 18:58:24 +00:00
|
|
|
idSphere Expand( const float d ) const; // return bounds expanded in all directions with the given value
|
2012-11-28 15:47:07 +00:00
|
|
|
idSphere& ExpandSelf( const float d ); // expand bounds in all directions with the given value
|
|
|
|
idSphere Translate( const idVec3& translation ) const;
|
|
|
|
idSphere& TranslateSelf( const idVec3& translation );
|
|
|
|
|
|
|
|
float PlaneDistance( const idPlane& plane ) const;
|
|
|
|
int PlaneSide( const idPlane& plane, const float epsilon = ON_EPSILON ) const;
|
|
|
|
|
|
|
|
bool ContainsPoint( const idVec3& p ) const; // includes touching
|
|
|
|
bool IntersectsSphere( const idSphere& s ) const; // includes touching
|
|
|
|
bool LineIntersection( const idVec3& start, const idVec3& end ) const;
|
|
|
|
// intersection points are (start + dir * scale1) and (start + dir * scale2)
|
|
|
|
bool RayIntersection( const idVec3& start, const idVec3& dir, float& scale1, float& scale2 ) const;
|
|
|
|
|
|
|
|
// Tight sphere for a point set.
|
|
|
|
void FromPoints( const idVec3* points, const int numPoints );
|
|
|
|
// Most tight sphere for a translation.
|
|
|
|
void FromPointTranslation( const idVec3& point, const idVec3& translation );
|
|
|
|
void FromSphereTranslation( const idSphere& sphere, const idVec3& start, const idVec3& translation );
|
|
|
|
// Most tight sphere for a rotation.
|
|
|
|
void FromPointRotation( const idVec3& point, const idRotation& rotation );
|
|
|
|
void FromSphereRotation( const idSphere& sphere, const idVec3& start, const idRotation& rotation );
|
|
|
|
|
|
|
|
void AxisProjection( const idVec3& dir, float& min, float& max ) const;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
private:
|
|
|
|
idVec3 origin;
|
|
|
|
float radius;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern idSphere sphere_zero;
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idSphere::idSphere()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idSphere::idSphere( const idVec3& point )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
origin = point;
|
|
|
|
radius = 0.0f;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idSphere::idSphere( const idVec3& point, const float r )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
origin = point;
|
|
|
|
radius = r;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE float idSphere::operator[]( const int index ) const
|
|
|
|
{
|
|
|
|
return ( ( float* ) &origin )[index];
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE float& idSphere::operator[]( const int index )
|
|
|
|
{
|
|
|
|
return ( ( float* ) &origin )[index];
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idSphere idSphere::operator+( const idVec3& t ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return idSphere( origin + t, radius );
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idSphere& idSphere::operator+=( const idVec3& t )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
origin += t;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE bool idSphere::Compare( const idSphere& a ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return ( origin.Compare( a.origin ) && radius == a.radius );
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE bool idSphere::Compare( const idSphere& a, const float epsilon ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return ( origin.Compare( a.origin, epsilon ) && idMath::Fabs( radius - a.radius ) <= epsilon );
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE bool idSphere::operator==( const idSphere& a ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return Compare( a );
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE bool idSphere::operator!=( const idSphere& a ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return !Compare( a );
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idSphere::Clear()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
origin.Zero();
|
|
|
|
radius = -1.0f;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idSphere::Zero()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
origin.Zero();
|
|
|
|
radius = 0.0f;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idSphere::SetOrigin( const idVec3& o )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
origin = o;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idSphere::SetRadius( const float r )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
radius = r;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE const idVec3& idSphere::GetOrigin() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return origin;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE float idSphere::GetRadius() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return radius;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE bool idSphere::IsCleared() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return ( radius < 0.0f );
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE bool idSphere::AddPoint( const idVec3& p )
|
|
|
|
{
|
|
|
|
if( radius < 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
origin = p;
|
|
|
|
radius = 0.0f;
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
float r = ( p - origin ).LengthSqr();
|
2012-11-28 15:47:07 +00:00
|
|
|
if( r > radius * radius )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
r = idMath::Sqrt( r );
|
2012-11-28 15:47:07 +00:00
|
|
|
origin += ( p - origin ) * 0.5f * ( 1.0f - radius / r );
|
2012-11-26 18:58:24 +00:00
|
|
|
radius += 0.5f * ( r - radius );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE bool idSphere::AddSphere( const idSphere& s )
|
|
|
|
{
|
|
|
|
if( radius < 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
origin = s.origin;
|
|
|
|
radius = s.radius;
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
float r = ( s.origin - origin ).LengthSqr();
|
2012-11-28 15:47:07 +00:00
|
|
|
if( r > ( radius + s.radius ) * ( radius + s.radius ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
r = idMath::Sqrt( r );
|
2012-11-28 15:47:07 +00:00
|
|
|
origin += ( s.origin - origin ) * 0.5f * ( 1.0f - radius / ( r + s.radius ) );
|
2012-11-26 18:58:24 +00:00
|
|
|
radius += 0.5f * ( ( r + s.radius ) - radius );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idSphere idSphere::Expand( const float d ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return idSphere( origin, radius + d );
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idSphere& idSphere::ExpandSelf( const float d )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
radius += d;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idSphere idSphere::Translate( const idVec3& translation ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return idSphere( origin + translation, radius );
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idSphere& idSphere::TranslateSelf( const idVec3& translation )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
origin += translation;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE bool idSphere::ContainsPoint( const idVec3& p ) const
|
|
|
|
{
|
|
|
|
if( ( p - origin ).LengthSqr() > radius * radius )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE bool idSphere::IntersectsSphere( const idSphere& s ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
float r = s.radius + radius;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( ( s.origin - origin ).LengthSqr() > r * r )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idSphere::FromPointTranslation( const idVec3& point, const idVec3& translation )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
origin = point + 0.5f * translation;
|
|
|
|
radius = idMath::Sqrt( 0.5f * translation.LengthSqr() );
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idSphere::FromSphereTranslation( const idSphere& sphere, const idVec3& start, const idVec3& translation )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
origin = start + sphere.origin + 0.5f * translation;
|
|
|
|
radius = idMath::Sqrt( 0.5f * translation.LengthSqr() ) + sphere.radius;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idSphere::FromPointRotation( const idVec3& point, const idRotation& rotation )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idVec3 end = rotation * point;
|
|
|
|
origin = ( point + end ) * 0.5f;
|
|
|
|
radius = idMath::Sqrt( 0.5f * ( end - point ).LengthSqr() );
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idSphere::FromSphereRotation( const idSphere& sphere, const idVec3& start, const idRotation& rotation )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idVec3 end = rotation * sphere.origin;
|
|
|
|
origin = start + ( sphere.origin + end ) * 0.5f;
|
|
|
|
radius = idMath::Sqrt( 0.5f * ( end - sphere.origin ).LengthSqr() ) + sphere.radius;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idSphere::AxisProjection( const idVec3& dir, float& min, float& max ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
float d;
|
|
|
|
d = dir * origin;
|
|
|
|
min = d - radius;
|
|
|
|
max = d + radius;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !__BV_SPHERE_H__ */
|