-
Notifications
You must be signed in to change notification settings - Fork 4
/
process.cpp
201 lines (167 loc) · 4.7 KB
/
process.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
#include "process.h"
#include "utility.h"
#include <pcl/visualization/common/common.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <string>
#include <fstream>
#include <vector>
#include <pcl/io/pcd_io.h>
using namespace std;
using namespace utility;
bool Process::readPcdFileXYZ(const std::string &fileName, const pcXYZPtr &pointCloud)
{
if (pcl::io::loadPCDFile<pcl::PointXYZ>(fileName, *pointCloud) == -1) //* load the file
{
PCL_ERROR("Couldn't read file\n");
return false;
}
cout << "Data loaded" << endl;
cout << "Raw point number:" << pointCloud->size() << endl;
return true;
}
bool Process::writePcdFileXYZ(const string &fileName, const pcXYZPtr &pointCloud)
{
if (pcl::io::savePCDFileBinary(fileName, *pointCloud) == -1) //* load the file
{
PCL_ERROR("Couldn't write file\n");
return false;
}
return true;
}
void Process::display(const pcXYZPtr &cloudS, const pcXYZPtr &cloudT)
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(255, 255, 255);
char t[256];
string s;
int n = 0;
pcXYZRGBPtr pointcloudS(new pcXYZRGB());
pcXYZRGBPtr pointcloudT(new pcXYZRGB());
for (size_t i = 0; i < cloudT->points.size(); ++i)
{
pcl::PointXYZRGB pt;
pt.x = cloudT->points[i].x;
pt.y = cloudT->points[i].y;
pt.z = cloudT->points[i].z;
pt.r = 0;
pt.g = 0;
pt.b = 255;
pointcloudT->points.push_back(pt);
}
viewer->addPointCloud(pointcloudT, "pointcloudT");
for (size_t i = 0; i < cloudS->points.size(); ++i)
{
pcl::PointXYZRGB pt;
pt.x = cloudS->points[i].x;
pt.y = cloudS->points[i].y;
pt.z = cloudS->points[i].z;
pt.r = 255;
pt.g = 0;
pt.b = 0;
pointcloudS->points.push_back(pt);
}
viewer->addPointCloud(pointcloudS, "pointcloudS");
cout << "Click X(close) to continue..." << endl;
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
}
void Process::presort(const pcXYZPtr &pointCloud, int axis, vector<double> &cor, vector<int> &corkey)
{
for (size_t i = 0; i < pointCloud->size(); i++)
{
switch(axis){
case 1:{cor.push_back(pointCloud->points[i].x); break; }
case 2:{cor.push_back(pointCloud->points[i].y); break; }
case 3:{cor.push_back(pointCloud->points[i].z); break; }
default: cout << "A problem encountered." << endl;
}
corkey.push_back(i);
}
}
void Process::quicksort(vector<double> &data, vector<int> &datakey, int left, int right)
{
if (left >= right)
return;
int pivot = (left + right) / 2;
vector<double> less;
vector<double> greater;
vector<int> lesskey;
vector<int> greaterkey;
double tmp;
int tmpkey;
for (int i = left; i < right; i++)
{
if (i == pivot)
{
tmp = data[pivot];
tmpkey = datakey[pivot];
}
else if (data[i] < data[pivot])
{
less.push_back(data[i]);
lesskey.push_back(datakey[i]);
}
else
{
greater.push_back(data[i]);
greaterkey.push_back(datakey[i]);
}
}
for (int i = left; i < right; i++)
{
if (i < left + less.size())
{
data[i] = less[less.size() - i - 1 + left];
datakey[i] = lesskey[less.size() - i - 1 + left];
}
else if (i == left + less.size())
{
data[i] = tmp;
datakey[i] = tmpkey;
pivot = i;
}
else
{
data[i] = greater[greater.size() - i + less.size() + left];
datakey[i] = greaterkey[greater.size() - i + less.size() + left];
}
}
quicksort(data, datakey, left, pivot);
quicksort(data, datakey, pivot + 1, right);
}
void Process::saveindices(vector<int> &corkey, const pcXYZPtr &pCOriginal, const pcXYZPtr &pCSort)
{
pCSort->width = corkey.size();
pCSort->height = 1;
pCSort->resize(pCSort->width*pCSort->height);
for (size_t i = 0; i < corkey.size(); i++)
{
pCSort->points[i] = pCOriginal->points[corkey[i]];
}
}
void Process::cut(const pcXYZPtr &pointCloud, double IoU, const pcXYZPtr &pCSeg1, const pcXYZPtr &pCSeg2)
{
int indice_down, indice_up;
indice_down = (0.5 - IoU / 2)*pointCloud->size();
indice_up = (0.5 + IoU / 2)*pointCloud->size();
pCSeg1->width = indice_up;
pCSeg2->width = pointCloud->size()-indice_down;
pCSeg1->height = 1;
pCSeg2->height = 1;
pCSeg1->resize(pCSeg1->width*pCSeg1->height);
pCSeg2->resize(pCSeg2->width*pCSeg2->height);
for (size_t i = 0; i < indice_up; i++)
{
pCSeg1->points[i] = pointCloud->points[i];
}
for (size_t i = indice_down ; i < pointCloud->size(); i++)
{
pCSeg2->points[i-indice_down] = pointCloud->points[i];
}
cout << "Data Cut Done" << endl;
cout << "Seg1 point number:" << pCSeg1->size() << endl;
cout << "Seg2 point number:" << pCSeg2->size() << endl;
}