Skip to content

Commit 2897605

Browse files
authored
[Interpolations] Remove raw ptr for mstate and topology (#185)
* use link instead of raw pointers * use SingleStateAccessor (+ some minor cleanings)
1 parent 1b8dcaf commit 2897605

6 files changed

Lines changed: 91 additions & 92 deletions

File tree

src/BeamAdapter/component/BaseBeamInterpolation.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@
2828
#include <sofa/core/behavior/ForceField.h>
2929
#include <sofa/core/behavior/Mass.h>
3030
#include <sofa/core/objectmodel/Data.h>
31-
#include <sofa/defaulttype/SolidTypes.h>
3231
#include <sofa/core/topology/BaseMeshTopology.h>
32+
#include <sofa/core/behavior/SingleStateAccessor.h>
3333

3434
#include <sofa/type/vector.h>
3535
#include <sofa/type/Vec.h>
3636
#include <sofa/type/Mat.h>
37+
#include <sofa/type/Transform.h>
38+
#include <sofa/type/SpatialVector.h>
39+
3740

38-
#include <sofa/core/objectmodel/BaseObject.h>
3941
#include <sofa/component/statecontainer/MechanicalObject.h>
4042

4143

@@ -55,13 +57,13 @@ using sofa::component::statecontainer::MechanicalObject;
5557
*
5658
*/
5759
template<class DataTypes>
58-
class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
60+
class BaseBeamInterpolation : public sofa::core::behavior::SingleStateAccessor<DataTypes>
5961
{
6062
public:
6163
SOFA_CLASS(SOFA_TEMPLATE(BaseBeamInterpolation, DataTypes) ,
62-
sofa::core::objectmodel::BaseObject);
64+
SOFA_TEMPLATE(sofa::core::behavior::SingleStateAccessor, DataTypes));
6365

64-
using Inherit = sofa::core::objectmodel::BaseObject;
66+
using Inherit = sofa::core::behavior::SingleStateAccessor<DataTypes>;
6567

6668
using Coord = typename DataTypes::Coord;
6769
using VecCoord = typename DataTypes::VecCoord;
@@ -70,8 +72,8 @@ class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
7072
using Deriv = typename DataTypes::Deriv;
7173
using VecDeriv = typename DataTypes::VecDeriv;
7274

73-
using Transform = typename sofa::defaulttype::SolidTypes<Real>::Transform;
74-
using SpatialVector = typename sofa::defaulttype::SolidTypes<Real>::SpatialVector;
75+
using Transform = sofa::type::Transform<Real>;
76+
using SpatialVector = sofa::type::SpatialVector<Real>;
7577

7678
using Vec2 = sofa::type::Vec<2, Real>;
7779
using Vec3 = sofa::type::Vec<3, Real>;
@@ -84,7 +86,7 @@ class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
8486
using VecEdgeID = type::vector<BaseMeshTopology::EdgeID>;
8587
using VecEdges = type::vector<BaseMeshTopology::Edge>;
8688

87-
BaseBeamInterpolation(/*sofa::component::engine::WireRestShape<DataTypes> *_restShape = nullptr*/);
89+
BaseBeamInterpolation();
8890

8991
virtual ~BaseBeamInterpolation() = default;
9092

@@ -212,7 +214,6 @@ class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
212214
typename MechanicalObject<StateDataTypes>::SPtr m_StateNodes;
213215

214216
Data< VecEdgeID > d_edgeList;
215-
const VecEdges* m_topologyEdges{ nullptr };
216217

217218
///2. Vector of length of each beam. Same size as @sa d_edgeList
218219
Data< type::vector< Real > > d_lengthList;
@@ -230,13 +231,9 @@ class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
230231
Data< sofa::type::vector<EdgeID> > d_beamCollision;
231232

232233
Data<bool> d_dofsAndBeamsAligned;
233-
234-
235-
/// pointer to the topology
236-
BaseMeshTopology* m_topology{ nullptr };
237-
238-
/// pointer on mechanical state
239-
MechanicalState<DataTypes>* m_mstate{ nullptr };
234+
235+
/// link to the (edge) topology
236+
SingleLink<BaseBeamInterpolation<DataTypes>, BaseMeshTopology, BaseLink::FLAG_STOREPATH|BaseLink::FLAG_STRONGLINK> l_topology;
240237
};
241238

