fix OFF surface_mesh reading

This commit is contained in:
Maxime Gimeno 2020-06-08 16:14:08 +02:00
parent 9199534373
commit 63c554fb7d
8 changed files with 57 additions and 66 deletions

View File

@ -146,11 +146,6 @@ bool read_OBJ(const char* fname,
bool verbose = true)
{
std::ifstream in(fname);
if(!in)
{
std::cerr<<"File doesn't exist"<<std::endl;
return false;
}
return read_OBJ(in, g, np, verbose);
}

View File

@ -157,10 +157,6 @@ bool read_OFF(const char* fname, FaceGraph& g, const CGAL_BGL_NP_CLASS& np,
bool verbose = true)
{
std::ifstream is(fname);
if(!is){
std::cerr<<"File doesn't exist."<<std::endl;
return false;
}
return read_OFF(is, g, np, verbose);
}

View File

@ -133,11 +133,6 @@ bool read_STL(const char* fname, FaceGraph& g, const CGAL_BGL_NP_CLASS& np,
bool verbose = true)
{
std::ifstream is(fname);
if(!is)
{
std::cerr<<"File doesn't exist."<<std::endl;
return false;
}
return read_STL(is, g, np, verbose);
}

View File

@ -754,32 +754,14 @@ template<>
void test_bgl_VTP<Polyhedron>(const char* filename,
const bool binary)
{
Polyhedron fg;
CGAL::make_tetrahedron(Point(0, 0, 0), Point(1, 1, 0),
Point(2, 0, 1), Point(3, 0, 0), fg);
typedef boost::property_map<Polyhedron, CGAL::dynamic_vertex_property_t<std::size_t> >::type VertexIdMap;
VertexIdMap vid = get(CGAL::dynamic_vertex_property_t<std::size_t>(), fg);
std::size_t id = 0;
for(auto v : vertices(fg))
put(vid, v, id++);
std::ofstream os("tetrahedron.vtp");
bool ok = CGAL::write_VTP(os, fg, CGAL::parameters::vertex_index_map(vid).use_binary_mode(binary));
assert(ok);
Polyhedron fg2;
ok = CGAL::read_polygon_mesh("tetrahedron.vtp", fg2);
assert(ok);
assert(are_equal_meshes(fg, fg2));
//todo same tests as the others.
}
#endif // CGAL_USE_VTK
int main(int argc, char** argv)
{
// OFF
/*
const char* off_file = (argc > 1) ? argv[1] : "data/prim.off";
test_bgl_OFF<Polyhedron>(off_file);
test_bgl_OFF<SM>(off_file);
@ -787,7 +769,7 @@ int main(int argc, char** argv)
#ifdef CGAL_USE_OPENMESH
test_bgl_OFF<OMesh>(off_file);
#endif
return 0; //tmp
// OBJ
const char* obj_file = (argc > 2) ? argv[2] : "data/sphere.obj";
test_bgl_OBJ<Polyhedron>(obj_file);
@ -814,7 +796,7 @@ int main(int argc, char** argv)
#ifdef CGAL_USE_OPENMESH
test_bgl_STL<OMesh>(stl_file);
#endif
*/
// GOCAD
const char* gocad_file = (argc > 5) ? argv[5] : "data/2016206_MHT_surface.ts";
test_bgl_GOCAD<Polyhedron>(gocad_file);
@ -825,6 +807,7 @@ int main(int argc, char** argv)
#endif
// VTP
/*
#ifdef CGAL_USE_VTK
const char* vtp_file = (argc > 6) ? argv[6] : "data/prim.off"; // @fixme put a VTP file
@ -836,6 +819,6 @@ int main(int argc, char** argv)
test_bgl_VTP<SM>(vtp_file, true);
test_bgl_VTP<LCC>(vtp_file, true);
#endif
*/
return EXIT_SUCCESS;
}

View File

