Skip to content

Commit 402de71

Browse files
author
hugtalbot
committed
force push - Apply template method design pattern to BaseConstraintSet
1 parent fb6ac6c commit 402de71

File tree

7 files changed

+128
-57
lines changed

7 files changed

+128
-57
lines changed

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

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,19 @@ class SOFA_CORE_API BaseConstraintSet : public virtual objectmodel::BaseObject
4545
BaseConstraintSet& operator=(const BaseConstraintSet& n) = delete;
4646

4747
public:
48-
virtual void resetConstraint() {}
48+
/**
49+
* !!! WARNING since v26.06 !!!
50+
*
51+
* The template method pattern has been applied to this part of the API.
52+
* This method calls the newly introduced method "doFunctionName" internally,
53+
* which is the method to override from now on.
54+
*
55+
**/
56+
virtual void resetConstraint() final
57+
{
58+
//TODO (SPRINT SED 2025): Component state mechanism
59+
doResetConstraint();
60+
}
4961

5062
/// Set the id of the constraint (this id is build in the getConstraintViolation function)
5163
///
@@ -55,39 +67,93 @@ class SOFA_CORE_API BaseConstraintSet : public virtual objectmodel::BaseObject
5567
d_constraintIndex.setValue(cId);
5668
}
5769

70+
/**
71+
* !!! WARNING since v26.06 !!!
72+
*
73+
* The template method pattern has been applied to this part of the API.
74+
* This method calls the newly introduced method "doFunctionName" internally,
75+
* which is the method to override from now on.
76+
*
77+
**/
5878
/// Process geometrical data.
5979
///
6080
/// This function is called by the CollisionVisitor, it can be used to process a collision detection specific for the constraint
61-
virtual void processGeometricalData() {}
81+
virtual void processGeometricalData() final
82+
{
83+
//TODO (SPRINT SED 2025): Component state mechanism
84+
doProcessGeometricalData();
85+
}
6286

87+
/**
88+
* !!! WARNING since v26.06 !!!
89+
*
90+
* The template method pattern has been applied to this part of the API.
91+
* This method calls the newly introduced method "doFunctionName" internally,
92+
* which is the method to override from now on.
93+
*
94+
**/
6395
/// Construct the Jacobian Matrix
6496
///
6597
/// \param cId is the result constraint sparse matrix Id
6698
/// \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
6799
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
68-
virtual void buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) = 0;
100+
virtual void buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) final
101+
{
102+
//TODO (SPRINT SED 2025): Component state mechanism
103+
doBuildConstraintMatrix(cParams, cId, cIndex);
104+
}
69105

106+
/**
107+
* !!! WARNING since v26.06 !!!
108+
*
109+
* The template method pattern has been applied to this part of the API.
110+
* This method calls the newly introduced method "doFunctionName" internally,
111+
* which is the method to override from now on.
112+
*
113+
**/
70114
/// Construct the Constraint violations vector
71115
///
72116
/// \param v is the result vector that contains the whole constraints violations
73117
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
74-
virtual void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v)
118+
virtual void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) final
75119
{
76-
getConstraintViolation(cParams, v, d_constraintIndex.getValue());
120+
//TODO (SPRINT SED 2025): Component state mechanism
121+
doGetConstraintViolation(cParams, v);
77122
}
78123

124+
125+
/**
126+
* !!! WARNING since v26.06 !!!
127+
*
128+
* The template method pattern has been applied to this part of the API.
129+
* This method calls the newly introduced method "doFunctionName" internally,
130+
* which is the method to override from now on.
131+
*
132+
**/
79133
/// Construct the Constraint violations vector
80134
///
81135
/// \param v is the result vector that contains the whole constraints violations
82136
/// \param cIndex is the index of the next constraint equation
83137
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
84-
virtual void getConstraintViolation(const ConstraintParams* /*cParams*/, linearalgebra::BaseVector * /*v*/, unsigned int /*cIndex*/) {
85-
dmsg_error() << "getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v, const unsigned int cIndex) is not implemented while it should";
138+
virtual void getConstraintViolation(const ConstraintParams* /*cParams*/, linearalgebra::BaseVector * /*v*/, unsigned int /*cIndex*/) final
139+
{
140+
86141
}
87142

88143
protected:
89-
144+
virtual void doResetConstraint() {}
145+
virtual void doProcessGeometricalData() {}
146+
virtual void doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) = 0;
147+
virtual void doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v)
148+
{
149+
getConstraintViolation(cParams, v, d_constraintIndex.getValue());
150+
}
151+
virtual void doGetConstraintViolation(const ConstraintParams* /*cParams*/, linearalgebra::BaseVector * /*v*/, unsigned int /*cIndex*/)
152+
{
153+
dmsg_error() << "getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v, const unsigned int cIndex) is not implemented while it should";
154+
}
90155
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.
156+
91157
public:
92158
Data< sofa::Index > d_constraintIndex; ///< Constraint index (first index in the right hand term resolution vector)
93159

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,27 @@ class LagrangianConstraint : public BaseLagrangianConstraint, public virtual Sin
6262
~LagrangianConstraint() override;
6363

6464
virtual void init() override;
65+
66+
/// Construct the Constraint violations vector of each constraint
67+
///
68+
/// \param v is the result vector that contains the whole constraints violations
69+
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
70+
void doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) override;
71+
72+
/// Construct the Jacobian Matrix
73+
///
74+
/// \param cId is the result constraint sparse matrix Id
75+
/// \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
76+
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
77+
void doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) override;
78+
79+
6580
public:
6681
Data<Real> endTime; ///< The constraint stops acting after the given value. Use a negative value for infinite constraints
6782
virtual bool isActive() const; ///< if false, the constraint does nothing
6883

6984
using BaseConstraintSet::getConstraintViolation;
7085

71-
/// Construct the Constraint violations vector of each constraint
72-
///
73-
/// \param v is the result vector that contains the whole constraints violations
74-
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
75-
void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) override;
7686

7787
/// Construct the Constraint violations vector of each constraint
7888
///
@@ -85,13 +95,6 @@ class LagrangianConstraint : public BaseLagrangianConstraint, public virtual Sin
8595
virtual void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *resV, const DataVecCoord &x, const DataVecDeriv &v) = 0;
8696

8797

88-
/// Construct the Jacobian Matrix
89-
///
90-
/// \param cId is the result constraint sparse matrix Id
91-
/// \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
92-
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
93-
void buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) override;
94-
9598
/// Construct the Jacobian Matrix
9699
///
97100
/// \param c is the result constraint sparse matrix

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void LagrangianConstraint<DataTypes>::init()
5555
}
5656

5757
template<class DataTypes>
58-
void LagrangianConstraint<DataTypes>::getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v)
58+
void LagrangianConstraint<DataTypes>::doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v)
5959
{
6060
if (cParams)
6161
{
@@ -65,7 +65,7 @@ void LagrangianConstraint<DataTypes>::getConstraintViolation(const ConstraintPar
6565

6666

6767
template<class DataTypes>
68-
void LagrangianConstraint<DataTypes>::buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex)
68+
void LagrangianConstraint<DataTypes>::doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex)
6969
{
7070
if (cParams)
7171
{

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,24 @@ class MixedInteractionConstraint : public BaseInteractionConstraint, public Pair
7373

7474
virtual type::vector<std::string> getMixedInteractionIdentifiers(){ return {}; }
7575

76+
/// Construct the Constraint violations vector of each constraint
77+
///
78+
/// \param v is the result vector that contains the whole constraints violations
79+
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
80+
void doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) override;
81+
82+
/// Construct the Jacobian Matrix
83+
///
84+
/// \param cId is the result constraint sparse matrix Id
85+
/// \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
86+
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
87+
void doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) override;
7688

7789
public:
7890
Data<SReal> endTime; ///< The constraint stops acting after the given value. Use a negative value for infinite constraints
7991
virtual bool isActive() const; ///< if false, the constraint does nothing
8092

8193
using BaseConstraintSet::getConstraintViolation;
82-
/// Construct the Constraint violations vector of each constraint
83-
///
84-
/// \param v is the result vector that contains the whole constraints violations
85-
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
86-
void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) override;
8794

8895
/// Construct the Constraint violations vector of each constraint
8996
///
@@ -96,13 +103,6 @@ class MixedInteractionConstraint : public BaseInteractionConstraint, public Pair
96103
virtual void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v, const DataVecCoord1 &x1, const DataVecCoord2 &x2
97104
, const DataVecDeriv1 &v1, const DataVecDeriv2 &v2) = 0;
98105

