-
Notifications
You must be signed in to change notification settings - Fork 288
/
CloudSliceProcessor.cpp
231 lines (168 loc) · 8.27 KB
/
CloudSliceProcessor.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
/*
* This file is part of Kintinuous.
*
* Copyright (C) 2015 The National University of Ireland Maynooth and
* Massachusetts Institute of Technology
*
* The use of the code within this file and all code within files that
* make up the software that is Kintinuous is permitted for
* non-commercial purposes only. The full terms and conditions that
* apply to the code within this file are detailed within the LICENSE.txt
* file and at <http://www.cs.nuim.ie/research/vision/data/kintinuous/code.php>
* unless explicitly stated. By downloading this file you agree to
* comply with these terms.
*
* If you wish to use any of this code for commercial purposes then
* please email commercialisation@nuim.ie.
*/
#include "CloudSliceProcessor.h"
CloudSliceProcessor::CloudSliceProcessor()
: ThreadObject("CloudSliceProcessorThread")
{
reset();
}
CloudSliceProcessor::~CloudSliceProcessor()
{
}
void CloudSliceProcessor::reset()
{
latestPushedCloud = 0;
cycledMutex = false;
}
bool inline CloudSliceProcessor::process()
{
boost::mutex::scoped_lock lock(threadPack.tracker->cloudMutex);
threadPack.tracker->cloudSignal.wait(threadPack.tracker->cloudMutex);
std::vector<CloudSlice *> * trackerSlices = &threadPack.tracker->getCloudSlices();
numClouds = trackerSlices->size();
cycledMutex = threadPack.tracker->cycledMutex;
if(cycledMutex)
{
threadPack.tracker->cycledMutex = false;
}
if(threadPack.cloudSlices.size() == 0)
{
uint64_t initTime = threadPack.tracker->init_utime.getValue();
if(initTime == std::numeric_limits<unsigned long long>::max())
{
return true;
}
Eigen::Matrix<float, 3, 3, Eigen::RowMajor> lastRotation = threadPack.tracker->getLastRotation();
Eigen::Vector3f lastTranslation = threadPack.tracker->getLastTranslation();
threadPack.cloudSlices.push_back(new CloudSlice(new pcl::PointCloud<pcl::PointXYZRGB>(),
CloudSlice::FIRST,
CloudSlice::FAIL,
lastTranslation,
lastRotation,
initTime,
Stopwatch::getCurrentSystemTime(),
0,
0,
0,
0,
&threadPack.tracker->placeRecognitionBuffer[0]));
threadPack.cloudSlices.back()->processedCloud = new pcl::PointCloud<pcl::PointXYZRGBNormal>();
threadPack.latestPoseId.assignAndNotifyAll(threadPack.cloudSlices.size());
}
lock.unlock();
if(cycledMutex)
{
while(latestPushedCloud < numClouds)
{
trackerSlices->at(latestPushedCloud)->processedCloud = new pcl::PointCloud<pcl::PointXYZRGBNormal>();
if(trackerSlices->at(latestPushedCloud)->cloud->size() > 0)
{
TICK(threadIdentifier);
if(ConfigArgs::get().weightCull > 0)
{
pcl::PointCloud<pcl::PointXYZRGB> * tempCloud = new pcl::PointCloud<pcl::PointXYZRGB>();
for(unsigned int i = 0; i < trackerSlices->at(latestPushedCloud)->cloud->size(); i++)
{
if(trackerSlices->at(latestPushedCloud)->cloud->at(i).a >= ConfigArgs::get().weightCull)
{
tempCloud->push_back(trackerSlices->at(latestPushedCloud)->cloud->at(i));
}
}
trackerSlices->at(latestPushedCloud)->cloud->clear();
trackerSlices->at(latestPushedCloud)->cloud->insert(trackerSlices->at(latestPushedCloud)->cloud->end(), tempCloud->begin(), tempCloud->end());
delete tempCloud;
}
//This isn't a double check, after culling weights the cloud might be empty :(
if(trackerSlices->at(latestPushedCloud)->cloud->size() > 0)
{
pcl::VoxelGrid<pcl::PointXYZRGB> sor;
sor.setInputCloud(trackerSlices->at(latestPushedCloud)->cloud->makeShared());
const float3 & voxelSizeMeters = Volume::get().getVoxelSizeMeters();
float leafSize = std::max(voxelSizeMeters.x,
std::max(voxelSizeMeters.y,
voxelSizeMeters.z));
sor.setLeafSize(leafSize, leafSize, leafSize);
pcl::PointCloud<pcl::PointXYZRGB> * tempCloud = new pcl::PointCloud<pcl::PointXYZRGB>();
sor.filter(*tempCloud);
trackerSlices->at(latestPushedCloud)->cloud->clear();
trackerSlices->at(latestPushedCloud)->cloud->insert(trackerSlices->at(latestPushedCloud)->cloud->end(), tempCloud->begin(), tempCloud->end());
delete tempCloud;
pcl::NormalEstimation<pcl::PointXYZRGB, pcl::Normal> n;
pcl::PointCloud<pcl::Normal>::Ptr normals (new pcl::PointCloud<pcl::Normal>);
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB>);
tree->setInputCloud(trackerSlices->at(latestPushedCloud)->cloud->makeShared());
n.setInputCloud(trackerSlices->at(latestPushedCloud)->cloud->makeShared());
n.setSearchMethod(tree);
n.setKSearch(20);
n.compute(*normals);
pcl::concatenateFields(*trackerSlices->at(latestPushedCloud)->cloud, *normals, *trackerSlices->at(latestPushedCloud)->processedCloud);
TOCK(threadIdentifier);
}
}
threadPack.cloudSlices.push_back(trackerSlices->at(latestPushedCloud));
threadPack.latestPoseId.assignAndNotifyAll(threadPack.cloudSlices.size());
latestPushedCloud++;
}
}
if(latestPushedCloud)
{
lagTime.assignValue(Stopwatch::getCurrentSystemTime() - trackerSlices->at(latestPushedCloud - 1)->lagTime);
}
if(threadPack.cloudSlices.size() && threadPack.cloudSlices.back()->dimension == CloudSlice::FINAL)
{
threadPack.cloudSliceProcessorFinished.assignAndNotifyAll(true);
lagTime.assignValue(0);
return false;
}
return true;
}
void CloudSliceProcessor::save()
{
assert(threadPack.finalised.getValue() && threadPack.cloudSlices.size() > 1);
pcl::PointCloud<pcl::PointXYZRGBNormal> * fullCloud = new pcl::PointCloud<pcl::PointXYZRGBNormal>;
int latestPoseIdCopy = threadPack.latestPoseId.getValue();
fullCloud->height = threadPack.cloudSlices.at(1)->processedCloud->height;
fullCloud->width = threadPack.cloudSlices.at(1)->processedCloud->width;
for(int i = 1; i < latestPoseIdCopy; i++)
{
fullCloud->insert(fullCloud->end(), threadPack.cloudSlices.at(i)->processedCloud->begin(), threadPack.cloudSlices.at(i)->processedCloud->end());
}
if(ConfigArgs::get().extractOverlap && !ConfigArgs::get().saveOverlap)
{
pcl::VoxelGrid<pcl::PointXYZRGBNormal> sor;
sor.setInputCloud (fullCloud->makeShared());
const float3 & voxelSizeMeters = Volume::get().getVoxelSizeMeters();
float leafSize = std::max(voxelSizeMeters.x,
std::max(voxelSizeMeters.y,
voxelSizeMeters.z));
sor.setLeafSize(leafSize, leafSize, leafSize);
pcl::PointCloud<pcl::PointXYZRGBNormal> * tempCloud = new pcl::PointCloud<pcl::PointXYZRGBNormal>();
sor.filter(*tempCloud);
fullCloud->clear();
fullCloud->insert(fullCloud->end(), tempCloud->begin(), tempCloud->end());
delete tempCloud;
}
std::cout << "Saving " << fullCloud->size() << " points... ";
std::cout.flush();
std::string filePCD = ConfigArgs::get().saveFile;
filePCD.append(".pcd");
pcl::io::savePCDFile(filePCD, *fullCloud, true);
std::cout << "PCD saved" << std::endl;
delete fullCloud;
return;
}