@ -42,6 +42,11 @@ bool read_OBJ(std::istream& is,
VertexNormalOutputIterator vn_out,
bool verbose = true)
{
if(!is.good())
{
std::cerr<<"File doesn't exist"<<std::endl;
return false;
}
typedef typename boost::range_value<PointRange>::type Point;
typedef typename CGAL::Kernel_traits<Point>::Kernel Kernel;
typedef typename Kernel::Vector_3 Normal;
@ -167,11 +172,6 @@ bool read_OBJ(const char* fname, PointRange& points, PolygonRange& polygons,
const CGAL_BGL_NP_CLASS& np, bool verbose = true)
{
std::ifstream in(fname);
if(!in)
{
std::cerr<<"File doesn't exist."<<std::endl;
return false;
}
return read_OBJ(in, points, polygons, np, verbose);
}

View File

@ -60,8 +60,10 @@ bool read_OFF(std::istream& is,
CGAL_USE(verbose);
if(!is.good())
if(!is.good()){
std::cerr<<"File doesn't exist."<<std::endl;
return false;
}
CGAL::File_scanner_OFF scanner(is);
if(is.fail())
@ -185,11 +187,6 @@ bool read_OFF(const char* fname,
bool verbose = true)
{
std::ifstream in(fname);
if(!in)
{
std::cerr<<"File doesn't exist."<<std::endl;
return false;
}
return read_OFF(in, points, polygons, np, verbose);
}

View File

@ -39,8 +39,13 @@ bool read_STL(std::istream& is,
const CGAL_BGL_NP_CLASS& /*np*/, // might become useful one day for face normals
bool verbose = false)
{
int pos = 0;
if(!is.good())
{
std::cerr<<"File doesn't exist"<<std::endl;
return false;
}
int pos = 0;
// Ignore all initial whitespace
unsigned char c;

View File

@ -243,10 +243,14 @@ bool write_OFF_with_or_without_fcolors(std::ostream& os,
typedef CGAL::Color Color;
typename Mesh::template Property_map<Face_index, Color> fcolors;
bool has_fcolors;
std::tie(fcolors, has_fcolors) = sm.template property_map<Face_index, CGAL::Color>("f:color");
using parameters::choose_parameter;
using parameters::is_default_parameter;
using parameters::get_parameter;
const bool has_fcolors = !(is_default_parameter(get_parameter(np, internal_np::face_color_map)));
bool has_internal_fcolors;
std::tie(fcolors, has_internal_fcolors) = sm.template property_map<Face_index, CGAL::Color>("f:color");
if(has_fcolors && !std::distance(fcolors.begin(), fcolors.end()) > 0)
if(!has_fcolors && has_internal_fcolors && !std::distance(fcolors.begin(), fcolors.end()) > 0)
return write_OFF_BGL(os, sm, np.face_color_map(fcolors));
else
return write_OFF_BGL(os, sm, np);
@ -264,10 +268,14 @@ bool write_OFF_with_or_without_vtextures(std::ostream& os,
typedef typename K::Point_2 Texture;
typename Mesh::template Property_map<Vertex_index, Texture> vtextures;
bool has_vtextures;
std::tie(vtextures, has_vtextures) = sm.template property_map<Vertex_index, Texture>("v:texcoord");
using parameters::choose_parameter;
using parameters::is_default_parameter;
using parameters::get_parameter;
const bool has_vtextures = !(is_default_parameter(get_parameter(np, internal_np::vertex_texture_map)));
bool has_internal_vtextures;
std::tie(vtextures, has_internal_vtextures) = sm.template property_map<Vertex_index, Texture>("v:texcoord");
if(has_vtextures && !std::distance(vtextures.begin(), vtextures.end()) > 0)
if(!has_vtextures && has_internal_vtextures && !std::distance(vtextures.begin(), vtextures.end()) > 0)
return write_OFF_with_or_without_fcolors(os, sm, np.vertex_texture_map(vtextures));
else
return write_OFF_with_or_without_fcolors(os, sm, np);
@ -283,10 +291,16 @@ bool write_OFF_with_or_without_vcolors(std::ostream& os,
typedef CGAL::Color Color;
typename Mesh::template Property_map<Vertex_index, Color> vcolors;
bool has_vcolors;
std::tie(vcolors, has_vcolors) = sm.template property_map<Vertex_index, CGAL::Color>("v:color");
using parameters::choose_parameter;
using parameters::is_default_parameter;
using parameters::get_parameter;
const bool has_vcolors = !(is_default_parameter(get_parameter(np, internal_np::vertex_color_map)));
bool has_internal_vcolors;
if(has_vcolors && !std::distance(vcolors.begin(), vcolors.end()) > 0)
std::tie(vcolors, has_internal_vcolors) = sm.template property_map<Vertex_index, CGAL::Color>("v:color");
if(!has_vcolors && has_internal_vcolors && !std::distance(vcolors.begin(), vcolors.end()) > 0)
return write_OFF_with_or_without_vtextures(os, sm, np.vertex_color_map(vcolors));
else
return write_OFF_with_or_without_vtextures(os, sm, np);
@ -304,10 +318,14 @@ bool write_OFF_with_or_without_vnormals(std::ostream& os,
typedef typename K::Vector_3 Normal;
typename Mesh::template Property_map<Vertex_index, Normal> vnormals;
bool has_vnormals;
std::tie(vnormals, has_vnormals) = sm.template property_map<Vertex_index, Normal>("v:normal");
using parameters::choose_parameter;
using parameters::is_default_parameter;
using parameters::get_parameter;
const bool has_vnormals = !(is_default_parameter(get_parameter(np, internal_np::vertex_normal_map)));
bool has_internal_vnormals;
std::tie(vnormals, has_internal_vnormals) = sm.template property_map<Vertex_index, Normal>("v:normal");
if(has_vnormals && !std::distance(vnormals.begin(), vnormals.end()) > 0)
if(!has_vnormals && has_internal_vnormals && !std::distance(vnormals.begin(), vnormals.end()) > 0)
return write_OFF_with_or_without_vcolors(os, sm, np.vertex_normal_map(vnormals));
else
return write_OFF_with_or_without_vcolors(os, sm, np);
@ -329,12 +347,14 @@ bool write_OFF(std::ostream& os,
const Surface_mesh<Point>& sm,
const CGAL_BGL_NP_CLASS& np)
{
// Just to discard any excess named parameters
typename CGAL::GetVertexPointMap<Surface_mesh<Point>, CGAL_BGL_NP_CLASS>::const_type
vpm = parameters::choose_parameter(parameters::get_parameter(np, internal_np::vertex_point),
get_const_property_map(CGAL::vertex_point, sm));
using parameters::choose_parameter;
using parameters::is_default_parameter;
using parameters::get_parameter;
return IO::internal::write_OFF_with_or_without_vnormals(os, sm, np.vertex_point_map(vpm));
const bool has_vpoints= !(is_default_parameter(get_parameter(np, internal_np::vertex_point)));
if(has_vpoints)
return IO::internal::write_OFF_with_or_without_vnormals(os, sm, np);
return IO::internal::write_OFF_with_or_without_vnormals(os, sm, np.vertex_point_map(get_const_property_map(CGAL::vertex_point, sm)));
}
} // namespace CGAL