Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 74 additions & 8 deletions Sofa/framework/Core/src/sofa/core/behavior/BaseConstraintSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
///
Expand All @@ -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)

Expand Down
27 changes: 15 additions & 12 deletions Sofa/framework/Core/src/sofa/core/behavior/LagrangianConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Real> 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
///
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void LagrangianConstraint<DataTypes>::init()
}

template<class DataTypes>
void LagrangianConstraint<DataTypes>::getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v)
void LagrangianConstraint<DataTypes>::doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v)
{
if (cParams)
{
Expand All @@ -65,7 +65,7 @@ void LagrangianConstraint<DataTypes>::getConstraintViolation(const ConstraintPar


template<class DataTypes>
void LagrangianConstraint<DataTypes>::buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex)
void LagrangianConstraint<DataTypes>::doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex)
{
if (cParams)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,24 @@ class MixedInteractionConstraint : public BaseInteractionConstraint, public Pair

virtual type::vector<std::string> 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<SReal> 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
///
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ bool MixedInteractionConstraint<DataTypes1, DataTypes2>::isActive() const
}

template<class DataTypes1, class DataTypes2>
void MixedInteractionConstraint<DataTypes1, DataTypes2>::getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v)
void MixedInteractionConstraint<DataTypes1, DataTypes2>::doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v)
{
if (cParams)
{
Expand All @@ -57,7 +57,7 @@ void MixedInteractionConstraint<DataTypes1, DataTypes2>::getConstraintViolation(
}

template<class DataTypes1, class DataTypes2>
void MixedInteractionConstraint<DataTypes1, DataTypes2>::buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex)
void MixedInteractionConstraint<DataTypes1, DataTypes2>::doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex)
{
if (cParams)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -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
Expand Down Expand Up @@ -157,14 +145,28 @@ class PairInteractionConstraint : public BaseInteractionConstraint, public PairS

protected:

virtual type::vector<std::string> getInteractionIdentifiers() override final
{
type::vector<std::string> 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<std::string> getInteractionIdentifiers() override final
{
type::vector<std::string> ids = getPairInteractionIdentifiers();
ids.push_back("Pair");
return ids;
}

virtual type::vector<std::string> getPairInteractionIdentifiers(){ return {}; }
virtual type::vector<std::string> getPairInteractionIdentifiers(){ return {}; }

void storeLambda(const ConstraintParams* cParams, Data<VecDeriv>& res1, Data<VecDeriv>& res2, const Data<MatrixDeriv>& j1, const Data<MatrixDeriv>& j2,
const sofa::linearalgebra::BaseVector* lambda);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bool PairInteractionConstraint<DataTypes>::isActive() const


template<class DataTypes>
void PairInteractionConstraint<DataTypes>::getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v)
void PairInteractionConstraint<DataTypes>::doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v)
{
if (cParams)
{
Expand All @@ -60,7 +60,7 @@ void PairInteractionConstraint<DataTypes>::getConstraintViolation(const Constrai


template<class DataTypes>
void PairInteractionConstraint<DataTypes>::buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex)
void PairInteractionConstraint<DataTypes>::doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex)
{
if (cParams)
{
Expand Down
Loading