Point Cloud Library (PCL)  1.11.1-dev
data_source.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * Author: Anatoly Baskeheev, Itseez Ltd, (myname.mysurname@mycompany.com)
35  */
36 
37 
38 #ifndef PCL_GPU_FEATURES_TEST_DATA_SOURCE_HPP_
39 #define PCL_GPU_FEATURES_TEST_DATA_SOURCE_HPP_
40 
41 #include<string>
42 
43 #include <pcl/point_types.h>
44 #include <pcl/point_cloud.h>
45 #include <pcl/io/pcd_io.h>
46 #include <pcl/common/common.h>
47 #include <pcl/features/normal_3d.h>
48 #include <pcl/visualization/cloud_viewer.h>
49 #include <pcl/gpu/containers/kernel_containers.h>
50 #include <pcl/search/search.h>
51 
52 #include <Eigen/StdVector>
53 
54 #if defined (_WIN32) || defined(_WIN64)
55  EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(pcl::PointXYZ)
56  EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(pcl::Normal)
57 #endif
58 
59 #include <algorithm>
60 
61 namespace pcl
62 {
63  namespace gpu
64  {
65  struct DataSource
66  {
67  const static int k = 32;
68  const static int max_elements = 500;
69 
73 
76  float radius;
77 
78  std::vector< std::vector<int> > neighbors_all;
79  std::vector<int> sizes;
81 
82  DataSource(const std::string& file = "d:/office_chair_model.pcd")
83  : cloud(new PointCloud<PointXYZ>()), surface(new PointCloud<PointXYZ>()), indices( new std::vector<int>() ),
85  {
86  PCDReader pcd;
87  pcd.read(file, *cloud);
88 
89  PointXYZ minp, maxp;
90  pcl::getMinMax3D(*cloud, minp, maxp);
91  float sz = (maxp.x - minp.x + maxp.y - minp.y + maxp.z - minp.z) / 3;
92  radius = sz / 15;
93  }
94 
96  {
97  for (auto& p: *cloud)
98  {
99  int r = std::max(1, std::min(255, static_cast<int>((double(rand())/RAND_MAX)*255)));
100  int g = std::max(1, std::min(255, static_cast<int>((double(rand())/RAND_MAX)*255)));
101  int b = std::max(1, std::min(255, static_cast<int>((double(rand())/RAND_MAX)*255)));
102 
103  *reinterpret_cast<int*>(&p.data[3]) = (b << 16) + (g << 8) + r;
104  }
105  }
106 
108  {
110  ne.setInputCloud (cloud);
112  ne.setKSearch (k);
113  //ne.setRadiusSearch (radius);
114 
115  ne.compute (*normals);
116  }
117 
118  void runCloudViewer() const
119  {
120  pcl::visualization::CloudViewer viewer ("Simple Cloud Viewer");
121  viewer.showCloud (cloud);
122  while (!viewer.wasStopped ()) {}
123  }
124 
126  {
128  kdtree->setInputCloud(cloud);
129 
130  const auto cloud_size = cloud->size();
131 
132  std::vector<float> dists;
133  neighbors_all.resize(cloud_size);
134  for(std::size_t i = 0; i < cloud_size; ++i)
135  {
136  kdtree->nearestKSearch((*cloud)[i], k, neighbors_all[i], dists);
137  sizes.push_back((int)neighbors_all[i].size());
138  }
139  max_nn_size = *max_element(sizes.begin(), sizes.end());
140  }
141 
142  void findRadiusNeghbors(float radius = -1)
143  {
144  radius = radius == -1 ? this->radius : radius;
145 
147  kdtree->setInputCloud(cloud);
148 
149  const auto cloud_size = cloud->size();
150 
151  std::vector<float> dists;
152  neighbors_all.resize(cloud_size);
153  for(std::size_t i = 0; i < cloud_size; ++i)
154  {
155  kdtree->radiusSearch((*cloud)[i], radius, neighbors_all[i], dists);
156  sizes.push_back((int)neighbors_all[i].size());
157  }
158  max_nn_size = *max_element(sizes.begin(), sizes.end());
159  }
160 
161  void getNeghborsArray(std::vector<int>& data)
162  {
163  data.resize(max_nn_size * neighbors_all.size());
164  pcl::gpu::PtrStep<int> ps(&data[0], max_nn_size * sizeof(int));
165  for(std::size_t i = 0; i < neighbors_all.size(); ++i)
166  copy(neighbors_all[i].begin(), neighbors_all[i].end(), ps.ptr(i));
167  }
168 
170  {
171  surface->clear();
172  for(std::size_t i = 0; i < cloud->size(); i+= 10)
173  surface->push_back((*cloud)[i]);
174  surface->width = surface->size();
175  surface->height = 1;
176 
177  if (!normals->empty())
178  {
179  normals_surface->clear();
180  for(std::size_t i = 0; i < normals->size(); i+= 10)
181  normals_surface->push_back((*normals)[i]);
182 
183  normals_surface->width = surface->size();
184  normals_surface->height = 1;
185  }
186  }
187 
188  void generateIndices(std::size_t step = 100)
189  {
190  indices->clear();
191  for(std::size_t i = 0; i < cloud->size(); i += step)
192  indices->push_back(i);
193  }
194 
196  {
197  PointXYZ operator()(const Normal& n) const
198  {
199  PointXYZ xyz;
200  xyz.x = n.normal[0];
201  xyz.y = n.normal[1];
202  xyz.z = n.normal[2];
203  return xyz;
204  }
205  };
206  };
207  }
208 }
209 
210 #endif /* PCL_GPU_FEATURES_TEST_DATA_SOURCE_HPP_ */
pcl
Definition: convolution.h:46
point_types.h
pcl::Normal
A point structure representing normal coordinates and the surface curvature estimate.
Definition: point_types.hpp:812
pcl::NormalEstimation
NormalEstimation estimates local surface properties (surface normals and curvatures)at each 3D point.
Definition: normal_3d.h:243
pcl::IndicesPtr
shared_ptr< Indices > IndicesPtr
Definition: pcl_base.h:58
common.h
pcl::gpu::DataSource
Definition: data_source.hpp:65
pcl::visualization::CloudViewer
Simple point cloud visualization class.
Definition: cloud_viewer.h:52
pcl::gpu::DataSource::normals
PointCloud< Normal >::Ptr normals
Definition: data_source.hpp:74
pcl::gpu::DataSource::sizes
std::vector< int > sizes
Definition: data_source.hpp:79
pcl::gpu::DataSource::DataSource
DataSource(const std::string &file="d:/office_chair_model.pcd")
Definition: data_source.hpp:82
pcl::gpu::DataSource::cloud
PointCloud< PointXYZ >::Ptr cloud
Definition: data_source.hpp:70
pcl::gpu::DataSource::getNeghborsArray
void getNeghborsArray(std::vector< int > &data)
Definition: data_source.hpp:161
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: distances.h:55
pcl::search::KdTree::Ptr
shared_ptr< KdTree< PointT, Tree > > Ptr
Definition: kdtree.h:75
pcl::gpu::DataSource::neighbors_all
std::vector< std::vector< int > > neighbors_all
Definition: data_source.hpp:78
pcl::gpu::DataSource::estimateNormals
void estimateNormals()
Definition: data_source.hpp:107
pcl::NormalEstimation::setInputCloud
void setInputCloud(const PointCloudConstPtr &cloud) override
Provide a pointer to the input dataset.
Definition: normal_3d.h:332
pcl::gpu::DataSource::runCloudViewer
void runCloudViewer() const
Definition: data_source.hpp:118
pcl::KdTreeFLANN
KdTreeFLANN is a generic type of 3D spatial locator using kD-tree structures.
Definition: kdtree_flann.h:64
pcl::gpu::PtrStep::ptr
__PCL_GPU_HOST_DEVICE__ T * ptr(int y=0)
Definition: kernel_containers.h:82
pcl::PointXYZ
A point structure representing Euclidean xyz coordinates.
Definition: point_types.hpp:300
pcl::search::KdTree
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
Definition: kdtree.h:61
pcl::Feature::compute
void compute(PointCloudOut &output)
Base method for feature estimation for all points given in <setInputCloud (), setIndices ()> using th...
Definition: feature.hpp:194
pcl::KdTreeFLANN::radiusSearch
int radiusSearch(const PointT &point, double radius, std::vector< int > &k_indices, std::vector< float > &k_sqr_distances, unsigned int max_nn=0) const override
Search for all the nearest neighbors of the query point in a given radius.
Definition: kdtree_flann.hpp:169
pcl::gpu::DataSource::normals_surface
PointCloud< Normal >::Ptr normals_surface
Definition: data_source.hpp:75
pcl::Feature::setKSearch
void setKSearch(int k)
Set the number of k nearest neighbors to use for the feature estimation.
Definition: feature.h:187
pcl::visualization::CloudViewer::wasStopped
bool wasStopped(int millis_to_wait=1)
Check if the gui was quit, you should quit also.
pcl::gpu::DataSource::max_elements
const static int max_elements
Definition: data_source.hpp:68
pcl::gpu::DataSource::generateSurface
void generateSurface()
Definition: data_source.hpp:169
pcl::gpu::DataSource::indices
IndicesPtr indices
Definition: data_source.hpp:72
pcl::gpu::DataSource::Normal2PointXYZ::operator()
PointXYZ operator()(const Normal &n) const
Definition: data_source.hpp:197
pcl::PointCloud::Ptr
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
pcl::getMinMax3D
void getMinMax3D(const pcl::PointCloud< PointT > &cloud, PointT &min_pt, PointT &max_pt)
Get the minimum and maximum values on each of the 3 (x-y-z) dimensions in a given pointcloud.
Definition: common.hpp:295
pcl::gpu::PtrStep
Definition: kernel_containers.h:74
pcl::gpu::DataSource::radius
float radius
Definition: data_source.hpp:76
pcl::gpu::DataSource::generateIndices
void generateIndices(std::size_t step=100)
Definition: data_source.hpp:188
pcl::gpu::DataSource::surface
PointCloud< PointXYZ >::Ptr surface
Definition: data_source.hpp:71
pcl::visualization::CloudViewer::showCloud
void showCloud(const ColorCloud::ConstPtr &cloud, const std::string &cloudname="cloud")
Show a cloud, with an optional key for multiple clouds.
pcl::PCDReader
Point Cloud Data (PCD) file format reader.
Definition: pcd_io.h:53
pcl::gpu::DataSource::generateColor
void generateColor()
Definition: data_source.hpp:95
pcl::gpu::DataSource::Normal2PointXYZ
Definition: data_source.hpp:195
pcl::gpu::DataSource::findRadiusNeghbors
void findRadiusNeghbors(float radius=-1)
Definition: data_source.hpp:142
pcl::PCDReader::read
int read(const std::string &file_name, pcl::PCLPointCloud2 &cloud, Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &pcd_version, const int offset=0) override
Read a point cloud data from a PCD file and store it into a pcl/PCLPointCloud2.
pcl::KdTreeFLANN::setInputCloud
void setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr()) override
Provide a pointer to the input dataset.
Definition: kdtree_flann.hpp:92
pcl::KdTreeFLANN::Ptr
shared_ptr< KdTreeFLANN< PointT, Dist > > Ptr
Definition: kdtree_flann.h:84
pcl::gpu::DataSource::max_nn_size
int max_nn_size
Definition: data_source.hpp:80
pcl::KdTreeFLANN::nearestKSearch
int nearestKSearch(const PointT &point, int k, std::vector< int > &k_indices, std::vector< float > &k_sqr_distances) const override
Search for k-nearest neighbors for the given query point.
Definition: kdtree_flann.hpp:132
pcl::gpu::DataSource::findKNNeghbors
void findKNNeghbors()
Definition: data_source.hpp:125
pcl::Feature::setSearchMethod
void setSearchMethod(const KdTreePtr &tree)
Provide a pointer to the search object.
Definition: feature.h:167
pcl::gpu::DataSource::k
const static int k
Definition: data_source.hpp:67