Skip to content

Commit 3504364

Browse files
committed
use SingleStateAccessor (+ some minor cleanings)
1 parent 8273c0c commit 3504364

6 files changed

Lines changed: 57 additions & 71 deletions

File tree

src/BeamAdapter/component/BaseBeamInterpolation.h

Lines changed: 9 additions & 10 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>;
@@ -232,9 +234,6 @@ class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
232234

233235
/// link to the (edge) topology
234236
SingleLink<BaseBeamInterpolation<DataTypes>, BaseMeshTopology, BaseLink::FLAG_STOREPATH|BaseLink::FLAG_STRONGLINK> l_topology;
235-
236-
/// link to the mechanical state
237-
SingleLink<BaseBeamInterpolation<DataTypes>, MechanicalState<DataTypes>, BaseLink::FLAG_STOREPATH|BaseLink::FLAG_STRONGLINK> l_mstate;
238237
};
239238

240239

src/BeamAdapter/component/BaseBeamInterpolation.inl

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ void BaseBeamInterpolation<DataTypes>::RotateFrameForAlignNormalizedX(const Quat
9797

9898
template <class DataTypes>
9999
BaseBeamInterpolation<DataTypes>::BaseBeamInterpolation()
100-
: m_StateNodes(sofa::core::objectmodel::New< sofa::component::statecontainer::MechanicalObject<StateDataTypes> >())
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"))
@@ -107,41 +108,21 @@ BaseBeamInterpolation<DataTypes>::BaseBeamInterpolation()
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"))
109110
, l_topology(initLink("topology", "link to the topology (must contain edges)"))
110-
, l_mstate(initLink("mstate", "link to the mechanical container"))
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-
if (!l_mstate)
127-
{
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);
139-
return;
140-
}
141-
122+
142123
if (!l_topology)
143124
{
144-
l_topology.set(this->getContext()->getMeshTopologyLink());
125+
l_topology.set(this->getMState()->getContext()->getMeshTopologyLink());
145126
}
146127

147128
if (l_topology)
@@ -151,14 +132,14 @@ void BaseBeamInterpolation<DataTypes>::init()
151132
else
152133
{
153134
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);
135+
this->d_componentState.setValue(ComponentState::Invalid);
155136
return;
156137
}
157138

158139
if(l_topology->getNbEdges() == 0)
159140
{
160141
msg_error() << "Found a topology but it is empty (no edges). Component is de-activated";
161-
d_componentState.setValue(ComponentState::Invalid);
142+
this->d_componentState.setValue(ComponentState::Invalid);
162143
return;
163144
}
164145

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

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

285266
/// we find the length of the beam that is "out"
286267
for (unsigned int e = start; e < edgeListSize; e++)
@@ -374,7 +355,7 @@ void BaseBeamInterpolation<DataTypes>::getDOFtoLocalTransformInGlobalFrame(const
374355
template <class DataTypes>
375356
void BaseBeamInterpolation<DataTypes>::setTransformBetweenDofAndNode(const sofa::Index beam, const Transform& DOF_H_Node, unsigned int zeroORone)
376357
{
377-
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))
378359
{
379360
msg_error() << "WARNING setTransformBetweenDofAndNode on non existing beam";
380361
return;
@@ -458,7 +439,7 @@ unsigned int BaseBeamInterpolation<DataTypes>::getStateSize() const
458439
}
459440
else
460441
{
461-
return l_mstate->getSize();
442+
return this->getMState()->getSize();
462443
}
463444
}
464445

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

597578
///vId_Out provides the id of the multiVecId which stores the position of the Bezier Points
598579
template<class DataTypes>
599-
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());
600583
///Mechanical Object to stock Bezier points.
601-
m_StateNodes->resize(d_edgeList.getValue().size() * 4);
584+
m_StateNodes->resize(edgeListSize * 4);
602585
auto bezierPosVec = sofa::helper::getWriteOnlyAccessor(*m_StateNodes->write(vId_Out));
603-
bezierPosVec.resize(d_edgeList.getValue().size() * 4);
586+
bezierPosVec.resize(edgeListSize * 4);
604587

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

608591
}
609592
}
610593

611594
template<class DataTypes>
612595
void BaseBeamInterpolation<DataTypes>::updateBezierPoints(const VecCoord& x, unsigned int index,
613-
VectorVec3& bezierPosVec) {
614-
/// <<" interpolatePointUsingSpline : "<< edgeInList<<" xbary="<<baryCoord<<" localPos"<<localPos<<std::endl;
596+
VectorVec3& bezierPosVec)
597+
{
615598
const Real _L = d_lengthList.getValue()[index];
616599

617600
/// \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: 9 additions & 8 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" ))
@@ -192,7 +193,7 @@ 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;
@@ -225,7 +226,7 @@ void BeamInterpolation<DataTypes>::init()
225226
DOF1TransformNode1.resize(edgeList.size());
226227
}
227228

228-
ReadAccessor<Data<VecCoord> > statePos = this->l_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();
@@ -600,16 +601,16 @@ void BeamInterpolation<DataTypes>::updateInterpolation(){
600601
if(d_vecID.getValue().getSelectedItem() == "current")
601602
{
602603
dmsg_info() <<" position " << msgendl
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 );
604+
<< " ="<< this->getMState()->read( sofa::core::vec_id::read_access::position )->getValue( ) ;
605+
x=this->getMState()->read( sofa::core::vec_id::read_access::position );
605606
}
606607
else if(d_vecID.getValue().getSelectedItem() == "free")
607608
{
608-
x=this->l_mstate->read( sofa::core::vec_id::read_access::freePosition ) ;
609+
x=this->getMState()->read( sofa::core::vec_id::read_access::freePosition ) ;
609610
}
610611
else /// rest position
611612
{
612-
x=this->l_mstate->read( sofa::core::vec_id::read_access::restPosition ) ;
613+
x=this->getMState()->read( sofa::core::vec_id::read_access::restPosition ) ;
613614
computeVel = false;
614615
}
615616

src/BeamAdapter/component/WireBeamInterpolation.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,22 @@ class WireBeamInterpolation : public BaseBeamInterpolation<DataTypes>
7575
public:
7676
SOFA_CLASS(SOFA_TEMPLATE(WireBeamInterpolation, DataTypes) ,
7777
SOFA_TEMPLATE(BaseBeamInterpolation, DataTypes) );
78+
79+
using Inherit = BaseBeamInterpolation<DataTypes>;
7880

79-
typedef BaseBeamInterpolation<DataTypes> Inherited;
80-
81-
typedef typename Inherited::VecCoord VecCoord;
82-
typedef typename Inherited::VecDeriv VecDeriv;
83-
typedef typename Inherited::Coord Coord;
84-
typedef typename Inherited::Deriv Deriv;
81+
typedef typename Inherit::VecCoord VecCoord;
82+
typedef typename Inherit::VecDeriv VecDeriv;
83+
typedef typename Inherit::Coord Coord;
84+
typedef typename Inherit::Deriv Deriv;
8585

86-
typedef typename Inherited::Real Real;
86+
typedef typename Inherit::Real Real;
8787

88-
typedef typename Inherited::Transform Transform;
89-
typedef typename Inherited::SpatialVector SpatialVector;
88+
typedef typename Inherit::Transform Transform;
89+
typedef typename Inherit::SpatialVector SpatialVector;
9090

91-
typedef typename Inherited::Vec2 Vec2;
92-
typedef typename Inherited::Vec3 Vec3;
93-
typedef typename Inherited::Quat Quat;
91+
typedef typename Inherit::Vec2 Vec2;
92+
typedef typename Inherit::Vec3 Vec3;
93+
typedef typename Inherit::Quat Quat;
9494

9595
using EdgeID = BaseMeshTopology::EdgeID;
9696

@@ -176,9 +176,9 @@ class WireBeamInterpolation : public BaseBeamInterpolation<DataTypes>
176176
BaseLink::FLAG_STOREPATH|BaseLink::FLAG_STRONGLINK> m_restShape; /*! link on an external rest-shape*/
177177

178178

179-
////////////////////////// Inherited attributes ////////////////////////////
179+
////////////////////////// Inherit attributes ////////////////////////////
180180
/// https://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html
181-
/// Bring inherited attributes and function in the current lookup context.
181+
/// Bring Inherit attributes and function in the current lookup context.
182182
/// otherwise any access to the base::attribute would require
183183
/// the "this->" approach.
184184
using BaseBeamInterpolation<DataTypes>::d_componentState ;
@@ -189,7 +189,7 @@ class WireBeamInterpolation : public BaseBeamInterpolation<DataTypes>
189189
template<class T>
190190
static bool canCreate(T* obj, sofa::core::objectmodel::BaseContext* context, sofa::core::objectmodel::BaseObjectDescription* arg)
191191
{
192-
return Inherited::canCreate(obj,context,arg);
192+
return Inherit::canCreate(obj,context,arg);
193193
}
194194

195195
template<class T>

src/BeamAdapter/component/WireBeamInterpolation.inl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ namespace beamadapter
4141

4242
template <class DataTypes>
4343
WireBeamInterpolation<DataTypes>::WireBeamInterpolation(WireRestShape<DataTypes> *_restShape)
44-
: m_restShape(initLink("WireRestShape", "link to the component on the scene"), _restShape)
44+
: Inherit()
45+
, m_restShape(initLink("WireRestShape", "link to the component on the scene"), _restShape)
4546
{
4647

4748

@@ -51,7 +52,7 @@ WireBeamInterpolation<DataTypes>::WireBeamInterpolation(WireRestShape<DataTypes>
5152
template <class DataTypes>
5253
void WireBeamInterpolation<DataTypes>::init()
5354
{
54-
Inherited::init();
55+
Inherit::init();
5556

5657
if( m_restShape.get() == nullptr )
5758
{
@@ -72,7 +73,7 @@ void WireBeamInterpolation<DataTypes>::init()
7273
template <class DataTypes>
7374
void WireBeamInterpolation<DataTypes>::bwdInit()
7475
{
75-
Inherited::bwdInit();
76+
Inherit::bwdInit();
7677

7778
if (this->isControlled()){
7879
msg_info() << "external controller for this ForceField is detected" ;

0 commit comments

Comments
 (0)