MEPP2 Project
generic_writer.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>
15 #include "FEVV/Wrappings/Graph_traits_extension.h" // for size_of_edges()
18 
21 
24 #ifdef FEVV_USE_VTK
26 #endif
28 
29 
30 namespace FEVV {
31 namespace Filters {
32 
33 
45 template< typename HalfedgeGraph,
46  typename GeometryTraits >
47 void
48 write_mesh(const std::string &filename,
49  HalfedgeGraph &g,
50  PMapsContainer &pmaps,
51  const GeometryTraits &gt)
52 {
53  // TODO-elo templatize this 4 types ? Extract them from the mesh type ?
54  typedef double coordP_type; // position coordinate type
55  typedef double coordN_type; // normal coordinate type
56  typedef float coordC_type; // color coordinate type
57  typedef float coordT_type; // texture coordinate type
58  typedef size_t index_type;
59 
60 
61  if(!FEVV::FileUtils::has_extension(filename))
62  throw std::invalid_argument(
63  "write_mesh(): output file path has no extension.");
64 
65  std::cout << "Generic writing of \""
66  << FEVV::FileUtils::get_file_full_name(filename) << "\""
67  << std::endl;
68 
69  std::vector< std::string > valid_extensions = {".obj", ".off", ".coff",
70  /*".ply",*/ ".msh"};
71  std::vector< std::string > valid_vtk_extensions = {".vtk", ".vtp", ".vtu"};
72  if(!(FEVV::FileUtils::has_extension(filename, valid_extensions)
73 #ifdef FEVV_USE_VTK
74  || FEVV::FileUtils::has_extension(filename, valid_vtk_extensions)
75 #endif
76  ))
77  {
78  throw std::invalid_argument(
79  "write_mesh(): output file format not yet supported.");
80  }
81 
83 
84  // build the vector representation of the mesh
85 
86  FEVV::Types::MVR< coordP_type,
87  coordN_type,
88  coordT_type,
89  coordC_type,
90  index_type > mvr;
91 
92  mesh_to_vector_representation(g, pmaps, mvr, gt);
93 
94  // WRITE TO FILE
95 
96  if(FEVV::FileUtils::has_extension(filename, ".obj"))
97  {
98  IO::write_obj_file(filename,
99  mvr.points_coords,
100  mvr.normals_coords,
101  mvr.texture_coords,
102  mvr.vertex_color_coords,
103  mvr.faces_indices,
104  mvr.texture_face_indices,
105  mvr.normal_face_indices,
106  mvr.materials,
107  mvr.face_material);
108  }
109  else if(FEVV::FileUtils::has_extension(filename, ".off") ||
110  FEVV::FileUtils::has_extension(filename, ".coff"))
111  {
112  IO::write_off_file(filename,
113  mvr.points_coords,
114  mvr.normals_coords,
115  mvr.texture_coords,
116  mvr.vertex_color_coords,
117  mvr.faces_indices,
118  mvr.face_color_coords,
119  static_cast< unsigned long >(size_of_edges(g)));
120  }
121  else if(FEVV::FileUtils::has_extension(filename, ".ply"))
122  {
123  throw std::runtime_error("write_mesh(): ply writer not yet implemented");
124  }
125  else if(FEVV::FileUtils::has_extension(filename, ".msh"))
126  {
127  IO::write_gmsh_file(filename,
128  mvr.points_coords,
129  mvr.normals_coords,
130  mvr.points_colors,
131  mvr.lines_indices,
132  mvr.lines_colors,
133  mvr.faces_indices,
134  mvr.faces_colors,
135  mvr.field_attributes,
136  mvr.field_names);
137  }
138 #ifdef FEVV_USE_VTK
139  else if(FEVV::FileUtils::has_extension(filename, ".vtk") ||
140  FEVV::FileUtils::has_extension(filename, ".vtp") ||
141  FEVV::FileUtils::has_extension(filename, ".vtu"))
142  {
144  mvr.points_coords,
145  mvr.normals_coords,
146  mvr.points_colors,
147  mvr.lines_indices,
148  mvr.lines_colors,
149  mvr.faces_indices,
150  mvr.faces_colors,
151  mvr.field_attributes,
152  mvr.field_names);
153  }
154 #endif
155 
156  std::cout << "Generic writing of \""
157  << FEVV::FileUtils::get_file_full_name(filename) << "\" Done."
158  << std::endl;
159 }
160 
161 
171 template< typename HalfedgeGraph,
172  typename GeometryTraits = FEVV::Geometry_traits< HalfedgeGraph > >
173 void
174 write_mesh(const std::string &filename, HalfedgeGraph &g, PMapsContainer &pmaps)
175 {
176  GeometryTraits gt(g);
177  write_mesh< HalfedgeGraph, GeometryTraits >(filename, g, pmaps, gt);
178 }
179 
180 } // namespace Filters
181 } // namespace FEVV
FEVV::IO::write_off_file
void write_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, unsigned long nb_total_edges=0)
Definition: OffFileWriter.h:50
FEVV::Types::MVR
Definition: Mesh_vector_representation.h:32
mesh_to_vector_representation.hpp
FEVV::Geometry_traits< HalfedgeGraph >
ObjFileWriter.h
FEVV::IO::write_obj_file
void write_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: ObjFileWriter.h:196
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::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::write_vtk_or_vtp_or_vtu_file
void write_vtk_or_vtp_or_vtu_file(std::string file_path, const std::vector< std::vector< CoordType > > &points_coords, const std::vector< std::vector< CoordNType > > &normals_coords, const std::vector< std::vector< CoordCType > > &vertex_color_coords, const std::vector< std::vector< IndexType > > &line_indices, const std::vector< std::vector< CoordCType > > &lines_color_coords, const std::vector< std::vector< IndexType > > &face_indices, const std::vector< std::vector< CoordCType > > &face_color_coords, const std::vector< std::vector< std::vector< double > > > &field_attributes, const std::vector< std::string > &field_names)
Definition: VtkFileWriter.h:727
MshFileWriter.h
VtkFileWriter.h
Geometry_traits.h
FEVV::IO::write_gmsh_file
bool write_gmsh_file(const std::string &file_path, const std::vector< std::vector< CoordType > > &points_coords, const std::vector< std::vector< CoordNType > > &normals_coords, const std::vector< std::vector< CoordCType > > &vertex_color_coords, const std::vector< std::vector< IndexType > > &line_indices, const std::vector< std::vector< CoordCType > > &lines_color_coords, const std::vector< std::vector< IndexType > > &face_indices, const std::vector< std::vector< CoordCType > > &face_color_coords, const std::vector< std::vector< std::vector< double > > > &field_attributes, const std::vector< std::string > &field_names)
Definition: MshFileWriter.h:51
FEVV::Filters::write_mesh
void write_mesh(const std::string &filename, FEVV::CGALPointSet &g, PMapsContainer &pmaps)
Write mesh to file.
Definition: cgal_point_set_writer.hpp:42
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
Graph_traits_extension.h
FEVV::FileUtils::has_extension
bool has_extension(const std::string &file_name)
Definition: FileUtilities.hpp:58
properties.h
OffFileWriter.h
Mesh_vector_representation.h