From 99de79c60b6bb680b2b9f168054e8e38e90df90b Mon Sep 17 00:00:00 2001 From: Yinoussa Adagolodjo Date: Fri, 31 Oct 2025 20:46:28 +0100 Subject: [PATCH 1/4] feat(constraint): Improve SurfacePressureConstraint logic and fix bug This commit addresses a bug in the `SurfacePressureConstraintResolution` where the force was incorrectly calculated due to a flawed `if/if/else` control flow. The logic has been corrected to a proper `if/else if/else` structure. Additionally, the clamping logic in `VolumeGrowthConstraintResolution` has been refactored to use `std::min` and `std::max`, improving code clarity and conciseness. --- .../constraint/SurfacePressureConstraint.cpp | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/SoftRobots/component/constraint/SurfacePressureConstraint.cpp b/src/SoftRobots/component/constraint/SurfacePressureConstraint.cpp index 9e1c678a..e19c26b9 100644 --- a/src/SoftRobots/component/constraint/SurfacePressureConstraint.cpp +++ b/src/SoftRobots/component/constraint/SurfacePressureConstraint.cpp @@ -29,6 +29,7 @@ #include #include +#include namespace softrobots::constraint { @@ -56,26 +57,21 @@ void SurfacePressureConstraintResolution::init(int line, SReal**w, SReal*force) void SurfacePressureConstraintResolution::resolution(int line, SReal** w, SReal* d, SReal* force, SReal* dfree) { SOFA_UNUSED(w); - SOFA_UNUSED(d); SOFA_UNUSED(dfree); - double volumeGrowth = m_wActuatorActuator*m_imposedPressure + d[line]; + const double volumeGrowth = m_wActuatorActuator*m_imposedPressure + d[line]; if(volumeGrowthm_maxVolumeGrowth) + else if(volumeGrowth>m_maxVolumeGrowth) { - volumeGrowth = m_maxVolumeGrowth; - force[line] -= (d[line]-volumeGrowth) / m_wActuatorActuator ; + force[line] -= (d[line] - m_maxVolumeGrowth) / m_wActuatorActuator ; } else force[line] = m_imposedPressure ; - - } ///////////////////////////////////////////////////////////////////////////////////////////////////// @@ -106,10 +102,8 @@ void VolumeGrowthConstraintResolution::resolution(int line, SReal** w, SReal* d, // da=Waa*(lambda_a) + Sum Wai * lambda_i = m_imposedVolumeGrowth lambda[line] -= (d[line]-m_imposedVolumeGrowth) / m_wActuatorActuator ; - if(lambda[line]m_maxPressure) - lambda[line] = m_maxPressure; + lambda[line] = std::max(m_minPressure, lambda[line]); + lambda[line] = std::min(m_maxPressure, lambda[line]); } ///////////////////////////////////////////////////////////////////////////////////////////////////// From 55e58da4235b3071853fae7b01591fed5aaafeb8 Mon Sep 17 00:00:00 2001 From: Yinoussa Adagolodjo Date: Sat, 1 Nov 2025 07:44:46 +0100 Subject: [PATCH 2/4] refactor(model): Improve clarity of volume computation Refactors the `getCavityVolume` function in `SurfacePressureModel` to improve readability and reduce code duplication. The volume calculation for a single tetrahedron has been extracted into a new private helper function, `getTetrahedronVolume`. This change makes the overall volume computation logic easier to follow without altering the underlying algorithm. --- .../constraint/model/SurfacePressureModel.h | 1 + .../constraint/model/SurfacePressureModel.inl | 26 +++++++------------ 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/SoftRobots/component/constraint/model/SurfacePressureModel.h b/src/SoftRobots/component/constraint/model/SurfacePressureModel.h index 387e1e44..e5b72a06 100644 --- a/src/SoftRobots/component/constraint/model/SurfacePressureModel.h +++ b/src/SoftRobots/component/constraint/model/SurfacePressureModel.h @@ -150,6 +150,7 @@ class SOFA_SOFTROBOTS_API SurfacePressureModel : virtual public SoftRobotsConstr void drawValue(const sofa::core::visual::VisualParams* vparams); void computeEdges(); + Real getTetrahedronVolume(const Coord& p0, const Coord& p1, const Coord& p2); }; extern template class SurfacePressureModel; diff --git a/src/SoftRobots/component/constraint/model/SurfacePressureModel.inl b/src/SoftRobots/component/constraint/model/SurfacePressureModel.inl index c2a2171c..2dce337d 100644 --- a/src/SoftRobots/component/constraint/model/SurfacePressureModel.inl +++ b/src/SoftRobots/component/constraint/model/SurfacePressureModel.inl @@ -263,6 +263,12 @@ void SurfacePressureModel::internalInit() } +template +typename SurfacePressureModel::Real SurfacePressureModel::getTetrahedronVolume(const Coord& p0, const Coord& p1, const Coord& p2) +{ + return ((p1[1]-p0[1])*(p2[2]-p0[2])-(p2[1]-p0[1])*(p1[2]-p0[2]))*(p0[0]+p1[0]+p2[0])/6; +} + template SReal SurfacePressureModel::getCavityVolume(const VecCoord& positions) { @@ -272,31 +278,17 @@ SReal SurfacePressureModel::getCavityVolume(const VecCoord& positions ReadAccessor>> triangles = d_triangles; ReadAccessor>> quads = d_quads; - Coord p0, p1, p2; Real volume = 0; for (unsigned int t=0; t Date: Wed, 4 Feb 2026 10:10:16 +0100 Subject: [PATCH 3/4] Apply suggestions from code review --- .../component/constraint/model/SurfacePressureModel.h | 2 +- .../component/constraint/model/SurfacePressureModel.inl | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/SoftRobots/component/constraint/model/SurfacePressureModel.h b/src/SoftRobots/component/constraint/model/SurfacePressureModel.h index e5b72a06..992bd289 100644 --- a/src/SoftRobots/component/constraint/model/SurfacePressureModel.h +++ b/src/SoftRobots/component/constraint/model/SurfacePressureModel.h @@ -150,7 +150,7 @@ class SOFA_SOFTROBOTS_API SurfacePressureModel : virtual public SoftRobotsConstr void drawValue(const sofa::core::visual::VisualParams* vparams); void computeEdges(); - Real getTetrahedronVolume(const Coord& p0, const Coord& p1, const Coord& p2); + Real getTriangleContributionToVolume(const Coord& p0, const Coord& p1, const Coord& p2); }; extern template class SurfacePressureModel; diff --git a/src/SoftRobots/component/constraint/model/SurfacePressureModel.inl b/src/SoftRobots/component/constraint/model/SurfacePressureModel.inl index 2dce337d..4c05c912 100644 --- a/src/SoftRobots/component/constraint/model/SurfacePressureModel.inl +++ b/src/SoftRobots/component/constraint/model/SurfacePressureModel.inl @@ -282,13 +282,13 @@ SReal SurfacePressureModel::getCavityVolume(const VecCoord& positions for (unsigned int t=0; t Date: Wed, 4 Feb 2026 10:11:48 +0100 Subject: [PATCH 4/4] Apply suggestion from @EulalieCoevoet --- .../component/constraint/model/SurfacePressureModel.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SoftRobots/component/constraint/model/SurfacePressureModel.inl b/src/SoftRobots/component/constraint/model/SurfacePressureModel.inl index 4c05c912..419bc83e 100644 --- a/src/SoftRobots/component/constraint/model/SurfacePressureModel.inl +++ b/src/SoftRobots/component/constraint/model/SurfacePressureModel.inl @@ -264,7 +264,7 @@ void SurfacePressureModel::internalInit() template -typename SurfacePressureModel::Real SurfacePressureModel::getTetrahedronVolume(const Coord& p0, const Coord& p1, const Coord& p2) +typename SurfacePressureModel::Real SurfacePressureModel::getTriangleContributionToVolume(const Coord& p0, const Coord& p1, const Coord& p2) { return ((p1[1]-p0[1])*(p2[2]-p0[2])-(p2[1]-p0[1])*(p1[2]-p0[2]))*(p0[0]+p1[0]+p2[0])/6; }