Skip to content

Commit

Permalink
Optimizer: fixed some constraints between same nodes with different t…
Browse files Browse the repository at this point in the history
…ype ignored. GraphViewer: fixed display of links between same nodes with different type
  • Loading branch information
matlabbe committed Nov 13, 2023
1 parent 56552af commit 4812ce4
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
12 changes: 12 additions & 0 deletions corelib/include/rtabmap/core/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ std::multimap<int, Link>::iterator RTABMAP_CORE_EXPORT findLink(
int to,
bool checkBothWays = true,
Link::Type type = Link::kUndef);
std::multimap<int, std::pair<int, Link::Type> >::iterator RTABMAP_CORE_EXPORT findLink(
std::multimap<int, std::pair<int, Link::Type> > & links,
int from,
int to,
bool checkBothWays = true,
Link::Type type = Link::kUndef);
std::multimap<int, int>::iterator RTABMAP_CORE_EXPORT findLink(
std::multimap<int, int> & links,
int from,
Expand All @@ -147,6 +153,12 @@ std::multimap<int, Link>::const_iterator RTABMAP_CORE_EXPORT findLink(
int to,
bool checkBothWays = true,
Link::Type type = Link::kUndef);
std::multimap<int, std::pair<int, Link::Type> >::const_iterator RTABMAP_CORE_EXPORT findLink(
const std::multimap<int, std::pair<int, Link::Type> > & links,
int from,
int to,
bool checkBothWays = true,
Link::Type type = Link::kUndef);
std::multimap<int, int>::const_iterator RTABMAP_CORE_EXPORT findLink(
const std::multimap<int, int> & links,
int from,
Expand Down
66 changes: 66 additions & 0 deletions corelib/src/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,39 @@ std::multimap<int, Link>::iterator findLink(
return links.end();
}

std::multimap<int, std::pair<int, Link::Type> >::iterator findLink(
std::multimap<int, std::pair<int, Link::Type> > & links,
int from,
int to,
bool checkBothWays,
Link::Type type)
{
std::multimap<int, std::pair<int, Link::Type> >::iterator iter = links.find(from);
while(iter != links.end() && iter->first == from)
{
if(iter->second.first == to && (type==Link::kUndef || type == iter->second.second))
{
return iter;
}
++iter;
}

if(checkBothWays)
{
// let's try to -> from
iter = links.find(to);
while(iter != links.end() && iter->first == to)
{
if(iter->second.first == from && (type==Link::kUndef || type == iter->second.second))
{
return iter;
}
++iter;
}
}
return links.end();
}

std::multimap<int, int>::iterator findLink(
std::multimap<int, int> & links,
int from,
Expand Down Expand Up @@ -1118,6 +1151,39 @@ std::multimap<int, Link>::const_iterator findLink(
return links.end();
}

std::multimap<int, std::pair<int, Link::Type> >::const_iterator findLink(
const std::multimap<int, std::pair<int, Link::Type> > & links,
int from,
int to,
bool checkBothWays,
Link::Type type)
{
std::multimap<int, std::pair<int, Link::Type> >::const_iterator iter = links.find(from);
while(iter != links.end() && iter->first == from)
{
if(iter->second.first == to && (type==Link::kUndef || type == iter->second.second))
{
return iter;
}
++iter;
}

if(checkBothWays)
{
// let's try to -> from
iter = links.find(to);
while(iter != links.end() && iter->first == to)
{
if(iter->second.first == from && (type==Link::kUndef || type == iter->second.second))
{
return iter;
}
++iter;
}
}
return links.end();
}

std::multimap<int, int>::const_iterator findLink(
const std::multimap<int, int> & links,
int from,
Expand Down
17 changes: 9 additions & 8 deletions corelib/src/Optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,15 @@ void Optimizer::getConnectedGraph(

std::set<int> nextPoses;
nextPoses.insert(fromId);
std::multimap<int, int> biLinks;
std::multimap<int, std::pair<int, Link::Type> > biLinks;
for(std::multimap<int, Link>::const_iterator iter=linksIn.begin(); iter!=linksIn.end(); ++iter)
{
if(iter->second.from() != iter->second.to())
{
if(graph::findLink(biLinks, iter->second.from(), iter->second.to()) == biLinks.end())
if(graph::findLink(biLinks, iter->second.from(), iter->second.to(), true, iter->second.type()) == biLinks.end())
{
biLinks.insert(std::make_pair(iter->second.from(), iter->second.to()));
biLinks.insert(std::make_pair(iter->second.to(), iter->second.from()));
biLinks.insert(std::make_pair(iter->second.from(), std::make_pair(iter->second.to(), iter->second.type())));
biLinks.insert(std::make_pair(iter->second.to(), std::make_pair(iter->second.from(), iter->second.type())));
}
}
}
Expand All @@ -234,12 +234,13 @@ void Optimizer::getConnectedGraph(
}
}

for(std::multimap<int, int>::const_iterator iter=biLinks.find(currentId); iter!=biLinks.end() && iter->first==currentId; ++iter)
for(std::multimap<int, std::pair<int, Link::Type> >::const_iterator iter=biLinks.find(currentId); iter!=biLinks.end() && iter->first==currentId; ++iter)
{
int toId = iter->second;
int toId = iter->second.first;
Link::Type type = iter->second.second;
if(posesIn.find(toId) != posesIn.end() && (!landmarksIgnored() || toId>0))
{
std::multimap<int, Link>::const_iterator kter = graph::findLink(linksIn, currentId, toId);
std::multimap<int, Link>::const_iterator kter = graph::findLink(linksIn, currentId, toId, true, type);
if(nextPoses.find(toId) == nextPoses.end())
{
if(!uContains(posesOut, toId))
Expand Down Expand Up @@ -282,7 +283,7 @@ void Optimizer::getConnectedGraph(
}

// only add unique links
if(graph::findLink(linksOut, currentId, toId) == linksOut.end())
if(graph::findLink(linksOut, currentId, toId, true, kter->second.type()) == linksOut.end())
{
if(kter->second.to() < 0)
{
Expand Down
6 changes: 3 additions & 3 deletions guilib/src/GraphViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ class LinkItem: public QGraphicsLineItem
protected:
virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event )
{
QString str = QString("%1->%2 (%3 m)").arg(_from).arg(_to).arg(_poseA.getDistance(_poseB));
QString str = QString("%1->%2 (type=%3 length=%4 m)").arg(_from).arg(_to).arg(_link.type()).arg(_poseA.getDistance(_poseB));
if(!_link.transform().isNull())
{
str.append(QString("\n%1\n%2 %3").arg(_link.transform().prettyPrint().c_str()).arg(_link.transVariance()).arg(_link.rotVariance()));
str.append(QString("\n%1\nvar= %2 %3").arg(_link.transform().prettyPrint().c_str()).arg(_link.transVariance()).arg(_link.rotVariance()));
}
this->setToolTip(str);
QPen pen = this->pen();
Expand Down Expand Up @@ -566,7 +566,7 @@ void GraphViewer::updateGraph(const std::map<int, Transform> & poses,
itemIter = _linkItems.find(iter->first);
while(itemIter.key() == idFrom && itemIter != _linkItems.end())
{
if(itemIter.value()->to() == idTo)
if(itemIter.value()->to() == idTo && itemIter.value()->type() == iter->second.type())
{
itemIter.value()->setPoses(poseA, poseB, _viewPlane);
itemIter.value()->show();
Expand Down

0 comments on commit 4812ce4

Please sign in to comment.