aimsalgo  5.1.2
Neuroimaging image processing
mesh_graph.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 
35 #ifndef AIMS_MESH_GRAPH_H
36 #define AIMS_MESH_GRAPH_H
37 
38 #include <aims/vector/vector.h>
39 #include <cartobase/smart/rcptr.h>
40 #include <list>
41 #include <vector>
42 
43 namespace aims
44 {
45 
46  namespace meshgraph
47  {
48 
52  template < typename TMeshVertexNode >
53  struct MeshFaceNode
54  {
56  };
57 
58 
62  template < typename TAttribute >
64  {
65  public: // typedefs
66 
69  typedef std::list<Self> VertexCollection;
70  typedef typename VertexCollection::iterator VertexIndex;
71  typedef std::list<VertexIndex> VertexIndexCollection;
72 
74  typedef std::list<Face> FaceCollection;
75  typedef typename FaceCollection::iterator FaceIndex;
76  typedef std::list<FaceIndex> FaceIndexCollection;
77 
78  public: // set & get
79 
80  Vertex & pos() { return _pos; }
81  Vertex const & pos() const { return _pos; }
82 
83  VertexIndexCollection & neighbors() { return _neighbors; }
84  VertexIndexCollection const & neighbors() const { return _neighbors; }
85 
86  FaceIndexCollection & faces() { return _faces; }
87  FaceIndexCollection const & faces() const { return _faces; }
88 
89  TAttribute & attribute() { return _attribute; }
90  TAttribute const & attribute() const { return _attribute; }
91 
92  private: // data
93 
94  Vertex _pos;
95  VertexIndexCollection _neighbors;
96  FaceIndexCollection _faces;
97  TAttribute _attribute;
98  };
99 
100 
101  // we usually use these types
104  typedef std::list<MeshGraphVertex> MeshGraphVertices;
105  typedef std::list<MeshGraphFace> MeshGraphFaces;
106 
107 
108  //---------------------------//
109  // List2GraphMeshConvertor //
110  //---------------------------//
111 
115  template < typename TVertexNode, typename TFaceNode,
116  typename TVertexCollection, typename TFaceCollection,
117  typename TNeighborCollection >
119  {
120  public: // operators
121 
122  void operator()
123  (
124  TVertexCollection const & vertices,
125  TFaceCollection const & faceIndices,
126  std::vector<std::list<std::size_t> > const & invertedFaceIndices,
127  TNeighborCollection const & neighbors,
128  std::list<TVertexNode> & graph_vertices,
129  std::list<TFaceNode> & graph_faces
130  );
131 
132  public: // set & get
133 
134  private: // data, internal
135  std::vector<typename std::list<TVertexNode>::iterator> _index_vertex;
136  std::vector<typename std::list<TFaceNode>::iterator> _index_face;
137  };
138 
139 
140  //---------------------------//
141  // Graph2ListMeshConvertor //
142  //---------------------------//
143 
144  template < typename TMesh >
146  {
147  public: // typedefs
148 
149  typedef std::map<const void*, std::size_t> VertexIndexMap;
150 
151  public: // set & get
152 
154  = carto::rc_ptr<TMesh>( 0 ) )
155  : _mesh( mesh ? mesh : carto::rc_ptr<TMesh>( new TMesh ) ) {}
156 
157  VertexIndexMap & index_vertex() { return _index_vertex; }
158  carto::rc_ptr<TMesh> & mesh() { return _mesh; }
159 
160  public: // operators
161 
162  template < typename TVertexNode, typename TFaceNode >
163  void operator()
164  (
165  const std::list<TVertexNode> & graph_vertices,
166  const std::list<TFaceNode> & graph_faces
167  )
168  {
169  {
170  _mesh->vertex().resize( graph_vertices.size() );
171  std::size_t c = 0;
172  for (typename std::list<TVertexNode>::const_iterator i = graph_vertices.begin(); i != graph_vertices.end(); ++i)
173  {
174  _mesh->vertex()[c] = i->pos();
175  _index_vertex.insert(std::make_pair(static_cast<const void*>(&*i),c));
176  ++c;
177  }
178  }
179  {
180  int c = 0;
181  _mesh->polygon().resize( graph_faces.size() );
182  for (typename std::list<TFaceNode>::const_iterator i = graph_faces.begin(); i != graph_faces.end(); ++i)
183  {
184  for (int j = 0; j < 3; ++j)
185  _mesh->polygon()[c][j] = _index_vertex[static_cast<const void*>(&*(i->face[j]))];
186  ++c;
187  }
188  }
189  _mesh->normal().clear();
190  }
191 
192  private: // data
193  carto::rc_ptr<TMesh> _mesh;
194  VertexIndexMap _index_vertex;
195  };
196 
197 
198 
199  template < typename TIndexCollection >
200  inline
201  std::vector<std::list<std::size_t> >
202  invertIndices( const std::vector<TIndexCollection> & c )
203  {
204  // Do a first pass to get maximum index
205  typename TIndexCollection::value_type mx = 0;
206  for (std::size_t i = 0; i < c.size(); ++i)
207  for (std::size_t j = 0; j < c[i].size(); ++j)
208  mx = std::max( c[i][j], mx );
209 
210  // allocate result
211  std::vector<std::list<std::size_t> > res(mx+1);
212 
213  // Fill result
214  for (std::size_t i = 0; i < c.size(); ++i)
215  for (std::size_t j = 0; j < c[i].size(); ++j)
216  res[c[i][j]].push_back(i);
217 
218  return res;
219  }
220 
221  template < typename TFaceCollection >
222  carto::rc_ptr<std::vector<std::vector<
223  typename TFaceCollection::value_type::value_type> > >
224  circular_neighborhoods( TFaceCollection const & faces,
225  std::size_t nVertices );
226 
227  //-- shortcuts --//
228 
229  // TODO: this is wrong, because l2g is destroyed. EIther use smart pointers,
230  // OR pass along the
231  // reference to vertexOut/facesOut to l2g.
232 
233  template < typename TVerticesIn, typename TFacesIn, typename TNeighbors,
234  typename TVertexNode, typename TFaceNode >
235  inline
237  (
238  const TVerticesIn & verticesIn,
239  const TFacesIn & facesIn,
240  const TNeighbors & neighc,
241  std::list<TVertexNode> & verticesOut,
242  std::list<TFaceNode> & facesOut
243  )
244  {
245  std::vector<std::list<std::size_t> > faceIndices
246  = invertIndices(facesIn);
247  List2GraphMeshConvertor<TVertexNode, TFaceNode, TVerticesIn, TFacesIn,
248  TNeighbors> l2g;
249  l2g( verticesIn, facesIn, faceIndices, neighc, verticesOut, facesOut );
250  }
251 
252 
253  template < typename TVerticesIn, typename TFacesIn, typename TVertexNode,
254  typename TFaceNode >
255  inline
257  (
258  const TVerticesIn & verticesIn,
259  const TFacesIn & facesIn,
260  std::list<TVertexNode> & verticesOut,
261  std::list<TFaceNode> & facesOut
262  )
263  {
264  carto::rc_ptr<std::vector<std::vector<
265  typename TFacesIn::value_type::value_type > > > neighc
266  = circular_neighborhoods( facesIn, verticesIn.size() );
267  list2graph_mesh_conversion( verticesIn, facesIn, *neighc, verticesOut,
268  facesOut);
269  }
270 
271  }
272 
273 }
274 
275 #endif
276 
carto::rc_ptr< TMesh > & mesh()
Definition: mesh_graph.h:158
std::map< const void *, std::size_t > VertexIndexMap
Definition: mesh_graph.h:149
Graph2ListMeshConvertor(carto::rc_ptr< TMesh > mesh=carto::rc_ptr< TMesh >(0))
Definition: mesh_graph.h:153
Converts a AimsSurface mesh into a (vertexList, faceList) mesh graph.
Definition: mesh_graph.h:119
Mesh described as a graph, with nodes and edges.
Definition: mesh_graph.h:64
std::list< VertexIndex > VertexIndexCollection
Definition: mesh_graph.h:71
MeshFaceNode< Self > Face
Definition: mesh_graph.h:73
VertexCollection::iterator VertexIndex
Definition: mesh_graph.h:70
TAttribute const & attribute() const
Definition: mesh_graph.h:90
MeshVertexNode< TAttribute > Self
Definition: mesh_graph.h:67
FaceIndexCollection const & faces() const
Definition: mesh_graph.h:87
std::list< Self > VertexCollection
Definition: mesh_graph.h:69
VertexIndexCollection const & neighbors() const
Definition: mesh_graph.h:84
AimsVector< float, 3 > Vertex
Definition: mesh_graph.h:68
Vertex const & pos() const
Definition: mesh_graph.h:81
FaceCollection::iterator FaceIndex
Definition: mesh_graph.h:75
FaceIndexCollection & faces()
Definition: mesh_graph.h:86
std::list< Face > FaceCollection
Definition: mesh_graph.h:74
std::list< FaceIndex > FaceIndexCollection
Definition: mesh_graph.h:76
VertexIndexCollection & neighbors()
Definition: mesh_graph.h:83
float max(float x, float y)
Definition: thickness.h:98
void list2graph_mesh_conversion(const TVerticesIn &verticesIn, const TFacesIn &facesIn, const TNeighbors &neighc, std::list< TVertexNode > &verticesOut, std::list< TFaceNode > &facesOut)
Definition: mesh_graph.h:237
MeshFaceNode< MeshGraphVertex > MeshGraphFace
Definition: mesh_graph.h:103
std::list< MeshGraphFace > MeshGraphFaces
Definition: mesh_graph.h:105
MeshVertexNode< uint > MeshGraphVertex
Definition: mesh_graph.h:102
carto::rc_ptr< std::vector< std::vector< typename TFaceCollection::value_type::value_type > > > circular_neighborhoods(TFaceCollection const &faces, std::size_t nVertices)
Definition: mesh_graph_d.h:50
std::vector< std::list< std::size_t > > invertIndices(const std::vector< TIndexCollection > &c)
Definition: mesh_graph.h:202
std::list< MeshGraphVertex > MeshGraphVertices
Definition: mesh_graph.h:104
Mesh described as a graph, with nodes and edges.
Definition: mesh_graph.h:54
AimsVector< typename std::list< TMeshVertexNode >::iterator, 3 > face
Definition: mesh_graph.h:55