51 const std::vector<unsigned int> & cameraIndices,
53 bool intermediateNodesIgnored,
54 bool landmarksIgnored,
59 const std::vector<Transform> & cameraLocalTransformOverrides) :
61 _paths(
uSplit(databasePath,
';')),
62 _odometryIgnored(odometryIgnored),
63 _ignoreGoalDelay(ignoreGoalDelay),
64 _goalsIgnored(goalsIgnored),
67 _cameraIndices(cameraIndices),
68 _intermediateNodesIgnored(intermediateNodesIgnored),
69 _landmarksIgnored(landmarksIgnored),
70 _featuresIgnored(featuresIgnored),
71 _priorsIgnored(priorsIgnored),
72 _startMapId(startMapId),
73 _stopMapId(stopMapId),
74 _cameraLocalTransformOverrides(cameraLocalTransformOverrides),
76 _currentId(_ids.
end()),
91 const std::vector<unsigned int> & cameraIndices,
93 bool intermediateNodesIgnored,
94 bool landmarksIgnored,
99 const std::vector<Transform> & cameraLocalTransformOverrides) :
101 _paths(databasePaths),
102 _odometryIgnored(odometryIgnored),
103 _ignoreGoalDelay(ignoreGoalDelay),
104 _goalsIgnored(goalsIgnored),
107 _cameraIndices(cameraIndices),
108 _intermediateNodesIgnored(intermediateNodesIgnored),
109 _landmarksIgnored(landmarksIgnored),
110 _featuresIgnored(featuresIgnored),
111 _priorsIgnored(priorsIgnored),
112 _startMapId(startMapId),
113 _stopMapId(stopMapId),
114 _cameraLocalTransformOverrides(cameraLocalTransformOverrides),
116 _currentId(_ids.
end()),
142 UERROR(
"Camera local transform overrides (%d) are not the same size than the camera indices (%d). The overrides are ignored.",
152 UERROR(
"Camera local transform overrides vector cannot contains null transforms! Clearing overrides.");
189 UERROR(
"No database path set...");
196 UERROR(
"Database path does not exist (%s)",
path.c_str());
205 UERROR(
"Driver doesn't exist.");
210 UERROR(
"Can't open database %s",
path.c_str());
223 UWARN(
"Start index is too high (%d), the last ID in database is %d. Starting from beginning...",
_startId, *
_ids.rbegin());
233 std::vector<CameraModel> models;
234 std::vector<StereoCameraModel> stereoModels;
239 if(models.at(0).isValidForProjection())
243 else if(models.at(0).fx() && models.at(0).fy() && models.at(0).imageWidth() == 0)
249 data.uncompressData(&rgb, 0);
250 if(
data.cameraModels().size() &&
data.cameraModels().at(0).isValidForProjection())
256 else if(stereoModels.size() && stereoModels.at(0).isValidForProjection())
264 if(
s->sensorData().imageCompressed().empty() &&
265 s->getWords().empty() &&
266 !
s->sensorData().laserScanCompressed().empty())
296 UERROR(
"DBReader only provides pose when capturing data, it cannot provide asynchronous pose.");
305 UINFO(
"Last ID %d has been reached! Ignoring",
_stopId);
310 UINFO(
"no more images...");
314 UWARN(
"Loading next database \"%s\"...",
_paths.front().c_str());
317 UERROR(
"Failed to initialize the next database \"%s\"",
_paths.front().c_str());
330 double previousStamp =
data.stamp();
331 if(previousStamp == 0)
337 data.userDataRaw().type() == CV_8SC1 &&
338 data.userDataRaw().cols >= 7 &&
339 data.userDataRaw().rows == 1 &&
340 memcmp(
data.userDataRaw().data,
"GOAL:", 5) == 0)
343 std::string goalStr = (
const char *)
data.userDataRaw().data;
346 std::list<std::string> strs =
uSplit(goalStr,
':');
349 goalId = *strs.rbegin();
350 data.setUserData(cv::Mat());
361 Transform localTransform, pose, groundTruth;
366 if(previousStamp && stamp && stamp > previousStamp)
368 delay = stamp - previousStamp;
374 UWARN(
"Goal \"%s\" detected, posting it! Waiting %f seconds before sending next data...",
375 goalId.c_str(), delay);
379 UWARN(
"Goal \"%s\" detected, posting it!", goalId.c_str());
409 std::list<int> signIds;
411 std::list<Signature *> signatures;
413 if(signatures.empty())
422 UDEBUG(
"Ignoring node %d (intermediate nodes ignored)",
s->id());
436 data =
s->sensorData();
441 cv::Mat globalPoseCov;
443 std::multimap<int, Link> priorLinks;
447 if( priorLinks.size() &&
448 !priorLinks.begin()->second.transform().isNull() &&
449 priorLinks.begin()->second.infMatrix().cols == 6 &&
450 priorLinks.begin()->second.infMatrix().rows == 6)
452 globalPose = priorLinks.begin()->second.transform();
453 globalPoseCov = priorLinks.begin()->second.infMatrix().inv();
454 if(
data.gps().stamp() != 0.0 &&
455 globalPoseCov.at<
double>(3,3)>=9999 &&
456 globalPoseCov.at<
double>(4,4)>=9999 &&
457 globalPoseCov.at<
double>(5,5)>=9999)
466 std::multimap<int, Link> gravityLinks;
468 if( gravityLinks.size() &&
469 !gravityLinks.begin()->second.transform().isNull() &&
470 gravityLinks.begin()->second.infMatrix().cols == 6 &&
471 gravityLinks.begin()->second.infMatrix().rows == 6)
473 gravityTransform = gravityLinks.begin()->second.transform();
479 std::multimap<int, Link> landmarkLinks;
481 for(std::multimap<int, Link>::iterator
iter=landmarkLinks.begin();
iter!=landmarkLinks.end(); ++
iter)
483 cv::Mat landmarkSize =
iter->second.uncompressUserDataConst();
484 landmarks.insert(std::make_pair(-
iter->first,
486 !landmarkSize.empty() && landmarkSize.type() == CV_32FC1 && landmarkSize.total()==1?landmarkSize.at<
float>(0,0):0.0f,
487 iter->second.transform(),
488 iter->second.infMatrix().inv())));
492 cv::Mat infMatrix = cv::Mat::eye(6,6,CV_64FC1);
495 std::multimap<int, Link> links;
497 if(links.size() && links.begin()->first < *
_currentId)
500 infMatrix = links.begin()->second.infMatrix();
509 UDEBUG(
"First node of map %d, variance set to 9999",
s->mapId());
518 infMatrix = links.begin()->second.infMatrix();
545 if(
s->getStamp() == 0)
547 UERROR(
"The option to use database stamps is set (framerate<0), but there are no stamps saved in the database! Aborting...");
555 double stamp =
s->getStamp();
556 if(sleepTime > 10000)
558 UWARN(
"Detected long delay (%d sec, stamps = %f vs %f). Waiting a maximum of 10 seconds.",
582 data.uncompressData();
583 std::map<int, int> cameraOldNewIndices;
584 std::vector<CameraModel> dbModels =
data.cameraModels();
585 if(dbModels.empty() && !
data.stereoCameraModels().empty())
587 for(
size_t i=0;
i<
data.stereoCameraModels().
size(); ++
i)
589 dbModels.push_back(
data.stereoCameraModels()[
i].left());
597 UERROR(
"Camera local transform overrides (%d) are not the same size than the camera indices (%d). The overrides are ignored.",
604 std::vector<Transform> combinedLocalTransforms;
608 cv::Mat combinedImages;
609 cv::Mat combinedDepthImages;
610 std::vector<CameraModel> combinedModels;
611 std::vector<StereoCameraModel> combinedStereoModels;
617 int addedCameras =
std::max(combinedModels.size(), combinedStereoModels.size());
619 int subImageWidth =
data.imageRaw().cols/dbModels.size();
621 data.imageRaw().cols % dbModels.size() == 0 &&
623 if(combinedImages.empty())
626 combinedImages = cv::Mat(
data.imageRaw().rows, subImageWidth*(
_cameraIndices.size()-
i),
data.imageRaw().type());
629 cv::Mat fromROI = cv::Mat(
data.imageRaw(), cv::Rect(
_cameraIndices[
i]*subImageWidth, 0, subImageWidth,
data.imageRaw().rows));
630 cv::Mat toROI = cv::Mat(combinedImages, cv::Rect(addedCameras*subImageWidth, 0, subImageWidth, combinedImages.rows));
631 fromROI.copyTo(toROI);
634 if(!
data.depthOrRightRaw().empty())
636 subImageWidth =
data.depthOrRightRaw().cols/dbModels.size();
637 UASSERT(
data.depthOrRightRaw().cols % dbModels.size() == 0 &&
638 subImageWidth ==
data.depthOrRightRaw().cols/(
int)dbModels.size() &&
640 if(combinedDepthImages.empty())
643 combinedDepthImages = cv::Mat(
data.depthOrRightRaw().rows, subImageWidth*(
_cameraIndices.size()-
i),
data.depthOrRightRaw().type());
645 fromROI = cv::Mat(
data.depthOrRightRaw(), cv::Rect(
_cameraIndices[
i]*subImageWidth, 0, subImageWidth,
data.depthOrRightRaw().rows));
646 toROI = cv::Mat(combinedDepthImages, cv::Rect(addedCameras*subImageWidth, 0, subImageWidth, combinedDepthImages.rows));
647 fromROI.copyTo(toROI);
650 if(!
data.cameraModels().empty())
657 combinedModels.push_back(
model);
658 combinedLocalTransforms.push_back(
model.localTransform());
667 combinedStereoModels.push_back(stereoModel);
672 if(!combinedModels.empty())
674 data.setRGBDImage(combinedImages, combinedDepthImages, combinedModels);
678 data.setStereoImage(combinedImages, combinedDepthImages, combinedStereoModels);
685 std::vector<CameraModel> combinedModels;
686 std::vector<StereoCameraModel> combinedStereoModels;
687 for(
size_t i=0;
i<dbModels.size(); ++
i)
689 if(!
data.cameraModels().empty())
693 combinedModels.push_back(
model);
694 combinedLocalTransforms.push_back(
model.localTransform());
700 combinedStereoModels.push_back(stereoModel);
704 if(!combinedModels.empty())
706 data.setCameraModels(combinedModels);
710 data.setStereoCameraModels(combinedStereoModels);
714 data.setStamp(
s->getStamp());
715 data.setGroundTruth(
s->getGroundTruthPose());
718 data.setGlobalPose(globalPose, globalPoseCov);
720 if(!gravityTransform.
isNull())
724 cv::Vec4d(
q.x(),
q.y(),
q.z(),
q.w()), cv::Mat::eye(3,3,CV_64FC1),
725 cv::Vec3d(), cv::Mat(),
726 cv::Vec3d(), cv::Mat(),
729 data.setLandmarks(landmarks);
731 UDEBUG(
"Laser=%d RGB/Left=%d Depth/Right=%d, Grid=%d, UserData=%d, GlobalPose=%d, GPS=%d, IMU=%d",
732 data.laserScanRaw().isEmpty()?0:1,
733 data.imageRaw().empty()?0:1,
734 data.depthOrRightRaw().empty()?0:1,
735 data.gridCellSize()==0.0f?0:1,
736 data.userDataRaw().empty()?0:1,
738 data.gps().stamp()!=0.0?1:0,
739 gravityTransform.
isNull()?0:1);
741 cv::Mat descriptors =
s->getWordsDescriptors().clone();
742 const std::vector<cv::KeyPoint> & keypoints =
s->getWordsKpts();
743 const std::vector<cv::Point3f> & keypoints3D =
s->getWords3();
745 !keypoints.empty() &&
746 (keypoints3D.empty() || keypoints.size() == keypoints3D.size()) &&
747 (descriptors.empty() || (
int)keypoints.size() == descriptors.rows))
749 if(!cameraOldNewIndices.empty())
751 cv::Mat newDescriptors;
752 std::vector<cv::KeyPoint> newKeypoints;
753 std::vector<cv::Point3f> newKeypoints3D;
754 UASSERT(!dbModels.empty() && dbModels[0].imageWidth()>0);
755 int subImageWidth = dbModels[0].imageWidth();
756 for(
size_t i = 0;
i<keypoints.size(); ++
i)
758 int cameraIndex =
int(keypoints.at(
i).pt.x / subImageWidth);
759 UASSERT_MSG(cameraIndex >= 0 && cameraIndex < (
int)dbModels.size(),
760 uFormat(
"cameraIndex=%d, db models=%d, kpt.x=%f, image width=%d",
761 cameraIndex, (
int)dbModels.size(), keypoints[
i].pt.x, subImageWidth).c_str());
762 if(cameraOldNewIndices.find(cameraIndex) != cameraOldNewIndices.end())
764 int newCameraIndex = cameraOldNewIndices.at(cameraIndex);
765 newKeypoints.push_back(keypoints[
i]);
766 newKeypoints.back().pt.x += (newCameraIndex-cameraIndex)*subImageWidth;
767 if(!keypoints3D.empty())
771 newKeypoints3D.push_back(pt);
773 if(!descriptors.empty())
775 newDescriptors.push_back(descriptors.row(
i));
779 data.setFeatures(newKeypoints, newKeypoints3D, newDescriptors);
781 else if(!combinedLocalTransforms.empty())
784 UASSERT(dbModels.size() == combinedLocalTransforms.size());
785 std::vector<cv::Point3f> newKeypoints3D;
786 for(
size_t i = 0;
i<keypoints3D.size(); ++
i)
790 newKeypoints3D.push_back(pt);
792 data.setFeatures(keypoints, newKeypoints3D, descriptors);
796 data.setFeatures(keypoints, keypoints3D, descriptors);
799 else if(!
_featuresIgnored && !keypoints.empty() && (!keypoints3D.empty() || !descriptors.empty()))
801 UERROR(
"Missing feature data, features won't be published.");
804 if(
data.imageRaw().empty() &&
data.imageCompressed().empty() &&
s->getWeight()>=0 && keypoints.empty())
806 UWARN(
"No image loaded from the database for id=%d!",
seq);
813 UWARN(
"Reading the database: odometry is null! "
814 "Please set \"Ignore odometry = true\" if there is "
815 "no odometry in the database.");
819 info->odomPose = pose;
820 info->odomCovariance = infMatrix.inv();
821 info->odomVelocity =
s->getVelocity();
822 UDEBUG(
"odom variance = %f/%f",
info->odomCovariance.at<
double>(0,0),
info->odomCovariance.at<
double>(5,5));
831 UERROR(
"Not initialized...");