MEPP2 Project
mesh_to_vector_representation.hpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as
6 // published by the Free Software Foundation; either version 3 of
7 // the License, or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 #pragma once
12 
13 #include <boost/graph/graph_traits.hpp>
14 #include <boost/graph/properties.hpp>
17 
19 
20 
21 namespace FEVV {
22 namespace Filters {
23 
24 
36 template< typename FaceListGraph,
37  typename coordP_type,
38  typename coordN_type,
39  typename coordT_type,
40  typename coordC_type,
41  typename index_type,
42  typename GeometryTraits >
43 void
44 mesh_to_vector_representation(const FaceListGraph &g,
45  const FEVV::PMapsContainer &pmaps,
46  FEVV::Types::MVR < coordP_type,
47  coordN_type,
48  coordT_type,
49  coordC_type,
50  index_type > &mvr,
51  const GeometryTraits &/*gt*/)
52 {
53  typedef boost::graph_traits< FaceListGraph > GraphTraits;
55  typedef typename GraphTraits::vertex_iterator vertex_iterator;
56  typedef boost::iterator_range< vertex_iterator > vertex_range;
57  //typedef typename GraphTraits::edge_descriptor edge_descriptor;
58  typedef typename GraphTraits::halfedge_descriptor halfedge_descriptor;
59  //typedef typename GraphTraits::face_descriptor face_descriptor;
60  typedef typename GraphTraits::face_iterator face_iterator;
61  typedef boost::iterator_range< face_iterator > face_range;
62  typedef typename GeometryTraits::Point Point;
63  typedef typename GeometryTraits::Vector Vector;
64 
65  // which vertex attribute are available for export ?
66 
67  bool use_vertex_normal = false;
68  bool use_vertex_color = false;
69  bool use_vertex_texture_coord = false;
70  bool use_halfedge_texture_coord = false;
71  bool use_face_color = false;
72  bool use_face_normal = false; // per-face vertices normals
73  bool use_face_material = false;
74  bool use_mesh_materials = false;
75 
76  if(has_map(pmaps, vertex_normal))
77  use_vertex_normal = true;
78  else if(has_map(pmaps, face_normal))
79  use_face_normal = true;
80  if(has_map(pmaps, vertex_color))
81  use_vertex_color = true;
82  if(has_map(pmaps, face_color))
83  use_face_color = true;
84  if(has_map(pmaps, vertex_texcoord))
85  use_vertex_texture_coord = true;
86  else if(has_map(pmaps, halfedge_texcoord))
87  use_halfedge_texture_coord = true;
88  if(has_map(pmaps, FEVV::face_material))
89  use_face_material = true;
90  if(has_map(pmaps, FEVV::mesh_materials))
91  use_mesh_materials = true;
92 
93  long vertex_index = 0; // to be sure to start to 0
94  std::map< vertex_descriptor, long >
95  index_map; // for correct corresponding between vertex order in file and
96  // vertices around face indices
97 
98  // PUT VERTICES IN CONTAINER
99 
100  vertex_range v_range = vertices(g);
101  auto point_pm = get(boost::vertex_point, g);
102 
103  // loop over vertices
104  for(vertex_iterator it_v = v_range.begin(); it_v != v_range.end(); ++it_v)
105  {
106  Point p = get(point_pm, *it_v);
107  std::vector< coordP_type > point;
108 
109  point.push_back(p[0]);
110  point.push_back(p[1]);
111  point.push_back(p[2]);
112 
113  mvr.points_coords.push_back(point);
114 
115  index_map[*it_v] = vertex_index;
116  ++vertex_index;
117 
118  // deal with vertex attributes
119 
120  if(use_vertex_normal)
121  {
122  // store vertex normal in standard container
123  auto vn_pm = get_property_map(FEVV::vertex_normal, g, pmaps);
124  auto vn = get(vn_pm, *it_v);
125  std::vector< coordN_type > normal;
126  normal.push_back(vn[0]);
127  normal.push_back(vn[1]);
128  normal.push_back(vn[2]);
129  mvr.normals_coords.push_back(normal);
130  }
131 
132  if(use_vertex_color)
133  {
134  // store vertex color in standard container
135  auto vc_pm = get_property_map(FEVV::vertex_color, g, pmaps);
136  auto vc = get(vc_pm, *it_v);
137  std::vector< coordC_type > color;
138  color.push_back(static_cast< coordC_type >(vc[0]));
139  color.push_back(static_cast< coordC_type >(vc[1]));
140  color.push_back(static_cast< coordC_type >(vc[2]));
141  mvr.vertex_color_coords.push_back(color);
142  }
143 
144  if(use_vertex_texture_coord)
145  {
146  // store vertex texture-coordinates in standard container
147  auto vt_pm = get_property_map(FEVV::vertex_texcoord, g, pmaps);
148  auto vt = get(vt_pm, *it_v);
149  // vt = (uvx, uvy, texture_index)
150  // do NOT store texture index in output file
151  std::vector< coordT_type > uvi;
152  uvi.push_back(static_cast< coordT_type >(vt[0]));
153  uvi.push_back(static_cast< coordT_type >(vt[1]));
154  mvr.texture_coords.push_back(uvi);
155  }
156  }
157 
158  // PUT FACES IN CONTAINER
159 
160  // loop over faces
161  face_range f_range = faces(g);
162  for(face_iterator it_f = f_range.begin(); it_f != f_range.end(); ++it_f)
163  {
164  std::vector< index_type > face;
165  std::vector< index_type > normal_indices;
166  std::vector< index_type > texture_indices;
167 
168  // loop over vertices incident to face
169  halfedge_descriptor h = halfedge(*it_f, g);
170  vertex_descriptor vbegin = target(h, g);
171  vertex_descriptor v = vbegin;
172  unsigned int face_vertices_nbr = 0;
173  do
174  {
175  face.push_back(
176  index_map[v]); // for correct corresponding between vertex order in
177  // file and vertices around face indices
178  face_vertices_nbr++;
179 
180  // face's vertices attributes (normal & texture)
181  if(use_vertex_normal)
182  normal_indices.push_back(index_map[v]);
183  if(use_vertex_texture_coord)
184  texture_indices.push_back(index_map[v]);
185  else if(use_halfedge_texture_coord)
186  {
187  // retrieve halfedge texture-coordinates from property map
188  auto ht_pm = get_property_map(FEVV::halfedge_texcoord, g, pmaps);
189  auto ht = get(ht_pm, h);
190 
191  // store halfedge texture-coordinates in standard container
192  // ht = (uvx, uvy, texture_index)
193  // do NOT store texture index in output file
194  std::vector< coordT_type > uvi;
195  uvi.push_back(static_cast< coordT_type >(ht[0]));
196  uvi.push_back(static_cast< coordT_type >(ht[1]));
197  mvr.texture_coords.push_back(uvi);
198 
199  // store halfedge texture-coordinates index in standard container
200  texture_indices.push_back(
201  static_cast< index_type >(mvr.texture_coords.size() - 1));
202  }
203 
204  // next vertex in the face
205  h = next(h, g);
206  v = target(h, g);
207  } while(v != vbegin);
208 
209  // face attribute (normal)
210  if(use_face_normal)
211  {
212  // retrieve face-normal from property map
213  auto fn_pm = get_property_map(FEVV::face_normal, g, pmaps);
214  auto fn = get(fn_pm, *it_f);
215 
216  // store face-normal in standard container
217  std::vector< coordN_type > normal;
218  normal.push_back(fn[0]);
219  normal.push_back(fn[1]);
220  normal.push_back(fn[2]);
221  mvr.normals_coords.push_back(normal);
222 
223  // store normal index in standard container for each face vertex
224  for(unsigned int i = 0; i < face_vertices_nbr; i++)
225  normal_indices.push_back(
226  static_cast< index_type >(mvr.normals_coords.size() - 1));
227  }
228 
229  mvr.faces_indices.push_back(face);
230  if(use_vertex_normal || use_face_normal)
231  mvr.normal_face_indices.push_back(normal_indices);
232  if(use_vertex_texture_coord || use_halfedge_texture_coord)
233  mvr.texture_face_indices.push_back(texture_indices);
234 
235  // face color
236  if(use_face_color)
237  {
238  // retrieve face-color from property map
239  auto fc_pm = get_property_map(FEVV::face_color, g, pmaps);
240  Vector fc = get(fc_pm, *it_f);
241 
242  // store face-color in standard container
243  std::vector< coordC_type > color;
244  color.push_back(static_cast< coordC_type >(fc[0]));
245  color.push_back(static_cast< coordC_type >(fc[1]));
246  color.push_back(static_cast< coordC_type >(fc[2]));
247  mvr.face_color_coords.push_back(color);
248  }
249 
250  // face material
251  if(use_face_material)
252  {
253  // retrieve face-color from property map
254  auto fm_pm = get_property_map(FEVV::face_material, g, pmaps);
255  auto material_id = get(fm_pm, *it_f);
256 
257  // store face-material in standard container
258  mvr.face_material.push_back(material_id);
259  }
260  }
261 
262  // PUT MATERIALS IN CONTAINER
263 
264  if(use_mesh_materials)
265  {
266  // loop over materials
267  auto mm_pm = get_property_map(FEVV::mesh_materials, g, pmaps);
268  auto it = mm_pm.storage_begin();
269  auto it_end = mm_pm.storage_end();
270  for(; it != it_end; ++it)
271  mvr.materials.push_back(*it);
272  }
273 }
274 
275 
285 template< typename FaceListGraph,
286  typename coordP_type,
287  typename coordN_type,
288  typename coordT_type,
289  typename coordC_type,
290  typename index_type,
291  typename GeometryTraits = FEVV::Geometry_traits< FaceListGraph > >
292 void
293 mesh_to_vector_representation(const FaceListGraph &g,
294  const FEVV::PMapsContainer &pmaps,
295  FEVV::Types::MVR < coordP_type,
296  coordN_type,
297  coordT_type,
298  coordC_type,
299  index_type > &mvr)
300 {
301  GeometryTraits gt(g);
302  mesh_to_vector_representation(g, pmaps, mvr, gt);
303 }
304 
305 
306 } // namespace Filters
307 } // namespace FEVV
FEVV::DataStructures::AIF::vertices
std::pair< typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::vertex_iterator, typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::vertex_iterator > vertices(const FEVV::DataStructures::AIF::AIFMesh &sm)
Returns the iterator range of the vertices of the mesh.
Definition: Graph_traits_aif.h:172
FEVV::DataStructures::AIF::next
boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::halfedge_descriptor next(typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::halfedge_descriptor h, const FEVV::DataStructures::AIF::AIFMesh &sm)
Returns the next halfedge around its face.
Definition: Graph_traits_aif.h:599
FEVV::Types::MVR
Definition: Mesh_vector_representation.h:32
Vector
AIFMesh::Vector Vector
Definition: Graph_properties_aif.h:22
FEVV::get_property_map
PMap_traits< PropertyT, MeshT >::pmap_type get_property_map(PropertyT p, const MeshT &, const PMapsContainer &pmaps)
Definition: properties.h:646
FEVV::vertex_texcoord
@ vertex_texcoord
Definition: properties.h:43
FEVV::mesh_materials
@ mesh_materials
Definition: properties.h:89
FEVV::has_map
bool has_map(const PMapsContainer &pmaps, const std::string &map_name)
(refer to Property Maps API)
Definition: properties.h:103
FEVV::Geometry_traits
Refer to Geometry_traits_documentation_dummy for further documentation on provided types and algorith...
Definition: Geometry_traits.h:162
Point
AIFMesh::Point Point
Definition: Graph_properties_aif.h:21
FEVV::PMapsContainer
std::map< std::string, boost::any > PMapsContainer
Definition: properties.h:99
FEVV::get
FEVV::PCLPointCloudPointMap::value_type get(const FEVV::PCLPointCloudPointMap &pm, FEVV::PCLPointCloudPointMap::key_type key)
Specialization of get(point_map, key) for PCLPointCloud.
Definition: Graph_properties_pcl_point_cloud.h:117
FEVV
Interfaces for plugins These interfaces will be used for different plugins.
Definition: Assert.h:16
FEVV::DataStructures::AIF::faces
std::pair< typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::face_iterator, typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::face_iterator > faces(const FEVV::DataStructures::AIF::AIFMesh &sm)
Returns an iterator range over all faces of the mesh.
Definition: Graph_traits_aif.h:679
FEVV::DataStructures::AIF::halfedge
boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::halfedge_descriptor halfedge(typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::vertex_descriptor v, const FEVV::DataStructures::AIF::AIFMesh &sm)
Returns a halfedge with target v.
Definition: Graph_traits_aif.h:296
FEVV::DataStructures::AIF::target
boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::vertex_descriptor target(typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::edge_descriptor e, const FEVV::DataStructures::AIF::AIFMesh &)
Returns the target vertex of e.
Definition: Graph_traits_aif.h:400
FEVV::vertex_color
@ vertex_color
Definition: properties.h:47
FEVV::face_material
@ face_material
Definition: properties.h:85
Geometry_traits.h
FEVV::DataStructures::AIF::AIFVector
Definition: AIFProperties.h:173
msdm2::vertex_descriptor
boost::graph_traits< MeshT >::vertex_descriptor vertex_descriptor
Definition: msdm2_surfacemesh.h:33
FEVV::Filters::mesh_to_vector_representation
void mesh_to_vector_representation(const FaceListGraph &g, const FEVV::PMapsContainer &pmaps, FEVV::Types::MVR< coordP_type, coordN_type, coordT_type, coordC_type, index_type > &mvr, const GeometryTraits &)
Build the vector representation of the mesh.
Definition: mesh_to_vector_representation.hpp:44
FEVV::face_color
@ face_color
Definition: properties.h:81
FEVV::DataStructures::AIF::face
boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::face_descriptor face(typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::halfedge_descriptor h, const FEVV::DataStructures::AIF::AIFMesh &)
Returns the face incident to halfedge h.
Definition: Graph_traits_aif.h:664
properties.h
FEVV::halfedge_texcoord
@ halfedge_texcoord
Definition: properties.h:69
FEVV::vertex_normal
@ vertex_normal
Definition: properties.h:35
Mesh_vector_representation.h
FEVV::DataStructures::AIF::AIFPoint
Definition: AIFProperties.h:31
FEVV::face_normal
@ face_normal
Definition: properties.h:77