Skip to content

Commit a343708

Browse files
committed
use link instead of raw pointers
1 parent 9d5c20b commit a343708

3 files changed

Lines changed: 61 additions & 48 deletions

File tree

src/BeamAdapter/component/BaseBeamInterpolation.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
8484
using VecEdgeID = type::vector<BaseMeshTopology::EdgeID>;
8585
using VecEdges = type::vector<BaseMeshTopology::Edge>;
8686

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

8989
virtual ~BaseBeamInterpolation() = default;
9090

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

214214
Data< VecEdgeID > d_edgeList;
215-
const VecEdges* m_topologyEdges{ nullptr };
216215

217216
///2. Vector of length of each beam. Same size as @sa d_edgeList
218217
Data< type::vector< Real > > d_lengthList;
@@ -230,13 +229,12 @@ class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
230229
Data< sofa::type::vector<EdgeID> > d_beamCollision;
231230

232231
Data<bool> d_dofsAndBeamsAligned;
232+
233+
/// link to the (edge) topology
234+
SingleLink<BaseBeamInterpolation<DataTypes>, BaseMeshTopology, BaseLink::FLAG_STOREPATH|BaseLink::FLAG_STRONGLINK> l_topology;
233235

234-
235-
/// pointer to the topology
236-
BaseMeshTopology* m_topology{ nullptr };
237-
238-
/// pointer on mechanical state
239-
MechanicalState<DataTypes>* m_mstate{ nullptr };
236+
/// link to the mechanical state
237+
SingleLink<BaseBeamInterpolation<DataTypes>, MechanicalState<DataTypes>, BaseLink::FLAG_STOREPATH|BaseLink::FLAG_STRONGLINK> l_mstate;
240238
};
241239

242240

