TAPs 0.7.7.3
TAPsModelDeformableCPU.hpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsModelDeformableCPU.hpp
00003 ******************************************************************************/
00007 /******************************************************************************
00008 SUKITTI PUNAK   (12/21/2006)
00009 UPDATE          (08/05/2007)
00010 ******************************************************************************/
00011 #ifndef TAPs_MODEL_DEFORMABLE_CPU_HPP
00012 #define TAPs_MODEL_DEFORMABLE_CPU_HPP
00013 
00014 #include "../GLSL/TAPsGLSLFns.hpp"
00015 //#include "../GLSL/TAPsGLEXTFrameBufferObject.hpp"
00016 #include "TAPsOpenGLModel.hpp"
00017 #include "../CG/TAPsGridGenerator_ForTheDefModBasedOnMSS.hpp"
00018 
00019 //=============================================================================
00020 // DEFINES / CHOICES
00021 //=============================================================================
00022 
00023 //-----------------------------------------------------------------------------
00024 // VISUALIZATION
00025 //-----------------------------------------------------------------------------
00026 // Visualization/Rendering Selection
00027 // One of the defined statement below 
00028 //   should be declared in a file that declares and uses this class.
00029 //   E.g. in the main file
00030 #define TAPs_DEFORMABLE_MODEL_VISUALIZATION_RT_GEN_MESH // Default
00031 //#define TAPs_DEFORMABLE_MODEL_VISUALIZATION_3D_TEXTURE    // Not Finished Yet!
00032 //#define TAPs_DEFORMABLE_MODEL_VISUALIZATION_RAY_CASTING   // Not Implemented Yet!
00033 //#define TAPs_DEFORMABLE_MODEL_VISUALIZATION_MESH          // Not Implemented Yet!
00034 //-----------------------------------------------------------------------------
00035 #if defined     ( TAPs_DEFORMABLE_MODEL_VISUALIZATION_RT_GEN_MESH )
00036     #define TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00037 #elif defined   ( TAPs_DEFORMABLE_MODEL_VISUALIZATION_3D_TEXTURE )  // Not Finished Yet!
00038     #define TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_3D_TEXTURE
00039 #elif defined   ( TAPs_DEFORMABLE_MODEL_VISUALIZATION_RAY_CASTING ) // Not Implemented Yet!
00040     #define TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RAY_CASTING
00041 #elif defined   ( TAPs_DEFORMABLE_MODEL_VISUALIZATION_MESH )        // Not Implemented Yet!
00042     #define TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_MESH
00043 #else
00044     //#define TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH   // Default
00045 #endif
00046 #if defined     ( TAPs_DEFORMABLE_MODEL_VISUALIZATION_RT_GEN_MESH )
00047     #define TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00048 #endif
00049 #if defined ( TAPs_DEFORMABLE_MODEL_VISUALIZATION_3D_TEXTURE )  // Not Finished Yet!
00050     #define TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_3D_TEXTURE
00051 #endif
00052 #if defined ( TAPs_DEFORMABLE_MODEL_VISUALIZATION_RAY_CASTING ) // Not Implemented Yet!
00053     #define TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RAY_CASTING
00054 #endif
00055 #if defined ( TAPs_DEFORMABLE_MODEL_VISUALIZATION_MESH )        // Not Implemented Yet!
00056     #define TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_MESH
00057 #endif
00058 //-----------------------------------------------------------------------------
00059 // For Helping Coding
00060 //-----------------------------------------------------------------------------
00061 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00062     //...
00063 #endif
00064 //-------------------------------------------------------------------
00065 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_3D_TEXTURE   // NOT FINISHED YET!
00066     //...
00067 #endif
00068 //-------------------------------------------------------------------
00069 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RAY_CASTING  // NOT IMPLEMENTED YET!
00070     //...
00071 #endif
00072 //-----------------------------------------------------------------------------
00073 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_MESH         // NOT IMPLEMENTED YET!
00074     //...
00075 #endif
00076 //-----------------------------------------------------------------------------
00077 
00078 //=============================================================================
00079 // For Visualization
00080 //-----------------------------------------------------------------------------
00081 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00082     #include "TAPsModelDeformableCPU_Visualization_RTGenMesh.hpp"
00083 #endif
00084 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_3D_TEXTURE   // NOT FINISHED YET!
00085     //#include "TAPsModelDeformableCPU_Visualization_3DTexture.hpp"
00086 #endif
00087 //-----------------------------------------------------------------------------
00088 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RAY_CASTING  // NOT IMPLEMENTED YET!
00089 //  #include "TAPsModelDeformableCPU_Visualization_RayCasting.hpp"
00090 #endif
00091 //-----------------------------------------------------------------------------
00092 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_MESH         // NOT IMPLEMENTED YET!
00093 //  #include "TAPsModelDeformableCPU_Visualization_Mesh.hpp"
00094 #endif
00095 //-----------------------------------------------------------------------------
00096 //=============================================================================
00097 //-------------------------------------------------------------------
00098 // Integration Method Selection
00099 // One of the defined statement below 
00100 //   should be declared in a file that declares and uses this class.
00101 //   E.g. in the main file
00102 //#define TAPs_EXPLICIT_EULER_INTEGRATION
00103 //#define TAPs_VERLET_INTEGRATION
00104 //-------------------------------------------------------------------
00105 #ifdef      TAPs_EXPLICIT_EULER_INTEGRATION
00106     #define TAPs_MODEL_DEFORMABLE_CPU_EXPLICIT_EULER_INTEGRATION
00107 #else if    TAPs_VERLET_INTEGRATION
00108     #define TAPs_MODEL_DEFORMABLE_CPU_VERLET_INTEGRATION
00109 #endif
00110 //-------------------------------------------------------------------
00111 //-----------------------------------------------------------------------------
00112 //=============================================================================
00113 //-----------------------------------------------------------------------------
00114 // For Spring Properties (according to the type of connectivity)
00115 #include "Support/TAPsSpringPropertiesForDefModel.hpp"
00116 //=============================================================================
00117 
00118 BEGIN_NAMESPACE_TAPs__OpenGL
00119 //=============================================================================
00120 template <typename T>
00121 class ModelDeformableCPU : public /*virtual*/ OpenGLModel<T> {
00122     //-------------------------------------------------------------------------
00123     // (Friend Fn) put it through ostream
00124     friend std::ostream & operator<< ( std::ostream &output, ModelDeformableCPU<T> const &o )
00125     {
00126         output  << "\n=========================================================\n"
00127                 <<   "TAPs::OpenGL::ModelDeformableCPU<" << typeid(T).name() << "> Class:\n"
00128                 <<   "=========================================================\n";
00129         //-------------------------------------------------
00130         output  << o.Info();
00131         output  << "\n=========================================================\n";
00132         //-------------------------------------------------
00133         return output;
00134     }
00135 //-----------------------------------------------------------------------------
00136 // Member Functions -----------------------------------------------------------
00137 public:
00138     //-------------------------------------------------------------------------
00139     // default constructor
00140     ModelDeformableCPU ();
00141     //-------------------------------------------------------------------------
00142     // destructor
00143     virtual ~ModelDeformableCPU ();
00144     //-------------------------------------------------------------------------
00145     virtual void Default ();
00146     virtual void Clear ();
00147     //-------------------------------------------------------------------------
00148     virtual void Initialize () {};  // Abstract Virtual Fn from Model class
00149     virtual int InitializeFromGridGeneratorDataFloat ( 
00150         GridGenerator<T> *  pGridGenerator
00151     );
00152     //-------------------------------------------------------------------------
00153     virtual T GetMaxHalfLength () const; // Abstract Virtual Fn from Model class
00154     virtual void ApplyAndResetTransform ();
00155     //-------------------------------------------------------------------------
00156     // Calculate and set the normals
00157     virtual void CalAndSetNormals();
00158     //-------------------------------------------------------------------------
00159     // Get/Set Fn(s)
00160     //-------------------------------------------------------------------------
00161     // New/Delete Fn(s)
00162     //=========================================================================
00163     // Rendering/Display/Drawing Fn(s)
00164     //-------------------------------------------------------------------------
00165     // OpenGL Display Fn(s)
00166     // Inherited from OpenGLSupport
00167     //virtual void DisplayGL ( OpenGL::Enum::DrawMode = OpenGL::Enum::POLYGON );
00168     virtual void DrawGL ( GLenum eDrawMode );
00169     //-------------------------------------------
00170     // Internal Drawing Mode
00171     static enum DrawType {
00172         AS_2D_TEXTURE,
00173         AS_3D_TEXTURE,
00174         POSITION_TEXTURE,
00175         ALL_ELEMENTS,
00176         ALL_ELEMENTS_WITH_CONNECTIVITIES,
00177         DUMMY_DrawType
00178     };
00179     bool IsVisualizedByRTGenMesh () {
00180         #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00181             return true;
00182         #else
00183             return false;
00184         #endif
00185     }
00186     /* Not Implemented Yet!
00187     bool IsVisualizedBy3DTexture () {
00188         #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_3D_TEXTURE
00189             return true;
00190         #else
00191             return false;
00192         #endif
00193     }
00194     bool IsVisualizedByRayCasting () {
00195         #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RAY_CASTING
00196             return true;
00197         #else
00198             return false;
00199         #endif
00200     }
00201     bool IsVisualizedByMesh () {
00202         #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_MESH
00203             return true;
00204         #else
00205             return false;
00206         #endif
00207     }
00208     */
00209     virtual void DrawByGL ( DrawType drawType = POSITION_TEXTURE );
00210     //virtual void DrawByGLSL ( DrawType drawType = POSITION_TEXTURE );
00211     virtual void Visualize  ( DrawType drawType = ALL_ELEMENTS );
00212     //---------------------------------
00213 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00214     virtual inline void VisualizeByRTGenMesh        ( DrawType drawType );
00215 #endif
00216 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_3D_TEXTURE
00217     virtual inline void VisualizeBy3DTexture    ( DrawType drawType );
00218 #endif
00219 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RAY_CASTING
00220     virtual inline void VisualizeByRayCasting   ( DrawType drawType );
00221 #endif
00222 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_MESH
00223     virtual inline void VisualizeByMesh         ( DrawType drawType );
00224 #endif
00225     //---------------------------------
00226 
00227     //===============================================================
00228 public:
00229     //---------------------------------------------------------------
00230     // Check Available Visualizations
00231     bool IsVisualizedByRTGenMeshAvailable () const {
00232         #ifdef TAPs_MODEL_DEFORMABLE_GLSL_VISUALIZATION_RT_GEN_MESH
00233             if ( m_Visualization_RTGenMesh )    return true;
00234         #endif
00235         return false;
00236     }
00237 
00238     // Not Implemented Yet!
00239     //bool IsVisualizedBy3DTextureAvailable () const {
00240     //  #ifdef TAPs_MODEL_DEFORMABLE_GLSL_VISUALIZATION_3D_TEXTURE
00241     //      if ( m_Visualization_3DTexture )    return true;
00242     //  #endif
00243     //  return false;
00244     //}
00245 
00246     // Not Implemented Yet!
00247     //bool IsVisualizedByRayCastingAvailable () const {
00248     //  #ifdef TAPs_MODEL_DEFORMABLE_GLSL_VISUALIZATION_RAY_CASTING
00249     //      if ( m_Visualization_RayCasting )   return true;
00250     //  #endif
00251     //  return false;
00252     //}
00253 
00254     // Not Implemented Yet!
00255     //bool IsVisualizedByMeshAvailable () const {
00256     //  #ifdef TAPs_MODEL_DEFORMABLE_GLSL_VISUALIZATION_MESH
00257     //      if ( m_Visualization_Mesh ) return true;
00258     //  #endif
00259     //  return false;
00260     //}
00261     //---------------------------------------------------------------
00262     //===============================================================
00263     // Get/Set Fns for Visualization by Real-Time Generate Mesh
00264     //---------------------------------------------------------------
00265     bool    GetRTGenMesh_MCUseGlobalInterpolationValue () const {
00266         #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00267             if ( m_Visualization_RTGenMesh ) {
00268                 return m_Visualization_RTGenMesh->GetUseGlobalInterpolationValue();
00269             }
00270         #endif
00271         return false;
00272     }
00273     void    SetRTGenMesh_MCUseGlobalInterpolationValue ( bool b ) {
00274         #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00275             if ( m_Visualization_RTGenMesh ) {
00276                 m_Visualization_RTGenMesh->SetUseGlobalInterpolationValue( b );
00277             }
00278         #endif
00279     }
00286     T   GetRTGenMesh_MCGlobalInterpolationValue () const {
00287         #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00288             if ( m_Visualization_RTGenMesh ) {
00289                 return m_Visualization_RTGenMesh->GetGlobalInterpolationValue();
00290             }
00291         #endif
00292         return -1;
00293     }
00299     void    SetRTGenMesh_MCGlobalInterpolationValue ( T val ) {
00300         #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00301             if ( m_Visualization_RTGenMesh ) {
00302                 m_Visualization_RTGenMesh->SetGlobalInterpolationValue( val );
00303             }
00304         #endif
00305     }
00309     bool GetRTGenMesh_StatusDrawnWithPNTriangle () const {
00310         #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00311             if ( m_Visualization_RTGenMesh ) {
00312                 return m_Visualization_RTGenMesh->GetStatusDrawnWithPNTriangle();
00313             }
00314         #endif
00315         return false;
00316     }
00320     void SetRTGenMesh_StatusDrawnWithPNTriangle ( bool b ) {
00321         #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00322             if ( m_Visualization_RTGenMesh ) {
00323                 m_Visualization_RTGenMesh->SetStatusDrawnWithPNTriangle( b );
00324             }
00325         #endif
00326     }
00330     int GetRTGenMesh_PNTriangleSmoothness () const {
00331         #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00332             if ( m_Visualization_RTGenMesh ) {
00333                 return m_Visualization_RTGenMesh->GetPNTriangleSmoothness();
00334             }
00335         #endif
00336         return -1;
00337     }
00341     void SetRTGenMesh_PNTriangleSmoothness ( int i ) {
00342         #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00343             if ( m_Visualization_RTGenMesh ) {
00344                 m_Visualization_RTGenMesh->SetPNTriangleSmoothness( i );
00345             }
00346         #endif
00347     }
00351     void SetRTGenMesh_3DTexture ( GLuint i ) {
00352         #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00353             if ( m_Visualization_RTGenMesh ) {
00354                 m_Visualization_RTGenMesh->Set3DTexture( i );
00355             }
00356         #endif
00357     }
00361     GLuint GetRTGenMesh_3DTexture () {
00362         #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00363             if ( m_Visualization_RTGenMesh ) {
00364                 return m_Visualization_RTGenMesh->Get3DTexture();
00365             }
00366         #else
00367             return 0;
00368         #endif
00369     }
00370     //---------------------------------------------------------------
00371     //===============================================================
00372 
00373     //---------------------------------
00374 protected:
00375     virtual void DrawByGL_As2DTexture ();
00376     virtual void DrawByGL_As3DTexture ();
00377     virtual void DrawByGL_PositionTexture ();
00378     virtual void DrawByGL_AllElements ();
00379     virtual void DrawByGL_AllElementsWithConnectivities ();
00380     //-------------------------------------------------------------------------
00381     //=========================================================================
00382     //===============================================================
00383     // Data Members for Visualization
00384     //---------------------------------------------------------------
00385 //
00386 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RT_GEN_MESH
00387 protected:
00388     ModelDeformableCPU_Visualization_RTGenMesh<T> * m_Visualization_RTGenMesh;
00389 public:
00390     ModelDeformableCPU_Visualization_RTGenMesh<T> * const RetVisualRTGenMesh ()
00391     {   return m_Visualization_RTGenMesh;   }
00392 protected:
00393 #endif
00394 //
00395 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_3D_TEXTURE
00396     ModelDeformableCPU_Visualization_3DTexture<T> * m_Visualization_3DTexture;
00397 public:
00398     ModelDeformableCPU_Visualization_3DTexture<T> * const RetVisual3DTexture ()
00399     {   return m_Visualization_3DTexture;   }
00400 protected:
00401 #endif
00402 //
00403 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_RAY_CASTING
00404     ModelDeformableCPU_Visualization_RayCasting<T> * m_Visualization_RayCasting;
00405 public:
00406     ModelDeformableCPU_Visualization_RayCasting<T> * const RetVisualRayCasting ()
00407     {   return m_Visualization_RayCasting;  }
00408 protected:
00409 #endif
00410 //
00411 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VISUALIZATION_MESH
00412     ModelDeformableCPU_Visualization_Mesh<T> * m_Visualization_Mesh;
00413 public:
00414     ModelDeformableCPU_Visualization_Mesh<T> * const RetVisualMesh ()
00415     {   return m_Visualization_Mesh;    }
00416 protected:
00417 #endif
00418 //
00419     //---------------------------------------------------------------
00420     // Helper Fn(s)
00421     void DefaultVisualization ();
00422     void ClearVisualization   ();
00423     int SetupVisualization   ( 
00424         enum GridGenerator<T>::VertexFlag *** pFlagDataForXYZVoxels
00425     );
00426     //---------------------------------------------------------------
00427     //===============================================================
00428     //---------------------------------------------------------------
00429 
00430     //=========================================================================
00431     // Simulation/Animation Fn(s)
00432 public:
00433     //-------------------------------------------------------------------------
00434     // Get/Set Fn(s)
00435     Vector3<int> const & RetGridResolution () const { return m_vGridResolution; }
00436     Vector3<T> const &   RetGridDimension  () const { return m_vGridDimension; }
00437     Vector3<int>         GetGridResolution () const { return m_vGridResolution; }
00438     Vector3<T>           GetGridDimension  () const { return m_vGridDimension; }
00439     int GetTotalGridElements () const { return m_iTotalElements; }
00440     T   GetGridElementVolume () const { return m_fElementVolume; }
00441     //-------------------------------------------------------------------------
00442     T * const ReturnPositionPointer () {
00443         return m_tpElementPos;
00444     }
00445 //  Vector3<T> GetPositionOfElementNo ( int x, int y, int z ) const {
00446 //      return m_tpElementPos[ TranslateElementNumber(x,y,z) ];
00447 //  }
00448 //  void SetPositionOfElementNo ( int x, int y, int z, Vector3<T> & position ) {
00449 //      m_tpElementPos[ TranslateElementNumber(x,y,z) ] = position;
00450 //  }
00451 //  void AddToPositionOfElementNo ( int x, int y, int z, Vector3<T> & position ) {
00452 //      m_tpElementPos[ TranslateElementNumber(x,y,z) ] += position;
00453 //  }
00454     void GetTheClosestElementToThePosition ( Vector3<T> const & position, 
00455             int & x, int & y, int & z, T closestDistance ) {
00456         int iElementNo = 0;
00457         T   tDistance;
00458         closestDistance = (m_tpElementPos[0] - position).Length();
00459         Vector3<int> closetPt( 0, 0, 0 );
00460         for ( int k = 0; k < m_vGridResolution[2]; ++k ) {
00461             for ( int j = 0; j < m_vGridResolution[1]; ++j ) {
00462                 for ( int i = 0; i < m_vGridResolution[0]; ++i ) {
00463                     tDistance = (m_tpElementPos[ iElementNo ] - position).Length();
00464                     if ( closestDistance > tDistance ) {
00465                         closestDistance = tDistance;
00466                         x = i;
00467                         y = j;
00468                         z = k;
00469                     }
00470                     ++iElementNo;
00471                 }
00472             }
00473         }
00474     }
00475     //-------------------------------------------------------------------------
00476     //=========================================================================
00477 
00478 //-----------------------------------------------------------------------------
00479 protected:
00480     //=========================================================================
00481     // Helper Fn(s):    Creations/Initializations
00482     //-------------------------------------------------------------------------
00483     int TranslateElementNumber ( int x, int y, int z ) {
00484         assert( 0 <= x && x < m_vGridResolution[0] );
00485         assert( 0 <= y && y < m_vGridResolution[1] );
00486         assert( 0 <= z && z < m_vGridResolution[2] );
00487         return  (z * m_vGridResolution[1] + y) * m_vGridResolution[0] + x;
00488     }
00489     //-------------------------------------------------------------------------
00490     void CreateSurfaceBoundary ();
00491     void CreateConnections ();
00492     void CreateConnectionsFullOneRing ();
00493     void CreateConnectionsOfOneElementNotOnBoundary ( 
00494         int z, int y, int x, int * connectionPattern, int connectionSize );
00495     void CreateConnectionsOfOneElementOnBoundary ( 
00496         int z, int y, int x, int * connectionPattern, int connectionSize );
00497     //-------------------------------------------------------------------------
00498     //=========================================================================
00499 
00500     //=========================================================================
00501     // Helper Fn(s):    Simulation/Animation
00502     //-------------------------------------------------------------------------
00503     //---------------------------------------------------------------
00507     void VoidAnElement ( int iElementID );
00508     //---------------------------------------------------------------
00512     void RemoveElementConnection ( int iElementID, int iConnection );
00513     //---------------------------------------------------------------
00518     void CalElementForceTotal ( int iElementID, T Vforce[3] );
00522     void CalElementForceInternal ( int iElementID, T Vforce[3] );
00526     void CalElementForceExternal ( int iElementID, T Vforce[3] );
00527     //-------------------------------------------
00528 
00529     //===========================================
00530     // EXPLICIT EULER INTEGRATION -- EXPLICIT EULER INTEGRATION
00531     //-------------------------------------------
00532 #ifdef TAPs_MODEL_DEFORMABLE_CPU_EXPLICIT_EULER_INTEGRATION
00533 
00537     void SwapCurrentStatesWithNextStatesForExplicitEulerIntegration ()
00538     {
00539         T * tpTemp          = m_tpElementPos;
00540         m_tpElementPos      = m_tpElementPosNext;
00541         m_tpElementPosNext  = tpTemp;
00542         //-----------------------------
00543         tpTemp              = m_tpElementVel;
00544         m_tpElementVel      = m_tpElementVelNext;
00545         m_tpElementVelNext  = tpTemp;
00546     }
00547     //-------------------------------------------
00553     void AddDampedSpringForceForExplicitEulerIntegration ( 
00554         T VfromPointPos[3],     // I/P: start point position
00555         T VtoPointPos[3],       // I/P: end   point position
00556         T VfromPointVel[3],     // I/P: start point velocity
00557         T VtoPointVel[3],       // I/P: end   point velocity
00558         T restLength,           // I/P: spring rest length
00559         T Ks,                   // I/P: spring stiffness
00560         T Kd,                   // I/P: spring damping constant
00561         T Vforce[3]             // O/P: force vector
00562     )
00563     {
00564         T L[3] = {      // Distance Vector
00565             VtoPointPos[0] - VfromPointPos[0], 
00566             VtoPointPos[1] - VfromPointPos[1], 
00567             VtoPointPos[2] - VfromPointPos[2]
00568         };
00569         T V[3] = {      // deformed velocity
00570             VtoPointVel[0] - VfromPointVel[0], 
00571             VtoPointVel[1] - VfromPointVel[1], 
00572             VtoPointVel[2] - VfromPointVel[2]
00573         };
00574         //-------------------------------------------------
00575         T currentLength = sqrt( L[0]*L[0] + L[1]*L[1] + L[2]*L[2] );
00576         T force = Ks * ( currentLength - restLength ) 
00577                 + Kd * sqrt( V[0]*L[0] + V[1]*L[1] + V[2]*L[2] );
00578         //-------------------------------------------------
00579         Vforce[0] = L[0] * force;
00580         Vforce[1] = L[1] * force;
00581         Vforce[2] = L[2] * force;
00582     }
00583     //-------------------------------------------
00587     void CalElementNextPositionByExplicitEulerIntegration ( int iElementID )
00588     {
00589     }
00590     //-------------------------------------------
00594     void SimStepByExplicitEulerIntegration ( T dt );
00595     //-------------------------------------------
00596     //===========================================
00597 #endif
00598     //===========================================
00599     // VERLET INTEGRATION -- VERLET INTEGRATION
00600     //-------------------------------------------
00601 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VERLET_INTEGRATION
00602 
00606     void SwapCurrentStatesWithNextStatesForVerletIntegration ()
00607     {
00608         // Swap
00609         //std::cout << " ------> Swap Elements\n";
00610         T * tpTemp          = m_tpElementPosPrev;
00611         m_tpElementPosPrev  = m_tpElementPos;
00612         m_tpElementPos      = m_tpElementPosNext;
00613         m_tpElementPosNext  = tpTemp;
00614 
00615         /*
00616         // Copy
00617         std::cout << "Total Element: " << m_iTotalElements << "\n";
00618         for ( int i = 0; i < m_iTotalElements * 4; ++i ) {
00619         //  m_tpElementPosPrev[i] = m_tpElementPos[i];
00620             m_tpElementPos[i]     = m_tpElementPosNext[i];
00621         }
00622         //*/
00623     }
00624     //-------------------------------------------
00630     void AddUndampedSpringForceForVerletIntegration ( 
00631         T VfromPointPos[3],     // I/P: start point position
00632         T VtoPointPos[3],       // I/P: end   point position
00633         T restLength,           // I/P: spring rest length
00634         T Ks,                   // I/P: spring stiffness
00635         T Vforce[3]             // O/P: force vector
00636     )
00637     {
00638         //-------------------------------------------------
00639         // Distance Vector
00640         T L[3] = {
00641             VtoPointPos[0] - VfromPointPos[0], 
00642             VtoPointPos[1] - VfromPointPos[1], 
00643             VtoPointPos[2] - VfromPointPos[2]
00644         };
00645         T currentLength = sqrt( L[0]*L[0] + L[1]*L[1] + L[2]*L[2] );
00646         //-------------------------------------------------
00647         //assert( fabs( currentLength ) > 1E-16 );
00648         //-------------------------------------------------
00649         T differentLength = currentLength - restLength;
00650         //-------------------------------------------------
00651         // EQUATION FOR FORCE CALCULATION
00652         //--------------------------------
00653         // Vforce = + ( [spring stiffness] * [diff length] / [unit distance vector] )
00654         // Vforce has positive sign because the current length is calculated 
00655         //   from end position vector - start position vector.
00656         //   Vforce is the force with direction of (end position vector - start position vector).
00657         //   So if the different length (= current length - rest length) is 
00658         //   greater than (or equal to) zero then the force has to be positive,
00659         //   or negative otherwise, to affect the start position to restore 
00660         //   the spring to return to the rest length.
00661         //   In other word, the force is positive if the current length is 
00662         //   greather than the rest length (spring elongation) and the force 
00663         //   is negative if the currenet length is less than the rest length 
00664         //   (spring contraction).
00665         //   The key to determine the sign of force is the direction of the force.
00666         //   In most of literature, force direction is the start point minus the end point.
00667         //   For our, the force direction is the end point minus the start point.
00668         //-------------------------------------------------
00669         // Calculate Force Vector
00670         //T activateLength = 0.001; // 1/1000 of the rest length
00671         // If different length is less than activate length, 
00672         //   then treat this force as zero.
00673         //if ( fabs( differentLength ) > 0.0001 ) {
00674         if ( fabs( differentLength ) > 0 ) {
00675             T force = Ks * differentLength / currentLength;
00676             Vforce[0] += L[0] * force;
00677             Vforce[1] += L[1] * force;
00678             Vforce[2] += L[2] * force;
00679         }
00680 
00681         /*
00682         // DEBUG
00683         std::cout << "currentLength: " << currentLength << "\n";
00684         std::cout << "differentLength: " << differentLength << "\n";
00685         std::cout << "L: " << L[0] << " " << L[1] << " " << L[2] << "\n";
00686         std::cout << "Vforce: " << Vforce[0] << " " << Vforce[1] << " " << Vforce[2] << "\n\n";
00687         //*/
00688 
00689 
00690     }
00691     //-------------------------------------------
00699     void CalElementNextPositionByVerletIntegration ( 
00700         int iElementID, T dt, T dtdt_over_mass, T tScaleForDampedForce )
00701     {
00702         // T dtdt_over_mass = dt * dt / m_tMass;
00703         T Ftotal[3] = { 0, 0, 0 };
00704         //T Ftotal[3];
00705         //-------------------------------------------------
00706         CalElementForceTotal( iElementID, Ftotal );
00707 //      std::cout << "Ftotal: " << Ftotal[0] << " " << Ftotal[1] << " " << Ftotal[2] << "\n";   // DEBUG
00708         //-------------------------------------------------
00709         //T tScaleForDampedForce = SPRING_PROP.Kdamping / dt;
00710 
00711         int idx = iElementID * 4;
00712 
00713         //*
00714         m_tpElementPosNext[idx] = Ftotal[0] * dtdt_over_mass + 2*m_tpElementPos[idx] - m_tpElementPosPrev[idx]
00715                                 - tScaleForDampedForce * ( m_tpElementPos[idx] - m_tpElementPosPrev[idx] );
00716         ++idx;
00717         m_tpElementPosNext[idx] = Ftotal[1] * dtdt_over_mass + 2*m_tpElementPos[idx] - m_tpElementPosPrev[idx]
00718                                 - tScaleForDampedForce * ( m_tpElementPos[idx] - m_tpElementPosPrev[idx] );
00719         ++idx;
00720         m_tpElementPosNext[idx] = Ftotal[2] * dtdt_over_mass + 2*m_tpElementPos[idx] - m_tpElementPosPrev[idx]
00721                                 - tScaleForDampedForce * ( m_tpElementPos[idx] - m_tpElementPosPrev[idx] );
00722                               
00723         ++idx;
00724         m_tpElementPosNext[idx] = m_tpElementPos[idx];
00725         //*/
00726 
00727 
00728         /*
00729         m_tpElementPosNext[idx] = Ftotal[0] * dtdt_over_mass + 2*m_tpElementPos[idx] - m_tpElementPosPrev[idx];
00730         ++idx;
00731         m_tpElementPosNext[idx] = Ftotal[1] * dtdt_over_mass + 2*m_tpElementPos[idx] - m_tpElementPosPrev[idx];
00732         ++idx;
00733         m_tpElementPosNext[idx] = Ftotal[2] * dtdt_over_mass + 2*m_tpElementPos[idx] - m_tpElementPosPrev[idx];
00734         ++idx;
00735         m_tpElementPosNext[idx] = m_tpElementPos[idx];
00736         //*/
00737 
00738         /*
00739         m_tpElementPosNext[idx] = m_tpElementPos[idx];
00740         ++idx;
00741         m_tpElementPosNext[idx] = m_tpElementPos[idx];
00742         ++idx;
00743         m_tpElementPosNext[idx] = m_tpElementPos[idx];
00744         ++idx;
00745         m_tpElementPosNext[idx] = m_tpElementPos[idx];
00746         //*/
00747 
00748         /*
00749         // DEBUG
00750         std::cout << "dtdt_over_mass: " << dtdt_over_mass << "\n";
00751         std::cout << "PositionPrev: " 
00752             << m_tpElementPosPrev[idx-3] << " "
00753             << m_tpElementPosPrev[idx-2] << " "
00754             << m_tpElementPosPrev[idx-1] << "\n";
00755         std::cout << "PositionNext: " 
00756             << m_tpElementPosNext[idx-3] << " "
00757             << m_tpElementPosNext[idx-2] << " "
00758             << m_tpElementPosNext[idx-1] << "\n";
00759         std::cout << "Position: " 
00760             << m_tpElementPos[idx-3] << " "
00761             << m_tpElementPos[idx-2] << " "
00762             << m_tpElementPos[idx-1] << "\n";
00763         std::cout << "Position Diff: " 
00764             << m_tpElementPosNext[idx-3] - m_tpElementPos[idx-3] << " "
00765             << m_tpElementPosNext[idx-2] - m_tpElementPos[idx-2] << " "
00766             << m_tpElementPosNext[idx-1] - m_tpElementPos[idx-1] << "\n";
00767         //*/
00768     }
00769     //-------------------------------------------
00773     void SimStepByVerletIntegration ( T dt );
00774     //-------------------------------------------
00775     //===========================================
00776 #endif
00777     //-------------------------------------------------------------------------
00778     //=========================================================================
00779 
00780     //-------------------------------------------------------------------------
00781 //  virtual inline void CalAndSetFaceNormalsNotNormalized ();
00782 //  virtual inline void NormalizeFaceNormals ();
00783 //  virtual inline void DetermineFaceRings ();
00784 //  virtual inline void CalAndSetElementNormals ();
00785     //-------------------------------------------------------------------------
00786     // Abstract Virtual Fns for Collision Detection from ColDetSupport class
00787     virtual void CalBoundingAABB ();
00788     virtual void CalBoundingEllipsoid ();
00789     virtual void CalBoundingSphere ();
00790 //-----------------------------------------------------------------------------
00791 // Data Members  --------------------------------------------------------------
00792 protected:
00793     //=========================================================================
00794     // GPU
00795     //-------------------------------------------------------------------------
00796     // For OpenGL Shading Language
00797     //GLSLShaderManager     m_glslShaderManager;
00798     //GLSLProgramObject *       m_glslProgramObject;
00799     //-------------------------------------------------------------------------
00800     //=========================================================================
00801     //-------------------------------------------------------------------------
00802     // Grid Sizes/Resolutions and Grid Dimensions
00803     Vector3< int >  m_vGridResolution;  // width, height, and depth numbers
00804                                                 // e.g. 64 x 64 x 64
00805     Vector3< T >    m_vGridDimension;   // width, height, and depth lengths
00806                                                 // e.g. 0.1 x 0.1 x 0.1
00807     int         m_iTotalElements;   // multiplication of the grid resolutions
00808     T           m_fElementVolume;   // multiplication of the grid dimensions
00809     //-------------------------------------------------------------------------
00810     //=========================================================================
00811     //-------------------------------------------------------------------------
00812     // For Helping Drawing
00813     OpenGLUsefulObj< T >    m_OGLUsefulObj; // for drawing useful objects
00814     //-------------------------------------------------------------------------
00815     //=========================================================================
00816     // CPU
00817     //-------------------------------------------------------------------------
00833     //-------------------------------------------
00834     // Element Status and Connectivity
00838     //T *       m_tpElementStatus;      // vertex status (scalar)
00853     //-------------------------------------------------------------------------
00858 protected:
00859     //-------------------------------------------------------------------------
00864     int     CONNECTION_INDEX[27];
00865     //-------------------------------------------------------------------------
00869     void InitConnectionConstants ();
00870     //---------------------------------
00871     int *   m_ipElementConnect;     // vertex connectivities for each vertex
00872             // arranged sequentially, 
00873             // i.e. = new int[m_iTotalElements * m_iElementMaxValence];
00874     int     m_iElementMaxValence;   // vertex max valence (up to 26)
00875     //---------------------------------
00876     // Previous
00877 #ifdef TAPs_MODEL_DEFORMABLE_CPU_VERLET_INTEGRATION
00878     T *     m_tpElementPosPrev;     // vertex positions (x,y,z) + a flag
00879 #endif
00880     //---------------------------------
00881     // Current
00882     T *     m_tpElementPos;         // vertex positions (x,y,z) + a flag
00883 #ifdef TAPs_MODEL_DEFORMABLE_CPU_EXPLICIT_EULER_INTEGRATION
00884     T *     m_tpElementVel;         // vertex velocities (u,v,w)
00885 #endif
00886     //---------------------------------
00887     // Next
00888     T *     m_tpElementPosNext;     // vertex positions (x,y,z) + a flag
00889 #ifdef TAPs_MODEL_DEFORMABLE_CPU_EXPLICIT_EULER_INTEGRATION
00890     T *     m_tpElementVelNext;     // vertex velocities (u,v,w)
00891 #endif
00892     //---------------------------------
00893     // Surface Boundary
00894     int *   m_ipSurfaceBoundary;    // surface boundary (by indices)
00895     //-------------------------------------------------------------------------
00896     //=========================================================================
00897     // For Simulation
00898     //-------------------------------------------------------------------------
00899     T   m_tMass;    // the mass of an element
00900     //-------------------------------------------
00905 public:
00906     typedef SpringPropertiesForDefModel<T> SpringProp;
00907     SpringProp  SPRING_PROP;
00908     T   GetSpringRestLength ( enum SpringProp::CONNECT springSelection )
00909         {   return SPRING_PROP.REST_LENGTH[ springSelection ];  }
00910     T   GetSpringStiffness ( enum SpringProp::CONNECT springSelection )
00911         {   return SPRING_PROP.STIFFNESS[ springSelection ];    }
00912     void    SetSpringRestLength ( enum SpringProp::CONNECT springSelection, T val )
00913             {   SPRING_PROP.REST_LENGTH[ springSelection ] = val;   }
00914     void    SetSpringStiffness ( enum SpringProp::CONNECT springSelection, T val )
00915             {   SPRING_PROP.STIFFNESS[ springSelection ] = val; }
00916 #ifdef TAPs_MODEL_DEFORMABLE_CPU_EXPLICIT_EULER_INTEGRATION
00917     T   GetSpringDamper ( enum SpringProp::CONNECT springSelection )
00918         {   return SPRING_PROP.DAMPER[ springSelection ];   }
00919     void    SetSpringDamper ( enum SpringProp::CONNECT springSelection, T val )
00920             {   SPRING_PROP.DAMPER[ springSelection ] = val;    }
00921 #else // #ifdef TAPs_MODEL_DEFORMABLE_CPU_EXPLICIT_EULER_INTEGRATION
00922 #endif // #ifdef TAPs_MODEL_DEFORMABLE_CPU_EXPLICIT_EULER_INTEGRATION
00923 
00924     //-------------------------------------------------------------------------
00925     // Helper Fns
00926 public:
00927     virtual std::string Info () const;
00928     //-------------------------------------------------------------------------
00929     virtual void InfoConnectionProperties ( std::string & addToStr,
00930         enum SpringProp::CONNECT connect, int maxNumConnections = 26 ) const {
00931         char cStr[80];
00932         if ( connect <= maxNumConnections ) {
00933             #ifdef TAPs_MODEL_DEFORMABLE_CPU_EXPLICIT_EULER_INTEGRATION
00934             sprintf( cStr, " %g, %g, %g", 
00935                 SPRING_PROP.REST_LENGTH[ connect ], 
00936                 SPRING_PROP.STIFFNESS[ connect ], 
00937                 SPRING_PROP.DAMPER[ connect ] 
00938             );
00939             #else // #ifdef TAPs_MODEL_DEFORMABLE_CPU_EXPLICIT_EULER_INTEGRATION
00940             sprintf( cStr, " %g, %g", 
00941                 SPRING_PROP.REST_LENGTH[ connect ], 
00942                 SPRING_PROP.STIFFNESS[ connect ] 
00943             );
00944             #endif // #ifdef TAPs_MODEL_DEFORMABLE_CPU_EXPLICIT_EULER_INTEGRATION
00945             addToStr += cStr;
00946         }
00947     }
00948     //-------------------------------------------------------------------------
00949     //=========================================================================
00950     //-------------------------------------------------------------------------
00951 }; // CLASS END: ModelDeformableCPU *******************************************
00952 //=============================================================================
00953 END_NAMESPACE_TAPs__OpenGL
00954 //-----------------------------------------------------------------------------
00955 // Include definition if TAPs_USE_EXPORT is not defined
00956 //#if !defined( TAPs_USE_EXPORT )
00957     #include "TAPsModelDeformableCPU.cpp"
00958 //#endif
00959 //-----------------------------------------------------------------------------
00960 #endif
00961 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00962 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines