00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "DeformableOctreeParticleSystemNode.h"
00013 #include "OctreeParticleSystemEngine.cpp"
00014 #include <animal/X3DTK/linearX3DTK.h>
00015 #include "ManipulatedMeshVertexAnimated.cpp"
00016
00017 using std::cerr;
00018 using std::cout;
00019 using std::endl;
00020
00021
00022
00023 namespace X3DTK
00024 {
00025
00026 namespace X3D
00027 {
00028
00029 typedef animal::octree::OctreeParticleSystemEngine<MFVec3f,float,MFFloat> OctreeParticleSystemEngine;
00030 typedef ManipulatedMeshVertexAnimated<MFVec3f,float,MFFloat> ManipulatedMeshVertexAnimated;
00031
00032
00033 DeformableOctreeParticleSystemNode::DeformableOctreeParticleSystemNode()
00034 : DeformableOctreeNode()
00035 {
00036 DeformableOctreeNode::defineTypeName("DeformableOctreeParticleSystemNode");
00037
00038 }
00039
00040
00041 DeformableOctreeParticleSystemNode::~DeformableOctreeParticleSystemNode()
00042 {}
00043
00044
00045 void DeformableOctreeParticleSystemNode::animate( float dt )
00046 {
00047
00048 _octree->move(dt);
00049
00050 }
00051
00052 void DeformableOctreeParticleSystemNode::postAnimate( float dt )
00053 {
00054
00055 }
00056
00057
00058 void DeformableOctreeParticleSystemNode::postInit()
00059 {
00060
00061 std::cerr << "DON::postInit\n";
00062
00063
00064
00065 MFAbstractNode children = getChildList();
00066
00067 for( MFAbstractNode::iterator it = children.begin() ; it != children.end() ; ++it )
00068 {
00069 std::cerr << "\tGot one child : " << (*it)->getName() << "\n";
00070
00071 if( DeformableOctreeShapeNode *tmpNode =dynamic_cast<DeformableOctreeShapeNode *>(*it) )
00072 {
00073 Require( !_dosn );
00074
00075
00076
00077
00078
00079
00080
00081
00082 if( !tmpNode->getCoordinate() )
00083 {
00084 std::cerr << "DON::init : can't get coordinates from DOSN\n";
00085 }
00086
00087 setDeformableOctreeShapeNode( tmpNode );
00088 }
00089 }
00090
00091
00092 }
00093
00094
00095 void DeformableOctreeParticleSystemNode::createOctree()
00096 {
00097 std::cerr << "DeformableOctreeParticleSystemNode::createOctree()\n";
00098
00099 X3D::Coordinate * coord = _dosn->getCoordinate();
00100
00101
00102 if (!coord)
00103 {
00104 std::cerr<< "Warning: impossible to init the octree. In the file .x3d, there is no Coordinate child of the ShapeNode."<< std::endl;
00105 return;
00106 }
00107 if ( coord->getPoint().size() == 0)
00108 {
00109 std::cerr << "Warning: no vertex in the mesh !\n";
00110 return;
00111 }
00112
00113
00114 const MFVec3f * pointsTmp = &(coord->getPoint());
00115 MFVec3f * points = const_cast<MFVec3f *>(pointsTmp);
00116
00117 Vec3d minVec, maxVec;
00118 minVec.x = (*points)[0].x;
00119 maxVec.x = (*points)[0].x;
00120 minVec.y = (*points)[0].y;
00121 maxVec.y = (*points)[0].y;
00122 minVec.z = (*points)[0].z;
00123 maxVec.z = (*points)[0].z;
00124
00125 for( unsigned int i=0 ; i< points->size() ; ++i )
00126 {
00127 SFVec3f p = (*points)[i];
00128
00129 if( p.x < minVec.x )
00130 minVec.x = p.x;
00131 if( p.y < minVec.y )
00132 minVec.y = p.y;
00133 if( p.z < minVec.z )
00134 minVec.z = p.z;
00135
00136 if( p.x > maxVec.x )
00137 maxVec.x = p.x;
00138 if( p.y > maxVec.y )
00139 maxVec.y = p.y;
00140 if( p.z > maxVec.z )
00141 maxVec.z = p.z;
00142 }
00143
00144
00145
00146 Vec3d sizeVec = maxVec - minVec;
00147
00148
00149 minVec -= sizeVec/100.0;
00150 maxVec += sizeVec/100.0;
00151
00152 minVec.z -= 0.5;
00153 maxVec.z += 1;
00154
00155 sizeVec = maxVec - minVec;
00156
00157 MFVec3f normals = _dosn->computeNormals();
00158
00159
00160 cerr<<"DeformableOctreeNode::createOctree create a particle octree"<<endl;
00161 _octree = new OctreeParticleSystemEngine( minVec, maxVec, points, normals, 10000 );
00162 this->DeformableOctreeNode::_octree = _octree;
00163
00164
00165 _selectedCell = _octree->root();
00166 }
00167
00168
00170 void DeformableOctreeParticleSystemNode::setDeformableOctreeShapeNode( DeformableOctreeShapeNode *dosn)
00171 {
00172 _dosn = dosn;
00173 createOctree();
00174 if( _octree )
00175 {
00176 _dosn->setOctree( _octree );
00177 }
00178 }
00179
00180
00181 void DeformableOctreeParticleSystemNode::draw()
00182 {
00183 DeformableOctreeNode::draw();
00184 if(_drawOctree) _octree->draw();
00185 }
00186
00187 void DeformableOctreeParticleSystemNode::slotSubdivide()
00188 {
00189
00190 _octree->OctreeParticleSystemEngine::subdivideRecDeformed( getSelectedCell() );
00191 }
00192
00193 void DeformableOctreeParticleSystemNode::slotSimplify()
00194 {
00195 _octree->OctreeParticleSystemEngine::simplify(getSelectedCell());
00196
00197 }
00198
00199
00200
00201 animal::ConstrainedItem* DeformableOctreeParticleSystemNode::pickPoint( float* origin, float* direction, float threshold )
00202 {
00203
00204
00205 if( _dosn )
00206 {
00207 X3D::Coordinate* coord = _dosn->getCoordinate();
00208
00209 int index;
00210 float distance;
00211 SFVec3f lineOrigin(origin[0], origin[1], origin[2]);
00212 SFVec3f lineDirection(direction[0],direction[1],direction[2]);
00213
00214
00215 if( animal::findClosestPointToLine( index, distance, coord->getPoint(), lineOrigin, lineDirection, threshold ) )
00216 {
00217 ManipulatedMeshVertexAnimated * c = new ManipulatedMeshVertexAnimated( this, &coord->getPoint()[index]);
00218 return c;
00219 }
00220 else
00221 {
00222 return NULL;
00223 }
00224 }
00225 else
00226 {
00227 return NULL;
00228 }
00229
00230 }
00231
00232
00233
00234
00235 };
00236
00237 };