src/BeamAdapter/component/BaseBeamInterpolation.inl

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void BaseBeamInterpolation<DataTypes>::RotateFrameForAlignNormalizedX(const Quat
9696

9797

9898
template <class DataTypes>
99-
BaseBeamInterpolation<DataTypes>::BaseBeamInterpolation(/*sofa::component::engine::WireRestShape<DataTypes> *_restShape*/)
99+
BaseBeamInterpolation<DataTypes>::BaseBeamInterpolation()
100100
: m_StateNodes(sofa::core::objectmodel::New< sofa::component::statecontainer::MechanicalObject<StateDataTypes> >())
101101
, d_edgeList(initData(&d_edgeList, "edgeList", "list of the edge in the topology that are concerned by the Interpolation"))
102102
, d_lengthList(initData(&d_lengthList, "lengthList", "list of the length of each beam"))
@@ -106,8 +106,8 @@ BaseBeamInterpolation<DataTypes>::BaseBeamInterpolation(/*sofa::component::engin
106106
, d_beamCollision(initData(&d_beamCollision, "beamCollision", "list of beam (in edgeList) that needs to be considered for collision"))
107107
, d_dofsAndBeamsAligned(initData(&d_dofsAndBeamsAligned, true, "dofsAndBeamsAligned",
108108
"if false, a transformation for each beam is computed between the DOF and the beam nodes"))
109-
, m_topology(nullptr)
110-
, m_mstate(nullptr)
109+
, l_topology(initLink("topology", "link to the topology (must contain edges)"))
110+
, l_mstate(initLink("mstate", "link to the mechanical container"))
111111
{
112112

113113

@@ -122,33 +122,47 @@ void BaseBeamInterpolation<DataTypes>::init()
122122
Inherit::init();
123123

124124
BaseContext* context = getContext();
125-
126-
m_mstate = dynamic_cast<sofa::core::behavior::MechanicalState<DataTypes> *> (context->getMechanicalState());
127-
if (m_mstate == nullptr)
125+
126+
if (!l_mstate)
128127
{
129-
msg_error() << "No MechanicalState found. Component is de-activated.";
130-
d_componentState.setValue(ComponentState::Invalid);
128+
auto mstate = dynamic_cast<sofa::core::behavior::MechanicalState<DataTypes> *> (context->getMechanicalState());
129+
l_mstate.set(mstate);
130+
}
131+
if (l_mstate)
132+
{
133+
msg_info() << "Found mechanical object named "<< l_mstate->getName() ;
134+
}
135+
else
136+
{
137+
msg_error() << "Cannot find the mechanical object. Please specify the link to the topology or insert one in the same node.";
138+
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid);
131139
return;
132140
}
141+
142+
if (!l_topology)
143+
{
144+
l_topology.set(this->getContext()->getMeshTopologyLink());
145+
}
133146

134-
/// Get the topology from context and check if it is valid.
135-
m_topology = context->getMeshTopology();
136-
if (!m_topology)
147+
if (l_topology)
137148
{
138-
msg_error() << "No Topology found. Component is de-activated.";
139-
d_componentState.setValue(ComponentState::Invalid);
149+
msg_info() << "Found topology named "<< l_topology->getName() ;
150+
}
151+
else
152+
{
153+
msg_error() << "Cannot find a topology container. Please specify the link to the topology or insert one in the same node.";
154+
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid);
140155
return;
141156
}
142-
143-
/// Check the topology properties
144-
if (m_topology->getNbEdges() == 0)
157+
158+
if(l_topology->getNbEdges() == 0)
145159
{
146-
msg_error() << "Found a topology but it is empty. Component is de-activated";
160+
msg_error() << "Found a topology but it is empty (no edges). Component is de-activated";
147161
d_componentState.setValue(ComponentState::Invalid);
148162
return;
149163
}
150-
151-
m_topologyEdges = &m_topology->getEdges();
164+
165+
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid);
152166
}
153167

154168

@@ -402,15 +416,15 @@ int BaseBeamInterpolation<DataTypes>::getNodeIndices(const EdgeID edgeInList,
402416
unsigned int& node0Idx,
403417
unsigned int& node1Idx)
404418
{
405-
if (m_topologyEdges == nullptr)
419+
if(!this->isComponentStateValid())
406420
{
407-
msg_error() << "This object does not have edge topology defined (computation halted). ";
421+
msg_error() << "(getNodeIndices) This component is invalid, check the other error messages. ";
408422
return -1;
409423
}
410424

411425
/// 1. Get the indices of element and nodes
412426
const EdgeID& e = d_edgeList.getValue()[edgeInList];
413-
const BaseMeshTopology::Edge& edge = (*m_topologyEdges)[e];
427+
const BaseMeshTopology::Edge& edge = l_topology->getEdge(e);
414428
node0Idx = edge[0];
415429
node1Idx = edge[1];
416430

@@ -437,14 +451,14 @@ void BaseBeamInterpolation<DataTypes>::getSplinePoints(const EdgeID edgeInList,
437451
template<class DataTypes>
438452
unsigned int BaseBeamInterpolation<DataTypes>::getStateSize() const
439453
{
440-
if (m_mstate == nullptr)
454+
if(!this->isComponentStateValid())
441455
{
442-
msg_error() << "No _mstate found (Aborting)";
456+
msg_error() << "(getStateSize) This component is invalid, check the other error messages. ";
443457
return 0;
444458
}
445459
else
446460
{
447-
return m_mstate->getSize();
461+
return l_mstate->getSize();
448462
}
449463
}
450464

src/BeamAdapter/component/BeamInterpolation.inl

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void BeamInterpolation<DataTypes>::computeCrossSectionInertiaMatrix()
139139
}
140140
else
141141
{
142-
Size nbEdges = this->m_topology->getNbEdges();
142+
Size nbEdges = this->l_topology->getNbEdges();
143143
m_section.resize(nbEdges);
144144
if ( crossSectionShape.getValue().getSelectedItem() == "elliptic")
145145
{
@@ -197,7 +197,7 @@ void BeamInterpolation<DataTypes>::init()
197197
if (this->d_componentState.getValue() == ComponentState::Invalid)
198198
return;
199199

200-
Size nbEdges = this->m_topology->getNbEdges();
200+
Size nbEdges = this->l_topology->getNbEdges();
201201
checkDataSize(m_defaultRadius, d_radius, nbEdges);
202202
checkDataSize(m_defaultInnerRadius, d_innerRadius, nbEdges);
203203
checkDataSize(m_defaultLengthY, d_lengthY, nbEdges);
@@ -211,7 +211,7 @@ void BeamInterpolation<DataTypes>::init()
211211
auto edgeList = sofa::helper::getWriteOnlyAccessor(this->d_edgeList);
212212
edgeList.clear();
213213

214-
for (unsigned int i=0; i<this->m_topology->getNbEdges(); i++)
214+
for (unsigned int i=0; i<this->l_topology->getNbEdges(); i++)
215215
{
216216
edgeList.push_back(i);
217217
}
@@ -225,7 +225,7 @@ void BeamInterpolation<DataTypes>::init()
225225
DOF1TransformNode1.resize(edgeList.size());
226226
}
227227

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

230230
auto lengthList = sofa::helper::getWriteOnlyAccessor(this->d_lengthList);
231231
lengthList.clear();
@@ -335,17 +335,18 @@ bool BeamInterpolation<DataTypes>::interpolationIsAlreadyInitialized()
335335
template<class DataTypes>
336336
bool BeamInterpolation<DataTypes>::verifyTopology()
337337
{
338+
const auto nbEdges = this->l_topology->getNbEdges();
339+
338340
//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-
341+
dmsg_info() << "The vector _topologyEdges is now set with " << nbEdges << " edges" ;
341342

342343
const VecElementID &edgeList = this->d_edgeList.getValue();
343344
for (unsigned int j = 0; j < edgeList.size(); j++)
344345
{
345-
if(edgeList[j] > this->m_topologyEdges->size())
346+
if(edgeList[j] > nbEdges)
346347
{
347348
msg_error() << "The provided edge index '" << edgeList[j] << "'is larger than '"
348-
<< this->m_topologyEdges->size() << "' the amount of edges in the topology. " ;
349+
<< nbEdges << "' the amount of edges in the topology. " ;
349350
return false;
350351
}
351352
}
@@ -420,7 +421,7 @@ void BeamInterpolation<DataTypes>::getMechanicalSampling(Real& dx, const Real x_
420421
{
421422
SOFA_UNUSED(x_localcurv_abs);
422423

423-
const auto numLines = this->m_topologyEdges->size();
424+
const auto numLines = this->l_topology->getNbEdges();
424425
dx = getRestTotalLength()/numLines;
425426
}
426427

@@ -429,14 +430,14 @@ void BeamInterpolation<DataTypes>::getCollisionSampling(Real &dx, const Real x_l
429430
{
430431
SOFA_UNUSED(x_localcurv_abs);
431432

432-
const auto numLines = this->m_topologyEdges->size();
433+
const auto numLines = this->l_topology->getNbEdges();
433434
dx = getRestTotalLength()/numLines;
434435
}
435436

436437
template <class DataTypes>
437438
void BeamInterpolation<DataTypes>::getNumberOfCollisionSegment(Real &dx, unsigned int &numLines)
438439
{
439-
numLines = static_cast<unsigned int>(this->m_topologyEdges->size());
440+
numLines = static_cast<unsigned int>(this->l_topology->getNbEdges());
440441
dx = getRestTotalLength()/numLines;
441442
}
442443

@@ -599,16 +600,16 @@ void BeamInterpolation<DataTypes>::updateInterpolation(){
599600
if(d_vecID.getValue().getSelectedItem() == "current")
600601
{
601602
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 );
603+
<< " ="<< this->l_mstate->read( sofa::core::vec_id::read_access::position )->getValue( ) ;
604+
x=this->l_mstate->read( sofa::core::vec_id::read_access::position );
604605
}
605606
else if(d_vecID.getValue().getSelectedItem() == "free")
606607
{
607-
x=this->m_mstate->read( sofa::core::vec_id::read_access::freePosition ) ;
608+
x=this->l_mstate->read( sofa::core::vec_id::read_access::freePosition ) ;
608609
}
609610
else /// rest position
610611
{
611-
x=this->m_mstate->read( sofa::core::vec_id::read_access::restPosition ) ;
612+
x=this->l_mstate->read( sofa::core::vec_id::read_access::restPosition ) ;
612613
computeVel = false;
613614
}
614615

0 commit comments

Comments
 (0)