ScanClustering.cpp
Go to the documentation of this file.
1#include "ScanClustering.h"
2
3#include <cmath>
4#include <vector>
5
6#include <RobotAPI/interface/units/LaserScannerUnit.h>
7
9{
10 ScanClustering::ScanClustering(const Params& params) : params(params)
11 {
12 }
13
14 bool
15 ScanClustering::add(const LaserScanStep& scanStep)
16 {
17 if (scan.empty())
18 {
19 scan.push_back(scanStep);
20 return true;
21 }
22
23 if (not supports(scanStep))
24 {
25 return false;
26 }
27
28 scan.push_back(scanStep);
29 return true;
30 }
31
32 std::vector<LaserScan>
33 ScanClustering::detectClusters(const LaserScan& scan)
34 {
35 const auto isInvalid = [&](const LaserScanStep& step) -> bool
36 { return step.distance > params.maxDistance; };
37
38 std::vector<LaserScan> clusters;
39 for (const auto& laserScanStep : scan)
40 {
41 if (isInvalid(laserScanStep))
42 {
43 continue;
44 }
45
46 // cluster finished
47 if (not add(laserScanStep))
48 {
49 if (not cluster().empty())
50 {
51 clusters.push_back(cluster());
52 clear();
53
54 // work on a new cluster
55 add(laserScanStep);
56 }
57 }
58 }
59
60 // need to finish last open cluster?
61 if (not cluster().empty())
62 {
63 clusters.push_back(cluster());
64 clear();
65 }
66
67 return clusters;
68 }
69
70 const LaserScan&
72 {
73 return scan;
74 }
75
76 void
78 {
79 scan.clear();
80 }
81
82 bool
83 ScanClustering::supports(const LaserScanStep& scanStep)
84 {
85 // OK to create a new cluster if it's empty
86 if (scan.empty())
87 {
88 return true;
89 }
90
91 const float distanceDiff = std::fabs(scanStep.distance - scan.back().distance);
92 const bool isWithinDistanceRange = distanceDiff < params.distanceThreshold;
93
94 const float angleDiff = std::fabs(scanStep.angle - scan.back().angle);
95 const bool isWithinAngleRange = angleDiff < params.angleThreshold;
96
97 return (isWithinDistanceRange and isWithinAngleRange);
98 }
99} // namespace armarx::navigation::components::laser_scanner_feature_extraction
Clusters detectClusters(const LaserScan &scan)
Performs cluster detection on a full laser scan.