-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsegmentator.h
167 lines (148 loc) · 4.87 KB
/
segmentator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#ifndef SEGMENTATOR_H
#define SEGMENTATOR_H
#include <queue>
#include <iostream>
#include "pointcloud.h"
#include "angleutils.h"
#define MIN_NUM_POINTS 100
// reference article: Robust segmentation in laser scanning 3D point cloud data.
template <size_t DIMENSION>
class Segmentator
{
public:
typedef typename Point<DIMENSION>::Vector Vector;
Segmentator(const PointCloud<DIMENSION> *pointCloud)
: mPointCloud(pointCloud)
, mNumVisitedPoints(0)
{
if (!pointCloud->hasConnectivity())
{
throw "You need to estimate normals first!";
}
mNewGraph = new ConnectivityGraph(mPointCloud->size());
for (size_t i = 0; i < mPointCloud->size(); i++)
{
mNewGraph->addNode(i, mPointCloud->connectivity()->neighbors(i));
}
mVisitedPoints = std::vector<bool>(pointCloud->size(), false);
}
Segmentator(const PointCloud<DIMENSION> *pointCloud, ConnectivityGraph *graph)
: mPointCloud(pointCloud)
, mNumVisitedPoints(0)
{
if (!pointCloud->hasConnectivity())
{
throw "You need to estimate normals first!";
}
mNewGraph = graph;
mVisitedPoints = std::vector<bool>(pointCloud->size(), false);
}
void segmentNewGroup()
{
if (mNumVisitedPoints / (float)mPointCloud->size() > 0.96f)
{
mNumVisitedPoints = mPointCloud->size();
return;
}
size_t minCurvature = getMinCurvaturePoint();
if (minCurvature == std::numeric_limits<size_t>::max()) return;
std::queue<size_t> queue;
queue.push(minCurvature);
mVisitedPoints[minCurvature] = true;
std::vector<size_t> pointsOnRegion;
while (!queue.empty())
{
size_t seedPoint = queue.front();
queue.pop();
++mNumVisitedPoints;
pointsOnRegion.push_back(seedPoint);
for (const size_t &neighbor : mNewGraph->neighbors(seedPoint))
{
if (!mVisitedPoints[neighbor] && getAngleBetween(seedPoint, neighbor) > sAngleThreshold)
{
mVisitedPoints[neighbor] = true;
queue.push(neighbor);
}
}
}
size_t group = mNewGraph->numGroups() + 1;
mNewGraph->addGroup(group, pointsOnRegion);
}
void segmentGroupsBFS()
{
std::queue<size_t> queue;
for (size_t i = 0; i < mPointCloud->size(); i++)
{
if (!mVisitedPoints[i])
{
mVisitedPoints[i] = true;
++mNumVisitedPoints;
queue.push(i);
std::vector<size_t> points;
while (!queue.empty())
{
size_t point = queue.front();
queue.pop();
points.push_back(point);
for (const size_t &neighbor : mNewGraph->neighbors(point))
{
if (!mVisitedPoints[neighbor])
{
mVisitedPoints[neighbor] = true;
queue.push(neighbor);
}
}
}
size_t group = mNewGraph->numGroups() + 1;
mNewGraph->addGroup(group, points);
}
}
}
void removeInvalidGroups()
{
std::set<size_t> groups = mNewGraph->groups();
for (const size_t &group : groups)
{
if (mNewGraph->numPointsInGroup(group) < MIN_NUM_POINTS)
{
mNewGraph->removeGroup(group);
}
}
}
size_t numVisitedPoints()
{
return mNumVisitedPoints;
}
ConnectivityGraph* getNewConnectivityGraph()
{
return mNewGraph;
}
private:
const static float sAngleThreshold;
const PointCloud<DIMENSION> *mPointCloud;
ConnectivityGraph *mNewGraph;
std::vector<bool> mVisitedPoints;
size_t mNumVisitedPoints;
size_t getMinCurvaturePoint()
{
size_t minCurvature = std::numeric_limits<size_t>::max();
for (size_t i = 0; i < mPointCloud->size(); i++)
{
if (!mVisitedPoints[i] && (minCurvature == std::numeric_limits<size_t>::max() ||
mPointCloud->at(i).curvature() < mPointCloud->at(minCurvature).curvature()))
{
minCurvature = i;
}
}
return minCurvature;
}
inline float getAngleBetween(size_t pointA, size_t pointB)
{
return std::abs(mPointCloud->at(pointA).normal().dot(mPointCloud->at(pointB).normal()));
}
};
template <size_t DIMENSION>
const float Segmentator<DIMENSION>::sAngleThreshold = std::cos(AngleUtils::deg2rad(20.0f));
template class Segmentator<3>;
typedef Segmentator<3> Segmentator3d;
#endif // SEGMENTATOR_H