Skip to content

Commit c9b4c50

Browse files
committed
use link instead of raw pointers
1 parent 10ebb35 commit c9b4c50

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
@@ -82,7 +82,7 @@ class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
8282
using VecEdgeID = type::vector<BaseMeshTopology::EdgeID>;
8383
using VecEdges = type::vector<BaseMeshTopology::Edge>;
8484

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

8787
virtual ~BaseBeamInterpolation() = default;
8888

@@ -210,7 +210,6 @@ class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
210210
typename MechanicalObject<StateDataTypes>::SPtr m_StateNodes;
211211

212212
Data< VecEdgeID > d_edgeList;
213-
const VecEdges* m_topologyEdges{ nullptr };
214213

215214
///2. Vector of length of each beam. Same size as @sa d_edgeList
216215
Data< type::vector< Real > > d_lengthList;
@@ -228,13 +227,12 @@ class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
228227
Data< sofa::type::vector<EdgeID> > d_beamCollision;
229228

230229
Data<bool> d_dofsAndBeamsAligned;
230+
231+
/// link to the (edge) topology
232+
SingleLink<BaseBeamInterpolation<DataTypes>, BaseMeshTopology, BaseLink::FLAG_STOREPATH|BaseLink::FLAG_STRONGLINK> l_topology;
231233

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

240238

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

@@ -120,33 +120,47 @@ template<class DataTypes>
120120
void BaseBeamInterpolation<DataTypes>::init()
121121
{
122122
BaseContext* context = getContext();
123-
124-
m_mstate = dynamic_cast<sofa::core::behavior::MechanicalState<DataTypes> *> (context->getMechanicalState());
125-
if (m_mstate == nullptr)
123+
124+
if (!l_mstate)
126125
{
127-
msg_error() << "No MechanicalState found. Component is de-activated.";
128-
d_componentState.setValue(ComponentState::Invalid);
126+
auto mstate = dynamic_cast<sofa::core::behavior::MechanicalState<DataTypes> *> (context->getMechanicalState());
127+
l_mstate.set(mstate);
128+
}
129+
if (l_mstate)
130+
{
131+
msg_info() << "Found mechanical object named "<< l_mstate->getName() ;
132+
}
133+
else
134+
{
135+
msg_error() << "Cannot find the mechanical object. Please specify the link to the topology or insert one in the same node.";
136+
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid);
129137
return;
130138
}
139+
140+
if (!l_topology)
141+
{
142+
l_topology.set(this->getContext()->getMeshTopologyLink());
143+
}
131144

132-
/// Get the topology from context and check if it is valid.
133-
m_topology = context->getMeshTopology();
134-
if (!m_topology)
145+
if (l_topology)
135146
{
136-
msg_error() << "No Topology found. Component is de-activated.";
137-
d_componentState.setValue(ComponentState::Invalid);
147+
msg_info() << "Found topology named "<< l_topology->getName() ;
148+
}
149+
else
150+
{
151+
msg_error() << "Cannot find a topology container. Please specify the link to the topology or insert one in the same node.";
152+
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid);
138153
return;
139154
}
140-
141-
/// Check the topology properties
142-
if (m_topology->getNbEdges() == 0)
155+
156+
if(l_topology->getNbEdges() == 0)
143157
{
144-
msg_error() << "Found a topology but it is empty. Component is de-activated";
158+
msg_error() << "Found a topology but it is empty (no edges). Component is de-activated";
145159
d_componentState.setValue(ComponentState::Invalid);
146160
return;
147161
}
148-
149-
m_topologyEdges = &m_topology->getEdges();
162+
163+
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid);
150164
}
151165

152166

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

409423
/// 1. Get the indices of element and nodes
410424
const EdgeID& e = d_edgeList.getValue()[edgeInList];
411-
const BaseMeshTopology::Edge& edge = (*m_topologyEdges)[e];
425+
const BaseMeshTopology::Edge& edge = l_topology->getEdge(e);
412426
node0Idx = edge[0];
413427
node1Idx = edge[1];
414428

@@ -435,14 +449,14 @@ void BaseBeamInterpolation<DataTypes>::getSplinePoints(const EdgeID edgeInList,
435449
template<class DataTypes>
436450
unsigned int BaseBeamInterpolation<DataTypes>::getStateSize() const
437451
{
438-
if (m_mstate == nullptr)
452+
if(!this->isComponentStateValid())
439453
{
440-
msg_error() << "No _mstate found (Aborting)";
454+
msg_error() << "(getStateSize) This component is invalid, check the other error messages. ";
441455
return 0;
442456
}
443457
else
444458
{
445-
return m_mstate->getSize();
459+
return l_mstate->getSize();
446460
}
447461
}
448462

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)