TAPs 0.7.7.3
TAPsBVHNodeLeaf.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsBVHNodeLeaf.cpp
00003 
00004 BVHNodeLeaf class is for a Boundary Volume Hierarchy Leaf Node.
00005 It does not contain any primitive.
00006 
00007 SUKITTI PUNAK   (10/14/2009)
00008 UPDATE          (10/08/2010)
00009 ******************************************************************************/
00010 #include "TAPsBVHNodeLeaf.hpp"
00011 // Using Inclusion Model (i.e. definitions are included in declarations)
00012 //                       (this name.cpp is included in name.hpp)
00013 // Each friend is defined directly inside its declaration.
00014 
00015 BEGIN_NAMESPACE_TAPs
00016 //=============================================================================
00017 // Constructor(s) and Destructor
00018 //-----------------------------------------------------------------------------
00019 template <typename T>
00020 BVHNodeLeaf<T>::BVHNodeLeaf (
00021     Enum::CD            type, 
00022     int                 id, 
00023     BVHNode<T> *        parent )
00024     : BVHNode<T>( id, type, parent )
00025 {}
00026 //-----------------------------------------------------------------------------
00027 template <typename T>
00028 BVHNodeLeaf<T>::~BVHNodeLeaf ()
00029 {}
00030 //-----------------------------------------------------------------------------
00031 // StrInfo
00032 template <typename T>
00033 std::string BVHNodeLeaf<T>::StrInfo () const
00034 {
00035     std::stringstream output;
00036     output  <<  "TAPs::BVHNodeLeaf<"
00037             <<  typeid(T).name() << "> Class\n";
00038     return output.str();
00039 }
00040 //-----------------------------------------------------------------------------
00041 //=============================================================================
00042 // Node Operations
00043 //-----------------------------------------------------------------------------
00044 // Update
00045 // update only this node
00046 template <typename T>
00047 void BVHNodeLeaf<T>::Update ()
00048 {
00049     //---------------------------------------------------------------
00050     switch ( m_eType ) {
00051         case Enum::BVH_NODE_LEAF_SPHERE:
00052             UpdateSphere();
00053             break;
00054         case Enum::BVH_NODE_LEAF_AABB:
00055             break;
00056         case Enum::BVH_NODE_LEAF_OBB:
00057             break;
00058         default:
00059             assert( false );
00060             break;
00061     }
00062 }
00063 //-----------------------------------------------------------------------------
00064 // UpdateSphere
00065 template <typename T>
00066 void BVHNodeLeaf<T>::UpdateSphere ()
00067 {
00068     //std::cout << "UpdateSphere Node: " << this << "\n";
00069 
00070     /*
00071     //---------------------------------------------------------------
00072     Vector3<T> AABB[2];
00073     //HEVertex<T> * vertex = m_listVertex->Head();
00074     HEHalfEdge<T> * firstHalfEdge = m_primHEFace->IncidentHalfEdge();
00075     HEHalfEdge<T> * halfEdge = firstHalfEdge->Next();
00076     Vector3<T> vertex = firstHalfEdge->Vertex()->GetPosition();
00077     AABB[0] = AABB[1] = vertex;
00078     //---------------------------------------------------------------
00079     // For Each Vertex
00080      while ( halfEdge != firstHalfEdge ) {
00081         vertex = halfEdge->Vertex()->GetPosition();
00082         // Find the lowest an the highest of x, y, and z
00083         // AABB[0] is min and AABB[1] is max
00084         if      ( AABB[0][0] > vertex[0] )  AABB[0][0] = vertex[0];
00085         else if ( AABB[1][0] < vertex[0] )  AABB[1][0] = vertex[0];
00086         if      ( AABB[0][1] > vertex[1] )  AABB[0][1] = vertex[1];
00087         else if ( AABB[1][1] < vertex[1] )  AABB[1][1] = vertex[1];
00088         if      ( AABB[0][2] > vertex[2] )  AABB[0][2] = vertex[2];
00089         else if ( AABB[1][2] < vertex[2] )  AABB[1][2] = vertex[2];
00090         // Next vertex
00091         halfEdge = halfEdge->Next();
00092     }
00093     //---------------------------------------------------------------
00094     // Find the bounding volume center from the AABB
00095     Vector3<T> vCenter( ( AABB[0][0] + AABB[1][0] ) / 2.0,
00096                         ( AABB[0][1] + AABB[1][1] ) / 2.0,
00097                         ( AABB[0][2] + AABB[1][2] ) / 2.0 );
00098     //---------------------------------------------------------------
00099     // Find the Sphere Bounding Volume
00100     T radius = 0, squaredLength;
00101     halfEdge = firstHalfEdge = m_primHEFace->IncidentHalfEdge();
00102     //-----------------------------------------------------
00103     do {
00104         vertex = halfEdge->Vertex()->GetPosition();
00105         //squaredLength = (vertex - vCenter).SquaredLength();
00106         squaredLength = (vertex - vCenter).Length();
00107         if ( squaredLength > radius )   radius = squaredLength;
00108         halfEdge = halfEdge->Next();
00109     } while ( halfEdge != firstHalfEdge );
00110     //radius = sqrt( radius );
00111     //---------------------------------------------------------------
00112     SetCenter( vCenter );
00113     SetRadius( radius );
00114     //*/
00115 }
00116 //-----------------------------------------------------------------------------
00117 // TestOverlapWith
00118 template <typename T>
00119 T BVHNodeLeaf<T>::TestOverlapWith ( BVHNode<T> const * const that ) const
00120 {
00121     switch ( m_eType ) {
00122         case Enum::BVH_NODE_BINARY_SPHERE:
00123         case Enum::BVH_NODE_QUAD_SPHERE:
00124         case Enum::BVH_NODE_OCTANT_SPHERE:
00125         case Enum::BVH_NODE_GENERIC_SPHERE:
00126         case Enum::BVH_NODE_LEAF_SPHERE:
00127             switch ( that->GetType() ) {
00128                 case Enum::BVH_NODE_BINARY_SPHERE:
00129                 case Enum::BVH_NODE_QUAD_SPHERE:
00130                 case Enum::BVH_NODE_OCTANT_SPHERE:
00131                 case Enum::BVH_NODE_GENERIC_SPHERE:
00132                 case Enum::BVH_NODE_LEAF_SPHERE:
00133                     return    ( GetCenter() - that->GetCenter() ).Length() 
00134                             - ( GetRadius() + that->GetRadius() );  // m_vW[0] is m_tRadius
00135                     break;
00136                 default:
00137                     std::cout << "Error: BVHNode<T>::TestOverlapWith Fn!" << std::endl;
00138                     assert( false );
00139                     break;
00140             }
00141             break;
00142         default:
00143             std::cout << "Error: BVHNode<T>::TestOverlapWith Fn!" << std::endl;
00144             assert( false );
00145             break;
00146     }
00147     return -777;    // -777 is an Error Code
00148 }
00149 //-----------------------------------------------------------------------------
00150 // TestOverlapSphereWithSphere #1
00151 template <typename T>
00152 T BVHNodeLeaf<T>::TestOverlapSphereWithSphere ( 
00153             BVHNode<T> const * const that ) const
00154 {
00155     std::cout << "BVHNodeLeaf<T>::TestOverlapSphereWithSphere( #1 )\n";
00156 
00157     if ( that->IsLeaf() ) {
00158         T distance = BVHNode<T>::TestOverlapSphereWithSphere( that );
00159         return distance;
00160     }
00161     else {
00162         return BVHNode<T>::TestOverlapSphereWithSphere( that );
00163     }
00164 }
00165 //-----------------------------------------------------------------------------
00166 // TestOverlapSphereWithSphere #2
00167 template <typename T>
00168 T BVHNodeLeaf<T>::TestOverlapSphereWithSphere ( 
00169             BVHNode<T> const * const that, 
00170             Matrix4x4<T> const & thatTransform ) const
00171 {
00172     std::cout << "BVHNodeLeaf<T>::TestOverlapSphereWithSphere( #2 )\n";
00173 
00174     if ( that->IsLeaf() ) {
00175         T distance = BVHNode<T>::TestOverlapSphereWithSphere( that, thatTransform );
00176         return distance;
00177     }
00178     else {
00179         return BVHNode<T>::TestOverlapSphereWithSphere( that, thatTransform );
00180     }
00181 }
00182 //-----------------------------------------------------------------------------
00183 // TestOverlapSphereWithSphere #3
00184 template <typename T>
00185 T BVHNodeLeaf<T>::TestOverlapSphereWithSphere ( 
00186             Matrix4x4<T> const & thisTransform, 
00187             BVHNode<T> const * const that ) const
00188 {
00189     std::cout << "BVHNodeLeaf<T>::TestOverlapSphereWithSphere( #3 )\n";
00190 
00191     if ( that->IsLeaf() ) {
00192         T distance = BVHNode<T>::TestOverlapSphereWithSphere( thisTransform, that );
00193         return distance;
00194     }
00195     else {
00196         return BVHNode<T>::TestOverlapSphereWithSphere( thisTransform, that );
00197     }
00198 }
00199 //-----------------------------------------------------------------------------
00200 // TestOverlapSphereWithSphere #4
00201 template <typename T>
00202 T BVHNodeLeaf<T>::TestOverlapSphereWithSphere ( 
00203             Matrix4x4<T> const & thisTransform, 
00204             BVHNode<T> const * const that, 
00205             Matrix4x4<T> const & thatTransform ) const
00206 {
00207     std::cout << "BVHNodeLeaf<T>::TestOverlapSphereWithSphere( #4 )\n";
00208 
00209     if ( that->IsLeaf() ) {
00210         T distance = BVHNode<T>::TestOverlapSphereWithSphere( thisTransform, that, thatTransform );
00211         return distance;
00212     }
00213     else {
00214         return BVHNode<T>::TestOverlapSphereWithSphere( thisTransform, that, thatTransform );
00215     }
00216 }
00217 //-----------------------------------------------------------------------------
00218 
00219 
00220 //-----------------------------------------------------------------------------
00221 // TestOverlapSphereWithBVSphere #1
00222 template <typename T>
00223 T BVHNodeLeaf<T>::TestOverlapSphereWithBVSphere ( 
00224     Vector3<T> const & centerOfBVSphere,    
00225     T radiusOfBVSphere                      
00226 ) const
00227 {
00228     std::cout << "BVHNodeLeaf<T>::TestOverlapSphereWithBVSphere( #1 )\n";
00229 
00230     T distance = BVHNode<T>::TestOverlapSphereWithBVSphere( centerOfBVSphere, radiusOfBVSphere );
00231     return distance;
00232 }
00233 //-----------------------------------------------------------------------------
00234 // TestOverlapSphereWithBVSphere #2
00235 template <typename T>
00236 T BVHNodeLeaf<T>::TestOverlapSphereWithBVSphere ( 
00237     Matrix4x4<T> const & transformA,        
00238     Vector3<T> const & centerOfBVSphere,    
00239     T radiusOfBVSphere                      
00240 ) const
00241 {
00242     std::cout << "BVHNodeLeaf<T>::TestOverlapSphereWithBVSphere( #2 )\n";
00243 
00244     T distance = BVHNode<T>::TestOverlapSphereWithBVSphere( transformA, centerOfBVSphere, radiusOfBVSphere );
00245     return distance;
00246 }
00247 //-----------------------------------------------------------------------------
00248 
00249 
00250 //-----------------------------------------------------------------------------
00251 // TestOverlapSphereWithBVCylinder #1
00252 template <typename T>
00253 bool BVHNodeLeaf<T>::TestOverlapSphereWithBVCylinder_AtOrigin ( 
00254     Vector3<T> const & centerOfBVCylinder,  
00255     T radiusOfBVCylinder,                   
00256     T heightOfBVCylinder                    
00257 ) const
00258 {
00259     std::cout << "BVHNodeLeaf<T>::TestOverlapSphereWithBVCylinder( #1 )\n";
00260 
00261     T distance = BVHNode<T>::TestOverlapSphereWithBVCylinder_AtOrigin( radiusOfBVCylinder, heightOfBVCylinder );
00262     if ( distance > Math<T>::ZERO ) return false;
00263     else                            return true;
00264 }
00265 //-----------------------------------------------------------------------------
00266 // TestOverlapSphereWithBVCylinder #2
00267 template <typename T>
00268 bool BVHNodeLeaf<T>::TestOverlapSphereWithBVCylinder_AtOrigin ( 
00269     Matrix4x4<T> const & transformA,        
00270     Vector3<T> const & centerOfBVCylinder,  
00271     T radiusOfBVCylinder,                   
00272     T heightOfBVCylinder                    
00273 ) const
00274 {
00275     std::cout << "BVHNodeLeaf<T>::TestOverlapSphereWithBVCylinder( #2 )\n";
00276 
00277     T distance = BVHNode<T>::TestOverlapSphereWithBVCylinder_AtOrigin( transformA, radiusOfBVCylinder, heightOfBVCylinder );
00278     return ( distance > Math<T>::ZERO );
00279 }
00280 //-----------------------------------------------------------------------------
00281 
00282 
00283 #if defined(__gl_h_) || defined(__GL_H__)
00284 //=============================================================================
00285 // DrawByOpenGL
00286 //-----------------------------------------------------------------------------
00287 template <typename T>
00288 void BVHNodeLeaf<T>::DrawByOpenGL () const
00289 {
00290     DrawBV();
00291 }
00292 //-----------------------------------------------------------------------------
00293 template <typename T>
00294 void BVHNodeLeaf<T>::DrawByOpenGL ( 
00295         int currentLevel, int startLevel, int endLevel ) const
00296 {
00297     if ( startLevel <= currentLevel && currentLevel <= endLevel ) {
00298         DrawBV();
00299     }
00300 }
00301 //-----------------------------------------------------------------------------
00302 template <typename T>
00303 void BVHNodeLeaf<T>::DrawBV ( GLenum drawStyle ) const
00304 {
00305     switch ( m_eType ) {
00306         //-----------------------------------------------------------
00307         // DRAW SPHERE BVH
00308         case Enum::BVH_NODE_LEAF_SPHERE:
00309             DrawSphereBV( drawStyle );
00310             break;
00311         //-----------------------------------------------------------
00312         // DRAW AABB BVH
00313         case Enum::BVH_NODE_LEAF_AABB:
00314             DrawAABBBV( drawStyle );
00315             break;
00316         //-----------------------------------------------------------
00317         // DRAW OBB BVH
00318         case Enum::BVH_NODE_LEAF_OBB:
00319             DrawOBBBV( drawStyle );
00320             break;
00321 
00322         //-----------------------------------------------------------
00323         // DRAW SPHERE BVH
00324         case Enum::BVH_NODE_BINARY_SPHERE:
00325         case Enum::BVH_NODE_QUAD_SPHERE:
00326         case Enum::BVH_NODE_OCTANT_SPHERE:
00327         case Enum::BVH_NODE_GENERIC_SPHERE:
00328             DrawSphereBV();
00329             break;
00330         //-----------------------------------------------------------
00331         // DRAW AABB BVH
00332         case Enum::BVH_NODE_BINARY_AABB:
00333         case Enum::BVH_NODE_QUAD_AABB:
00334         case Enum::BVH_NODE_OCTANT_AABB:
00335         case Enum::BVH_NODE_GENERIC_AABB:
00336             DrawAABBBV();
00337             break;
00338         //-----------------------------------------------------------
00339         // DRAW OBB BVH
00340         case Enum::BVH_NODE_BINARY_OBB:
00341         case Enum::BVH_NODE_QUAD_OBB:
00342         case Enum::BVH_NODE_OCTANT_OBB:
00343         case Enum::BVH_NODE_GENERIC_OBB:
00344             DrawOBBBV();
00345             break;
00346     }
00347 }
00348 //-----------------------------------------------------------------------------
00349 #endif // #if defined(__gl_h_) || defined(__GL_H__)
00350 //=============================================================================
00351 END_NAMESPACE_TAPs
00352 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00353 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines