-
Notifications
You must be signed in to change notification settings - Fork 25
/
generic_reader.hpp
286 lines (256 loc) · 9.75 KB
/
generic_reader.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (c) 2012-2019 University of Lyon and CNRS (France).
// All rights reserved.
//
// This file is part of MEPP2; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 3 of
// the License, or (at your option) any later version.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#pragma once
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/properties.hpp>
#include "FEVV/Wrappings/Graph_traits_extension.h"
#include "FEVV/Wrappings/Geometry_traits.h"
#include "FEVV/Wrappings/properties.h"
#include "FEVV/Types/Mesh_vector_representation.h"
#include "FEVV/Filters/Generic/mesh_from_vector_representation.hpp"
#include "FEVV/Tools/IO/ObjFileReader.h"
#include "FEVV/Tools/IO/OffFileReader.h"
#include "FEVV/Tools/IO/PlyFileReader.h"
#ifdef FEVV_USE_VTK
#include "FEVV/Tools/IO/VtkFileReader.h"
#endif
#ifdef FEVV_USE_FBX
#include "FEVV/Tools/IO/FbxFileReader.h"
#endif
#include "FEVV/Tools/IO/MshFileReader.h"
#include <chrono> // for time measurement
namespace FEVV {
namespace Filters {
/**
*
* \brief Measure time since starting time and reset starting time.
*
* \param Starting time
* \return Elapsed time in seconds since starting time.
*/
inline double
get_time_and_reset(
std::chrono::time_point< std::chrono::steady_clock > &time_start)
{
auto time_now = std::chrono::steady_clock::now();
std::chrono::duration< double > duration = time_now - time_start;
time_start = time_now;
return duration.count();
}
/**
* \brief Load mesh from file.
*
* \param filename name of the input mesh file
* \param g mesh object to store the mesh being read
* \param pmaps property maps bag to store the properties
* of the mesh being read
* \param gt the geometry traits to use
*
* \sa the simplified variant that use the default geometry traits
* of the mesh.
*/
template< typename HalfedgeGraph,
typename GeometryTraits = FEVV::Geometry_traits< HalfedgeGraph > >
void
read_mesh(const std::string &filename,
HalfedgeGraph &g,
PMapsContainer &pmaps,
const GeometryTraits >, bool only_pts/*=false*/)
{
// TODO-elo-make templatize this 4 types ? Extract them from the mesh type ?
typedef double coordP_type; // position coordinate type
typedef double coordN_type; // normal coordinate type
typedef float coordC_type; // color coordinate type
typedef float coordT_type; // texture coordinate type
typedef size_t index_type;
///////////////////////////////////////////////////////////////////////////
// check that input file exists and format is supported
if(!boost::filesystem::exists(filename))
throw std::invalid_argument(
"read_mesh() -> input file path refer to no existing file.");
else if(!FEVV::FileUtils::has_extension(filename))
throw std::invalid_argument(
"read_mesh() -> input file path find without any extension.");
std::cout << "Generic reading of \""
<< FEVV::FileUtils::get_file_full_name(filename) << "\""
<< std::endl;
std::vector< std::string > valid_extensions = {
".obj", ".off", ".coff", ".ply", ".msh"};
std::vector< std::string > valid_vtk_extensions = {".vtk", ".vtp", ".vtu"};
if(!(FEVV::FileUtils::has_extension(filename, valid_extensions)
#ifdef FEVV_USE_VTK
|| FEVV::FileUtils::has_extension(filename, valid_vtk_extensions)
#endif
#ifdef FEVV_USE_FBX
|| FEVV::FileUtils::has_extension(filename, ".fbx")
#endif
))
{
throw std::invalid_argument(
"read_mesh() -> input file extension can't be read (yet).");
}
///////////////////////////////////////////////////////////////////////////
// parse input file
FEVV::Types::MVR< coordP_type,
coordN_type,
coordT_type,
coordC_type,
index_type > mvr;
bool obj_file = false;
auto time = std::chrono::steady_clock::now();
if(FEVV::FileUtils::has_extension(filename, ".obj"))
{
IO::read_obj_file(filename,
mvr.points_coords,
mvr.normals_coords,
mvr.texture_coords,
mvr.vertex_color_coords,
mvr.faces_indices,
mvr.texture_face_indices,
mvr.normal_face_indices,
mvr.materials,
mvr.face_material);
obj_file = true;
}
else if(FEVV::FileUtils::has_extension(filename, ".off") ||
FEVV::FileUtils::has_extension(filename, ".coff"))
{
IO::read_off_file(filename,
mvr.points_coords,
mvr.normals_coords,
mvr.texture_coords,
mvr.vertex_color_coords,
mvr.faces_indices,
mvr.face_color_coords);
}
else if(FEVV::FileUtils::has_extension(filename, ".ply"))
{
IO::read_ply_file(filename,
mvr.points_coords,
mvr.normals_coords,
mvr.texture_coords,
mvr.vertex_color_coords,
mvr.faces_indices,
mvr.texture_face_indices);
}
else if(FEVV::FileUtils::has_extension(filename, ".msh"))
{
IO::read_gmsh_file(filename,
mvr.points_coords,
mvr.normals_coords,
mvr.vertex_color_coords,
mvr.lines_indices,
mvr.lines_colors,
mvr.faces_indices,
mvr.face_color_coords,
mvr.field_attributes,
mvr.field_names);
}
#ifdef FEVV_USE_VTK
else if(FEVV::FileUtils::has_extension(filename, ".vtk") ||
FEVV::FileUtils::has_extension(filename, ".vtp") ||
FEVV::FileUtils::has_extension(filename, ".vtu"))
{
IO::read_vtk_or_vtp_or_vtu_file(filename,
mvr.points_coords,
mvr.normals_coords,
mvr.vertex_color_coords,
mvr.lines_indices,
mvr.lines_colors,
mvr.faces_indices,
mvr.face_color_coords,
mvr.field_attributes,
mvr.field_names);
}
#endif
#ifdef FEVV_USE_FBX
else if(FEVV::FileUtils::has_extension(filename, ".fbx"))
{
IO::read_fbx_file(filename,
mvr.points_coords,
mvr.normals_coords,
mvr.texture_coords,
mvr.vertex_color_coords,
mvr.faces_indices,
mvr.texture_face_indices,
mvr.normal_face_indices,
mvr.materials,
mvr.face_material);
}
#endif
auto parsing_duration = get_time_and_reset(time);
///////////////////////////////////////////////////////////////////////////
// populate mesh object
if (only_pts)
{
//normals_coords.clear(); // we can also keep that...
mvr.texture_coords.clear();
//vertex_color_coords.clear();
mvr.face_color_coords.clear();
mvr.lines_indices.clear();
mvr.faces_indices.clear();
mvr.texture_face_indices.clear();
mvr.normal_face_indices.clear();
mvr.points_colors.clear();
mvr.faces_colors.clear();
mvr.lines_colors.clear();
mvr.field_attributes.clear();
mvr.field_names.clear();
mvr.materials.clear();
mvr.face_material.clear();
}
unsigned int duplicated_vertices_nbr;
MeshFromVectorReprParameters< HalfedgeGraph > mfvr_params;
mesh_from_vector_representation(g,
pmaps,
duplicated_vertices_nbr,
mvr,
mfvr_params.use_corner_texcoord(obj_file),
gt);
// corner texture are supported with OBJ file only
auto populating_duration = get_time_and_reset(time);
///////////////////////////////////////////////////////////////////////////
// display some information
if(duplicated_vertices_nbr > 0)
{
std::cout << "read_mesh(): " << duplicated_vertices_nbr
<< " vertices duplicated (non-manifold mesh)." << std::endl;
}
std::cout << "read_mesh(): the mesh has " << size_of_vertices(g)
<< " vertices, " << size_of_edges(g) << " edges, "
<< size_of_faces(g) << " faces." << std::endl;
std::cout << "Generic reading of \""
<< FEVV::FileUtils::get_file_full_name(filename) << "\" done"
<< " in " << parsing_duration + populating_duration << "s"
<< " (parsing " << parsing_duration << "s"
<< ", populating DS " << populating_duration << "s)."
<< std::endl;
}
/**
* \brief Load mesh from file.
*
* \param filename name of the input mesh file
* \param g mesh object to store the mesh being read
* \param pmaps property maps bag to store the properties
* of the mesh being read
*
* \sa the variant that use the geometry traits provided by the user.
*/
template< typename HalfedgeGraph,
typename GeometryTraits = FEVV::Geometry_traits< HalfedgeGraph > >
void
read_mesh(const std::string &filename, HalfedgeGraph &g, PMapsContainer &pmaps, bool only_pts=false)
{
GeometryTraits gt(g);
read_mesh< HalfedgeGraph, GeometryTraits >(filename, g, pmaps, gt, only_pts);
}
} // namespace Filters
} // namespace FEVV