From c1d57398d8118e4847c4666a776979dd7c8726be Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Fri, 9 Oct 2026 14:32:49 +0200 Subject: [PATCH 1/2] [Core] Simplify Data access from MultiVecId in Multi2Mapping --- .../Core/src/sofa/core/Multi2Mapping.h | 37 +++++----- .../Core/src/sofa/core/Multi2Mapping.inl | 73 ++++++++++++++----- Sofa/framework/Core/src/sofa/core/config.h.in | 7 ++ 3 files changed, 82 insertions(+), 35 deletions(-) diff --git a/Sofa/framework/Core/src/sofa/core/Multi2Mapping.h b/Sofa/framework/Core/src/sofa/core/Multi2Mapping.h index c0ead569710..7f7d11a8728 100644 --- a/Sofa/framework/Core/src/sofa/core/Multi2Mapping.h +++ b/Sofa/framework/Core/src/sofa/core/Multi2Mapping.h @@ -231,43 +231,44 @@ class Multi2Mapping : public BaseMapping } protected: - void getVecIn1Coord (const MultiVecCoordId id, type::vector< DataVecCoord_t*> &v) const + + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getVecIn1Coord (const MultiVecCoordId id, type::vector< DataVecCoord_t*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getConstVecIn1Coord(const ConstMultiVecCoordId id, type::vector*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getVecIn1Deriv (const MultiVecDerivId id, type::vector< DataVecDeriv_t*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getConstVecIn1Deriv(const ConstMultiVecDerivId id, type::vector*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getMatIn1Deriv (const MultiMatrixDerivId id, type::vector< DataMatrixDeriv_t*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getConstMatIn1Deriv(const ConstMultiMatrixDerivId id, type::vector*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getVecIn2Coord (const MultiVecCoordId id, type::vector< DataVecCoord_t*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getConstVecIn2Coord(const ConstMultiVecCoordId id, type::vector*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getVecIn2Deriv (const MultiVecDerivId id, type::vector< DataVecDeriv_t*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getConstVecIn2Deriv(const ConstMultiVecDerivId id, type::vector*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getMatIn2Deriv (const MultiMatrixDerivId id, type::vector< DataMatrixDeriv_t*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getConstMatIn2Deriv(const ConstMultiMatrixDerivId id, type::vector*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getVecOutCoord (const MultiVecCoordId id, type::vector< DataVecCoord_t*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getConstVecOutCoord(const ConstMultiVecCoordId id, type::vector*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getVecOutDeriv (const MultiVecDerivId id, type::vector< DataVecDeriv_t*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getConstVecOutDeriv(const ConstMultiVecDerivId id, type::vector*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getMatOutDeriv (const MultiMatrixDerivId id, type::vector< DataMatrixDeriv_t*> &v) const { for (unsigned int i=0; i*> &v) const + SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() void getConstMatOutDeriv(const ConstMultiMatrixDerivId id, type::vector*> &v) const { for (unsigned int i=0; i Multi2Mapping::getMech return mechToVec; } +template +auto* GetData(const StateVecAccessor& stateVecAccessor) +{ + if constexpr (vaccess == VecAccess::V_READ) + { + return stateVecAccessor.read(); + } + else + { + return stateVecAccessor.write(); + } +} + +template +void getDataVecFromMultiVecId(auto* self, const auto& links, const TMultiVecId &id, auto &v) +{ + v.reserve(links.size()); + for (unsigned int i = 0; i < links.size(); ++i) + { + auto* data = GetData(id[links[i]]); + if (data == nullptr) + { + msg_error(self) << "Cannot find a Data in '" << links[i]->getPathName() << "' associated to the id " << id; + } + v.push_back(data); + } +} + template < class In1, class In2,class Out> void Multi2Mapping::apply (const MechanicalParams* mparams, MultiVecCoordId outPos, ConstMultiVecCoordId inPos ) { type::vector*> vecOutPos; - getVecOutCoord(outPos, vecOutPos); + getDataVecFromMultiVecId(this, toModels, outPos, vecOutPos); + type::vector*> vecIn1Pos; - getConstVecIn1Coord(inPos, vecIn1Pos); + getDataVecFromMultiVecId(this, fromModels1, inPos, vecIn1Pos); + type::vector*> vecIn2Pos; - getConstVecIn2Coord(inPos, vecIn2Pos); + getDataVecFromMultiVecId(this, fromModels2, inPos, vecIn2Pos); this->apply(mparams, vecOutPos, vecIn1Pos, vecIn2Pos); } @@ -155,11 +185,14 @@ template < class In1, class In2,class Out> void Multi2Mapping::applyJ (const MechanicalParams* mparams, MultiVecDerivId outVel, ConstMultiVecDerivId inVel ) { type::vector*> vecOutVel; - getVecOutDeriv(outVel, vecOutVel); + getDataVecFromMultiVecId(this, toModels, outVel, vecOutVel); + type::vector*> vecIn1Vel; - getConstVecIn1Deriv(inVel, vecIn1Vel); + getDataVecFromMultiVecId(this, fromModels1, inVel, vecIn1Vel); + type::vector*> vecIn2Vel; - getConstVecIn2Deriv(inVel, vecIn2Vel); + getDataVecFromMultiVecId(this, fromModels2, inVel, vecIn2Vel); + this->applyJ(mparams, vecOutVel, vecIn1Vel, vecIn2Vel); } @@ -167,12 +200,14 @@ template < class In1, class In2,class Out> void Multi2Mapping::applyJT (const MechanicalParams* mparams, MultiVecDerivId inForce, ConstMultiVecDerivId outForce ) { type::vector*> vecOut1Force; - getVecIn1Deriv(inForce, vecOut1Force); + getDataVecFromMultiVecId(this, fromModels1, inForce, vecOut1Force); + type::vector*> vecOut2Force; - getVecIn2Deriv(inForce, vecOut2Force); + getDataVecFromMultiVecId(this, fromModels2, inForce, vecOut2Force); type::vector*> vecInForce; - getConstVecOutDeriv(outForce, vecInForce); + getDataVecFromMultiVecId(this, toModels, outForce, vecInForce); + this->applyJT(mparams, vecOut1Force, vecOut2Force, vecInForce); } @@ -180,12 +215,14 @@ template < class In1, class In2,class Out> void Multi2Mapping::applyJT(const ConstraintParams* cparams, MultiMatrixDerivId inConst, ConstMultiMatrixDerivId outConst ) { type::vector*> matOut1Const; - getMatIn1Deriv(inConst, matOut1Const); + getDataVecFromMultiVecId(this, fromModels1, inConst, matOut1Const); + type::vector*> matOut2Const; - getMatIn2Deriv(inConst, matOut2Const); + getDataVecFromMultiVecId(this, fromModels2, inConst, matOut2Const); type::vector*> matInConst; - getConstMatOutDeriv(outConst, matInConst); + getDataVecFromMultiVecId(this, toModels, outConst, matInConst); + this->applyJT(cparams, matOut1Const, matOut2Const, matInConst); } @@ -193,17 +230,19 @@ template < class In1, class In2,class Out> void Multi2Mapping::computeAccFromMapping(const MechanicalParams* mparams, MultiVecDerivId outAcc, ConstMultiVecDerivId inVel, ConstMultiVecDerivId inAcc ) { type::vector*> vecOutAcc; - getVecOutDeriv(outAcc, vecOutAcc); + getDataVecFromMultiVecId(toModels, outAcc, vecOutAcc); type::vector*> vecIn1Vel; - getConstVecIn1Deriv(inVel, vecIn1Vel); + getDataVecFromMultiVecId(fromModels1, inVel, vecIn1Vel); + type::vector*> vecIn1Acc; - getConstVecIn1Deriv(inAcc, vecIn1Acc); + getDataVecFromMultiVecId(fromModels1, inAcc, vecIn1Acc); type::vector*> vecIn2Vel; - getConstVecIn2Deriv(inVel, vecIn2Vel); + getDataVecFromMultiVecId(fromModels2, inVel, vecIn2Vel); + type::vector*> vecIn2Acc; - getConstVecIn2Deriv(inAcc, vecIn2Acc); + getDataVecFromMultiVecId(fromModels2, inAcc, vecIn2Acc); this->computeAccFromMapping(mparams, vecOutAcc, vecIn1Vel, vecIn2Vel,vecIn1Acc, vecIn2Acc); } diff --git a/Sofa/framework/Core/src/sofa/core/config.h.in b/Sofa/framework/Core/src/sofa/core/config.h.in index 575ffd7e041..8e691846ce4 100644 --- a/Sofa/framework/Core/src/sofa/core/config.h.in +++ b/Sofa/framework/Core/src/sofa/core/config.h.in @@ -181,3 +181,10 @@ SOFA_ATTRIBUTE_DEPRECATED("v26.12", "v27.12", "Use BaseContext instead of Contex #define SOFA_CORE_DEPRECATED_ACCFROMF() \ SOFA_ATTRIBUTE_DISABLED("v26.12", "v27.06", "Use solveLinearSystem instead of accFromF.") #endif + +#ifdef SOFA_BUILD_SOFA_CORE +#define SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() +#else +#define SOFA_CORE_DEPRECATED_GETDATAVECFROMMULTIVECID() \ +SOFA_ATTRIBUTE_DEPRECATED("v26.12", "v27.12", "") +#endif From 9930c5a3e7ead88afcfcaf2065861639ec794c67 Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Thu, 29 Oct 2026 16:51:24 +0100 Subject: [PATCH 2/2] Fix missing parameter --- Sofa/framework/Core/src/sofa/core/Multi2Mapping.inl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sofa/framework/Core/src/sofa/core/Multi2Mapping.inl b/Sofa/framework/Core/src/sofa/core/Multi2Mapping.inl index 69f8f44de1a..5348de34209 100644 --- a/Sofa/framework/Core/src/sofa/core/Multi2Mapping.inl +++ b/Sofa/framework/Core/src/sofa/core/Multi2Mapping.inl @@ -230,19 +230,19 @@ template < class In1, class In2,class Out> void Multi2Mapping::computeAccFromMapping(const MechanicalParams* mparams, MultiVecDerivId outAcc, ConstMultiVecDerivId inVel, ConstMultiVecDerivId inAcc ) { type::vector*> vecOutAcc; - getDataVecFromMultiVecId(toModels, outAcc, vecOutAcc); + getDataVecFromMultiVecId(this, toModels, outAcc, vecOutAcc); type::vector*> vecIn1Vel; - getDataVecFromMultiVecId(fromModels1, inVel, vecIn1Vel); + getDataVecFromMultiVecId(this, fromModels1, inVel, vecIn1Vel); type::vector*> vecIn1Acc; - getDataVecFromMultiVecId(fromModels1, inAcc, vecIn1Acc); + getDataVecFromMultiVecId(this, fromModels1, inAcc, vecIn1Acc); - type::vector*> vecIn2Vel; - getDataVecFromMultiVecId(fromModels2, inVel, vecIn2Vel); + type::vector*> vecIn2Vel; + getDataVecFromMultiVecId(this, fromModels2, inVel, vecIn2Vel); type::vector*> vecIn2Acc; - getDataVecFromMultiVecId(fromModels2, inAcc, vecIn2Acc); + getDataVecFromMultiVecId(this, fromModels2, inAcc, vecIn2Acc); this->computeAccFromMapping(mparams, vecOutAcc, vecIn1Vel, vecIn2Vel,vecIn1Acc, vecIn2Acc); }