TAPs 0.7.7.3
TAPsOpenWound_TycoEndostitch_V2.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsOpenWound_TycoEndostitch_V2.cpp
00003 ******************************************************************************/
00007 /******************************************************************************
00008 SUKITTI PUNAK   (06/15/2010)
00009 UPDATE          (03/02/2011)
00010 ******************************************************************************/
00011 #include "TAPsOpenWound_TycoEndostitch_V2.hpp"
00012 // Using Inclusion Model (i.e. definitions are included in declarations)
00013 //                       (this name.cpp is included in name.hpp)
00014 // Each friend is defined directly inside its declaration.
00015 
00016 BEGIN_NAMESPACE_TAPs
00017 //=============================================================================
00018 // Constructors
00019 //-----------------------------------------------------------------------------
00020 template <typename T, typename DATA>
00021 OpenWound_TycoEndostitch_V2<T,DATA>::OpenWound_TycoEndostitch_V2 ( std::string const & modelFile )
00022     : ModelDefBasedOnFEMHex<T,DATA>( modelFile )
00023 {}
00024 //-----------------------------------------------------------------------------
00025 template <typename T, typename DATA>
00026 OpenWound_TycoEndostitch_V2<T,DATA>::~OpenWound_TycoEndostitch_V2 ()
00027 {
00028     ClearAllPuncturedLocations();
00029 }
00030 //-----------------------------------------------------------------------------
00031 template <typename T, typename DATA>
00032 std::string OpenWound_TycoEndostitch_V2<T,DATA>::StrInfo () const
00033 {
00034     std::ostringstream ss;
00035     ss << "OpenWound_TycoEndostitch_V2<" << typeid(T).name() << "," << typeid(DATA).name() << ">";
00036     return ss.str();
00037 }
00038 //-----------------------------------------------------------------------------
00039 //=============================================================================
00040 
00041 
00042 
00043 
00044 //=============================================================================
00045 // Simulation
00046 //-----------------------------------------------------------------------------
00047 template <typename T, typename DATA>
00048 void OpenWound_TycoEndostitch_V2<T,DATA>::ClearAllPunctureInfo ()
00049 {
00050     ClearAllPuncturedPoints();      // on the surface mesh
00051     ClearAllPuncturedLocations();   // inside the FEM mesh
00052 }
00053 //-----------------------------------------------------------------------------
00054 template <typename T, typename DATA>
00055 void OpenWound_TycoEndostitch_V2<T,DATA>::AddPuncturedPoint ( HEVertex<T> * pV )
00056 {
00057     m_SetOfPuncturedPoints.push_back( pV );
00058 }
00059 //-----------------------------------------------------------------------------
00060 template <typename T, typename DATA>
00061 void OpenWound_TycoEndostitch_V2<T,DATA>::DeleteTheLastPuncturedPoint ()
00062 {
00063     m_SetOfPuncturedPoints.erase( m_SetOfPuncturedPoints.end() - 1 );
00064 }
00065 //-----------------------------------------------------------------------------
00066 template <typename T, typename DATA>
00067 HEVertex<T> * OpenWound_TycoEndostitch_V2<T,DATA>::GetPuncturedPointOnSurface ( unsigned int i )
00068 {
00069     assert( i < m_SetOfPuncturedPoints.size() );
00070     return m_SetOfPuncturedPoints[i];
00071 }
00072 //-----------------------------------------------------------------------------
00073 template <typename T, typename DATA>
00074 void OpenWound_TycoEndostitch_V2<T,DATA>::AddInterpolatedPuncturedLocations (
00075     int sutureID,           
00076     int suturePa,           
00077     int suturePb,           
00078     int modelID,            
00079     HEVertex<T> * pModelVa, 
00080     HEVertex<T> * pModelVb, 
00081     int ListOfSuturePts[],  
00082     HEVertex<T> * ListOfInterpolatedPuncturedLocations[],   
00083     int & numOfPts,         
00084     T * pMinIntervalDistance    
00085 )
00086 {
00087     if ( numOfPts <= 0 )    return;
00088 
00089     // BOTH pModelVa and pModelVb MUST BE FROM THIS MODEL'S SURFACE MESH VERTICES!
00090     // Since the both vertices pointed by pModelVa and pModelVb are the model's surface mesh vertices, 
00091     // the vertices' positions are in the local coordinates.
00092     // Therefore, no transforamtion is needed.
00093 
00094     // THE LOCATION IS ALREADY IN THE LOCAL FEM MESH COORDINATES
00095     Vector3<T> locA = pModelVa->RefToPosition();
00096     Vector3<T> locB = pModelVb->RefToPosition();
00097     //TransformationSupport<T> & trx = RefToTransformationSupport();
00098     //Matrix4x4<T> invTrxMat = trx.GetMatrixTransform().GetInverse();
00099     //locA = invTrxMat * locA;
00100     //locB = invTrxMat * locB;
00101 
00102     // Swap if suturePa is less than suturePb
00103     int * sutureIntPts = NULL;
00104     if ( suturePa > suturePb ) {
00105         int tmp = suturePa;
00106         suturePa = suturePb;
00107         suturePb = tmp;
00108         Vector3<T> V = locA;
00109         locA = locB;
00110         locB = V;
00111     }
00112 
00113     // Determine the number of interpolated locations available and initialize variables
00114     int posNumOfPts = suturePb - suturePa - 1;
00115     if ( posNumOfPts <= 0 ) return; // no suture points left for interpolations
00116     if ( posNumOfPts < numOfPts )   numOfPts = posNumOfPts;
00117 
00118     // Adjust the numOfPts if the pMinIntervalDistance is greater than the current interval distance
00119     T totalDistance = (locB - locA).Length();
00120     T intervalDist = totalDistance / ( numOfPts+1 );
00121     if ( pMinIntervalDistance != NULL && intervalDist < *pMinIntervalDistance ) {
00122         int finalNumOfPts = static_cast<int>( totalDistance / *pMinIntervalDistance ) - 1;
00123         if ( finalNumOfPts <= 0 ) {
00124             numOfPts = 0;
00125             return;
00126         }
00127         else if ( finalNumOfPts < numOfPts ) {
00128             numOfPts = finalNumOfPts;
00129         }
00130     }
00131 
00132     //int incPt = posNumOfPts / numOfPts;
00133     int incPt = 1;
00134     int sutureP = suturePa+incPt;
00135     Vector3<T> incLoc = ( locB - locA ) / ( numOfPts+1 );
00136     Vector3<T> Loc = locA + incLoc;
00137 
00138 
00139     //std::cout << "suturePb & Pa: " << suturePb << " & " << suturePa << "\n";
00140     //std::cout << "incPt: " << incPt << "\n";
00141 
00142 
00143     // Find the element id (i.e. grid locations) that locA is in.
00144     // Do the same for locB.
00145     unsigned int gridLocs[2][3];
00146     HexBarycentricCoordsForVertexRef<T,DATA> * pBCa = dynamic_cast< HexBarycentricCoordsForVertexRef<T,DATA> * >( pModelVa->GetBarycentricPtr() );
00147     HexBarycentricCoordsForVertexRef<T,DATA> * pBCb = dynamic_cast< HexBarycentricCoordsForVertexRef<T,DATA> * >( pModelVb->GetBarycentricPtr() );
00148     pBCa->GetGridLocations( gridLocs[0][0], gridLocs[0][1], gridLocs[0][2] );
00149     pBCb->GetGridLocations( gridLocs[1][0], gridLocs[1][1], gridLocs[1][2] );
00150     T paraCoords[2][3];
00151     pModelVa->GetBarycentricPtr()->GetGenBaryCoords( paraCoords[0][0], paraCoords[0][1], paraCoords[0][2] );
00152     pModelVb->GetBarycentricPtr()->GetGenBaryCoords( paraCoords[1][0], paraCoords[1][1], paraCoords[1][2] );
00153 
00154     //std::cout << "GridA: " << Vector3<unsigned int>( gridLocs[0] ) << "\n";
00155     //std::cout << "GridB: " << Vector3<unsigned int>( gridLocs[1] ) << "\n";
00156     //std::cout << "ParaCoordA: " << Vector3<T>( paraCoords[0] ) << "\n";
00157     //std::cout << "ParaCoordB: " << Vector3<T>( paraCoords[1] ) << "\n";
00158     //std::cout << "ElementAid: " << GetElementIDFromGridLocation( gridLocs[0][0], gridLocs[0][1], gridLocs[0][2] ) << "\n";
00159     //std::cout << "ElementBid: " << GetElementIDFromGridLocation( gridLocs[1][0], gridLocs[1][1], gridLocs[1][2] ) << "\n";
00160 
00161     unsigned int gridSets[3][2]; // grid [x,y,z][low-to-high]
00162     for ( unsigned int i = 0; i < 3; ++i ) {
00163         if ( gridLocs[0][i] < gridLocs[1][i] ) {
00164             gridSets[i][0] = gridLocs[0][i];
00165             gridSets[i][1] = gridLocs[1][i];
00166         }
00167         else {
00168             gridSets[i][0] = gridLocs[1][i];
00169             gridSets[i][1] = gridLocs[0][i];
00170         }
00171     }
00172     //std::cout << "gridSets x: " << gridSets[0][0] << " to " << gridSets[0][1] << "\n";
00173     //std::cout << "gridSets y: " << gridSets[1][0] << " to " << gridSets[1][1] << "\n";
00174     //std::cout << "gridSets z: " << gridSets[2][0] << " to " << gridSets[2][1] << "\n";
00175 
00176     // Interpolate and record the puncture locations
00177     int elementID;
00178     T intepolatedParaCoords[3];
00179     int countFoundLocations = 0;
00180     for ( int i = 0; i < numOfPts; ++i ) {
00181         bool bFound = false;
00182         for ( unsigned int x = gridSets[0][0]; !bFound && x <= gridSets[0][1]; ++x ) {
00183             for ( unsigned int y = gridSets[1][0]; !bFound && y <= gridSets[1][1]; ++y ) {
00184                 for ( unsigned int z = gridSets[2][0]; !bFound && z <= gridSets[2][1]; ++z ) {
00185                     //std::cout << x << " " << y << " " << z << "\n";
00186                     elementID = GetElementIDFromGridLocation( x, y, z );
00187                     //std::cout << "elementID: " << elementID << "\n";
00188                     if ( elementID >= 0 ) {
00189                         bFound = RefToFEMMesh().RefToElement( elementID ).EstimateParametricCoords( 
00190                             Loc[0], Loc[1], Loc[2], 
00191                             intepolatedParaCoords[0], intepolatedParaCoords[1], intepolatedParaCoords[2] ) 
00192                             > 0 ? true : false;
00193                         //std::cout << "intetpolatedParaCoords: " << Vector3<T>( intepolatedParaCoords ) << "\n";
00194 
00195                         if ( bFound ) {
00196                             //std::cout << "FOUND intetpolatedParaCoords: " << Vector3<T>( intepolatedParaCoords ) << "\n";
00197 
00198                             ++countFoundLocations;
00199 
00200                             AddExtraVertexInLocalSpace( Loc, elementID, &RefToFEMMesh().RefToElement( elementID ), Vector3<unsigned int>(x,y,z), intepolatedParaCoords );
00201                             ListOfInterpolatedPuncturedLocations[i] = m_SetOfExtraVertices.back();
00202                             //*/
00203 
00204                             //std::cout << "sutureP: " << sutureP << "\n";
00205 
00206                             ListOfSuturePts[i] = sutureP;
00207                         }
00208                     }
00209                 }
00210             }
00211         }
00212         sutureP += incPt;
00213         Loc += incLoc; 
00214     }
00215     numOfPts = countFoundLocations; // record the number of interpolated points
00216 }
00217 //-----------------------------------------------------------------------------
00218 template <typename T, typename DATA>
00219 HEVertex<T> * OpenWound_TycoEndostitch_V2<T,DATA>::GetPuncturedLocationInsideModel ( unsigned int i )
00220 {
00221     assert( i < m_SetOfExtraVertices.size() );
00222     return m_SetOfExtraVertices[i];
00223 }
00224 //-----------------------------------------------------------------------------
00225 template <typename T, typename DATA>
00226 void OpenWound_TycoEndostitch_V2<T,DATA>::AddPointClue ( unsigned int vertexNumber )
00227 {
00228     if ( vertexNumber < static_cast<unsigned int>( m_pHEModel->GetNumVertices() ) ) {
00229         HEVertex<T> * pV = m_pHEModel->GetVertexList()->Head();
00230         for ( unsigned int i = 0; i < vertexNumber; ++i ) {
00231             pV = pV->Next();
00232         }
00233         m_SetOfPointClues.push_back( pV );
00234     }
00235 }
00236 //-----------------------------------------------------------------------------
00237 template <typename T, typename DATA>
00238 void OpenWound_TycoEndostitch_V2<T,DATA>::ClearAllPuncturedPoints ()
00239 {
00240     m_SetOfPuncturedPoints.clear();
00241     m_SetOfPairPuncturedPoints.clear();
00242 }
00243 //-----------------------------------------------------------------------------
00244 template <typename T, typename DATA>
00245 void OpenWound_TycoEndostitch_V2<T,DATA>::ClearAllPuncturedLocations ()
00246 {
00247     ClearAllExtraVertices();
00248 }
00249 //-----------------------------------------------------------------------------
00250 template <typename T, typename DATA>
00251 void OpenWound_TycoEndostitch_V2<T,DATA>::ClearAllPointClues ()
00252 {
00253     m_SetOfPointClues.clear();
00254 }
00255 //-----------------------------------------------------------------------------
00256 template <typename T, typename DATA>
00257 void OpenWound_TycoEndostitch_V2<T,DATA>::PairTwoPuncturedPoints ( unsigned int pt1, unsigned int pt2 )
00258 {
00259     assert( pt1 != pt2 && pt1 < m_SetOfPuncturedPoints.size() && pt2 < m_SetOfPuncturedPoints.size() );
00260     m_SetOfPairPuncturedPoints.push_back( PairPuncturedPoints( pt1, pt2 ) );
00261 }
00262 //-----------------------------------------------------------------------------
00263 // Simulation
00264 //=============================================================================
00265 
00266 
00267 
00268 
00269 //=============================================================================
00270 // OpenGL
00271 #if defined(__gl_h_) || defined(__GL_H__)
00272 //-----------------------------------------------------------------------------
00273 template <typename T, typename DATA>
00274 void OpenWound_TycoEndostitch_V2<T,DATA>::Draw ( OpenGL::Enum::DrawMode DM = OpenGL::Enum::POLYGON )
00275 {
00276     ModelDefBasedOnFEMHex<T,DATA>::Draw( DM );
00277 
00278     // DEBUG
00279     // Draw all puncture points
00280     //DrawAllPuncturedPoints();
00281 }
00282 //-----------------------------------------------------------------------------
00283 template <typename T, typename DATA>
00284 void OpenWound_TycoEndostitch_V2<T,DATA>::DrawAllPuncturedPoints ()
00285 {
00286     glPushAttrib( GL_ALL_ATTRIB_BITS );
00287         //glPushAttrib( GL_ENABLE_BIT | GL_CURRENT_BIT | GL_POINT_BIT );
00288         glEnable( GL_BLEND );
00289         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00290         TransformationSupport<T> & trx = RefToTransformationSupport();
00291         for ( unsigned int i = 0; i < m_SetOfPuncturedPoints.size(); ++i ) {
00292             Vector3<T> pos = m_SetOfPuncturedPoints[i]->GetPosition();
00293             pos = trx.GetMatrixTransform() * pos;
00294             glPushMatrix();
00295                 glTranslatef( pos[0], pos[1], pos[2] );
00296                 //m_HelpDrawingUsefulObj.SetColor( 0.5, 0.1, 0.1, 0.15 );
00297                 //m_HelpDrawingUsefulObj.DrawSphere(0.025f);
00298                 if      ( i % 4 == 0 )  m_HelpDrawingUsefulObj.SetColor( 1, 0, 0, 0.5 );
00299                 else if ( i % 4 == 1 )  m_HelpDrawingUsefulObj.SetColor( 0, 1, 0, 0.5 );
00300                 else if ( i % 4 == 2 )  m_HelpDrawingUsefulObj.SetColor( 0, 0, 1, 0.5 );
00301                 else if ( i % 4 == 3 )  m_HelpDrawingUsefulObj.SetColor( 1, 1, 0, 0.5 );
00302                 m_HelpDrawingUsefulObj.DrawSphere(0.025f);
00303             glPopMatrix();
00304         }
00305     glPopAttrib();
00306 }
00307 //-----------------------------------------------------------------------------
00308 template <typename T, typename DATA>
00309 void OpenWound_TycoEndostitch_V2<T,DATA>::DrawAllPuncturedLocations ()
00310 {
00311     glPushAttrib( GL_ALL_ATTRIB_BITS );
00312         //glPushAttrib( GL_ENABLE_BIT | GL_CURRENT_BIT | GL_POINT_BIT );
00313         glEnable( GL_BLEND );
00314         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00315         TransformationSupport<T> & trx = RefToTransformationSupport();
00316         for ( unsigned int i = 0; i < m_SetOfExtraVertices.size(); ++i ) {
00317             Vector3<T> pos = m_SetOfExtraVertices[i]->GetPosition();
00318             pos = trx.GetMatrixTransform() * pos;
00319             glPushMatrix();
00320                 glTranslatef( pos[0], pos[1], pos[2] );
00321                 m_HelpDrawingUsefulObj.SetColor( 0.5, 0.5, 0.5, 0.25 );
00322                 m_HelpDrawingUsefulObj.DrawSphere(0.05f);
00323             glPopMatrix();
00324         }
00325     glPopAttrib();
00326 }
00327 //-----------------------------------------------------------------------------
00328 template <typename T, typename DATA>
00329 void OpenWound_TycoEndostitch_V2<T,DATA>::DrawAllPuncturedPointPairs ()
00330 {
00331     TransformationSupport<T> & trx = RefToTransformationSupport();
00332     std::vector< PairPuncturedPoints >::iterator it = m_SetOfPairPuncturedPoints.begin();
00333     glPushMatrix();
00334     glPushAttrib( GL_ALL_ATTRIB_BITS );
00335         glLineWidth( 5 );
00336         glBegin( GL_LINES );
00337         while ( it != m_SetOfPairPuncturedPoints.end() ) {
00338             Vector3<T> P1 = m_SetOfPuncturedPoints[it->P1]->GetPosition();
00339             Vector3<T> P2 = m_SetOfPuncturedPoints[it->P2]->GetPosition();
00340             P1 = trx.GetMatrixTransform() * P1;
00341             P2 = trx.GetMatrixTransform() * P2;
00342             glColor3f( 0, 0, 1 );
00343             glVertex3fv( P1.GetDataFloat() );
00344             glColor3f( 1, 0, 0 );
00345             glVertex3fv( P2.GetDataFloat() );
00346             ++it;
00347         }
00348         glEnd();
00349     glPopAttrib();
00350     glPopMatrix();
00351 }
00352 //-----------------------------------------------------------------------------
00353 template <typename T, typename DATA>
00354 void OpenWound_TycoEndostitch_V2<T,DATA>::DrawAllPointClues ()
00355 {
00356     for ( unsigned int i = 0; i < m_SetOfPointClues.size(); ++i ) {
00357         DrawPointClue( i );
00358     }
00359 }
00360 //-----------------------------------------------------------------------------
00361 template <typename T, typename DATA>
00362 void OpenWound_TycoEndostitch_V2<T,DATA>::DrawPointClue ( unsigned int i )
00363 {
00364     assert( i < m_SetOfPointClues.size() );
00365     glPushAttrib( GL_ALL_ATTRIB_BITS );
00366     //glPushAttrib( GL_ENABLE_BIT | GL_CURRENT_BIT | GL_POINT_BIT );
00367     glPushMatrix();
00368         glEnable( GL_BLEND );
00369         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00370         TransformationSupport<T> & trx = RefToTransformationSupport();
00371         Vector3<T> pos = m_SetOfPointClues[i]->GetPosition();
00372         pos = trx.GetMatrixTransform() * pos;
00373         glTranslatef( pos[0], pos[1], pos[2] );
00374         m_HelpDrawingUsefulObj.SetColor( 0.5, 1.0, 0.5, 0.25 );
00375         m_HelpDrawingUsefulObj.DrawSphere(0.02f);
00376     glPopMatrix();
00377     glPopAttrib();
00378 }
00379 //-----------------------------------------------------------------------------
00380 template <typename T, typename DATA>
00381 void OpenWound_TycoEndostitch_V2<T,DATA>::DrawASphereOnVertex ( HEVertex<T> * pV )
00382 {
00383     glPushAttrib( GL_ALL_ATTRIB_BITS );
00384     //glPushAttrib( GL_ENABLE_BIT | GL_CURRENT_BIT | GL_POINT_BIT );
00385     glPushMatrix();
00386         glEnable( GL_BLEND );
00387         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00388         TransformationSupport<T> & trx = RefToTransformationSupport();
00389         Vector3<T> pos = pV->GetPosition();
00390         pos = trx.GetMatrixTransform() * pos;
00391         glTranslatef( pos[0], pos[1], pos[2] );
00392         m_HelpDrawingUsefulObj.SetColor( 0.0, 1.0, 0.0, 0.3 );
00393         m_HelpDrawingUsefulObj.DrawSphere(0.04f, 0.04f, 0.04f);
00394     glPopMatrix();
00395     glPopAttrib();
00396 }
00397 //-----------------------------------------------------------------------------
00398 #endif
00399 // OpenGL
00400 //=============================================================================
00401 
00402 
00403 
00404 
00405 //=============================================================================
00406 END_NAMESPACE_TAPs
00407 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00408 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines