Skip to content

Commit ed3c534

Browse files
alxbilgerbakpaul
andauthored
[Core] Replace raw pointer by SingleStateAccessor (#5499)
Co-authored-by: Paul Baksic <30337881+bakpaul@users.noreply.github.com>
1 parent f95579b commit ed3c534

5 files changed

Lines changed: 40 additions & 42 deletions

File tree

Sofa/Component/Constraint/Lagrangian/Correction/src/sofa/component/constraint/lagrangian/correction/LinearSolverConstraintCorrection.inl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void LinearSolverConstraintCorrection<DataTypes>::addComplianceInConstraintSpace
212212
}
213213

214214
{
215-
helper::ReadAccessor inputConstraintMatrix ( *cparams->readJ(this->mstate) );
215+
helper::ReadAccessor inputConstraintMatrix ( *cparams->readJ(this->mstate.get()) );
216216
const sofa::SignedIndex numberOfConstraints = W->rowSize();
217217
convertConstraintMatrix(numberOfConstraints, inputConstraintMatrix.ref());
218218
}
@@ -279,8 +279,8 @@ void LinearSolverConstraintCorrection< DataTypes >::applyMotionCorrection(const
279279
auto dx = sofa::helper::getWriteAccessor(dx_d);
280280

281281
const VecDeriv& correction = correction_d.getValue();
282-
const VecCoord& x_free = cparams->readX(mstate)->getValue();
283-
const VecDeriv& v_free = cparams->readV(mstate)->getValue();
282+
const VecCoord& x_free = cparams->readX(mstate.get())->getValue();
283+
const VecDeriv& v_free = cparams->readV(mstate.get())->getValue();
284284

285285
const SReal positionFactor = l_ODESolver.get()->getPositionIntegrationFactor();
286286
const SReal velocityFactor = l_ODESolver.get()->getVelocityIntegrationFactor();
@@ -307,7 +307,7 @@ void LinearSolverConstraintCorrection< DataTypes >::applyPositionCorrection(cons
307307
auto dx = sofa::helper::getWriteAccessor(dx_d);
308308

309309
const VecDeriv& correction = correction_d.getValue();
310-
const VecCoord& x_free = cparams->readX(mstate)->getValue();
310+
const VecCoord& x_free = cparams->readX(mstate.get())->getValue();
311311

312312
const SReal positionFactor = l_ODESolver.get()->getPositionIntegrationFactor();
313313
for (unsigned int i = 0; i < numDOFs; i++)
@@ -331,7 +331,7 @@ void LinearSolverConstraintCorrection< DataTypes >::applyVelocityCorrection(cons
331331
auto dv = sofa::helper::getWriteAccessor(dv_d);
332332

333333
const VecDeriv& correction = correction_d.getValue();
334-
const VecDeriv& v_free = cparams->readV(mstate)->getValue();
334+
const VecDeriv& v_free = cparams->readV(mstate.get())->getValue();
335335

336336
const SReal velocityFactor = l_ODESolver.get()->getVelocityIntegrationFactor();
337337

Sofa/Component/Constraint/Lagrangian/Correction/src/sofa/component/constraint/lagrangian/correction/PrecomputedConstraintCorrection.inl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ void PrecomputedConstraintCorrection< DataTypes >::addComplianceInConstraintSpac
415415
{
416416
m_activeDofs.clear();
417417

418-
const MatrixDeriv& c = cparams->readJ(this->mstate)->getValue();
418+
const MatrixDeriv& c = cparams->readJ(this->mstate.get())->getValue();
419419

420420
SReal factor = 1.0_sreal;
421421

@@ -615,8 +615,8 @@ void PrecomputedConstraintCorrection<DataTypes>::applyMotionCorrection(const sof
615615

616616
auto dx = sofa::helper::getWriteAccessor(dx_d);
617617

618-
const VecCoord& x_free = cparams->readX(this->mstate)->getValue();
619-
const VecDeriv& v_free = cparams->readV(this->mstate)->getValue();
618+
const VecCoord& x_free = cparams->readX(this->mstate.get())->getValue();
619+
const VecDeriv& v_free = cparams->readV(this->mstate.get())->getValue();
620620

621621
const SReal invDt = 1.0_sreal / this->getContext()->getDt();
622622

@@ -648,7 +648,7 @@ void PrecomputedConstraintCorrection<DataTypes>::applyPositionCorrection(const s
648648

649649
auto dx = sofa::helper::getWriteAccessor(dx_d);
650650

651-
const VecCoord& x_free = cparams->readX(this->mstate)->getValue();
651+
const VecCoord& x_free = cparams->readX(this->mstate.get())->getValue();
652652

653653
if (d_rotations.getValue())
654654
rotateResponse();
@@ -672,7 +672,7 @@ void PrecomputedConstraintCorrection<DataTypes>::applyVelocityCorrection(const s
672672
VecDeriv& v = *v_d.beginEdit();
673673

674674
const VecDeriv& dx = this->mstate->read(core::vec_id::write_access::dx)->getValue();
675-
const VecDeriv& v_free = cparams->readV(this->mstate)->getValue();
675+
const VecDeriv& v_free = cparams->readV(this->mstate.get())->getValue();
676676

677677
const SReal invDt = 1.0_sreal / this->getContext()->getDt();
678678

Sofa/Component/Constraint/Lagrangian/Correction/src/sofa/component/constraint/lagrangian/correction/UncoupledConstraintCorrection.inl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ void UncoupledConstraintCorrection<DataTypes>::addComplianceInConstraintSpace(co
357357
if(!this->isComponentStateValid())
358358
return;
359359

360-
const MatrixDeriv& constraints = cparams->readJ(this->mstate)->getValue() ;
360+
const MatrixDeriv& constraints = cparams->readJ(this->mstate.get())->getValue() ;
361361
VecReal comp = d_compliance.getValue();
362362
Real comp0 = d_defaultCompliance.getValue();
363363
const bool verbose = d_verbose.getValue();
@@ -533,8 +533,8 @@ void UncoupledConstraintCorrection<DataTypes>::applyMotionCorrection(const core:
533533
VecCoord& x = *x_d.beginEdit();
534534
VecDeriv& v = *v_d.beginEdit();
535535

536-
const VecCoord& x_free = cparams->readX(this->mstate)->getValue();
537-
const VecDeriv& v_free = cparams->readV(this->mstate)->getValue();
536+
const VecCoord& x_free = cparams->readX(this->mstate.get())->getValue();
537+
const VecDeriv& v_free = cparams->readV(this->mstate.get())->getValue();
538538

539539
const bool useOdeIntegrationFactors = d_useOdeSolverIntegrationFactors.getValue();
540540

@@ -566,7 +566,7 @@ void UncoupledConstraintCorrection<DataTypes>::applyPositionCorrection(const cor
566566

567567
VecCoord& x = *x_d.beginEdit();
568568

569-
const VecCoord& x_free = cparams->readX(this->mstate)->getValue();
569+
const VecCoord& x_free = cparams->readX(this->mstate.get())->getValue();
570570

571571
const bool useOdeIntegrationFactors = d_useOdeSolverIntegrationFactors.getValue();
572572

@@ -594,7 +594,7 @@ void UncoupledConstraintCorrection<DataTypes>::applyVelocityCorrection(const cor
594594

595595
VecDeriv& v = *v_d.beginEdit();
596596

597-
const VecDeriv& v_free = cparams->readV(this->mstate)->getValue();
597+
const VecDeriv& v_free = cparams->readV(this->mstate.get())->getValue();
598598

599599
const bool useOdeIntegrationFactors = d_useOdeSolverIntegrationFactors.getValue();
600600

Sofa/framework/Core/src/sofa/core/behavior/ConstraintCorrection.h

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <sofa/core/behavior/BaseConstraintCorrection.h>
2525
#include <sofa/core/behavior/MechanicalState.h>
2626
#include <sofa/core/objectmodel/Link.h>
27-
27+
#include <sofa/core/behavior/SingleStateAccessor.h>
2828

2929
namespace sofa::core::behavior
3030
{
@@ -33,10 +33,12 @@ namespace sofa::core::behavior
3333
* Component computing constraint forces within a simulated body using the compliance method.
3434
*/
3535
template<class TDataTypes>
36-
class ConstraintCorrection : public BaseConstraintCorrection
36+
class ConstraintCorrection : public BaseConstraintCorrection, public SingleStateAccessor<TDataTypes>
3737
{
3838
public:
39-
SOFA_ABSTRACT_CLASS(SOFA_TEMPLATE(ConstraintCorrection, TDataTypes), BaseConstraintCorrection);
39+
SOFA_ABSTRACT_CLASS2(SOFA_TEMPLATE(ConstraintCorrection, TDataTypes),
40+
BaseConstraintCorrection,
41+
SOFA_TEMPLATE(SingleStateAccessor, TDataTypes))
4042

4143
typedef TDataTypes DataTypes;
4244
typedef typename DataTypes::Real Real;
@@ -50,12 +52,13 @@ class ConstraintCorrection : public BaseConstraintCorrection
5052
typedef typename DataTypes::MatrixDeriv::RowIterator MatrixDerivRowIterator;
5153
typedef typename DataTypes::MatrixDeriv::ColIterator MatrixDerivColIterator;
5254

55+
using SingleStateAccessor<TDataTypes>::mstate;
56+
5357
protected:
5458
/// Default Constructor
5559
explicit ConstraintCorrection(MechanicalState< DataTypes > *ms = nullptr)
56-
: Inherit1()
60+
: Inherit1(), Inherit2(ms)
5761
, l_constraintsolvers(initLink("constraintSolvers", "Constraint solvers using this constraint correction"))
58-
, mstate(ms)
5962
{}
6063

6164
/// Default Destructor
@@ -150,19 +153,11 @@ class ConstraintCorrection : public BaseConstraintCorrection
150153
return BaseObject::canCreate(obj, context, arg);
151154
}
152155

153-
MechanicalState<DataTypes> *getMState() const
154-
{
155-
return mstate;
156-
}
157-
158156
void setMState(MechanicalState<DataTypes> *_mstate)
159157
{
160158
mstate = _mstate;
161159
}
162160

163-
protected:
164-
MechanicalState<DataTypes> *mstate;
165-
166161
private:
167162
/// Converts constraint force from the constraints space to the motion space and accumulates it in f vector
168163
///

Sofa/framework/Core/src/sofa/core/behavior/ConstraintCorrection.inl

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ namespace sofa::core::behavior
3131
template< class DataTypes >
3232
void ConstraintCorrection< DataTypes >::init()
3333
{
34-
Inherit1::init();
34+
Inherit2::init();
3535

36-
mstate = dynamic_cast< behavior::MechanicalState< DataTypes >* >(getContext()->getMechanicalState());
36+
if (!mstate)
37+
{
38+
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid);
39+
}
3740
}
3841

3942
template< class DataTypes >
@@ -80,10 +83,10 @@ void ConstraintCorrection< DataTypes >::applyMotionCorrection(const core::Constr
8083
{
8184
if (mstate)
8285
{
83-
Data< VecCoord > *x_d = x[mstate].write();
84-
Data< VecDeriv > *v_d = v[mstate].write();
85-
Data< VecDeriv > *dx_d = dx[mstate].write();
86-
const Data< VecDeriv > *correction_d = correction[mstate].read();
86+
Data< VecCoord > *x_d = x[mstate.get()].write();
87+
Data< VecDeriv > *v_d = v[mstate.get()].write();
88+
Data< VecDeriv > *dx_d = dx[mstate.get()].write();
89+
const Data< VecDeriv > *correction_d = correction[mstate.get()].read();
8790

8891
if (x_d && v_d && dx_d && correction_d)
8992
{
@@ -98,9 +101,9 @@ void ConstraintCorrection< DataTypes >::applyPositionCorrection(const core::Cons
98101
{
99102
if (mstate)
100103
{
101-
Data< VecCoord > *x_d = x[mstate].write();
102-
Data< VecDeriv > *dx_d = dx[mstate].write();
103-
const Data< VecDeriv > *correction_d = correction[mstate].read();
104+
Data< VecCoord > *x_d = x[mstate.get()].write();
105+
Data< VecDeriv > *dx_d = dx[mstate.get()].write();
106+
const Data< VecDeriv > *correction_d = correction[mstate.get()].read();
104107

105108
if (x_d && dx_d && correction_d)
106109
{
@@ -115,9 +118,9 @@ void ConstraintCorrection< DataTypes >::applyVelocityCorrection(const core::Cons
115118
{
116119
if (mstate)
117120
{
118-
Data< VecDeriv >* v_d = v[mstate].write();
119-
Data< VecDeriv >* dv_d = dv[mstate].write();
120-
const Data< VecDeriv >* correction_d = correction[mstate].read();
121+
Data< VecDeriv >* v_d = v[mstate.get()].write();
122+
Data< VecDeriv >* dv_d = dv[mstate.get()].write();
123+
const Data< VecDeriv >* correction_d = correction[mstate.get()].read();
121124

122125
if (v_d && dv_d && correction_d)
123126
{
@@ -141,8 +144,8 @@ void ConstraintCorrection< DataTypes >::addConstraintForceInMotionSpace(const co
141144
{
142145
if (mstate)
143146
{
144-
Data< VecDeriv > *f_d = f[mstate].write();
145-
const Data< MatrixDeriv > * j_d = j[mstate].read();
147+
Data< VecDeriv > *f_d = f[mstate.get()].write();
148+
const Data< MatrixDeriv > * j_d = j[mstate.get()].read();
146149
if (f_d && j_d)
147150
{
148151
addConstraintForceInMotionSpace(cparams,*f_d, *j_d, lambda);

0 commit comments

Comments
 (0)