00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #pragma once
00013
00015 #include "nvsgcommon.h"
00016
00017 #include "nvmath/Mat44f.h"
00018 #include "nvmath/Sphere3f.h"
00019
00020 namespace nvmath
00021 {
00023
00026 class BoundingSphere : public Sphere3f
00027 {
00028 public:
00030 BoundingSphere();
00031
00032 public:
00034
00037 NVSG_API bool extend( const Sphere3f &sphere
00038 );
00039
00041
00043 NVSG_API const Sphere3f & getSphere( void ) const;
00044
00046
00047 NVSG_API void setSphere( const Sphere3f &sphere );
00048
00050
00052 NVSG_API const Vec3f & getCenter( void ) const;
00053
00055
00057 NVSG_API float getRadius( void ) const;
00058
00060
00061 NVSG_API void invalidate( void );
00062
00064
00066 NVSG_API bool isValid( void ) const;
00067
00069
00071 NVSG_API bool set( const Vec3f * vertices
00072 , size_t numVertices
00073 );
00074
00076
00077 NVSG_API void transform( const Mat44f &m
00078 );
00079
00081 NVSG_API void enlarge( float factor
00082 );
00083
00084 private:
00085 bool m_valid;
00086 };
00087
00088
00089
00090
00091 inline BoundingSphere::BoundingSphere()
00092 : m_valid(false)
00093 {
00094 }
00095
00096 inline const Sphere3f & BoundingSphere::getSphere( void ) const
00097 {
00098 __ASSERT( m_valid );
00099 return( *this );
00100 }
00101
00102 inline void BoundingSphere::setSphere( const Sphere3f &sphere )
00103 {
00104 m_valid = true;
00105 setCenter( sphere.getCenter() );
00106 setRadius( sphere.getRadius() );
00107 }
00108
00109 inline const Vec3f & BoundingSphere::getCenter( void ) const
00110 {
00111 __ASSERT( m_valid );
00112 return( Sphere3f::getCenter() );
00113 }
00114
00115 inline float BoundingSphere::getRadius( void ) const
00116 {
00117 __ASSERT( m_valid );
00118 return( Sphere3f::getRadius() );
00119 }
00120
00121 inline void BoundingSphere::invalidate( void )
00122 {
00123 m_valid = false;
00124 }
00125
00126 inline bool BoundingSphere::isValid( void ) const
00127 {
00128 return( m_valid );
00129 }
00130
00131 inline void BoundingSphere::transform( const Mat44f &m )
00132 {
00133 __ASSERT( m_valid );
00134 setCenter( Vec3f( m * Vec4f( Sphere3f::getCenter(), 1.0f ) ) );
00135 }
00136
00137 inline void BoundingSphere::enlarge( float factor )
00138 {
00139 __ASSERT( m_valid );
00140 setRadius( Sphere3f::getRadius() * factor );
00141 }
00142
00143 inline bool BoundingSphere::set( const Vec3f * vertices, size_t numVertices )
00144 {
00145 __ASSERT( numVertices > 1 );
00146 bool m_valid = false;
00147 for ( size_t i=1 ; !m_valid && i<numVertices ; i++ )
00148 {
00149 m_valid = ( FLT_EPSILON < distance( vertices[0], vertices[i] ) );
00150 }
00151 if ( m_valid )
00152 {
00153 setSphere( boundingSphere( vertices, numVertices ) );
00154 }
00155 return( m_valid );
00156 }
00157
00158 inline bool BoundingSphere::extend( const Sphere3f &sphere )
00159 {
00160 setSphere( m_valid ? boundingSphere( getSphere(), sphere ) : sphere );
00161 return( m_valid );
00162 }
00163
00164 }