Home › Forum › SOFA › Programming with SOFA › [SOLVED] find tetrahedra by unique id
Tagged: 64_bits, Linux_ubuntu, SOFA_other
- This topic has 3 replies, 3 voices, and was last updated 7 years, 5 months ago by jnbrunet.
-
AuthorPosts
-
2 May 2017 at 00:09 #9024hhhBlocked
how do i go about finding a particular tetrahedra?
i’ve looked through the code and it seems like a particular tetrahedra is identified by its location or array index.
are tetrahedra elements not identified by a unique tetra id number?
the problem with this is that if tetrahedras are added or removed the datastructure is dynamic so a particular tetrahedra can move around and change its index.
for example, maybe im interested in tetra number 12. through out a simulation i change topology. now i have no idea where tetra number 12 is now because elements were removed and or added so that tetra moved somewhere in the datastructure.
2 May 2017 at 16:22 #9027jnbrunetModeratorThe proper way to do it would probably be by creating your own topological engine that would create an unique index for each tetra and receive topological change events from the mesh topology.
Here is an example of a simple component that create an unique id for each tetra and then print out the unique id of a tetra when it is created/deleted:
#include <sofa/core/objectmodel/BaseObject.h> #include <sofa/core/topology/BaseMeshTopology.h> #include <SofaBaseTopology/TopologyData.h> #include <SofaBaseTopology/TopologyData.inl> #include <SofaBaseTopology/TopologyDataHandler.h> #include <sofa/core/ObjectFactory.h> class ElementListener : public sofa::core::objectmodel::BaseObject { public: SOFA_CLASS(ElementListener, sofa::core::objectmodel::BaseObject); typedef typename sofa::defaulttype::Vec3dTypes DataTypes; typedef typename DataTypes::VecCoord VecCoord; class TetrahedronHandler : public virtual sofa::component::topology::TopologyDataHandler<sofa::core::topology::BaseMeshTopology::Tetrahedron, sofa::helper::vector<unsigned int> > { typedef typename sofa::component::topology::TopologyDataHandler<sofa::core::topology::BaseMeshTopology::Tetrahedron, sofa::helper::vector<unsigned int> > Inherit; public : TetrahedronHandler(sofa::component::topology::TetrahedronData<sofa::helper::vector<unsigned int> >* data) : Inherit(data) {} void applyCreateFunction(unsigned int idx, unsigned int &unique_id, const sofa::core::topology::BaseMeshTopology::Tetrahedron &tetra, const sofa::helper::vector<unsigned int> &ancestors, const sofa::helper::vector<double> &coefs) { static unsigned int tetra_count = 0; unique_id = tetra_count++; std::cout<<"Tetra #"<<unique_id<<" created"<<std::endl; } virtual void applyDestroyFunction(unsigned int idx, unsigned int& unique_id) { std::cout<<"Tetra #"<<unique_id<<" deleted"<<std::endl; } }; virtual void init() { _topology = this->getContext()->getMeshTopology(); if (_topology->getNbTetrahedra()==0) { serr << "No tetrahedral set topology found."<<sendl; return; } reinit(); } virtual void reinit() { sofa::helper::vector<unsigned int>& tetrahedronInf = *(tetrahedronInfo.beginEdit()); tetrahedronInf.resize(_topology->getNbTetrahedra()); for (int i=0; i<_topology->getNbTetrahedra(); ++i) { tetrahedronHandler->applyCreateFunction(i, tetrahedronInf[i], _topology->getTetrahedron(i), (const std::vector< unsigned int > )0, (const std::vector< double >)0); } tetrahedronInfo.createTopologicalEngine(_topology,tetrahedronHandler); tetrahedronInfo.registerTopologicalData(); tetrahedronInfo.endEdit(); } protected: ElementListener() : tetrahedronInfo(initData(&tetrahedronInfo, "tetrahedronInfo", "Internal tetrahedron data")) { tetrahedronHandler = new TetrahedronHandler(&tetrahedronInfo); } private: TetrahedronHandler* tetrahedronHandler; sofa::component::topology::TetrahedronData<sofa::helper::vector<unsigned int> > tetrahedronInfo; sofa::core::topology::BaseMeshTopology* _topology; }; int ElementListenerClass = sofa::core::RegisterObject("Simple element listener") .add<ElementListener>(); SOFA_DECL_CLASS(ElementListener)
If you add this component to your scene, you should receive a console message whenever a tetrahedron get created or deleted.
27 May 2017 at 04:43 #9193BineshBlockedHI.
Also you can use handletopologychange function.
This is virtual function and you can reimplemented this function with current topology change.
You can search this function in project to learn how can use it.
Good luck
Behnam Binesh29 May 2017 at 16:58 #9206jnbrunetModeratorI would advise not to use the
handletopologychange
function since it is marked as deprecated and could be removed eventually. -
AuthorPosts
- You must be logged in to reply to this topic.