MEPP2 Project
core.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 
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 #include <math.h>
23 #include <algorithm>
24 #include <chrono>
25 #include <utility>
26 #include <vector>
27 
28 #include <Eigen/Core>
29 #include <Eigen/Dense>
30 #include <Eigen/Eigenvalues>
31 
32 #if defined _MSC_VER
33 #pragma warning(push)
34 #pragma warning(disable: 4267 4244)
35 #endif
36 
37 #include <flann/flann.hpp>
38 
39 #if defined _MSC_VER
40 #pragma warning(pop)
41 #endif
42 
43 using namespace std;
44 
45 //Constants with effect on result
46 const float mu_max2 = 0.35f; // initial mu when starting the optimization for selection 2
47 // increase if too many normals stuck in medium position/ decrease if too many face confusions when sampling anisotropy
48 const float div_fact = 1.01f;
49 
50 //Usual constants DO NOT MODIFY
51 const float epsilon = 1e-10f; // small value for initializations of comparisons
52 const float lim_error = 1e-5f; // to stop optimize when not moving
53 const float lim_diff = 1e-7f; // to stop optimize when not moving
54 
55 //Detail constants
56 const int itr_min = 5; // minimum number of iterations to perform
57 const float mu_max = 1.0; // coefficient to compute the initial mu when starting optimize for the first time (emax = er_max*mu_max)
58 const int itr_per_mu = 1; // number of iterations to perform for each mu
59 const float noise_min = 0.000001f; // minimum noise to make it not infinite
60 const float likelihood_threshold = 0.95f; // value for evaluation/comparison between vectors
61 const float min_points_fact = 0.1f; // limit number of points of the neighborhood which must have an impact on the normal computation
62 const float thresh_weight = 0.25; // threshold to select neighbors
63 
64 
65 class CApp{
66  public:
67  CApp()
68  {
69  std::cout<<"add one pointcloud + its kdtree + the reference point you want to compute the normal on"<<std::endl<<std::endl;
70  normalFirst0_ = NULL;
71  normalFirst1_ = NULL;
72  normalFirst2_ = NULL;
73  normalSecond0_ = NULL;
74  normalSecond1_ = NULL;
75  normalSecond2_ = NULL;
76  pointFirst_ = NULL;
77  pointSecond_ = NULL;
78  };
79 
80  CApp(Eigen::Matrix<float, Eigen::Dynamic, 3> *pc, flann::Index<flann::L2<float>>* tree, float noise)
81  {
82  setPc(pc);
83  setTree(tree);
84  noise_ = noise;
85  normalFirst0_ = NULL;
86  normalFirst1_ = NULL;
87  normalFirst2_ = NULL;
88  normalSecond0_ = NULL;
89  normalSecond1_ = NULL;
90  normalSecond2_ = NULL;
91  pointFirst_ = NULL;
92  pointSecond_ = NULL;
93  };
94 
95  CApp(Eigen::Matrix<float, Eigen::Dynamic, 3> *pc, flann::Index<flann::L2<float>>* tree, int ref, float noise)
96  {
97  setPc(pc);
98  setTree(tree);
99  setRef(ref);
100  noise_ = noise;
101  normalFirst0_ = NULL;
102  normalFirst1_ = NULL;
103  normalFirst2_ = NULL;
104  normalSecond0_ = NULL;
105  normalSecond1_ = NULL;
106  normalSecond2_ = NULL;
107  pointFirst_ = NULL;
108  pointSecond_ = NULL;
109  };
110 
111  CApp(float divFact, float limMu, float limMuPos, Eigen::Matrix<float, Eigen::Dynamic, 3> *pc, flann::Index<flann::L2<float>>* tree, int ref, float noise)
112  {
113  divFact_ = divFact;
114  limMu_ = limMu;
115  limMuPos_ = limMuPos;
116  noise_ = noise;
117  setPc(pc);
118  setTree(tree);
119  setRef(ref);
120  normalFirst0_ = NULL;
121  normalFirst1_ = NULL;
122  normalFirst2_ = NULL;
123  normalSecond0_ = NULL;
124  normalSecond1_ = NULL;
125  normalSecond2_ = NULL;
126  pointFirst_ = NULL;
127  pointSecond_ = NULL;
128 
129  };
130 
132  {
133  if(normalFirst0_!= NULL)
134  delete normalFirst0_;
135  if(normalFirst1_!= NULL)
136  delete normalFirst1_;
137  if(normalFirst2_!= NULL)
138  delete normalFirst2_;
139  if(normalSecond0_!= NULL)
140  delete normalSecond0_;
141  if(normalSecond1_!= NULL)
142  delete normalSecond1_;
143  if(normalSecond2_!= NULL)
144  delete normalSecond2_;
145  if(pointFirst_!= NULL)
146  delete pointFirst_;
147  if(pointSecond_!= NULL)
148  delete pointSecond_;
149  };
150 
151  void setTree(flann::Index<flann::L2<float>> *t){tree_ = t;}
152  void setPc(Eigen::Matrix<float, Eigen::Dynamic, 3> *pc){pointcloud_ = pc;};
153  void setRef (int ref);
154  void setNormal(Eigen::Vector3f norm){normal = norm;};
155  void setPoint(Eigen::Vector3f point);
156  void setLimMu ( float limMu){limMu_ = limMu;}
157  void setNbrNeighbors(int N){n_neigh_ = N;}
158  void setLimMuPos ( float limMuPos){limMuPos_ = limMuPos;}
159  void setDivFact ( float divFact){divFact_= divFact;}
160  void setNoise ( float noise){noise_= noise;}
161  void setParams(float divFact, float curvature)
162  {
163  divFact_= divFact;
164  curvature_ = curvature;
165  };
166  Eigen::Vector3f getNormal(){return normal;}
167  Eigen::Vector3f getPoint (){return pt;}
168  Eigen::Matrix<float, Eigen::Dynamic, 3> getNeighborhood(){return neighborhood_;}
169  int get_N_neigh(){return n_neigh_;}
170 
171  void reinitPoint();
172  void ComputeDist();
173  void SearchFLANNTreeKnn(flann::Index<flann::L2<float>>* index,
174  Eigen::Vector3f& input,
175  std::vector<int>& indices,
176  std::vector<float>& dists,
177  int nn);
178  void selectNeighborsKnn(int N);
179  void selectNeighborsRadius(float r);
180 
181  void init1();
182  void reinitFirst0();
183  void init2();
184  void Optimize(bool first);
185  void OptimizePos(bool first, float thresh_weigh);
186  Eigen::Vector3f getEdgeDirection(int it);
187  void evaluate(int *impact, float *moyError, float impacter_weigh);
188  void select_normal();
190  bool isOnEdge();
191  bool isSecondOption();
192 
193 
194 
195  bool isNan();
196  Eigen::Vector3f finalNormal_;
197  Eigen::Vector3f finalPos_;
198 
199  Eigen::Vector3f normal;
201 
202 
203  private:
204  int n_neigh_;
205  float mu_;
206  float noise_;
207  float limMu_;
208  float limMuPos_;
209  float curvature_;
210  float divFact_;
212  Eigen::Matrix<float, Eigen::Dynamic, 3> *pointcloud_;
213  flann::Index<flann::L2<float>>* tree_;
214  Eigen::Matrix<float, Eigen::Dynamic, 3> neighborhood_;
215  Eigen::Matrix<float, Eigen::Dynamic, 3> dist_;
216  int ref_;
217  Eigen::Vector3f pt;
218  Eigen::Vector3f ptRef_;
219  Eigen::Vector3f centroid_;
220  Eigen::Vector3f* normalFirst0_;
221  Eigen::Vector3f* normalFirst1_;
222  Eigen::Vector3f* normalFirst2_;
223  Eigen::Vector3f* normalSecond0_;
224  Eigen::Vector3f* normalSecond1_;
225  Eigen::Vector3f* normalSecond2_;
226  Eigen::Vector3f* pointFirst_;
227  Eigen::Vector3f* pointSecond_;
228  float theta;
229  float phi;
230  Eigen::VectorXf weights_;
231  std::vector<float> error_;
232  std::vector<float> diff_error_;
233  void ComputeWeights();
234  float GetMaxError();
235  void ComputeNormProjError(std::vector<float>& er_proj);
236  void orient();
237  void actualizeMu();
238  void init_weight();
239  Eigen::Vector3f getThirdEigenVector(Eigen::Matrix3f& C);
240  void save_itr(int itr);
246  float er_max;
247  float mu_init2_;
248 };
249 
250 #include "core.inl"
CApp::moyErrorSecond_
float moyErrorSecond_
Definition: core.h:244
CApp::normalFirst2_
Eigen::Vector3f * normalFirst2_
Definition: core.h:222
CApp::getNeighborhood
Eigen::Matrix< float, Eigen::Dynamic, 3 > getNeighborhood()
Definition: core.h:168
CApp::jamais_fait
bool jamais_fait
Definition: core.h:245
CApp::get_N_neigh
int get_N_neigh()
Definition: core.h:169
CApp::setNoise
void setNoise(float noise)
Definition: core.h:160
CApp::getEdgeDirection
Eigen::Vector3f getEdgeDirection(int it)
CApp::tree_
flann::Index< flann::L2< float > > * tree_
Definition: core.h:213
CApp::dist_
Eigen::Matrix< float, Eigen::Dynamic, 3 > dist_
Definition: core.h:215
mu_max2
const float mu_max2
Definition: core.h:46
CApp::ptRef_
Eigen::Vector3f ptRef_
Definition: core.h:218
CApp::pointSecond_
Eigen::Vector3f * pointSecond_
Definition: core.h:227
CApp::setLimMu
void setLimMu(float limMu)
Definition: core.h:156
likelihood_threshold
const float likelihood_threshold
Definition: core.h:60
CApp::ComputeNormProjError
void ComputeNormProjError(std::vector< float > &er_proj)
CApp::mu_
float mu_
Definition: core.h:205
CApp::normalSecond1_
Eigen::Vector3f * normalSecond1_
Definition: core.h:224
CApp::moyErrorFirst_
float moyErrorFirst_
Definition: core.h:243
CApp::save_itr
void save_itr(int itr)
CApp::n_neigh_
int n_neigh_
Definition: core.h:204
lim_diff
const float lim_diff
Definition: core.h:53
CApp::CApp
CApp(float divFact, float limMu, float limMuPos, Eigen::Matrix< float, Eigen::Dynamic, 3 > *pc, flann::Index< flann::L2< float >> *tree, int ref, float noise)
Definition: core.h:111
CApp::~CApp
~CApp()
Definition: core.h:131
itr_per_mu
const int itr_per_mu
Definition: core.h:58
CApp::noise_
float noise_
Definition: core.h:206
CApp::normalSecond2_
Eigen::Vector3f * normalSecond2_
Definition: core.h:225
CApp::normalFirst1_
Eigen::Vector3f * normalFirst1_
Definition: core.h:221
mu_max
const float mu_max
Definition: core.h:57
CApp::er_max
float er_max
Definition: core.h:246
CApp::neighborhood_
Eigen::Matrix< float, Eigen::Dynamic, 3 > neighborhood_
Definition: core.h:214
CApp::SuspectedOnEdge_
bool SuspectedOnEdge_
Definition: core.h:200
CApp::finalPos_
Eigen::Vector3f finalPos_
Definition: core.h:197
CApp::normal
Eigen::Vector3f normal
Definition: core.h:199
CApp::curvature_
float curvature_
Definition: core.h:209
CApp::CApp
CApp()
Definition: core.h:67
CApp::CApp
CApp(Eigen::Matrix< float, Eigen::Dynamic, 3 > *pc, flann::Index< flann::L2< float >> *tree, float noise)
Definition: core.h:80
itr_min
const int itr_min
Definition: core.h:56
CApp::mu_init2_
float mu_init2_
Definition: core.h:247
noise_min
const float noise_min
Definition: core.h:59
CApp::pointcloud_
Eigen::Matrix< float, Eigen::Dynamic, 3 > * pointcloud_
Definition: core.h:212
CApp::ref_
int ref_
Definition: core.h:216
CApp::getPoint
Eigen::Vector3f getPoint()
Definition: core.h:167
CApp::impactSecond_
int impactSecond_
Definition: core.h:242
CApp::limMuPos_
float limMuPos_
Definition: core.h:208
min_points_fact
const float min_points_fact
Definition: core.h:61
CApp::normalSecond0_
Eigen::Vector3f * normalSecond0_
Definition: core.h:223
CApp::phi
float phi
Definition: core.h:229
CApp::setNormal
void setNormal(Eigen::Vector3f norm)
Definition: core.h:154
CApp::setNbrNeighbors
void setNbrNeighbors(int N)
Definition: core.h:157
CApp::limMu_
float limMu_
Definition: core.h:207
CApp::selectNeighborsRadius
void selectNeighborsRadius(float r)
CApp::CApp
CApp(Eigen::Matrix< float, Eigen::Dynamic, 3 > *pc, flann::Index< flann::L2< float >> *tree, int ref, float noise)
Definition: core.h:95
CApp::setDivFact
void setDivFact(float divFact)
Definition: core.h:159
CApp::divFact_
float divFact_
Definition: core.h:210
CApp::centroid_
Eigen::Vector3f centroid_
Definition: core.h:219
thresh_weight
const float thresh_weight
Definition: core.h:62
CApp::diff_error_
std::vector< float > diff_error_
Definition: core.h:232
CApp::setPc
void setPc(Eigen::Matrix< float, Eigen::Dynamic, 3 > *pc)
Definition: core.h:152
CApp::impactFirst_
int impactFirst_
Definition: core.h:241
core.inl
CApp::theta
float theta
Definition: core.h:228
CApp::error_
std::vector< float > error_
Definition: core.h:231
CApp::getNormal
Eigen::Vector3f getNormal()
Definition: core.h:166
CApp::normalFirst0_
Eigen::Vector3f * normalFirst0_
Definition: core.h:220
CApp::weights_
Eigen::VectorXf weights_
Definition: core.h:230
CApp::setTree
void setTree(flann::Index< flann::L2< float >> *t)
Definition: core.h:151
CApp
Definition: core.h:65
CApp::pt
Eigen::Vector3f pt
Definition: core.h:217
CApp::finalNormal_
Eigen::Vector3f finalNormal_
Definition: core.h:196
lim_error
const float lim_error
Definition: core.h:52
CApp::setParams
void setParams(float divFact, float curvature)
Definition: core.h:161
CApp::addSymmetricDist
void addSymmetricDist()
CApp::setLimMuPos
void setLimMuPos(float limMuPos)
Definition: core.h:158
epsilon
const float epsilon
Definition: core.h:51
CApp::pointFirst_
Eigen::Vector3f * pointFirst_
Definition: core.h:226
CApp::thresh_proj_
float thresh_proj_
Definition: core.h:211
curvature
void curvature(FEVV::DataStructures::AIF::AIFMesh *mesh)
Definition: test_curvature_filter_aif.cpp:26
CApp::actualizeMu
void actualizeMu()
div_fact
const float div_fact
Definition: core.h:48