![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsHETriMeshOneModelMultiParts.hpp 00003 00004 SUKITTI PUNAK (07/25/2008) 00005 UPDATE (10/07/2009) 00006 ******************************************************************************/ 00007 #ifndef TAPs_HE_TRI_MESH_ONE_MODEL_MULTI_PARTS 00008 #define TAPs_HE_TRI_MESH_ONE_MODEL_MULTI_PARTS 00009 00010 #include "TAPsHalfEdgeModel.hpp" 00011 //#include "../Physics/TAPsParticleRef.hpp" 00012 #include "../Physics/TAPsSpringRef.hpp" 00013 00014 #include "Support/TAPsHEvertexPtrWithExtraInfo.hpp" 00015 00016 #include "Support/TAPsSimPropForMultiPartMeshModel_HalfEdge.hpp" 00017 00018 #include "../Simulation/TAPsListOfODESolvers.hpp" 00019 00020 //#ifdef TAPs_ADVANCED_SIMULATION 00021 // #include "../Support/TAPsAdvSimSupport.hpp" 00022 //#endif//TAPs_ADVANCED_SIMULATION 00023 00024 // For GUIs by wxWidgets 00025 #ifdef TAPs_USE_WXWIDGETS 00026 #include "../wxWidgets/Dialogs/TAPsWXHETriMeshOneModelMultiPartsParameters.hpp" 00027 #endif//TAPs_USE_WXWIDGETS 00028 00046 /*============================================================================= 00047 Half-Edge Data Structure: 00048 ========================= 00049 HEVertex class is a class for 3D half-edge vertex. 00050 - position 00051 - normal 00052 - incident half-edge 00053 HEFace class is a class for 3D half-edge Face. 00054 - normal 00055 - vertex loop (in CCW order) 00056 - texture coordinates for the vertex loop 00057 - incident half-edge 00058 HEHalfEdge class is a class for 3D half-edge. 00059 - originating vertex 00060 - incident face 00061 - pair of this half-edge 00062 - next half-edge (CCW) 00063 - previous half-edge (CW) 00064 * 00065 * 00066 E2 * 00067 V3 <--------------------- V2 * 00068 \ /\ * 00069 \ / * 00070 E3 \ F0 E1 / * 00071 \ / * 00072 \ / * 00073 _\/ E0 / * 00074 V0 --------> V1 * 00075 * 00076 * 00077 | * 00078 \_ | _/ * 00079 \_ | _/ * 00080 \ | / * 00081 -------- V -------- * 00082 _/ | \_ * 00083 _/ | \_ * 00084 / | \ * 00085 | * 00086 * 00087 * 00088 =============================================================================*/ 00089 00090 00091 BEGIN_NAMESPACE_TAPs__OpenGL 00092 //============================================================================= 00093 template <typename T> 00094 class HETriMeshOneModelMultiParts : public /*virtual*/ HalfEdgeModel<T> { 00095 //------------------------------------------------------------------------- 00096 // (Friend Fn) put it through ostream 00097 friend std::ostream & operator<< ( std::ostream &output, HETriMeshOneModelMultiParts<T> const &o ) 00098 { 00099 // NOT FINISHED YET!!! 00100 00101 output << "\n======================\n" 00102 << "TAPs::HETriMeshOneModelMultiParts<" 00103 << typeid(T).name() << "> Class:\n" 00104 << "======================\n"; 00105 //----------------------------------------------------------- 00106 output << "Name: " << o.GetName() << "\n"; 00107 // Vertex Nodes 00108 output << "\n\nVertices " << o.m_listVertex->Size(); 00109 /* 00110 int i; 00111 i = 0; 00112 HEVertex<T> *vertex = o.m_listVertex->Head(); 00113 output << "\n{"; 00114 while ( vertex ) { 00115 output << "\n #" << ++i << "\t" << *vertex; 00116 vertex = vertex->Next(); 00117 } 00118 output << "\n}"; 00119 //*/ 00120 //----------------------------------------------------------- 00121 // Face Nodes 00122 output << "\n\nFaces " << o.m_listFace->Size(); 00123 /* 00124 i = 0; 00125 HEFace<T> *face = o.m_listFace->Head(); 00126 output << "\n{"; 00127 while ( face ) { 00128 output << "\n #" << ++i << "\t" << *face; 00129 face = face->Next(); 00130 } 00131 output << "\n}"; 00132 //*/ 00133 //----------------------------------------------------------- 00134 // Hole Face Nodes 00135 output << "\n\nHole Faces " << o.m_listHoleFace->Size(); 00136 /* 00137 i = 0; 00138 face = o.m_listHoleFace->Head(); 00139 output << "\n{"; 00140 while ( face ) { 00141 output << "\n #" << ++i << "\t" << *face; 00142 face = face->Next(); 00143 } 00144 output << "\n}"; 00145 //*/ 00146 //----------------------------------------------------------- 00147 return output; 00148 } 00149 00150 //============================================================================= 00151 public: 00152 // Member Functions ------------------------------------------------------- 00153 00155 HETriMeshOneModelMultiParts (); 00156 00158 virtual ~HETriMeshOneModelMultiParts (); 00159 00161 void CreateMeshSprings ( 00162 T mass, 00163 T Ks, 00164 T Kd, 00165 T HKs, 00166 T HKd, 00167 int rigidity = 1, 00168 T predefinedTimeStep = 0.01, 00169 int numSimSubSteps = 10, 00170 bool enableColDet = true, 00171 bool enableSimulation = true 00172 ); 00173 00175 virtual void Initialize (); 00176 00178 int GetRigidity () const { return m_iRigidity; } 00180 T GetKPointMass () const { return m_tPtMass; } 00182 T GetKStiffness () const { return m_tKStiffness; } 00184 T GetKDamper () const { return m_tKDamper; } 00186 T GetHomeKStiffness () const { return m_tHomeKStiffness; } 00188 T GetHomeKDamper () const { return m_tHomeKDamper; } 00189 00190 void SetKPointMass ( T val ) { m_tPtMass = val; } 00191 void SetKStiffness ( T val ) { m_tKStiffness = val; } 00192 void SetKDamper ( T val ) { m_tKDamper = val; } 00193 void SetHomeKStiffness ( T val ) { m_tHomeKStiffness = val; } 00194 void SetHomeKDamper ( T val ) { m_tHomeKDamper = val; } 00195 00197 std::vector< SimPropForMultiPartMeshModel_HalfEdge<T> > & GetListOfParts () 00198 { return m_Parts; } 00199 00201 SimPropForMultiPartMeshModel_HalfEdge<T> * GetPtrToPartNo ( unsigned int i ) 00202 { 00203 if ( i < static_cast<unsigned int>( m_Parts.size() ) ) return &m_Parts[i]; 00204 else return NULL; 00205 } 00206 00217 virtual void ApplyAndResetTransform (); 00218 00219 //=============================================================== 00220 // For simulation 00221 //--------------------------------------------------------------- 00223 virtual void AdvanceSimulation ( T tCurrent, T tNext ); 00225 virtual void AdvanceSimulation ( T tCurrent ); 00227 virtual void AdvanceSimulation ( Simulation::SimClock<T> & simClock ); 00228 00230 T GetPredefinedTimeStep () const { return m_tPredefinedTimeStep; } 00232 void SetPredefinedTimeStep ( T ts ) { m_tPredefinedTimeStep = ts; } 00233 00235 int GetNumSimSubSteps () const { return m_iNumSimSubSteps; } 00237 void SetNumSimSubSteps ( int i ) { m_iNumSimSubSteps = i; } 00238 //--------------------------------------------------------------- 00239 00240 // Data Members ----------------------------------------------------------- 00241 //============================================================================= 00242 00243 protected: 00244 00245 // CUDA --------------------------------------------------------- 00246 #ifdef TAPs_USE_CUDA 00247 00248 bool CUDA_Initialize_All (); 00249 bool CUDA_Initialize_VertexList (); 00250 bool CUDA_Initialize_PrevVertexList (); 00251 bool CUDA_Initialize_HomeVertexList (); 00252 bool CUDA_Initialize_VertexConnectionList (); 00253 void CUDA_Cleanup_All (); 00254 void CUDA_Cleanup_VertexList (); 00255 void CUDA_Cleanup_PrevVertexList (); 00256 void CUDA_Cleanup_HomeVertexList (); 00257 void CUDA_Cleanup_VertexConnectionList (); 00258 void CUDA_CopyVertexToMem (); 00259 void CUDA_CopyMemToVertex (); 00260 void CUDA_CopyPrevVertexToMem (); 00261 void CUDA_CopyMemToPrevVertex (); 00262 void CUDA_CopyHomeVertexToMem (); 00263 void CUDA_CopyMemToHomeVertex (); 00264 void CUDA_CopyVertexConnectionToMem (); 00265 //void CUDA_CopyMemToVertexConnection (); 00266 00267 inline void CUDA_SwapBuffers (); 00268 #endif//TAPs_USE_CUDA 00269 // CUDA --------------------------------------------------------- 00270 00271 // Member Functions ------------------------------------------------------- 00272 void ChangeConstantStiffnessOfAllSprings ( T val ); 00273 void ChangeConstantDampingOfAllSprings ( T val ); 00274 void ChangeMassOfAllPoints ( T val ); 00275 00276 // Helper Fn(s) 00277 00279 void DeleteAllData (); 00280 00282 void SetListOf1RingVertices (); 00283 00285 void PrepareData (); 00286 00288 void SeparateIntoParts ( 00289 T mass, 00290 T Ks, 00291 T Kd, 00292 T HKs, 00293 T HKd, 00294 int rigidity, 00295 T predefinedTimeStep, 00296 int numSimSubSteps, 00297 bool enableColDet, 00298 bool enableSimulation 00299 ); 00300 00302 void SetMassOfParticles ( T mass ); 00303 00304 // Create springs (m_listSpringRef) 00305 std::vector<int> m_tmpForPuttingSpringRefIntoParts; 00306 bool CheckASpringExistance ( int p1, int p2 ); 00307 void CreateASpring ( T Ks, T Kd, int p1, int p2, int hops ); 00308 void CreateSprings ( T Ks, T Kd, int rigidity = 1 ); 00309 void BFSCreateSprings ( 00310 T Ks, T Kd, int rigidity, // Ks, Kd, & hop distance 00311 int p1, int p2, // particle#1 & #2 00312 std::vector<int> & particleSet, // set of connected particles 00313 bool * isMark ); // marked particles in particleSet 00314 // void DFSCreateSprings ( 00315 // T Ks, T Kd, int rigidity, // Ks, Kd, & hop distance 00316 // int p1, int p2, // particle#1 & #2 00317 // std::vector<int> & particleSet, // set of connected particles 00318 // bool * isMark ); // marked particles in particleSet 00319 00320 //=============================================================== 00321 // For simulation by ODE solver 00322 //--------------------------------------------------------------- 00323 // REMARK: DxDt has to be static so that we can pass it to 00324 // ODESolver Fn. 00325 // But we don't want StateToArray and ArrayToState be static. 00326 // Therefore, we need to pass userData, i.e. this pointer 00327 // as a void pointer. 00328 static bool DxDt ( 00329 T dt, // i/p: time step 00330 Simulation::VectorSet<T> &x, // i/p: array x = {pos, vel} 00331 Simulation::VectorSet<T> &xdot, // o/p: array xdot = {vel, accel} 00332 void *userData // o/p: array of user data 00333 ); 00334 //--------------------------------------------------------------- 00335 void StateToArray ( T *dst ); // copy states to dst array 00336 void ArrayToState ( T *src ); // copy src array to states 00337 void DdtStateToArray ( T *xdot, T dt ); // copy d/dt states to dst (xdot) array 00338 void ClearAllForces (); // clear (all) forces (and maybe velocities too) 00339 00340 // Data Members ----------------------------------------------------------- 00341 00342 //--------------------------------------------------------------- 00343 TAPs::Simulation::ODESolver<T> * m_prODESolver; 00344 Simulation::VectorSet<T> * x0; // pointer to x0 Data 00345 Simulation::VectorSet<T> * xEnd; // pointer to xEnd Data 00346 Simulation::VectorSet<T> xData_1; // #m_iStateSize * 6 00347 Simulation::VectorSet<T> xData_2; // #m_iStateSize * 6 00348 int m_iStateSize; 00349 int m_iLimitStepCount; 00350 00351 T m_tPredefinedTimeStep; 00352 int m_iNumSimSubSteps; 00353 //--------------------------------------------------------------- 00354 00355 //--------------------------------------------------------------- 00356 // Spring Data 00357 // Since List of HEVertex composes the mesh 00358 // In order to create spring connecting between HEVertex, 00359 // First create pointers to HEVertex for pointing to ParticleRef 00360 // Second create integer list of connected vertices for each HEVertex 00361 // Third create integer list of springs 00362 // 00363 // std::vector<HEVertex<T> *> 00364 // m_ptrHEVertex 00365 // ----------- 00366 // | *------- 0 ------------------------> V0 --> V1 --> ... --> V(n-1) 00367 // ----------- | ^ 00368 // | . | \ | 00369 // | . | \ | 00370 // | . | \ | 00371 // ----------- | | 00372 // | *-----|- #HEVertices - 1 ------------------------------------ 00373 // ----------- | | 00374 // | \ 00375 // std::vector<int> * | \ 00376 // m_ptriListConnectedVertex \ 00377 // ----------- | \ 00378 // | | | ... | | 0 <--+ \ 00379 // ----------- | \ 00380 // | . | | \ 00381 // | . | | | 00382 // | . | | | 00383 // ----------- | | 00384 // | | | ... | | #HEVertices - 1 <--+ 00385 // ----------- | | 00386 // | | 00387 // std::vector<int> * | | 00388 // m_ptriListSpring | | 00389 // ----------- | | 00390 // | | | ... | | 0 <--+ | 00391 // ----------- | 00392 // | . | | 00393 // | . | | 00394 // | . | | 00395 // ----------- | 00396 // | | | ... | | #HEVertices - 1 <--+ 00397 // ----------- 00398 // 00399 // std::list<ParticleRef<T> *> 00400 // m_ptrParticleRef 00401 // ----------- 00402 // | * | 0 -------------------------> P0 --> P1 --> ... --> P(n-1) 00403 // ----------- ^ 00404 // | . | | 00405 // | . | | 00406 // | . | | 00407 // ----------- | 00408 // | *-----|- #Particles - 1 -------------------------------------- 00409 // ----------- 00410 // 00411 // std::list<SpringRef<T> *> 00412 // m_ptrSpringRef 00413 // ----------- 00414 // | * | 0 -------------------------> S0 --> S1 --> ... --> S(s-1) 00415 // ----------- ^ 00416 // | . | | 00417 // | . | | 00418 // | . | | 00419 // ----------- | 00420 // | *-----|- #Springs - 1 ---------------------------------------- 00421 // ----------- 00422 // 00423 // std::list< ParticleRef<T> > m_listParticleRef 00424 // 00425 // std::list< SpringRef<T> > m_listSpringRef 00426 // 00427 //--------------------------------------------------------------- 00428 // Not Finish Yet!!! 00429 // These values have to be set into the springs 00430 // !!! 00431 T m_tPtMass; 00432 T m_tKStiffness; 00433 T m_tKDamper; 00434 T m_tHomeKStiffness; 00435 T m_tHomeKDamper; 00436 //----------------------------------------------------- 00437 int m_iRigidity; 00438 std::vector< SpringRef<T> * > m_vpSpringRef; 00439 //----------------------------------------------------- 00440 std::vector< HEVertexPtrWithExtraInfo<T> * > * m_pHEVertexList; 00441 std::vector< HEVertexPtrWithExtraInfo<T> * > m_HEVertexList_1; 00442 std::vector< ParticleRef<T> * > m_svpParticleRef; 00443 std::vector< SimPropForMultiPartMeshModel_HalfEdge<T> > m_Parts; 00444 00445 #ifdef TAPs_USE_CUDA 00446 00447 00448 00449 unsigned int m_cudaMaxConnectionSize; 00450 unsigned int m_cudaID; 00451 float * m_cudaVertexList; 00452 float * m_cudaPrevVertexList; 00453 float * m_cudaHomeVertexList; 00454 00455 #ifdef TAPs_CUDA_MSS_INDIVIDUAL_SPRING_PROPS 00456 // Need rounding of float to int 00457 float * m_cudaVertexConnectionList; 00458 #else //TAPs_CUDA_MSS_INDIVIDUAL_SPRING_PROPS 00459 int * m_cudaVertexConnectionList; 00460 #endif//TAPs_CUDA_MSS_INDIVIDUAL_SPRING_PROPS 00461 00462 void PrintDebug() 00463 { 00464 for ( int i = 0; i < 5; ++i ) { 00465 int idx = i*4; 00466 printf( "Host Vertex List # %d: %g %g %g %g\n", i, m_cudaVertexList[idx], m_cudaVertexList[idx+1], m_cudaVertexList[idx+2], m_cudaVertexList[idx+3] ); 00467 printf( "Host Prev Vertex List #%d: %g %g %g %g\n", i, m_cudaPrevVertexList[idx], m_cudaPrevVertexList[idx+1], m_cudaPrevVertexList[idx+2], m_cudaPrevVertexList[idx+3] ); 00468 printf( "Host Home Vertex List #%d: %g %g %g %g\n", i, m_cudaHomeVertexList[idx], m_cudaHomeVertexList[idx+1], m_cudaHomeVertexList[idx+2], m_cudaHomeVertexList[idx+3] ); 00469 } 00470 } 00471 00472 void PrintDebug_ConnectionList() 00473 { 00474 //int idx = 0; 00475 //for ( int i = 0; i < 5; ++i ) { 00476 // printf( "Vertex Connection List #%d:", i ); 00477 // for ( int j = 0; j < m_cudaMaxConnectionSize; ++j, ++idx ) { 00478 // printf( "\t%d", m_cudaVertexConnectionList[idx] ); 00479 // } 00480 // printf( "\n" ); 00481 //} 00482 printf( "Spring rest lengths\n" ); 00483 for ( int i = 0; i < static_cast<int>( m_vpSpringRef.size() ); ) { 00484 printf( "\t%g", m_vpSpringRef[i]->GetRestLengthL() ); 00485 ++i; 00486 if ( i % 5 == 0 ) printf( "\n" ); 00487 } 00488 printf( "\n" ); 00489 00490 printf( "Vertex\n" ); 00491 for ( int i = 0; i < static_cast<int>( m_pHEVertexList->size() ); ++i ) { 00492 //printf( "\t%g", m_pHEVertexList[i]->GetVertexPosition() ); 00493 std::cout << "#" << i << ": " << (*m_pHEVertexList)[i]->GetHEVertexPtr()->GetPosition() << "\n"; 00494 } 00495 00496 } 00497 00498 //TAPs_CUDA_DATA_HETriMeshOneModelMultiParts * m_cudaDeviceData; //!< CUDA Device Data 00499 #endif//TAPs_USE_CUDA 00500 00501 #ifdef TAPs_ADVANCED_SIMULATION 00502 public: 00503 // t_TAPsAdvSimID AdvSimID; //!< advanced simulation ID 00504 static unsigned int TotalModels; 00505 #endif//TAPs_ADVANCED_SIMULATION 00506 00507 //============================================================================= 00508 private: 00509 // Member Functions ------------------------------------------------------- 00510 00512 //HEVertexPtrWithExtraInfo<T> & GetVertexPtrWithExtraInfo ( int i ) 00513 //{ return *((*m_pHEVertexList)[i]); } 00514 00515 // Data Members ----------------------------------------------------------- 00516 00517 00518 00519 // For GUIs by wxWidgets 00520 //----------------------------------------------------------------------------- 00521 #ifdef TAPs_USE_WXWIDGETS 00522 public: 00524 void ShowPropertyDialog ( wxWindow * parent = NULL ); 00525 private: 00526 WX::WXHETriMeshOneModelMultiPartsParameters * pDialogs; 00527 #endif//TAPs_USE_WXWIDGETS 00528 //----------------------------------------------------------------------------- 00529 00530 //============================================================================= 00531 }; // CLASS END: HETriMeshOneModelMultiParts 00532 //============================================================================= 00533 END_NAMESPACE_TAPs__OpenGL 00534 //----------------------------------------------------------------------------- 00535 // Include definition if TAPs_USE_EXPORT is not defined 00536 //#if !defined( TAPs_USE_EXPORT ) 00537 #include "TAPsHETriMeshOneModelMultiParts.cpp" 00538 //#endif 00539 //----------------------------------------------------------------------------- 00540 #endif 00541 //34567890123456789012345678901234567890123456789012345678901234567890123456789 00542 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----