Skip to content

Commit a047216

Browse files
th-skamepernod
andauthored
[algorithm] Add some checks for null BaseProximity::SPtr (#65)
* [algorithm] shaftProx in if check and normalize * [algorithm] check for tipProx etc. * [algorithm] edgeProx in if check * [algorithm] Added comments * [algorithm] More compact syntax for shaftProx check in loop * [algorithm] Added a final check for coupling points removal --------- Co-authored-by: epernod <[email protected]>
1 parent 9f40bff commit a047216

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

src/sofa/collisionAlgorithm/algorithm/InsertionAlgorithm.h

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ class InsertionAlgorithm : public BaseAlgorithm
213213
auto createTipProximity =
214214
Operations::CreateCenterProximity::Operation::get(itTip->getTypeInfo());
215215
const BaseProximity::SPtr tipProx = createTipProximity(itTip->element());
216+
if (!tipProx) return;
216217

217218
// 2.1 Check whether coupling point should be added
218219
const type::Vec3 tip2Pt = m_couplingPts.back()->getPosition() - tipProx->getPosition();
@@ -251,15 +252,32 @@ class InsertionAlgorithm : public BaseAlgorithm
251252
auto createShaftProximity =
252253
Operations::CreateCenterProximity::Operation::get(itShaft->getTypeInfo());
253254
const BaseProximity::SPtr shaftProx = createShaftProximity(itShaft->element());
254-
const EdgeProximity::SPtr edgeProx = dynamic_pointer_cast<EdgeProximity>(shaftProx);
255-
const type::Vec3 normal = (edgeProx->element()->getP1()->getPosition() -
256-
edgeProx->element()->getP0()->getPosition())
257-
.normalized();
258-
// If the coupling point lies ahead of the tip (positive dot product),
259-
// the needle is retreating. The last coupling point is removed
260-
if (dot(tip2Pt, normal) > 0_sreal)
255+
if (shaftProx)
261256
{
262-
m_couplingPts.pop_back();
257+
const EdgeProximity::SPtr edgeProx =
258+
dynamic_pointer_cast<EdgeProximity>(shaftProx);
259+
if (edgeProx)
260+
{
261+
const type::Vec3 normal = (edgeProx->element()->getP1()->getPosition() -
262+
edgeProx->element()->getP0()->getPosition())
263+
.normalized();
264+
// If the (last) coupling point lies ahead of the tip (positive dot product),
265+
// the needle is retreating. Thus, that point is removed.
266+
if (dot(tip2Pt, normal) > 0_sreal)
267+
{
268+
m_couplingPts.pop_back();
269+
}
270+
}
271+
else
272+
{
273+
msg_warning() << "shaftGeom: " << l_shaftGeom->getName()
274+
<< " is not an EdgeGeometry. Point removal is disabled";
275+
}
276+
}
277+
else
278+
{
279+
msg_warning() << "Cannot create proximity from shaftGeom: "
280+
<< l_shaftGeom->getName() << " - point removal is disabled";
263281
}
264282
}
265283
}
@@ -274,8 +292,15 @@ class InsertionAlgorithm : public BaseAlgorithm
274292
{
275293
const BaseProximity::SPtr shaftProx = findClosestProxOnShaft(
276294
m_couplingPts[i], l_shaftGeom.get(), projectOnShaft, getFilterFunc());
295+
if (!shaftProx) continue;
296+
shaftProx->normalize();
277297
insertionOutput.add(shaftProx, m_couplingPts[i]);
278298
}
299+
// This is a final-frontier check: If there are coupling points stored, but the
300+
// findClosestProxOnShaf operation yields no proximities on the shaft, it could be
301+
// because the needle has exited abruptly. Thus, we clear the coupling points.
302+
if (insertionOutput.size() == 0)
303+
m_couplingPts.clear();
279304
}
280305

281306
d_collisionOutput.endEdit();

0 commit comments

Comments
 (0)