forked from JenningsL/slam-sysu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
287 lines (254 loc) · 7.92 KB
/
main.cpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "pointmatcher/PointMatcher.h"
#include <cassert>
#include <iostream>
#include <fstream>
#include <time.h>
#include <map>
#include <octomap/octomap.h>
#include <octomap/OcTree.h>
#include <octomap/ColorOcTree.h>
#include "icp.h"
#include "dbscan/dbscan.h"
#include "ransac/ransac.h"
using namespace std;
using namespace octomap;
using namespace Eigen;
#define MAX_RANGE -1
typedef PointMatcher<float>::TransformationParameters TransformMatrix;
// global
TransformMatrix TransAcc; // accumulated transform matrix
int total, progress; // for display progress
class CSVRow
{
public:
std::string const& operator[](std::size_t index) const
{
return m_data[index];
}
std::size_t size() const
{
return m_data.size();
}
void readNextRow(std::istream& str)
{
std::string line;
std::getline(str,line);
std::stringstream lineStream(line);
std::string cell;
m_data.clear();
while(std::getline(lineStream,cell,','))
{
m_data.push_back(cell);
}
}
private:
std::vector<std::string> m_data;
};
std::istream& operator>>(std::istream& str,CSVRow& data)
{
data.readNextRow(str);
return str;
}
/**
* read pointcloud from a csvfile
* @param filename [description]
* @return [description]
*/
Pointcloud readPointCloud(const char* filename) {
Pointcloud Q;
std::ifstream in(filename);
CSVRow row;
while(in >> row)
{
float x, y, z;
x = atof(row[0].c_str());
y = atof(row[1].c_str());
z = atof(row[2].c_str());
Q.push_back(x, y, z);
}
return Q;
}
void extractGround(Pointcloud P, Pointcloud & ground, Pointcloud &nonGround) {
bool *isGround;
isGround = ransacFitPlane(P, 0.5, 6000, 500);
int size = P.size();
for (int i = 0; i < size; i++) {
if (isGround[i]) {
ground.push_back(P[i]);
} else {
nonGround.push_back(P[i]);
}
}
}
void getPointcloudFeatures(Pointcloud cluster, point3d ¢roid) {
int size = cluster.size();
float x, y, z;
for (int i = 0; i < size; i++) {
x += cluster[i].x();
y += cluster[i].y();
z += cluster[i].z();
}
centroid = point3d(x/size, y/size, z/size);
}
/**
* filter out dynamic points
* @param tree [description]
* @param P [description]
* @return [pointcloud without dynamic points]
*/
Pointcloud dynamicFilter(ColorOcTree &tree, Pointcloud P) {
long beginTime = clock();
typedef std::map<int, Pointcloud> MAP;
typedef std::pair<int, Pointcloud> PAIR;
Pointcloud dcs; // dynamic candidates
Pointcloud stationary;
for (Pointcloud::iterator it = P.begin(); it != P.end(); it++) {
point3d endPoint((*it).x(), (*it).y(), (*it).z());
OcTreeNode* node = tree.search (endPoint);
if (node != NULL && node->getOccupancy() < 0.5) {
dcs.push_back(endPoint);
} else {
stationary.push_back(endPoint);
}
}
int size = dcs.size();
cout << "dynamic candidates size: " << size << "/" << P.size() << endl;
int* clusters_idxs = new int[size];
clusters_idxs = dbscan(dcs, 10, 1); // -min_points -epsilon
MAP clusterMap;
for (int i = 0; i < size; i++) {
int cluster_idx = clusters_idxs[i];
// if (cluster_idx == 0) continue; // noise
MAP::iterator it = clusterMap.find(cluster_idx);
float x = dcs[i].x();
float y = dcs[i].y();
float z = dcs[i].z();
if (it != clusterMap.end()) {
(it->second).push_back(point3d(x, y, z));
} else {
Pointcloud v;
v.push_back(point3d(x, y, z));
clusterMap.insert(PAIR(cluster_idx, v));
}
}
for (MAP::iterator it = clusterMap.begin(); it != clusterMap.end(); it++) {
Pointcloud cluster = it->second;
int cluster_idx = it->first;
point3d c; // centroid of the cluster
getPointcloudFeatures(cluster, c);
if (cluster_idx == 0 || cluster.size() < 100 || cluster.size() > 500 || c.z() > 1.5) {
stationary.push_back(cluster);
} else {
cout<< "cluster-" << it->first << " has " << cluster.size() << " points" << endl;
// clear points
Pointcloud tmp(P);
point3d lowerBound, upperBound;
cluster.calcBBX(lowerBound, upperBound);
lowerBound -= point3d(0.5, 0.5, 0.5);
upperBound += point3d(0.5, 0.5, 0.5);
tmp.crop(lowerBound, upperBound);
for (Pointcloud::iterator it = tmp.begin(); it != tmp.end(); it++) {
tree.updateNode((*it), false);
// ColorOcTreeNode* n = tree.updateNode((*it), true);
// n->setColor(255,0,0); // set color to red
}
}
}
long endTime = clock();
char msg[100];
sprintf(msg, "filter dynamic consume time: %.2f s.\n", (float)(endTime-beginTime)/1000000);
cout << msg;
cout << "dynamic points size: " << P.size() - stationary.size() << "/" << P.size() << endl;
return stationary;
}
void initMap(ColorOcTree &tree, Pointcloud P) {
// Pointcloud ground, PWithOutGround;
// extractGround(P, ground, PWithOutGround);
// P = PWithOutGround;
tree.insertPointCloud(P, point3d(0,0,0), MAX_RANGE, true); // maxrange
for (Pointcloud::iterator it = P.begin(); it != P.end(); it++) {
tree.setNodeColor((*it).x(), (*it).y(), (*it).z(), 0, 0, 255);
}
tree.updateInnerOccupancy();
}
Pointcloud updateMap(ColorOcTree &tree, Pointcloud P, Pointcloud lastP) {
Pointcloud ground, PWithOutGround, P_;
// icp and dynamic dectction should be implemented without ground points
P = icp(lastP, P, TransAcc);
// dynamic detection
extractGround(P, ground, PWithOutGround);
P_ = dynamicFilter(tree, PWithOutGround);
P_.push_back(ground);
P = P_;
// P = icp(lastP, P, TransAcc);
// P = dynamicFilter(tree, P);
long beginTime = clock();
tree.insertPointCloud(P, point3d(0, 0, 0), MAX_RANGE);
// for (Pointcloud::iterator it = P.begin(); it != P.end(); it++) {
// tree.setNodeColor((*it).x(), (*it).y(), (*it).z(), 0, 0, 255);
// } // color
long endTime = clock();
char msg[100];
sprintf(msg, "frame %d/%d completed, update map consume time: %.2f s.\n", progress, total, (float)(endTime-beginTime)/1000000);
cout << msg;
return P;
}
int main(int argc, char** argv) {
ColorOcTree tree (0.1); // create empty tree with resolution 0.1
int from = atoi(argv[1]);
int to = atoi(argv[2]);
int step = atoi(argv[3]);
string path = argv[4];
// init
char baseFile[50];
sprintf(baseFile, "%s (Frame %04d).csv", path.c_str(), from);
Pointcloud base = readPointCloud(baseFile);
initMap(tree, base);
TransAcc.resize(4, 4);
TransAcc.setIdentity();
total = (int) (to - from) / step;
progress = 1;
Pointcloud P, lastP;
lastP = base;
char file[50];
for (int i = from + 1; i <= to; i += step) {
sprintf(file, "%s (Frame %04d).csv", path.c_str(), i);
P = readPointCloud(file);
lastP = updateMap(tree, P, lastP);
progress++;
}
string result = "map.bt";
tree.writeBinary(result);
// color tree
// tree.updateInnerOccupancy();
// string result = "map.ot";
// tree.write(result);
cout << "wrote example file " << result << endl;
return 0;
/*test ransac performance*/
// Pointcloud P;
// char file[50];
// for (int i = from + 1; i <= to; i += step) {
// sprintf(file, "./data/dynamic_segment (Frame %04d).csv", i);
// P = readPointCloud(file);
// long beginTime = clock();
// ransacFitPlane(P, 0.5, 6000, 500);
// long endTime = clock();
// char msg[100];
// sprintf(msg, "ransac consume time: %.2f s.\n", (float)(endTime-beginTime)/1000000);
// cout << msg;
// }
/*test dbscan*/
// Pointcloud P;
// char file[50];
// for (int i = from + 1; i <= to; i += step) {
// sprintf(file, "./data/dynamic_segment (Frame %04d).csv", i);
// P = readPointCloud(file);
// long beginTime = clock();
// dbscan(P, 10, 1);
// long endTime = clock();
// char msg[100];
// sprintf(msg, "dbscan consume time: %.2f s.\n", (float)(endTime-beginTime)/1000000);
// cout << msg;
// }
}