diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/CylinderCollisionModel.h b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/CylinderCollisionModel.h index ed1fbd88f95..2d07fc404e0 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/CylinderCollisionModel.h +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/CylinderCollisionModel.h @@ -24,7 +24,7 @@ #include #include -#include +#include namespace sofa::component::collision::geometry { @@ -71,10 +71,10 @@ using Cylinder = TCylinder; *CylinderModel templated by RigidTypes (frames), direction is given by Y direction of the frame. */ template< class TDataTypes> -class CylinderCollisionModel : public core::CollisionModel +class CylinderCollisionModel : public core::CollisionModel, public virtual core::behavior::SingleStateAccessor { public: - SOFA_CLASS(SOFA_TEMPLATE(CylinderCollisionModel, TDataTypes), core::CollisionModel); + SOFA_CLASS2(SOFA_TEMPLATE(CylinderCollisionModel, TDataTypes), core::CollisionModel, SOFA_TEMPLATE(core::behavior::SingleStateAccessor, TDataTypes)); typedef TDataTypes DataTypes; typedef DataTypes InDataTypes; @@ -99,7 +99,7 @@ class CylinderCollisionModel : public core::CollisionModel protected: CylinderCollisionModel(); - CylinderCollisionModel(core::behavior::MechanicalState* mstate ); + CylinderCollisionModel(core::behavior::MechanicalState* _mstate ); public: void init() override; @@ -111,7 +111,7 @@ class CylinderCollisionModel : public core::CollisionModel void draw(const core::visual::VisualParams* vparams,sofa::Index index) override; - core::behavior::MechanicalState* getMechanicalState() { return m_mstate; } + core::behavior::MechanicalState* getMechanicalState() { return this->mstate; } Real radius(sofa::Index index) const; @@ -138,8 +138,6 @@ class CylinderCollisionModel : public core::CollisionModel Data& writeHeights(); Data& writeLocalAxes(); -protected: - core::behavior::MechanicalState* m_mstate; }; diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/CylinderCollisionModel.inl b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/CylinderCollisionModel.inl index b73b1a0641e..e0a17f3b75d 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/CylinderCollisionModel.inl +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/CylinderCollisionModel.inl @@ -40,17 +40,16 @@ CylinderCollisionModel::CylinderCollisionModel(): d_cylinder_heights(initData(&d_cylinder_heights,"heights","The cylinder heights")), d_default_radius(initData(&d_default_radius,Real(0.5),"defaultRadius","The default radius")), d_default_height(initData(&d_default_height,Real(2),"defaultHeight","The default height")), - d_default_local_axis(initData(&d_default_local_axis,typename DataTypes::Vec3(0.0, 1.0, 0.0),"defaultLocalAxis", "The default local axis cylinder is modeled around")), - m_mstate(nullptr) + d_default_local_axis(initData(&d_default_local_axis,typename DataTypes::Vec3(0.0, 1.0, 0.0),"defaultLocalAxis", "The default local axis cylinder is modeled around")) { enum_type = CYLINDER_TYPE; } template -CylinderCollisionModel::CylinderCollisionModel(core::behavior::MechanicalState* mstate) +CylinderCollisionModel::CylinderCollisionModel(core::behavior::MechanicalState* ms) : CylinderCollisionModel() { - m_mstate = mstate; + this->mstate = ms; enum_type = CYLINDER_TYPE; } @@ -101,16 +100,12 @@ void CylinderCollisionModel::resize(sofa::Size size) template void CylinderCollisionModel::init() { - this->CollisionModel::init(); - m_mstate = dynamic_cast< core::behavior::MechanicalState* > (getContext()->getMechanicalState()); - if (m_mstate==nullptr) - { - msg_error() << "CylinderCollisionModel requires a Rigid Mechanical Model"; - d_componentState.setValue(ComponentState::Invalid); + Inherit2::init(); + + if (d_componentState.getValue() == ComponentState::Invalid) return; - } - resize(m_mstate->getSize()); + resize(this->mstate->getSize()); } @@ -120,7 +115,7 @@ void CylinderCollisionModel::computeBoundingTree(int maxDepth) using namespace sofa::type; using namespace sofa::defaulttype; CubeCollisionModel* cubeModel = createPrevious(); - const auto ncyl = m_mstate->getSize(); + const auto ncyl = this->mstate->getSize(); bool updated = false; if (ncyl != size) @@ -198,7 +193,7 @@ typename CylinderCollisionModel::Real CylinderCollisionModel< DataTyp template const typename CylinderCollisionModel::Coord & CylinderCollisionModel< DataTypes >::center(sofa::Index i)const{ - return DataTypes::getCPos((m_mstate->read(core::vec_id::read_access::position)->getValue())[i]); + return DataTypes::getCPos((this->mstate->read(core::vec_id::read_access::position)->getValue())[i]); } template @@ -240,7 +235,7 @@ typename TCylinder::Real TCylinder::radius() const template const typename CylinderCollisionModel::Coord & CylinderCollisionModel::velocity(sofa::Index index) const { - return DataTypes::getDPos(((m_mstate->read(core::vec_id::read_access::velocity)->getValue()))[index]); + return DataTypes::getDPos(((this->mstate->read(core::vec_id::read_access::velocity)->getValue()))[index]); } @@ -249,7 +244,7 @@ const typename TCylinder::Coord & TCylinder::v() const {r template const sofa::type::Quat CylinderCollisionModel::orientation(sofa::Index index)const{ - return m_mstate->read(core::vec_id::read_access::position)->getValue()[index].getOrientation(); + return this->mstate->read(core::vec_id::read_access::position)->getValue()[index].getOrientation(); } template diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/LineCollisionModel.h b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/LineCollisionModel.h index 374569733fa..fa40c6cddd1 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/LineCollisionModel.h +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/LineCollisionModel.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -73,10 +74,10 @@ class TLine : public core::TCollisionElementIterator; template -class LineCollisionModel : public core::CollisionModel +class LineCollisionModel : public core::CollisionModel, public virtual core::behavior::SingleStateAccessor { public : - SOFA_CLASS(SOFA_TEMPLATE(LineCollisionModel, TDataTypes), core::CollisionModel); + SOFA_CLASS2(SOFA_TEMPLATE(LineCollisionModel, TDataTypes), core::CollisionModel, SOFA_TEMPLATE(core::behavior::SingleStateAccessor, TDataTypes)); enum LineFlag { @@ -130,7 +131,7 @@ public : bool canCollideWithElement(sofa::Index index, CollisionModel* model2, sofa::Index index2) override; - core::behavior::MechanicalState* getMechanicalState() { return mstate; } + core::behavior::MechanicalState* getMechanicalState() { return this->mstate; } Deriv velocity(sofa::Index index)const; @@ -139,6 +140,10 @@ public : int getLineFlags(sofa::Index i); Data d_bothSide; ///< activate collision on both side of the line model (when surface normals are defined on these lines) + Data d_displayFreePosition; ///< Display Collision Model Points free position(in green) + + /// Link to be set to the topology container in the component graph. + SingleLink, sofa::core::topology::BaseMeshTopology, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_topology; /// Pre-construction check method called by ObjectFactory. /// Check that DataTypes matches the MechanicalState. @@ -161,14 +166,7 @@ public : void computeBBox(const core::ExecParams* params, bool onlyVisible) override; - Data d_displayFreePosition; ///< Display Collision Model Points free position(in green) - - /// Link to be set to the topology container in the component graph. - SingleLink, sofa::core::topology::BaseMeshTopology, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_topology; - protected: - core::behavior::MechanicalState* mstate; - Topology* topology; PointCollisionModel* mpoints; int meshRevision; }; diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/LineCollisionModel.inl b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/LineCollisionModel.inl index dd485591a07..ae7a8022fe1 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/LineCollisionModel.inl +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/LineCollisionModel.inl @@ -37,10 +37,11 @@ using core::topology::BaseMeshTopology; template LineCollisionModel::LineCollisionModel() - : d_bothSide(initData(&d_bothSide, false, "bothSide", "activate collision on both side of the line model (when surface normals are defined on these lines)") ) + : needsUpdate(true) + , d_bothSide(initData(&d_bothSide, false, "bothSide", "activate collision on both side of the line model (when surface normals are defined on these lines)") ) , d_displayFreePosition(initData(&d_displayFreePosition, false, "displayFreePosition", "Display Collision Model Points free position(in green)") ) , l_topology(initLink("topology", "link to the topology container")) - , mstate(nullptr), topology(nullptr), meshRevision(-1) + , meshRevision(-1) { enum_type = LINE_TYPE; } @@ -56,15 +57,11 @@ void LineCollisionModel::resize(sofa::Size size) template void LineCollisionModel::init() { - this->CollisionModel::init(); - mstate = dynamic_cast< core::behavior::MechanicalState* > (getContext()->getMechanicalState()); + Inherit2::init(); this->getContext()->get(mpoints); - if (mstate==nullptr) - { - msg_error() << "LineModel requires a Vec3 Mechanical Model"; + if (this->d_componentState.getValue() == sofa::core::objectmodel::ComponentState::Invalid) return; - } if (l_topology.empty()) { @@ -72,21 +69,20 @@ void LineCollisionModel::init() l_topology.set(this->getContext()->getMeshTopologyLink()); } - core::topology::BaseMeshTopology *bmt = l_topology.get(); msg_info() << "Topology path used: '" << l_topology.getLinkedPath() << "'"; - if (!bmt) + if (!l_topology) { msg_error() << "No topology component found at path: " << l_topology.getLinkedPath() << ", nor in current context: " << this->getContext()->name << ". LineCollisionModel requires a MeshTopology"; this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid); return; } - resize( bmt->getNbEdges() ); + resize( l_topology->getNbEdges() ); - for(core::topology::BaseMeshTopology::EdgeID i = 0; i < bmt->getNbEdges(); i++) + for(core::topology::BaseMeshTopology::EdgeID i = 0; i < l_topology->getNbEdges(); i++) { - elems[i].p[0] = bmt->getEdge(i)[0]; - elems[i].p[1] = bmt->getEdge(i)[1]; + elems[i].p[0] = l_topology->getEdge(i)[0]; + elems[i].p[1] = l_topology->getEdge(i)[1]; } updateFromTopology(); @@ -95,23 +91,22 @@ void LineCollisionModel::init() template void LineCollisionModel::handleTopologyChange() { - core::topology::BaseMeshTopology *bmt = l_topology.get(); - if (bmt) + if (l_topology) { - resize(bmt->getNbEdges()); + resize(l_topology->getNbEdges()); - for(sofa::Size i = 0; i < bmt->getNbEdges(); i++) + for(sofa::Size i = 0; i < l_topology->getNbEdges(); i++) { - elems[i].p[0] = bmt->getEdge(i)[0]; - elems[i].p[1] = bmt->getEdge(i)[1]; + elems[i].p[0] = l_topology->getEdge(i)[0]; + elems[i].p[1] = l_topology->getEdge(i)[1]; } needsUpdate = true; } - if (bmt) + if (l_topology) { - std::list::const_iterator itBegin = bmt->beginChange(); - const std::list::const_iterator itEnd = bmt->endChange(); + std::list::const_iterator itBegin = l_topology->beginChange(); + const std::list::const_iterator itEnd = l_topology->endChange(); while( itBegin != itEnd ) { @@ -149,9 +144,9 @@ void LineCollisionModel::handleTopologyChange() sofa::Index last; sofa::Index ind_last; - if (bmt) + if (l_topology) { - last = bmt->getNbEdges() - 1; + last = l_topology->getNbEdges() - 1; } else { @@ -188,9 +183,9 @@ void LineCollisionModel::handleTopologyChange() case core::topology::POINTSREMOVED : { - if (bmt) + if (l_topology) { - sofa::Index last = bmt->getNbPoints() - 1; + sofa::Index last = l_topology->getNbPoints() - 1; sofa::Index i,j; const auto& tab = ( static_cast< const core::topology::PointsRemoved * >( *itBegin ) )->getArray(); @@ -216,7 +211,7 @@ void LineCollisionModel::handleTopologyChange() lastIndexVec[i_next] = lastIndexVec[i]; } - const auto &shell = bmt->getEdgesAroundVertex(lastIndexVec[i]); + const auto &shell = l_topology->getEdgesAroundVertex(lastIndexVec[i]); for (j = 0; j < shell.size(); ++j) { @@ -243,7 +238,7 @@ void LineCollisionModel::handleTopologyChange() case core::topology::POINTSRENUMBERING: { - if (bmt) + if (l_topology) { sofa::Index i; @@ -272,24 +267,23 @@ void LineCollisionModel::handleTopologyChange() template void LineCollisionModel::updateFromTopology() { - core::topology::BaseMeshTopology *bmt = l_topology.get(); - if (bmt) + if (l_topology) { - const int revision = bmt->getRevision(); + const int revision = l_topology->getRevision(); if (revision == meshRevision) return; needsUpdate = true; - const sofa::Size nbPoints = mstate->getSize(); - const sofa::Size nbLines = bmt->getNbEdges(); + const sofa::Size nbPoints = this->mstate->getSize(); + const sofa::Size nbLines = l_topology->getNbEdges(); resize( nbLines ); sofa::Index index = 0; for (sofa::Size i = 0; i < nbLines; i++) { - core::topology::BaseMeshTopology::Line idx = bmt->getEdge(i); + core::topology::BaseMeshTopology::Line idx = l_topology->getEdge(i); if (idx[0] >= nbPoints || idx[1] >= nbPoints) { @@ -358,7 +352,6 @@ bool LineCollisionModel::canCollideWithElement(sofa::Index index, Col { if (!this->bSelfCollision.getValue()) return true; if (this->getContext() != model2->getContext()) return true; - core::topology::BaseMeshTopology *topology = l_topology.get(); /* TODO : separate 2 case: the model is only composed of lines or is composed of triangles */ @@ -366,13 +359,13 @@ bool LineCollisionModel::canCollideWithElement(sofa::Index index, Col sofa::Index p12 = elems[index].p[1]; - if (!topology) + if (!l_topology) { msg_error() << "no topology found"; return true; } - const auto& EdgesAroundVertex11 =topology->getEdgesAroundVertex(p11); - const auto& EdgesAroundVertex12 =topology->getEdgesAroundVertex(p12); + const auto& EdgesAroundVertex11 = l_topology->getEdgesAroundVertex(p11); + const auto& EdgesAroundVertex12 = l_topology->getEdgesAroundVertex(p12); if (model2 == this) { @@ -385,8 +378,8 @@ bool LineCollisionModel::canCollideWithElement(sofa::Index index, Col // in the neighborhood, if we find a segment in common, we cancel the collision - const auto& EdgesAroundVertex21 =topology->getEdgesAroundVertex(p21); - const auto& EdgesAroundVertex22 =topology->getEdgesAroundVertex(p22); + const auto& EdgesAroundVertex21 = l_topology->getEdgesAroundVertex(p21); + const auto& EdgesAroundVertex22 = l_topology->getEdgesAroundVertex(p22); for (sofa::Size i1=0; i1 int LineCollisionModel::getLineFlags(sofa::Index i) { int f = 0; - if (topology) + if (l_topology) { sofa::core::topology::BaseMeshTopology::Edge e(elems[i].p[0], elems[i].p[1]); i = getElemEdgeIndex(i); - if (i < topology->getNbEdges()) + if (i < l_topology->getNbEdges()) { for (sofa::Index j=0; j<2; ++j) { - const auto& eav = topology->getEdgesAroundVertex(e[j]); + const auto& eav = l_topology->getEdgesAroundVertex(e[j]); if (eav[0] == (sofa::core::topology::BaseMeshTopology::EdgeID)i) f |= (FLAG_P1 << j); if (eav.size() == 1) @@ -629,7 +622,7 @@ template inline typename DataTypes::Deriv TLine::n() const {return (this->model->mpoints->getNormal(this->i1()) + this->model->mpoints->getNormal( this->i2())).normalized();} template -inline typename LineCollisionModel::Deriv LineCollisionModel::velocity(sofa::Index index) const { return (mstate->read(core::vec_id::read_access::velocity)->getValue()[elems[index].p[0]] + mstate->read(core::vec_id::read_access::velocity)->getValue()[elems[index].p[1]])/((Real)(2.0)); } +inline typename LineCollisionModel::Deriv LineCollisionModel::velocity(sofa::Index index) const { return (this->mstate->read(core::vec_id::read_access::velocity)->getValue()[elems[index].p[0]] + this->mstate->read(core::vec_id::read_access::velocity)->getValue()[elems[index].p[1]])/((Real)(2.0)); } template inline int TLine::flags() const { return this->model->getLineFlags(this->index); } diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/PointCollisionModel.h b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/PointCollisionModel.h index e5ebbec02ba..b7c302fd1c9 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/PointCollisionModel.h +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/PointCollisionModel.h @@ -23,7 +23,7 @@ #include #include -#include +#include #include #include @@ -60,10 +60,10 @@ class TPoint : public core::TCollisionElementIterator; template -class PointCollisionModel : public core::CollisionModel +class PointCollisionModel : public core::CollisionModel, public virtual core::behavior::SingleStateAccessor { public: - SOFA_CLASS(SOFA_TEMPLATE(PointCollisionModel, TDataTypes), core::CollisionModel); + SOFA_CLASS2(SOFA_TEMPLATE(PointCollisionModel, TDataTypes), core::CollisionModel, SOFA_TEMPLATE(core::behavior::SingleStateAccessor, TDataTypes)); typedef TDataTypes DataTypes; typedef DataTypes InDataTypes; @@ -97,7 +97,7 @@ class PointCollisionModel : public core::CollisionModel bool canCollideWithElement(sofa::Index index, CollisionModel* model2, sofa::Index index2) override; - core::behavior::MechanicalState* getMechanicalState() { return mstate; } + core::behavior::MechanicalState* getMechanicalState() { return this->mstate; } Deriv getNormal(sofa::Index index){ return (normals.size()) ? normals[index] : Deriv();} @@ -127,18 +127,15 @@ class PointCollisionModel : public core::CollisionModel return l_topology.get(); } -protected: - - core::behavior::MechanicalState* mstate; - Data d_computeNormals; ///< activate computation of normal vectors (required for some collision detection algorithms) Data d_displayFreePosition; ///< Display Collision Model Points free position(in green) - VecDeriv normals; - /// Link to be set to the topology container in the component graph. SingleLink, sofa::core::topology::BaseMeshTopology, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_topology; +protected: + + VecDeriv normals; }; @@ -172,7 +169,7 @@ template inline const typename DataTypes::Deriv& TPoint::v() const { return this->model->mstate->read(core::vec_id::read_access::velocity)->getValue()[this->index]; } template -inline const typename DataTypes::Deriv& PointCollisionModel::velocity(sofa::Index index) const { return mstate->read(core::vec_id::read_access::velocity)->getValue()[index]; } +inline const typename DataTypes::Deriv& PointCollisionModel::velocity(sofa::Index index) const { return this->mstate->read(core::vec_id::read_access::velocity)->getValue()[index]; } template inline typename DataTypes::Deriv TPoint::n() const { return ((unsigned)this->indexmodel->normals.size()) ? this->model->normals[this->index] : Deriv(); } diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/PointCollisionModel.inl b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/PointCollisionModel.inl index 8a10d5227df..638664028d1 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/PointCollisionModel.inl +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/PointCollisionModel.inl @@ -35,7 +35,6 @@ namespace sofa::component::collision::geometry template PointCollisionModel::PointCollisionModel() : d_bothSide(initData(&d_bothSide, false, "bothSide", "activate collision on both side of the point model (when surface normals are defined on these points)") ) - , mstate(nullptr) , d_computeNormals(initData(&d_computeNormals, false, "computeNormals", "activate computation of normal vectors (required for some collision detection algorithms)") ) , d_displayFreePosition(initData(&d_displayFreePosition, false, "displayFreePosition", "Display Collision Model Points free position(in green)") ) , l_topology(initLink("topology", "link to the topology container")) @@ -52,14 +51,10 @@ void PointCollisionModel::resize(sofa::Size size) template void PointCollisionModel::init() { - this->CollisionModel::init(); - mstate = dynamic_cast< core::behavior::MechanicalState* > (getContext()->getMechanicalState()); + Inherit2::init(); - if (mstate==nullptr) - { - msg_error() << "PointModel requires a Vec3 Mechanical Model"; + if (this->d_componentState.getValue() == sofa::core::objectmodel::ComponentState::Invalid) return; - } if (l_topology.empty()) { @@ -67,7 +62,7 @@ void PointCollisionModel::init() l_topology.set(this->getContext()->getMeshTopologyLink()); } - const int npoints = mstate->getSize(); + const int npoints = this->mstate->getSize(); resize(npoints); if (d_computeNormals.getValue()) updateNormals(); } @@ -86,11 +81,10 @@ bool PointCollisionModel::canCollideWithElement(sofa::Index index, Co if (index<=index2) // to avoid to have two times the same auto-collision we only consider the case when index > index2 return false; - sofa::core::topology::BaseMeshTopology* topology = l_topology.get(); // in the neighborhood, if we find a point in common, we cancel the collision - const auto& verticesAroundVertex1 =topology->getVerticesAroundVertex(index); - const auto& verticesAroundVertex2 =topology->getVerticesAroundVertex(index2); + const auto& verticesAroundVertex1 = l_topology->getVerticesAroundVertex(index); + const auto& verticesAroundVertex2 = l_topology->getVerticesAroundVertex(index2); for (sofa::Index i1=0; i1 void PointCollisionModel::computeBoundingTree(int maxDepth) { CubeCollisionModel* cubeModel = createPrevious(); - const auto npoints = mstate->getSize(); + const auto npoints = this->mstate->getSize(); bool updated = false; if (npoints != size) { @@ -130,7 +124,7 @@ void PointCollisionModel::computeBoundingTree(int maxDepth) cubeModel->resize(size); if (!empty()) { - //VecCoord& x =mstate->read(core::vec_id::read_access::position)->getValue(); + //VecCoord& x =this->mstate->read(core::vec_id::read_access::position)->getValue(); const SReal distance = this->d_contactDistance.getValue(); for (sofa::Size i=0; i void PointCollisionModel::computeContinuousBoundingTree(SReal dt, ContinuousIntersectionTypeFlag continuousIntersectionFlag , int maxDepth) { CubeCollisionModel* cubeModel = createPrevious(); - const auto npoints = mstate->getSize(); + const auto npoints = this->mstate->getSize(); bool updated = false; if (npoints != size) { @@ -162,8 +156,8 @@ void PointCollisionModel::computeContinuousBoundingTree(SReal dt, Con cubeModel->resize(size); if (!empty()) { - //VecCoord& x =mstate->read(core::vec_id::read_access::position)->getValue(); - //VecDeriv& v = mstate->read(core::vec_id::read_access::velocity)->getValue(); + //VecCoord& x =this->mstate->read(core::vec_id::read_access::position)->getValue(); + //VecDeriv& v = this->mstate->read(core::vec_id::read_access::velocity)->getValue(); const SReal distance = (SReal)this->d_contactDistance.getValue(); for (sofa::Size i=0; i::updateNormals() { normals[i].clear(); } - core::topology::BaseMeshTopology* mesh = l_topology.get(); - if (mesh->getNbTetrahedra()+mesh->getNbHexahedra() > 0) + if (!l_topology) + return; + + if (l_topology->getNbTetrahedra()+l_topology->getNbHexahedra() > 0) { - if (mesh->getNbTetrahedra()>0) + if (l_topology->getNbTetrahedra()>0) { - const core::topology::BaseMeshTopology::SeqTetrahedra &elems = mesh->getTetrahedra(); + const core::topology::BaseMeshTopology::SeqTetrahedra &elems = l_topology->getTetrahedra(); for (sofa::Index i=0; i < elems.size(); ++i) { const core::topology::BaseMeshTopology::Tetra &e = elems[i]; @@ -235,11 +231,11 @@ void PointCollisionModel::updateNormals() } /// @todo Hexahedra } - else if (mesh->getNbTriangles()+mesh->getNbQuads() > 0) + else if (l_topology->getNbTriangles()+l_topology->getNbQuads() > 0) { - if (mesh->getNbTriangles()>0) + if (l_topology->getNbTriangles()>0) { - const core::topology::BaseMeshTopology::SeqTriangles &elems = mesh->getTriangles(); + const core::topology::BaseMeshTopology::SeqTriangles &elems = l_topology->getTriangles(); for (sofa::Index i=0; i < elems.size(); ++i) { const core::topology::BaseMeshTopology::Triangle &e = elems[i]; @@ -256,9 +252,9 @@ void PointCollisionModel::updateNormals() n3 += n; } } - if (mesh->getNbQuads()>0) + if (l_topology->getNbQuads()>0) { - const core::topology::BaseMeshTopology::SeqQuads &elems = mesh->getQuads(); + const core::topology::BaseMeshTopology::SeqQuads &elems = l_topology->getQuads(); for (sofa::Index i=0; i < elems.size(); ++i) { const core::topology::BaseMeshTopology::Quad &e = elems[i]; @@ -297,7 +293,7 @@ void PointCollisionModel::computeBBox(const core::ExecParams* params, if( onlyVisible && !sofa::core::visual::VisualParams::defaultInstance()->displayFlags().getShowCollisionModels()) return; - const auto npoints = mstate->getSize(); + const auto npoints = this->mstate->getSize(); if (npoints != size) return; @@ -332,7 +328,7 @@ void PointCollisionModel::drawCollisionModel(const core::visual::Visu } // Check topological modifications - const auto npoints = mstate->getSize(); + const auto npoints = this->mstate->getSize(); if (npoints != size) return; std::vector pointsP; diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/RayCollisionModel.cpp b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/RayCollisionModel.cpp index 08ec23ec121..55065dbbcfa 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/RayCollisionModel.cpp +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/RayCollisionModel.cpp @@ -68,19 +68,12 @@ void RayCollisionModel::resize(sofa::Size size) void RayCollisionModel::init() { - this->CollisionModel::init(); + Inherit2::init(); - mstate = dynamic_cast< core::behavior::MechanicalState* > (getContext()->getMechanicalState()); - if (mstate==nullptr) - { - msg_error() << "RayCollisionModel requires a Vec3 Mechanical Model"; + if (d_componentState.getValue() == sofa::core::objectmodel::ComponentState::Invalid) return; - } - { - const int npoints = mstate->getSize(); - resize(npoints); - } + resize(this->mstate->getSize()); } diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/RayCollisionModel.h b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/RayCollisionModel.h index 5e492069de7..632b201720b 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/RayCollisionModel.h +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/RayCollisionModel.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -52,10 +53,10 @@ class SOFA_COMPONENT_COLLISION_GEOMETRY_API Ray : public core::TCollisionElement void setL(SReal newL); }; -class SOFA_COMPONENT_COLLISION_GEOMETRY_API RayCollisionModel : public core::CollisionModel +class SOFA_COMPONENT_COLLISION_GEOMETRY_API RayCollisionModel : public core::CollisionModel, public virtual core::behavior::SingleStateAccessor { public: - SOFA_CLASS(RayCollisionModel, core::CollisionModel); + SOFA_CLASS2(RayCollisionModel, core::CollisionModel, SOFA_TEMPLATE(core::behavior::SingleStateAccessor, defaulttype::Vec3Types)); typedef sofa::defaulttype::Vec3Types InDataTypes; typedef sofa::defaulttype::Vec3Types DataTypes; @@ -88,14 +89,13 @@ class SOFA_COMPONENT_COLLISION_GEOMETRY_API RayCollisionModel : public core::Col virtual const std::set &getContacts() const { return contacts;} + Data d_defaultLength; ///< The default length for all rays in this collision model + protected: sofa::type::vector length; sofa::type::vector direction; - Data d_defaultLength; ///< The default length for all rays in this collision model - std::set contacts; - core::behavior::MechanicalState* mstate; }; diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/SphereCollisionModel.h b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/SphereCollisionModel.h index 6ba48175cde..80ca4a43642 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/SphereCollisionModel.h +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/SphereCollisionModel.h @@ -24,7 +24,7 @@ #include #include -#include +#include #include namespace sofa::component::collision::geometry @@ -80,10 +80,10 @@ sofa::type::Vec3 TSphere::getContactPointWithSurfacePoi template< class TDataTypes> -class SphereCollisionModel : public core::CollisionModel +class SphereCollisionModel : public core::CollisionModel, public virtual core::behavior::SingleStateAccessor { public: - SOFA_CLASS(SOFA_TEMPLATE(SphereCollisionModel, TDataTypes), core::CollisionModel); + SOFA_CLASS2(SOFA_TEMPLATE(SphereCollisionModel, TDataTypes), core::CollisionModel, SOFA_TEMPLATE(core::behavior::SingleStateAccessor, TDataTypes)); typedef TDataTypes DataTypes; typedef DataTypes InDataTypes; @@ -114,7 +114,7 @@ class SphereCollisionModel : public core::CollisionModel void draw(const core::visual::VisualParams*, sofa::Index index) override; - core::behavior::MechanicalState* getMechanicalState() { return mstate; } + core::behavior::MechanicalState* getMechanicalState() { return this->mstate; } const VecReal& getR() const { return this->d_radius.getValue(); } @@ -166,12 +166,10 @@ class SphereCollisionModel : public core::CollisionModel Data< SReal > d_defaultRadius; ///< Default radius Data< bool > d_showImpostors; ///< Draw spheres as impostors instead of "real" spheres + /// Link to be set to the topology container in the component graph. + SingleLink, sofa::core::topology::BaseMeshTopology, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_topology; void computeBBox(const core::ExecParams* params, bool onlyVisible=false) override; - -protected: - core::behavior::MechanicalState* mstate; - SingleLink, sofa::core::topology::BaseMeshTopology, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_topology; }; template @@ -198,7 +196,7 @@ template inline const typename TSphere::Coord& TSphere::pFree() const { return DataTypes::getCPos((*this->model->mstate->read(core::vec_id::read_access::freePosition)).getValue()[this->index]); } template -inline const typename SphereCollisionModel::Coord& SphereCollisionModel::velocity(sofa::Index index) const { return DataTypes::getDPos(mstate->read(core::vec_id::read_access::velocity)->getValue()[index]);} +inline const typename SphereCollisionModel::Coord& SphereCollisionModel::velocity(sofa::Index index) const { return DataTypes::getDPos(this->mstate->read(core::vec_id::read_access::velocity)->getValue()[index]);} template inline const typename TSphere::Coord& TSphere::v() const { return DataTypes::getDPos(this->model->mstate->read(core::vec_id::read_access::velocity)->getValue()[this->index]); } diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/SphereCollisionModel.inl b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/SphereCollisionModel.inl index f1c9370aba8..aae95278595 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/SphereCollisionModel.inl +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/SphereCollisionModel.inl @@ -37,17 +37,16 @@ SphereCollisionModel::SphereCollisionModel() : d_radius(initData(&d_radius, "listRadius", "Radius of each sphere")) , d_defaultRadius(initData(&d_defaultRadius, (SReal)(1.0), "radius", "Default radius")) , d_showImpostors(initData(&d_showImpostors, true, "showImpostors", "Draw spheres as impostors instead of \"real\" spheres")) - , mstate(nullptr) { enum_type = SPHERE_TYPE; } template -SphereCollisionModel::SphereCollisionModel(core::behavior::MechanicalState* _mstate ) - : d_radius(initData(&d_radius, "listRadius", "Radius of each sphere")) +SphereCollisionModel::SphereCollisionModel(core::behavior::MechanicalState* ms ) + : core::behavior::SingleStateAccessor(ms) + , d_radius(initData(&d_radius, "listRadius", "Radius of each sphere")) , d_defaultRadius(initData(&d_defaultRadius, (SReal)(1.0), "radius", "Default radius")) , d_showImpostors(initData(&d_showImpostors, true, "showImpostors", "Draw spheres as impostors instead of \"real\" spheres")) - , mstate(_mstate) { enum_type = SPHERE_TYPE; } @@ -81,24 +80,13 @@ void SphereCollisionModel::init() msg_warning() << "Calling an already fully initialized component. You should use reinit instead." ; } - this->CollisionModel::init(); - mstate = dynamic_cast< core::behavior::MechanicalState* > (getContext()->getMechanicalState()); - if (mstate==nullptr) - { - //TODO(dmarchal): The previous message was saying this only work for a vec3 mechanicalstate but there - // it seems that a mechanicalstate will work we well...where is the truth ? - msg_error(this) << "Missing a MechanicalObject with template '" << DataTypes::Name() << ". " - "This MechnicalObject stores the position of the spheres. When this one is missing the collision model is deactivated. \n" - "To remove this error message you can add to your scene a line . "; - d_componentState.setValue(ComponentState::Invalid) ; + Inherit2::init(); + if (d_componentState.getValue() == ComponentState::Invalid) return; - } - const auto npoints = mstate->getSize(); + const auto npoints = this->mstate->getSize(); resize(npoints); - - d_componentState.setValue(ComponentState::Valid) ; } @@ -124,7 +112,7 @@ void SphereCollisionModel::drawCollisionModel(const core::visual::Vis vparams->drawTool()->setPolygonMode(0, false); // Check topological modifications - const auto npoints = mstate->getSize(); + const auto npoints = this->mstate->getSize(); std::vector points; std::vector radius; @@ -162,7 +150,7 @@ void SphereCollisionModel::computeBoundingTree(int maxDepth) return ; CubeCollisionModel* cubeModel = createPrevious(); - const auto npoints = mstate->getSize(); + const auto npoints = this->mstate->getSize(); bool updated = false; if (npoints != size) { @@ -202,7 +190,7 @@ void SphereCollisionModel::computeContinuousBoundingTree(SReal dt, Co return ; CubeCollisionModel* cubeModel = createPrevious(); - const auto npoints = mstate->getSize(); + const auto npoints = this->mstate->getSize(); bool updated = false; if (npoints != size) { @@ -261,7 +249,7 @@ void SphereCollisionModel::computeBBox(const core::ExecParams* params if( onlyVisible && !sofa::core::visual::VisualParams::defaultInstance()->displayFlags().getShowCollisionModels()) return; - const auto npoints = mstate->getSize(); + const auto npoints = this->mstate->getSize(); type::BoundingBox bbox; for(sofa::Size i = 0 ; i < npoints ; ++i ) diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TetrahedronCollisionModel.cpp b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TetrahedronCollisionModel.cpp index ca88ef0f133..c4f72aef3fb 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TetrahedronCollisionModel.cpp +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TetrahedronCollisionModel.cpp @@ -42,7 +42,6 @@ void registerTetrahedronCollisionModel(sofa::core::ObjectFactory* factory) TetrahedronCollisionModel::TetrahedronCollisionModel() : tetra(nullptr) - , mstate(nullptr) , m_topology(nullptr) , m_topologyRevision(-1) , l_topology(initLink("topology", "link to the topology container")) @@ -59,6 +58,8 @@ void TetrahedronCollisionModel::resize(sofa::Size size) void TetrahedronCollisionModel::init() { + Inherit2::init(); + if (l_topology.empty()) { msg_info() << "link to Topology container should be set to ensure right behavior. First Topology found in current context will be used."; @@ -75,15 +76,6 @@ void TetrahedronCollisionModel::init() return; } - this->CollisionModel::init(); - mstate = dynamic_cast< core::behavior::MechanicalState* > (getContext()->getMechanicalState()); - - if (mstate==nullptr) - { - msg_error() << "TetrahedronCollisionModel requires a Vec3 Mechanical Model"; - return; - } - updateFromTopology(); } diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TetrahedronCollisionModel.h b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TetrahedronCollisionModel.h index 5c93d0fb484..177793a8e11 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TetrahedronCollisionModel.h +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TetrahedronCollisionModel.h @@ -23,7 +23,7 @@ #include #include -#include +#include #include #include @@ -69,10 +69,10 @@ class Tetrahedron : public core::TCollisionElementIterator { public: - SOFA_CLASS(TetrahedronCollisionModel, core::CollisionModel); + SOFA_CLASS2(TetrahedronCollisionModel, core::CollisionModel, SOFA_TEMPLATE(core::behavior::SingleStateAccessor, defaulttype::Vec3Types)); typedef defaulttype::Vec3Types InDataTypes; typedef defaulttype::Vec3Types DataTypes; @@ -94,14 +94,11 @@ class SOFA_COMPONENT_COLLISION_GEOMETRY_API TetrahedronCollisionModel : public c sofa::type::vector elems; const sofa::core::topology::BaseMeshTopology::SeqTetrahedra* tetra; - core::behavior::MechanicalState* mstate; sofa::core::topology::BaseMeshTopology* m_topology; int m_topologyRevision; ///< internal revision number to check if topology has changed. -protected: - TetrahedronCollisionModel(); virtual void updateFromTopology(); diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleCollisionModel.h b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleCollisionModel.h index 712c6db49f6..eb5579bfbac 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleCollisionModel.h +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleCollisionModel.h @@ -27,7 +27,7 @@ #include #include #include -#include +#include namespace sofa::component::collision::geometry { @@ -102,10 +102,10 @@ using Triangle = TTriangle; * The class \sa TTriangle is used to access specific triangle of this collision Model. */ template -class TriangleCollisionModel : public core::CollisionModel +class TriangleCollisionModel : public core::CollisionModel, public virtual core::behavior::SingleStateAccessor { public: - SOFA_CLASS(SOFA_TEMPLATE(TriangleCollisionModel, TDataTypes), core::CollisionModel); + SOFA_CLASS2(SOFA_TEMPLATE(TriangleCollisionModel, TDataTypes), core::CollisionModel, SOFA_TEMPLATE(core::behavior::SingleStateAccessor, TDataTypes)); typedef TDataTypes DataTypes; typedef DataTypes InDataTypes; @@ -143,7 +143,6 @@ class TriangleCollisionModel : public core::CollisionModel SingleLink, sofa::core::topology::BaseMeshTopology, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_topology; protected: - core::behavior::MechanicalState* m_mstate; ///< Pointer to the corresponding MechanicalState sofa::core::topology::BaseMeshTopology* m_topology; ///< Pointer to the corresponding Topology VecDeriv m_normals; ///< Vector of normal direction per triangle. @@ -185,8 +184,8 @@ class TriangleCollisionModel : public core::CollisionModel bool canCollideWithElement(sofa::Index index, CollisionModel* model2, sofa::Index index2) override; - core::behavior::MechanicalState* getMechanicalState() { return m_mstate; } - const core::behavior::MechanicalState* getMechanicalState() const { return m_mstate; } + core::behavior::MechanicalState* getMechanicalState() { return this->mstate; } + const core::behavior::MechanicalState* getMechanicalState() const { return this->mstate; } const VecCoord& getX() const { return(getMechanicalState()->read(core::vec_id::read_access::position)->getValue()); } const sofa::core::topology::BaseMeshTopology::SeqTriangles& getTriangles() const { return *m_triangles; } diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleCollisionModel.inl b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleCollisionModel.inl index f2bc2b681ef..78a8aaf59f7 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleCollisionModel.inl +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleCollisionModel.inl @@ -39,7 +39,6 @@ TriangleCollisionModel::TriangleCollisionModel() , d_computeNormals(initData(&d_computeNormals, true, "computeNormals", "set to false to disable computation of triangles normal")) , d_useCurvature(initData(&d_useCurvature, false, "useCurvature", "use the curvature of the mesh to avoid some self-intersection test")) , l_topology(initLink("topology", "link to the topology container")) - , m_mstate(nullptr) , m_topology(nullptr) , m_needsUpdate(true) , m_topologyRevision(-1) @@ -59,6 +58,8 @@ void TriangleCollisionModel::resize(sofa::Size size) template void TriangleCollisionModel::init() { + Inherit2::init(); + if (l_topology.empty()) { msg_info() << "link to Topology container should be set to ensure right behavior. First Topology found in current context will be used."; @@ -75,23 +76,10 @@ void TriangleCollisionModel::init() return; } - // TODO epernod 2019-01-21: Check if this call super is needed. - this->CollisionModel::init(); - m_mstate = dynamic_cast< core::behavior::MechanicalState* > (this->getContext()->getMechanicalState()); - this->getContext()->get(m_pointModels); - // Check object pointer access - bool modelsOk = true; - if (m_mstate == nullptr) - { - msg_error() << "No MechanicalState found. TriangleCollisionModel requires a Vec3 MechanicalState in the same Node."; - modelsOk = false; - } - - if (!modelsOk) + if (this->d_componentState.getValue() == sofa::core::objectmodel::ComponentState::Invalid) { - this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid); return; } @@ -145,7 +133,7 @@ void TriangleCollisionModel::updateFromTopology() else { const sofa::Size newsize = ntris+2*nquads; - const sofa::Size npoints = m_mstate->getSize(); + const sofa::Size npoints = this->mstate->getSize(); m_triangles = &m_internalTriangles; m_internalTriangles.resize(newsize); @@ -232,7 +220,7 @@ void TriangleCollisionModel::computeBoundingTree(int maxDepth) m_needsUpdate=false; type::Vec3 minElem, maxElem; - const VecCoord& x = this->m_mstate->read(core::vec_id::read_access::position)->getValue(); + const VecCoord& x = this->mstate->read(core::vec_id::read_access::position)->getValue(); const bool calcNormals = d_computeNormals.getValue(); @@ -384,7 +372,7 @@ void TriangleCollisionModel::computeBBox(const core::ExecParams* para if (m_topology->getRevision() != m_topologyRevision) updateFromTopology(); - const auto& positions = this->m_mstate->read(core::vec_id::read_access::position)->getValue(); + const auto& positions = this->mstate->read(core::vec_id::read_access::position)->getValue(); type::BoundingBox bbox; for(const auto& triangle : (*this->m_triangles)) @@ -463,26 +451,26 @@ void TriangleCollisionModel::drawCollisionModel(const core::visual::V } template -inline const typename DataTypes::Coord& TTriangle::p1() const { return this->model->m_mstate->read(core::vec_id::read_access::position)->getValue()[(*(this->model->m_triangles))[this->index][0]]; } +inline const typename DataTypes::Coord& TTriangle::p1() const { return this->model->mstate->read(core::vec_id::read_access::position)->getValue()[(*(this->model->m_triangles))[this->index][0]]; } template -inline const typename DataTypes::Coord& TTriangle::p2() const { return this->model->m_mstate->read(core::vec_id::read_access::position)->getValue()[(*(this->model->m_triangles))[this->index][1]]; } +inline const typename DataTypes::Coord& TTriangle::p2() const { return this->model->mstate->read(core::vec_id::read_access::position)->getValue()[(*(this->model->m_triangles))[this->index][1]]; } template -inline const typename DataTypes::Coord& TTriangle::p3() const { return this->model->m_mstate->read(core::vec_id::read_access::position)->getValue()[(*(this->model->m_triangles))[this->index][2]]; } +inline const typename DataTypes::Coord& TTriangle::p3() const { return this->model->mstate->read(core::vec_id::read_access::position)->getValue()[(*(this->model->m_triangles))[this->index][2]]; } template inline const typename DataTypes::Coord& TTriangle::p(Index i) const { - return this->model->m_mstate->read(core::vec_id::read_access::position)->getValue()[(*(this->model->m_triangles))[this->index][i]]; + return this->model->mstate->read(core::vec_id::read_access::position)->getValue()[(*(this->model->m_triangles))[this->index][i]]; } template inline const typename DataTypes::Coord& TTriangle::operator[](Index i) const { - return this->model->m_mstate->read(core::vec_id::read_access::position)->getValue()[(*(this->model->m_triangles))[this->index][i]]; + return this->model->mstate->read(core::vec_id::read_access::position)->getValue()[(*(this->model->m_triangles))[this->index][i]]; } template -inline const typename DataTypes::Coord& TTriangle::p1Free() const { return (this->model->m_mstate->read(sofa::core::vec_id::read_access::freePosition)->getValue())[(*(this->model->m_triangles))[this->index][0]]; } +inline const typename DataTypes::Coord& TTriangle::p1Free() const { return (this->model->mstate->read(sofa::core::vec_id::read_access::freePosition)->getValue())[(*(this->model->m_triangles))[this->index][0]]; } template -inline const typename DataTypes::Coord& TTriangle::p2Free() const { return (this->model->m_mstate->read(sofa::core::vec_id::read_access::freePosition)->getValue())[((*this->model->m_triangles))[this->index][1]]; } +inline const typename DataTypes::Coord& TTriangle::p2Free() const { return (this->model->mstate->read(sofa::core::vec_id::read_access::freePosition)->getValue())[((*this->model->m_triangles))[this->index][1]]; } template -inline const typename DataTypes::Coord& TTriangle::p3Free() const { return (this->model->m_mstate->read(sofa::core::vec_id::read_access::freePosition)->getValue())[(*(this->model->m_triangles))[this->index][2]]; } +inline const typename DataTypes::Coord& TTriangle::p3Free() const { return (this->model->mstate->read(sofa::core::vec_id::read_access::freePosition)->getValue())[(*(this->model->m_triangles))[this->index][2]]; } template inline typename TTriangle::Index TTriangle::p1Index() const { return (*(this->model->m_triangles))[this->index][0]; } @@ -492,13 +480,13 @@ template inline typename TTriangle::Index TTriangle::p3Index() const { return (*(this->model->m_triangles))[this->index][2]; } template -inline const typename DataTypes::Deriv& TTriangle::v1() const { return (this->model->m_mstate->read(core::vec_id::read_access::velocity)->getValue())[(*(this->model->m_triangles))[this->index][0]]; } +inline const typename DataTypes::Deriv& TTriangle::v1() const { return (this->model->mstate->read(core::vec_id::read_access::velocity)->getValue())[(*(this->model->m_triangles))[this->index][0]]; } template -inline const typename DataTypes::Deriv& TTriangle::v2() const { return this->model->m_mstate->read(core::vec_id::read_access::velocity)->getValue()[(*(this->model->m_triangles))[this->index][1]]; } +inline const typename DataTypes::Deriv& TTriangle::v2() const { return this->model->mstate->read(core::vec_id::read_access::velocity)->getValue()[(*(this->model->m_triangles))[this->index][1]]; } template -inline const typename DataTypes::Deriv& TTriangle::v3() const { return this->model->m_mstate->read(core::vec_id::read_access::velocity)->getValue()[(*(this->model->m_triangles))[this->index][2]]; } +inline const typename DataTypes::Deriv& TTriangle::v3() const { return this->model->mstate->read(core::vec_id::read_access::velocity)->getValue()[(*(this->model->m_triangles))[this->index][2]]; } template -inline const typename DataTypes::Deriv& TTriangle::v(Index i) const { return this->model->m_mstate->read(core::vec_id::read_access::velocity)->getValue()[(*(this->model->m_triangles))[this->index][i]]; } +inline const typename DataTypes::Deriv& TTriangle::v(Index i) const { return this->model->mstate->read(core::vec_id::read_access::velocity)->getValue()[(*(this->model->m_triangles))[this->index][i]]; } template inline const typename DataTypes::Deriv& TTriangle::n() const { return this->model->m_normals[this->index]; } @@ -509,11 +497,11 @@ template inline int TTriangle::flags() const { return this->model->getTriangleFlags(this->index); } template -inline bool TTriangle::hasFreePosition() const { return this->model->m_mstate->read(core::vec_id::read_access::freePosition)->isSet(); } +inline bool TTriangle::hasFreePosition() const { return this->model->mstate->read(core::vec_id::read_access::freePosition)->isSet(); } template -inline typename DataTypes::Deriv TriangleCollisionModel::velocity(sofa::Index index) const { return (m_mstate->read(core::vec_id::read_access::velocity)->getValue()[(*(m_triangles))[index][0]] + m_mstate->read(core::vec_id::read_access::velocity)->getValue()[(*(m_triangles))[index][1]] + - m_mstate->read(core::vec_id::read_access::velocity)->getValue()[(*(m_triangles))[index][2]])/((Real)(3.0)); } +inline typename DataTypes::Deriv TriangleCollisionModel::velocity(sofa::Index index) const { return (this->mstate->read(core::vec_id::read_access::velocity)->getValue()[(*(m_triangles))[index][0]] + this->mstate->read(core::vec_id::read_access::velocity)->getValue()[(*(m_triangles))[index][1]] + + this->mstate->read(core::vec_id::read_access::velocity)->getValue()[(*(m_triangles))[index][2]])/((Real)(3.0)); } } //namespace sofa::component::collision::geometry diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleModelInRegularGrid.cpp b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleModelInRegularGrid.cpp index 31912c20506..d6bb3843d42 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleModelInRegularGrid.cpp +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleModelInRegularGrid.cpp @@ -61,9 +61,8 @@ void TriangleModelInRegularGrid::init() TriangleCollisionModel::init(); _topology = this->getContext()->getMeshTopology(); - m_mstate = dynamic_cast< core::behavior::MechanicalState* > (getContext()->getMechanicalState()); - if (!m_mstate) { msg_error() << "TriangleModelInRegularGrid requires a Vec3 Mechanical Model"; return; } + if (this->d_componentState.getValue() == sofa::core::objectmodel::ComponentState::Invalid) return; if (!m_topology) { msg_error() << "TriangleModelInRegularGrid requires a BaseMeshTopology"; return; } // Test if _topology depend on an higher topology (to compute Bounding Tree faster) and get it @@ -71,7 +70,7 @@ void TriangleModelInRegularGrid::init() vector topoVec; getContext()->get ( &topoVec, core::objectmodel::BaseContext::SearchRoot ); _higher_topo = m_topology; - _higher_mstate = m_mstate; + _higher_mstate = this->mstate; bool found = true; while ( found ) { @@ -111,7 +110,7 @@ void TriangleModelInRegularGrid::computeBoundingTree ( int ) m_needsUpdate=false; Vec3 minElem, maxElem; const VecCoord& xHigh =_higher_mstate->read(core::vec_id::read_access::position)->getValue(); - const VecCoord& x =m_mstate->read(core::vec_id::read_access::position)->getValue(); + const VecCoord& x =this->mstate->read(core::vec_id::read_access::position)->getValue(); // no hierarchy if ( empty() ) diff --git a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleOctreeCollisionModel.cpp b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleOctreeCollisionModel.cpp index a6f9019aff2..b83813b638a 100644 --- a/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleOctreeCollisionModel.cpp +++ b/Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleOctreeCollisionModel.cpp @@ -83,16 +83,16 @@ void TriangleOctreeCollisionModel::computeBoundingTree(int maxDepth) updateFromTopology(); if (!isMoving() && !cubeModel->empty()) return; // No need to recompute BBox if immobile - const std::size_t size2=m_mstate->getSize(); + const std::size_t size2=this->mstate->getSize(); pNorms.resize(size2); for(sofa::Size i=0; iread(core::vec_id::read_access::position)->getValue()[0][0]; - maxElem[1]=minElem[1]=m_mstate->read(core::vec_id::read_access::position)->getValue()[0][1]; - maxElem[2]=minElem[2]=m_mstate->read(core::vec_id::read_access::position)->getValue()[0][2]; + maxElem[0]=minElem[0]=this->mstate->read(core::vec_id::read_access::position)->getValue()[0][0]; + maxElem[1]=minElem[1]=this->mstate->read(core::vec_id::read_access::position)->getValue()[0][1]; + maxElem[2]=minElem[2]=this->mstate->read(core::vec_id::read_access::position)->getValue()[0][2]; cubeModel->resize(1); // size = number of triangles for (std::size_t i=1; i