MEPP2 Project
Graph_properties_pcl_point_cloud.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as
6 // published by the Free Software Foundation; either version 3 of
7 // the License, or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 #pragma once
12 
14 #include <CGAL/boost/graph/properties.h> // for boost::vertex_point_t
15 
16 #include <stdexcept> // for std::runtime_error
17 
18 #include <pcl/kdtree/kdtree_flann.h>
19 
20 #include <utility> // for std::make_pair
21 #include <memory> // for std::unique_ptr
22 #include <cmath> // for std::sqrt
23 
24 
25 namespace FEVV {
26 
27 // --------------- Point map ---------------
28 
29 //note: get(boost::vertex_point_t,...) must return a shallow copy of the point
30 // map and NOT a reference, so we must create a real PCLPointCloudPointMap
31 // class that:
32 // - implement shallow copy
33 // - implement operator[]
34 // see
35 // https://github.com/CGAL/cgal/blob/ea20dfd63fcdec0b98258c9c47b0cbb88cdb356c/BGL/include/CGAL/boost/graph/properties_OpenMesh.h#L185
36 // and
37 // https://www.boost.org/doc/libs/1_57_0/libs/property_map/doc/property_map.html
38 // https://www.boost.org/doc/libs/1_57_0/libs/property_map/doc/LvaluePropertyMap.html
40 {
41 public:
46  typedef boost::read_write_property_map_tag category;
47 
49 
51  {
52  }
53 
55 
57  {
58  // 'points' attribute of pcl::PointCloud<>
59  // is a std::vector<FEVV::PCLEnrichedPoint>
60  const FEVV::PCLEnrichedPoint &enriched_point = m_pc->points[k];
61  return FEVV::PCLPoint(enriched_point.x,
62  enriched_point.y,
63  enriched_point.z);
64  }
65 
66  void set(key_type k, const FEVV::PCLPoint &point)
67  {
68  // 'points' attribute of pcl::PointCloud<>
69  // is a std::vector<FEVV::PCLEnrichedPoint>
70  FEVV::PCLEnrichedPoint &enriched_point = m_pc->points[k];
71  enriched_point.x = point[0];
72  enriched_point.y = point[1];
73  enriched_point.z = point[2];
74  }
75 
76 private:
77  FEVV::PCLPointCloud *m_pc; //TODO-elo replace by a reference
78 };
79 
80 } // namespace FEVV
81 
82 
83 namespace pcl {
84 
92 inline
94 get(const boost::vertex_point_t, FEVV::PCLPointCloud &pc)
95 {
97  return pm;
98 }
99 // const version for filters that take a const pc as parameter
100 inline
102 get(const boost::vertex_point_t, const FEVV::PCLPointCloud &pc)
103 {
104  FEVV::PCLPointCloudPointMap pm(const_cast< FEVV::PCLPointCloud& >(pc));
105  return pm;
106 }
107 
108 
109 } // namespace pcl
110 
111 
112 namespace FEVV {
113 
115 inline
119 {
120  return pm[key];
121 }
122 
124 inline
125 void
129 {
130  pm.set(key, value);
131 }
132 
133 // --------------- Normal map ---------------
134 
135 // implement a normal map with shallow copy
136 // normal is stored inside PCLEnrichedPoint structure
138 {
139 public:
144  typedef boost::read_write_property_map_tag category;
145 
146  PCLPointCloudNormalMap() : m_pc(nullptr) {}
147 
149 
150  // shallow copy
152  {
153  }
154 
156 
158  {
159  // 'points' attribute of pcl::PointCloud<>
160  // is a std::vector<FEVV::PCLEnrichedPoint>
161  if(m_pc)
162  {
163  const FEVV::PCLEnrichedPoint &enriched_point = m_pc->points[k];
164  return FEVV::PCLVector(enriched_point.normal_x,
165  enriched_point.normal_y,
166  enriched_point.normal_z);
167  }
168  else
169  {
170  throw std::runtime_error(
171  "FEVV::PCLPointCloudNormalMap: invalid use of un-initialized map");
172  return FEVV::PCLVector(0, 0, 0); // dummy return to avoid a warning
173  }
174  }
175 
176  void set(key_type k, const FEVV::PCLVector &normal)
177  {
178  // 'points' attribute of pcl::PointCloud<>
179  // is a std::vector<FEVV::PCLEnrichedPoint>
180  if(m_pc)
181  {
182  FEVV::PCLEnrichedPoint &enriched_point = m_pc->points[k];
183  enriched_point.normal_x = normal[0];
184  enriched_point.normal_y = normal[1];
185  enriched_point.normal_z = normal[2];
186  }
187  else
188  {
189  throw std::runtime_error("Invalid un-initialized PCLPointCloudNormalMap");
190  }
191  }
192 
193 private:
195 };
196 
198 inline
202 {
203  return nm[key];
204 }
205 
207 inline
208 void
212 {
213  nm.set(key, value);
214 }
215 
216 // --------------- Color map ---------------
217 
218 // implement a color map with shallow copy
219 // color is stored inside PCLEnrichedPoint structure
221 {
222 public:
227  typedef boost::read_write_property_map_tag category;
228 
229  PCLPointCloudColorMap() : m_pc(nullptr) {}
230 
232 
233  // shallow copy
235  {
236  }
237 
239 
241  {
242  // 'points' attribute of pcl::PointCloud<>
243  // is a std::vector<FEVV::PCLEnrichedPoint>
244  if(m_pc)
245  {
246  const FEVV::PCLEnrichedPoint &enriched_point = m_pc->points[k];
247  return FEVV::PCLColor(enriched_point.r,
248  enriched_point.g,
249  enriched_point.b);
250  }
251  else
252  {
253  throw std::runtime_error(
254  "FEVV::PCLPointCloudColorMap: invalid use of un-initialized map");
255  return FEVV::PCLColor(0, 0, 0); // dummy return to avoid a warning
256  }
257  }
258 
259  void set(key_type k, const FEVV::PCLColor &color)
260  {
261  // 'points' attribute of pcl::PointCloud<>
262  // is a std::vector<FEVV::PCLEnrichedPoint>
263  if(m_pc)
264  {
265  FEVV::PCLEnrichedPoint &enriched_point = m_pc->points[k];
266  enriched_point.r = color[0];
267  enriched_point.g = color[1];
268  enriched_point.b = color[2];
269  }
270  else
271  {
272  throw std::runtime_error("Invalid un-initialized PCLPointCloudNormalMap");
273  }
274  }
275 
276 private:
278 };
279 
281 inline
285 {
286  return cm[key];
287 }
288 
290 inline
291 void
295 {
296  cm.set(key, value);
297 }
298 
299 } // namespace FEVV
300 
301 
302 namespace boost {
303 
304 template<>
305 struct property_traits< FEVV::PCLPointCloudPointMap >
306 {
309 };
310 
311 } // namespace boost
312 
313 // --------------- Nearest neighbors search ---------------
314 
315 namespace pcl {
316 
317 // NN-search types
319 {
320  typedef pcl::KdTreeFLANN< FEVV::PCLEnrichedPoint > KdTree;
321  typedef std::unique_ptr< KdTree > KdTreePtr;
322 };
323 
324 
325 // MEPP2 PointCloud concept
329 inline
332 {
333  typedef PCLPointCloudNNSearch::KdTree KdTree;
334  typedef PCLPointCloudNNSearch::KdTreePtr KdTreePtr;
335 
336  // create k-d tree
337  KdTreePtr kd_tree(new KdTree);
338 
339  // copy all points in the tree
340  kd_tree->setInputCloud(pc.makeShared());
341  // note : cloud is passed as a shared pointer here
342  // so all data is copied twice:
343  // 1. whole point cloud copied to shared pointer
344  // due to makeShared()
345  // 2. all points X,Y,Z copied into kdtree by
346  // setInputCloud()
347 
348  return kd_tree;
349 }
350 
351 
352 // MEPP2 PointCloud concept
359 inline
360 std::pair< std::vector< typename boost::graph_traits<
362  std::vector< double > >
364  const PCLPointCloudNNSearch::KdTree &kd_tree,
365  unsigned int k,
366  const FEVV::PCLPoint &query,
367  const FEVV::PCLPointCloud &/*pc*/) // useless for PCL
368 {
371 
372  // prepare query
373  FEVV::PCLEnrichedPoint search_point;
374  search_point.x = query[0];
375  search_point.y = query[1];
376  search_point.z = query[2];
377 
378  // search K nearest neighbors
379  std::vector< int > nn_ids_tmp(k);
380  std::vector< float > nn_distances_tmp(k); // squared distance
381  int nn_nbr =
382  kd_tree.nearestKSearch(search_point, k, nn_ids_tmp, nn_distances_tmp);
383  // note : kd_tree.nearestKSearch() wants a std::vector< int > and a
384  // std::vector< float > as outputs, and nothing else ; the more
385  // generic std::vector< vertex_descriptor > and
386  // std::vector <double > trigger a compilation error as of PCL 1.9.1
387 
388  // note : should launch an exception if nn_nbr != k ?
389 
390  // convert ids and distances
391  std::vector< vertex_descriptor > nn_ids(nn_nbr);
392  std::vector< double > nn_distances(nn_nbr);
393  for(int i = 0; i < nn_nbr; i++)
394  {
395  nn_ids[i] = static_cast< vertex_descriptor >(nn_ids_tmp[i]);
396  nn_distances[i] = std::sqrt(nn_distances_tmp[i]);
397  }
398 
399  // return pair (vector_of_ids, vector_of_distances)
400  return make_pair(nn_ids, nn_distances);
401 }
402 
403 
404 // MEPP2 PointCloud concept
413 inline
414 std::pair< std::vector< typename boost::graph_traits<
416  std::vector< double > >
418  const PCLPointCloudNNSearch::KdTree &kd_tree,
419  double radius,
420  const FEVV::PCLPoint &query,
421  const FEVV::PCLPointCloud &/*pc*/) // useless for PCL
422 {
425 
426  // prepare query
427  FEVV::PCLEnrichedPoint search_point;
428  search_point.x = query[0];
429  search_point.y = query[1];
430  search_point.z = query[2];
431 
432  // search nearest neighbors
433  std::vector< int > nn_ids_tmp;
434  std::vector< float > nn_distances_tmp; // squared distance
435  int nn_nbr =
436  kd_tree.radiusSearch(search_point, radius, nn_ids_tmp, nn_distances_tmp);
437 
438  // convert ids and distances
439  std::vector< vertex_descriptor > nn_ids(nn_nbr);
440  std::vector< double > nn_distances(nn_nbr);
441  for(int i = 0; i < nn_nbr; i++)
442  {
443  nn_ids[i] = static_cast< vertex_descriptor >(nn_ids_tmp[i]);
444  nn_distances[i] = std::sqrt(nn_distances_tmp[i]);
445  }
446 
447  // return pair (vector_of_ids, vector_of_distances)
448  return make_pair(nn_ids, nn_distances);
449 }
450 
451 } // namespace pcl
FEVV::PCLPointCloudPointMap::category
boost::read_write_property_map_tag category
Definition: Graph_properties_pcl_point_cloud.h:46
pcl::create_kd_tree
PCLPointCloudNNSearch::KdTreePtr create_kd_tree(const FEVV::PCLPointCloud &pc)
Initialize a k-d tree with all cloud points.
Definition: Graph_properties_pcl_point_cloud.h:331
pcl
Definition: Graph_properties_pcl_point_cloud.h:83
boost::graph_traits< FEVV::PCLPointCloud >::vertex_descriptor
index_type vertex_descriptor
Definition: Graph_traits_pcl_point_cloud.h:46
Graph_traits_pcl_point_cloud.h
FEVV::PCLPointCloudColorMap::key_type
boost::graph_traits< FEVV::PCLPointCloud >::vertex_descriptor key_type
Definition: Graph_properties_pcl_point_cloud.h:226
pcl::radius_search
std::pair< std::vector< typename boost::graph_traits< FEVV::PCLPointCloud >::vertex_descriptor >, std::vector< double > > radius_search(const PCLPointCloudNNSearch::KdTree &kd_tree, double radius, const FEVV::PCLPoint &query, const FEVV::PCLPointCloud &)
Search for the nearest neighbors of a geometric point in the given radius.
Definition: Graph_properties_pcl_point_cloud.h:417
FEVV::PCLPointCloudNormalMap::PCLPointCloudNormalMap
PCLPointCloudNormalMap(FEVV::PCLPointCloud &pc)
Definition: Graph_properties_pcl_point_cloud.h:148
pcl::PCLPointCloudNNSearch::KdTree
pcl::KdTreeFLANN< FEVV::PCLEnrichedPoint > KdTree
Definition: Graph_properties_pcl_point_cloud.h:320
FEVV::PCLPointCloudPointMap::m_pc
FEVV::PCLPointCloud * m_pc
Definition: Graph_properties_pcl_point_cloud.h:77
FEVV::PCLPointCloudPointMap::PCLPointCloudPointMap
PCLPointCloudPointMap(const FEVV::PCLPointCloudPointMap &pm)
Definition: Graph_properties_pcl_point_cloud.h:50
FEVV::PCLPointCloudPointMap::operator[]
value_type operator[](key_type k) const
Definition: Graph_properties_pcl_point_cloud.h:56
FEVV::PCLPointCloudNormalMap::value_type
FEVV::PCLVector value_type
Definition: Graph_properties_pcl_point_cloud.h:140
FEVV::PCLPointCloudColorMap::PCLPointCloudColorMap
PCLPointCloudColorMap()
Definition: Graph_properties_pcl_point_cloud.h:229
FEVV::PCLPointCloud
pcl::PointCloud< PCLEnrichedPoint > PCLPointCloud
Definition: DataStructures_pcl_point_cloud.h:28
FEVV::PCLPointCloudPointMap::PCLPointCloudPointMap
PCLPointCloudPointMap(FEVV::PCLPointCloud &pc)
Definition: Graph_properties_pcl_point_cloud.h:48
FEVV::PCLPointCloudColorMap
Definition: Graph_properties_pcl_point_cloud.h:221
FEVV::PCLPointCloudPointMap::operator=
PCLPointCloudPointMap & operator=(const PCLPointCloudPointMap &)=default
FEVV::PCLPointCloudColorMap::category
boost::read_write_property_map_tag category
Definition: Graph_properties_pcl_point_cloud.h:227
boost
Definition: Graph_properties_aif.h:48
FEVV::PCLPointCloudPointMap::set
void set(key_type k, const FEVV::PCLPoint &point)
Definition: Graph_properties_pcl_point_cloud.h:66
FEVV::PCLPointCloudPointMap::value_type
FEVV::PCLPoint value_type
Definition: Graph_properties_pcl_point_cloud.h:42
FEVV::PCLPointCloudColorMap::operator=
PCLPointCloudColorMap & operator=(const PCLPointCloudColorMap &)=default
pcl::kNN_search
std::pair< std::vector< typename boost::graph_traits< FEVV::PCLPointCloud >::vertex_descriptor >, std::vector< double > > kNN_search(const PCLPointCloudNNSearch::KdTree &kd_tree, unsigned int k, const FEVV::PCLPoint &query, const FEVV::PCLPointCloud &)
Search the k nearest neighbors of a geometric point.
Definition: Graph_properties_pcl_point_cloud.h:363
FEVV::PCLPointCloudNormalMap::operator=
PCLPointCloudNormalMap & operator=(const PCLPointCloudNormalMap &)=default
FEVV::PCLPointCloudColorMap::value_type
FEVV::PCLColor value_type
Definition: Graph_properties_pcl_point_cloud.h:223
FEVV::PCLPointCloudNormalMap::reference
FEVV::PCLVector & reference
Definition: Graph_properties_pcl_point_cloud.h:141
FEVV::PCLPointCloudPointMap
Definition: Graph_properties_pcl_point_cloud.h:40
pcl::PCLPointCloudNNSearch
Definition: Graph_properties_pcl_point_cloud.h:319
FEVV::get
FEVV::PCLPointCloudPointMap::value_type get(const FEVV::PCLPointCloudPointMap &pm, FEVV::PCLPointCloudPointMap::key_type key)
Specialization of get(point_map, key) for PCLPointCloud.
Definition: Graph_properties_pcl_point_cloud.h:117
FEVV::PCLPointCloudColorMap::operator[]
value_type operator[](key_type k) const
Definition: Graph_properties_pcl_point_cloud.h:240
FEVV
Interfaces for plugins These interfaces will be used for different plugins.
Definition: Assert.h:16
FEVV::PCLPointCloudNormalMap::PCLPointCloudNormalMap
PCLPointCloudNormalMap(const FEVV::PCLPointCloudNormalMap &nm)
Definition: Graph_properties_pcl_point_cloud.h:151
FEVV::PCLPoint
Eigen::Vector3f PCLPoint
Definition: DataStructures_pcl_point_cloud.h:32
FEVV::PCLPointCloudNormalMap::set
void set(key_type k, const FEVV::PCLVector &normal)
Definition: Graph_properties_pcl_point_cloud.h:176
FEVV::PCLPointCloudColorMap::PCLPointCloudColorMap
PCLPointCloudColorMap(const FEVV::PCLPointCloudColorMap &cm)
Definition: Graph_properties_pcl_point_cloud.h:234
boost::property_traits< FEVV::PCLPointCloudPointMap >::value_type
FEVV::PCLPointCloudPointMap::value_type value_type
Definition: Graph_properties_pcl_point_cloud.h:307
FEVV::PCLPointCloudPointMap::reference
FEVV::PCLPoint & reference
Definition: Graph_properties_pcl_point_cloud.h:43
FEVV::PCLPointCloudNormalMap::operator[]
value_type operator[](key_type k) const
Definition: Graph_properties_pcl_point_cloud.h:157
FEVV::PCLPointCloudNormalMap
Definition: Graph_properties_pcl_point_cloud.h:138
FEVV::PCLPointCloudColorMap::set
void set(key_type k, const FEVV::PCLColor &color)
Definition: Graph_properties_pcl_point_cloud.h:259
FEVV::PCLPointCloudNormalMap::category
boost::read_write_property_map_tag category
Definition: Graph_properties_pcl_point_cloud.h:144
FEVV::PCLPointCloudColorMap::reference
FEVV::PCLColor & reference
Definition: Graph_properties_pcl_point_cloud.h:224
msdm2::vertex_descriptor
boost::graph_traits< MeshT >::vertex_descriptor vertex_descriptor
Definition: msdm2_surfacemesh.h:33
FEVV::PCLPointCloudNormalMap::m_pc
FEVV::PCLPointCloud * m_pc
Definition: Graph_properties_pcl_point_cloud.h:194
boost::property_traits< FEVV::PCLPointCloudPointMap >::reference
FEVV::PCLPointCloudPointMap::reference reference
Definition: Graph_properties_pcl_point_cloud.h:308
FEVV::put
void put(FEVV::PCLPointCloudPointMap &pm, FEVV::PCLPointCloudPointMap::key_type key, const FEVV::PCLPointCloudPointMap::value_type &value)
Specialization of put(point_map, key, value) for PCLPointCloud.
Definition: Graph_properties_pcl_point_cloud.h:126
FEVV::PCLPointCloudPointMap::key_type
boost::graph_traits< FEVV::PCLPointCloud >::vertex_descriptor key_type
Definition: Graph_properties_pcl_point_cloud.h:45
FEVV::PCLVector
Eigen::Vector3f PCLVector
Definition: DataStructures_pcl_point_cloud.h:33
FEVV::PCLColor
Eigen::Matrix< PCLColorType, 3, 1 > PCLColor
Definition: DataStructures_pcl_point_cloud.h:34
FEVV::PCLPointCloudNormalMap::key_type
boost::graph_traits< FEVV::PCLPointCloud >::vertex_descriptor key_type
Definition: Graph_properties_pcl_point_cloud.h:143
pcl::PCLPointCloudNNSearch::KdTreePtr
std::unique_ptr< KdTree > KdTreePtr
Definition: Graph_properties_pcl_point_cloud.h:321
FEVV::PCLPointCloudNormalMap::PCLPointCloudNormalMap
PCLPointCloudNormalMap()
Definition: Graph_properties_pcl_point_cloud.h:146
FEVV::PCLPointCloudColorMap::m_pc
FEVV::PCLPointCloud * m_pc
Definition: Graph_properties_pcl_point_cloud.h:277
FEVV::PCLPointCloudColorMap::PCLPointCloudColorMap
PCLPointCloudColorMap(FEVV::PCLPointCloud &pc)
Definition: Graph_properties_pcl_point_cloud.h:231
pcl::get
FEVV::PCLPointCloudPointMap get(const boost::vertex_point_t, FEVV::PCLPointCloud &pc)
Returns the points property map (aka the geometry) of the mesh.
Definition: Graph_properties_pcl_point_cloud.h:94
FEVV::PCLEnrichedPoint
pcl::PointXYZRGBNormal PCLEnrichedPoint
Definition: DataStructures_pcl_point_cloud.h:27