Skip to content

Commit be8790e

Browse files
feat(constraint): Improve SurfacePressureConstraint logic and fix bug (#340)
* 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. * refactoring of volume computation * Apply suggestions from code review (renaming) --------- Co-authored-by: EulalieCoevoet <eulalie.coevoet@gmail.com>
1 parent 1961779 commit be8790e

3 files changed

Lines changed: 17 additions & 30 deletions

File tree

src/SoftRobots/component/constraint/SurfacePressureConstraint.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <SoftRobots/component/constraint/SurfacePressureConstraint.inl>
3030

3131
#include <sofa/core/ObjectFactory.h>
32+
#include <algorithm>
3233

3334
namespace softrobots::constraint
3435
{
@@ -56,26 +57,21 @@ void SurfacePressureConstraintResolution::init(int line, SReal**w, SReal*force)
5657
void SurfacePressureConstraintResolution::resolution(int line, SReal** w, SReal* d, SReal* force, SReal* dfree)
5758
{
5859
SOFA_UNUSED(w);
59-
SOFA_UNUSED(d);
6060
SOFA_UNUSED(dfree);
6161

6262

63-
double volumeGrowth = m_wActuatorActuator*m_imposedPressure + d[line];
63+
const double volumeGrowth = m_wActuatorActuator*m_imposedPressure + d[line];
6464

6565
if(volumeGrowth<m_minVolumeGrowth)
6666
{
67-
volumeGrowth = m_minVolumeGrowth;
68-
force[line] -= (d[line]-volumeGrowth) / m_wActuatorActuator ;
67+
force[line] -= (d[line] - m_minVolumeGrowth) / m_wActuatorActuator ;
6968
}
70-
if(volumeGrowth>m_maxVolumeGrowth)
69+
else if(volumeGrowth>m_maxVolumeGrowth)
7170
{
72-
volumeGrowth = m_maxVolumeGrowth;
73-
force[line] -= (d[line]-volumeGrowth) / m_wActuatorActuator ;
71+
force[line] -= (d[line] - m_maxVolumeGrowth) / m_wActuatorActuator ;
7472
}
7573
else
7674
force[line] = m_imposedPressure ;
77-
78-
7975
}
8076

8177
/////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -106,10 +102,8 @@ void VolumeGrowthConstraintResolution::resolution(int line, SReal** w, SReal* d,
106102
// da=Waa*(lambda_a) + Sum Wai * lambda_i = m_imposedVolumeGrowth
107103
lambda[line] -= (d[line]-m_imposedVolumeGrowth) / m_wActuatorActuator ;
108104

109-
if(lambda[line]<m_minPressure)
110-
lambda[line] = m_minPressure;
111-
if(lambda[line]>m_maxPressure)
112-
lambda[line] = m_maxPressure;
105+
lambda[line] = std::max(m_minPressure, lambda[line]);
106+
lambda[line] = std::min(m_maxPressure, lambda[line]);
113107
}
114108

115109
/////////////////////////////////////////////////////////////////////////////////////////////////////

src/SoftRobots/component/constraint/model/SurfacePressureModel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class SOFA_SOFTROBOTS_API SurfacePressureModel : virtual public SoftRobotsConstr
150150

151151
void drawValue(const sofa::core::visual::VisualParams* vparams);
152152
void computeEdges();
153+
Real getTriangleContributionToVolume(const Coord& p0, const Coord& p1, const Coord& p2);
153154
};
154155

155156
extern template class SurfacePressureModel<sofa::defaulttype::Vec3Types>;

src/SoftRobots/component/constraint/model/SurfacePressureModel.inl

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ void SurfacePressureModel<DataTypes>::internalInit()
263263
}
264264

265265

266+
template<class DataTypes>
267+
typename SurfacePressureModel<DataTypes>::Real SurfacePressureModel<DataTypes>::getTriangleContributionToVolume(const Coord& p0, const Coord& p1, const Coord& p2)
268+
{
269+
return ((p1[1]-p0[1])*(p2[2]-p0[2])-(p2[1]-p0[1])*(p1[2]-p0[2]))*(p0[0]+p1[0]+p2[0])/6;
270+
}
271+
266272
template<class DataTypes>
267273
SReal SurfacePressureModel<DataTypes>::getCavityVolume(const VecCoord& positions)
268274
{
@@ -272,31 +278,17 @@ SReal SurfacePressureModel<DataTypes>::getCavityVolume(const VecCoord& positions
272278
ReadAccessor<Data<vector<Triangle>>> triangles = d_triangles;
273279
ReadAccessor<Data<vector<Quad>>> quads = d_quads;
274280

275-
Coord p0, p1, p2;
276281
Real volume = 0;
277282

278283
for (unsigned int t=0; t<triangles.size(); t++)
279284
{
280-
p0 = positions[triangles[t][0]];
281-
p1 = positions[triangles[t][1]];
282-
p2 = positions[triangles[t][2]];
283-
284-
volume += ((p1[1]-p0[1])*(p2[2]-p0[2])-(p2[1]-p0[1])*(p1[2]-p0[2]))*(p0[0]+p1[0]+p2[0])/6;
285+
volume += getTriangleContributionToVolume(positions[triangles[t][0]], positions[triangles[t][1]], positions[triangles[t][2]]);
285286
}
286287

287288
for (unsigned int q=0; q<quads.size(); q++)
288289
{
289-
p0 = positions[quads[q][0]];
290-
p1 = positions[quads[q][1]];
291-
p2 = positions[quads[q][2]];
292-
293-
volume += ((p1[1]-p0[1])*(p2[2]-p0[2])-(p2[1]-p0[1])*(p1[2]-p0[2]))*(p0[0]+p1[0]+p2[0])/6;
294-
295-
p0 = positions[quads[q][0]];
296-
p1 = positions[quads[q][2]];
297-
p2 = positions[quads[q][3]];
298-
299-
volume += ((p1[1]-p0[1])*(p2[2]-p0[2])-(p2[1]-p0[1])*(p1[2]-p0[2]))*(p0[0]+p1[0]+p2[0])/6;
290+
volume += getTriangleContributionToVolume(positions[quads[q][0]], positions[quads[q][1]], positions[quads[q][2]]);
291+
volume += getTriangleContributionToVolume(positions[quads[q][0]], positions[quads[q][2]], positions[quads[q][3]]);
300292
}
301293

302294
if(volume<0)

0 commit comments

Comments
 (0)