Skip to content

Commit e4ab9eb

Browse files
th-skamepernod
andauthored
[algorithm] Refactor: Add sanity checks and failproof (#49)
* [algorithm] check whether a constraint solver is found in the context at init * [algorithm] check whether user provided acceptable values for thresholds used by the algorithm * [algorithm] sum all lambda values applied on the tipGeom mstate Still better to iterate and sum than accessing the first value (->[0]). It also makes sense in case one defines the tip as a collection of points (in case the needle has thickness). * [algorithm] Distinguish execution flow between puncture and insertion based on m_couplingPts.empty * Apply changes #1 Co-authored-by: erik pernod <erik.pernod@gmail.com> * Apply changes #2 Co-authored-by: erik pernod <erik.pernod@gmail.com> * Apply changes #3 Co-authored-by: erik pernod <erik.pernod@gmail.com> * Apply change #4 - Style Co-authored-by: erik pernod <erik.pernod@gmail.com> --------- Co-authored-by: erik pernod <erik.pernod@gmail.com>
1 parent df8a37f commit e4ab9eb

1 file changed

Lines changed: 26 additions & 6 deletions

File tree

src/sofa/collisionAlgorithm/algorithm/InsertionAlgorithm.h

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,11 @@ class InsertionAlgorithm : public BaseAlgorithm
4848
d_projective(initData(
4949
&d_projective, false, "projective",
5050
"Projection of closest detected proximity back onto the needle tip element.")),
51-
d_punctureForceThreshold(initData(&d_punctureForceThreshold,
52-
std::numeric_limits<double>::max(),
51+
d_punctureForceThreshold(initData(&d_punctureForceThreshold, -1_sreal,
5352
"punctureForceThreshold",
5453
"Threshold for the force applied to the needle tip. "
5554
"Once exceeded, puncture is initiated.")),
56-
d_tipDistThreshold(initData(&d_tipDistThreshold, std::numeric_limits<double>::min(),
57-
"tipDistThreshold",
55+
d_tipDistThreshold(initData(&d_tipDistThreshold, -1_sreal, "tipDistThreshold",
5856
"Threshold for the distance advanced by the needle tip since "
5957
"the last proximity detection. Once exceeded, a new "
6058
"proximity pair is added for the needle-volume coupling.")),
@@ -72,6 +70,24 @@ class InsertionAlgorithm : public BaseAlgorithm
7270
{
7371
BaseAlgorithm::init();
7472
this->getContext()->get<ConstraintSolver>(m_constraintSolver);
73+
msg_warning_when(!m_constraintSolver)
74+
<< "No constraint solver found in context. Insertion algorithm is disabled.";
75+
76+
if (d_punctureForceThreshold.getValue() < 0)
77+
{
78+
msg_warning() << d_punctureForceThreshold.getName() +
79+
" parameter not defined or set to negative value." msgendl
80+
<< "Puncture will not function properly; provide a positive value";
81+
d_punctureForceThreshold.setValue(std::numeric_limits<double>::max());
82+
}
83+
84+
if (d_tipDistThreshold.getValue() < 0)
85+
{
86+
msg_warning() << d_tipDistThreshold.getName() +
87+
" parameter not defined or set to negative value." msgendl
88+
<< "Needle-volume coupling is disabled; provide a positive value";
89+
d_tipDistThreshold.setValue(std::numeric_limits<double>::max());
90+
}
7591
}
7692

7793
void draw(const core::visual::VisualParams* vparams)
@@ -106,14 +122,18 @@ class InsertionAlgorithm : public BaseAlgorithm
106122
auto& collisionOutput = *d_collisionOutput.beginEdit();
107123
auto& insertionOutput = *d_insertionOutput.beginEdit();
108124

109-
if (insertionOutput.size() == 0)
125+
if (m_couplingPts.empty())
110126
{
111127
const MechStateTipType::SPtr mstate = l_tipGeom->getContext()->get<MechStateTipType>();
112128
if (m_constraintSolver)
113129
{
114130
const auto& lambda =
115131
m_constraintSolver->getLambda()[mstate.get()].read()->getValue();
116-
if (lambda[0].norm() > d_punctureForceThreshold.getValue())
132+
SReal norm{0_sreal};
133+
for (const auto& l : lambda) {
134+
norm += l.norm();
135+
}
136+
if (norm > d_punctureForceThreshold.getValue())
117137
{
118138
auto findClosestProxOnShaft =
119139
Operations::FindClosestProximity::Operation::get(l_shaftGeom);

0 commit comments

Comments
 (0)