triangularMesh.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2013 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef __TRIANGULARMESH_HH
22 #define __TRIANGULARMESH_HH 1
23 
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include <string>
30 #include <mia/core/iodata.hh>
31 #include <mia/core/ioplugin.hh>
32 #include <mia/core/iohandler.hh>
33 #include <mia/3d/vector.hh>
34 
35 #ifdef WIN32
36 # ifdef miamesh_EXPORTS
37 # define EXPORT_MESH __declspec(dllexport)
38 # else
39 # define EXPORT_MESH __declspec(dllimport)
40 # endif
41 #else
42 # ifdef __GNUC__
43 # define EXPORT_MESH __attribute__((visibility("default")))
45 # else
46 # define EXPORT_MESH
47 # endif
48 
49 #endif
50 
51 
53 
62 
63 public:
66  static const char *data_descr;
67 
68  typedef CTriangleMesh type;
69 
70 
71  typedef std::shared_ptr<CTriangleMesh> Pointer;
72 
74  enum EData {ed_none = 0,
75  ed_vertex = 1,
76  ed_normal = 2,
77  ed_color = 4,
78  ed_scale = 8};
79 
81  typedef std::vector<T3DVector<unsigned int> > CTrianglefield;
82  typedef std::vector<C3DFVector> CVertexfield;
83  typedef std::vector<C3DFVector> CNormalfield;
84  typedef std::vector<C3DFVector> CColorfield;
85  typedef std::vector<float> CScalefield;
86 
87  typedef std::shared_ptr<CTrianglefield > PTrianglefield;
88  typedef std::shared_ptr<CVertexfield > PVertexfield;
89  typedef std::shared_ptr<CNormalfield > PNormalfield;
90  typedef std::shared_ptr<CColorfield > PColorfield;
91  typedef std::shared_ptr<CScalefield > PScalefield;
92 
93  typedef CTrianglefield::value_type triangle_type;
94  typedef CVertexfield::value_type vertex_type;
95  typedef CNormalfield::value_type normal_type;
96  typedef CColorfield::value_type color_type;
97  typedef CScalefield::value_type scale_type;
98 
99  typedef CVertexfield::const_iterator const_vertex_iterator;
100  typedef CVertexfield::iterator vertex_iterator;
101 
102  typedef CTrianglefield::const_iterator const_triangle_iterator;
103  typedef CTrianglefield::iterator triangle_iterator;
104 
105  typedef CNormalfield::const_iterator const_normal_iterator;
106  typedef CNormalfield::iterator normal_iterator;
107 
108  typedef CScalefield::const_iterator const_scale_iterator;
109  typedef CScalefield::iterator scale_iterator;
110 
111  typedef CColorfield::const_iterator const_color_iterator;
112  typedef CColorfield::iterator color_iterator;
114 
116  CTriangleMesh();
117 
123  CTriangleMesh(const CTriangleMesh& orig);
124 
129  CTriangleMesh(int n_triangles, int n_vertices);
130 
138  CTriangleMesh(PTrianglefield triangles,
139  PVertexfield vertices,
140  PNormalfield normals,
141  PColorfield colors,
142  PScalefield scales);
143 
148  CTriangleMesh(PTrianglefield triangles, PVertexfield vertices);
149 
150 
152  CTriangleMesh *clone() const __attribute__((warn_unused_result));
153 
156  CTriangleMesh clone_connectivity()const;
157 
159  ~CTriangleMesh();
160 
164  static const std::string get_type_description();
165 
169  int get_available_data() const;
170 
174  const void *get_vertex_pointer()const;
175 
179  const void *get_normal_pointer()const;
180 
181 
185  const void *get_triangle_pointer()const;
186 
190  const void *get_color_pointer()const;
191 
192 
196  unsigned int triangle_size()const;
197 
201  unsigned int vertices_size()const;
202 
206  const triangle_type& triangle_at(unsigned int i)const;
207 
211  const vertex_type& vertex_at(unsigned int i)const;
212 
216  const normal_type& normal_at(unsigned int i)const;
217 
221  const scale_type& scale_at(unsigned int i)const;
222 
227  const color_type& color_at(unsigned int i)const;
228 
230  const_triangle_iterator triangles_begin() const;
231  triangle_iterator triangles_begin();
232  const_triangle_iterator triangles_end() const;
233  triangle_iterator triangles_end();
234 
235  const_vertex_iterator vertices_begin()const;
236  const_vertex_iterator vertices_end()const;
237  vertex_iterator vertices_begin();
238  vertex_iterator vertices_end();
239 
240  const_normal_iterator normals_begin()const;
241  const_normal_iterator normals_end()const;
242  normal_iterator normals_begin();
243  normal_iterator normals_end();
244 
245  const_scale_iterator scale_begin()const;
246  const_scale_iterator scale_end()const;
247  scale_iterator scale_begin();
248  scale_iterator scale_end();
249 
250  const_color_iterator color_begin()const;
251  const_color_iterator color_end()const;
252  color_iterator color_begin();
253  color_iterator color_end();
255 
257  const std::string& get_sourceformat()const {
258  return m_sourceformat;
259  }
260 
262  void set_sourceformat(const std::string& fmt){
263  m_sourceformat = fmt;
264  }
265 
271  void evaluate_normals();
272 
273 private:
274  std::string m_sourceformat;
275  struct CTriangleMeshData * data;
276 };
277 
280 
288 template <class Deformation>
289 void colorize_mesh(CTriangleMesh *mesh, const Deformation& deform)
290 {
292  mesh->evaluate_normals();
293 
294  // make sure we use the const versions of some iterators to keep the sharing of
295  // vertex (and evl. normal) data
296  const CTriangleMesh& cmesh = *mesh;
297 
298 
299  CTriangleMesh::scale_iterator sb = mesh->scale_begin();
300  CTriangleMesh::scale_iterator se = mesh->scale_end();
301 
302  CTriangleMesh::const_vertex_iterator vb = cmesh.vertices_begin();
303  CTriangleMesh::const_normal_iterator nb = cmesh.normals_begin();
304 
305  while (sb != se)
306  *sb++ = dot(*nb++, deform.apply(*vb++));
307 
308 }
309 
310 
319 template <class Deformation>
320 CTriangleMesh colorize_mesh(const CTriangleMesh& mesh, const Deformation& deform)
321 {
322  CTriangleMesh result(mesh);
323  colorize_mesh(&result,deform);
324  return result;
325 }
326 
327 
328 /* evaluate the distance between two meshes, and set the scale accordingly
329  \param mesh this mesh will get the per vertex distance values in its scale
330  \param reference the mesh we want to know the distance from
331  void distance_transform(CTriangleMesh *mesh, const CTriangleMesh& reference);
332 */
333 
334 
337 
340 
341 
343 
344 #endif // __TRIANGULARMESH_HH