MEPP2 Project
generic_reader.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>
18 
21 
25 #ifdef FEVV_USE_VTK
27 #endif
28 #ifdef FEVV_USE_FBX
30 #endif
32 
33 #include <chrono> // for time measurement
34 
35 namespace FEVV {
36 namespace Filters {
37 
38 
46 inline double
48  std::chrono::time_point< std::chrono::steady_clock > &time_start)
49 {
50  auto time_now = std::chrono::steady_clock::now();
51  std::chrono::duration< double > duration = time_now - time_start;
52  time_start = time_now;
53 
54  return duration.count();
55 }
56 
57 
70 template< typename HalfedgeGraph,
71  typename GeometryTraits = FEVV::Geometry_traits< HalfedgeGraph > >
72 void
73 read_mesh(const std::string &filename,
74  HalfedgeGraph &g,
75  PMapsContainer &pmaps,
76  const GeometryTraits &gt, bool only_pts/*=false*/)
77 {
78  // TODO-elo-make templatize this 4 types ? Extract them from the mesh type ?
79  typedef double coordP_type; // position coordinate type
80  typedef double coordN_type; // normal coordinate type
81  typedef float coordC_type; // color coordinate type
82  typedef float coordT_type; // texture coordinate type
83  typedef size_t index_type;
84 
86  // check that input file exists and format is supported
87 
88  if(!boost::filesystem::exists(filename))
89  throw std::invalid_argument(
90  "read_mesh() -> input file path refer to no existing file.");
91  else if(!FEVV::FileUtils::has_extension(filename))
92  throw std::invalid_argument(
93  "read_mesh() -> input file path find without any extension.");
94 
95  std::cout << "Generic reading of \""
96  << FEVV::FileUtils::get_file_full_name(filename) << "\""
97  << std::endl;
98 
99  std::vector< std::string > valid_extensions = {
100  ".obj", ".off", ".coff", ".ply", ".msh"};
101  std::vector< std::string > valid_vtk_extensions = {".vtk", ".vtp", ".vtu"};
102  if(!(FEVV::FileUtils::has_extension(filename, valid_extensions)
103 #ifdef FEVV_USE_VTK
104  || FEVV::FileUtils::has_extension(filename, valid_vtk_extensions)
105 #endif
106 #ifdef FEVV_USE_FBX
107  || FEVV::FileUtils::has_extension(filename, ".fbx")
108 #endif
109  ))
110  {
111  throw std::invalid_argument(
112  "read_mesh() -> input file extension can't be read (yet).");
113  }
114 
116  // parse input file
117 
118  FEVV::Types::MVR< coordP_type,
119  coordN_type,
120  coordT_type,
121  coordC_type,
122  index_type > mvr;
123 
124  bool obj_file = false;
125 
126  auto time = std::chrono::steady_clock::now();
127 
128  if(FEVV::FileUtils::has_extension(filename, ".obj"))
129  {
130  IO::read_obj_file(filename,
131  mvr.points_coords,
132  mvr.normals_coords,
133  mvr.texture_coords,
134  mvr.vertex_color_coords,
135  mvr.faces_indices,
136  mvr.texture_face_indices,
137  mvr.normal_face_indices,
138  mvr.materials,
139  mvr.face_material);
140  obj_file = true;
141  }
142  else if(FEVV::FileUtils::has_extension(filename, ".off") ||
143  FEVV::FileUtils::has_extension(filename, ".coff"))
144  {
145  IO::read_off_file(filename,
146  mvr.points_coords,
147  mvr.normals_coords,
148  mvr.texture_coords,
149  mvr.vertex_color_coords,
150  mvr.faces_indices,
151  mvr.face_color_coords);
152  }
153  else if(FEVV::FileUtils::has_extension(filename, ".ply"))
154  {
155  IO::read_ply_file(filename,
156  mvr.points_coords,
157  mvr.normals_coords,
158  mvr.texture_coords,
159  mvr.vertex_color_coords,
160  mvr.faces_indices,
161  mvr.texture_face_indices);
162  }
163  else if(FEVV::FileUtils::has_extension(filename, ".msh"))
164  {
165  IO::read_gmsh_file(filename,
166  mvr.points_coords,
167  mvr.normals_coords,
168  mvr.vertex_color_coords,
169  mvr.lines_indices,
170  mvr.lines_colors,
171  mvr.faces_indices,
172  mvr.face_color_coords,
173  mvr.field_attributes,
174  mvr.field_names);
175  }
176 #ifdef FEVV_USE_VTK
177  else if(FEVV::FileUtils::has_extension(filename, ".vtk") ||
178  FEVV::FileUtils::has_extension(filename, ".vtp") ||
179  FEVV::FileUtils::has_extension(filename, ".vtu"))
180  {
182  mvr.points_coords,
183  mvr.normals_coords,
184  mvr.vertex_color_coords,
185  mvr.lines_indices,
186  mvr.lines_colors,
187  mvr.faces_indices,
188  mvr.face_color_coords,
189  mvr.field_attributes,
190  mvr.field_names);
191  }
192 #endif
193 #ifdef FEVV_USE_FBX
194  else if(FEVV::FileUtils::has_extension(filename, ".fbx"))
195  {
196  IO::read_fbx_file(filename,
197  mvr.points_coords,
198  mvr.normals_coords,
199  mvr.texture_coords,
200  mvr.vertex_color_coords,
201  mvr.faces_indices,
202  mvr.texture_face_indices,
203  mvr.normal_face_indices,
204  mvr.materials,
205  mvr.face_material);
206  }
207 #endif
208 
209  auto parsing_duration = get_time_and_reset(time);
210 
212  // populate mesh object
213 
214  if (only_pts)
215  {
216  //normals_coords.clear(); // we can also keep that...
217  mvr.texture_coords.clear();
218  //vertex_color_coords.clear();
219  mvr.face_color_coords.clear();
220  mvr.lines_indices.clear();
221  mvr.faces_indices.clear();
222  mvr.texture_face_indices.clear();
223  mvr.normal_face_indices.clear();
224  mvr.points_colors.clear();
225  mvr.faces_colors.clear();
226  mvr.lines_colors.clear();
227  mvr.field_attributes.clear();
228  mvr.field_names.clear();
229  mvr.materials.clear();
230  mvr.face_material.clear();
231  }
232 
233  unsigned int duplicated_vertices_nbr;
235 
237  pmaps,
238  duplicated_vertices_nbr,
239  mvr,
240  mfvr_params.use_corner_texcoord(obj_file),
241  gt);
242  // corner texture are supported with OBJ file only
243 
244  auto populating_duration = get_time_and_reset(time);
245 
247  // display some information
248 
249  if(duplicated_vertices_nbr > 0)
250  {
251  std::cout << "read_mesh(): " << duplicated_vertices_nbr
252  << " vertices duplicated (non-manifold mesh)." << std::endl;
253  }
254  std::cout << "read_mesh(): the mesh has " << size_of_vertices(g)
255  << " vertices, " << size_of_edges(g) << " edges, "
256  << size_of_faces(g) << " faces." << std::endl;
257  std::cout << "Generic reading of \""
258  << FEVV::FileUtils::get_file_full_name(filename) << "\" done"
259  << " in " << parsing_duration + populating_duration << "s"
260  << " (parsing " << parsing_duration << "s"
261  << ", populating DS " << populating_duration << "s)."
262  << std::endl;
263 }
264 
265 
276 template< typename HalfedgeGraph,
277  typename GeometryTraits = FEVV::Geometry_traits< HalfedgeGraph > >
278 void
279 read_mesh(const std::string &filename, HalfedgeGraph &g, PMapsContainer &pmaps, bool only_pts=false)
280 {
281  GeometryTraits gt(g);
282  read_mesh< HalfedgeGraph, GeometryTraits >(filename, g, pmaps, gt, only_pts);
283 }
284 
285 } // namespace Filters
286 } // namespace FEVV
FEVV::Types::MVR
Definition: Mesh_vector_representation.h:32
FEVV::Filters::MeshFromVectorReprParameters::use_corner_texcoord
MeshFromVectorReprParameters & use_corner_texcoord(bool _use_corner_texcoord)
Definition: mesh_from_vector_representation.hpp:96
FEVV::IO::read_ply_file
void read_ply_file(std::string file_path, std::vector< std::vector< CoordType > > &points_coords, std::vector< std::vector< CoordNType > > &normals_coords, std::vector< std::vector< CoordTType > > &texture_coords, std::vector< std::vector< CoordCType > > &vertex_color_coords, std::vector< std::vector< IndexType > > &face_indices, std::vector< std::vector< IndexType > > &texture_face_indices)
Definition: PlyFileReader.h:41
MshFileReader.h
FEVV::IO::read_vtk_or_vtp_or_vtu_file
void read_vtk_or_vtp_or_vtu_file(std::string file_path, std::vector< std::vector< CoordType > > &points_coords, std::vector< std::vector< CoordNType > > &normals_coords, std::vector< std::vector< CoordCType > > &vertex_color_coords, std::vector< std::vector< IndexType > > &line_indices, std::vector< std::vector< CoordCType > > &lines_color_coords, std::vector< std::vector< IndexType > > &face_indices, std::vector< std::vector< CoordCType > > &face_color_coords, std::vector< std::vector< std::vector< double > > > &field_attributes, std::vector< std::string > &field_names, bool if_unstructured_grid_then_take_surface=false)
Definition: VtkFileReader.h:929
VtkFileReader.h
FEVV::Filters::mesh_from_vector_representation
void mesh_from_vector_representation(HalfedgeGraph &g, FEVV::PMapsContainer &pmaps, unsigned int &dup_v_nbr, FEVV::Types::MVR< coordP_type, coordN_type, coordT_type, coordC_type, index_type > &mvr, MeshFromVectorReprParameters< HalfedgeGraph > const &params, const GeometryTraits &)
Build the mesh from the given vector representation.
Definition: mesh_from_vector_representation.hpp:142
FEVV::IO::read_fbx_file
void read_fbx_file(const std::string &filePath, std::vector< std::vector< coord_type > > &points_coords, std::vector< std::vector< coordN_type > > &normals_coords, std::vector< std::vector< coordT_type > > &texture_coords, std::vector< std::vector< coordC_type > > &, std::vector< std::vector< index_type > > &face_indices, std::vector< std::vector< index_type > > &texture_face_indices, std::vector< std::vector< index_type > > &normal_face_indices, std::vector< material_type > &materials, std::vector< index_type > &face_material)
Read the FBX file and load its data into given parameters.
Definition: FbxFileReader.h:176
FEVV::Geometry_traits< HalfedgeGraph >
FEVV::size_of_edges
boost::graph_traits< MeshT >::edges_size_type size_of_edges(const MeshT &g)
Real current number of edges of the mesh. Generic version.
Definition: Graph_traits_extension.h:46
FEVV::PMapsContainer
std::map< std::string, boost::any > PMapsContainer
Definition: properties.h:99
FEVV::Filters::MeshFromVectorReprParameters
Definition: mesh_from_vector_representation.hpp:92
FEVV::FileUtils::get_file_full_name
std::string get_file_full_name(const std::string &file_name)
Definition: FileUtilities.hpp:152
FEVV
Interfaces for plugins These interfaces will be used for different plugins.
Definition: Assert.h:16
FEVV::IO::read_off_file
void read_off_file(std::string file_path, std::vector< std::vector< CoordType > > &points_coords, std::vector< std::vector< CoordNType > > &normals_coords, std::vector< std::vector< CoordTType > > &texture_coords, std::vector< std::vector< CoordCType > > &vertex_color_coords, std::vector< std::vector< IndexType > > &face_indices, std::vector< std::vector< CoordCType > > &face_color_coords)
Definition: OffFileReader.h:40
FEVV::Filters::get_time_and_reset
double get_time_and_reset(std::chrono::time_point< std::chrono::steady_clock > &time_start)
Measure time since starting time and reset starting time.
Definition: generic_reader.hpp:47
FbxFileReader.h
FEVV::Filters::read_mesh
void read_mesh(const std::string &filename, FEVV::CGALPointSet &g, PMapsContainer &pmaps, bool=false)
Load mesh from file.
Definition: cgal_point_set_reader.hpp:110
Geometry_traits.h
mesh_from_vector_representation.hpp
OffFileReader.h
FEVV::IO::read_obj_file
void read_obj_file(const std::string &file_path, std::vector< std::vector< CoordType > > &points_coords, std::vector< std::vector< CoordNType > > &normals_coords, std::vector< std::vector< CoordTType > > &texture_coords, std::vector< std::vector< CoordCType > > &vertex_color_coords, std::vector< std::vector< IndexType > > &face_indices, std::vector< std::vector< IndexType > > &texture_face_indices, std::vector< std::vector< IndexType > > &normal_face_indices, std::vector< MaterialType > &materials, std::vector< IndexType > &face_material)
Definition: ObjFileReader.h:324
FEVV::size_of_faces
boost::graph_traits< MeshT >::faces_size_type size_of_faces(const MeshT &g)
Real current number of faces of the mesh. Generic version.
Definition: Graph_traits_extension.h:80
Graph_traits_extension.h
PlyFileReader.h
FEVV::FileUtils::has_extension
bool has_extension(const std::string &file_name)
Definition: FileUtilities.hpp:58
FEVV::IO::read_gmsh_file
bool read_gmsh_file(const std::string &file_path, std::vector< std::vector< CoordType > > &points_coords, std::vector< std::vector< CoordNType > > &normals_coords, std::vector< std::vector< CoordCType > > &vertex_color_coords, std::vector< std::vector< IndexType > > &line_indices, std::vector< std::vector< CoordCType > > &lines_color_coords, std::vector< std::vector< IndexType > > &face_indices, std::vector< std::vector< CoordCType > > &face_color_coords, std::vector< std::vector< std::vector< double > > > &field_attributes, std::vector< std::string > &field_names)
Definition: MshFileReader.h:427
properties.h
Mesh_vector_representation.h
FEVV::size_of_vertices
boost::graph_traits< MeshT >::vertices_size_type size_of_vertices(const MeshT &g)
Real current number of vertices of the mesh. Generic version.
Definition: Graph_traits_extension.h:29
ObjFileReader.h