Documentation


OctreeParticleSystemEngine.cpp

Go to the documentation of this file.
00001 //
00002 // C++ Implementation: OctreeParticleSystemEngine
00003 //
00004 // Description:
00005 //
00006 //
00007 // Author: Matthieu Nesme <>, (C) 2004
00008 //
00009 // Copyright: See COPYING file that comes with this distribution
00010 //
00011 //
00012 #include "OctreeParticleSystemEngine.h"
00013 #include "ConstrainedVertex.h"
00014 
00015 namespace animal
00016 {
00017 
00018 namespace octree
00019 {
00020 
00021 
00022 template<typename t_V, typename t_R, typename t_M>
00023 OctreeParticleSystemEngine<t_V,t_R,t_M>::OctreeParticleSystemEngine(Vec3d bboxMin, Vec3d bboxMax, MFVec3f *points, MFVec3f normals, unsigned int nMaxPointsPerCell)
00024         : Octree(bboxMin, bboxMax, points, normals, nMaxPointsPerCell)
00025         , ParticleSystemEngine<t_V,t_R,t_M>()
00026 {
00027     x=&_particlesPositions;
00028     v=&_particlesVelocities;
00029     m=&_particlesInvMasses;
00030 
00031     _springfield.links=&_springs;
00032     _springfield.stiffnesses=&_stiffnesses;
00033     _springfield.restLengths=&_restLengths;
00034     addForce( &_springfield );
00035 
00036     gravity=Vec(0,0,0);
00037 }
00038 
00039 template<typename t_V, typename t_R, typename t_M>
00040 OctreeParticleSystemEngine<t_V,t_R,t_M>::~OctreeParticleSystemEngine()
00041 {}
00042 
00043 
00044 
00045 
00046 template<typename t_V, typename t_R, typename t_M>
00047 int OctreeParticleSystemEngine<t_V,t_R,t_M>::allLevelsPreUpdateVerticesPositions(Cell * tmpCell,int *indice)
00048 {
00049     if(tmpCell->getData()._points.size() != 0 )
00050     {
00051         for(int i=0;i<8;++i)
00052         {
00053             tmpCell->vertex(i)->_mass += pow(8,tmpCell->getData()._depth+1);
00054         }
00055 
00056         //if( tmpCell->isLeaf() )
00057         {
00058             for(int i=0;i<8;++i)
00059             {
00060                 int pointeur = reinterpret_cast<int>(tmpCell->vertex(i));
00061 
00062                 if(_verticesMap.find(pointeur)==_verticesMap.end())
00063                 {
00064                     _verticesMap[pointeur] = (*indice)++;
00065                     _particlesPositions.push_back( Vec( tmpCell->vertex(i)->getPosition()[0],tmpCell->vertex(i)->getPosition()[1],tmpCell->vertex(i)->getPosition()[2] ));
00066                     _particlesVelocities.push_back( Vec(0,0,0) );
00067                     _particlesInvMasses.push_back( tmpCell->vertex(i)->_mass );
00068                 }
00069             }
00070 
00071             for(int i=0;i<8;++i)
00072             {
00073                 tmpCell->vertex(i)->_mass = 0; //re-init mass to 0
00074                 int pointeuri = reinterpret_cast<int>(tmpCell->vertex(i));
00075                 for(int j=i+1;j<8;++j)
00076                 {
00077                     int pointeurj = reinterpret_cast<int>(tmpCell->vertex(j));
00078 
00079                     _allLevelsSprings[tmpCell->getData()._depth].push_back(_verticesMap[pointeuri]);
00080                     _allLevelsSprings[tmpCell->getData()._depth].push_back(_verticesMap[pointeurj]);
00081                     _allLevelsStiffnesses[tmpCell->getData()._depth].push_back(1000/pow(2,tmpCell->getData()._depth));
00082 
00083                     Real longueur = 0;
00084 
00085 
00086                     int directions[8][3] = { -1, -1, -1,
00087                                              1,-1,-1,
00088                                              -1,1,-1,
00089                                              1,1,-1,
00090                                              -1,-1,1,
00091                                              1,-1,1,
00092                                              -1,1,1,
00093                                              1,1,1};
00094 
00095 
00096 
00097                     if( directions[i][0] * directions[j][0] < 0 )
00098                         longueur += (tmpCell->getData()._initialSize[0] * tmpCell->getData()._initialSize[0]);
00099                     if( directions[i][1] * directions[j][1] < 0 )
00100                         longueur += (tmpCell->getData()._initialSize[1] * tmpCell->getData()._initialSize[1]);
00101                     if( directions[i][2] * directions[j][2] < 0 )
00102                         longueur += (tmpCell->getData()._initialSize[2] * tmpCell->getData()._initialSize[2]);
00103                     _allLevelsRestLengths[tmpCell->getData()._depth].push_back(sqrt(longueur));
00104                 }
00105             }
00106         }
00107 //        else
00108         {
00109             for(int i=0;i<8;++i)
00110                 preUpdateVerticesPositions(tmpCell->child(i),indice);
00111         }
00112     }
00113 }
00114 
00115 
00116 
00117 
00118 
00119 
00120 
00121 
00122 template<typename t_V, typename t_R, typename t_M>
00123 int OctreeParticleSystemEngine<t_V,t_R,t_M>::preUpdateVerticesPositions(Cell * tmpCell,int *indice)
00124 {
00125     if(tmpCell->getData()._points.size() != 0 )
00126     {
00127         for(int i=0;i<8;++i)
00128         {
00129             tmpCell->vertex(i)->_mass += pow(8,tmpCell->getData()._depth+1);
00130         }
00131 
00132         if( tmpCell->isLeaf() )
00133         {
00134             for(int i=0;i<8;++i)
00135             {
00136                 int pointeur = reinterpret_cast<int>(tmpCell->vertex(i));
00137 
00138                 if(_verticesMap.find(pointeur)==_verticesMap.end())
00139                 {
00140                     _verticesMap[pointeur] = (*indice)++;
00141                     _particlesPositions.push_back( Vec( tmpCell->vertex(i)->getPosition()[0],tmpCell->vertex(i)->getPosition()[1],tmpCell->vertex(i)->getPosition()[2] ));
00142                     _particlesVelocities.push_back( Vec(0,0,0) );
00143                     _particlesInvMasses.push_back( tmpCell->vertex(i)->_mass );
00144                 }
00145             }
00146 
00147             for(int i=0;i<8;++i)
00148             {
00149                 tmpCell->vertex(i)->_mass = 0; //re-init mass to 0
00150                 int pointeuri = reinterpret_cast<int>(tmpCell->vertex(i));
00151                 for(int j=i+1;j<8;++j)
00152                 {
00153                     int pointeurj = reinterpret_cast<int>(tmpCell->vertex(j));
00154 
00155                     _springs.push_back(_verticesMap[pointeuri]);
00156                     _springs.push_back(_verticesMap[pointeurj]);
00157                     _stiffnesses.push_back(1000/pow(2,tmpCell->getData()._depth));
00158 
00159                     Real longueur = 0;
00160 
00161 
00162                     int directions[8][3] = { -1, -1, -1,
00163                                              1,-1,-1,
00164                                              -1,1,-1,
00165                                              1,1,-1,
00166                                              -1,-1,1,
00167                                              1,-1,1,
00168                                              -1,1,1,
00169                                              1,1,1};
00170 
00171 
00172 
00173                     if( directions[i][0] * directions[j][0] < 0 )
00174                         longueur += (tmpCell->getData()._initialSize[0] * tmpCell->getData()._initialSize[0]);
00175                     if( directions[i][1] * directions[j][1] < 0 )
00176                         longueur += (tmpCell->getData()._initialSize[1] * tmpCell->getData()._initialSize[1]);
00177                     if( directions[i][2] * directions[j][2] < 0 )
00178                         longueur += (tmpCell->getData()._initialSize[2] * tmpCell->getData()._initialSize[2]);
00179                     _restLengths.push_back(sqrt(longueur));
00180                 }
00181             }
00182         }
00183         else
00184         {
00185             for(int i=0;i<8;++i)
00186                 preUpdateVerticesPositions(tmpCell->child(i),indice);
00187         }
00188     }
00189 }
00190 
00191 
00192 template<typename t_V, typename t_R, typename t_M>
00193 int OctreeParticleSystemEngine<t_V,t_R,t_M>::postUpdateVerticesPositions()
00194 {
00195     for( VerticesMap::iterator it( _verticesMap.begin() ) ; it!=_verticesMap.end() ; ++it)
00196     {
00197         ConstrainedVertex * cv = reinterpret_cast<ConstrainedVertex *>((*it).first);
00198         cv->setPosition(Vec3d( _particlesPositions[(*it).second][0], _particlesPositions[(*it).second][1], _particlesPositions[(*it).second][2]  ));
00199         cv->_mass=0;
00200     }
00201 }
00202 
00203 template<typename t_V, typename t_R, typename t_M>
00204 void OctreeParticleSystemEngine<t_V,t_R,t_M>::move(double dt)
00205 {
00206     //cerr<<"--------move----------\n";
00207     _particlesPositions.resize(0);
00208     _particlesVelocities.resize(0);
00209     _particlesInvMasses.resize(0);
00210     _springs.resize(0);
00211     _stiffnesses.resize(0);
00212     _restLengths.resize(0);
00213     _verticesMap.clear();
00214     
00215     
00219     
00220 /*  int n=10;
00221     _allLevelsSprings.resize(n);
00222     for(int i=0;i<n;++i)
00223     {
00224         _allLevelsSprings[i].resize(0);
00225         _allLevelsStiffnesses[i].resize(0);
00226         _allLevelsRestLengths[i].resize(0);
00227     }
00228     
00229     allLevelsPreUpdateVerticesPositions( root() );
00230     
00231     for(int i=0;i<n;++i)
00232     {
00233         if( _allLevelsSprings[i].size()!=0)
00234         {
00235             _springfield.links=&_allLevelsSprings[i];
00236             _springfield.stiffnesses=&_allLevelsStiffnesses[i];
00237             _springfield.restLengths=&_allLevelsRestLengths[i];
00238             ParticleSystemEngine<t_V,t_R,t_M>::move(dt);
00239             postUpdateVerticesPositions(  );
00240         }
00241         else
00242             break;
00243     }
00244 */
00245     
00249     
00250     preUpdateVerticesPositions( root() );
00251     ParticleSystemEngine<t_V,t_R,t_M>::move(dt);
00252     postUpdateVerticesPositions(  );
00253 
00255     
00256     movedRecLeaves(root());
00257 }
00258 
00259 
00260 
00261 
00262 
00263 
00264 
00265 
00266 // template<typename t_V, typename t_R, typename t_M>
00267 // void OctreeParticleSystemEngine<t_V,t_R,t_M>::subdivideRecDeformed( Cell *cell )
00268 // {
00269 //  //cerr<<"OctreeParticleSystemEngine<t_V,t_R,t_M>::subdivideRecDeformed( Cell *cell )\n";
00270 //     /*for( dfs_leaf_iterator it( cell ) ; !it.empty() ; )
00271 //     {
00272 //         Cell *tmpCell = ++it;
00273 //
00274 //         if( tmpCell->isLeaf() && tmpCell->getData()._points.size() != 0 )
00275 //         {
00276 //          subdivideDeformed( tmpCell );
00277 //         }
00278 //     }*/
00279 //
00280 //
00281 //  subdivideDeformed( cell );
00282 //
00283 //  structureChangedRecLeaves(root());
00284 //     movedRecLeaves(root());
00285 // }
00286 
00287 /*template<typename t_V, typename t_R, typename t_M>
00288 void OctreeParticleSystemEngine<t_V,t_R,t_M>::subdivideDeformed( Cell *cell )
00289 {
00290     //cerr<<"OctreeParticleSystemEngine<t_V,t_R,t_M>::subdivideDeformed( Cell *cell )\n";
00291     Octree::subdivideDeformed( cell );
00292     for(int i=0;i<8;++i)
00293         if(cell->child(i)->getData()._points.size() != 0)
00294             addSprings( cell->child(i) );
00295 }*/
00296 
00297 /*template<typename t_V, typename t_R, typename t_M>
00298 void OctreeParticleSystemEngine<t_V,t_R,t_M>::subdivideDeformed( Cell *cell )
00299 {
00300     if(cell)
00301     {
00302         if(cell->isLeaf())
00303         {
00304             if( cell->getData()._points.size() != 0 )
00305             {
00306                 Octree::subdivideDeformed( cell );
00307                 for(int i=0;i<8;++i)
00308                     if(cell->child(i)->getData()._points.size() != 0)
00309                         addSprings( cell->child(i) );
00310             }
00311         }
00312         else
00313         {
00314             for(int i=0;i<8;++i)
00315                 subdivideDeformed(cell->child(i));
00316         }
00317     }
00318 }*/
00319 
00320 
00321 
00322 
00323 
00324 
00325 
00326 
00327 
00328 /*template<typename t_V, typename t_R, typename t_M>
00329 void OctreeParticleSystemEngine<t_V,t_R,t_M>::addSprings( Cell *cell )
00330 {
00331     for(int i=0;i<8;i++)
00332     {
00333         _particlesPositions.push_back( Vec( cell->vertex(i)->getPosition()[0],cell->vertex(i)->getPosition()[1],cell->vertex(i)->getPosition()[2]) );
00334         _particlesVelocities.push_back( Vec(0,0,0) );
00335         _particlesInvMasses.push_back(1);
00336     }   
00337     
00338     for(unsigned int i=_particlesPositions.size()-8;i<_particlesPositions.size();i++)
00339     {
00340         for(unsigned int j=i+1;j<_particlesPositions.size();++j)
00341         {
00342             _springs.push_back(i);
00343             _springs.push_back(j);
00344             _stiffnesses.push_back(1000);
00345             _restLengths.push_back(v_norm(_particlesPositions[i]-_particlesPositions[j]));
00346         }
00347     }
00348 }*/
00349 
00350 
00351 /*template<typename t_V, typename t_R, typename t_M>
00352 void OctreeParticleSystemEngine<t_V,t_R,t_M>::simplify( Cell *cell )
00353 {
00354     Octree::simplify( cell );
00355     
00356  
00357     _particlesPositions.resize(0);
00358     _particlesVelocities.resize(0);
00359     _particlesInvMasses.resize(0);
00360     
00361     _springs.resize(0);
00362     _stiffnesses.resize(0);
00363     _restLengths.resize(0);
00364     
00365     addSprings(root());
00366     
00367 }*/
00368 
00369 
00370 
00371 template<typename t_V, typename t_R, typename t_M>
00372 void OctreeParticleSystemEngine<t_V,t_R,t_M>::draw()
00373 {
00374     ParticleSystemEngine<t_V,t_R,t_M>::draw();
00375 }
00376 
00377 
00378 };
00379 
00380 };

Generated on Thu Dec 23 13:52:26 2004 by doxygen 1.3.6