TAPs 0.7.7.3
TAPsHelpCreateHalfEdgeModel.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsHelpCreateHalfEdgeModel.cpp
00003 
00004 Help create HalfEdgeModel called by Read Model Classes.
00005 
00006 SUKITTI PUNAK   (08/31/2005)
00007 ******************************************************************************/
00008 #include "TAPsHelpCreateHalfEdgeModel.hpp"
00009 // Using Inclusion Model (i.e. definitions are included in declarations)
00010 //                       (this name.cpp is included in name.hpp)
00011 // Each friend is defined directly inside its declaration.
00012 
00013 //-----------------------------------------------------------------------------
00014 // DEBUG ENABLE
00015 #ifdef TAPs_ENABLE_DEBUG
00016     #define DEBUG_MESSAGE_TAPs_HELP_CREATE_HALF_EDGE_MODEL_HPP
00017 #endif
00018 
00019 //#ifndef
00020 //  #define EXTRA_DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP
00021 //#endif
00022 //-----------------------------------------------------------------------------
00023 
00024 BEGIN_NAMESPACE_TAPs
00025 //=============================================================================
00026 template <typename T>
00027 HEVertex<T> ** HelpCreateHalfEdgeModel<T>::vertexList = NULL;
00028 template <typename T>
00029 DS::HashTableBySTLVector<int> * HelpCreateHalfEdgeModel<T>::vertexRingList = NULL;
00030 template <typename T>
00031 DS::HashTableBySTLVector< HEHalfEdge<T>* > * 
00032     HelpCreateHalfEdgeModel<T>::halfEdgeRingList = NULL;
00033 template <typename T>
00034 HEHalfEdge<T> * HelpCreateHalfEdgeModel<T>::boundaryHalfEdgePtr = NULL;
00035 //=============================================================================
00036 template <typename T>
00037 void HelpCreateHalfEdgeModel<T>::CreateHalfEdgeModel ( 
00038         OpenGL::HalfEdgeModel<T> * const prModel )
00039 {
00040     //----------------------------------------------------------------
00041     // Pair all half-edges
00042     PairAllHalfEdges();
00043     //----------------------------------------------------------------
00044     // Create boundary half-edges
00045     CreateBoundaryHalfEdges();
00046     HEHalfEdge<T> *tmpHalfEdge = boundaryHalfEdgePtr;
00047     #ifdef DEBUG_MESSAGE_TAPs_HELP_CREATE_HALF_EDGE_MODEL_HPP
00048     std::cout << "\nBOUNDARY HALF-EDGES:\n";
00049     #endif
00050     while ( tmpHalfEdge ) {
00051         #ifdef DEBUG_MESSAGE_TAPs_HELP_CREATE_HALF_EDGE_MODEL_HPP
00052         std::cout << *tmpHalfEdge << "\n";
00053         #endif
00054         tmpHalfEdge = tmpHalfEdge->Next();
00055     }
00056     #ifdef DEBUG_MESSAGE_TAPs_HELP_CREATE_HALF_EDGE_MODEL_HPP
00057     std::cout << "\n";
00058     #endif
00059     //----------------------------------------------------------------
00060     // Create hole faces
00061     CreateHoleFaces( prModel );
00062     //----------------------------------------------------------------
00063     // Deallocate memory for vertex list
00064     ClearAllocatedMemory( prModel );
00065     //----------------------------------------------------------------
00066 }
00067 //-----------------------------------------------------------------------------
00068 // Clear Allocated Memory
00069 template <typename T>
00070 void HelpCreateHalfEdgeModel<T>::ClearAllocatedMemory ( 
00071     OpenGL::HalfEdgeModel<T> * const prModel )
00072 {
00073     //----------------------------------------------------------------
00074     // Deallocate memory for vertex list
00075     #ifdef DEBUG_MESSAGE_TAPs_HELP_CREATE_HALF_EDGE_MODEL_HPP
00076     //HEVertex<T> * tmpVertex = prModel->GetVertexList()->Head();
00077     //while ( tmpVertex ) {
00078     //  std::cout << "Vertex#" << ++count << " " << *tmpVertex << "\n";
00079     //  tmpVertex = tmpVertex->Next();
00080     //}
00081     #endif
00082     if ( vertexList != NULL ) {
00083         delete [] vertexList;
00084         vertexList = NULL;
00085     }
00086     //----------------------------------------------------------------
00087     // Deallocate memory for vertex ring list
00088     #ifdef DEBUG_MESSAGE_TAPs_HELP_CREATE_HALF_EDGE_MODEL_HPP
00089     std::cout << "vertexRingList ==> " << *vertexRingList;
00090     #endif
00091     if ( vertexRingList != NULL ) {
00092         delete vertexRingList;
00093         vertexRingList = NULL;
00094     }
00095     //----------------------------------------------------------------
00096     // Deallocate memory for halfedge ring list
00097     #ifdef DEBUG_MESSAGE_TAPs_HELP_CREATE_HALF_EDGE_MODEL_HPP
00098     std::cout << "halfEdgeRingList ==> " << *halfEdgeRingList;
00099     #endif
00100     if ( halfEdgeRingList != NULL ) {
00101         delete halfEdgeRingList;
00102         halfEdgeRingList = NULL;
00103     }
00104     //----------------------------------------------------------------
00105     // Deallocate memory for boundary halfedge
00106     #ifdef DEBUG_MESSAGE_TAPs_HELP_CREATE_HALF_EDGE_MODEL_HPP
00107     std::cout << "boundaryHalfEdgePtr ==> " << *boundaryHalfEdgePtr;
00108     #endif
00109     if ( boundaryHalfEdgePtr != NULL ) {
00110         delete boundaryHalfEdgePtr;
00111         boundaryHalfEdgePtr = NULL;
00112     }
00113 }
00114 //-----------------------------------------------------------------------------
00115 // Create a half-edge from vertex1 to vertex2 incident to face
00116 // with prev and next half-edges
00117 template <typename T>
00118 HEHalfEdge<T> * const 
00119 HelpCreateHalfEdgeModel<T>::CreateHalfEdgeFrom ( 
00120                 HEVertex<T>   * const v1,
00121                 HEVertex<T>   * const v2,
00122                 HEFace<T>    * const face,
00123                 HEHalfEdge<T> * const hePrev, 
00124                 HEHalfEdge<T> * const heNext )
00125 {
00126     HEHalfEdge<T> * halfEdge = new HEHalfEdge<T>( v1, face, hePrev, heNext );
00127     // Set prev half-edge next to this half-edge
00128     if ( hePrev != NULL ) {
00129         hePrev->Next( halfEdge );
00130     }
00131     // Set next half-edge prev to this half-edge
00132     if ( heNext != NULL ) {
00133         heNext->Prev( halfEdge );
00134     }
00135     // Connect this half-edge to new ring
00136     // Or NULL(s) if hePrev / heNext is NULL
00137     //halfEdge->Prev( hePrev );
00138     //halfEdge->Next( heNext );
00139     
00140     if ( !v1->IncidentHalfEdge() )  v1->IncidentHalfEdge( halfEdge );
00141 
00142     return halfEdge;
00143 }
00144 //-----------------------------------------------------------------------------
00145 // Pair all (non-boundary) half-edges
00146 template <typename T>
00147 void HelpCreateHalfEdgeModel<T>::PairAllHalfEdges ()
00148 {
00149     int i, j, p, q;
00150     HEHalfEdge<T> *halfEdgeVitoVj, *halfEdgeVjtoVi;
00151     for ( i = 0; i < vertexRingList->GetNoOfBuckets(); ++i ) {
00152         for ( p = 0; p < static_cast<int>(vertexRingList->GetBucketNo(i).size()); ++p ) {
00153             // find pair for the half-edge from Vi to Vj
00154             j = vertexRingList->GetBucketNo(i)[p];
00155             halfEdgeVitoVj = halfEdgeRingList->GetBucketNo(i)[p];
00156             if ( halfEdgeVitoVj->Pair() == NULL ) {
00157                 // find the half-edge from Vj to Vi
00158                 for ( q = 0; q < static_cast<int>(vertexRingList->GetBucketNo(j).size()); ++q ) {
00159                     // found its pair -- half-edge from Vj to Vi
00160                     if ( i == vertexRingList->GetBucketNo(j)[q] ) {
00161                         halfEdgeVjtoVi = halfEdgeRingList->GetBucketNo(j)[q];
00162                         halfEdgeVitoVj->Pair( halfEdgeVjtoVi );
00163                         halfEdgeVjtoVi->Pair( halfEdgeVitoVj );
00164                         break;
00165                     }
00166                 }
00167             }
00168             if ( halfEdgeVitoVj->Pair() != NULL ) {
00169                 #ifdef DEBUG_MESSAGE_TAPs_READ_3DSMAX_ASC_HPP
00170                 std::cout << "Half-edge " << halfEdgeVitoVj << " from " << i << " to " << j 
00171                         << " has its pair at " << halfEdgeVitoVj->Pair()
00172                         << "\n";
00173                 #endif
00174             }
00175             else {
00176                 #ifdef DEBUG_MESSAGE_TAPs_READ_3DSMAX_ASC_HPP
00177                 std::cout << "Half-edge " << halfEdgeVitoVj << " from " << i << " to " << j 
00178                         << " has no pair.\n";
00179                 #endif
00180             }
00181 
00182 
00183         }
00184     }
00185 }
00186 //-----------------------------------------------------------------------------
00187 // Create boundary half-edges
00188 template <typename T>
00189 void HelpCreateHalfEdgeModel<T>::CreateBoundaryHalfEdges ()
00190 {
00191     int i, p;
00192     HEHalfEdge<T> *halfEdge, *boundaryHalfEdge, *curtHalfEdge = NULL;
00193     for ( i = 0; i < vertexRingList->GetNoOfBuckets(); ++i ) {
00194         for ( p = 0; p < static_cast<int>(vertexRingList->GetBucketNo(i).size()); ++p ) {
00195             halfEdge = halfEdgeRingList->GetBucketNo(i)[p];
00196             if ( !halfEdge->Pair() ) {
00197                 if ( !boundaryHalfEdgePtr ) {
00198                     boundaryHalfEdgePtr = 
00199                     curtHalfEdge = new HEHalfEdge<T>(
00200                         halfEdge->Next()->Vertex(), // originating from vertex
00201                         NULL,                       // incident (hole) face
00202                         NULL,                       // previous
00203                         NULL,                       // next
00204                         halfEdge                    // pair
00205                     );
00206                 }
00207                 else {
00208                     boundaryHalfEdge = new HEHalfEdge<T>(
00209                         halfEdge->Next()->Vertex(), // originating from vertex
00210                         NULL,                       // incident (hole) face
00211                         curtHalfEdge,               // previous
00212                         NULL,                       // next
00213                         halfEdge                    // pair
00214                     );
00215                     curtHalfEdge->Next( boundaryHalfEdge );
00216                     curtHalfEdge = boundaryHalfEdge;
00217                 }
00218                 halfEdge->Pair( curtHalfEdge );
00219             }
00220         }
00221     }
00222 }
00223 //-----------------------------------------------------------------------------
00224 // Create hole faces
00225 template <typename T>
00226 void HelpCreateHalfEdgeModel<T>::CreateHoleFaces ( 
00227             OpenGL::HalfEdgeModel<T> * const prModel )
00228 {
00229     int count = 0;
00230     while ( boundaryHalfEdgePtr ) {
00231         #ifdef DEBUG_MESSAGE_TAPs_READ_3DSMAX_ASC_HPP
00232         std::cout << "Find Hole# " << ++count << "\n";
00233         #endif
00234         CreateAHoleFace( prModel );
00235     }
00236 }
00237 //-----------------------------------------------------------------------------
00238 // Create a hole face from model's boundary half-edge list
00239 template <typename T>
00240 void HelpCreateHalfEdgeModel<T>::CreateAHoleFace ( 
00241             OpenGL::HalfEdgeModel<T> * const prModel )
00242 {
00243     HEHalfEdge<T> *curtHalfEdge = boundaryHalfEdgePtr;
00244     HEHalfEdge<T> *iterHalfEdge;
00245     #ifdef DEBUG_MESSAGE_TAPs_READ_3DSMAX_ASC_HPP
00246     std::cout << "REMOVE: " << curtHalfEdge << std::endl;
00247     #endif
00248     boundaryHalfEdgePtr = boundaryHalfEdgePtr->Next();
00249     HEHalfEdge<T> *halfEdge = boundaryHalfEdgePtr;
00250     //boundaryHalfEdgePtr->Prev( NULL );
00251     HEFace<T> *holeFace = new HEFace<T>( curtHalfEdge );
00252     //std::cout << *holeFace << "\n";
00253     curtHalfEdge->Face( holeFace );
00254     curtHalfEdge->Prev( NULL );
00255     curtHalfEdge->Next( NULL );
00256     prModel->GetHoleFaceList()->Append( holeFace );
00257 
00258     // Find previous half-edges until the half-edge loop of the hole face is complete
00259     while ( !holeFace->IncidentHalfEdge()->Next() ) {
00260         // Found a previous half-edge
00261         if ( halfEdge->Pair()->Vertex() == curtHalfEdge->Vertex() ) {
00262             #ifdef DEBUG_MESSAGE_TAPs_READ_3DSMAX_ASC_HPP
00263             std::cout << "REMOVE: " << halfEdge << std::endl;
00264             #endif
00265             // Remove the half-edge from the boundary list
00266             if ( halfEdge == boundaryHalfEdgePtr ) {
00267                 boundaryHalfEdgePtr = boundaryHalfEdgePtr->Next();
00268                 iterHalfEdge = boundaryHalfEdgePtr;
00269             }
00270             else {
00271                 if ( halfEdge->Next() ) {
00272                     halfEdge->Prev()->Next( halfEdge->Next() );
00273                     halfEdge->Next()->Prev( halfEdge->Prev() );
00274                 }
00275                 else {
00276                     halfEdge->Prev()->Next( NULL );
00277                 }
00278                 iterHalfEdge = halfEdge->Next();
00279             }
00280             halfEdge->Face( holeFace );
00281             halfEdge->Next( curtHalfEdge ); // add half-edge to the loop
00282             curtHalfEdge = halfEdge;        // new current half-edge
00283             // Loop is complete here
00284             if ( holeFace->IncidentHalfEdge()->Pair()->Vertex() == halfEdge->Vertex() ) {
00285                 halfEdge->Prev( holeFace->IncidentHalfEdge() );
00286                 holeFace->IncidentHalfEdge()->Next( halfEdge );
00287             }
00288             // Loop is not complete yet
00289             else {
00290                 halfEdge->Prev( NULL );
00291             }
00292             halfEdge = iterHalfEdge;
00293         }
00294         else {
00295             halfEdge = halfEdge->Next();
00296         }
00297         // Back to the start of boundaryHalfEdgePtr
00298         if ( !halfEdge ) {
00299             halfEdge = boundaryHalfEdgePtr;
00300         }
00301     }
00302 }
00303 //-----------------------------------------------------------------------------
00304 //=============================================================================
00305 END_NAMESPACE_TAPs
00306 //-----------------------------------------------------------------------------
00307 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00308 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines