18 #ifndef karto_sdk_MAPPER_H
19 #define karto_sdk_MAPPER_H
23 #include <unordered_map>
28 #include "tbb/parallel_for.h"
29 #include "tbb/parallel_do.h"
30 #include "tbb/blocked_range.h"
52 virtual void Info(
const std::string& ) {};
53 friend class boost::serialization::access;
54 template<
class Archive>
55 void serialize(Archive &ar,
const unsigned int version)
59 BOOST_SERIALIZATION_ASSUME_ABSTRACT(MapperListener)
69 virtual void Debug(
const std::string& ) {};
70 friend class boost::serialization::access;
71 template<
class Archive>
72 void serialize(Archive &ar,
const unsigned int version)
76 BOOST_SERIALIZATION_ASSUME_ABSTRACT(MapperDebugListener)
97 friend class boost::serialization::access;
98 template<
class Archive>
99 void serialize(Archive &ar,
const unsigned int version)
103 BOOST_SERIALIZATION_ASSUME_ABSTRACT(MapperLoopClosureListener)
127 friend class boost::serialization::access;
128 template<
class Archive>
155 Update(rPose1, rPose2, rCovariance);
231 template<
class Archive>
234 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(
EdgeLabel);
235 ar & BOOST_SERIALIZATION_NVP(
m_Pose1);
236 ar & BOOST_SERIALIZATION_NVP(
m_Pose2);
282 inline const std::vector<Edge<T>*>&
GetEdges()
const
338 std::vector<Vertex<T>*> vertices;
378 friend class boost::serialization::access;
379 template<
class Archive>
383 ar & BOOST_SERIALIZATION_NVP(
m_Edges);
384 ar & BOOST_SERIALIZATION_NVP(
m_Score);
477 template<
class Archive>
482 ar & BOOST_SERIALIZATION_NVP(
m_pLabel);
503 friend class boost::serialization::access;
504 template<
class Archive>
509 BOOST_SERIALIZATION_ASSUME_ABSTRACT(Visitor<LocalizedRangeScan>)
544 friend class boost::serialization::access;
545 template<
class Archive>
548 ar & BOOST_SERIALIZATION_NVP(
m_pGraph);
551 BOOST_SERIALIZATION_ASSUME_ABSTRACT(GraphTraversal<LocalizedRangeScan>)
567 typedef std::map<Name, std::map<int, Vertex<T>*> >
VertexMap;
603 std::map<int, Vertex<LocalizedRangeScan>* >::iterator it =
m_Vertices[rName].find(idx);
611 std::cout <<
"RemoveVertex: Failed to remove vertex " << idx
612 <<
" because it doesnt exist in m_Vertices." << std::endl;
644 typename std::map<int, Vertex<T>*>::iterator iter;
645 for (iter = indexIter->second.begin(); iter != indexIter->second.end(); ++iter)
648 iter->second =
nullptr;
665 inline const std::vector<Edge<T>*>&
GetEdges()
const
692 friend class boost::serialization::access;
693 template<
class Archive>
696 std::cout <<
"Graph <- m_Edges; ";
697 ar & BOOST_SERIALIZATION_NVP(
m_Edges);
698 std::cout <<
"Graph <- m_Vertices\n";
794 std::vector<Vertex<LocalizedRangeScan>*> FindNearByVertices(
Name name,
const Pose2 refPose,
kt_double maxDistance);
808 return m_pLoopScanMatcher;
815 void UpdateLoopScanMatcher(
kt_double rangeThreshold);
826 std::map<int, Vertex<LocalizedRangeScan>* >::iterator it = m_Vertices[rName].find(pScan->
GetStateId());
827 if (it != m_Vertices[rName].end())
833 std::cout <<
"GetVertex: Failed to get vertex, idx " << pScan->
GetStateId() <<
834 " is not in m_Vertices." << std::endl;
891 Pose2 ComputeWeightedMean(
const Pose2Vector& rMeans,
const std::vector<Matrix3>& rCovariances)
const;
902 const Name& rSensorName,
924 friend class boost::serialization::access;
925 template<
class Archive>
928 std::cout <<
"MapperGraph <- Graph; ";
930 std::cout <<
"MapperGraph <- m_pMapper; ";
931 ar & BOOST_SERIALIZATION_NVP(m_pMapper);
932 std::cout <<
"MapperGraph <- m_pLoopScanMatcher; ";
933 ar & BOOST_SERIALIZATION_NVP(m_pLoopScanMatcher);
934 std::cout <<
"MapperGraph <- m_pTraversal\n";
935 ar & BOOST_SERIALIZATION_NVP(m_pTraversal);
1026 virtual std::unordered_map<int, Eigen::Vector3d>*
getGraph()
1028 std::cout <<
"getGraph method not implemented for this solver type. Graph visualization unavailable." << std::endl;
1035 virtual void ModifyNode(
const int& unique_id, Eigen::Vector3d pose)
1037 std::cout <<
"ModifyNode method not implemented for this solver type. Manual loop closure unavailable." << std::endl;
1044 std::cout <<
"GetNodeOrientation method not implemented for this solver type." << std::endl;
1047 friend class boost::serialization::access;
1048 template<
class Archive>
1053 BOOST_SERIALIZATION_ASSUME_ABSTRACT(ScanSolver)
1071 delete [] m_pKernel;
1093 assert(resolution != 0.0);
1096 kt_int32u borderSize = GetHalfKernelSize(smearDeviation, resolution) + 1;
1141 assert(m_pKernel != NULL);
1143 int gridIndex = GridIndex(rGridPoint);
1149 kt_int32s halfKernel = m_KernelSize / 2;
1152 for (
kt_int32s j = -halfKernel; j <= halfKernel; j++)
1156 kt_int32s kernelConstant = (halfKernel) + m_KernelSize * (j + halfKernel);
1161 for (
kt_int32s i = -halfKernel; i <= halfKernel; i++)
1163 kt_int32s kernelArrayIndex = i + kernelConstant;
1165 kt_int8u kernelValue = m_pKernel[kernelArrayIndex];
1166 if (kernelValue > pGridAdr[i])
1169 pGridAdr[i] = kernelValue;
1186 :
Grid<
kt_int8u>(width + borderSize * 2, height + borderSize * 2)
1187 , m_SmearDeviation(smearDeviation)
1190 GetCoordinateConverter()->SetScale(1.0 / resolution);
1206 assert(resolution != 0.0);
1207 assert(m_SmearDeviation != 0.0);
1211 const kt_double MIN_SMEAR_DISTANCE_DEVIATION = 0.5 * resolution;
1212 const kt_double MAX_SMEAR_DISTANCE_DEVIATION = 10 * resolution;
1215 if (!
math::InRange(m_SmearDeviation, MIN_SMEAR_DISTANCE_DEVIATION, MAX_SMEAR_DISTANCE_DEVIATION))
1217 std::stringstream error;
1218 error <<
"Mapper Error: Smear deviation too small: Must be between "
1219 << MIN_SMEAR_DISTANCE_DEVIATION
1221 << MAX_SMEAR_DISTANCE_DEVIATION;
1222 throw std::runtime_error(error.str());
1228 m_KernelSize = 2 * GetHalfKernelSize(m_SmearDeviation, resolution) + 1;
1231 m_pKernel =
new kt_int8u[m_KernelSize * m_KernelSize];
1232 if (m_pKernel == NULL)
1234 throw std::runtime_error(
"Unable to allocate memory for kernel!");
1238 kt_int32s halfKernel = m_KernelSize / 2;
1239 for (
kt_int32s i = -halfKernel; i <= halfKernel; i++)
1241 for (
kt_int32s j = -halfKernel; j <= halfKernel; j++)
1244 kt_double distanceFromMean = _hypot(i * resolution, j * resolution);
1246 kt_double distanceFromMean = hypot(i * resolution, j * resolution);
1248 kt_double z = exp(-0.5 * pow(distanceFromMean / m_SmearDeviation, 2));
1253 int kernelArrayIndex = (i + halfKernel) + m_KernelSize * (j + halfKernel);
1254 m_pKernel[kernelArrayIndex] =
static_cast<kt_int8u>(kernelValue);
1268 assert(resolution != 0.0);
1291 friend class boost::serialization::access;
1292 template<
class Archive>
1296 ar & BOOST_SERIALIZATION_NVP(m_SmearDeviation);
1297 ar & BOOST_SERIALIZATION_NVP(m_KernelSize);
1298 if (Archive::is_loading::value)
1300 m_pKernel =
new kt_int8u[m_KernelSize * m_KernelSize];
1302 ar & boost::serialization::make_array<kt_int8u>(m_pKernel, m_KernelSize * m_KernelSize);
1303 ar & BOOST_SERIALIZATION_NVP(m_Roi);
1306 BOOST_SERIALIZATION_ASSUME_ABSTRACT(CorrelationGrid)
1329 void operator() (
const kt_double& y)
const;
1350 template<
class T = LocalizedRangeScanVector>
1352 const T& rBaseScans,
1355 kt_bool doRefineMatch =
true);
1374 const Pose2& rSearchCenter,
1394 void ComputePositionalCovariance(
const Pose2& rBestPose,
1396 const Pose2& rSearchCenter,
1411 void ComputeAngularCovariance(
const Pose2& rBestPose,
1413 const Pose2& rSearchCenter,
1424 return m_pCorrelationGrid;
1465 : m_pMapper(pMapper)
1466 , m_pCorrelationGrid(NULL)
1467 , m_pSearchSpaceProbs(NULL)
1468 , m_pGridLookup(NULL)
1469 , m_pPoseResponse(NULL)
1470 , m_doPenalize(false)
1491 friend class boost::serialization::access;
1492 template<
class Archive>
1495 ar & BOOST_SERIALIZATION_NVP(m_pMapper);
1496 ar & BOOST_SERIALIZATION_NVP(m_pCorrelationGrid);
1497 ar & BOOST_SERIALIZATION_NVP(m_pSearchSpaceProbs);
1498 ar & BOOST_SERIALIZATION_NVP(m_pGridLookup);
1499 ar & BOOST_SERIALIZATION_NVP(m_xPoses);
1500 ar & BOOST_SERIALIZATION_NVP(m_yPoses);
1501 ar & BOOST_SERIALIZATION_NVP(m_rSearchCenter);
1502 ar & BOOST_SERIALIZATION_NVP(m_searchAngleResolution);
1503 ar & BOOST_SERIALIZATION_NVP(m_nAngles);
1504 ar & BOOST_SERIALIZATION_NVP(m_searchAngleResolution);;
1505 ar & BOOST_SERIALIZATION_NVP(m_doPenalize);
1514 static_cast<kt_int32u>(m_xPoses.size() * m_yPoses.size() * m_nAngles);
1519 m_pPoseResponse =
new std::pair<kt_double, Pose2>[poseResponseSize];
1520 ar & boost::serialization::make_array<std::pair<kt_double, Pose2>>(m_pPoseResponse,
1524 delete[] m_pPoseResponse;
1525 m_pPoseResponse =
nullptr;
1548 : m_RunningBufferMaximumSize(runningBufferMaximumSize)
1549 , m_RunningBufferMaximumDistance(runningBufferMaximumDistance)
1571 void RegisterSensor(
const Name& rSensorName);
1587 std::vector<Name> deviceNames;
1590 deviceNames.push_back(iter->first);
1619 void ClearLastScan(
const Name& name);
1628 std::map<int, LocalizedRangeScan*>::iterator it = m_Scans.find(
id);
1629 if (it != m_Scans.end())
1635 std::cout <<
"GetScan: id " <<
id <<
1636 " does not exist in m_scans, cannot retrieve it." << std::endl;
1676 void ClearRunningScans(
const Name& rSensorName);
1681 kt_int32u GetRunningScanBufferSize(
const Name& rSensorName);
1687 void SetRunningScanBufferSize(
kt_int32u rScanBufferSize);
1693 void SetRunningScanBufferMaximumDistance(
kt_double rScanBufferMaxDistance);
1723 if (m_ScanManagers.find(rSensorName) != m_ScanManagers.end())
1725 return m_ScanManagers[rSensorName];
1731 friend class boost::serialization::access;
1732 template<
class Archive>
1735 std::cout <<
"MapperSensorManager <- m_ScanManagers; ";
1736 ar & BOOST_SERIALIZATION_NVP(m_ScanManagers);
1737 ar & BOOST_SERIALIZATION_NVP(m_RunningBufferMaximumSize);
1738 ar & BOOST_SERIALIZATION_NVP(m_RunningBufferMaximumDistance);
1739 ar & BOOST_SERIALIZATION_NVP(m_NextScanId);
1740 std::cout <<
"MapperSensorManager <- m_Scans\n";
1741 ar & BOOST_SERIALIZATION_NVP(m_Scans);
1943 Mapper(
const std::string& rName);
1955 void Initialize(
kt_double rangeThreshold);
1961 void SaveToFile(
const std::string&
filename);
1967 void LoadFromFile(
const std::string&
filename);
1973 virtual void Reset();
2000 void ClearLocalizationBuffer();
2044 virtual ScanMatcher* GetSequentialScanMatcher()
const;
2058 return m_pMapperSensorManager;
2068 return m_pGraph->TryCloseLoop(pScan, rSensorName);
2073 m_pGraph->CorrectPoses();
2077 void InitializeParameters();
2096 void FireInfo(
const std::string& rInfo)
const;
2102 void FireDebug(
const std::string& rInfo)
const;
2108 void FireLoopClosureCheck(
const std::string& rInfo)
const;
2114 void FireBeginLoopClosure(
const std::string& rInfo)
const;
2120 void FireEndLoopClosure(
const std::string& rInfo)
const;
2342 friend class boost::serialization::access;
2343 template<
class Archive>
2346 std::cout <<
"Mapper <- Module\n";
2347 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(
Module);
2348 ar & BOOST_SERIALIZATION_NVP(m_Initialized);
2349 std::cout <<
"Mapper <- m_pSequentialScanMatcher\n";
2350 ar & BOOST_SERIALIZATION_NVP(m_pSequentialScanMatcher);
2351 std::cout <<
"Mapper <- m_pGraph\n";
2352 ar & BOOST_SERIALIZATION_NVP(m_pGraph);
2353 std::cout <<
"Mapper <- m_pMapperSensorManager\n";
2354 ar & BOOST_SERIALIZATION_NVP(m_pMapperSensorManager);
2355 std::cout <<
"Mapper <- m_Listeners\n";
2356 ar & BOOST_SERIALIZATION_NVP(m_Listeners);
2357 ar & BOOST_SERIALIZATION_NVP(m_pUseScanMatching);
2358 ar & BOOST_SERIALIZATION_NVP(m_pUseScanBarycenter);
2359 ar & BOOST_SERIALIZATION_NVP(m_pMinimumTimeInterval);
2360 ar & BOOST_SERIALIZATION_NVP(m_pMinimumTravelDistance);
2361 ar & BOOST_SERIALIZATION_NVP(m_pMinimumTravelHeading);
2362 ar & BOOST_SERIALIZATION_NVP(m_pScanBufferSize);
2363 ar & BOOST_SERIALIZATION_NVP(m_pScanBufferMaximumScanDistance);
2364 ar & BOOST_SERIALIZATION_NVP(m_pLinkMatchMinimumResponseFine);
2365 ar & BOOST_SERIALIZATION_NVP(m_pLinkScanMaximumDistance);
2366 ar & BOOST_SERIALIZATION_NVP(m_pDoLoopClosing);
2367 ar & BOOST_SERIALIZATION_NVP(m_pLoopSearchMaximumDistance);
2368 ar & BOOST_SERIALIZATION_NVP(m_pLoopMatchMinimumChainSize);
2369 ar & BOOST_SERIALIZATION_NVP(m_pLoopMatchMaximumVarianceCoarse);
2370 ar & BOOST_SERIALIZATION_NVP(m_pLoopMatchMinimumResponseCoarse);
2371 ar & BOOST_SERIALIZATION_NVP(m_pLoopMatchMinimumResponseFine);
2372 ar & BOOST_SERIALIZATION_NVP(m_pCorrelationSearchSpaceDimension);
2373 ar & BOOST_SERIALIZATION_NVP(m_pCorrelationSearchSpaceResolution);
2374 ar & BOOST_SERIALIZATION_NVP(m_pCorrelationSearchSpaceSmearDeviation);
2375 ar & BOOST_SERIALIZATION_NVP(m_pLoopSearchSpaceDimension);
2376 ar & BOOST_SERIALIZATION_NVP(m_pLoopSearchSpaceResolution);
2377 ar & BOOST_SERIALIZATION_NVP(m_pLoopSearchSpaceSmearDeviation);
2378 ar & BOOST_SERIALIZATION_NVP(m_pDistanceVariancePenalty);
2379 ar & BOOST_SERIALIZATION_NVP(m_pAngleVariancePenalty);
2380 ar & BOOST_SERIALIZATION_NVP(m_pFineSearchAngleOffset);
2381 ar & BOOST_SERIALIZATION_NVP(m_pCoarseSearchAngleOffset);
2382 ar & BOOST_SERIALIZATION_NVP(m_pCoarseAngleResolution);
2383 ar & BOOST_SERIALIZATION_NVP(m_pMinimumAnglePenalty);
2384 ar & BOOST_SERIALIZATION_NVP(m_pMinimumDistancePenalty);
2385 ar & BOOST_SERIALIZATION_NVP(m_pUseResponseExpansion);
2386 std::cout <<
"**Finished serializing Mapper**\n";
2393 bool getParamUseScanMatching();
2394 bool getParamUseScanBarycenter();
2395 double getParamMinimumTimeInterval();
2396 double getParamMinimumTravelDistance();
2397 double getParamMinimumTravelHeading();
2398 int getParamScanBufferSize();
2399 double getParamScanBufferMaximumScanDistance();
2400 double getParamLinkMatchMinimumResponseFine();
2401 double getParamLinkScanMaximumDistance();
2402 double getParamLoopSearchMaximumDistance();
2403 bool getParamDoLoopClosing();
2404 int getParamLoopMatchMinimumChainSize();
2405 double getParamLoopMatchMaximumVarianceCoarse();
2406 double getParamLoopMatchMinimumResponseCoarse();
2407 double getParamLoopMatchMinimumResponseFine();
2410 double getParamCorrelationSearchSpaceDimension();
2411 double getParamCorrelationSearchSpaceResolution();
2412 double getParamCorrelationSearchSpaceSmearDeviation();
2415 double getParamLoopSearchSpaceDimension();
2416 double getParamLoopSearchSpaceResolution();
2417 double getParamLoopSearchSpaceSmearDeviation();
2420 double getParamDistanceVariancePenalty();
2421 double getParamAngleVariancePenalty();
2422 double getParamFineSearchAngleOffset();
2423 double getParamCoarseSearchAngleOffset();
2424 double getParamCoarseAngleResolution();
2425 double getParamMinimumAnglePenalty();
2426 double getParamMinimumDistancePenalty();
2427 bool getParamUseResponseExpansion();
2431 void setParamUseScanMatching(
bool b);
2432 void setParamUseScanBarycenter(
bool b);
2433 void setParamMinimumTimeInterval(
double d);
2434 void setParamMinimumTravelDistance(
double d);
2435 void setParamMinimumTravelHeading(
double d);
2436 void setParamScanBufferSize(
int i);
2437 void setParamScanBufferMaximumScanDistance(
double d);
2438 void setParamLinkMatchMinimumResponseFine(
double d);
2439 void setParamLinkScanMaximumDistance(
double d);
2440 void setParamLoopSearchMaximumDistance(
double d);
2441 void setParamDoLoopClosing(
bool b);
2442 void setParamLoopMatchMinimumChainSize(
int i);
2443 void setParamLoopMatchMaximumVarianceCoarse(
double d);
2444 void setParamLoopMatchMinimumResponseCoarse(
double d);
2445 void setParamLoopMatchMinimumResponseFine(
double d);
2448 void setParamCorrelationSearchSpaceDimension(
double d);
2449 void setParamCorrelationSearchSpaceResolution(
double d);
2450 void setParamCorrelationSearchSpaceSmearDeviation(
double d);
2453 void setParamLoopSearchSpaceDimension(
double d);
2454 void setParamLoopSearchSpaceResolution(
double d);
2455 void setParamLoopSearchSpaceSmearDeviation(
double d);
2458 void setParamDistanceVariancePenalty(
double d);
2459 void setParamAngleVariancePenalty(
double d);
2460 void setParamFineSearchAngleOffset(
double d);
2461 void setParamCoarseSearchAngleOffset(
double d);
2462 void setParamCoarseAngleResolution(
double d);
2463 void setParamMinimumAnglePenalty(
double d);
2464 void setParamMinimumDistancePenalty(
double d);
2465 void setParamUseResponseExpansion(
bool b);
2467 BOOST_SERIALIZATION_ASSUME_ABSTRACT(Mapper)
2470 #endif // karto_sdk_MAPPER_H