aimstil  5.0.5
MeshTraits.h
Go to the documentation of this file.
1 #ifndef _MESHTRAITS_H_
2 #define _MESHTRAITS_H_
3 
4 // includes from STL
5 #include <vector>
6 
7 // includes from BOOST
8 #include <boost/array.hpp>
9 #include <boost/type_traits.hpp>
10 
11 // include from TIL
12 #include "cyclic_iterator.h"
13 #include "Mesh.h"
14 #include "misc_sort.h"
15 #include "miscUtils.h"
16 
17 // declarations
18 #include "til/til_declarations.h"
19 
20 
21 namespace til
22 {
25  {
26  enum { has_edge_indices = 0 };
27  enum { has_neighbor_indices = 0 };
28  enum { has_faces_indices = 0 };
29  };
30 
35  template < class TMesh >
36  struct MeshTraits {};
37 
38 
39 
40 
41  /*
42  template < typename TMesh >
43  const typename MeshTraits<TMesh>::Vertex &
44  getVertex(std::size_t i, const TMesh & mesh)
45  {
46  return getVertices(mesh)[i];
47  }
48 
49  template < typename TMesh >
50  const typename MeshTraits<TMesh>::Vertex &
51  getVertex(const typename MeshTraits<TMesh>::Vertex *v, const TMesh &)
52  {
53  return *v;
54  }
55  */
56 
57 
59 
61  template < typename TParam >
62  // NB: Deriving from TParam simply enable us to get all the typedefs.
63  struct MeshTraits<Mesh<TParam> > : public MeshTraits_default, public TParam
64  {
65  // constants
66  enum { has_faces_indices = 1 };
67 
68  // typedefs
69  //typedef MeshFaceCollection<TParam> FaceCollection;
70  };
71 
72  template < typename TParam >
73  inline
74  const typename MeshTraits<Mesh<TParam> >::VertexCollection &
75  getVertices(const Mesh<TParam> & mesh) { return mesh.getVertices(); }
76 
77  template < typename TParam >
78  inline
79  typename MeshTraits<Mesh<TParam> >::VertexCollection &
80  getVertices(Mesh<TParam> & mesh) { return mesh.getVertices(); }
81 
82  /*
83  template < typename TParam >
84  inline
85  const typename MeshTraits<Mesh<TParam> >::FaceIndexCollection &
86  getFaceIndices(const Mesh<TParam> & mesh) { return mesh.getFaceIndices(); }
87 
88  template < typename TParam >
89  inline
90  typename MeshTraits<Mesh<TParam> >::FaceIndexCollection &
91  getFaceIndices(Mesh<TParam> & mesh) { return mesh.getFaceIndices(); }
92  */
93 
94  inline
96  getFaceIndices(const Mesh1 & mesh) { return mesh.getFaceIndices(); }
97 
98  inline
100  getFaceIndices(const Mesh2 & mesh) { return mesh.getFaceIndices(); }
101 
102  inline
104  getFaceIndices(Mesh1 & mesh) { return mesh.getFaceIndices(); }
105 
106  inline
108  getFaceIndices(Mesh2 & mesh) { return mesh.getFaceIndices(); }
109 
110  /*
111  template < typename TParam >
112  class MeshFaceCollection
113  {
114  public: // constructors & destructor
115 
116  MeshFaceCollection(Mesh<TParam> & mesh) :
117  m_mesh(mesh)
118  {
119  }
120 
121  public: // functions
122 
123 
124  class ConstIterator
125  {
126  / *
127  public: // Face
128 
129  class Face
130  {
131  public: // constructors & destructor
132  Face(const AimsSurface<D,T> & mesh, typename MeshTraits<AimsSurface<D,T> >::FaceIndex &f)
133  : m_mesh(mesh), m_f(f) {}
134 
135  public: // functions
136  typename MeshTraits<AimsSurface<D,T> >::Vertex &
137  operator[](int i)
138  {
139  return getVertices(m_mesh)[m_f[i]];
140  }
141 
142  private: // data
143  const AimsSurface<D,T> & m_mesh;
144  typename MeshTraits<AimsSurface<D,T> >::FaceIndex & m_f;
145  };
146  * /
147 
148  public: // constructors & destructor
149  ConstIterator(const Mesh<TParam> & mesh, typename MeshTraits<Mesh<TParam> >::FaceIndexCollection::iterator i)
150  : m_vertices(getVertices(mesh)), m_i(i) {}
151 
152  public: // functions
153  inline void operator++() { ++m_i; }
154  inline const ConstIterator & operator*() const { return (*this); }
155  inline bool operator!=(const ConstIterator &i) const { return m_i != i.m_i; }
156 
157  inline
158  const typename MeshTraits<Mesh<TParam> >::Vertex &
159  operator[](int i) const
160  {
161  return m_vertices[(*m_i)[i]];
162  }
163 
164 
165  private: // data
166 
167  const typename MeshTraits<Mesh<TParam> >::VertexCollection & m_vertices;
168  //const AimsSurface<D,T> & m_mesh;
169  typename MeshTraits<Mesh<TParam> >::FaceIndexCollection::iterator m_i;
170  //Face m_face;
171  };
172 
173 
174  typedef ConstIterator const_iterator;
175 
176  ConstIterator begin() { return ConstIterator(m_mesh, getFaceIndices(m_mesh).begin()); }
177  ConstIterator end() { return ConstIterator(m_mesh, getFaceIndices(m_mesh).end()); }
178 
179  private: // data
180  Mesh<TParam> & m_mesh;
181  };
182  */
183 
184  /*
185  template < >
186  class MeshFaceCollection<MeshParam2>
187  {
188  public: // constructors & destructor
189 
190  MeshFaceCollection(Mesh<MeshParam2> & mesh) :
191  m_mesh(mesh)
192  {
193  }
194 
195  public: // functions
196 
197 
198  class ConstIterator
199  {
200  / *
201  public: // Face
202 
203  class Face
204  {
205  public: // constructors & destructor
206  Face(const AimsSurface<D,T> & mesh, typename MeshTraits<AimsSurface<D,T> >::FaceIndex &f)
207  : m_mesh(mesh), m_f(f) {}
208 
209  public: // functions
210  typename MeshTraits<AimsSurface<D,T> >::Vertex &
211  operator[](int i)
212  {
213  return getVertices(m_mesh)[m_f[i]];
214  }
215 
216  private: // data
217  const AimsSurface<D,T> & m_mesh;
218  typename MeshTraits<AimsSurface<D,T> >::FaceIndex & m_f;
219  };
220  * /
221 
222  public: // constructors & destructor
223  ConstIterator(const Mesh<MeshParam2> & mesh, MeshTraits<Mesh<MeshParam2> >::FaceIndexCollection::iterator i)
224  : m_vertices(getVertices(mesh)), m_i(i) {}
225 
226  public: // functions
227  inline void operator++() { ++m_i; }
228  inline const ConstIterator & operator*() const { return (*this); }
229  inline bool operator!=(const ConstIterator &i) const { return m_i != i.m_i; }
230 
231 
232  inline
233  const MeshTraits<Mesh<MeshParam2> >::Vertex &
234  operator[](int i) const
235  {
236  return *((*m_i)[i]);
237  }
238 
239  / *
240  inline
241  typename boost::enable_if<boost::is_same<MeshParam2, MeshParam1>,
242  const typename MeshTraits<Mesh<MeshParam2> >::Vertex &>::type
243  get(int i) const
244  {
245  return m_vertices[(*m_i)[i]];
246  }
247 
248  inline
249  typename boost::enable_if<boost::is_same<MeshParam2, MeshParam2>,
250  const typename MeshTraits<Mesh<MeshParam2> >::Vertex &>::type
251  get(int i) const
252  {
253  return *((*m_i)[i]);
254  }
255  * /
256 
257  private: // data
258 
259  const MeshTraits<Mesh<MeshParam2> >::VertexCollection & m_vertices;
260  //const AimsSurface<D,T> & m_mesh;
261  MeshTraits<Mesh<MeshParam2> >::FaceIndexCollection::iterator m_i;
262  //Face m_face;
263  };
264 
265  typedef ConstIterator const_iterator;
266 
267  ConstIterator begin() { return ConstIterator(m_mesh, getFaceIndices(m_mesh).begin()); }
268  ConstIterator end() { return ConstIterator(m_mesh, getFaceIndices(m_mesh).end()); }
269 
270  private: // data
271  Mesh<MeshParam2> & m_mesh;
272  };
273  */
274 
275  /*
276  template < typename TParam >
277  inline
278  MeshFaceCollection<TParam>
279  getFaces(Mesh<TParam> & mesh)
280  {
281  return MeshFaceCollection<TParam>(mesh);
282  }
283  */
284 
285  /*
286  template < typename TMesh, typename TEdgeCollection >
287  class MeshEdgeExtractor
288  {
289  private: // typedefs
290  typedef typename MeshTraits<TMesh>::FaceIndex::value_type VertexIndex;
291  typedef numeric_array<VertexIndex,2> Edge;
292  typedef std::set<Edge, Lexicographical_compare<Edge> > UniqueEdges;
293 
294  public: // functions
295  void operator()(const TMesh & mesh, TEdgeCollection & edges)
296  {
297  UniqueEdges uniqueEdges;
298  this->_getEdges(mesh, uniqueEdges);
299  edges.resize(uniqueEdges.size());
300  std::copy(uniqueEdges.begin(), uniqueEdges.end(), edges.begin());
301  }
302 
303  private: // functions
304 
305  void _getEdges(const TMesh & mesh, UniqueEdges & res)
306  {
307  typename MeshTraits<TMesh>::FaceIndexCollection::const_iterator iFic = getFaceIndices(mesh).begin();
308  for (; iFic != getFaceIndices(mesh).end(); ++iFic)
309  {
310  typename MeshTraits<TMesh>::FaceIndex::const_iterator iFi1 = iFic->begin();
311  const_cyclic_iterator<typename MeshTraits<TMesh>::FaceIndex> iFi2(*iFic, iFi1+1);
312  //const_cyclic_iterator<typename MeshTraits<Mesh<TParam> >::FaceIndex> iFi2(iFic, iFi1+1);
313  //const_cyclic_iterator<typename TParam::FaceIndexCollection::value_type> iFi2(iFic, iFi1+1);
314  //const_cyclic_iterator<boost::array<Point<float,3>*, 3> > iFi2(iFic, iFi1+1);
315  for (;iFi1 != iFic->end(); ++iFi1, ++iFi2)
316  {
317  // We sort vertex by index to input only edges (a,b) where
318  // a < b. This is to ensure that the same edge is not
319  // input twice as (a,b) and (b,a).
320  //res.insert((*iFi1 < *iFi2 ? Edge(*iFi1, *iFi2) : Edge(*iFi2, *iFi1)));
321  res.insert(til::sortedVector<Edge>(*iFi1, *iFi2));
322  }
323  }
324  }
325  };
326 
327  template < typename TParam >
328  std::set<numeric_array<typename TParam::FaceIndex::value_type,2>,
329  Lexicographical_compare<numeric_array<typename TParam::FaceIndex::value_type,2> > >
330  getEdges(const Mesh<TParam> & mesh)
331  {
332  typedef typename TParam::FaceIndex::value_type VertexIndex;
333  typedef numeric_array<VertexIndex,2> Edge;
334  std::set<Edge, Lexicographical_compare<Edge> > res;
335  typename TParam::FaceIndexCollection::const_iterator iFic = getFaceIndices(mesh).begin();
336  for (; iFic != getFaceIndices(mesh).end(); ++iFic)
337  {
338  typename TParam::FaceIndex::const_iterator iFi1 = iFic->begin();
339  const_cyclic_iterator<typename TParam::FaceIndex> iFi2(*iFic, iFi1+1);
340  //const_cyclic_iterator<typename MeshTraits<Mesh<TParam> >::FaceIndex> iFi2(iFic, iFi1+1);
341  //const_cyclic_iterator<typename TParam::FaceIndexCollection::value_type> iFi2(iFic, iFi1+1);
342  //const_cyclic_iterator<boost::array<Point<float,3>*, 3> > iFi2(iFic, iFi1+1);
343  for (;iFi1 != iFic->end(); ++iFi1, ++iFi2)
344  {
345  // We sort vertex by index to input only edges (a,b) where
346  // a < b. This is to ensure that the same edge is not
347  // input twice as (a,b) and (b,a).
348  //res.insert((*iFi1 < *iFi2 ? Edge(*iFi1, *iFi2) : Edge(*iFi2, *iFi1)));
349  res.insert(til::sortedVector<Edge>(*iFi1, *iFi2));
350  }
351  }
352  return res;
353  }
354 
355  template < typename TResult, typename TParam >
356  void
357  getEdges(const Mesh<TParam> & mesh, TResult & res)
358  {
359  MeshEdgeExtractor<Mesh<TParam>, TResult> edgeExtractor;
360  edgeExtractor(mesh, res);
361  }
362  */
363 
365 
366  template < >
367  struct MeshTraits<Mesh_N> : public MeshTraits<Mesh1>
368  {
369  // constants
370  enum { has_neighbor_indices = 1 };
371 
372  // typedefs
373  typedef std::vector<std::size_t> NeighborIndex;
374  typedef std::vector<NeighborIndex> NeighborIndexCollection;
375  };
376 
377 
378  inline
381  {
382  return mesh.getNeighborIndices();
383  }
384 
385  inline
388  {
389  return mesh.getNeighborIndices();
390  }
391 
392  template < >
393  struct MeshTraits<Mesh2_N> : public MeshTraits<Mesh2>
394  {
395  // constants
396  enum { has_neighbor_indices = 1 };
397 
398  // typedefs
401  };
402 
403 
404  inline
407  {
408  return mesh.getNeighborIndices();
409  }
410 
411  inline
414  {
415  return mesh.getNeighborIndices();
416  }
417 
418 
420  // Adding and removing attributes to a mesh
422 
423 
426  // TODO: right now this kind of stuff is really a pain in the asset, and
427  // it will only get more painful as the number of possible attribute grows, but
428  // I can't really find an alternative where all of this could be done
429  // automatically. Namely, the problem is to have only one possible class for a
430  // given set of added attributes, i.e. we don't want to have both AddNormal<AddNeighbors>
431  // and AddNeighbors<AddNormal>. Even more important, we don't want to add twice
432  // the same attributes, like AddNormal<AddNeighbors<AddNormal>>.
433  template < class TMesh >
434  struct MeshAttributes
435  {
436  };
437 
439  /*
440  template <typename TMeshParam>
441  struct MeshAttributes<Mesh<TMeshParam> >
442  {
443  typedef typename detail::AddNeighborIndiceAttribute<Mesh<TMeshParam> > AddNeighborIndices;
444  typedef typename Mesh<TMeshParam> RemoveNeighborIndices;
445  typedef typename detail::AddNormalAttribute<Mesh<TMeshParam> > AddNormals;
446  typedef typename Mesh<TMeshParam> RemoveNormals;
447  };
448  */
449 
451  template <>
453  {
458  };
459 
460  template <>
462  {
467  };
468 
469  template <>
471  {
476  };
477 
478 
480 
481 
483  inline
486  (
487  const Mesh1 & mesh,
488  const std::vector<numeric_array<std::size_t, 3> >::const_iterator & iFC,
489  int i
490  )
491  {
492  return mesh.getVertices()[(*iFC)[i]];
493  }
494 
496  inline
499  (
500  const Mesh2 &,
502  int i
503  )
504  {
505  return *((*iFC)[i]);
506  }
507 
508 
510  inline
513  (
514  const Mesh_N & mesh,
516  )
517  {
518  return mesh.getVertices()[iNi];
519  }
520 
522  inline
523  const numeric_array<float,3> &
525  (
526  const Mesh2_N &,
528  )
529  {
530  return *iNi;
531  }
532 
533 
536  #define TIL_FOR_ALL_CONST_FACES(mesh) \
537  typename MeshTraits<TMesh>::FaceIndexCollection::const_iterator iFace; \
538  for (iFace = getFaceIndices(mesh).begin(); iFace != getFaceIndices(mesh).end(); ++iFace) \
539 
540  #define TIL_FOR_ALL_CONST_VERTICES(mesh) \
542  typename MeshTraits<TMesh>::VertexCollection::const_iterator iVertex; \
543  for (iVertex = getVertices(mesh).begin(); iVertex != getVertices(mesh).end(); ++iVertices) \
544 
545 } // namespace til
546 
547 #endif //_MESHTRAITS_H_
A class to represent a very basic mesh, consisting of a set of vertices and a set of edges...
const VertexCollection & getVertices() const
Definition: Mesh.h:63
This class enhance a mesh class with a normal vector attribute.
MeshTraits< Mesh_N >::NeighborIndexCollection & getNeighborIndices(Mesh_N &mesh)
Definition: MeshTraits.h:380
const NeighborIndexCollection & getNeighborIndices() const
Definition: Mesh.h:341
const FaceIndexCollection & getFaceIndices() const
Definition: Mesh.h:66
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
const Point3df & getFaceVertex(const AimsSurface< D, T > &mesh, const std::vector< AimsVector< unsigned int, 3 > >::const_iterator &iFC, int i)
Definition: aims_wrap.h:492
std::vector< std::size_t > NeighborIndex
Definition: MeshTraits.h:373
Mesh2_N::NeighborIndexCollection NeighborIndexCollection
Definition: MeshTraits.h:400
This file contains forward declarations of classes defined in the TIL library.
Mesh2_N::NeighborIndex NeighborIndex
Definition: MeshTraits.h:399
Default values for MeshTraits.
Definition: MeshTraits.h:24
A trait for knowing the return type of a Mesh when adding redundant attributes such as normals...
std::vector< NeighborIndex > NeighborIndexCollection
Definition: MeshTraits.h:374
const MeshTraits< Mesh_N >::Vertex & getVertexNeighbor(const Mesh_N &mesh, const MeshTraits< Mesh_N >::NeighborIndex::value_type &iNi)
Returns the vertex neighbors of a mesh of type Mesh_N given by its index.
Definition: MeshTraits.h:513
TParam::NeighborIndex NeighborIndex
Definition: Mesh.h:329
Traits for meshes.
const MeshTraits< AimsSurface< D, T > >::FaceIndexCollection & getFaceIndices(const AimsSurface< D, T > &mesh)
Definition: aims_wrap.h:505
This class enhance a mesh class with a neighbor index attribute.
TParam::NeighborIndexCollection NeighborIndexCollection
Definition: Mesh.h:330
const MeshTraits< AimsSurface< D, T > >::VertexCollection & getVertices(const AimsSurface< D, T > &mesh)
Definition: aims_wrap.h:474