-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunderground_filter.cpp
214 lines (179 loc) · 5.78 KB
/
underground_filter.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
// made by mirceta
#include <pcl/point_cloud.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <ctime>
#include <string>
#include <map>
#include <Windows.h>
using namespace std;
#pragma region [configuration]
string dmr_directory = "E:\\workspaces\\LIDAR_WORKSPACE\\tests\\test\\";
string dmr_file_name = "449_121";
string aug_directory = "E:\\workspaces\\LIDAR_WORKSPACE\\tests\\dmr_augs_merge_test\\";
string aug_file_name = "pbbmdf.txt";
#pragma endregion
#pragma region [auxiliary]
class AugmentablesFile {
public:
std::vector<pcl::PointXYZ> points;
std::vector<int> centerPointIndices;
std::map<int, int> CentralPointMap;
std::vector<float> radii;
};
class DMRStruct {
public:
pcl::PointCloud<pcl::PointXYZ>::Ptr pdmr; // DMR point cloud projected to the 3D plane z=0
std::vector<float> z_vals; // values for z
};
int ReadDMRStructure(DMRStruct& dmr, const string& filepath) {
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>(filepath, *cloud) == -1) //* load the file
{
PCL_ERROR("Couldn't read file test_pcd.pcd \n");
return (-1);
}
std::cout << "Loaded "
<< cloud->width * cloud->height
<< " data points from test_pcd.pcd with the following fields: "
<< std::endl;
vector<float> z_vals;
for (auto& x : cloud->points) {
z_vals.push_back(x.z);
x.z = 0.0f;
}
dmr.pdmr = cloud;
dmr.z_vals = z_vals;
return 0;
}
int ReadAugmentablesFile(AugmentablesFile &f, const string& filepath) {
// read the input file <point maxdim> => 1.0,2.0,3.0 5.0
std::vector<pcl::PointXYZ> points;
std::vector<int> centerPointIndices;
std::map<int, int> CentralPointMap; // accepts index of any point and returns the central point index
std::vector<float> radii;
ifstream infile;
infile.open(filepath, ios::in);
string line;
int currIdx = 0;
while (getline(infile, line)) {
// get the max dimension
std::vector<std::string> results;
boost::split(results, line, [](char c) {return c == ' '; });
float rbnn_r = atof(results[9].c_str());
radii.push_back(rbnn_r);
// get the central point
std::vector<std::string> centralPointStrings;
boost::split(centralPointStrings, results[0], [](char c) {return c == ','; });
float x = atof(centralPointStrings[0].c_str());
float y = atof(centralPointStrings[1].c_str());
float z = atof(centralPointStrings[2].c_str());
points.push_back(pcl::PointXYZ(x, y, z));
int currentCentralPointIndex = currIdx++;
CentralPointMap[currentCentralPointIndex] = currentCentralPointIndex;
centerPointIndices.push_back(currentCentralPointIndex);
// get the boundary points
for (int i = 1; i < 9; i++) {
std::vector<std::string> boundaryPointStrings;
boost::split(boundaryPointStrings, results[i], [](char c) {return c == ','; });
float x = atof(boundaryPointStrings[0].c_str());
float y = atof(boundaryPointStrings[1].c_str());
float z = atof(boundaryPointStrings[2].c_str());
points.push_back(pcl::PointXYZ(x, y, z));
CentralPointMap[currIdx++] = currentCentralPointIndex;
}
}
AugmentablesFile retval;
retval.centerPointIndices = centerPointIndices;
retval.CentralPointMap = CentralPointMap;
retval.points = points;
retval.radii = radii;
f = retval;
return 1;
}
int ParseArguments(int argc, char** argv) {
// parsing arguments
if (argc == 5) {
dmr_directory = argv[1];
dmr_file_name = argv[2];
aug_directory = argv[3];
aug_file_name = argv[4];
}
else {
return false;
}
return true;
}
#pragma endregion
/*
dmr should be in pcd format.
augmentables should be in the same format as overlap_compute tool's
args: dmr_dir dmr_filename augmentables_dir augmentables_filename
assumptions:
*/
int main (int argc, char** argv)
{
if (!ParseArguments(argc, argv)) {
return 1;
}
// read inputs
AugmentablesFile augs;
ReadAugmentablesFile(augs, aug_directory + "\\" + aug_file_name);
DMRStruct dmr;
ReadDMRStructure(dmr, dmr_directory + "\\" + dmr_file_name);
float dmr_density = max(abs(dmr.pdmr->points[0].x - dmr.pdmr->points[1].x),
abs(dmr.pdmr->points[0].y - dmr.pdmr->points[1].y));
// init DMR KDTree
pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
kdtree.setInputCloud(dmr.pdmr);
std::set<int> underground;
std::vector<int> pointIdxRadiusSearch;
std::vector<float> pointRadiusSquaredDistance;
for (int i = 0; i < augs.points.size(); i++)
{
pcl::PointXYZ pp(augs.points[i].x, augs.points[i].y, 0.0f); // projected point
float p_z = augs.points[i].z;
if (kdtree.radiusSearch(pp, sqrt(2 * pow(dmr_density, 2)) + 0.1f, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0)
{
if (pointIdxRadiusSearch.empty())
{
PCL_ERROR("At least one of the augmentations is not defined in the area of the DMR at all!\n");
}
else
{
// find the closest point
float min_r = 10.0f;
int min_r_idx = 0;
for (int j = 0; j < pointIdxRadiusSearch.size(); j++) {
int idx = pointIdxRadiusSearch[j];
pcl::PointXYZ nbr = dmr.pdmr->points[idx];
float dist = sqrt(pow(nbr.x - pp.x, 2) + pow(nbr.y - pp.y, 2));
if (dist < min_r) {
min_r = dist;
min_r_idx = pointIdxRadiusSearch[j];
}
}
// if the closest point is higher, then filter, if not then don't filter
if (dmr.z_vals[min_r_idx] > p_z) {
underground.insert(augs.CentralPointMap[i] / 9); // because there are 9 points per bounding box that stick together.
}
}
}
}
// write to the output file
ofstream outfile;
outfile.open(aug_directory + "\\" + "underground" + aug_file_name, ios::out);
stringstream ss;
for (auto & i : underground) {
ss << i << " ";
}
outfile << ss.str().substr(0, ss.str().length() - 1);
outfile.close();
cout << "results written to disc. Press any key to exit..." << endl;
return 0;
}