KernelCollectionJetFitting.cpp
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
8 
9 #ifdef NUKLEI_USE_CGAL
10 #if CGAL_VERSION_NR >= 1040100000
11 #define CGAL_LAPACK_ENABLED
12 #endif
13 #include <CGAL/Monge_via_jet_fitting.h>
14 #include <CGAL/Cartesian.h>
15 #endif
16 
18 
19 namespace nuklei
20 {
21 
22 #ifdef NUKLEI_USE_CGAL
23  namespace cgal_jet_fitting_types
24  {
25  typedef double DFT;
26  typedef CGAL::Cartesian<DFT> Data_Kernel;
27  typedef Data_Kernel::Point_3 DPoint;
28  typedef CGAL::Monge_via_jet_fitting<Data_Kernel> Monge_via_jet_fitting;
29  typedef Monge_via_jet_fitting::Monge_form Monge_form;
30  }
31 #endif
32 
33  boost::tuple<Matrix3, Vector3, coord_t>
35  {
36  NUKLEI_TRACE_BEGIN();
37 
38 #ifdef NUKLEI_USE_CGAL
39 
40  using namespace cgal_neighbor_search_types;
41  using namespace cgal_jet_fitting_types;
42 
43  if (!deco_.has_key(NSTREE_KEY))
44  NUKLEI_THROW("Undefined neighbor search tree. Call buildNeighborSearchTree() first.");
45 
46  boost::shared_ptr<Tree> tree(deco_.get< boost::shared_ptr<Tree> >(NSTREE_KEY));
47 
48  Point_d center(loc.X(), loc.Y(), loc.Z());
49 
50  std::vector<DPoint> in_points;
51  K_neighbor_search search(*tree, center, 16+1);
52  NUKLEI_ASSERT(search.begin() != search.end());
53  for (K_neighbor_search::iterator i = search.begin(); i != search.end(); ++i)
54  {
55  in_points.push_back(DPoint(i->first.x(), i->first.y(), i->first.z()));
56  }
57 
58  size_t d_fitting = 4;
59  size_t d_monge = 4;
60 
61  if (in_points.size() != 16+1)
62  return boost::tuple<Matrix3, Vector3, coord_t>();
63 
64  Monge_form monge_form;
65  Monge_via_jet_fitting monge_fit;
66  monge_form = monge_fit(in_points.begin(), in_points.end(), d_fitting, d_monge);
67 
68 // NUKLEI_LOG("vertex : " << in_points[0] << std::endl
69 // << "number of points used : " << in_points.size() << std::endl
70 // << monge_form << "condition_number : " << monge_fit.condition_number() << std::endl
71 // << "pca_eigen_vals and associated pca_eigen_vecs :" << std::endl <<
72 // "first eval: " << monge_fit.pca_basis(0).first << "; first evec: " << monge_fit.pca_basis(0).second)
73 
74 
75  Matrix3 eigenVectors;
76  Vector3 eigenValues;
77  coord_t conditionNumber;
78 
79  for (int i = 0; i < 3; i++)
80  {
81  Vector3 v(monge_fit.pca_basis(i).second[0],
82  monge_fit.pca_basis(i).second[1],
83  monge_fit.pca_basis(i).second[2]);
84  eigenVectors.SetColumn(i, v);
85  eigenValues[i] = monge_fit.pca_basis(i).first;
86  }
87 
88  conditionNumber = monge_fit.condition_number();
89  NUKLEI_ASSERT(conditionNumber != 0);
90 
91  return boost::make_tuple(eigenVectors, eigenValues, conditionNumber);
92 
93 #else
94  NUKLEI_THROW("This function requires CGAL. See http://renaud-detry.net/nuklei/group__install.html");
95 #endif
96 
97  NUKLEI_TRACE_END();
98  }
99 
101  {
102  KernelCollection kc2;
103 
104  if (!deco_.has_key(NSTREE_KEY))
105  NUKLEI_THROW("Undefined neighbor search tree. "
106  "Call buildNeighborSearchTree() first.");
107 
108  int skipped = 0;
109  for (KernelCollection::const_iterator i = as_const(*this).begin();
110  i != as_const(*this).end(); ++i)
111  {
112  kernel::r3xs2p k;
113  k.loc_ = i->getLoc();
114  boost::tuple<Matrix3, Vector3, coord_t> dp =
116  if (dp.get<2>() == 0)
117  {
118  skipped++;
119  continue;
120  }
121  k.dir_ = dp.get<0>().GetColumn(2);
122  k.setWeight(i->getWeight());
123  if (i->hasDescriptor()) k.setDescriptor(i->getDescriptor());
124  kc2.add(k);
125  }
126  if (skipped > 0)
127  NUKLEI_WARN("Skipped " << skipped << " kernels for which "
128  "CGAL couldn't compute local diff.");
129  *this = kc2;
130  }
131 }
132 
133 
void add(const kernel::base &f)
Adds a copy of f.
Public namespace.
Definition: Color.cpp:9
Container::const_iterator const_iterator
KernelCollection iterator.
This class acts as a vector-like container for kernels. It also provides methods related to kernel de...
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
void setWeight(const weight_t w)
Sets this kernel's weight.
Definition: Kernel.h:217
Definition: Kernel.h:505
double coord_t
Type for point coordinates.
Definition: Definitions.h:25
boost::tuple< Matrix3, Vector3, coord_t > localLocationDifferential(const Vector3 &k) const
Computes the local differential properties of the nearest neighbors of k.
T const & as_const(T const &t)
Provides a const reference to an object.
Definition: Common.h:197
void computeSurfaceNormals()
Computes surface normals at all points. After running this method, all kernels are nuklei::kernel::r3...
#define NUKLEI_THROW(x)
Throws an Error.
Definition: Common.h:94
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.