MEPP2 Project
cloud.inl
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 
12 // -------------------------------------------------------------------------------------
13 // Iterative weighted PCA for robust and edge-aware normal vector estimation
14 //--------------------------------------------------------------------------------------
15 // Julia Sanchez, Florence Denis, David Coeurjolly, Florent dupont, Laurent Trassoudaine, Paul Checchin
16 // Liris (Lyon), Institut Pascal (Clermont Ferrand)
17 // Région Auvergne Rhône Alpes ARC6
18 // Private use for reviewers only
19 // --------------------------------------------------------------------------------------
20 
21 
22 #if defined _MSC_VER
23 #pragma warning(push)
24 #pragma warning(disable: 4267 4244)
25 #endif
26 
27 #include <pcl/search/impl/search.hpp>
28 #include <pcl/features/normal_3d.h>
29 #include <pcl/kdtree/kdtree_flann.h>
30 
31 // Reads file and puts it into pointcloud_
32 inline int RawCloud::ReadCSV(std::string filepath)
33 {
34  int n = 0;
35  vector<Eigen::Vector3f> res;
36  std::cout<<"reading file : "<<filepath<<std::endl<<std::endl;
37  std::ifstream fin(filepath);
38  if (fin.is_open())
39  {
40  string test;
41  std::getline ( fin, test, '\n' );
42 
43  int count = 0;
44  for(size_t i =0; i<test.size(); ++i)
45  {
46  if(test[i]==',')
47  ++count;
48  }
49 
50  if(count==2)
51  {
52  while (std::getline ( fin, test, ',' ))
53  {
54  Eigen::Vector3f pt;
55  pt(0) = stof(test);
56  std::getline ( fin, test, ',' );
57  pt(1) = stof(test);
58  std::getline ( fin, test, '\n' );
59  pt(2) = stof(test);
60 
61  res.push_back(pt);
62  ++n;
63  }
64  }
65  else if(count==5)
66  {
67  while (std::getline ( fin, test, ',' ))
68  {
69  Eigen::Vector3f pt;
70  pt(0) = stof(test);
71  std::getline ( fin, test, ',' );
72  pt(1) = stof(test);
73  std::getline ( fin, test, ',' );
74  pt(2) = stof(test);
75  std::getline ( fin, test, '\n' );
76 
77  res.push_back(pt);
78  ++n;
79  }
80  }
81  fin.close();
82  pointcloud_ = new Eigen::Matrix<float, Eigen::Dynamic, 3>(res.size(), 3);
83  for(size_t i = 0; i<res.size(); ++i)
84  pointcloud_->row(i) = res[i];
85  }
86  else
87  {
88  std::cout<<"did not find file"<<std::endl<<std::endl;
89  }
90  std::cout<<"number of points : "<<n<<std::endl<<std::endl;
91  return n;
92 }
93 
94 //builds tree to extract neighbors
95 inline void RawCloud::buildTree()
96 {
97  int dim = pointcloud_->cols();
98  int pc_size = pointcloud_->rows();
99 
100  std::vector<float> pc(pc_size * dim);
101  flann::Matrix<float> flann_mat(&pc[0], pc_size, dim);
102 
103  int n = 0;
104 
105  for (int i =0; i < pc_size; ++i)
106  {
107  for (int j =0; j < dim; ++j)
108  {
109  pc[n] = (*pointcloud_)(i,j);
110  ++n;
111  }
112  }
113 
114  tree_ = new flann::Index<flann::L2<float>>(flann_mat, flann::KDTreeSingleIndexParams(15));
115  tree_->buildIndex();
116 }
117 
118 //computes resolution as mean distance between nearest neighbors
119 inline float RawCloud::getResolution ()
120 {
121  float res = 0.0;
122 
123  for (int i = 0; i<pointcloud_->rows(); ++i)
124  res += getNearestNeighborDistance(pointcloud_->row(i));
125 
126  res /= pointcloud_->rows();
127  return res;
128 }
129 
130 inline void RawCloud::rescale ()
131 {
132  float resolution = getResolution();
133  std::cout<<"current resolution : "<<resolution<<std::endl<<std::endl;
134  *pointcloud_ *= (float)(0.001)/resolution;
135 }
136 
137 // Gets first neighbor distance
138 inline float RawCloud::getNearestNeighborDistance(Eigen::Vector3f pt)
139 {
140  std::vector<float> dis;
141  std::vector<int> neigh;
142 
143  SearchFLANNTree(tree_, pt, neigh, dis, 3);
144  if(dis[1] != 0)
145  return sqrt(dis[1]);
146  else
147  return sqrt(dis[2]);
148 }
149 
150 
151 //Search function in the tree to get the nearest neighbors
152 inline void RawCloud::SearchFLANNTree(flann::Index<flann::L2<float>>* index,
153  Eigen::Vector3f& input,
154  std::vector<int>& indices,
155  std::vector<float>& dists,
156  int nn)
157 {
158  int dim = input.size();
159 
160  std::vector<float> query;
161  query.resize(dim);
162  for (int i = 0; i < dim; i++)
163  query[i] = input(i);
164  flann::Matrix<float> query_mat(&query[0], 1, dim);
165 
166  indices.resize(nn);
167  dists.resize(nn);
168  flann::Matrix<int> indices_mat(&indices[0], 1, nn);
169  flann::Matrix<float> dists_mat(&dists[0], 1, nn);
170 
171  index->knnSearch(query_mat, indices_mat, dists_mat, nn, flann::SearchParams(128));
172 }
173 
174 
175 #if defined _MSC_VER
176 #pragma warning(pop)
177 #endif
RawCloud::pointcloud_
Eigen::Matrix< float, Eigen::Dynamic, 3 > * pointcloud_
Definition: cloud.h:55
RawCloud::getNearestNeighborDistance
float getNearestNeighborDistance(Eigen::Vector3f pt)
Definition: cloud.inl:138
RawCloud::tree_
flann::Index< flann::L2< float > > * tree_
Definition: cloud.h:60
RawCloud::getResolution
float getResolution()
Definition: cloud.inl:119
RawCloud::buildTree
void buildTree()
Definition: cloud.inl:95
RawCloud::rescale
void rescale()
Definition: cloud.inl:130
RawCloud::SearchFLANNTree
void SearchFLANNTree(flann::Index< flann::L2< float >> *index, Eigen::Vector3f &input, std::vector< int > &indices, std::vector< float > &dists, int nn)
Definition: cloud.inl:152
RawCloud::ReadCSV
int ReadCSV(std::string filepath)
Definition: cloud.inl:32