Skip to content

Commit

Permalink
fix iterator bugs in path optimizers (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kazfyx authored Feb 20, 2024
1 parent c2f8621 commit 0b37972
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
17 changes: 15 additions & 2 deletions src/rl/plan/AdvancedOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,16 @@ namespace rl
{
changed = false;

for (VectorList::iterator i = path.begin(), j = ::std::next(i), k = ::std::next(j); i != path.end() && j != path.end() && k != path.end(); ++i, ++j, ++k)
for (VectorList::iterator i = path.begin(), j = ::std::next(i), k = ::std::next(j); i != path.end() && j != path.end() && k != path.end();)
{
::rl::math::Real ik = this->getModel()->distance(*i, *k);

if (!this->getVerifier()->isColliding(*i, *k, ik))
{
::rl::math::Real ij = this->getModel()->distance(*i, *j);
::rl::math::Real jk = this->getModel()->distance(*j, *k);
this->getModel()->interpolate(*i, *k, ij / (ij + jk), inter);
::rl::math::Real alpha = ij / (ij + jk);
this->getModel()->interpolate(*i, *k, alpha, inter);
::rl::math::Real ratio = this->getModel()->distance(*j, inter) / ik;

if (ratio > this->ratio)
Expand All @@ -93,6 +94,18 @@ namespace rl

changed = true;
}
else
{
++i;
++j;
++k;
}
}
else
{
++i;
++j;
++k;
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/rl/plan/Optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ namespace rl
bool changed = false;
::rl::math::Vector inter(this->getModel()->getDofPosition());

for (VectorList::iterator i = path.begin(), j = ::std::next(i); i != path.end() && j != path.end(); ++i, ++j)
for (VectorList::iterator i = path.begin(), j = ::std::next(i); i != path.end() && j != path.end();)
{
if (0 == length || this->getModel()->distance(*i, *j) > length)
if (length > 0 && this->getModel()->distance(*i, *j) > length)
{
this->getModel()->interpolate(*i, *j, static_cast<::rl::math::Real>(0.5), inter);
i = path.insert(j, inter);
j = path.insert(j, inter);

if (nullptr != this->getViewer())
{
Expand All @@ -99,6 +99,11 @@ namespace rl

changed = true;
}
else
{
++i;
++j;
}
}

return changed;
Expand Down
8 changes: 7 additions & 1 deletion src/rl/plan/SimpleOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace rl
{
changed = false;

for (VectorList::iterator i = path.begin(), j = ::std::next(i), k = ::std::next(j); i != path.end() && j != path.end() && k != path.end(); ++i, ++j, ++k)
for (VectorList::iterator i = path.begin(), j = ::std::next(i), k = ::std::next(j); i != path.end() && j != path.end() && k != path.end();)
{
::rl::math::Real ik = this->getModel()->distance(*i, *k);

Expand All @@ -69,6 +69,12 @@ namespace rl

changed = true;
}
else
{
++i;
++j;
++k;
}
}
}
}
Expand Down

0 comments on commit 0b37972

Please sign in to comment.