00001
00002
00003
00004
00005
00006
00007
00008
00009
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
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;
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
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;
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
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
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
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
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
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 };