Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include <sofa/core/CollisionModel.h>
#include <sofa/defaulttype/VecTypes.h>
#include <sofa/core/behavior/MechanicalState.h>
#include <sofa/core/behavior/SingleStateAccessor.h>

namespace sofa::component::collision::geometry
{
Expand Down Expand Up @@ -71,10 +71,10 @@ using Cylinder = TCylinder<sofa::defaulttype::Rigid3Types>;
*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<TDataTypes>
{
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;
Expand All @@ -99,7 +99,7 @@ class CylinderCollisionModel : public core::CollisionModel

protected:
CylinderCollisionModel();
CylinderCollisionModel(core::behavior::MechanicalState<DataTypes>* mstate );
CylinderCollisionModel(core::behavior::MechanicalState<DataTypes>* _mstate );

public:
void init() override;
Expand All @@ -111,7 +111,7 @@ class CylinderCollisionModel : public core::CollisionModel

void draw(const core::visual::VisualParams* vparams,sofa::Index index) override;

core::behavior::MechanicalState<DataTypes>* getMechanicalState() { return m_mstate; }
core::behavior::MechanicalState<DataTypes>* getMechanicalState() { return this->mstate; }

Real radius(sofa::Index index) const;

Expand All @@ -138,8 +138,6 @@ class CylinderCollisionModel : public core::CollisionModel
Data<VecReal>& writeHeights();
Data<VecAxisCoord>& writeLocalAxes();

protected:
core::behavior::MechanicalState<DataTypes>* m_mstate;
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@ CylinderCollisionModel<DataTypes>::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<class DataTypes>
CylinderCollisionModel<DataTypes>::CylinderCollisionModel(core::behavior::MechanicalState<DataTypes>* mstate)
CylinderCollisionModel<DataTypes>::CylinderCollisionModel(core::behavior::MechanicalState<DataTypes>* ms)
: CylinderCollisionModel()
{
m_mstate = mstate;
this->mstate = ms;
enum_type = CYLINDER_TYPE;
}

Expand Down Expand Up @@ -101,16 +100,12 @@ void CylinderCollisionModel<DataTypes>::resize(sofa::Size size)
template<class DataTypes>
void CylinderCollisionModel<DataTypes>::init()
{
this->CollisionModel::init();
m_mstate = dynamic_cast< core::behavior::MechanicalState<DataTypes>* > (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());
}


Expand All @@ -120,7 +115,7 @@ void CylinderCollisionModel<DataTypes>::computeBoundingTree(int maxDepth)
using namespace sofa::type;
using namespace sofa::defaulttype;
CubeCollisionModel* cubeModel = createPrevious<CubeCollisionModel>();
const auto ncyl = m_mstate->getSize();
const auto ncyl = this->mstate->getSize();

bool updated = false;
if (ncyl != size)
Expand Down Expand Up @@ -198,7 +193,7 @@ typename CylinderCollisionModel<DataTypes>::Real CylinderCollisionModel< DataTyp

template<class DataTypes>
const typename CylinderCollisionModel<DataTypes>::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<class DataTypes>
Expand Down Expand Up @@ -240,7 +235,7 @@ typename TCylinder<DataTypes>::Real TCylinder<DataTypes >::radius() const

template<class DataTypes>
const typename CylinderCollisionModel<DataTypes>::Coord & CylinderCollisionModel<DataTypes >::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]);
}


Expand All @@ -249,7 +244,7 @@ const typename TCylinder<DataTypes>::Coord & TCylinder<DataTypes >::v() const {r

template<class DataTypes>
const sofa::type::Quat<SReal> CylinderCollisionModel<DataTypes >::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<class DataTypes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <sofa/component/collision/geometry/config.h>
#include <sofa/core/fwd.h>
#include <sofa/core/CollisionModel.h>
#include <sofa/core/behavior/SingleStateAccessor.h>
#include <sofa/core/objectmodel/BaseComponent.h>
#include <sofa/core/topology/BaseMeshTopology.h>
#include <sofa/core/collision/Intersection.h>
Expand Down Expand Up @@ -73,10 +74,10 @@ class TLine : public core::TCollisionElementIterator<LineCollisionModel<TDataTyp
using Line = TLine<sofa::defaulttype::Vec3Types>;

template<class TDataTypes>
class LineCollisionModel : public core::CollisionModel
class LineCollisionModel : public core::CollisionModel, public virtual core::behavior::SingleStateAccessor<TDataTypes>
{
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
{
Expand Down Expand Up @@ -130,7 +131,7 @@ public :

bool canCollideWithElement(sofa::Index index, CollisionModel* model2, sofa::Index index2) override;

core::behavior::MechanicalState<DataTypes>* getMechanicalState() { return mstate; }
core::behavior::MechanicalState<DataTypes>* getMechanicalState() { return this->mstate; }

Deriv velocity(sofa::Index index)const;

Expand All @@ -139,6 +140,10 @@ public :
int getLineFlags(sofa::Index i);

Data<bool> d_bothSide; ///< activate collision on both side of the line model (when surface normals are defined on these lines)
Data<bool> d_displayFreePosition; ///< Display Collision Model Points free position(in green)

/// Link to be set to the topology container in the component graph.
SingleLink<LineCollisionModel<DataTypes>, 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.
Expand All @@ -161,14 +166,7 @@ public :

void computeBBox(const core::ExecParams* params, bool onlyVisible) override;

Data<bool> d_displayFreePosition; ///< Display Collision Model Points free position(in green)

/// Link to be set to the topology container in the component graph.
SingleLink<LineCollisionModel<DataTypes>, sofa::core::topology::BaseMeshTopology, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_topology;

protected:
core::behavior::MechanicalState<DataTypes>* mstate;
Topology* topology;
PointCollisionModel<sofa::defaulttype::Vec3Types>* mpoints;
int meshRevision;
};
Expand Down
Loading
Loading