aimstil  5.0.5
Mesh.h
Go to the documentation of this file.
1 #ifndef _MESH_H_
2 #define _MESH_H_
3 
4 // includes from STL
5 #include <list>
6 #include <vector>
7 
8 // includes from BOOST
9 #include <boost/array.hpp>
10 #include <boost/shared_ptr.hpp>
11 #include <boost/type_traits.hpp>
12 
13 // includes from TIL
14 //#include "til/Point.h"
15 //#include "til/Vector3.h"
16 #include "til/numeric_array.h"
17 
18 // includes from TIL
19 //#include "til_common.h"
20 #include "globalTraits.h"
21 #include "miscUtils.h"
22 
23 // declarations
24 #include "til/til_declarations.h"
25 
26 
27 namespace til
28 {
29  using boost::shared_ptr;
30 
33  template < typename TMeshParam >
34  class Mesh : public TMeshParam
35  {
36  public: // typedefs
37 
38  // typedefs from TMeshParam
40  typedef typename TMeshParam::Vertex Vertex;
41  typedef typename TMeshParam::VertexCollection VertexCollection;
42  typedef typename TMeshParam::FaceIndex FaceIndex;
43  typedef typename TMeshParam::FaceIndexCollection FaceIndexCollection;
44 
45  public: // constructors & destructor
46 
52 
53  Mesh() : m_vertices(new VertexCollection()), m_faceIndices(new FaceIndexCollection()) {}
54 
55  /*
56  Mesh(const Mesh & mesh) :
57  m_vertices(new VertexCollection(*(mesh.m_vertices)))
58  , m_faceIndices(new FaceIndexCollection(*(mesh.m_faceIndices)))
59  */
60 
61  public: // set & get
62 
63  const VertexCollection & getVertices() const { return *m_vertices; }
64  VertexCollection & getVertices() { return *m_vertices; }
65 
66  const FaceIndexCollection & getFaceIndices() const { return *m_faceIndices; }
67  FaceIndexCollection & getFaceIndices() { return *m_faceIndices; }
68 
69  public: // functions
70 
71  void deepCopy(const Mesh & mesh)
72  {
73  // NB: the til:: is necessary to disambiguate with the this->deepCopy member function
74  //til::deepCopy(mesh.m_vertices, m_vertices);
75  //til::deepCopy(mesh.m_faceIndices, m_faceIndices);
76  //m_vertices->resize(size(*(mesh.m_vertices)));
77 
78  // TODO: this whole deepCopy crap sucks. First, if the new container is much smaller, are we sure we
79  // gain memory? Second, it does not work for pointers (see code below). An elegant solution has yet
80  // to be found for pointer indices. E.G. by letting this code be handled by an index collection class.
81 
82  *m_vertices = *mesh.m_vertices;
83  *m_faceIndices = *mesh.m_faceIndices;
84 
85  /*
86  //if (boost::is_same<typename value_type<FaceIndex>::type, std::size_t>::value)
87  if (boost::is_same<typename value_type_of<FaceIndex>::type, std::size_t>::value)
88  {
89  *m_faceIndices = *mesh.m_faceIndices;
90  }
91  else
92  {
93  m_faceIndices->resize(size(*(mesh.m_faceIndices)));
94  // *m_faceIndices = *(mesh.m_faceIndices);
95  for (std::size_t i = 0; i < size(*m_faceIndices); ++i)
96  {
97  for (std::size_t j = 0; j < size((*m_faceIndices)[i]); ++j)
98  {
99  (*m_faceIndices)[i][j] = (*mesh.m_faceIndices)[i][j]-&(*mesh.m_vertices)[0] + &(*m_vertices)[0];
100  }
101  }
102  }
103  */
104  }
105 
106  shared_ptr<VertexCollection> & vertices() { return m_vertices; }
107  const shared_ptr<VertexCollection> & vertices() const { return m_vertices; }
108  shared_ptr<FaceIndexCollection> & faces() { return m_faceIndices; }
109  const shared_ptr<FaceIndexCollection> & faces() const { return m_faceIndices; }
110 
111  private: //data
112 
113  // Vertices
114  shared_ptr<VertexCollection> m_vertices;
115 
116  // Faces as vertex indices
117  shared_ptr<FaceIndexCollection> m_faceIndices;
118  };
119 
120  /*
121  template < typename TVertex, typename TAttribute >
122  class XVertex
123  {
124 
125  private: //data
126  TVertex m_vertex;
127  TAttribute m_attributes;
128  //TAttribute * m_attributes;
129  };
130 
131  template < typename TMesh, typename TAttribute >
132  class AddVertexAttribute
133  {
134  };
135 
136 
137 
138  template < typename TMeshParam, typename TAttributeParam >
139  class AttributeMesh : public Mesh
140  {
141  public: // typedefs
142  typedef typename TAttributeParam::VertexAttributeCollection VertexAttributeCollection;
143  typedef typename TAttributeParam::FaceAttributeCollection FaceAttributeCollection;
144  typedef typename TAttributeParam::EdgeAttributeCollection EdgeAttributeCollection;
145 
146  public: // constructors & destructors
147 
148  private: // data
149 
150  shared_ptr< VertexAttributeCollection > m_vertexAttributes;
151  shared_ptr< FaceAttributeCollection > m_faceAttributes;
152  shared_ptr< EdgeAttributeCollection > m_edgeAttributes;
153  };
154 
155  template < typename TVertexAttribute, typename TFaceAttribute, typename TEdgeAttribute, template <typename> TContainer = std::vector >
156  struct MeshAttribute
157  {
158  typedef TVertexAttribute VertexAttribute;
159  typedef TFaceAttribute FaceAttribute;
160  typedef TEdgeAttribute EdgeAttribute;
161  typedef TContainer<VertexAttribute> VertexAttributeCollection;
162  typedef TContainer<FaceAttribute> FaceAttributeCollection;
163  typedef TContainer<EdgeAttribute> EdgeAttributeCollection;
164  };
165  */
166 
167 
168 
169 
170  /*
171  class MyMesh
172  {
173  public: // typedefs
174 
175  // typedefs from TMeshParam
176  typedef Point<float,3> Vertex;
177  typedef std::vector<Vertex> VertexCollection;
178  typedef boost::array<std::size_t, 3> FaceIndex;
179  typedef std::vector<FaceIndex> FaceIndexCollection;
180 
181  public: // constructors & destructor
182 
188  //Mesh() : m_vertices(new VertexCollection()), m_faceIndices(new FaceIndexCollection()) {}
189  MyMesh() {}
190 
191  public: // set & get
192 
193  / *
194  const VertexCollection & getVertices() const { return *m_vertices; }
195  VertexCollection & getVertices() { return *m_vertices; }
196 
197  const FaceIndexCollection & getFaceIndices() const { return *m_faceIndices; }
198  FaceIndexCollection & getFaceIndices() { return *m_faceIndices; }
199  * /
200 
201  const VertexCollection & getVertices() const { return m_vertices; }
202  VertexCollection & getVertices() { return m_vertices; }
203 
204  const FaceIndexCollection & getFaceIndices() const { return m_faceIndices; }
205  FaceIndexCollection & getFaceIndices() { return m_faceIndices; }
206 
207 
208  private: //data
209 
210  // Vertices
211  //boost::shared_ptr<VertexCollection> m_vertices;
212  VertexCollection m_vertices;
213 
214  // Faces as vertex indices
215  //boost::shared_ptr<FaceIndexCollection> m_faceIndices;
216  FaceIndexCollection m_faceIndices;
217  };
218  */
219 
220  struct MeshParam1
221  {
222  //typedef Point<float,3> Vertex;
224  //typedef Point3df Vertex;
225  //typedef Vector<float,3> Vertex;
226  //typedef boost::array<float,3> Vertex;
227  typedef std::vector<Vertex> VertexCollection;
229  typedef std::vector<FaceIndex> FaceIndexCollection;
230  };
231 
232  struct MeshParam2
233  {
235  //typedef Point<float,3> Vertex;
236  typedef std::vector<Vertex> VertexCollection;
238  typedef std::vector<FaceIndex> FaceIndexCollection;
239  };
240 
243 
244 
247  namespace detail
248  {
249 
252  template < class TParam >
253  struct DefaultAttributes {};
254 
257  //TODO: right now, vector is used by default => should change to sth dependent
258  // on TParam
259  template <typename TParam>
260  struct DefaultAttributes<Mesh<TParam> >
261  {
262  //typedef Vector<float,3> Normal;
264  typedef std::vector<Normal> NormalCollection;
265  typedef std::vector<typename TParam::FaceIndex::value_type> NeighborIndex;
266  typedef std::vector<NeighborIndex> NeighborIndexCollection;
267  };
268 
269  /*
270  struct AddNormalAttributeParams1
271  {
272  typedef Vector<float,3> Normal;
273  typedef std::vector<Normal> NormalCollection;
274  };
275  */
276 
281  template < class TMesh, typename TParam = DefaultAttributes<TMesh> >
282  class AddNormalAttribute : public TMesh
283  {
284  public: // typedefs
285 
286  typedef typename TParam::Normal Normal;
287  typedef typename TParam::NormalCollaction NormalCollection;
288 
289  public: // constructors & destructor
290 
291  AddNormalAttribute() : TMesh() {}
292  // NB: We have to add explicit here, because otherwise it's a source of bugs which
293  // are complicated to track. Without explicit, the mother can be passed silently
294  // as a children during a function call. This kind of precaution should
295  // always been taken, especially when a constructor exists with a
296  // parent as the only argument.
297  explicit AddNormalAttribute(const TMesh & mesh) : TMesh(mesh) {}
298  AddNormalAttribute(const TMesh & mesh,
299  shared_ptr<NormalCollection> & ni) : TMesh(mesh), m_normals(ni) {}
300 
301  public: // set & get
302 
303  const NormalCollection & getNormals() const { return *m_normals; }
304  NormalCollection & getNormals() { return *m_normals; }
305 
306  private: // data
307 
308  // Neighbors as vertex indices
310  };
311 
312  /*
313  struct AddNeighborAttributeParam1
314  {
315  typedef std::vector<std::size_t> NeighborIndex;
316  typedef std::vector<NeighborIndex> NeighborIndexCollection;
317  };
318  */
319 
324  template < class TMesh, typename TParam = DefaultAttributes<TMesh> >
325  class AddNeighborIndexAttribute : public TMesh
326  {
327  public: // typedefs
328 
329  typedef typename TParam::NeighborIndex NeighborIndex;
330  typedef typename TParam::NeighborIndexCollection NeighborIndexCollection;
331 
332  public: // constructors & destructor
333 
335  explicit AddNeighborIndexAttribute(const TMesh & mesh) : TMesh(mesh) {}
336  AddNeighborIndexAttribute(const TMesh & mesh,
337  shared_ptr<NeighborIndexCollection> & ni) : TMesh(mesh), m_neighborIndices(ni) {}
338 
339  public: // set & get
340 
341  const NeighborIndexCollection & getNeighborIndices() const { return *m_neighborIndices; }
342  NeighborIndexCollection & getNeighborIndices() { return *m_neighborIndices; }
343 
344  private: // data
345 
346  // Neighbors as vertex indices
347  shared_ptr<NeighborIndexCollection> m_neighborIndices;
348  };
349  }
350 
354 
358 
359 
360 
361  /*
362  template < typename TIterator, typename TAccessor >
363  class subiterator
364  {
365  };
366 
367  template < typename TNodeIterator >
368  class vertex_iterator
369  {
370  public: // constructors
371  vertex_iterator() : m_i() {}
372  vertex_iterator(TNodeIterator i) : m_i(i) {}
373  public: // operatoooors
374  void operator++() { ++m_i; }
375  vertex operator*() { return m_i->pos(); }
376  private: // data
377  TNodeIterator m_i;
378  };
379  */
380 
381 
382 } // namespace til
383 
384 #endif //_MESH_H_
const shared_ptr< VertexCollection > & vertices() const
Definition: Mesh.h:107
A class to represent a very basic mesh, consisting of a set of vertices and a set of edges...
numeric_array< float, 3 > Vertex
Definition: Mesh.h:234
std::vector< typename TParam::FaceIndex::value_type > NeighborIndex
Definition: Mesh.h:265
AddNormalAttribute(const TMesh &mesh, shared_ptr< NormalCollection > &ni)
Definition: Mesh.h:298
const VertexCollection & getVertices() const
Definition: Mesh.h:63
VertexCollection & getVertices()
Definition: Mesh.h:64
shared_ptr< FaceIndexCollection > & faces()
Definition: Mesh.h:108
This class enhance a mesh class with a normal vector attribute.
shared_ptr< VertexCollection > & vertices()
Definition: Mesh.h:106
const NeighborIndexCollection & getNeighborIndices() const
Definition: Mesh.h:341
const FaceIndexCollection & getFaceIndices() const
Definition: Mesh.h:66
detail::AddNeighborIndexAttribute< Mesh2 > Mesh2_N
Definition: Mesh.h:355
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
std::vector< Vertex > VertexCollection
Definition: Mesh.h:227
AddNormalAttribute(const TMesh &mesh)
Definition: Mesh.h:297
A structure giving default template parameters for the Add_XXX_Attribute classes, for a given Mesh...
Definition: Mesh.h:253
detail::AddNeighborIndexAttribute< Mesh1 > Mesh_N
Definition: Mesh.h:351
detail::AddNormalAttribute< Mesh_N > Mesh_NNo
Definition: Mesh.h:353
detail::AddNormalAttribute< Mesh2 > Mesh2_No
Definition: Mesh.h:356
Mesh()
Default constructor.
Definition: Mesh.h:53
boost::array< Vertex *, 3 > FaceIndex
Definition: Mesh.h:237
NormalCollection & getNormals()
Definition: Mesh.h:304
This file contains forward declarations of classes defined in the TIL library.
NeighborIndexCollection & getNeighborIndices()
Definition: Mesh.h:342
std::vector< FaceIndex > FaceIndexCollection
Definition: Mesh.h:229
Mesh< MeshParam1 > Mesh1
Definition: Mesh.h:241
detail::AddNormalAttribute< Mesh2_N > Mesh2_NNo
Definition: Mesh.h:357
FaceIndexCollection & getFaceIndices()
Definition: Mesh.h:67
TMeshParam::FaceIndexCollection FaceIndexCollection
Definition: Mesh.h:43
detail::AddNormalAttribute< Mesh1 > Mesh_No
Definition: Mesh.h:352
std::vector< FaceIndex > FaceIndexCollection
Definition: Mesh.h:238
AddNeighborIndexAttribute(const TMesh &mesh)
Definition: Mesh.h:335
void deepCopy(const Mesh &mesh)
Definition: Mesh.h:71
numeric_array< float, 3 > Vertex
Definition: Mesh.h:223
std::vector< NeighborIndex > NeighborIndexCollection
Definition: Mesh.h:266
TParam::NeighborIndex NeighborIndex
Definition: Mesh.h:329
TParam::NormalCollaction NormalCollection
Definition: Mesh.h:287
AddNeighborIndexAttribute(const TMesh &mesh, shared_ptr< NeighborIndexCollection > &ni)
Definition: Mesh.h:336
TMeshParam::VertexCollection VertexCollection
Definition: Mesh.h:41
const shared_ptr< FaceIndexCollection > & faces() const
Definition: Mesh.h:109
numeric_array< std::size_t, 3 > FaceIndex
Definition: Mesh.h:228
std::vector< Vertex > VertexCollection
Definition: Mesh.h:236
This class enhance a mesh class with a neighbor index attribute.
TMeshParam::Vertex Vertex
Definition: Mesh.h:40
TParam::NeighborIndexCollection NeighborIndexCollection
Definition: Mesh.h:330
numeric_array< float, 3 > Normal
Definition: Mesh.h:263
TMeshParam::FaceIndex FaceIndex
Definition: Mesh.h:42
Mesh< TMeshParam > Self
Definition: Mesh.h:39
TParam::Normal Normal
Definition: Mesh.h:286
const NormalCollection & getNormals() const
Definition: Mesh.h:303
Mesh< MeshParam2 > Mesh2
Definition: Mesh.h:242