242239

src/BeamAdapter/component/BaseBeamInterpolation.inl

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ void BaseBeamInterpolation<DataTypes>::RotateFrameForAlignNormalizedX(const Quat
9696

9797

9898
template <class DataTypes>
99-
BaseBeamInterpolation<DataTypes>::BaseBeamInterpolation(/*sofa::component::engine::WireRestShape<DataTypes> *_restShape*/)
100-
: m_StateNodes(sofa::core::objectmodel::New< sofa::component::statecontainer::MechanicalObject<StateDataTypes> >())
99+
BaseBeamInterpolation<DataTypes>::BaseBeamInterpolation()
100+
: Inherit()
101+
, m_StateNodes(sofa::core::objectmodel::New< sofa::component::statecontainer::MechanicalObject<StateDataTypes> >())
101102
, d_edgeList(initData(&d_edgeList, "edgeList", "list of the edge in the topology that are concerned by the Interpolation"))
102103
, d_lengthList(initData(&d_lengthList, "lengthList", "list of the length of each beam"))
103104
, d_DOF0TransformNode0(initData(&d_DOF0TransformNode0, "DOF0TransformNode0", "Optional rigid transformation between the degree of Freedom and the first node of the beam"))
@@ -106,49 +107,43 @@ BaseBeamInterpolation<DataTypes>::BaseBeamInterpolation(/*sofa::component::engin
106107
, d_beamCollision(initData(&d_beamCollision, "beamCollision", "list of beam (in edgeList) that needs to be considered for collision"))
107108
, d_dofsAndBeamsAligned(initData(&d_dofsAndBeamsAligned, true, "dofsAndBeamsAligned",
108109
"if false, a transformation for each beam is computed between the DOF and the beam nodes"))
109-
, m_topology(nullptr)
110-
, m_mstate(nullptr)
110+
, l_topology(initLink("topology", "link to the topology (must contain edges)"))
111111
{
112112

113-
114113
m_StateNodes->setName("bezierNodes");
115-
addSlave(m_StateNodes);
114+
this->addSlave(m_StateNodes);
116115
}
117116

118117

119118
template<class DataTypes>
120119
void BaseBeamInterpolation<DataTypes>::init()
121120
{
122121
Inherit::init();
123-
124-
BaseContext* context = getContext();
125-
126-
m_mstate = dynamic_cast<sofa::core::behavior::MechanicalState<DataTypes> *> (context->getMechanicalState());
127-
if (m_mstate == nullptr)
122+
123+
if (!l_topology)
128124
{
129-
msg_error() << "No MechanicalState found. Component is de-activated.";
130-
d_componentState.setValue(ComponentState::Invalid);
131-
return;
125+
l_topology.set(this->getMState()->getContext()->getMeshTopologyLink());
132126
}
133127

134-
/// Get the topology from context and check if it is valid.
135-
m_topology = context->getMeshTopology();
136-
if (!m_topology)
128+
if (l_topology)
129+
{
130+
msg_info() << "Found topology named "<< l_topology->getName() ;
131+
}
132+
else
137133
{
138-
msg_error() << "No Topology found. Component is de-activated.";
139-
d_componentState.setValue(ComponentState::Invalid);
134+
msg_error() << "Cannot find a topology container. Please specify the link to the topology or insert one in the same node.";
135+
this->d_componentState.setValue(ComponentState::Invalid);
140136
return;
141137
}
142-
143-
/// Check the topology properties
144-
if (m_topology->getNbEdges() == 0)
138+
139+
if(l_topology->getNbEdges() == 0)
145140
{
146-
msg_error() << "Found a topology but it is empty. Component is de-activated";
147-
d_componentState.setValue(ComponentState::Invalid);
141+
msg_error() << "Found a topology but it is empty (no edges). Component is de-activated";
142+
this->d_componentState.setValue(ComponentState::Invalid);
148143
return;
149144
}
150-
151-
m_topologyEdges = &m_topology->getEdges();
145+
146+
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid);
152147
}
153148

154149

@@ -266,7 +261,7 @@ void BaseBeamInterpolation<DataTypes>::getBeamAtCurvAbs(const Real x_input, unsi
266261
/// LTotal = length sum of the beams that are "out"
267262
Real LTotal = 0.0;
268263

269-
const unsigned int edgeListSize = this->d_edgeList.getValue().size();
264+
const sofa::Size edgeListSize = static_cast<sofa::Size>(this->d_edgeList.getValue().size());
270265

271266
/// we find the length of the beam that is "out"
272267
for (unsigned int e = start; e < edgeListSize; e++)
@@ -360,7 +355,7 @@ void BaseBeamInterpolation<DataTypes>::getDOFtoLocalTransformInGlobalFrame(const
360355
template <class DataTypes>
361356
void BaseBeamInterpolation<DataTypes>::setTransformBetweenDofAndNode(const sofa::Index beam, const Transform& DOF_H_Node, unsigned int zeroORone)
362357
{
363-
if (beam > int(d_DOF0TransformNode0.getValue().size() - 1) || beam > int(d_DOF1TransformNode1.getValue().size() - 1))
358+
if (beam > (d_DOF0TransformNode0.getValue().size() - 1) || beam > (d_DOF1TransformNode1.getValue().size() - 1))
364359
{
365360
msg_error() << "WARNING setTransformBetweenDofAndNode on non existing beam";
366361
return;
@@ -402,15 +397,15 @@ int BaseBeamInterpolation<DataTypes>::getNodeIndices(const EdgeID edgeInList,
402397
unsigned int& node0Idx,
403398
unsigned int& node1Idx)
404399
{
405-
if (m_topologyEdges == nullptr)
400+
if(!this->isComponentStateValid())
406401
{
407-
msg_error() << "This object does not have edge topology defined (computation halted). ";
402+
msg_error() << "(getNodeIndices) This component is invalid, check the other error messages. ";
408403
return -1;
409404
}
410405

411406
/// 1. Get the indices of element and nodes
412407
const EdgeID& e = d_edgeList.getValue()[edgeInList];
413-
const BaseMeshTopology::Edge& edge = (*m_topologyEdges)[e];
408+
const BaseMeshTopology::Edge& edge = l_topology->getEdge(e);
414409
node0Idx = edge[0];
415410
node1Idx = edge[1];
416411

@@ -437,14 +432,14 @@ void BaseBeamInterpolation<DataTypes>::getSplinePoints(const EdgeID edgeInList,
437432
template<class DataTypes>
438433
unsigned int BaseBeamInterpolation<DataTypes>::getStateSize() const
439434
{
440-
if (m_mstate == nullptr)
435+
if(!this->isComponentStateValid())
441436
{
442-
msg_error() << "No _mstate found (Aborting)";
437+
msg_error() << "(getStateSize) This component is invalid, check the other error messages. ";
443438
return 0;
444439
}
445440
else
446441
{
447-
return m_mstate->getSize();
442+
return this->getMState()->getSize();
448443
}
449444
}
450445

@@ -582,22 +577,24 @@ void BaseBeamInterpolation<DataTypes>::computeStrechAndTwist(const EdgeID edgeIn
582577

583578
///vId_Out provides the id of the multiVecId which stores the position of the Bezier Points
584579
template<class DataTypes>
585-
void BaseBeamInterpolation<DataTypes>::updateBezierPoints(const VecCoord& x, sofa::core::VecCoordId& vId_Out) {
580+
void BaseBeamInterpolation<DataTypes>::updateBezierPoints(const VecCoord& x, sofa::core::VecCoordId& vId_Out)
581+
{
582+
const sofa::Size edgeListSize = static_cast<sofa::Size>(d_edgeList.getValue().size());
586583
///Mechanical Object to stock Bezier points.
587-
m_StateNodes->resize(d_edgeList.getValue().size() * 4);
584+
m_StateNodes->resize(edgeListSize * 4);
588585
auto bezierPosVec = sofa::helper::getWriteOnlyAccessor(*m_StateNodes->write(vId_Out));
589-
bezierPosVec.resize(d_edgeList.getValue().size() * 4);
586+
bezierPosVec.resize(edgeListSize * 4);
590587

591-
for (unsigned int i = 0; i < d_edgeList.getValue().size(); i++) {
588+
for (unsigned int i = 0; i < edgeListSize; i++) {
592589
updateBezierPoints(x, i, bezierPosVec.wref());
593590

594591
}
595592
}
596593

597594
template<class DataTypes>
598595
void BaseBeamInterpolation<DataTypes>::updateBezierPoints(const VecCoord& x, unsigned int index,
599-
VectorVec3& bezierPosVec) {
600-
/// <<" interpolatePointUsingSpline : "<< edgeInList<<" xbary="<<baryCoord<<" localPos"<<localPos<<std::endl;
596+
VectorVec3& bezierPosVec)
597+
{
601598
const Real _L = d_lengthList.getValue()[index];
602599

603600
/// \todo remove call to

src/BeamAdapter/component/BeamInterpolation.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class BeamInterpolation : public BaseBeamInterpolation<DataTypes>
6969
{
7070
public:
7171
SOFA_CLASS( SOFA_TEMPLATE(BeamInterpolation, DataTypes) , SOFA_TEMPLATE(BaseBeamInterpolation, DataTypes));
72+
73+
using Inherit = BaseBeamInterpolation<DataTypes>;
7274

7375
using Coord = typename DataTypes::Coord;
7476
using VecCoord = typename DataTypes::VecCoord;

src/BeamAdapter/component/BeamInterpolation.inl

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ using sofa::helper::ReadAccessor ;
5050

5151
//////////////////////////////////// BREAMINTERPOLATION ////////////////////////////////////////////
5252
template <class DataTypes>
53-
BeamInterpolation<DataTypes>::BeamInterpolation() :
54-
crossSectionShape(initData(&crossSectionShape,
53+
BeamInterpolation<DataTypes>::BeamInterpolation()
54+
: Inherit()
55+
, crossSectionShape(initData(&crossSectionShape,
5556
{"circular","elliptic (not available)","rectangular"},
5657
"crossSectionShape",
5758
"shape of the cross-section. Can be: circular, elliptic, square, rectangular. Default is circular" ))
@@ -139,7 +140,7 @@ void BeamInterpolation<DataTypes>::computeCrossSectionInertiaMatrix()
139140
}
140141
else
141142
{
142-
Size nbEdges = this->m_topology->getNbEdges();
143+
Size nbEdges = this->l_topology->getNbEdges();
143144
m_section.resize(nbEdges);
144145
if ( crossSectionShape.getValue().getSelectedItem() == "elliptic")
145146
{
@@ -192,12 +193,12 @@ template <class DataTypes>
192193
void BeamInterpolation<DataTypes>::init()
193194
{
194195
this->d_componentState.setValue(ComponentState::Loading);
195-
BaseBeamInterpolation<DataTypes>::init();
196+
Inherit::init();
196197

197198
if (this->d_componentState.getValue() == ComponentState::Invalid)
198199
return;
199200

200-
Size nbEdges = this->m_topology->getNbEdges();
201+
Size nbEdges = this->l_topology->getNbEdges();
201202
checkDataSize(m_defaultRadius, d_radius, nbEdges);
202203
checkDataSize(m_defaultInnerRadius, d_innerRadius, nbEdges);
203204
checkDataSize(m_defaultLengthY, d_lengthY, nbEdges);
@@ -211,7 +212,7 @@ void BeamInterpolation<DataTypes>::init()
211212
auto edgeList = sofa::helper::getWriteOnlyAccessor(this->d_edgeList);
212213
edgeList.clear();
213214

214-
for (unsigned int i=0; i<this->m_topology->getNbEdges(); i++)
215+
for (unsigned int i=0; i<this->l_topology->getNbEdges(); i++)
215216
{
216217
edgeList.push_back(i);
217218
}
@@ -225,7 +226,7 @@ void BeamInterpolation<DataTypes>::init()
225226
DOF1TransformNode1.resize(edgeList.size());
226227
}
227228

228-
ReadAccessor<Data<VecCoord> > statePos = this->m_mstate->read(sofa::core::vec_id::read_access::position) ;
229+
ReadAccessor<Data<VecCoord> > statePos = this->getMState()->read(sofa::core::vec_id::read_access::position) ;
229230

230231
auto lengthList = sofa::helper::getWriteOnlyAccessor(this->d_lengthList);
231232
lengthList.clear();
@@ -335,17 +336,18 @@ bool BeamInterpolation<DataTypes>::interpolationIsAlreadyInitialized()
335336
template<class DataTypes>
336337
bool BeamInterpolation<DataTypes>::verifyTopology()
337338
{
339+
const auto nbEdges = this->l_topology->getNbEdges();
340+
338341
//TODO(dmarchal) This contains "code" specific slang that cannot be understood by user.
339-
dmsg_info() << "The vector _topologyEdges is now set with " << this->m_topologyEdges->size() << " edges" ;
340-
342+
dmsg_info() << "The vector _topologyEdges is now set with " << nbEdges << " edges" ;
341343

342344
const VecElementID &edgeList = this->d_edgeList.getValue();
343345
for (unsigned int j = 0; j < edgeList.size(); j++)
344346
{
345-
if(edgeList[j] > this->m_topologyEdges->size())
347+
if(edgeList[j] > nbEdges)
346348
{
347349
msg_error() << "The provided edge index '" << edgeList[j] << "'is larger than '"
348-
<< this->m_topologyEdges->size() << "' the amount of edges in the topology. " ;
350+
<< nbEdges << "' the amount of edges in the topology. " ;
349351
return false;
350352
}
351353
}
@@ -420,7 +422,7 @@ void BeamInterpolation<DataTypes>::getMechanicalSampling(Real& dx, const Real x_
420422
{
421423
SOFA_UNUSED(x_localcurv_abs);
422424

423-
const auto numLines = this->m_topologyEdges->size();
425+
const auto numLines = this->l_topology->getNbEdges();
424426
dx = getRestTotalLength()/numLines;
425427
}
426428

@@ -429,14 +431,14 @@ void BeamInterpolation<DataTypes>::getCollisionSampling(Real &dx, const Real x_l
429431
{
430432
SOFA_UNUSED(x_localcurv_abs);
431433

432-
const auto numLines = this->m_topologyEdges->size();
434+
const auto numLines = this->l_topology->getNbEdges();
433435
dx = getRestTotalLength()/numLines;
434436
}
435437

436438
template <class DataTypes>
437439
void BeamInterpolation<DataTypes>::getNumberOfCollisionSegment(Real &dx, unsigned int &numLines)
438440
{
439-
numLines = static_cast<unsigned int>(this->m_topologyEdges->size());
441+
numLines = static_cast<unsigned int>(this->l_topology->getNbEdges());
440442
dx = getRestTotalLength()/numLines;
441443
}
442444

@@ -599,16 +601,16 @@ void BeamInterpolation<DataTypes>::updateInterpolation(){
599601
if(d_vecID.getValue().getSelectedItem() == "current")
600602
{
601603
dmsg_info() <<" position " << msgendl
602-
<< " ="<< this->m_mstate->read( sofa::core::vec_id::read_access::position )->getValue( ) ;
603-
x=this->m_mstate->read( sofa::core::vec_id::read_access::position );
604+
<< " ="<< this->getMState()->read( sofa::core::vec_id::read_access::position )->getValue( ) ;
605+
x=this->getMState()->read( sofa::core::vec_id::read_access::position );
604606
}
605607
else if(d_vecID.getValue().getSelectedItem() == "free")
606608
{
607-
x=this->m_mstate->read( sofa::core::vec_id::read_access::freePosition ) ;
609+
x=this->getMState()->read( sofa::core::vec_id::read_access::freePosition ) ;
608610
}
609611
else /// rest position
610612
{
611-
x=this->m_mstate->read( sofa::core::vec_id::read_access::restPosition ) ;
613+
x=this->getMState()->read( sofa::core::vec_id::read_access::restPosition ) ;
612614
computeVel = false;
613615
}
614616

0 commit comments

Comments
 (0)