-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
355 lines (307 loc) · 9.95 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#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"
#include "tracking/TrackManager.h"
using namespace std;
using namespace octomap;
using namespace Eigen;
#define MAX_RANGE 10
#define GROUND_HEIGHT -0.7
#define CEIL_HEIGHT 1
// #define MAX_RANGE 30
// #define GROUND_HEIGHT -0.2
// #define CEIL_HEIGHT 100
typedef PointMatcher<float>::TransformationParameters TransformMatrix;
// global
TransformMatrix TransAcc; // accumulated transform matrix
int total, progress; // for display progress
TrackManager trackManager;
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 extractGround(Pointcloud P, Pointcloud & ground, Pointcloud &nonGround) {
point3d upper, lower;
P.calcBBX(lower, upper);
// cout << "lower: " << lower << endl;
// cout << "upper: " << upper << endl;
point3d ground_upper(upper.x(), upper.y(), lower.z() + 1);
point3d ground_lower(lower.x(), lower.y(), GROUND_HEIGHT);
// remove ceiling
upper = point3d(upper.x(), upper.y(), CEIL_HEIGHT);
ground.push_back(P);
nonGround.push_back(P);
ground.crop(lower, ground_upper);
nonGround.crop(ground_lower, upper);
}
Pointcloud limitXY(Pointcloud P, float max) {
point3d upper, lower;
P.calcBBX(lower, upper);
upper = point3d(max, max, upper.z());
lower = point3d(-max, -max, lower.z());
Pointcloud result;
result.push_back(P);
result.crop(lower, upper);
return result;
}
void getClusterFeatures(Pointcloud cluster, point3d ¢roid, point3d & boxSize) {
int size = cluster.size();
// calc centroid
float x = 0, y = 0, z = 0;
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);
// calc boxSize
point3d upper, lower;
cluster.calcBBX(lower, upper);
boxSize = point3d(upper.x() - lower.x(), upper.y() - lower.y(), upper.z() - lower.z());
}
void getInAndOutliners(Pointcloud P, bool * cs, Pointcloud &inliners, Pointcloud &outliners) {
inliners.clear();
outliners.clear();
int size = P.size();
int count = 0;
for (int i = 0; i < size; i++) {
if (cs[i]) {
inliners.push_back(P[i]);
} else {
outliners.push_back(P[i]);
count++;
}
}
}
bool matchIndoorCriteria(Pointcloud cluster) {
point3d c; // centroid of the cluster
point3d boxSize; // width, height, length of outerBox
getClusterFeatures(cluster, c, boxSize);
return cluster.size() > 20 && c.z() < 1 && (boxSize.z() * boxSize.z()) / (boxSize.x() * boxSize.y()) > 1;
}
bool matchOutdoorCriteria(Pointcloud cluster) {
point3d c; // centroid of the cluster
point3d boxSize; // width, height, length of outerBox
getClusterFeatures(cluster, c, boxSize);
bool boxSizeLimit = boxSize.x() < 2 && boxSize.y() < 2 && boxSize.z() < 2 && boxSize.z() > 0.5;
return cluster.size() > 20 && c.z() < 1 && c.z() > GROUND_HEIGHT && boxSizeLimit;
}
/**
* filter out dynamic points
* @param tree [description]
* @param P [description]
* @return [pointcloud without dynamic points]
*/
Pointcloud dynamicFilter(ColorOcTree &tree, Pointcloud P, bool * cs, std::vector<Pointcloud> &movingObjs) {
long beginTime = clock();
typedef std::map<int, Pointcloud> MAP;
typedef std::pair<int, Pointcloud> PAIR;
Pointcloud dcs; // dynamic candidates
Pointcloud stationary;
movingObjs.clear();
getInAndOutliners(P, cs, stationary, dcs);
int size = dcs.size();
cout << "dynamic candidates size: " << size << "/" << P.size() << endl;
int* clusters_idxs = new int[size];
clusters_idxs = dbscan(dcs, 10, 0.5); // -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
// point3d boxSize; // width, height, length of outerBox
// getClusterFeatures(cluster, c, boxSize);
// if (cluster_idx == 0 || cluster.size() < 20 || (boxSize.z() * boxSize.z()) / (boxSize.x() * boxSize.y()) < 1) {
if (cluster_idx == 0 || !matchOutdoorCriteria(cluster)) {
stationary.push_back(cluster);
} else {
cout<< "cluster-" << it->first << " has " << cluster.size() << " points" << endl;
// cout << (boxSize.z() * boxSize.z()) / (boxSize.x() * boxSize.y()) << endl;
// cluster expand
Pointcloud tmp(P);
point3d lowerBound, upperBound;
cluster.calcBBX(lowerBound, upperBound);
// point3d ex(0.3, 0.3, 0.3);
point3d ex(0.1, 0.1, 0.1);
lowerBound -= ex;
upperBound += ex;
tmp.crop(lowerBound, upperBound);
movingObjs.push_back(tmp);
}
}
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 showMovingObjsTrajectory(ColorOcTree &tree) {
TrackTarget target = trackManager.targets[0];
for (int i = 0; i < target.frames.size(); i++) {
point3d p = target.trajectory[i];
ColorOcTreeNode* n = tree.updateNode(p, true);
n->setColor(255,0,0); // set color to red
}
}
void initMap(ColorOcTree &tree, Pointcloud P) {
Pointcloud ground, PWithOutGround;
extractGround(P, ground, PWithOutGround);
P = PWithOutGround;
P = limitXY(P, MAX_RANGE);
for (Pointcloud::iterator it = P.begin(); it != P.end(); it++) {
tree.updateNode((*it).x(), (*it).y(), (*it).z(), true);
tree.setNodeColor((*it).x(), (*it).y(), (*it).z(), 0, 0, 255);
}
}
Pointcloud updateMap(ColorOcTree &tree, Pointcloud P, Pointcloud lastP) {
long beginTime = clock();
Pointcloud ground, PWithOutGround, P_;
// icp and dynamic dectction should be implemented without ground points
extractGround(P, ground, PWithOutGround);
P = PWithOutGround;
P = limitXY(P, MAX_RANGE);
bool * cs = new bool[P.size()];
P = icp(lastP, P, TransAcc, cs);
// dynamic detection
std::vector<Pointcloud> movingObjs;
P_ = dynamicFilter(tree, P, cs, movingObjs);
trackManager.update(movingObjs);
// add points
for (Pointcloud::iterator it = P_.begin(); it != P_.end(); it++) {
tree.updateNode((*it), true);
tree.setNodeColor((*it).x(), (*it).y(), (*it).z(), 0, 0, 255);
}
// mark and clear dynamic points
for (int i = 0; i < movingObjs.size(); i++) {
Pointcloud dynObj = movingObjs[i];
for (Pointcloud::iterator it = dynObj.begin(); it != dynObj.end(); it++) {
ColorOcTreeNode* node = tree.updateNode((*it), false);
node->setLogOdds(-0.4);
// ColorOcTreeNode* n = tree.updateNode((*it), true);
// n->setColor(255,0,0); // set color to red
}
}
tree.setNodeColor(TransAcc(0, 3), TransAcc(1, 3), TransAcc(2, 3), 255, 0, 255); // lidar current pos
long endTime = clock();
char msg[100];
sprintf(msg, "frame %d/%d completed, consumed time: %.2f s.\n", progress, total, (float)(endTime-beginTime)/1000000);
cout << msg;
delete[] cs;
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++;
}
// showMovingObjsTrajectory(tree);
trackManager.saveTargets();
// string result = "map.bt";
// tree.writeBinary(result);
string result = "map.ot";
tree.write(result);
cout << "wrote example file " << result << endl;
return 0;
}