-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDataSource.hpp
More file actions
176 lines (145 loc) · 6.08 KB
/
Copy pathDataSource.hpp
File metadata and controls
176 lines (145 loc) · 6.08 KB
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
#ifndef PCL_GPU_FEATURES_TEST_DATA_SORUCE_HPP_
#define PCL_GPU_FEATURES_TEST_DATA_SORUCE_HPP_
#include<string>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include <pcl/common/common.h>
#include <pcl/features/normal_3d.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/gpu/containers/kernel_containers.h>
#include <pcl/search/search.h>
#include <Eigen/StdVector>
#if defined (_WIN32) || defined(_WIN64)
EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(pcl::PointXYZ)
EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(pcl::Normal)
#endif
#include <algorithm>
namespace pcl
{
namespace gpu
{
struct DataSource
{
const static int k = 32;
const static int max_elements = 500;
PointCloud<PointXYZ>::Ptr cloud;
PointCloud<PointXYZ>::Ptr surface;
IndicesPtr indices;
PointCloud<Normal>::Ptr normals;
PointCloud<Normal>::Ptr normals_surface;
float radius;
std::vector< std::vector<int> > neighbors_all;
std::vector<int> sizes;
int max_nn_size;
DataSource(const std::string& file = "d:/office_chair_model.pcd")
: cloud(new PointCloud<PointXYZ>()), surface(new PointCloud<PointXYZ>()), indices( new std::vector<int>() ),
normals(new PointCloud<Normal>()), normals_surface(new PointCloud<Normal>())
{
PCDReader pcd;
pcd.read(file, *cloud);
PointXYZ minp, maxp;
pcl::getMinMax3D(*cloud, minp, maxp);
float sz = (maxp.x - minp.x + maxp.y - minp.y + maxp.z - minp.z) / 3;
radius = sz / 15;
}
void generateColor()
{
size_t cloud_size = cloud->points.size();
for(size_t i = 0; i < cloud_size; ++i)
{
PointXYZ& p = cloud->points[i];
int r = std::max(1, std::min(255, static_cast<int>((double(rand())/RAND_MAX)*255)));
int g = std::max(1, std::min(255, static_cast<int>((double(rand())/RAND_MAX)*255)));
int b = std::max(1, std::min(255, static_cast<int>((double(rand())/RAND_MAX)*255)));
*reinterpret_cast<int*>(&p.data[3]) = (b << 16) + (g << 8) + r;
}
}
void estimateNormals()
{
pcl::NormalEstimation<PointXYZ, Normal> ne;
ne.setInputCloud (cloud);
ne.setSearchMethod (pcl::search::KdTree<PointXYZ>::Ptr (new pcl::search::KdTree<PointXYZ>));
ne.setKSearch (k);
//ne.setRadiusSearch (radius);
ne.compute (*normals);
}
void runCloudViewer() const
{
pcl::visualization::CloudViewer viewer ("Simple Cloud Viewer");
viewer.showCloud (cloud);
while (!viewer.wasStopped ()) {}
}
void findKNNeghbors()
{
KdTreeFLANN<PointXYZ>::Ptr kdtree(new KdTreeFLANN<PointXYZ>);
kdtree->setInputCloud(cloud);
size_t cloud_size = cloud->points.size();
std::vector<float> dists;
neighbors_all.resize(cloud_size);
for(size_t i = 0; i < cloud_size; ++i)
{
kdtree->nearestKSearch(cloud->points[i], k, neighbors_all[i], dists);
sizes.push_back((int)neighbors_all[i].size());
}
max_nn_size = *max_element(sizes.begin(), sizes.end());
}
void findRadiusNeghbors(float radius = -1)
{
radius = radius == -1 ? this->radius : radius;
KdTreeFLANN<PointXYZ>::Ptr kdtree(new KdTreeFLANN<PointXYZ>);
kdtree->setInputCloud(cloud);
size_t cloud_size = cloud->points.size();
std::vector<float> dists;
neighbors_all.resize(cloud_size);
for(size_t i = 0; i < cloud_size; ++i)
{
kdtree->radiusSearch(cloud->points[i], radius, neighbors_all[i], dists);
sizes.push_back((int)neighbors_all[i].size());
}
max_nn_size = *max_element(sizes.begin(), sizes.end());
}
void getNeghborsArray(std::vector<int>& data)
{
data.resize(max_nn_size * neighbors_all.size());
pcl::gpu::PtrStep<int> ps(&data[0], max_nn_size * sizeof(int));
for(size_t i = 0; i < neighbors_all.size(); ++i)
copy(neighbors_all[i].begin(), neighbors_all[i].end(), ps.ptr(i));
}
void generateSurface()
{
surface->points.clear();
for(size_t i = 0; i < cloud->points.size(); i+= 10)
surface->points.push_back(cloud->points[i]);
surface->width = surface->points.size();
surface->height = 1;
if (!normals->points.empty())
{
normals_surface->points.clear();
for(size_t i = 0; i < normals->points.size(); i+= 10)
normals_surface->points.push_back(normals->points[i]);
normals_surface->width = surface->points.size();
normals_surface->height = 1;
}
}
void generateIndices(size_t step = 100)
{
indices->clear();
for(size_t i = 0; i < cloud->points.size(); i += step)
indices->push_back(i);
}
struct Normal2PointXYZ
{
PointXYZ operator()(const Normal& n) const
{
PointXYZ xyz;
xyz.x = n.normal[0];
xyz.y = n.normal[1];
xyz.z = n.normal[2];
return xyz;
}
};
};
}
}
#endif /* PCL_GPU_FEATURES_TEST_DATA_SORUCE_HPP_ */