diff --git a/Sofa/framework/Core/src/sofa/core/behavior/BaseConstraintSet.h b/Sofa/framework/Core/src/sofa/core/behavior/BaseConstraintSet.h index 8ce184c161b..93ab78b0d02 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/BaseConstraintSet.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/BaseConstraintSet.h @@ -45,7 +45,19 @@ class SOFA_CORE_API BaseConstraintSet : public virtual objectmodel::BaseObject BaseConstraintSet& operator=(const BaseConstraintSet& n) = delete; public: - virtual void resetConstraint() {} + /** + * !!! WARNING since v26.06 !!! + * + * The template method pattern has been applied to this part of the API. + * This method calls the newly introduced method "doFunctionName" internally, + * which is the method to override from now on. + * + **/ + virtual void resetConstraint() final + { + //TODO (SPRINT SED 2025): Component state mechanism + doResetConstraint(); + } /// Set the id of the constraint (this id is build in the getConstraintViolation function) /// @@ -55,39 +67,93 @@ class SOFA_CORE_API BaseConstraintSet : public virtual objectmodel::BaseObject d_constraintIndex.setValue(cId); } + /** + * !!! WARNING since v26.06 !!! + * + * The template method pattern has been applied to this part of the API. + * This method calls the newly introduced method "doFunctionName" internally, + * which is the method to override from now on. + * + **/ /// Process geometrical data. /// /// This function is called by the CollisionVisitor, it can be used to process a collision detection specific for the constraint - virtual void processGeometricalData() {} + virtual void processGeometricalData() final + { + //TODO (SPRINT SED 2025): Component state mechanism + doProcessGeometricalData(); + } + /** + * !!! WARNING since v26.06 !!! + * + * The template method pattern has been applied to this part of the API. + * This method calls the newly introduced method "doFunctionName" internally, + * which is the method to override from now on. + * + **/ /// Construct the Jacobian Matrix /// /// \param cId is the result constraint sparse matrix Id /// \param cIndex is the index of the next constraint equation: when building the constraint matrix, you have to use this index, and then update it /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) - virtual void buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) = 0; + virtual void buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) final + { + //TODO (SPRINT SED 2025): Component state mechanism + doBuildConstraintMatrix(cParams, cId, cIndex); + } + /** + * !!! WARNING since v26.06 !!! + * + * The template method pattern has been applied to this part of the API. + * This method calls the newly introduced method "doFunctionName" internally, + * which is the method to override from now on. + * + **/ /// Construct the Constraint violations vector /// /// \param v is the result vector that contains the whole constraints violations /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) - virtual void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) + virtual void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) final { - getConstraintViolation(cParams, v, d_constraintIndex.getValue()); + //TODO (SPRINT SED 2025): Component state mechanism + doGetConstraintViolation(cParams, v); } + + /** + * !!! WARNING since v26.06 !!! + * + * The template method pattern has been applied to this part of the API. + * This method calls the newly introduced method "doFunctionName" internally, + * which is the method to override from now on. + * + **/ /// Construct the Constraint violations vector /// /// \param v is the result vector that contains the whole constraints violations /// \param cIndex is the index of the next constraint equation /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) - virtual void getConstraintViolation(const ConstraintParams* /*cParams*/, linearalgebra::BaseVector * /*v*/, unsigned int /*cIndex*/) { - dmsg_error() << "getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v, const unsigned int cIndex) is not implemented while it should"; + virtual void getConstraintViolation(const ConstraintParams* /*cParams*/, linearalgebra::BaseVector * /*v*/, unsigned int /*cIndex*/) final + { + } protected: - + virtual void doResetConstraint() {} + virtual void doProcessGeometricalData() {} + virtual void doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) = 0; + virtual void doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) + { + getConstraintViolation(cParams, v, d_constraintIndex.getValue()); + } + virtual void doGetConstraintViolation(const ConstraintParams* /*cParams*/, linearalgebra::BaseVector * /*v*/, unsigned int /*cIndex*/) + { + dmsg_error() << "getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v, const unsigned int cIndex) is not implemented while it should"; + } Data< int > group; ///< 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. + public: Data< sofa::Index > d_constraintIndex; ///< Constraint index (first index in the right hand term resolution vector) diff --git a/Sofa/framework/Core/src/sofa/core/behavior/LagrangianConstraint.h b/Sofa/framework/Core/src/sofa/core/behavior/LagrangianConstraint.h index 7eabee6cf10..05a8aaf7e4f 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/LagrangianConstraint.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/LagrangianConstraint.h @@ -62,17 +62,27 @@ class LagrangianConstraint : public BaseLagrangianConstraint, public virtual Sin ~LagrangianConstraint() override; virtual void init() override; + + /// Construct the Constraint violations vector of each constraint + /// + /// \param v is the result vector that contains the whole constraints violations + /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) + void doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) override; + + /// Construct the Jacobian Matrix + /// + /// \param cId is the result constraint sparse matrix Id + /// \param cIndex is the index of the next constraint equation: when building the constraint matrix, you have to use this index, and then update it + /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) + void doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) override; + + public: Data endTime; ///< The constraint stops acting after the given value. Use a negative value for infinite constraints virtual bool isActive() const; ///< if false, the constraint does nothing using BaseConstraintSet::getConstraintViolation; - /// Construct the Constraint violations vector of each constraint - /// - /// \param v is the result vector that contains the whole constraints violations - /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) - void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) override; /// Construct the Constraint violations vector of each constraint /// @@ -85,13 +95,6 @@ class LagrangianConstraint : public BaseLagrangianConstraint, public virtual Sin virtual void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *resV, const DataVecCoord &x, const DataVecDeriv &v) = 0; - /// Construct the Jacobian Matrix - /// - /// \param cId is the result constraint sparse matrix Id - /// \param cIndex is the index of the next constraint equation: when building the constraint matrix, you have to use this index, and then update it - /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) - void buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) override; - /// Construct the Jacobian Matrix /// /// \param c is the result constraint sparse matrix diff --git a/Sofa/framework/Core/src/sofa/core/behavior/LagrangianConstraint.inl b/Sofa/framework/Core/src/sofa/core/behavior/LagrangianConstraint.inl index f76acf95e3a..2014bcb83b7 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/LagrangianConstraint.inl +++ b/Sofa/framework/Core/src/sofa/core/behavior/LagrangianConstraint.inl @@ -55,7 +55,7 @@ void LagrangianConstraint::init() } template -void LagrangianConstraint::getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) +void LagrangianConstraint::doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) { if (cParams) { @@ -65,7 +65,7 @@ void LagrangianConstraint::getConstraintViolation(const ConstraintPar template -void LagrangianConstraint::buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) +void LagrangianConstraint::doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) { if (cParams) { diff --git a/Sofa/framework/Core/src/sofa/core/behavior/MixedInteractionConstraint.h b/Sofa/framework/Core/src/sofa/core/behavior/MixedInteractionConstraint.h index 51b2322c62e..8c9589e52dc 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/MixedInteractionConstraint.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/MixedInteractionConstraint.h @@ -73,17 +73,24 @@ class MixedInteractionConstraint : public BaseInteractionConstraint, public Pair virtual type::vector getMixedInteractionIdentifiers(){ return {}; } + /// Construct the Constraint violations vector of each constraint + /// + /// \param v is the result vector that contains the whole constraints violations + /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) + void doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) override; + + /// Construct the Jacobian Matrix + /// + /// \param cId is the result constraint sparse matrix Id + /// \param cIndex is the index of the next constraint equation: when building the constraint matrix, you have to use this index, and then update it + /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) + void doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) override; public: Data endTime; ///< The constraint stops acting after the given value. Use a negative value for infinite constraints virtual bool isActive() const; ///< if false, the constraint does nothing using BaseConstraintSet::getConstraintViolation; - /// Construct the Constraint violations vector of each constraint - /// - /// \param v is the result vector that contains the whole constraints violations - /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) - void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) override; /// Construct the Constraint violations vector of each constraint /// @@ -96,13 +103,6 @@ class MixedInteractionConstraint : public BaseInteractionConstraint, public Pair virtual void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v, const DataVecCoord1 &x1, const DataVecCoord2 &x2 , const DataVecDeriv1 &v1, const DataVecDeriv2 &v2) = 0; - /// Construct the Jacobian Matrix - /// - /// \param cId is the result constraint sparse matrix Id - /// \param cIndex is the index of the next constraint equation: when building the constraint matrix, you have to use this index, and then update it - /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) - void buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) override; - /// Construct the Jacobian Matrix /// /// \param c1 and c2 are the results constraint sparse matrix diff --git a/Sofa/framework/Core/src/sofa/core/behavior/MixedInteractionConstraint.inl b/Sofa/framework/Core/src/sofa/core/behavior/MixedInteractionConstraint.inl index a881ef7031a..9544b8aec59 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/MixedInteractionConstraint.inl +++ b/Sofa/framework/Core/src/sofa/core/behavior/MixedInteractionConstraint.inl @@ -47,7 +47,7 @@ bool MixedInteractionConstraint::isActive() const } template -void MixedInteractionConstraint::getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) +void MixedInteractionConstraint::doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) { if (cParams) { @@ -57,7 +57,7 @@ void MixedInteractionConstraint::getConstraintViolation( } template -void MixedInteractionConstraint::buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) +void MixedInteractionConstraint::doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) { if (cParams) { diff --git a/Sofa/framework/Core/src/sofa/core/behavior/PairInteractionConstraint.h b/Sofa/framework/Core/src/sofa/core/behavior/PairInteractionConstraint.h index 1f494cb85aa..184748c4885 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/PairInteractionConstraint.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/PairInteractionConstraint.h @@ -62,11 +62,6 @@ class PairInteractionConstraint : public BaseInteractionConstraint, public PairS virtual bool isActive() const; ///< if false, the constraint does nothing using BaseConstraintSet::getConstraintViolation; - /// Construct the Constraint violations vector of each constraint - /// - /// \param v is the result vector that contains the whole constraints violations - /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) - void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) override; /// Construct the Constraint violations vector of each constraint /// @@ -79,13 +74,6 @@ class PairInteractionConstraint : public BaseInteractionConstraint, public PairS virtual void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v, const DataVecCoord &x1, const DataVecCoord &x2 , const DataVecDeriv &v1, const DataVecDeriv &v2) = 0; - /// Construct the Jacobian Matrix - /// - /// \param cId is the result constraint sparse matrix Id - /// \param cIndex is the index of the next constraint equation: when building the constraint matrix, you have to use this index, and then update it - /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) - void buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) override; - /// Construct the Jacobian Matrix /// /// \param c1 and c2 are the results constraint sparse matrix @@ -157,14 +145,28 @@ class PairInteractionConstraint : public BaseInteractionConstraint, public PairS protected: - virtual type::vector getInteractionIdentifiers() override final - { - type::vector ids = getPairInteractionIdentifiers(); - ids.push_back("Pair"); - return ids; - } + /// Construct the Constraint violations vector of each constraint + /// + /// \param v is the result vector that contains the whole constraints violations + /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) + void doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) override; + + /// Construct the Jacobian Matrix + /// + /// \param cId is the result constraint sparse matrix Id + /// \param cIndex is the index of the next constraint equation: when building the constraint matrix, you have to use this index, and then update it + /// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC) + void doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) override; + + + virtual type::vector getInteractionIdentifiers() override final + { + type::vector ids = getPairInteractionIdentifiers(); + ids.push_back("Pair"); + return ids; + } - virtual type::vector getPairInteractionIdentifiers(){ return {}; } + virtual type::vector getPairInteractionIdentifiers(){ return {}; } void storeLambda(const ConstraintParams* cParams, Data& res1, Data& res2, const Data& j1, const Data& j2, const sofa::linearalgebra::BaseVector* lambda); diff --git a/Sofa/framework/Core/src/sofa/core/behavior/PairInteractionConstraint.inl b/Sofa/framework/Core/src/sofa/core/behavior/PairInteractionConstraint.inl index 7023b553922..f5e4bae8f4b 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/PairInteractionConstraint.inl +++ b/Sofa/framework/Core/src/sofa/core/behavior/PairInteractionConstraint.inl @@ -50,7 +50,7 @@ bool PairInteractionConstraint::isActive() const template -void PairInteractionConstraint::getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) +void PairInteractionConstraint::doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) { if (cParams) { @@ -60,7 +60,7 @@ void PairInteractionConstraint::getConstraintViolation(const Constrai template -void PairInteractionConstraint::buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) +void PairInteractionConstraint::doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) { if (cParams) {