aimstil  5.0.5
triangle_mesh_geodesic_map.h
Go to the documentation of this file.
1 #ifndef TRIANGLEMESHGEODESICMAP_H_
2 #define TRIANGLEMESHGEODESICMAP_H_
3 
4 // includes from STL
5 #include <queue>
6 #include <vector>
7 
8 // includes from TIL
9 #include "til/sparse_vector.h"
10 
11 // includes from TIL
12 #include "cyclic_iterator.h"
13 #include "fraction.h"
14 #include "MeshTraits.h"
15 #include "meshUtils.h"
16 #include "miscUtils.h"
17 #include "poly_solver.h"
18 
19 // declarations
20 #include "til/til_declarations.h"
21 
22 namespace til
23 {
24  // TODO: rename as plugin
25  namespace ghost
26  {
27 
29  {
30  // Only one function instead of two (warn(I,M) and proceed(void)) so that we don't have
31  // to store anything if not needed and then everything can be inlined.
32  template < typename TGMap >
33  bool proceed(std::size_t, TGMap &) { return true; }
34  };
35 
36  template < typename TPrec >
38  {
39  //GMapStop_AboveThreshold() : m_threshold() {}
40  GMapStop_AboveThreshold(TPrec threshold) : m_threshold(threshold) {}
41 
42  template < typename TGMap >
43  typename boost::enable_if<boost::is_same<TPrec, typename TGMap::precision_type>, bool>::type
44  proceed(std::size_t i, TGMap & gmap) const
45  {
46  return (*(gmap.distanceMap()))[i] < m_threshold;
47  }
48 
49  TPrec m_threshold;
50  };
51 
52 
53  template < typename TIterator >
55  {
56  GMapStop_MyPointsDone() : m_begin(), m_end() {}
57 
58  GMapStop_MyPointsDone(TIterator begin, TIterator end)
59  {
60  this->init(begin, end);
61  }
62 
63  void init(TIterator begin, TIterator end)
64  {
65  m_begin = begin;
66  m_end = end;
67  m_count = std::distance(m_begin, m_end);
68  }
69 
70  template < typename TGMap >
71  bool proceed(std::size_t i, TGMap &)
72  {
73  if (std::find(m_begin, m_end, i) != m_end)
74  {
75  if (--m_count == 0) return false;
76  }
77  return true;
78  }
79 
80  TIterator m_begin;
81  TIterator m_end;
82  int m_count;
83  };
84  }
85 
86 
87  namespace policy
88  {
89  /*
90  template < template < typename, typename, typename, typename > class GM >
91  struct GMapStop_DistAboveThreshold
92  {
93  };
94  */
95 
96  /*
97  template < typename T >
98  struct VectorStorage
99  {
100  typename std::vector<T> Container;
101  void init(Container & c, std::size_t size, T value)
102  {
103  c.resize(size);
104  std::fill(c.begin(), c.end(), value);
105  }
106  };
107  */
108 
109  //template < template <typename> class TContainer, typename TMesh, typename TPrec >
110 /* template < template <typename> class TContainer, typename TPrec >
111  struct GMap_DefaultStorage
112  {
113  typedef unsigned char Label;
114  typedef TContainer<Label> LabelCollection;
115  typedef TContainer<TPrec> DistCollection;
116  };
117 */
118  // created to replace GMap_DefaultStorage<til::sparse_vector, double> calls
119  // not allowed in new gcc version
121  {
122  typedef unsigned char Label;
123  typedef double TPrec;
126  };
127 
128  }
129 
130 
131 
133  {
134  enum {
135  UNACTIVE = 0,
137  ACTIVE
138  };
139  };
140 
141  //---------------------------------------------------------------------------
142 
143  //---------------------//
144  // Mesh_distance_map //
145  //---------------------//
146 
147  // TODO: Actually, this empty shell should better be named MeshGreedyGrowth, cause it's really what it is.
148  //template < typename TMesh, typename TPrec, typename TStopGhost = ghost::GMapStop_None, typename TStoragePolicy = policy::GMap_DefaultStorage<std::vector, TMesh, TPrec> >
149  // TODO: replace reference to containers with accessors.
150 
151  // til::policy::GMap_DefaultStorage<til::sparse_vector, TPrec > replaced by GMap_DefaultStorage_sparse_vect_dbl
152  template < typename TVertices, typename TNeighborhoods, typename TPrec, typename TStopGhost = ghost::GMapStop_None, typename TStoragePolicy = policy::GMap_DefaultStorage_sparse_vect_dbl >
154  : public DistanceMapLabels
155  {
156  private: // classes
157 
158  /*
160  template < typename T1, typename T2 >
161  struct PairComp
162  {
163  // is operator< on
164  inline bool operator()(const std::pair<T1, T2> & p1, const std::pair<T1, T2> & p2) const
165  //inline bool operator()(std::pair<T1, T2> p1, std::pair<T1, T2> p2) const
166  {
167  return p1.second > p2.second;
168  }
169  };
170  */
171 
172  public: // typedefs
173 
175 
176  typedef TPrec precision_type;
177 
178  // import typdefs from policies
179  typedef typename TStoragePolicy::DistCollection DistCollection;
180  typedef typename TStoragePolicy::Label Label;
181  typedef typename TStoragePolicy::LabelCollection LabelCollection;
182 
183  //typedef typename MeshTraits<TMesh>::FaceIndex::value_type VertexIndex;
184  //typedef typename MeshTraits<TMesh>::FaceIndex::value_type VertexIndex;
185 
186  typedef typename TNeighborhoods::value_type Neighborhood;
187 
188  typedef std::size_t VertexIndex;
189  //typedef std::vector<VertexIndex> Neighborhood;
190  //typedef std::vector<VertexIndex> Neighborhood;
191  typedef std::priority_queue<
192  std::pair<VertexIndex, TPrec>,
193  std::vector<std::pair<VertexIndex, TPrec> >,
195 
196 
197  public: // constructors & destructor
198 
199  Mesh_distance_map(const TVertices & vertices, const TNeighborhoods & neighc);
200 
201  Mesh_distance_map(const TVertices & vertices, const TNeighborhoods & neighc, const TStopGhost & sp);
202 
203  virtual ~Mesh_distance_map() {}
204 
205  public: // set & get
206 
207  TStopGhost & stopGhost() { return m_stopGhost; }
208  const TStopGhost & stopGhost() const { return m_stopGhost; }
209 
215  shared_ptr<const DistCollection> distanceMap() const { return m_pDist; }
216  shared_ptr<const LabelCollection> labels() const { return m_pLabel; }
218  shared_ptr<LabelCollection> labels() { return m_pLabel; }
219 
220  public: // functions
221 
223  void init(VertexIndex iVertex);
224 
228  // NB: we template on TVertexIndex to be able to tackle const Vertex* kind of stuff...
229  template < typename TVertexIndex >
230  void init(std::vector<TVertexIndex> & startPoints, std::vector<TPrec> & startDist);
231 
232  void process();
233 
234  private: // functions
235 
236  void _init();
237 
238  protected: // pure virtual functions
239 
240  virtual TPrec distanceEstimate(VertexIndex iVertex) = 0;
241 
242  protected: // data
243  //const TMesh & m_mesh;
244  const TVertices & m_vertices;
245  //const TFaces & m_faces;
246  const TNeighborhoods & m_neighc;
247  // contains the status of vertices
249  // contains the geodesic distance, i.e. the result
251  // Circular neighborhoods
252  //shared_ptr<std::vector<Neighborhood> > m_pNeigh;
253  TPrec F;
254  Queue m_queue;
255  TStopGhost m_stopGhost;
256  bool m_allDone;
257  };
258 
259  //---------------------------------------------------------------------------
260 
261  //----------------------//
262  // Graph_distance_map //
263  //----------------------//
264 
265  //template < typename TMesh, typename TPrec, typename TStopGhost = ghost::GMapStop_None, typename TStoragePolicy = policy::GMap_DefaultStorage<std::vector, TMesh, TPrec> >
266  // til::policy::GMap_DefaultStorage<til::sparse_vector, TPrec > replaced by GMap_DefaultStorage_sparse_vect_dbl
267  template < typename TVertices, typename TNeighborhoods, typename TPrec, typename TStopGhost = ghost::GMapStop_None, typename TStoragePolicy = policy::GMap_DefaultStorage_sparse_vect_dbl >
269  //: public Mesh_distance_map<TMesh, TPrec, TStopGhost, TStoragePolicy>
270  : public Mesh_distance_map<TVertices, TNeighborhoods, TPrec, TStopGhost, TStoragePolicy>
271  {
272  public: // typedefs
273 
274  //typedef Mesh_distance_map<TMesh, TPrec, TStopGhost, TStoragePolicy> Base;
277  typedef typename Base::VertexIndex VertexIndex;
278 
279  public: // constructors & destructor
280 
281  Graph_distance_map(const TVertices & vertices, const TNeighborhoods & neighc)
282  : Base(vertices, neighc) {}
283  Graph_distance_map(const TVertices & vertices, const TNeighborhoods & neighc, const TStopGhost & sp)
284  : Base(vertices, neighc, sp) {}
285 
286  private: // functions
287  TPrec distanceEstimate(VertexIndex iVertex);
288  };
289 
290 
291  //---------------------------------------------------------------------------
292 
293  //------------------------------//
294  // Triangle_mesh_geodesic_map //
295  //------------------------------//
296 
297  // NB: the neighborhood have to be circular now. TODO: enforce that.
298  //template < typename TMesh, typename TPrec, typename TStopGhost = ghost::GMapStop_None, typename TStoragePolicy = policy::GMap_DefaultStorage<std::vector, TMesh, TPrec> >
299  // til::policy::GMap_DefaultStorage<til::sparse_vector, TPrec > replaced by GMap_DefaultStorage_sparse_vect_dbl
300  template < typename TVertices, typename TCircularNeighborhood, typename TPrec, typename TStopGhost = ghost::GMapStop_None, typename TStoragePolicy = policy::GMap_DefaultStorage_sparse_vect_dbl >
302  //: public Mesh_distance_map<TMesh, TPrec, TStopGhost, TStoragePolicy>
303  : public Mesh_distance_map<TVertices, TCircularNeighborhood, TPrec, TStopGhost, TStoragePolicy>
304  {
305  public: // typedefs
306 
309  typedef typename Base::VertexIndex VertexIndex;
310 
311  public: // constructors & destructor
312 
313  Triangle_mesh_geodesic_map(const TVertices & vertices, const TCircularNeighborhood & neighc)
314  : Base(vertices, neighc) {}
315  Triangle_mesh_geodesic_map(const TVertices & vertices, const TCircularNeighborhood & neighc, const TStopGhost & sp)
316  : Base(vertices, neighc, sp) {}
317 
318  private: // functions
319  TPrec distanceEstimate(VertexIndex iVertex);
320  };
321 
322  //---------------------------------------------------------------------------
323 
324  //--------------------------------------//
325  // Voronoi_triangle_mesh_geodesic_map //
326  //--------------------------------------//
327 
328  // TODO: this should be redone to avoid code duplication with Triangle_mesh_geodesic_map.
329  // It shouldn't be too hard, provided the API of distance maps is modified. The trick though is that
330  // I guess I still want to have only one priority queues, not as many as there are elements...
331  //template < typename TMesh, typename TPrec, typename TStopGhost = ghost::GMapStop_None, typename TStoragePolicy = policy::GMap_DefaultStorage<std::vector, TMesh, TPrec> >
332  // til::policy::GMap_DefaultStorage<til::sparse_vector, TPrec > replaced by GMap_DefaultStorage_sparse_vect_dbl
333  template < typename TVertices, typename TCircularNeighborhoods, typename TPrec, typename TStopGhost = ghost::GMapStop_None, typename TStoragePolicy = policy::GMap_DefaultStorage_sparse_vect_dbl >
335  : public Mesh_distance_map<TVertices, TCircularNeighborhoods, TPrec, TStopGhost, TStoragePolicy>
336  {
337  public: // typedefs
338 
341  typedef typename Base::VertexIndex VertexIndex;
342 
343  public: // constructors & destructor
344 
345  Voronoi_triangle_mesh_geodesic_map(const TVertices & vertices, const TCircularNeighborhoods & neighc)
346  : Base(vertices, neighc)
347  , m_clusterLabel(new std::vector<unsigned int>(vertices.size(),0))
348  {}
349  Voronoi_triangle_mesh_geodesic_map(const TVertices & vertices, const TCircularNeighborhoods & neighc, const TStopGhost & sp)
350  : Base(vertices, sp)
351  , m_clusterLabel(new std::vector<unsigned int>(vertices.size(),0))
352  {}
353 
354  public: // functions
355 
357 
358  template < typename TVertexIndex >
359  void init(std::vector<TVertexIndex> & startPoints, std::vector<TPrec> & startDist)
360  {
361  unsigned int count = 1;
362  for (typename std::vector<TVertexIndex>::iterator i = startPoints.begin(); i != startPoints.end(); ++i)
363  {
364  //(*m_clusterLabel)[getVertexNumber(Base::m_mesh, *i)] = count++;
365  (*m_clusterLabel)[*i] = count++;
366  }
367  this->Base::init(startPoints, startDist);
368  }
369 
370  private: // functions
371  TPrec distanceEstimate(VertexIndex iVertex);
372 
373  private: // data
374  shared_ptr<std::vector<unsigned int> > m_clusterLabel;
375  };
376 
377  //---------------------------------------------------------------------------
378 
379  //--------------------//
380  // helper functions //
381  //--------------------//
382 
385  template < typename TVertices, typename TFaces, typename TPrec >
386  void
388  (
389  TVertices const & vertices
390  , TFaces const & faces
391  , TPrec distance
392  , std::vector<til::sparse_vector<TPrec> > & res
393  );
394 
395 } // namespace til
396 
397 
399 
400 #endif /*TRIANGLEMESHGEODESICMAP_H_*/
void distance_to_neighbors(TVertices const &vertices, TFaces const &faces, TPrec distance, std::vector< til::sparse_vector< TPrec > > &res)
Returns neighbors that are below a certain geodesic distance, along with their distance.
void init(std::vector< TVertexIndex > &startPoints, std::vector< TPrec > &startDist)
Mesh_distance_map< TVertices, TCircularNeighborhood, TPrec, TStopGhost, TStoragePolicy > Base
GMapStop_MyPointsDone(TIterator begin, TIterator end)
Triangle_mesh_geodesic_map(const TVertices &vertices, const TCircularNeighborhood &neighc, const TStopGhost &sp)
Graph_distance_map(const TVertices &vertices, const TNeighborhoods &neighc, const TStopGhost &sp)
TStoragePolicy::DistCollection DistCollection
shared_ptr< LabelCollection > m_pLabel
std::priority_queue< std::pair< VertexIndex, TPrec >, std::vector< std::pair< VertexIndex, TPrec > >, Greater_Pair2< VertexIndex, TPrec > > Queue
TStoragePolicy::LabelCollection LabelCollection
Triangle_mesh_geodesic_map(const TVertices &vertices, const TCircularNeighborhood &neighc)
shared_ptr< const LabelCollection > labels() const
shared_ptr< std::vector< unsigned int > > clusterLabels()
STL namespace.
Mesh_distance_map< TVertices, TCircularNeighborhoods, TPrec, TStopGhost, TStoragePolicy > Base
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
Voronoi_triangle_mesh_geodesic_map(const TVertices &vertices, const TCircularNeighborhoods &neighc, const TStopGhost &sp)
TNeighborhoods::value_type Neighborhood
Mesh_distance_map< TVertices, TNeighborhoods, TPrec, TStopGhost, TStoragePolicy > Base
numeric_array< T, D > size(const Box< T, D > &box)
Return the size of a box.
Definition: boxTools.h:56
Mesh_distance_map< TVertices, TNeighborhoods, TPrec, TStopGhost, TStoragePolicy > Self
This file contains forward declarations of classes defined in the TIL library.
A class that mimic the behavior of std::vector but with a storage policy focused on sparse data...
Voronoi_triangle_mesh_geodesic_map(const TVertices &vertices, const TCircularNeighborhoods &neighc)
const TStopGhost & stopGhost() const
boost::enable_if< boost::is_same< TPrec, typename TGMap::precision_type >, bool >::type proceed(std::size_t i, TGMap &gmap) const
shared_ptr< LabelCollection > labels()
Graph_distance_map(const TVertices &vertices, const TNeighborhoods &neighc)
const TNeighborhoods & m_neighc
shared_ptr< DistCollection > distanceMap()
void init(TIterator begin, TIterator end)
operator> on the second member of a pair.
Definition: miscUtils.h:388
shared_ptr< const DistCollection > distanceMap() const
Get computed distance map.
shared_ptr< DistCollection > m_pDist
bool proceed(std::size_t, TGMap &)