From 1588b0b25c9762560940d5d17b3acee4548eb97c Mon Sep 17 00:00:00 2001 From: Mathieu Labbe Date: Fri, 27 Feb 2015 19:33:32 -0500 Subject: [PATCH] refactored DBDriver (fixed the TODOs) --- corelib/src/DBDriver.cpp | 214 ++++++++++++++++++++---- guilib/include/rtabmap/gui/MainWindow.h | 1 + guilib/src/MainWindow.cpp | 22 +++ 3 files changed, 205 insertions(+), 32 deletions(-) diff --git a/corelib/src/DBDriver.cpp b/corelib/src/DBDriver.cpp index 94342483..f5d9d40b 100644 --- a/corelib/src/DBDriver.cpp +++ b/corelib/src/DBDriver.cpp @@ -307,8 +307,6 @@ void DBDriver::loadSignatures(const std::list & signIds, bool valueFound = false; _trashesMutex.lock(); { - _dbSafeAccessMutex.lock(); - _dbSafeAccessMutex.unlock(); for(std::list::iterator iter = ids.begin(); iter != ids.end();) { valueFound = false; @@ -361,8 +359,6 @@ void DBDriver::loadWords(const std::set & wordIds, std::list { if(_trashVisualWords.size()) { - _dbSafeAccessMutex.lock(); - _dbSafeAccessMutex.unlock(); for(std::set::iterator iter = ids.begin(); iter != ids.end();) { wIter = _trashVisualWords.find(*iter); @@ -394,15 +390,26 @@ void DBDriver::loadWords(const std::set & wordIds, std::list } } -//TODO Check also in the trash ? void DBDriver::loadNodeData(std::list & signatures, bool loadMetricData) const { + // Don't look in the trash, we assume that if we want to load + // data of a signature, it is not in thrash! Print an error if so. + _trashesMutex.lock(); + if(_trashSignatures.size()) + { + for(std::list::iterator iter=signatures.begin(); iter!=signatures.end(); ++iter) + { + UASSERT(*iter != 0); + UASSERT_MSG(uContains(_trashSignatures, (*iter)->id()), uFormat("Signature %d should not be used when transferred to trash!!!!", (*iter)->id()).c_str()); + } + } + _trashesMutex.unlock(); + _dbSafeAccessMutex.lock(); this->loadNodeDataQuery(signatures, loadMetricData); _dbSafeAccessMutex.unlock(); } -//TODO Check also in the trash ? void DBDriver::getNodeData( int signatureId, cv::Mat & imageCompressed, @@ -414,73 +421,216 @@ void DBDriver::getNodeData( float & cy, Transform & localTransform) const { - _dbSafeAccessMutex.lock(); - this->getNodeDataQuery(signatureId, imageCompressed, depthCompressed, laserScanCompressed, fx, fy, cx, cy, localTransform); - _dbSafeAccessMutex.unlock(); + bool found = false; + // look in the trash + _trashesMutex.lock(); + if(uContains(_trashSignatures, signatureId)) + { + const Signature * s = _trashSignatures.at(signatureId); + if(!s->getImageCompressed().empty() || !s->isSaved()) + { + imageCompressed = s->getImageCompressed(); + depthCompressed = s->getDepthCompressed(); + laserScanCompressed = s->getLaserScanCompressed(); + fx = s->getDepthFx(); + fy = s->getDepthFy(); + cx = s->getDepthCx(); + cy = s->getDepthCy(); + localTransform = s->getLocalTransform(); + found = true; + } + } + _trashesMutex.unlock(); + + if(!found) + { + _dbSafeAccessMutex.lock(); + this->getNodeDataQuery(signatureId, imageCompressed, depthCompressed, laserScanCompressed, fx, fy, cx, cy, localTransform); + _dbSafeAccessMutex.unlock(); + } } -//TODO Check also in the trash ? void DBDriver::getNodeData(int signatureId, cv::Mat & imageCompressed) const { - _dbSafeAccessMutex.lock(); - this->getNodeDataQuery(signatureId, imageCompressed); - _dbSafeAccessMutex.unlock(); + bool found = false; + // look in the trash + _trashesMutex.lock(); + if(uContains(_trashSignatures, signatureId)) + { + const Signature * s = _trashSignatures.at(signatureId); + if(!s->getImageCompressed().empty() || !s->isSaved()) + { + imageCompressed = s->getImageCompressed(); + found = true; + } + } + _trashesMutex.unlock(); + + if(!found) + { + _dbSafeAccessMutex.lock(); + this->getNodeDataQuery(signatureId, imageCompressed); + _dbSafeAccessMutex.unlock(); + } } -//TODO Check also in the trash ? void DBDriver::getPose(int signatureId, Transform & pose, int & mapId) const { - _dbSafeAccessMutex.lock(); - this->getPoseQuery(signatureId, pose, mapId); - _dbSafeAccessMutex.unlock(); + bool found = false; + // look in the trash + _trashesMutex.lock(); + if(uContains(_trashSignatures, signatureId)) + { + pose = _trashSignatures.at(signatureId)->getPose(); + mapId = _trashSignatures.at(signatureId)->mapId(); + found = true; + } + _trashesMutex.unlock(); + + if(!found) + { + _dbSafeAccessMutex.lock(); + this->getPoseQuery(signatureId, pose, mapId); + _dbSafeAccessMutex.unlock(); + } } -//TODO Check also in the trash ? void DBDriver::loadLinks(int signatureId, std::map & links, Link::Type type) const { - _dbSafeAccessMutex.lock(); - this->loadLinksQuery(signatureId, links, type); - _dbSafeAccessMutex.unlock(); + bool found = false; + // look in the trash + _trashesMutex.lock(); + if(uContains(_trashSignatures, signatureId)) + { + const Signature * s = _trashSignatures.at(signatureId); + UASSERT(s != 0); + for(std::multimap::const_iterator nIter = s->getLinks().begin(); + nIter!=s->getLinks().end(); + ++nIter) + { + if(type == Link::kUndef || nIter->second.type() == type) + { + links.insert(*nIter); + } + } + found = true; + } + _trashesMutex.unlock(); + + if(!found) + { + _dbSafeAccessMutex.lock(); + this->loadLinksQuery(signatureId, links, type); + _dbSafeAccessMutex.unlock(); + } } -//TODO Check also in the trash ? void DBDriver::getWeight(int signatureId, int & weight) const { - _dbSafeAccessMutex.lock(); - this->getWeightQuery(signatureId, weight); - _dbSafeAccessMutex.unlock(); + bool found = false; + // look in the trash + _trashesMutex.lock(); + if(uContains(_trashSignatures, signatureId)) + { + weight = _trashSignatures.at(signatureId)->getWeight(); + found = true; + } + _trashesMutex.unlock(); + + if(!found) + { + _dbSafeAccessMutex.lock(); + this->getWeightQuery(signatureId, weight); + _dbSafeAccessMutex.unlock(); + } } -//TODO Check also in the trash ? void DBDriver::getAllNodeIds(std::set & ids, bool ignoreChildren) const { + // look in the trash + _trashesMutex.lock(); + if(_trashSignatures.size()) + { + for(std::map::const_iterator sIter = _trashSignatures.begin(); sIter!=_trashSignatures.end(); ++sIter) + { + bool hasNeighbors = !ignoreChildren; + if(ignoreChildren) + { + for(std::multimap::const_iterator nIter = sIter->second->getLinks().begin(); + nIter!=sIter->second->getLinks().end(); + ++nIter) + { + if(nIter->second.type() == Link::kNeighbor) + { + hasNeighbors = true; + break; + } + } + } + if(hasNeighbors) + { + ids.insert(sIter->first); + } + } + + std::vector keys = uKeys(_trashSignatures); + + } + _trashesMutex.unlock(); + _dbSafeAccessMutex.lock(); this->getAllNodeIdsQuery(ids, ignoreChildren); _dbSafeAccessMutex.unlock(); } -//TODO Check also in the trash ? void DBDriver::getLastNodeId(int & id) const { + // look in the trash + _trashesMutex.lock(); + if(_trashSignatures.size()) + { + id = _trashSignatures.rbegin()->first; + } + _trashesMutex.unlock(); + _dbSafeAccessMutex.lock(); this->getLastIdQuery("Node", id); _dbSafeAccessMutex.unlock(); } -//TODO Check also in the trash ? void DBDriver::getLastWordId(int & id) const { + // look in the trash + _trashesMutex.lock(); + if(_trashVisualWords.size()) + { + id = _trashVisualWords.rbegin()->first; + } + _trashesMutex.unlock(); + _dbSafeAccessMutex.lock(); this->getLastIdQuery("Word", id); _dbSafeAccessMutex.unlock(); } -//TODO Check also in the trash ? void DBDriver::getInvertedIndexNi(int signatureId, int & ni) const { - _dbSafeAccessMutex.lock(); - this->getInvertedIndexNiQuery(signatureId, ni); - _dbSafeAccessMutex.unlock(); + bool found = false; + // look in the trash + _trashesMutex.lock(); + if(uContains(_trashSignatures, signatureId)) + { + ni = _trashSignatures.at(signatureId)->getWords().size(); + found = true; + } + _trashesMutex.unlock(); + + if(!found) + { + _dbSafeAccessMutex.lock(); + this->getInvertedIndexNiQuery(signatureId, ni); + _dbSafeAccessMutex.unlock(); + } } void DBDriver::getNodeIdByLabel(const std::string & label, int & id) const diff --git a/guilib/include/rtabmap/gui/MainWindow.h b/guilib/include/rtabmap/gui/MainWindow.h index 8a0ead52..01a7a1dd 100644 --- a/guilib/include/rtabmap/gui/MainWindow.h +++ b/guilib/include/rtabmap/gui/MainWindow.h @@ -113,6 +113,7 @@ protected: virtual void showEvent(QShowEvent* anEvent); virtual void moveEvent(QMoveEvent* anEvent); virtual void resizeEvent(QResizeEvent* anEvent); + virtual bool eventFilter(QObject *obj, QEvent *event); private slots: void changeState(MainWindow::State state); diff --git a/guilib/src/MainWindow.cpp b/guilib/src/MainWindow.cpp index 87b478ae..eb6bd94d 100644 --- a/guilib/src/MainWindow.cpp +++ b/guilib/src/MainWindow.cpp @@ -173,6 +173,19 @@ MainWindow::MainWindow(PreferencesDialog * prefDialog, QWidget * parent) : //_ui->dockWidget_odometry->setVisible(false); //_ui->dockWidget_cloudViewer->setVisible(false); //_ui->dockWidget_imageView->setVisible(false); + + // catch resize events + _ui->dockWidget_posterior->installEventFilter(this); + _ui->dockWidget_likelihood->installEventFilter(this); + _ui->dockWidget_rawlikelihood->installEventFilter(this); + _ui->dockWidget_statsV2->installEventFilter(this); + _ui->dockWidget_console->installEventFilter(this); + _ui->dockWidget_loopClosureViewer->installEventFilter(this); + _ui->dockWidget_mapVisibility->installEventFilter(this); + _ui->dockWidget_graphViewer->installEventFilter(this); + _ui->dockWidget_odometry->installEventFilter(this); + _ui->dockWidget_cloudViewer->installEventFilter(this); + _ui->dockWidget_imageView->installEventFilter(this); } _ui->widget_mainWindow->setVisible(false); @@ -2175,6 +2188,15 @@ void MainWindow::resizeEvent(QResizeEvent* anEvent) } } +bool MainWindow::eventFilter(QObject *obj, QEvent *event) +{ + if (event->type() == QEvent::Resize && qobject_cast(obj)) + { + this->setWindowModified(true); + } + return QWidget::eventFilter(obj, event); +} + void MainWindow::updateSelectSourceImageMenu(bool used, PreferencesDialog::Src src) { _ui->actionUsbCamera->setChecked(used && src == PreferencesDialog::kSrcUsbDevice);