-
Notifications
You must be signed in to change notification settings - Fork 0
/
pclUtil.hpp
309 lines (248 loc) · 7.72 KB
/
pclUtil.hpp
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
#ifndef __PCL_UTIL_HPP__
#define __PCL_UTIL_HPP__
//-- PCL --//
#include <pcl/ModelCoefficients.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/io/pcd_io.h>
#include <pcl/features/normal_3d.h>
#include <pcl/filters/crop_box.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/search/search.h>
#include <pcl/registration/icp.h>
#include <pcl/visualization/cloud_viewer.h>
namespace imaiUtil
{
#define MACRO_USE_PCL_UTIL \
std::vector<imaiUtil::CoordinatedObjectClass*> imaiUtil::CoordinatedObjectClass::vct_cobjAll;
class customPCLVisualizer : public pcl::visualization::PCLVisualizer
{
};
class customPCLVisualizerInteractorStyle : public pcl::visualization::PCLVisualizerInteractorStyle
{
public:
customPCLVisualizerInteractorStyle();
int key;
char* keySym;
protected:
//-- Override Inherit Functions --//
//virtual void OnChar();
// Keyboard events
virtual void OnKeyDown ();
//virtual void OnKeyUp ();
};
class CoordinatedObjectClass
{
//-- properties --//
public:
pcl::PointCloud<pcl::PointXYZRGB>::Ptr pc;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr pc_local;
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> colorHandler;
std::string strID;
int pointPix;
Eigen::Affine3f tf;
static std::vector<imaiUtil::CoordinatedObjectClass *> vct_cobjAll;
private:
pcl::visualization::PCLVisualizer* int_viewer;
int* int_viewPort;
bool flag_drawFirstTime;
//-- methods --//
public:
CoordinatedObjectClass( std::string stringID = "_" );
~CoordinatedObjectClass();
void draw(pcl::visualization::PCLVisualizer& viewer, int& viewPort);
void updateDrawing();
void updateDrawing_withLocalPC();
void updateDrawing_withTransformedLocalPC();
};
#ifndef __CoordinatedCropBoxClass
#define __CoordinatedCropBoxClass
template<class T_point>
class CoordinatedCropBoxClass : public pcl::CropBox<T_point>
{
public:
pcl::ModelCoefficients coeff;
Eigen::Affine3f tf;
std::string str_id;
private:
pcl::visualization::PCLVisualizer* int_viewer;
int* int_viewPort;
public:
CoordinatedCropBoxClass();
void setCropBoxParam(
Eigen::Vector3f translation,
Eigen::Vector3f roteVec,
Eigen::Vector3f size
);
void updateTf( Eigen::Affine3f* p_tfIn = (Eigen::Affine3f*)NULL );
/*
void setCropBoxParam(
Eigen::Vector3f translation,
Eigen::Vector3f roteVec ,
Eigen::Vector3f size
);
*/
//void setCropBoxParam_fromCoeff();
void draw(
pcl::visualization::PCLVisualizer& viewer,
int& viewPort
);
void updateDrawing();
};
template<class T_point>
imaiUtil::CoordinatedCropBoxClass<T_point>::CoordinatedCropBoxClass()
: pcl::CropBox<T_point>()
{}
template<class T_point>
void imaiUtil::CoordinatedCropBoxClass<T_point>::setCropBoxParam(
Eigen::Vector3f translation,
Eigen::Vector3f roteVec,
Eigen::Vector3f size
)
{
// pcl::ModelCoefficients box;
// box.values[0] = 0 ; box.values[1] = 0 ; box.values[2] = 0 ; // translate
// box.values[3] = 0 ; box.values[4] = 0 ; box.values[5] = 0 ; box.values[6] = 0; // rotation (quaternion)
// box.values[7] = 0.2; box.values[8] = 0.2; box.values[9] = 0.2; // size
// make quaternion from roteMat
Eigen::Quaternionf quat(
Eigen::AngleAxisf( roteVec.x(), Eigen::Vector3f::UnitX() ) *
Eigen::AngleAxisf( roteVec.y(), Eigen::Vector3f::UnitY() ) *
Eigen::AngleAxisf( roteVec.z(), Eigen::Vector3f::UnitZ() )
);
// init coeff for visual
coeff.values.resize(10);
coeff.values[0] = translation.x(); // x
coeff.values[1] = translation.y(); // y
coeff.values[2] = translation.z(); // z of inner translate
coeff.values[3] = quat.x(); // i
coeff.values[4] = quat.y(); // j
coeff.values[5] = quat.z(); // k
coeff.values[6] = quat.w(); // w of inner rotation (quaternion)
coeff.values[7] = size.x();
coeff.values[8] = size.y();
coeff.values[9] = size.z(); // size
// set box param
this->setMax( Eigen::Vector4f( size.x()/2.0, size.y()/2.0, size.z()/2.0, 1.0f ) );
this->setMin( Eigen::Vector4f(-size.x()/2.0, -size.y()/2.0, -size.z()/2.0, 1.0f ) );
this->setTranslation(translation);
this->setRotation(roteVec);
}
template<class T_point>
void imaiUtil::CoordinatedCropBoxClass<T_point>::updateTf( Eigen::Affine3f* p_tfIn )
{
if( p_tfIn != NULL )
this->tf = *p_tfIn;
//-- set filter param --//
// this function set transform for inputCloud before filtering.
// so neither coefficents for visual nor box param are not influenced.
this->setTransform( this->tf.inverse() );
}
template<class T_point>
void imaiUtil::CoordinatedCropBoxClass<T_point>::draw(
pcl::visualization::PCLVisualizer& viewer,
int& viewPort
)
{
this->int_viewer = &viewer ;
this->int_viewPort = &viewPort;
viewer.addCube( this->coeff, this->str_id, *this->int_viewPort);
}
template<class T_point>
void imaiUtil::CoordinatedCropBoxClass<T_point>::updateDrawing()
{
this->updateTf();
//this->int_viewer->removeShape(this->str_id);
//this->int_viewer->addCube( this->coeff, this->str_id, *this->int_viewPort);
this->int_viewer->updateShapePose(this->str_id, this->tf);
}
#endif
class ChainedObjectBaseClass
{
public:
Eigen::Affine3f tf_self;
ChainedObjectBaseClass* parent;
std::vector<ChainedObjectBaseClass*> chirdren;
public:
ChainedObjectBaseClass():
parent(NULL)
{}
Eigen::Affine3f getTf_Base2Self()
{
if( this->parent == NULL )
return this->tf_self;
else
return this->tf_self * parent->getTf_Base2Self();
}
};
template< class T_point >
int ransac_plane(
boost::shared_ptr<pcl::PointCloud<T_point>> cloud_in,
pcl::PointCloud<T_point>& cloud_out_inliers,
pcl::PointCloud<T_point>& cloud_out_outliers,
pcl::ModelCoefficients::Ptr coefficients,
bool flag_coloring4cloudIn = false
)
{
//-- RANSAC segmentation --//
coefficients.reset( new pcl::ModelCoefficients );
pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
// Create the segmentation object
pcl::SACSegmentation<pcl::PointXYZRGB> objRANSAC;
// Optional
objRANSAC.setOptimizeCoefficients (true);
// Mandatory
objRANSAC.setModelType (pcl::SACMODEL_PLANE);
objRANSAC.setMethodType (pcl::SAC_RANSAC);
objRANSAC.setDistanceThreshold (0.01);
objRANSAC.setInputCloud ( cloud_in );
// execute RANSAC segmentation
objRANSAC.segment (*inliers, *coefficients);
//-- Extruct inlier/outlier of detected plane --//
pcl::ExtractIndices<pcl::PointXYZRGB> extract;
extract.setInputCloud ( cloud_in );
extract.setIndices(inliers);
extract.setNegative( false );
extract.filter( cloud_out_inliers);
extract.setNegative( true );
extract.filter( cloud_out_outliers);
//-- Coloring --//
if(flag_coloring4cloudIn)
{
int size_inliers = inliers->indices.size();
for(int i=0 ; i<size_inliers ; i++)
{
cloud_in->points[ inliers->indices[i] ].r = 255;
cloud_in->points[ inliers->indices[i] ].g = 127;
cloud_in->points[ inliers->indices[i] ].b = 127;
}
}
if (inliers->indices.size () == 0)
{
PCL_ERROR ("Could not estimate a planar model for the given dataset.");
return -1;
}
else
{
std::cerr << "Model coefficients: " << coefficients->values[0] << " "
<< coefficients->values[1] << " "
<< coefficients->values[2] << " "
<< coefficients->values[3] << std::endl;
return 0;
}
}
/*
template<class T_point>
void setCropBoxCoefficients(
pcl::CropBox<T_point>& cropBox ,
pcl::ModelCoefficients& box
);
*/
};
#endif