Skip to content

Commit

Permalink
TopologySubsetIndices for RestShapeSpringsForceField (sofa-framework#…
Browse files Browse the repository at this point in the history
…3037)

* Changed d_points from Indices to a TopologySubsetIndices so that the indices get updated on topology changes.

* Renamed SetIndexArray to VecIndex. Renamed SetIndex to DataSubsetIndex to indicate, that it is an actual Data and a subset.

* Initialize link to topology

Co-authored-by: Alex Bilger <[email protected]>
  • Loading branch information
ScheiklP and alxbilger authored Jun 30, 2022
1 parent 6dc70fe commit 8225a6f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Authors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Pierre-Jean Bensoussan
Alexandre Bilger
Jean-Nicolas Brunet
Paul Maria Scheikl
Guillaume Bousquet
Olivier Carre
Bruno Carrez
Expand Down Expand Up @@ -99,4 +100,4 @@

+ Geomagic Interface
Hadrien Courtecuisse
Raffaela Trivisonne
Raffaela Trivisonne
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <sofa/core/behavior/ForceField.h>
#include <sofa/core/objectmodel/Data.h>
#include <sofa/core/topology/TopologySubsetIndices.h>
#include <sofa/type/vector.h>
#include <sofa/linearalgebra/EigenSparseMatrix.h>

Expand Down Expand Up @@ -60,16 +61,17 @@ class RestShapeSpringsForceField : public core::behavior::ForceField<DataTypes>
typedef typename DataTypes::Deriv Deriv;
typedef typename DataTypes::Real Real;
typedef type::vector< sofa::Index > VecIndex;
typedef sofa::core::topology::TopologySubsetIndices DataSubsetIndex;
typedef type::vector< Real > VecReal;

typedef core::objectmodel::Data<VecCoord> DataVecCoord;
typedef core::objectmodel::Data<VecDeriv> DataVecDeriv;

Data< type::vector< sofa::Index > > d_points; ///< points controlled by the rest shape springs
DataSubsetIndex d_points; ///< points controlled by the rest shape springs
Data< VecReal > d_stiffness; ///< stiffness values between the actual position and the rest shape position
Data< VecReal > d_angularStiffness; ///< angularStiffness assigned when controlling the rotation of the points
Data< type::vector< CPos > > d_pivotPoints; ///< global pivot points used when translations instead of the rigid mass centers
Data< type::vector< sofa::Index > > d_external_points; ///< points from the external Mechancial State that define the rest shape springs
Data< VecIndex > d_external_points; ///< points from the external Mechancial State that define the rest shape springs
Data< bool > d_recompute_indices; ///< Recompute indices (should be false for BBOX)
Data< bool > d_drawSpring; ///< draw Spring
Data< sofa::type::RGBAColor > d_springColor; ///< spring color. (default=[0.0,1.0,0.0,1.0])
Expand All @@ -88,6 +90,8 @@ class RestShapeSpringsForceField : public core::behavior::ForceField<DataTypes>

/// Add the forces.
void addForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) override;
/// Link to be set to the topology container in the component graph.
SingleLink<RestShapeSpringsForceField<DataTypes>, sofa::core::topology::BaseMeshTopology, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_topology;

void addDForce(const core::MechanicalParams* mparams, DataVecDeriv& df, const DataVecDeriv& dx) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ RestShapeSpringsForceField<DataTypes>::RestShapeSpringsForceField()
, d_drawSpring(initData(&d_drawSpring,false,"drawSpring","draw Spring"))
, d_springColor(initData(&d_springColor, sofa::type::RGBAColor::green(), "springColor","spring color. (default=[0.0,1.0,0.0,1.0])"))
, l_restMState(initLink("external_rest_shape", "rest_shape can be defined by the position of an external Mechanical State"))
, l_topology(initLink("topology", "Link to be set to the topology container in the component graph"))
{
}

Expand Down Expand Up @@ -106,6 +107,24 @@ void RestShapeSpringsForceField<DataTypes>::bwdInit()
useRestMState = true;
}

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.";
l_topology.set(this->getContext()->getMeshTopologyLink());
}

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

// Initialize topological changes support
d_points.createTopologyHandler(_topology);
}
else
{
msg_info() << "Cannot find the topology: topological changes will not be supported";
}

recomputeIndices();

BaseMechanicalState* state = this->getContext()->getMechanicalState();
Expand Down Expand Up @@ -183,14 +202,14 @@ void RestShapeSpringsForceField<DataTypes>::recomputeIndices()
m_indices.clear();
m_ext_indices.clear();

for (sofa::Index i = 0; i < d_points.getValue().size(); i++)
for (const sofa::Index i : d_points.getValue())
{
m_indices.push_back(d_points.getValue()[i]);
m_indices.push_back(i);
}

for (sofa::Index i = 0; i < d_external_points.getValue().size(); i++)
for (const sofa::Index i : d_external_points.getValue())
{
m_ext_indices.push_back(d_external_points.getValue()[i]);
m_ext_indices.push_back(i);
}

if (m_indices.empty())
Expand All @@ -214,9 +233,9 @@ void RestShapeSpringsForceField<DataTypes>::recomputeIndices()
}
else
{
for (sofa::Index i = 0; i < m_indices.size(); i++)
for (const sofa::Index i : m_indices)
{
m_ext_indices.push_back(m_indices[i]);
m_ext_indices.push_back(i);
}
}
}
Expand Down

0 comments on commit 8225a6f

Please sign in to comment.