39 #include <pcl/search/kdtree.h>
40 #include <pcl/common/eigen.h>
41 #include <pcl/common/common.h>
42 #include <pcl/common/point_tests.h>
55 const std::string & filePath,
57 const std::map<int, Transform> & poses,
58 const std::multimap<int, Link> & constraints,
59 const std::map<int, double> & stamps,
62 UDEBUG(
"%s", filePath.c_str());
63 std::string tmpPath = filePath;
71 return toro.
saveGraph(tmpPath, poses, constraints);
80 return g2o.saveGraph(tmpPath, poses, constraints);
91 if(stamps.size() != poses.size())
93 UERROR(
"When exporting poses to format 1 (RGBD-SLAM), stamps and poses maps should have the same size! stamps=%d poses=%d",
94 (
int)stamps.size(), (
int)poses.size());
101 fopen_s(&fout, tmpPath.c_str(),
"w");
103 fout = fopen(tmpPath.c_str(),
"w");
107 std::list<std::pair<int, Transform> > posesList;
108 for(std::map<int, Transform>::const_iterator
iter=poses.lower_bound(0);
iter!=poses.end(); ++
iter)
110 posesList.push_back(*
iter);
115 for(std::map<int, Transform>::const_iterator
iter=poses.begin();
iter!=poses.end() &&
iter->first < 0; ++
iter)
117 posesList.push_back(*
iter);
120 for(std::list<std::pair<int, Transform> >::const_iterator
iter=posesList.begin();
iter!=posesList.end(); ++
iter)
131 pose =
t.inverse() * pose;
135 pose =
t.inverse() * pose *
t;
141 if(
iter == posesList.begin())
146 fprintf(fout,
"#timestamp x y z qx qy qz qw id\n");
150 fprintf(fout,
"#timestamp x y z qx qy qz qw\n");
155 fprintf(fout,
"%f %f %f %f %f %f %f %f%s\n",
156 stamps.at(
iter->first),
176 pose =
t.inverse() * pose *
t;
180 const float *
p = (
const float *)pose.
data();
182 fprintf(fout,
"%f",
p[0]);
183 for(
int i=1;
i<pose.
size();
i++)
185 fprintf(fout,
" %f",
p[
i]);
198 const std::string & filePath,
200 std::map<int, Transform> & poses,
201 std::multimap<int, Link> * constraints,
202 std::map<int, double> * stamps)
207 std::multimap<int, Link> constraintsTmp;
212 *constraints = constraintsTmp;
220 std::multimap<int, Link> constraintsTmp;
221 UERROR(
"Cannot import from g2o format because it is not yet supported!");
227 file.open(filePath.c_str(), std::ifstream::in);
240 if(
str.size() &&
str.at(
str.size()-1) ==
'\r')
245 if(
str.empty() ||
str.at(0) ==
'#' ||
str.at(0) ==
'%')
252 std::list<std::string> strList =
uSplit(
str,
',');
253 if(strList.size() == 17)
255 double stamp =
uStr2Double(strList.front())/1000000000.0;
262 UWARN(
"Null transform read!? line parsed: \"%s\"",
str.c_str());
268 stamps->insert(std::make_pair(
id, stamp));
275 poses.insert(std::make_pair(
id, pose));
280 UERROR(
"Error parsing \"%s\" with EuRoC MAV format (should have 17 values: stamp x y z qw qx qy qz vx vy vz vr vp vy ax ay az)",
str.c_str());
286 if(strList.size() == 10)
300 stamps->insert(std::make_pair(
id, stamp));
303 if(poses.size() == 0)
307 pose = originPose * pose;
308 poses.insert(std::make_pair(
id, pose));
312 UERROR(
"Error parsing \"%s\" with Karlsruhe format (should have 10 values)",
str.c_str());
318 if(strList.size() == 12)
335 std::string millisecStr = strList[1];
336 while(millisecStr.size() < 6)
338 millisecStr =
"0" + millisecStr;
340 double stamp =
uStr2Double(strList[0] +
"." + millisecStr);
356 stamps->insert(std::make_pair(
id, stamp));
362 UERROR(
"Error parsing \"%s\" with St Lucia format (should have 12 values, e.g., 101215_153851_Ins0.log)",
str.c_str());
368 if(strList.size() == 25)
382 stamps->insert(std::make_pair(
id, stamp));
388 Transform & previousPose = poses.at(
id-1);
390 previousPose =
Transform(previousPose.
x(), previousPose.
y(),
yaw);
396 UERROR(
"Error parsing \"%s\" with Malaga Urban format (should have 25 values, *_GPS.txt)",
str.c_str());
402 if(strList.size() == 3)
415 stamps->insert(std::make_pair(
id, stamp));
421 Transform & previousPose = poses.at(
id-1);
423 previousPose =
Transform(previousPose.
x(), previousPose.
y(),
yaw);
426 if(poses.size() == 0)
430 pose = originPose * pose;
431 poses.insert(std::make_pair(
id, pose));
435 UDEBUG(
"Not valid values detected: \"%s\"",
str.c_str());
440 UERROR(
"Error parsing \"%s\" with NewCollege format (should have 3 values: stamp x y, found %d)",
str.c_str(), (
int)strList.size());
445 std::list<std::string> strList =
uSplit(
str);
446 if((strList.size() >= 8 &&
format!=11) || (strList.size() == 9 &&
format==11))
459 UWARN(
"Null transform read!? line parsed: \"%s\"",
str.c_str());
465 stamps->insert(std::make_pair(
id, stamp));
474 pose =
t * pose *
t.inverse();
480 poses.insert(std::make_pair(
id, pose));
485 UERROR(
"Error parsing \"%s\" with RGBD-SLAM format (should have 8 values (or 9 with id): stamp x y z qw qx qy qz [id])",
str.c_str());
498 pose =
t * pose *
t.inverse();
500 poses.insert(std::make_pair(
id, pose));
511 const std::string & filePath,
512 const std::map<int, GPS> & gpsValues,
515 UDEBUG(
"%s", filePath.c_str());
516 std::string tmpPath = filePath;
520 if(ext.compare(
"kml")!=0 && ext.compare(
"txt")!=0)
522 UERROR(
"Only txt and kml formats are supported!");
528 fopen_s(&fout, tmpPath.c_str(),
"w");
530 fout = fopen(tmpPath.c_str(),
"w");
534 if(ext.compare(
"kml")==0)
537 for(std::map<int, GPS>::const_iterator
iter=gpsValues.begin();
iter!=gpsValues.end(); ++
iter)
546 unsigned int abgr = 0xFF << 24 | (rgba & 0xFF) << 16 | (rgba & 0xFF00) | ((rgba >> 16) &0xFF);
548 std::string colorHexa =
uFormat(
"%08x", abgr);
550 fprintf(fout,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
551 fprintf(fout,
"<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n");
552 fprintf(fout,
"<Document>\n"
553 " <name>%s</name>\n", tmpPath.c_str());
554 fprintf(fout,
" <StyleMap id=\"msn_ylw-pushpin\">\n"
556 " <key>normal</key>\n"
557 " <styleUrl>#sn_ylw-pushpin</styleUrl>\n"
560 " <key>highlight</key>\n"
561 " <styleUrl>#sh_ylw-pushpin</styleUrl>\n"
564 " <Style id=\"sh_ylw-pushpin\">\n"
566 " <scale>1.2</scale>\n"
569 " <color>%s</color>\n"
572 " <Style id=\"sn_ylw-pushpin\">\n"
574 " <color>%s</color>\n"
576 " </Style>\n", colorHexa.c_str(), colorHexa.c_str());
577 fprintf(fout,
" <Placemark>\n"
579 " <styleUrl>#msn_ylw-pushpin</styleUrl>"
593 fprintf(fout,
"# stamp longitude latitude altitude error bearing\n");
594 for(std::map<int, GPS>::const_iterator
iter=gpsValues.begin();
iter!=gpsValues.end(); ++
iter)
596 fprintf(fout,
"%f %.*f %.*f %.*f %.*f %.*f\n",
597 iter->second.stamp(),
598 8,
iter->second.longitude(),
599 8,
iter->second.latitude(),
600 8,
iter->second.altitude(),
601 8,
iter->second.error(),
602 8,
iter->second.bearing());
613 float lengths[] = {100,200,300,400,500,600,700,800};
627 std::vector<float>
dist;
629 for (
unsigned int i=1;
i<poses.size();
i++) {
632 float dx = P1.
x()-P2.
x();
633 float dy = P1.
y()-P2.
y();
634 float dz = P1.
z()-P2.
z();
641 for (
unsigned int i=first_frame;
i<
dist.size();
i++)
648 float a = pose_error(0,0);
649 float b = pose_error(1,1);
650 float c = pose_error(2,2);
651 float d = 0.5*(
a+
b+
c-1.0);
656 float dx = pose_error.
x();
657 float dy = pose_error.
y();
658 float dz = pose_error.
z();
659 return sqrt(dx*dx+dy*dy+dz*dz);
663 const std::vector<Transform> &poses_gt,
664 const std::vector<Transform> &poses_result,
668 UASSERT(poses_gt.size() == poses_result.size());
671 std::vector<errors> err;
680 for (
unsigned int first_frame=0; first_frame<poses_gt.size(); first_frame+=step_size) {
696 Transform pose_delta_gt = poses_gt[first_frame].
inverse()*poses_gt[last_frame];
697 Transform pose_delta_result = poses_result[first_frame].
inverse()*poses_result[last_frame];
703 float num_frames = (
float)(last_frame-first_frame+1);
704 float speed =
len/(0.1*num_frames);
715 for (std::vector<errors>::iterator it=err.begin(); it!=err.end(); it++)
722 float num = err.size();
731 const std::vector<Transform> &poses_gt,
732 const std::vector<Transform> &poses_result,
736 UASSERT(poses_gt.size() == poses_result.size());
739 std::vector<errors> err;
742 for (
unsigned int i=0;
i<poses_gt.size()-1; ++
i)
748 float r_err = pose_error.
getAngle();
749 float t_err = pose_error.
getNorm();
752 err.push_back(
errors(
i,r_err,t_err,0,0));
759 for (std::vector<errors>::iterator it=err.begin(); it!=err.end(); it++)
766 float num = err.size();
773 const std::map<int, Transform> & groundTruth,
774 const std::map<int, Transform> & poses,
775 float & translational_rmse,
776 float & translational_mean,
777 float & translational_median,
778 float & translational_std,
779 float & translational_min,
780 float & translational_max,
781 float & rotational_rmse,
782 float & rotational_mean,
783 float & rotational_median,
784 float & rotational_std,
785 float & rotational_min,
786 float & rotational_max)
789 translational_rmse = 0.0f;
790 translational_mean = 0.0f;
791 translational_median = 0.0f;
792 translational_std = 0.0f;
793 translational_min = 0.0f;
794 translational_max = 0.0f;
796 rotational_rmse = 0.0f;
797 rotational_mean = 0.0f;
798 rotational_median = 0.0f;
799 rotational_std = 0.0f;
800 rotational_min = 0.0f;
801 rotational_max = 0.0f;
804 pcl::PointCloud<pcl::PointXYZ> cloud1, cloud2;
805 cloud1.resize(poses.size());
806 cloud2.resize(poses.size());
809 for(std::map<int, Transform>::const_iterator
iter=poses.begin();
iter!=poses.end(); ++
iter)
811 std::map<int, Transform>::const_iterator jter=groundTruth.find(
iter->first);
812 if(jter != groundTruth.end())
816 idFirst =
iter->first;
818 cloud1[oi] = pcl::PointXYZ(jter->second.x(), jter->second.y(), jter->second.z());
819 cloud2[oi++] = pcl::PointXYZ(
iter->second.x(),
iter->second.y(),
iter->second.z());
833 t = groundTruth.at(idFirst) * poses.at(idFirst).inverse();
836 std::vector<float> translationalErrors(poses.size());
837 std::vector<float> rotationalErrors(poses.size());
838 float sumTranslationalErrors = 0.0f;
839 float sumRotationalErrors = 0.0f;
840 float sumSqrdTranslationalErrors = 0.0f;
841 float sumSqrdRotationalErrors = 0.0f;
842 float radToDegree = 180.0f /
M_PI;
844 for(std::map<int, Transform>::const_iterator
iter=poses.begin();
iter!=poses.end(); ++
iter)
846 std::map<int, Transform>::const_iterator jter = groundTruth.find(
iter->first);
847 if(jter!=groundTruth.end())
850 Eigen::Vector3f xAxis(1,0,0);
852 Eigen::Vector3f
vB = jter->second.toEigen3f().linear()*xAxis;
853 double a = pcl::getAngle3D(Eigen::Vector4f(
vA[0],
vA[1],
vA[2], 0), Eigen::Vector4f(
vB[0],
vB[1],
vB[2], 0));
854 rotationalErrors[oi] =
a*radToDegree;
855 translationalErrors[oi] = pose.
getDistance(jter->second);
857 sumTranslationalErrors+=translationalErrors[oi];
858 sumSqrdTranslationalErrors+=translationalErrors[oi]*translationalErrors[oi];
859 sumRotationalErrors+=rotationalErrors[oi];
860 sumSqrdRotationalErrors+=rotationalErrors[oi]*rotationalErrors[oi];
864 translational_min = translational_max = translationalErrors[oi];
865 rotational_min = rotational_max = rotationalErrors[oi];
869 if(translationalErrors[oi] < translational_min)
871 translational_min = translationalErrors[oi];
873 else if(translationalErrors[oi] > translational_max)
875 translational_max = translationalErrors[oi];
878 if(rotationalErrors[oi] < rotational_min)
880 rotational_min = rotationalErrors[oi];
882 else if(rotationalErrors[oi] > rotational_max)
884 rotational_max = rotationalErrors[oi];
891 translationalErrors.resize(oi);
892 rotationalErrors.resize(oi);
895 float total =
float(oi);
896 translational_rmse =
std::sqrt(sumSqrdTranslationalErrors/total);
897 translational_mean = sumTranslationalErrors/total;
898 translational_median = translationalErrors[oi/2];
901 rotational_rmse =
std::sqrt(sumSqrdRotationalErrors/total);
902 rotational_mean = sumRotationalErrors/total;
903 rotational_median = rotationalErrors[oi/2];
910 const std::map<int, Transform> & poses,
911 const std::multimap<int, Link> & links,
912 float & maxLinearErrorRatio,
913 float & maxAngularErrorRatio,
914 float & maxLinearError,
915 float & maxAngularError,
916 const Link ** maxLinearErrorLink,
917 const Link ** maxAngularErrorLink,
920 maxLinearErrorRatio = -1;
921 maxAngularErrorRatio = -1;
923 maxAngularError = -1;
925 UDEBUG(
"poses=%d links=%d", (
int)poses.size(), (
int)links.size());
926 for(std::multimap<int, Link>::const_iterator
iter=links.begin();
iter!=links.end(); ++
iter)
929 if(
iter->second.from() !=
iter->second.to())
939 UWARN(
"Poses are null or not invertible, aborting optimized graph max error check! (Pose %d=%s Pose %d=%s)",
945 if(maxLinearErrorLink)
947 *maxLinearErrorLink = 0;
949 if(maxAngularErrorLink)
951 *maxAngularErrorLink = 0;
953 maxLinearErrorRatio = -1;
954 maxAngularErrorRatio = -1;
956 maxAngularError = -1;
962 float linearError =
uMax3(
965 force3DoF?0:
fabs(
iter->second.transform().z() -
t.z()));
967 float stddevLinear =
sqrt(
iter->second.transVariance(
false));
968 float linearErrorRatio = linearError/stddevLinear;
969 if(linearErrorRatio > maxLinearErrorRatio)
971 maxLinearError = linearError;
972 maxLinearErrorRatio = linearErrorRatio;
973 if(maxLinearErrorLink)
975 *maxLinearErrorLink = &
iter->second;
981 1.0 /
static_cast<double>(
iter->second.infMatrix().at<
double>(5,5)) < 9999.0)
983 float opt_roll,opt_pitch,opt_yaw;
984 float link_roll,link_pitch,link_yaw;
985 t.getEulerAngles(opt_roll, opt_pitch, opt_yaw);
986 iter->second.transform().getEulerAngles(link_roll, link_pitch, link_yaw);
987 float angularError =
uMax3(
988 force3DoF?0:
fabs(opt_roll - link_roll),
989 force3DoF?0:
fabs(opt_pitch - link_pitch),
990 fabs(opt_yaw - link_yaw));
991 angularError = angularError>
M_PI?2*
M_PI-angularError:angularError;
993 float stddevAngular =
sqrt(
iter->second.rotVariance(
false));
994 float angularErrorRatio = angularError/stddevAngular;
995 if(angularErrorRatio > maxAngularErrorRatio)
997 maxAngularError = angularError;
998 maxAngularErrorRatio = angularErrorRatio;
999 if(maxAngularErrorLink)
1001 *maxAngularErrorLink = &
iter->second;
1011 std::vector<double> maxOdomInf(6,0.0);
1012 maxOdomInf.resize(6,0.0);
1013 for(std::multimap<int, Link>::const_iterator
iter=links.begin();
iter!=links.end(); ++
iter)
1017 for(
int i=0;
i<6; ++
i)
1019 const double &
v =
iter->second.infMatrix().at<
double>(
i,
i);
1020 if(maxOdomInf[
i] <
v)
1027 if(maxOdomInf[0] == 0.0)
1038 std::multimap<int, Link> & links,
1044 std::multimap<int, Link>::iterator
iter = links.find(from);
1045 while(
iter != links.end() &&
iter->first == from)
1057 iter = links.find(to);
1058 while(
iter != links.end() &&
iter->first == to)
1071 std::multimap<
int, std::pair<int, Link::Type> > & links,
1077 std::multimap<int, std::pair<int, Link::Type> >
::iterator iter = links.find(from);
1078 while(
iter != links.end() &&
iter->first == from)
1090 iter = links.find(to);
1091 while(
iter != links.end() &&
iter->first == to)
1104 std::multimap<int, int> & links,
1109 std::multimap<int, int>::iterator
iter = links.find(from);
1110 while(
iter != links.end() &&
iter->first == from)
1112 if(
iter->second == to)
1122 iter = links.find(to);
1123 while(
iter != links.end() &&
iter->first == to)
1125 if(
iter->second == from)
1135 const std::multimap<int, Link> & links,
1141 std::multimap<int, Link>::const_iterator
iter = links.find(from);
1142 while(
iter != links.end() &&
iter->first == from)
1154 iter = links.find(to);
1155 while(
iter != links.end() &&
iter->first == to)
1167 std::multimap<int, std::pair<int, Link::Type> >::const_iterator
findLink(
1168 const std::multimap<
int, std::pair<int, Link::Type> > & links,
1174 std::multimap<int, std::pair<int, Link::Type> >::const_iterator
iter = links.find(from);
1175 while(
iter != links.end() &&
iter->first == from)
1187 iter = links.find(to);
1188 while(
iter != links.end() &&
iter->first == to)
1201 const std::multimap<int, int> & links,
1206 std::multimap<int, int>::const_iterator
iter = links.find(from);
1207 while(
iter != links.end() &&
iter->first == from)
1209 if(
iter->second == to)
1219 iter = links.find(to);
1220 while(
iter != links.end() &&
iter->first == to)
1222 if(
iter->second == from)
1233 const std::multimap<int, Link> & links,
1236 std::list<Link> output;
1237 for(std::multimap<int, Link>::const_iterator
iter=links.begin();
iter != links.end(); ++
iter)
1239 if(
iter->second.from() == from)
1241 output.push_back(
iter->second);
1243 else if(
iter->second.to() == from)
1245 output.push_back(
iter->second.inverse());
1252 const std::multimap<int, Link> & links)
1254 std::multimap<int, Link> output;
1255 for(std::multimap<int, Link>::const_iterator
iter=links.begin();
iter!=links.end(); ++
iter)
1259 output.insert(*
iter);
1266 const std::multimap<int, Link> & links,
1270 std::multimap<int, Link> output;
1271 for(std::multimap<int, Link>::const_iterator
iter=links.begin();
iter!=links.end(); ++
iter)
1275 if((!inverted &&
iter->second.from() !=
iter->second.to())||
1276 (inverted &&
iter->second.from() ==
iter->second.to()))
1278 output.insert(*
iter);
1281 else if((!inverted &&
iter->second.type() != filteredType)||
1282 (inverted &&
iter->second.type() == filteredType))
1284 output.insert(*
iter);
1291 const std::map<int, Link> & links,
1295 std::map<int, Link> output;
1296 for(std::map<int, Link>::const_iterator
iter=links.begin();
iter!=links.end(); ++
iter)
1300 if((!inverted &&
iter->second.from() !=
iter->second.to())||
1301 (inverted &&
iter->second.from() ==
iter->second.to()))
1303 output.insert(*
iter);
1306 else if((!inverted &&
iter->second.type() != filteredType)||
1307 (inverted &&
iter->second.type() == filteredType))
1309 output.insert(*
iter);
1316 const std::map<int, Transform> & poses,
1318 float horizontalFOV,
1320 float nearClipPlaneDistance,
1321 float farClipPlaneDistance,
1324 std::map<int, Transform> output;
1328 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZ>);
1329 std::vector<int> ids(poses.size());
1331 cloud->resize(poses.size());
1332 ids.resize(poses.size());
1334 for(std::map<int, rtabmap::Transform>::const_iterator
iter=poses.begin();
iter!=poses.end(); ++
iter)
1336 if(!
iter->second.isNull())
1338 (*cloud)[oi] = pcl::PointXYZ(
iter->second.x(),
iter->second.y(),
iter->second.z());
1339 ids[oi++] =
iter->first;
1347 pcl::IndicesPtr(
new std::vector<int>),
1351 nearClipPlaneDistance,
1352 farClipPlaneDistance,
1356 for(
unsigned int i=0;
i<
indices->size(); ++
i)
1358 output.insert(*poses.find(ids[
indices->at(
i)]));
1365 const std::map<int, Transform> & poses,
1370 if(poses.size() > 2 && radius > 0.0f)
1372 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZ>);
1373 cloud->resize(poses.size());
1375 for(std::map<int, Transform>::const_iterator
iter = poses.begin();
iter!=poses.end(); ++
iter, ++
i)
1377 (*cloud)[
i] = pcl::PointXYZ(
iter->second.x(),
iter->second.y(),
iter->second.z());
1378 UASSERT_MSG(pcl::isFinite((*cloud)[
i]),
uFormat(
"Invalid pose (%d) %s",
iter->first,
iter->second.prettyPrint().c_str()).c_str());
1383 std::vector<Transform> transforms =
uValues(poses);
1385 pcl::search::KdTree<pcl::PointXYZ>::Ptr
tree (
new pcl::search::KdTree<pcl::PointXYZ> (
false));
1386 tree->setInputCloud(cloud);
1387 std::set<int> indicesChecked;
1388 std::set<int> indicesKept;
1390 for(
unsigned int i=0;
i<cloud->size(); ++
i)
1392 if(indicesChecked.find(
i) == indicesChecked.end())
1395 std::vector<float> kDistances;
1396 tree->radiusSearch(cloud->at(
i), radius,
kIndices, kDistances);
1398 std::set<int> cloudIndices;
1399 const Transform & currentT = transforms.at(
i);
1403 if(indicesChecked.find(
kIndices[
j]) == indicesChecked.end())
1410 double a = pcl::getAngle3D(Eigen::Vector4f(
vA[0],
vA[1],
vA[2], 0), Eigen::Vector4f(
vB[0],
vB[1],
vB[2], 0));
1425 bool lastAdded =
false;
1426 for(std::set<int>::reverse_iterator
iter = cloudIndices.rbegin();
iter!=cloudIndices.rend(); ++
iter)
1430 indicesKept.insert(*
iter);
1433 indicesChecked.insert(*
iter);
1438 bool firstAdded =
false;
1439 for(std::set<int>::iterator
iter = cloudIndices.begin();
iter!=cloudIndices.end(); ++
iter)
1443 indicesKept.insert(*
iter);
1446 indicesChecked.insert(*
iter);
1454 UINFO(
"Cloud filtered In = %d, Out = %d (radius=%f angle=%f keepLatest=%d)", cloud->size(), indicesKept.size(), radius,
angle, keepLatest?1:0);
1458 std::map<int, Transform> keptPoses;
1459 for(std::set<int>::iterator
iter = indicesKept.begin();
iter!=indicesKept.end(); ++
iter)
1461 keptPoses.insert(std::make_pair(
names.at(*
iter), transforms.at(*
iter)));
1465 keptPoses.insert(*poses.begin());
1466 keptPoses.insert(*poses.rbegin());
1478 std::multimap<int, int> clusters;
1479 if(poses.size() > 1 && radius > 0.0f)
1481 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZ>);
1482 cloud->resize(poses.size());
1484 for(std::map<int, Transform>::const_iterator
iter = poses.begin();
iter!=poses.end(); ++
iter, ++
i)
1486 (*cloud)[
i] = pcl::PointXYZ(
iter->second.x(),
iter->second.y(),
iter->second.z());
1487 UASSERT_MSG(pcl::isFinite((*cloud)[
i]),
uFormat(
"Invalid pose (%d) %s",
iter->first,
iter->second.prettyPrint().c_str()).c_str());
1491 std::vector<int> ids =
uKeys(poses);
1492 std::vector<Transform> transforms =
uValues(poses);
1494 pcl::search::KdTree<pcl::PointXYZ>::Ptr
tree (
new pcl::search::KdTree<pcl::PointXYZ> (
false));
1495 tree->setInputCloud(cloud);
1497 for(
unsigned int i=0;
i<cloud->size(); ++
i)
1500 std::vector<float> kDistances;
1501 tree->radiusSearch(cloud->at(
i), radius,
kIndices, kDistances);
1503 std::set<int> cloudIndices;
1504 const Transform & currentT = transforms.at(
i);
1515 double a = pcl::getAngle3D(Eigen::Vector4f(
vA[0],
vA[1],
vA[2], 0), Eigen::Vector4f(
vB[0],
vB[1],
vB[2], 0));
1518 clusters.insert(std::make_pair(ids[
i], ids[
kIndices[
j]]));
1523 clusters.insert(std::make_pair(ids[
i], ids[
kIndices[
j]]));
1533 const std::map<int, Transform> & poses,
1534 const std::multimap<int, Link> & links,
1535 std::multimap<int, int> & hyperNodes,
1536 std::multimap<int, Link> & hyperLinks)
1538 UINFO(
"Input: poses=%d links=%d", (
int)poses.size(), (
int)links.size());
1540 std::map<int, int> posesToHyperNodes;
1541 std::map<int, std::multimap<int, Link> > clusterloopClosureLinks;
1544 std::multimap<int, Link> bidirectionalLoopClosureLinks;
1545 for(std::multimap<int, Link>::const_iterator jter=links.begin(); jter!=links.end(); ++jter)
1549 jter->second.userDataCompressed().empty())
1551 if(
uContains(poses, jter->second.from()) &&
1554 UASSERT_MSG(
graph::findLink(links, jter->second.to(), jter->second.from(),
false) == links.end(),
"Input links should be unique!");
1555 bidirectionalLoopClosureLinks.insert(std::make_pair(jter->second.from(), jter->second));
1561 UINFO(
"Clustering hyper nodes...");
1563 for(std::map<int, Transform>::const_reverse_iterator
iter=poses.rbegin();
iter!=poses.rend(); ++
iter)
1565 if(posesToHyperNodes.find(
iter->first) == posesToHyperNodes.end())
1567 int hyperNodeId =
iter->first;
1568 std::list<int> loopClosures;
1569 std::set<int> loopClosuresAdded;
1570 loopClosures.push_back(
iter->first);
1571 std::multimap<int, Link> clusterLinks;
1572 while(loopClosures.size())
1574 int id = loopClosures.front();
1575 loopClosures.pop_front();
1577 UASSERT(posesToHyperNodes.find(
id) == posesToHyperNodes.end());
1579 posesToHyperNodes.insert(std::make_pair(
id, hyperNodeId));
1580 hyperNodes.insert(std::make_pair(hyperNodeId,
id));
1582 for(std::multimap<int, Link>::const_iterator jter=bidirectionalLoopClosureLinks.find(
id); jter!=bidirectionalLoopClosureLinks.end() && jter->first==
id; ++jter)
1584 if(posesToHyperNodes.find(jter->second.to()) == posesToHyperNodes.end() &&
1585 loopClosuresAdded.find(jter->second.to()) == loopClosuresAdded.end())
1587 loopClosures.push_back(jter->second.to());
1588 loopClosuresAdded.insert(jter->second.to());
1589 clusterLinks.insert(*jter);
1590 clusterLinks.insert(std::make_pair(jter->second.to(), jter->second.inverse()));
1591 if(jter->second.from() < jter->second.to())
1593 UWARN(
"Child to Parent link? %d->%d (type=%d)",
1594 jter->second.from(),
1596 jter->second.type());
1601 UASSERT(clusterloopClosureLinks.find(hyperNodeId) == clusterloopClosureLinks.end());
1602 clusterloopClosureLinks.insert(std::make_pair(hyperNodeId, clusterLinks));
1603 UDEBUG(
"Created hyper node %d with %d children (%f%%)",
1604 hyperNodeId, (
int)loopClosuresAdded.size(),
float(posesToHyperNodes.size())/
float(poses.size())*100.0f);
1607 UINFO(
"Clustering hyper nodes... done! (%f s)",
timer.ticks());
1610 UINFO(
"Creating hyper links...");
1612 for(std::multimap<int, Link>::const_reverse_iterator jter=links.rbegin(); jter!=links.rend(); ++jter)
1616 !jter->second.userDataCompressed().empty()) &&
1617 uContains(poses, jter->second.from()) &&
1620 UASSERT_MSG(
uContains(posesToHyperNodes, jter->second.from()),
uFormat(
"%d->%d (type=%d)", jter->second.from(), jter->second.to(), jter->second.type()).c_str());
1621 UASSERT_MSG(
uContains(posesToHyperNodes, jter->second.to()),
uFormat(
"%d->%d (type=%d)", jter->second.from(), jter->second.to(), jter->second.type()).c_str());
1622 int hyperNodeIDFrom = posesToHyperNodes.at(jter->second.from());
1623 int hyperNodeIDTo = posesToHyperNodes.at(jter->second.to());
1626 if(hyperNodeIDFrom != hyperNodeIDTo)
1628 std::multimap<int, Link>::iterator tmpIter =
graph::findLink(hyperLinks, hyperNodeIDFrom, hyperNodeIDTo);
1629 if(tmpIter!=hyperLinks.end() &&
1630 hyperNodeIDFrom == jter->second.from() &&
1631 hyperNodeIDTo == jter->second.to() &&
1636 hyperLinks.erase(tmpIter);
1640 if(
graph::findLink(hyperLinks, hyperNodeIDFrom, hyperNodeIDTo) == hyperLinks.end())
1642 UASSERT(clusterloopClosureLinks.find(hyperNodeIDFrom) != clusterloopClosureLinks.end());
1643 UASSERT(clusterloopClosureLinks.find(hyperNodeIDTo) != clusterloopClosureLinks.end());
1644 std::multimap<int, Link> tmpLinks = clusterloopClosureLinks.at(hyperNodeIDFrom);
1645 tmpLinks.insert(clusterloopClosureLinks.at(hyperNodeIDTo).begin(), clusterloopClosureLinks.at(hyperNodeIDTo).end());
1646 tmpLinks.insert(std::make_pair(jter->second.from(), jter->second));
1648 std::list<int>
path =
computePath(tmpLinks, hyperNodeIDFrom, hyperNodeIDTo,
false,
true);
1650 uFormat(
"path.size()=%d, hyperNodeIDFrom=%d, hyperNodeIDTo=%d",
1653 hyperNodeIDTo).c_str());
1655 if(
path.size() > 10)
1657 UWARN(
"Large path! %d nodes", (
int)
path.size());
1658 std::stringstream
stream;
1672 std::list<int>::iterator
iter=
path.begin();
1676 std::multimap<int, Link>::const_iterator foundIter =
graph::findLink(tmpLinks, from, to,
false);
1677 UASSERT(foundIter != tmpLinks.end());
1678 Link hyperLink = foundIter->second;
1684 std::multimap<int, Link>::const_iterator foundIter =
graph::findLink(tmpLinks, from, to,
false);
1685 UASSERT(foundIter != tmpLinks.end());
1686 hyperLink = hyperLink.
merge(foundIter->second, jter->second.type());
1692 UASSERT(hyperLink.
to() == hyperNodeIDTo);
1693 hyperLinks.insert(std::make_pair(hyperNodeIDFrom, hyperLink));
1695 UDEBUG(
"Created hyper link %d->%d (%f%%)",
1696 hyperLink.
from(), hyperLink.
to(),
float(
i)/
float(links.size())*100.0f);
1699 UWARN(
"Large hyper link %d->%d (%f m)! original %d->%d (%f m)",
1703 jter->second.from(),
1705 jter->second.transform().getNorm());
1713 UINFO(
"Creating hyper links... done! (%f s)",
timer.ticks());
1759 typedef std::pair<int, float>
Pair;
1764 return a.second >
b.second;
1770 const std::map<int, rtabmap::Transform> & poses,
1771 const std::multimap<int, int> & links,
1774 bool updateNewCosts)
1776 std::list<std::pair<int, Transform> >
path;
1778 int startNode = from;
1781 std::map<int, Node>
nodes;
1782 nodes.insert(std::make_pair(startNode,
Node(startNode, 0, poses.at(startNode))));
1783 std::priority_queue<Pair, std::vector<Pair>,
Order> pq;
1784 std::multimap<float, int> pqmap;
1787 pqmap.insert(std::make_pair(0, startNode));
1791 pq.push(
Pair(startNode, 0));
1794 while((updateNewCosts && pqmap.size()) || (!updateNewCosts && pq.size()))
1799 currentNode = &
nodes.find(pqmap.begin()->second)->second;
1800 pqmap.erase(pqmap.begin());
1804 currentNode = &
nodes.find(pq.top().first)->second;
1810 if(currentNode->
id() == endNode)
1812 while(currentNode->
id()!=startNode)
1814 path.push_front(std::make_pair(currentNode->
id(), currentNode->
pose()));
1815 currentNode = &
nodes.find(currentNode->
fromId())->second;
1817 path.push_front(std::make_pair(startNode, poses.at(startNode)));
1822 for(std::multimap<int, int>::const_iterator
iter = links.find(currentNode->
id());
1823 iter!=links.end() &&
iter->first == currentNode->
id();
1826 std::map<int, Node>::iterator nodeIter =
nodes.find(
iter->second);
1827 if(nodeIter ==
nodes.end())
1829 std::map<int, rtabmap::Transform>::const_iterator poseIter = poses.find(
iter->second);
1830 if(poseIter == poses.end())
1832 UERROR(
"Next pose %d (from %d) should be found in poses! Ignoring it!",
iter->second,
iter->first);
1836 Node n(
iter->second, currentNode->
id(), poseIter->second);
1838 n.setDistToEnd(
n.distFrom(endPose));
1840 nodes.insert(std::make_pair(
iter->second,
n));
1843 pqmap.insert(std::make_pair(
n.totalCost(),
n.id()));
1847 pq.push(
Pair(
n.id(),
n.totalCost()));
1851 else if(updateNewCosts && nodeIter->second.isOpened())
1853 float newCostSoFar = currentNode->
costSoFar() + currentNode->
distFrom(nodeIter->second.pose());
1854 if(nodeIter->second.costSoFar() > newCostSoFar)
1857 for(std::multimap<float, int>::iterator mapIter=pqmap.begin(); mapIter!=pqmap.end(); ++mapIter)
1859 if(mapIter->second == nodeIter->first)
1861 pqmap.erase(mapIter);
1862 nodeIter->second.setCostSoFar(newCostSoFar);
1863 pqmap.insert(std::make_pair(nodeIter->second.totalCost(), nodeIter->first));
1876 const std::multimap<int, Link> & links,
1879 bool updateNewCosts,
1880 bool useSameCostForAllLinks)
1882 std::list<int>
path;
1884 int startNode = from;
1886 std::map<int, Node>
nodes;
1888 std::priority_queue<Pair, std::vector<Pair>,
Order> pq;
1889 std::multimap<float, int> pqmap;
1892 pqmap.insert(std::make_pair(0, startNode));
1896 pq.push(
Pair(startNode, 0));
1899 while((updateNewCosts && pqmap.size()) || (!updateNewCosts && pq.size()))
1904 currentNode = &
nodes.find(pqmap.begin()->second)->second;
1905 pqmap.erase(pqmap.begin());
1909 currentNode = &
nodes.find(pq.top().first)->second;
1915 if(currentNode->
id() == endNode)
1917 while(currentNode->
id()!=startNode)
1919 path.push_front(currentNode->
id());
1920 currentNode = &
nodes.find(currentNode->
fromId())->second;
1922 path.push_front(startNode);
1927 for(std::multimap<int, Link>::const_iterator
iter = links.find(currentNode->
id());
1928 iter!=links.end() &&
iter->first == currentNode->
id();
1931 std::map<int, Node>::iterator nodeIter =
nodes.find(
iter->second.to());
1933 if(!useSameCostForAllLinks)
1935 cost =
iter->second.transform().getNorm();
1937 if(nodeIter ==
nodes.end())
1941 n.setCostSoFar(currentNode->
costSoFar() + cost);
1942 nodes.insert(std::make_pair(
iter->second.to(),
n));
1945 pqmap.insert(std::make_pair(
n.totalCost(),
n.id()));
1949 pq.push(
Pair(
n.id(),
n.totalCost()));
1952 else if(!useSameCostForAllLinks && updateNewCosts && nodeIter->second.isOpened())
1954 float newCostSoFar = currentNode->
costSoFar() + cost;
1955 if(nodeIter->second.costSoFar() > newCostSoFar)
1958 for(std::multimap<float, int>::iterator mapIter=pqmap.begin(); mapIter!=pqmap.end(); ++mapIter)
1960 if(mapIter->second == nodeIter->first)
1962 pqmap.erase(mapIter);
1963 nodeIter->second.setCostSoFar(newCostSoFar);
1964 pqmap.insert(std::make_pair(nodeIter->second.totalCost(), nodeIter->first));
1981 bool lookInDatabase,
1982 bool updateNewCosts,
1983 float linearVelocity,
1984 float angularVelocity)
1989 std::list<std::pair<int, Transform> >
path;
1990 UDEBUG(
"fromId=%d, toId=%d, lookInDatabase=%d, updateNewCosts=%d, linearVelocity=%f, angularVelocity=%f",
1998 std::multimap<int, Link> allLinks;
2003 allLinks = memory->
getAllLinks(lookInDatabase,
true,
true);
2004 for(std::multimap<int, Link>::iterator
iter=allLinks.begin();
iter!=allLinks.end(); ++
iter)
2006 if(
iter->second.to() < 0)
2008 allLinks.insert(std::make_pair(
iter->second.to(),
iter->second.inverse()));
2011 UINFO(
"getting all %d links time = %f s", (
int)allLinks.size(),
t.ticks());
2015 int startNode = fromId;
2017 std::map<int, Node>
nodes;
2019 std::priority_queue<Pair, std::vector<Pair>,
Order> pq;
2020 std::multimap<float, int> pqmap;
2023 pqmap.insert(std::make_pair(0, startNode));
2027 pq.push(
Pair(startNode, 0));
2030 while((updateNewCosts && pqmap.size()) || (!updateNewCosts && pq.size()))
2035 currentNode = &
nodes.find(pqmap.begin()->second)->second;
2036 pqmap.erase(pqmap.begin());
2040 currentNode = &
nodes.find(pq.top().first)->second;
2046 if(currentNode->
id() == endNode)
2048 while(currentNode->
id()!=startNode)
2050 path.push_front(std::make_pair(currentNode->
id(), currentNode->
pose()));
2051 currentNode = &
nodes.find(currentNode->
fromId())->second;
2053 path.push_front(std::make_pair(startNode, currentNode->
pose()));
2058 std::multimap<int, Link> links;
2059 if(allLinks.size() == 0)
2061 links = memory->
getLinks(currentNode->
id(), lookInDatabase,
true);
2065 for(std::multimap<int, Link>::const_iterator
iter = allLinks.lower_bound(currentNode->
id());
2066 iter!=allLinks.end() &&
iter->first == currentNode->
id();
2069 links.insert(std::make_pair(
iter->second.to(),
iter->second));
2072 for(std::multimap<int, Link>::const_iterator
iter = links.begin();
iter!=links.end(); ++
iter)
2074 if(
iter->second.from() !=
iter->second.to())
2078 if(linearVelocity <= 0.0
f && angularVelocity <= 0.0
f)
2081 cost =
iter->second.transform().getNorm();
2085 if(linearVelocity > 0.0
f)
2087 cost +=
iter->second.transform().getNorm()/linearVelocity;
2089 if(angularVelocity > 0.0
f)
2091 Eigen::Vector4f
v1 = Eigen::Vector4f(nextPose.
x()-currentNode->
pose().
x(), nextPose.
y()-currentNode->
pose().
y(), nextPose.
z()-currentNode->
pose().
z(), 1.0f);
2094 cost +=
angle / angularVelocity;
2098 std::map<int, Node>::iterator nodeIter =
nodes.find(
iter->first);
2099 if(nodeIter ==
nodes.end())
2101 Node n(
iter->second.to(), currentNode->
id(), nextPose);
2103 n.setCostSoFar(currentNode->
costSoFar() + cost);
2104 nodes.insert(std::make_pair(
iter->second.to(),
n));
2107 pqmap.insert(std::make_pair(
n.totalCost(),
n.id()));
2111 pq.push(
Pair(
n.id(),
n.totalCost()));
2114 else if(updateNewCosts && nodeIter->second.isOpened())
2116 float newCostSoFar = currentNode->
costSoFar() + cost;
2117 if(nodeIter->second.costSoFar() > newCostSoFar)
2120 nodeIter->second.setPose(nextPose);
2123 for(std::multimap<float, int>::iterator mapIter=pqmap.begin(); mapIter!=pqmap.end(); ++mapIter)
2125 if(mapIter->second == nodeIter->first)
2127 pqmap.erase(mapIter);
2128 nodeIter->second.setCostSoFar(newCostSoFar);
2129 pqmap.insert(std::make_pair(nodeIter->second.totalCost(), nodeIter->first));
2142 std::stringstream
stream;
2144 std::list<std::pair<int, Transform> >::const_iterator previousIter =
path.end();
2146 for(std::list<std::pair<int, Transform> >::const_iterator
iter=
path.begin();
iter!=
path.end();++
iter)
2153 if(previousIter!=
path.end())
2158 std::multimap<int, Link>::iterator jter =
graph::findLink(allLinks, previousIter->first,
iter->first);
2159 if(jter != allLinks.end())
2169 ++linkTypes[jter->second.type()];
2170 stream <<
"[" << jter->second.type() <<
"]";
2171 length += jter->second.transform().getNorm();
2181 std::stringstream streamB;
2182 for(
unsigned int i=0;
i<linkTypes.size(); ++
i)
2188 streamB <<
i <<
"=" << linkTypes[
i];
2190 UDEBUG(
"Link types = %s", streamB.str().c_str());
2197 const std::map<int, rtabmap::Transform> & poses,
2202 std::map<int, float> nearestNodes =
findNearestNodes(targetPose, poses, 0, 0, 1);
2203 if(!nearestNodes.empty())
2205 id = nearestNodes.begin()->first;
2208 *
distance = nearestNodes.begin()->second;
2217 const std::map<int, Transform> & poses,
2224 std::map<int, Transform> nodesMinusTarget = poses;
2225 Transform targetPose = poses.at(nodeId);
2226 nodesMinusTarget.erase(nodeId);
2233 const std::map<int, Transform> & poses,
2241 std::map<int, float> foundNodes;
2247 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZ>);
2248 cloud->resize(poses.size());
2249 std::vector<int> ids(poses.size());
2251 for(std::map<int, Transform>::const_iterator
iter = poses.begin();
iter!=poses.end(); ++
iter)
2253 (*cloud)[oi] = pcl::PointXYZ(
iter->second.x(),
iter->second.y(),
iter->second.z());
2254 UASSERT_MSG(pcl::isFinite((*cloud)[oi]),
uFormat(
"Invalid pose (%d) %s",
iter->first,
iter->second.prettyPrint().c_str()).c_str());
2255 ids[oi] =
iter->first;
2263 pcl::search::KdTree<pcl::PointXYZ>::Ptr kdTree(
new pcl::search::KdTree<pcl::PointXYZ>);
2264 kdTree->setInputCloud(cloud);
2265 std::vector<int>
ind;
2266 std::vector<float> sqrdDist;
2267 pcl::PointXYZ pt(targetPose.
x(), targetPose.
y(), targetPose.
z());
2270 kdTree->radiusSearch(pt, radius,
ind, sqrdDist, k);
2274 kdTree->nearestKSearch(pt, k,
ind, sqrdDist);
2277 for(
unsigned int i=0;
i<
ind.size(); ++
i)
2286 double a = pcl::getAngle3D(Eigen::Vector4f(
vA[0],
vA[1],
vA[2], 0), Eigen::Vector4f(
vB[0],
vB[1],
vB[2], 0));
2289 foundNodes.insert(std::make_pair(ids[
ind[
i]], sqrdDist[
i]));
2294 foundNodes.insert(std::make_pair(ids[
ind[
i]], sqrdDist[
i]));
2299 UDEBUG(
"found nodes=%d", (
int)foundNodes.size());
2306 const std::map<int, Transform> & poses,
2313 std::map<int, Transform> nodesMinusTarget = poses;
2314 Transform targetPose = poses.at(nodeId);
2315 nodesMinusTarget.erase(nodeId);
2321 const std::map<int, Transform> & poses,
2327 std::map<int, Transform> foundPoses;
2328 for(std::map<int, float>::iterator
iter=nearestNodes.begin();
iter!=nearestNodes.end(); ++
iter)
2330 foundPoses.insert(*poses.find(
iter->first));
2332 UDEBUG(
"found nodes=%d/%d (radius=%f, angle=%f, k=%d)", (
int)foundPoses.size(), (
int)poses.size(), radius,
angle, k);
2341 std::map<int, float>
getNodesInRadius(
int nodeId,
const std::map<int, Transform> & nodes,
float radius)
2349 std::map<int, Transform>
getPosesInRadius(
int nodeId,
const std::map<int, Transform> & nodes,
float radius,
float angle)
2360 const std::vector<std::pair<int, Transform> > & path,
2361 unsigned int fromIndex,
2362 unsigned int toIndex)
2367 UASSERT(fromIndex <
path.size() && toIndex <
path.size() && fromIndex <= toIndex);
2368 if(fromIndex >= toIndex)
2370 toIndex = (
unsigned int)
path.size()-1;
2372 float x=0,
y=0,
z=0;
2373 for(
unsigned int i=fromIndex;
i<toIndex-1; ++
i)
2385 const std::map<int, Transform> & path)
2390 float x=0,
y=0,
z=0;
2391 std::map<int, Transform>::const_iterator
iter=
path.begin();
2397 x +=
fabs(previousPose.
x() - currentPose.
x());
2398 y +=
fabs(previousPose.
y() - currentPose.
y());
2399 z +=
fabs(previousPose.
z() - currentPose.
z());
2400 previousPose = currentPose;
2409 std::map<int, Transform> poses,
2410 const std::multimap<int, Link> & links)
2412 std::list<std::map<int, Transform> > paths;
2413 if(poses.size() && links.size())
2418 std::map<int, Transform>
path;
2419 for(std::map<int, Transform>::iterator
iter=poses.begin();
iter!=poses.end();)
2421 std::multimap<int, Link>::const_iterator jter =
findLink(links,
path.rbegin()->first,
iter->first);
2425 poses.erase(
iter++);
2433 paths.push_back(
path);
2446 min[0] =
max[0] = poses.begin()->second.x();
2447 min[1] =
max[1] = poses.begin()->second.y();
2448 min[2] =
max[2] = poses.begin()->second.z();
2449 for(std::map<int, Transform>::const_iterator
iter=poses.begin();
iter!=poses.end(); ++
iter)