From 7a15c99dd7b57cd0ce5ff5c0dfc1e73257bb7d57 Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Fri, 25 Apr 2025 16:10:17 +0200 Subject: [PATCH 1/3] [Core] Rename BaseConstraint to BaseLagrangianConstraint --- .../animationloop/ConstraintAnimationLoop.cpp | 2 +- .../model/BaseContactLagrangianConstraint.h | 18 +-- .../model/BilateralConstraintResolution.h | 2 +- .../model/BilateralLagrangianConstraint.h | 4 +- .../solver/GenericConstraintSolver.h | 2 +- .../lagrangian/solver/LCPConstraintSolver.cpp | 2 +- .../lagrangian/solver/LCPConstraintSolver.h | 24 ++-- .../visitors/ConstraintStoreLambdaVisitor.cpp | 4 +- ...chanicalGetConstraintResolutionVisitor.cpp | 4 +- Sofa/framework/Core/CMakeLists.txt | 3 +- .../src/sofa/core/behavior/BaseConstraint.h | 102 +-------------- .../core/behavior/BaseInteractionConstraint.h | 6 +- ...raint.cpp => BaseLagrangianConstraint.cpp} | 12 +- .../core/behavior/BaseLagrangianConstraint.h | 123 ++++++++++++++++++ .../Core/src/sofa/core/behavior/Constraint.h | 7 +- .../Core/src/sofa/core/behavior/Mass.inl | 2 +- .../Core/src/sofa/core/behavior/fwd.h | 2 +- Sofa/framework/Core/src/sofa/core/fwd.cpp | 2 +- Sofa/framework/Core/src/sofa/core/fwd.h | 2 +- .../Core/src/sofa/core/objectmodel/Base.h | 2 +- .../MechanicalGetConstraintInfoVisitor.cpp | 2 +- .../MechanicalGetConstraintInfoVisitor.h | 12 +- 22 files changed, 186 insertions(+), 153 deletions(-) rename Sofa/framework/Core/src/sofa/core/behavior/{BaseConstraint.cpp => BaseLagrangianConstraint.cpp} (82%) create mode 100644 Sofa/framework/Core/src/sofa/core/behavior/BaseLagrangianConstraint.h diff --git a/Sofa/Component/AnimationLoop/src/sofa/component/animationloop/ConstraintAnimationLoop.cpp b/Sofa/Component/AnimationLoop/src/sofa/component/animationloop/ConstraintAnimationLoop.cpp index d732bd93605..c37dc40f555 100644 --- a/Sofa/Component/AnimationLoop/src/sofa/component/animationloop/ConstraintAnimationLoop.cpp +++ b/Sofa/Component/AnimationLoop/src/sofa/component/animationloop/ConstraintAnimationLoop.cpp @@ -43,7 +43,7 @@ #include -#include ///< ConstraintResolution. +#include ///< ConstraintResolution. #include diff --git a/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/BaseContactLagrangianConstraint.h b/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/BaseContactLagrangianConstraint.h index 512a06ebd5b..e1e30c63c21 100644 --- a/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/BaseContactLagrangianConstraint.h +++ b/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/BaseContactLagrangianConstraint.h @@ -61,15 +61,15 @@ class BaseContactLagrangianConstraint : public core::behavior::PairInteractionCo typedef typename Coord::value_type Real; typedef typename core::behavior::MechanicalState MechanicalState; - typedef core::behavior::BaseConstraint::ConstraintBlockInfo ConstraintBlockInfo; - typedef core::behavior::BaseConstraint::PersistentID PersistentID; - typedef core::behavior::BaseConstraint::ConstCoord ConstCoord; - - typedef core::behavior::BaseConstraint::VecConstraintBlockInfo VecConstraintBlockInfo; - typedef core::behavior::BaseConstraint::VecPersistentID VecPersistentID; - typedef core::behavior::BaseConstraint::VecConstCoord VecConstCoord; - typedef core::behavior::BaseConstraint::VecConstDeriv VecConstDeriv; - typedef core::behavior::BaseConstraint::VecConstArea VecConstArea; + typedef core::behavior::BaseLagrangianConstraint::ConstraintBlockInfo ConstraintBlockInfo; + typedef core::behavior::BaseLagrangianConstraint::PersistentID PersistentID; + typedef core::behavior::BaseLagrangianConstraint::ConstCoord ConstCoord; + + typedef core::behavior::BaseLagrangianConstraint::VecConstraintBlockInfo VecConstraintBlockInfo; + typedef core::behavior::BaseLagrangianConstraint::VecPersistentID VecPersistentID; + typedef core::behavior::BaseLagrangianConstraint::VecConstCoord VecConstCoord; + typedef core::behavior::BaseLagrangianConstraint::VecConstDeriv VecConstDeriv; + typedef core::behavior::BaseLagrangianConstraint::VecConstArea VecConstArea; typedef core::objectmodel::Data DataVecCoord; typedef core::objectmodel::Data DataVecDeriv; diff --git a/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/BilateralConstraintResolution.h b/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/BilateralConstraintResolution.h index 3c4d0f1bcca..429f5e735ff 100644 --- a/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/BilateralConstraintResolution.h +++ b/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/BilateralConstraintResolution.h @@ -23,7 +23,7 @@ #include #include -#include +#include #include #include #include diff --git a/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/BilateralLagrangianConstraint.h b/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/BilateralLagrangianConstraint.h index 4d7da7fdd4a..9168060cec0 100644 --- a/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/BilateralLagrangianConstraint.h +++ b/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/BilateralLagrangianConstraint.h @@ -41,7 +41,7 @@ namespace sofa::component::constraint::lagrangian::model /// These 'using' are in a per-file namespace so they will not leak /// and polluate the standard namespace. -using sofa::core::behavior::BaseConstraint ; +using sofa::core::behavior::BaseLagrangianConstraint ; using sofa::core::behavior::ConstraintResolution ; using sofa::core::behavior::PairInteractionConstraint ; using sofa::core::ConstraintParams ; @@ -81,7 +81,7 @@ class BilateralLagrangianConstraint : public PairInteractionConstraint MechanicalState; - typedef BaseConstraint::PersistentID PersistentID; + typedef BaseLagrangianConstraint::PersistentID PersistentID; typedef Data DataVecCoord; typedef Data DataVecDeriv; diff --git a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/GenericConstraintSolver.h b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/GenericConstraintSolver.h index 761ec076298..e7a3a639cfc 100644 --- a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/GenericConstraintSolver.h +++ b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/GenericConstraintSolver.h @@ -25,7 +25,7 @@ #include #include -#include +#include #include #include diff --git a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.cpp b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.cpp index 541091eb613..c50fb7cb80a 100644 --- a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.cpp +++ b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.cpp @@ -676,7 +676,7 @@ void LCPConstraintSolver::computeInitialGuess() for (const ConstraintBlockInfo& info : constraintBlockInfo) { if (!info.hasId) continue; - std::map::const_iterator previt = _previousConstraints.find(info.parent); + std::map::const_iterator previt = _previousConstraints.find(info.parent); if (previt == _previousConstraints.end()) continue; const ConstraintBlockBuf& buf = previt->second; const int c0 = info.const0; diff --git a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.h b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.h index cacdb319c29..b4217228f20 100644 --- a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.h +++ b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.h @@ -24,7 +24,7 @@ #include #include -#include +#include #include #include @@ -147,17 +147,17 @@ class SOFA_COMPONENT_CONSTRAINT_LAGRANGIAN_SOLVER_API LCPConstraintSolver : publ std::vector _cclist_elem1; std::vector _cclist_elem2; - typedef core::behavior::BaseConstraint::ConstraintBlockInfo ConstraintBlockInfo; - typedef core::behavior::BaseConstraint::PersistentID PersistentID; - typedef core::behavior::BaseConstraint::ConstCoord ConstCoord; - typedef core::behavior::BaseConstraint::ConstDeriv ConstDeriv; - typedef core::behavior::BaseConstraint::ConstArea ConstArea; + typedef core::behavior::BaseLagrangianConstraint::ConstraintBlockInfo ConstraintBlockInfo; + typedef core::behavior::BaseLagrangianConstraint::PersistentID PersistentID; + typedef core::behavior::BaseLagrangianConstraint::ConstCoord ConstCoord; + typedef core::behavior::BaseLagrangianConstraint::ConstDeriv ConstDeriv; + typedef core::behavior::BaseLagrangianConstraint::ConstArea ConstArea; - typedef core::behavior::BaseConstraint::VecConstraintBlockInfo VecConstraintBlockInfo; - typedef core::behavior::BaseConstraint::VecPersistentID VecPersistentID; - typedef core::behavior::BaseConstraint::VecConstCoord VecConstCoord; - typedef core::behavior::BaseConstraint::VecConstDeriv VecConstDeriv; - typedef core::behavior::BaseConstraint::VecConstArea VecConstArea; + typedef core::behavior::BaseLagrangianConstraint::VecConstraintBlockInfo VecConstraintBlockInfo; + typedef core::behavior::BaseLagrangianConstraint::VecPersistentID VecPersistentID; + typedef core::behavior::BaseLagrangianConstraint::VecConstCoord VecConstCoord; + typedef core::behavior::BaseLagrangianConstraint::VecConstDeriv VecConstDeriv; + typedef core::behavior::BaseLagrangianConstraint::VecConstArea VecConstArea; class ConstraintBlockBuf { @@ -166,7 +166,7 @@ class SOFA_COMPONENT_CONSTRAINT_LAGRANGIAN_SOLVER_API LCPConstraintSolver : publ int nbLines; ///< how many dofs (i.e. lines in the matrix) are used by each constraint }; - std::map _previousConstraints; + std::map _previousConstraints; type::vector< SReal > _previousForces; type::vector< VecConstraintBlockInfo > hierarchy_constraintBlockInfo; diff --git a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/visitors/ConstraintStoreLambdaVisitor.cpp b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/visitors/ConstraintStoreLambdaVisitor.cpp index 398fc1feffc..fa5b3efd272 100644 --- a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/visitors/ConstraintStoreLambdaVisitor.cpp +++ b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/visitors/ConstraintStoreLambdaVisitor.cpp @@ -19,7 +19,7 @@ * * * Contact information: contact@sofa-framework.org * ******************************************************************************/ -#include +#include #include #include @@ -35,7 +35,7 @@ ConstraintStoreLambdaVisitor::ConstraintStoreLambdaVisitor(const sofa::core::Con simulation::Visitor::Result ConstraintStoreLambdaVisitor::fwdConstraintSet(simulation::Node* node, core::behavior::BaseConstraintSet* cSet) { - if (core::behavior::BaseConstraint *c = dynamic_cast(cSet) ) + if (core::behavior::BaseLagrangianConstraint *c = dynamic_cast(cSet) ) { const ctime_t t0 = begin(node, c); c->storeLambda(m_cParams, m_cParams->lambda(), m_lambda); diff --git a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/visitors/MechanicalGetConstraintResolutionVisitor.cpp b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/visitors/MechanicalGetConstraintResolutionVisitor.cpp index 3eb9ce87fa3..fda279d678c 100644 --- a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/visitors/MechanicalGetConstraintResolutionVisitor.cpp +++ b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/visitors/MechanicalGetConstraintResolutionVisitor.cpp @@ -22,7 +22,7 @@ #include #include #include -#include +#include namespace sofa::component::constraint::lagrangian::solver { @@ -39,7 +39,7 @@ MechanicalGetConstraintResolutionVisitor::MechanicalGetConstraintResolutionVisit MechanicalGetConstraintResolutionVisitor::Result MechanicalGetConstraintResolutionVisitor::fwdConstraintSet(simulation::Node* node, core::behavior::BaseConstraintSet* cSet) { - if (core::behavior::BaseConstraint *c=cSet->toBaseConstraint()) + if (core::behavior::BaseLagrangianConstraint *c=cSet->toBaseConstraint()) { const ctime_t t0 = begin(node, c); c->getConstraintResolution(cparams, _res, _offset); diff --git a/Sofa/framework/Core/CMakeLists.txt b/Sofa/framework/Core/CMakeLists.txt index 3574a69d674..5948823e627 100644 --- a/Sofa/framework/Core/CMakeLists.txt +++ b/Sofa/framework/Core/CMakeLists.txt @@ -58,6 +58,7 @@ set(HEADER_FILES ${SRC_ROOT}/behavior/BaseInteractionConstraint.h ${SRC_ROOT}/behavior/BaseInteractionForceField.h ${SRC_ROOT}/behavior/BaseInteractionProjectiveConstraintSet.h + ${SRC_ROOT}/behavior/BaseLagrangianConstraint.h ${SRC_ROOT}/behavior/BaseLinearSolver.h ${SRC_ROOT}/behavior/BaseLocalForceFieldMatrix.h ${SRC_ROOT}/behavior/BaseLocalMassMatrix.h @@ -234,11 +235,11 @@ set(SOURCE_FILES ${SRC_ROOT}/State.cpp ${SRC_ROOT}/VecId.cpp ${SRC_ROOT}/behavior/BaseAnimationLoop.cpp - ${SRC_ROOT}/behavior/BaseConstraint.cpp ${SRC_ROOT}/behavior/BaseConstraintCorrection.cpp ${SRC_ROOT}/behavior/BaseConstraintSet.cpp ${SRC_ROOT}/behavior/BaseForceField.cpp ${SRC_ROOT}/behavior/BaseInteractionForceField.cpp + ${SRC_ROOT}/behavior/BaseLagrangianConstraint.cpp ${SRC_ROOT}/behavior/BaseLinearSolver.cpp ${SRC_ROOT}/behavior/BaseMass.cpp ${SRC_ROOT}/behavior/BaseMatrixLinearSystem.cpp diff --git a/Sofa/framework/Core/src/sofa/core/behavior/BaseConstraint.h b/Sofa/framework/Core/src/sofa/core/behavior/BaseConstraint.h index ba176e79f8b..31ec84e088e 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/BaseConstraint.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/BaseConstraint.h @@ -21,103 +21,11 @@ ******************************************************************************/ #pragma once -#include -#include +#include -namespace sofa::core::behavior -{ +SOFA_HEADER_DEPRECATED("v25.06", "v26.06", "sofa/core/behavior/BaseLagrangianConstraint.h") -/** - * \brief Component computing constraints within a simulated body. - * - * This class defines the abstract API common to all constraints. - * A BaseConstraint computes constraints applied to one or more simulated body - * given its current position and velocity. - * - * Constraints can be internal to a given body (attached to one MechanicalState, - * see the Constraint class), or link several bodies together (such as contacts, - * see the InteractionConstraint class). - * - */ -class SOFA_CORE_API BaseConstraint : public BaseConstraintSet +namespace sofa::core::behavior { -public: - SOFA_ABSTRACT_CLASS(BaseConstraint, BaseConstraintSet); - SOFA_BASE_CAST_IMPLEMENTATION(BaseConstraint) - -protected: - BaseConstraint() {} - ~BaseConstraint() override {} - -private: - BaseConstraint(const BaseConstraint& n) = delete ; - BaseConstraint& operator=(const BaseConstraint& n) = delete ; - -public: - /// Get the ID of the group containing this constraint. This ID is used to specify which constraints are solved by which solver, by specifying in each solver which groups of constraints it should handle. - int getGroup() const; - - /// Set the ID of the group containing this constraint. This ID is used to specify which constraints are solved by which solver, by specifying in each solver which groups of constraints it should handle. - void setGroup(int g); - - typedef long long PersistentID; - typedef type::vector VecPersistentID; - typedef type::Vec<3,int> ConstCoord; - typedef type::vector VecConstCoord; - typedef type::Vec<3,double> ConstDeriv; - typedef type::vector VecConstDeriv; - typedef double ConstArea; - typedef type::vector VecConstArea; - - class ConstraintBlockInfo - { - public: - BaseConstraint* parent; - int const0; ///< index of first constraint - int nbLines; ///< how many dofs (i.e. lines in the matrix) are used by each constraint - int nbGroups; ///< how many groups of constraints are active - bool hasId; ///< true if this constraint has persistent ID information - bool hasPosition; ///< true if this constraint has coordinates information - bool hasDirection; ///< true if this constraint has direction information - bool hasArea; ///< true if this constraint has area information - int offsetId; ///< index of first constraint group info in vector of persistent ids and coordinates - int offsetPosition; ///< index of first constraint group info in vector of coordinates - int offsetDirection; ///< index of first constraint info in vector of directions - int offsetArea; ///< index of first constraint group info in vector of areas - ConstraintBlockInfo() : parent(nullptr), const0(0), nbLines(1), nbGroups(0), hasId(false), hasPosition(false), hasDirection(false), hasArea(false), offsetId(0), offsetPosition(0), offsetDirection(0), offsetArea(0) - {} - }; - typedef type::vector VecConstraintBlockInfo; - - /// Get information for each constraint: pointer to parent BaseConstraint, unique persistent ID, 3D position - /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) and resolution parameters (smoothness, ...) - virtual void getConstraintInfo(const ConstraintParams* cParams, VecConstraintBlockInfo& blocks, VecPersistentID& ids, VecConstCoord& positions, VecConstDeriv& directions, VecConstArea& areas); - - /// Add the corresponding ConstraintResolution using the offset parameter - /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) and resolution parameters (smoothness, ...) - /// \param resTab is the result vector that contains the constraint resolution algorithms - virtual void getConstraintResolution(const ConstraintParams* cParams, std::vector &resTab, unsigned int &offset); - - virtual void getConstraintResolution(std::vector &resTab, unsigned int &offset); - - type::vector getIdentifiers() - { - type::vector ids = getBaseConstraintIdentifiers(); - ids.push_back("Base"); - return ids; - } - - virtual type::vector getBaseConstraintIdentifiers() = 0; - - - /// Store the constraint lambda at the constraint dofs at the given VecDerivId location. - /// res = J^t * lambda. - /// J is the sparse matrix containing the constraint jacobian that was used to build the constraint matrix ( see BaseConstraintSet::buildConstraintMatrix ). - /// \param cParams stores the id of the state vectors used during the constraint solving step. Mostly it helps retrieving the MatrixDerivId where - /// the constraint jacobian J is stored. - /// \param res is the state vector Id where to store the result. - /// \param lambda is the vector of scalar constraint impulses. The direction are stored in the MatrixDerivId stored in the cParams. - virtual void storeLambda(const ConstraintParams* cParams, MultiVecDerivId res, const sofa::linearalgebra::BaseVector* lambda) = 0; -}; - -} // namespace sofa::core::behavior +using BaseConstraint SOFA_ATTRIBUTE_DEPRECATED("v25.06", "v26.06", "BaseConstraint has been renamed to BaseLagrangianConstraint") = BaseLagrangianConstraint; +} diff --git a/Sofa/framework/Core/src/sofa/core/behavior/BaseInteractionConstraint.h b/Sofa/framework/Core/src/sofa/core/behavior/BaseInteractionConstraint.h index 9243cab1ef8..a437acca2d2 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/BaseInteractionConstraint.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/BaseInteractionConstraint.h @@ -21,7 +21,7 @@ ******************************************************************************/ #pragma once -#include +#include #include namespace sofa::core::behavior @@ -34,10 +34,10 @@ namespace sofa::core::behavior * bodies given their current positions and velocities. * */ -class SOFA_CORE_API BaseInteractionConstraint : public BaseConstraint, public virtual StateAccessor +class SOFA_CORE_API BaseInteractionConstraint : public BaseLagrangianConstraint, public virtual StateAccessor { public: - SOFA_ABSTRACT_CLASS2(BaseInteractionConstraint, BaseConstraint, StateAccessor); + SOFA_ABSTRACT_CLASS2(BaseInteractionConstraint, BaseLagrangianConstraint, StateAccessor); SOFA_BASE_CAST_IMPLEMENTATION(BaseInteractionConstraint) /// Get the first MechanicalState diff --git a/Sofa/framework/Core/src/sofa/core/behavior/BaseConstraint.cpp b/Sofa/framework/Core/src/sofa/core/behavior/BaseLagrangianConstraint.cpp similarity index 82% rename from Sofa/framework/Core/src/sofa/core/behavior/BaseConstraint.cpp rename to Sofa/framework/Core/src/sofa/core/behavior/BaseLagrangianConstraint.cpp index 0d6ccf41939..2e7cebb9401 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/BaseConstraint.cpp +++ b/Sofa/framework/Core/src/sofa/core/behavior/BaseLagrangianConstraint.cpp @@ -19,21 +19,21 @@ * * * Contact information: contact@sofa-framework.org * ******************************************************************************/ -#include +#include namespace sofa::core::behavior { -int BaseConstraint::getGroup() const +int BaseLagrangianConstraint::getGroup() const { return group.getValue(); } -void BaseConstraint::setGroup(int g) +void BaseLagrangianConstraint::setGroup(int g) { group.setValue(g); } -void BaseConstraint::getConstraintInfo(const ConstraintParams* cParams, VecConstraintBlockInfo& blocks, +void BaseLagrangianConstraint::getConstraintInfo(const ConstraintParams* cParams, VecConstraintBlockInfo& blocks, VecPersistentID& ids, VecConstCoord& positions, VecConstDeriv& directions, VecConstArea& areas) { SOFA_UNUSED(cParams); @@ -45,14 +45,14 @@ void BaseConstraint::getConstraintInfo(const ConstraintParams* cParams, VecConst } -void BaseConstraint::getConstraintResolution(const ConstraintParams* cParams, +void BaseLagrangianConstraint::getConstraintResolution(const ConstraintParams* cParams, std::vector& resTab, unsigned& offset) { getConstraintResolution(resTab, offset); SOFA_UNUSED(cParams); } -void BaseConstraint::getConstraintResolution(std::vector& resTab, unsigned& offset) +void BaseLagrangianConstraint::getConstraintResolution(std::vector& resTab, unsigned& offset) { SOFA_UNUSED(resTab); SOFA_UNUSED(offset); diff --git a/Sofa/framework/Core/src/sofa/core/behavior/BaseLagrangianConstraint.h b/Sofa/framework/Core/src/sofa/core/behavior/BaseLagrangianConstraint.h new file mode 100644 index 00000000000..49eeb0cea1b --- /dev/null +++ b/Sofa/framework/Core/src/sofa/core/behavior/BaseLagrangianConstraint.h @@ -0,0 +1,123 @@ +/****************************************************************************** +* SOFA, Simulation Open-Framework Architecture * +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * +* * +* This program is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by * +* the Free Software Foundation; either version 2.1 of the License, or (at * +* your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this program. If not, see . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#pragma once + +#include +#include + +namespace sofa::core::behavior +{ + +/** + * \brief Component computing constraints within a simulated body. + * + * This class defines the abstract API common to all constraints. + * A BaseConstraint computes constraints applied to one or more simulated body + * given its current position and velocity. + * + * Constraints can be internal to a given body (attached to one MechanicalState, + * see the Constraint class), or link several bodies together (such as contacts, + * see the InteractionConstraint class). + * + */ +class SOFA_CORE_API BaseLagrangianConstraint : public BaseConstraintSet +{ +public: + SOFA_ABSTRACT_CLASS(BaseLagrangianConstraint, BaseConstraintSet); + SOFA_BASE_CAST_IMPLEMENTATION(BaseLagrangianConstraint) + +protected: + BaseLagrangianConstraint() {} + ~BaseLagrangianConstraint() override {} + +private: + BaseLagrangianConstraint(const BaseLagrangianConstraint& n) = delete ; + BaseLagrangianConstraint& operator=(const BaseLagrangianConstraint& n) = delete ; + +public: + /// Get the ID of the group containing this constraint. This ID is used to specify which constraints are solved by which solver, by specifying in each solver which groups of constraints it should handle. + int getGroup() const; + + /// Set the ID of the group containing this constraint. This ID is used to specify which constraints are solved by which solver, by specifying in each solver which groups of constraints it should handle. + void setGroup(int g); + + typedef long long PersistentID; + typedef type::vector VecPersistentID; + typedef type::Vec<3,int> ConstCoord; + typedef type::vector VecConstCoord; + typedef type::Vec<3,double> ConstDeriv; + typedef type::vector VecConstDeriv; + typedef double ConstArea; + typedef type::vector VecConstArea; + + class ConstraintBlockInfo + { + public: + BaseLagrangianConstraint* parent; + int const0; ///< index of first constraint + int nbLines; ///< how many dofs (i.e. lines in the matrix) are used by each constraint + int nbGroups; ///< how many groups of constraints are active + bool hasId; ///< true if this constraint has persistent ID information + bool hasPosition; ///< true if this constraint has coordinates information + bool hasDirection; ///< true if this constraint has direction information + bool hasArea; ///< true if this constraint has area information + int offsetId; ///< index of first constraint group info in vector of persistent ids and coordinates + int offsetPosition; ///< index of first constraint group info in vector of coordinates + int offsetDirection; ///< index of first constraint info in vector of directions + int offsetArea; ///< index of first constraint group info in vector of areas + ConstraintBlockInfo() : parent(nullptr), const0(0), nbLines(1), nbGroups(0), hasId(false), hasPosition(false), hasDirection(false), hasArea(false), offsetId(0), offsetPosition(0), offsetDirection(0), offsetArea(0) + {} + }; + typedef type::vector VecConstraintBlockInfo; + + /// Get information for each constraint: pointer to parent BaseConstraint, unique persistent ID, 3D position + /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) and resolution parameters (smoothness, ...) + virtual void getConstraintInfo(const ConstraintParams* cParams, VecConstraintBlockInfo& blocks, VecPersistentID& ids, VecConstCoord& positions, VecConstDeriv& directions, VecConstArea& areas); + + /// Add the corresponding ConstraintResolution using the offset parameter + /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) and resolution parameters (smoothness, ...) + /// \param resTab is the result vector that contains the constraint resolution algorithms + virtual void getConstraintResolution(const ConstraintParams* cParams, std::vector &resTab, unsigned int &offset); + + virtual void getConstraintResolution(std::vector &resTab, unsigned int &offset); + + type::vector getIdentifiers() + { + type::vector ids = getBaseConstraintIdentifiers(); + ids.push_back("Base"); + return ids; + } + + virtual type::vector getBaseConstraintIdentifiers() = 0; + + + /// Store the constraint lambda at the constraint dofs at the given VecDerivId location. + /// res = J^t * lambda. + /// J is the sparse matrix containing the constraint jacobian that was used to build the constraint matrix ( see BaseConstraintSet::buildConstraintMatrix ). + /// \param cParams stores the id of the state vectors used during the constraint solving step. Mostly it helps retrieving the MatrixDerivId where + /// the constraint jacobian J is stored. + /// \param res is the state vector Id where to store the result. + /// \param lambda is the vector of scalar constraint impulses. The direction are stored in the MatrixDerivId stored in the cParams. + virtual void storeLambda(const ConstraintParams* cParams, MultiVecDerivId res, const sofa::linearalgebra::BaseVector* lambda) = 0; +}; + +} // namespace sofa::core::behavior diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Constraint.h b/Sofa/framework/Core/src/sofa/core/behavior/Constraint.h index bc3c0a8ab35..8a4e5b8c722 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Constraint.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/Constraint.h @@ -22,10 +22,11 @@ #pragma once #include -#include +#include #include #include +#include namespace sofa::core::behavior { @@ -40,10 +41,10 @@ namespace sofa::core::behavior * */ template -class Constraint : public BaseConstraint, public SingleStateAccessor +class Constraint : public BaseLagrangianConstraint, public SingleStateAccessor { public: - SOFA_CLASS2(SOFA_TEMPLATE(Constraint, DataTypes), BaseConstraint, SOFA_TEMPLATE(SingleStateAccessor, DataTypes)); + SOFA_CLASS2(SOFA_TEMPLATE(Constraint, DataTypes), BaseLagrangianConstraint, SOFA_TEMPLATE(SingleStateAccessor, DataTypes)); typedef typename DataTypes::Real Real; typedef typename DataTypes::VecCoord VecCoord; diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl b/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl index c802ed94c79..88f94b7bea5 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl +++ b/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Sofa/framework/Core/src/sofa/core/behavior/fwd.h b/Sofa/framework/Core/src/sofa/core/behavior/fwd.h index 11019c4f5d2..a6c4927f9ac 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/fwd.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/fwd.h @@ -24,7 +24,7 @@ namespace sofa::core::behavior { class BaseAnimationLoop; - class BaseConstraint; + class BaseLagrangianConstraint; class BaseConstraintCorrection; class BaseController; class BaseForceField; diff --git a/Sofa/framework/Core/src/sofa/core/fwd.cpp b/Sofa/framework/Core/src/sofa/core/fwd.cpp index f9f2a857d1a..6a2de45217e 100644 --- a/Sofa/framework/Core/src/sofa/core/fwd.cpp +++ b/Sofa/framework/Core/src/sofa/core/fwd.cpp @@ -43,7 +43,7 @@ #include #include #include -#include +#include #include diff --git a/Sofa/framework/Core/src/sofa/core/fwd.h b/Sofa/framework/Core/src/sofa/core/fwd.h index 83dcd7fec12..622a30e1394 100644 --- a/Sofa/framework/Core/src/sofa/core/fwd.h +++ b/Sofa/framework/Core/src/sofa/core/fwd.h @@ -100,7 +100,7 @@ class BaseForceField; class BaseMass; class BaseMechanicalState; class BaseAnimationLoop; -class BaseConstraint; +class BaseLagrangianConstraint; class BaseConstraintSet; class ConstraintSolver; class ConstraintResolution; diff --git a/Sofa/framework/Core/src/sofa/core/objectmodel/Base.h b/Sofa/framework/Core/src/sofa/core/objectmodel/Base.h index ab1acf57676..0f796300374 100644 --- a/Sofa/framework/Core/src/sofa/core/objectmodel/Base.h +++ b/Sofa/framework/Core/src/sofa/core/objectmodel/Base.h @@ -425,7 +425,7 @@ class SOFA_CORE_API Base : public IntrusiveObject SOFA_BASE_CAST_DEFINITION( behavior, BaseProjectiveConstraintSet ) SOFA_BASE_CAST_DEFINITION( behavior, BaseInteractionProjectiveConstraintSet ) SOFA_BASE_CAST_DEFINITION( behavior, BaseConstraintSet ) - SOFA_BASE_CAST_DEFINITION( behavior, BaseConstraint ) + SOFA_BASE_CAST_DEFINITION( behavior, BaseLagrangianConstraint ) SOFA_BASE_CAST_DEFINITION( visual, VisualModel ) SOFA_BASE_CAST_DEFINITION( visual, VisualManager ) SOFA_BASE_CAST_DEFINITION( visual, VisualLoop ) diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalGetConstraintInfoVisitor.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalGetConstraintInfoVisitor.cpp index e4b183d8da8..b9070897f84 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalGetConstraintInfoVisitor.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalGetConstraintInfoVisitor.cpp @@ -41,7 +41,7 @@ MechanicalGetConstraintInfoVisitor::MechanicalGetConstraintInfoVisitor(const cor Visitor::Result MechanicalGetConstraintInfoVisitor::fwdConstraintSet(simulation::Node* node, core::behavior::BaseConstraintSet* cSet) { - if (core::behavior::BaseConstraint *c=cSet->toBaseConstraint()) + if (core::behavior::BaseLagrangianConstraint *c=cSet->toBaseLagrangianConstraint()) { const ctime_t t0 = begin(node, c); c->getConstraintInfo(_cparams, _blocks, _ids, _positions, _directions, _areas); diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalGetConstraintInfoVisitor.h b/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalGetConstraintInfoVisitor.h index 26b64c84fe5..5a5a73e7001 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalGetConstraintInfoVisitor.h +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalGetConstraintInfoVisitor.h @@ -22,18 +22,18 @@ #pragma once #include -#include +#include namespace sofa::simulation::mechanicalvisitor { class SOFA_SIMULATION_CORE_API MechanicalGetConstraintInfoVisitor : public simulation::BaseMechanicalVisitor { public: - typedef core::behavior::BaseConstraint::VecConstraintBlockInfo VecConstraintBlockInfo; - typedef core::behavior::BaseConstraint::VecPersistentID VecPersistentID; - typedef core::behavior::BaseConstraint::VecConstCoord VecConstCoord; - typedef core::behavior::BaseConstraint::VecConstDeriv VecConstDeriv; - typedef core::behavior::BaseConstraint::VecConstArea VecConstArea; + typedef core::behavior::BaseLagrangianConstraint::VecConstraintBlockInfo VecConstraintBlockInfo; + typedef core::behavior::BaseLagrangianConstraint::VecPersistentID VecPersistentID; + typedef core::behavior::BaseLagrangianConstraint::VecConstCoord VecConstCoord; + typedef core::behavior::BaseLagrangianConstraint::VecConstDeriv VecConstDeriv; + typedef core::behavior::BaseLagrangianConstraint::VecConstArea VecConstArea; MechanicalGetConstraintInfoVisitor(const core::ConstraintParams* params, VecConstraintBlockInfo& blocks, VecPersistentID& ids, VecConstCoord& positions, VecConstDeriv& directions, VecConstArea& areas); From 913ee114e59c6fa153f28298246146683da9b85a Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Fri, 25 Apr 2025 17:03:24 +0200 Subject: [PATCH 2/3] restore toBaseConstraint and deprecate it --- .../visitors/MechanicalGetConstraintResolutionVisitor.cpp | 2 +- Sofa/framework/Core/src/sofa/core/config.h.in | 8 ++++++++ Sofa/framework/Core/src/sofa/core/objectmodel/Base.h | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/visitors/MechanicalGetConstraintResolutionVisitor.cpp b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/visitors/MechanicalGetConstraintResolutionVisitor.cpp index fda279d678c..29d67ff2571 100644 --- a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/visitors/MechanicalGetConstraintResolutionVisitor.cpp +++ b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/visitors/MechanicalGetConstraintResolutionVisitor.cpp @@ -39,7 +39,7 @@ MechanicalGetConstraintResolutionVisitor::MechanicalGetConstraintResolutionVisit MechanicalGetConstraintResolutionVisitor::Result MechanicalGetConstraintResolutionVisitor::fwdConstraintSet(simulation::Node* node, core::behavior::BaseConstraintSet* cSet) { - if (core::behavior::BaseLagrangianConstraint *c=cSet->toBaseConstraint()) + if (core::behavior::BaseLagrangianConstraint *c=cSet->toBaseLagrangianConstraint()) { const ctime_t t0 = begin(node, c); c->getConstraintResolution(cparams, _res, _offset); diff --git a/Sofa/framework/Core/src/sofa/core/config.h.in b/Sofa/framework/Core/src/sofa/core/config.h.in index a444e394817..e4729fee84d 100644 --- a/Sofa/framework/Core/src/sofa/core/config.h.in +++ b/Sofa/framework/Core/src/sofa/core/config.h.in @@ -125,3 +125,11 @@ #define SOFA_ATTRIBUTE_DISABLED__MAPPING_JACOBIAN() SOFA_ATTRIBUTE_DISABLED("v24.12", "v25.06", "Use the variable sofa::core::vec_id::read_access::mappingJacobian or sofa::core::vec_id::write_access::mappingJacobian instead") #endif + + +#ifdef SOFA_BUILD_SOFA_CORE +#define SOFA_ATTRIBUTE_DEPRECATED__TOBASECONSTRAINT() +#else +#define SOFA_ATTRIBUTE_DEPRECATED__TOBASECONSTRAINT() \ + SOFA_ATTRIBUTE_DEPRECATED("v25.06", "v26.06", "Use toBaseLagrangianConstraint instead.") +#endif diff --git a/Sofa/framework/Core/src/sofa/core/objectmodel/Base.h b/Sofa/framework/Core/src/sofa/core/objectmodel/Base.h index 0f796300374..faba5c88119 100644 --- a/Sofa/framework/Core/src/sofa/core/objectmodel/Base.h +++ b/Sofa/framework/Core/src/sofa/core/objectmodel/Base.h @@ -442,6 +442,9 @@ class SOFA_CORE_API Base : public IntrusiveObject #undef SOFA_BASE_CAST_DEFINITION + virtual const behavior::BaseLagrangianConstraint* SOFA_ATTRIBUTE_DEPRECATED__TOBASECONSTRAINT() toBaseConstraint() const { return toBaseLagrangianConstraint(); } \ + virtual behavior::BaseLagrangianConstraint* SOFA_ATTRIBUTE_DEPRECATED__TOBASECONSTRAINT() toBaseConstraint() { return toBaseLagrangianConstraint(); } + /// @} }; From b3014d6aebf161b539f4a0fd736eb5d2ade11c65 Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Tue, 29 Apr 2025 09:33:03 +0200 Subject: [PATCH 3/3] fix? --- Sofa/framework/Core/src/sofa/core/objectmodel/Base.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sofa/framework/Core/src/sofa/core/objectmodel/Base.h b/Sofa/framework/Core/src/sofa/core/objectmodel/Base.h index faba5c88119..4ba0325664b 100644 --- a/Sofa/framework/Core/src/sofa/core/objectmodel/Base.h +++ b/Sofa/framework/Core/src/sofa/core/objectmodel/Base.h @@ -442,8 +442,8 @@ class SOFA_CORE_API Base : public IntrusiveObject #undef SOFA_BASE_CAST_DEFINITION - virtual const behavior::BaseLagrangianConstraint* SOFA_ATTRIBUTE_DEPRECATED__TOBASECONSTRAINT() toBaseConstraint() const { return toBaseLagrangianConstraint(); } \ - virtual behavior::BaseLagrangianConstraint* SOFA_ATTRIBUTE_DEPRECATED__TOBASECONSTRAINT() toBaseConstraint() { return toBaseLagrangianConstraint(); } + SOFA_ATTRIBUTE_DEPRECATED__TOBASECONSTRAINT() virtual const behavior::BaseLagrangianConstraint* toBaseConstraint() const { return toBaseLagrangianConstraint(); } \ + SOFA_ATTRIBUTE_DEPRECATED__TOBASECONSTRAINT() virtual behavior::BaseLagrangianConstraint* toBaseConstraint() { return toBaseLagrangianConstraint(); } /// @} };