99-
/// Construct the Jacobian Matrix
100-
///
101-
/// \param cId is the result constraint sparse matrix Id
102-
/// \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
103-
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
104-
void buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) override;
105-
106106
/// Construct the Jacobian Matrix
107107
///
108108
/// \param c1 and c2 are the results constraint sparse matrix

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ bool MixedInteractionConstraint<DataTypes1, DataTypes2>::isActive() const
4747
}
4848

4949
template<class DataTypes1, class DataTypes2>
50-
void MixedInteractionConstraint<DataTypes1, DataTypes2>::getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v)
50+
void MixedInteractionConstraint<DataTypes1, DataTypes2>::doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v)
5151
{
5252
if (cParams)
5353
{
@@ -57,7 +57,7 @@ void MixedInteractionConstraint<DataTypes1, DataTypes2>::getConstraintViolation(
5757
}
5858

5959
template<class DataTypes1, class DataTypes2>
60-
void MixedInteractionConstraint<DataTypes1, DataTypes2>::buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex)
60+
void MixedInteractionConstraint<DataTypes1, DataTypes2>::doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex)
6161
{
6262
if (cParams)
6363
{

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ class PairInteractionConstraint : public BaseInteractionConstraint, public PairS
6262
virtual bool isActive() const; ///< if false, the constraint does nothing
6363

6464
using BaseConstraintSet::getConstraintViolation;
65-
/// Construct the Constraint violations vector of each constraint
66-
///
67-
/// \param v is the result vector that contains the whole constraints violations
68-
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
69-
void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) override;
7065

7166
/// Construct the Constraint violations vector of each constraint
7267
///
@@ -79,13 +74,6 @@ class PairInteractionConstraint : public BaseInteractionConstraint, public PairS
7974
virtual void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v, const DataVecCoord &x1, const DataVecCoord &x2
8075
, const DataVecDeriv &v1, const DataVecDeriv &v2) = 0;
8176

82-
/// Construct the Jacobian Matrix
83-
///
84-
/// \param cId is the result constraint sparse matrix Id
85-
/// \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
86-
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
87-
void buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) override;
88-
8977
/// Construct the Jacobian Matrix
9078
///
9179
/// \param c1 and c2 are the results constraint sparse matrix
@@ -157,14 +145,28 @@ class PairInteractionConstraint : public BaseInteractionConstraint, public PairS
157145

158146
protected:
159147

160-
virtual type::vector<std::string> getInteractionIdentifiers() override final
161-
{
162-
type::vector<std::string> ids = getPairInteractionIdentifiers();
163-
ids.push_back("Pair");
164-
return ids;
165-
}
148+
/// Construct the Constraint violations vector of each constraint
149+
///
150+
/// \param v is the result vector that contains the whole constraints violations
151+
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
152+
void doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) override;
153+
154+
/// Construct the Jacobian Matrix
155+
///
156+
/// \param cId is the result constraint sparse matrix Id
157+
/// \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
158+
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
159+
void doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) override;
160+
161+
162+
virtual type::vector<std::string> getInteractionIdentifiers() override final
163+
{
164+
type::vector<std::string> ids = getPairInteractionIdentifiers();
165+
ids.push_back("Pair");
166+
return ids;
167+
}
166168

167-
virtual type::vector<std::string> getPairInteractionIdentifiers(){ return {}; }
169+
virtual type::vector<std::string> getPairInteractionIdentifiers(){ return {}; }
168170

169171
void storeLambda(const ConstraintParams* cParams, Data<VecDeriv>& res1, Data<VecDeriv>& res2, const Data<MatrixDeriv>& j1, const Data<MatrixDeriv>& j2,
170172
const sofa::linearalgebra::BaseVector* lambda);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bool PairInteractionConstraint<DataTypes>::isActive() const
5050

5151

5252
template<class DataTypes>
53-
void PairInteractionConstraint<DataTypes>::getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v)
53+
void PairInteractionConstraint<DataTypes>::doGetConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v)
5454
{
5555
if (cParams)
5656
{
@@ -60,7 +60,7 @@ void PairInteractionConstraint<DataTypes>::getConstraintViolation(const Constrai
6060

6161

6262
template<class DataTypes>
63-
void PairInteractionConstraint<DataTypes>::buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex)
63+
void PairInteractionConstraint<DataTypes>::doBuildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex)
6464
{
6565
if (cParams)
6666
{

0 commit comments

Comments
 (0)