Skip to content

Commit 746b1b3

Browse files
th-skamepernod
andauthored
[algorithm] Refactor: More consistent check to initiate the puncture (#53)
* [algorithm] Add a proximity and then check for puncture + small clean & rework * [algorithm] Store variables retrieved with getValue() before entering a loop, whenever possible * Adopt 0_sreal Co-authored-by: erik pernod <[email protected]> --------- Co-authored-by: erik pernod <[email protected]>
1 parent c72dfa1 commit 746b1b3

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

src/sofa/collisionAlgorithm/algorithm/InsertionAlgorithm.h

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -122,46 +122,20 @@ class InsertionAlgorithm : public BaseAlgorithm
122122
auto& insertionOutput = *d_insertionOutput.beginEdit();
123123

124124
insertionOutput.clear();
125+
collisionOutput.clear();
125126

126127
if (m_couplingPts.empty())
127128
{
128-
const MechStateTipType::SPtr mstate = l_tipGeom->getContext()->get<MechStateTipType>();
129-
if (m_constraintSolver)
130-
{
131-
const auto& lambda =
132-
m_constraintSolver->getLambda()[mstate.get()].read()->getValue();
133-
SReal norm{0_sreal};
134-
for (const auto& l : lambda) {
135-
norm += l.norm();
136-
}
137-
if (norm > d_punctureForceThreshold.getValue())
138-
{
139-
auto findClosestProxOnShaft =
140-
Operations::FindClosestProximity::Operation::get(l_shaftGeom);
141-
auto projectOnShaft = Operations::Project::Operation::get(l_shaftGeom);
142-
for (const auto& dpair : collisionOutput)
143-
{
144-
// Reproject onto the needle to create an EdgeProximity - the
145-
// EdgeNormalHandler in the Constraint classes will need this
146-
const BaseProximity::SPtr shaftProx = findClosestProxOnShaft(
147-
dpair.second, l_shaftGeom.get(), projectOnShaft, getFilterFunc());
148-
m_couplingPts.push_back(dpair.second);
149-
insertionOutput.add(shaftProx, dpair.second);
150-
}
151-
collisionOutput.clear();
152-
return;
153-
}
154-
}
155-
156-
collisionOutput.clear();
157-
129+
// 1. Puncture algorithm
158130
auto createTipProximity =
159131
Operations::CreateCenterProximity::Operation::get(l_tipGeom->getTypeInfo());
160132
auto findClosestProxOnSurf =
161133
Operations::FindClosestProximity::Operation::get(l_surfGeom);
162134
auto projectOnSurf = Operations::Project::Operation::get(l_surfGeom);
163135
auto projectOnTip = Operations::Project::Operation::get(l_tipGeom);
164136

137+
const bool isProjective = d_projective.getValue();
138+
const SReal punctureForceThreshold = d_punctureForceThreshold.getValue();
165139
for (const auto& itTip : *l_tipGeom)
166140
{
167141
BaseProximity::SPtr tipProx = createTipProximity(itTip.element());
@@ -171,7 +145,27 @@ class InsertionAlgorithm : public BaseAlgorithm
171145
if (surfProx)
172146
{
173147
surfProx->normalize();
174-
if (d_projective.getValue())
148+
149+
// 1.1 Check whether puncture is happening - if so, create coupling point
150+
if (m_constraintSolver)
151+
{
152+
const MechStateTipType::SPtr mstate =
153+
l_tipGeom->getContext()->get<MechStateTipType>();
154+
const auto& lambda =
155+
m_constraintSolver->getLambda()[mstate.get()].read()->getValue();
156+
SReal norm{0_sreal};
157+
for (const auto& l : lambda) {
158+
norm += l.norm();
159+
}
160+
if (norm > punctureForceThreshold)
161+
{
162+
m_couplingPts.push_back(surfProx);
163+
continue;
164+
}
165+
}
166+
167+
// 1.2 If not, create a proximity pair for the tip-surface collision
168+
if (isProjective)
175169
{
176170
tipProx = projectOnTip(surfProx->getPosition(), itTip.element()).prox;
177171
if (!tipProx) continue;
@@ -183,11 +177,13 @@ class InsertionAlgorithm : public BaseAlgorithm
183177
}
184178
else
185179
{
180+
// 2. Needle insertion algorithm
186181
ElementIterator::SPtr itTip = l_tipGeom->begin();
187182
auto createTipProximity =
188183
Operations::CreateCenterProximity::Operation::get(itTip->getTypeInfo());
189184
const BaseProximity::SPtr tipProx = createTipProximity(itTip->element());
190185

186+
// 2.1 Check whether coupling point should be added
191187
const type::Vec3 tip2Pt = m_couplingPts.back()->getPosition() - tipProx->getPosition();
192188
if (tip2Pt.norm() > d_tipDistThreshold.getValue())
193189
{
@@ -204,6 +200,7 @@ class InsertionAlgorithm : public BaseAlgorithm
204200
}
205201
else // Don't bother with removing the point that was just added
206202
{
203+
// 2.2. Check whether coupling point should be removed
207204
ElementIterator::SPtr itShaft = l_shaftGeom->begin(l_shaftGeom->getSize() - 2);
208205
auto createShaftProximity =
209206
Operations::CreateCenterProximity::Operation::get(itShaft->getTypeInfo());
@@ -216,7 +213,11 @@ class InsertionAlgorithm : public BaseAlgorithm
216213
m_couplingPts.pop_back();
217214
}
218215
}
216+
}
219217

218+
if (!m_couplingPts.empty())
219+
{
220+
// 3. Re-project proximities on the shaft geometry
220221
auto findClosestProxOnShaft =
221222
Operations::FindClosestProximity::Operation::get(l_shaftGeom);
222223
auto projectOnShaft = Operations::Project::Operation::get(l_shaftGeom);

0 commit comments

Comments
 (0)