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 @@ -26,6 +26,8 @@
#include <sofa/core/objectmodel/BaseObject.h>
#include <sofa/core/topology/BaseMeshTopology.h>
#include <sofa/core/collision/Intersection.h>
#include <sofa/core/behavior/SingleStateAccessor.h>
#include <sofa/core/behavior/TopologyAccessor.h>


namespace sofa::component::collision::geometry
Expand Down Expand Up @@ -73,10 +75,16 @@ 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 virtual core::behavior::TopologyAccessor
{
public :
SOFA_CLASS(SOFA_TEMPLATE(LineCollisionModel, TDataTypes), core::CollisionModel);
public:
SOFA_CLASS3(SOFA_TEMPLATE(LineCollisionModel, TDataTypes),
core::CollisionModel,
core::behavior::SingleStateAccessor<TDataTypes>,
core::behavior::TopologyAccessor);

enum LineFlag
{
Expand Down Expand Up @@ -130,7 +138,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.get(); }

Deriv velocity(sofa::Index index)const;

Expand Down Expand Up @@ -163,12 +171,9 @@ public :

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;
using sofa::core::behavior::SingleStateAccessor<TDataTypes>::mstate;
using sofa::core::behavior::TopologyAccessor::l_topology;
PointCollisionModel<sofa::defaulttype::Vec3Types>* mpoints;
int meshRevision;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
******************************************************************************/
#pragma once
#include <sofa/component/collision/geometry/LineCollisionModel.h>
#include <sofa/core/behavior/MechanicalState.h>
#include <sofa/component/collision/geometry/PointCollisionModel.h>
#include <sofa/core/visual/VisualParams.h>
#include <sofa/component/collision/geometry/CubeCollisionModel.h>
#include <sofa/core/CollisionElement.h>
#include <sofa/simulation/Node.h>
#include <sofa/core/topology/TopologyChange.h>
#include <vector>
#include <sofa/core/behavior/SingleStateAccessor.inl>

namespace sofa::component::collision::geometry
{
Expand All @@ -39,8 +39,7 @@ template<class DataTypes>
LineCollisionModel<DataTypes>::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)") )
, 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;
}
Expand All @@ -57,21 +56,24 @@ template<class DataTypes>
void LineCollisionModel<DataTypes>::init()
{
this->CollisionModel::init();
mstate = dynamic_cast< core::behavior::MechanicalState<DataTypes>* > (getContext()->getMechanicalState());
this->getContext()->get(mpoints);

if (mstate==nullptr)
if (!this->isComponentStateInvalid())
{
msg_error() << "LineModel requires a Vec3 Mechanical Model";
return;
this->validateMState();
}

if (!this->isComponentStateInvalid())
{
this->validateTopology();
}

if (l_topology.empty())
if (this->isComponentStateInvalid())
{
msg_info() << "link to Topology container should be set to ensure right behavior. First Topology found in current context will be used.";
l_topology.set(this->getContext()->getMeshTopologyLink());
return;
}

this->getContext()->get(mpoints);

core::topology::BaseMeshTopology *bmt = l_topology.get();
msg_info() << "Topology path used: '" << l_topology.getLinkedPath() << "'";

Expand Down Expand Up @@ -541,15 +543,15 @@ template<class DataTypes>
int LineCollisionModel<DataTypes>::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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <sofa/core/behavior/MechanicalState.h>
#include <sofa/core/topology/BaseMeshTopology.h>
#include <sofa/defaulttype/VecTypes.h>
#include <sofa/core/behavior/SingleStateAccessor.h>
#include <sofa/core/behavior/TopologyAccessor.h>

namespace sofa::component::collision::geometry
{
Expand Down Expand Up @@ -60,10 +62,16 @@ class TPoint : public core::TCollisionElementIterator<PointCollisionModel<TDataT
using Point = TPoint<sofa::defaulttype::Vec3Types>;

template<class TDataTypes>
class PointCollisionModel : public core::CollisionModel
class PointCollisionModel :
public core::CollisionModel,
public virtual core::behavior::SingleStateAccessor<TDataTypes>,
public virtual core::behavior::TopologyAccessor
{
public:
SOFA_CLASS(SOFA_TEMPLATE(PointCollisionModel, TDataTypes), core::CollisionModel);
SOFA_CLASS3(SOFA_TEMPLATE(PointCollisionModel, TDataTypes),
core::CollisionModel,
core::behavior::SingleStateAccessor<TDataTypes>,
core::behavior::TopologyAccessor);

typedef TDataTypes DataTypes;
typedef DataTypes InDataTypes;
Expand Down Expand Up @@ -129,16 +137,13 @@ class PointCollisionModel : public core::CollisionModel

protected:

core::behavior::MechanicalState<DataTypes>* mstate;
using sofa::core::behavior::SingleStateAccessor<TDataTypes>::mstate;

Data<bool> d_computeNormals; ///< activate computation of normal vectors (required for some collision detection algorithms)
Data<bool> 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<PointCollisionModel<DataTypes>, sofa::core::topology::BaseMeshTopology, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_topology;

};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@
#include <sofa/core/topology/BaseMeshTopology.h>
#include <sofa/simulation/Node.h>
#include <sofa/component/collision/geometry/CubeCollisionModel.h>
#include <sofa/core/behavior/SingleStateAccessor.inl>

namespace sofa::component::collision::geometry
{

template<class DataTypes>
PointCollisionModel<DataTypes>::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"))
{
enum_type = POINT_TYPE;
}
Expand All @@ -53,18 +52,20 @@ template<class DataTypes>
void PointCollisionModel<DataTypes>::init()
{
this->CollisionModel::init();
mstate = dynamic_cast< core::behavior::MechanicalState<DataTypes>* > (getContext()->getMechanicalState());

if (mstate==nullptr)
if (!this->isComponentStateInvalid())
{
msg_error() << "PointModel requires a Vec3 Mechanical Model";
return;
this->validateMState();
}

if (l_topology.empty())
if (!this->isComponentStateInvalid())
{
msg_info() << "link to Topology container should be set to ensure right behavior. First Topology found in current context will be used.";
l_topology.set(this->getContext()->getMeshTopologyLink());
this->validateTopology();
}

if (this->isComponentStateInvalid())
{
return;
}

const int npoints = mstate->getSize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,12 @@ void RayCollisionModel::init()
{
this->CollisionModel::init();

mstate = dynamic_cast< core::behavior::MechanicalState<Vec3Types>* > (getContext()->getMechanicalState());
if (mstate==nullptr)
if (!this->isComponentStateInvalid())
{
msg_error() << "RayCollisionModel requires a Vec3 Mechanical Model";
return;
this->validateMState();
}

if (!this->isComponentStateInvalid())
{
const int npoints = mstate->getSize();
resize(npoints);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <sofa/core/CollisionModel.h>
#include <sofa/defaulttype/VecTypes.h>
#include <set>
#include <sofa/core/behavior/SingleStateAccessor.h>

namespace sofa::component::collision::response::contact
{
Expand Down Expand Up @@ -52,10 +53,14 @@ 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 sofa::core::behavior::SingleStateAccessor<defaulttype::Vec3Types>
{
public:
SOFA_CLASS(RayCollisionModel, core::CollisionModel);
SOFA_CLASS2(RayCollisionModel,
core::CollisionModel,
sofa::core::behavior::SingleStateAccessor<defaulttype::Vec3Types>);

typedef sofa::defaulttype::Vec3Types InDataTypes;
typedef sofa::defaulttype::Vec3Types DataTypes;
Expand Down Expand Up @@ -95,7 +100,7 @@ class SOFA_COMPONENT_COLLISION_GEOMETRY_API RayCollisionModel : public core::Col
Data<SReal> d_defaultLength; ///< The default length for all rays in this collision model

std::set<response::contact::BaseRayContact*> contacts;
core::behavior::MechanicalState<defaulttype::Vec3Types>* mstate;
using sofa::core::behavior::SingleStateAccessor<defaulttype::Vec3Types>::mstate;

};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <sofa/defaulttype/VecTypes.h>
#include <sofa/core/behavior/MechanicalState.h>
#include <sofa/core/topology/BaseMeshTopology.h>
#include <sofa/core/behavior/SingleStateAccessor.h>

namespace sofa::component::collision::geometry
{
Expand Down Expand Up @@ -80,10 +81,14 @@ sofa::type::Vec3 TSphere<defaulttype::Vec3Types >::getContactPointWithSurfacePoi


template< class TDataTypes>
class SphereCollisionModel : public core::CollisionModel
class SphereCollisionModel :
public core::CollisionModel,
public virtual core::behavior::SingleStateAccessor<TDataTypes>
{
public:
SOFA_CLASS(SOFA_TEMPLATE(SphereCollisionModel, TDataTypes), core::CollisionModel);
SOFA_CLASS2(SOFA_TEMPLATE(SphereCollisionModel, TDataTypes),
core::CollisionModel,
core::behavior::SingleStateAccessor<TDataTypes>);

typedef TDataTypes DataTypes;
typedef DataTypes InDataTypes;
Expand Down Expand Up @@ -170,8 +175,7 @@ class SphereCollisionModel : public core::CollisionModel
void computeBBox(const core::ExecParams* params, bool onlyVisible=false) override;

protected:
core::behavior::MechanicalState<DataTypes>* mstate;
SingleLink<SphereCollisionModel<DataTypes>, sofa::core::topology::BaseMeshTopology, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_topology;
using sofa::core::behavior::SingleStateAccessor<TDataTypes>::mstate;
};

template<class DataTypes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <sofa/core/visual/VisualParams.h>
#include <sofa/component/collision/geometry/CubeCollisionModel.h>
#include <sofa/core/ObjectFactory.h>
#include <sofa/core/behavior/SingleStateAccessor.inl>

#include <sofa/core/objectmodel/BaseObject.h>
using sofa::core::objectmodel::ComponentState ;
Expand All @@ -37,7 +38,6 @@ SphereCollisionModel<DataTypes>::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;
}
Expand All @@ -47,7 +47,6 @@ SphereCollisionModel<DataTypes>::SphereCollisionModel(core::behavior::Mechanical
: 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;
}
Expand Down Expand Up @@ -81,24 +80,18 @@ void SphereCollisionModel<DataTypes>::init()
msg_warning() << "Calling an already fully initialized component. You should use reinit instead." ;
}

this->CollisionModel::init();
mstate = dynamic_cast< core::behavior::MechanicalState<DataTypes>* > (getContext()->getMechanicalState());
if (mstate==nullptr)
if (!this->isComponentStateInvalid())
{
//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 <MechanicalObject template='"<< DataTypes::Name() << "'/>. ";
d_componentState.setValue(ComponentState::Invalid) ;

return;
this->validateMState();
}

const auto npoints = mstate->getSize();
resize(npoints);
if (!this->isComponentStateInvalid())
{
const auto npoints = mstate->getSize();
resize(npoints);

d_componentState.setValue(ComponentState::Valid) ;
d_componentState.setValue(ComponentState::Valid) ;
}
}


Expand Down
Loading
Loading