From 5d3c5b5476baefdf8fa44bbbfa29743f831e3d3a Mon Sep 17 00:00:00 2001 From: Jane Tournois Date: Mon, 20 Mar 2017 16:52:10 +0100 Subject: [PATCH] avoid all the useless string manipulations and try to make the code easier to read --- .../include/CGAL/IO/read_surf_trianglemesh.h | 78 +++++-------------- 1 file changed, 21 insertions(+), 57 deletions(-) diff --git a/Polyhedron/demo/Polyhedron/include/CGAL/IO/read_surf_trianglemesh.h b/Polyhedron/demo/Polyhedron/include/CGAL/IO/read_surf_trianglemesh.h index 2efb0c5dddc..6193c1b0087 100644 --- a/Polyhedron/demo/Polyhedron/include/CGAL/IO/read_surf_trianglemesh.h +++ b/Polyhedron/demo/Polyhedron/include/CGAL/IO/read_surf_trianglemesh.h @@ -45,13 +45,9 @@ bool get_material_metadata(std::istream& input, bool line_starts_with(const std::string& line, const char* cstr) { - std::string line_copy = line; - std::size_t fnws = line_copy.find_first_not_of(" \t"); + const std::size_t fnws = line.find_first_not_of(" \t"); if (fnws != std::string::npos) - { - line_copy.erase(0, fnws); - return (line_copy.find(cstr) == 0); - } + return (line.compare(fnws, strlen(cstr), cstr) == 0); return false; } @@ -78,18 +74,11 @@ bool read_surf(std::istream& input, std::vector& output, //ignore header while(std::getline(input, line)) { - std::size_t fnws=line.find_first_not_of(" \t"); - if(fnws != std::string::npos) - line.erase(0, fnws); - if (line_starts_with(line, "Materials")) { while(std::getline(input, line)) { - std::size_t fnws=line.find_first_not_of(" \t"); - if(fnws != std::string::npos) - line.erase(0, fnws); - if(line.compare(0, 1, "}") == 0) + if(line_starts_with(line, "}")) break; else { @@ -106,16 +95,13 @@ bool read_surf(std::istream& input, std::vector& output, //get grid box while (std::getline(input, line)) { - line.erase(0, line.find_first_not_of(" \t")); - if (line.compare(0, 7, "GridBox") != 0) - continue; - else + if (line_starts_with(line, "GridBox")) { iss.clear(); - line.erase(0, 7); iss.str(line); + std::string dump; double xmin, xmax, ymin, ymax, zmin, zmax; - iss >> xmin >> xmax >> ymin >> ymax >> zmin >> zmax; + iss >> dump >> xmin >> xmax >> ymin >> ymax >> zmin >> zmax; grid_box = CGAL::Bbox_3(xmin, ymin, zmin, xmax, ymax, zmax); break; } @@ -124,15 +110,12 @@ bool read_surf(std::istream& input, std::vector& output, //get grid size while (std::getline(input, line)) { - line.erase(0, line.find_first_not_of(" \t")); - if (line.compare(0, 8, "GridSize") != 0) - continue; - else + if (line_starts_with(line, "GridSize")) { iss.clear(); - line.erase(0, 8); iss.str(line); - iss >> grid_size[0] >> grid_size[1] >> grid_size[2]; + std::string dump; + iss >> dump >> grid_size[0] >> grid_size[1] >> grid_size[2]; break; } } @@ -140,12 +123,7 @@ bool read_surf(std::istream& input, std::vector& output, //get number of vertices while(std::getline(input, line)) { - std::size_t fnws=line.find_first_not_of(" \t"); - if(fnws != std::string::npos) - line.erase(0, fnws); - if(line.compare(0, 8, "Vertices") != 0) - continue; - else + if (line_starts_with(line, "Vertices")) { iss.clear(); iss.str(line); @@ -158,14 +136,9 @@ bool read_surf(std::istream& input, std::vector& output, //get vertices while(std::getline(input, line)) { - std::size_t fnws=line.find_first_not_of(" \t"); - if(fnws != std::string::npos) - line.erase(0, fnws); - double x(0),y(0),z(0); - if(line.compare(0, 16, "NBranchingPoints") == 0) - { + if (line_starts_with(line, "NBranchingPoints")) break; - } + double x(0),y(0),z(0); iss.clear(); iss.str(line); iss >> CGAL::iformat(x) >> CGAL::iformat(y) >> CGAL::iformat(z); @@ -176,12 +149,7 @@ bool read_surf(std::istream& input, std::vector& output, //get number of patches while(std::getline(input, line)) { - std::size_t fnws=line.find_first_not_of(" \t"); - if(fnws != std::string::npos) - line.erase(0, fnws); - if(line.compare(0, 7, "Patches") != 0) - continue; - else + if (line_starts_with(line, "Patches")) { iss.clear(); iss.str(line); @@ -199,12 +167,7 @@ bool read_surf(std::istream& input, std::vector& output, //get metada while(std::getline(input, line)) { - std::size_t fnws=line.find_first_not_of(" \t"); - if(fnws != std::string::npos) - line.erase(0, fnws); - if(line.compare(0, 11, "InnerRegion") != 0) - continue; - else + if (line_starts_with(line, "InnerRegion")) { std::string name; iss.clear(); @@ -235,15 +198,15 @@ bool read_surf(std::istream& input, std::vector& output, } } + std::size_t nb_triangles(0); while(std::getline(input, line)) { - std::size_t fnws=line.find_first_not_of(" \t"); - if(fnws != std::string::npos) - line.erase(0, fnws); - if(line.compare(0, 9, "Triangles") != 0) - continue; - else + if (line_starts_with(line, "Triangles")) { + iss.clear(); + iss.str(line); + std::string dump; + iss >> dump >> nb_triangles; break; } } @@ -267,6 +230,7 @@ bool read_surf(std::istream& input, std::vector& output, std::vector polygon = {index[0] - 1, index[1] - 1, index[2] - 1}; polygons.push_back(polygon); } + CGAL_assertion(nb_triangles == polygons.size()); //build patch namespace PMP = CGAL::Polygon_mesh_processing;