diff --git a/GraphicsView/demo/Triangulation_2/Constrained_Delaunay_triangulation_2.cpp b/GraphicsView/demo/Triangulation_2/Constrained_Delaunay_triangulation_2.cpp index 809ca71bb53..5c09d1e3699 100644 --- a/GraphicsView/demo/Triangulation_2/Constrained_Delaunay_triangulation_2.cpp +++ b/GraphicsView/demo/Triangulation_2/Constrained_Delaunay_triangulation_2.cpp @@ -555,98 +555,32 @@ MainWindow::loadWKT(QString filename) { //Polygons todo : make it multipolygons std::ifstream ifs(qPrintable(filename)); - do - { - typedef CGAL::Polygon_with_holes_2 Polygon; - typedef CGAL::Point_2 Point; - std::deque points; - std::deque> linestrings; - std::deque polygons; + typedef CGAL::Polygon_with_holes_2 Polygon; + typedef CGAL::Point_2 Point; - CGAL::IO::read_WKT(ifs, points, linestrings, polygons); + std::deque points; + std::deque> linestrings; + std::deque polygons; - cdt.insert(points.begin(),points.end()); + CGAL::IO::read_WKT(ifs, points, linestrings, polygons); - for(const std::vector& line){ - cdt.insert_constraint(line.begin(), line.end()); + cdt.insert(points.begin(),points.end()); + + for(const std::vector& line : linestrings){ + cdt.insert_constraint(line.begin(), line.end()); + } + + for(const Polygon& p : polygons){ + if(p.outer_boundary().is_empty()) + continue; + + cdt.insert_constraint(p.outer_boundary().vertices_begin(), p.outer_boundary().vertices_end(),true); + + for(Polygon::Hole_const_iterator h_it = p.holes_begin(); h_it != p.holes_end(); ++h_it){ + cdt.insert_constraint(h_it->vertices_begin(); e_it != h_it->vertices_end(),true); } - - for(const Polygon& p : polygons) - { - if(p.outer_boundary().is_empty()) - continue; - - for(Point point : p.outer_boundary().container()) - cdt.insert(point); - for(Polygon::General_polygon_2::Edge_const_iterator - e_it=p.outer_boundary().edges_begin(); e_it != p.outer_boundary().edges_end(); ++e_it) - cdt.insert_constraint(e_it->source(), e_it->target()); - - for(Polygon::Hole_const_iterator h_it = - p.holes_begin(); h_it != p.holes_end(); ++h_it) - { - for(Point point : h_it->container()) - cdt.insert(point); - for(Polygon::General_polygon_2::Edge_const_iterator - e_it=h_it->edges_begin(); e_it != h_it->edges_end(); ++e_it) - { - cdt.insert_constraint(e_it->source(), e_it->target()); - } - } - } - }while(ifs.good() && !ifs.eof()); - //Edges - ifs.clear(); - ifs.seekg(0, ifs.beg); - do - { - typedef std::vector LineString; - std::vector mls; - CGAL::IO::read_multi_linestring_WKT(ifs, mls); - for(const LineString& ls : mls) - { - if(ls.empty()) - continue; - K::Point_2 p,q, qold(0,0); // initialize to avoid maybe-uninitialized warning from GCC6 - bool first = true; - CDT::Vertex_handle vp, vq, vqold; - LineString::const_iterator it = - ls.begin(); - for(; it != ls.end(); ++it) { - p = *it++; - q = *it; - if(p == q){ - continue; - } - if((!first) && (p == qold)){ - vp = vqold; - } else { - vp = cdt.insert(p); - } - vq = cdt.insert(q, vp->face()); - if(vp != vq) { - cdt.insert_constraint(vp,vq); - } - qold = q; - vqold = vq; - first = false; - } - } - }while(ifs.good() && !ifs.eof()); - - //Points - ifs.clear(); - ifs.seekg(0, ifs.beg); - do - { - std::vector mpts; - CGAL::IO::read_multi_point_WKT(ifs, mpts); - for(const K::Point_2& p : mpts) - { - cdt.insert(p); - } - }while(ifs.good() && !ifs.eof()); + } discoverComponents(cdt, m_seeds); Q_EMIT( changed()); diff --git a/Stream_support/include/CGAL/IO/WKT.h b/Stream_support/include/CGAL/IO/WKT.h index c56da5a4990..266939338fd 100644 --- a/Stream_support/include/CGAL/IO/WKT.h +++ b/Stream_support/include/CGAL/IO/WKT.h @@ -503,72 +503,74 @@ bool read_WKT(std::istream& is, if(!is.good()) return false; - do + while(is.good() && !is.eof()) { typedef typename MultiPoint::value_type Point; typedef typename MultiLineString::value_type LineString; typedef typename MultiPolygon::value_type Polygon; std::string line; - std::streampos input_pos = is.tellg(); std::getline(is, line); - std::istringstream iss(line); - std::string t; - std::string type=""; - iss >> t; - - for(std::size_t pos=0; pos < t.length(); ++pos) - { - char c = t[pos]; - if(c == '(') - break; - - type.push_back(c); + std::string::size_type header_end = line.find("("); // } + if(header_end == std::string::npos){ + continue; } + std::string type=""; + const std::string header = line.substr(0,header_end); + const std::string types[6] = { "MULTIPOLYGON", "MULTILINESTRING", "MULTIPOINT", "POLYGON", "LINESTRING", "POINT"}; + for(int i= 0; i < 6; ++i){ + if(header.find(types[i]) != std::string::npos){ + type = types[i]; + break; + } + } + if(type == ""){ + continue; + } + std::istringstream iss(line); - is.seekg(input_pos); if(type == "POINT") { Point p; - CGAL::IO::read_point_WKT(is, p); + CGAL::IO::read_point_WKT(iss, p); points.push_back(p); } else if(type == "LINESTRING") { LineString l; - CGAL::IO::read_linestring_WKT(is, l); + CGAL::IO::read_linestring_WKT(iss, l); polylines.push_back(l); } else if(type == "POLYGON") { Polygon p; - CGAL::IO::read_polygon_WKT(is, p); + CGAL::IO::read_polygon_WKT(iss, p); if(!p.outer_boundary().is_empty()) polygons.push_back(p); } else if(type == "MULTIPOINT") { MultiPoint mp; - CGAL::IO::read_multi_point_WKT(is, mp); + CGAL::IO::read_multi_point_WKT(iss, mp); for(const Point& point : mp) points.push_back(point); } else if(type == "MULTILINESTRING") { MultiLineString mls; - CGAL::IO::read_multi_linestring_WKT(is, mls); + CGAL::IO::read_multi_linestring_WKT(iss, mls); for(const LineString& ls : mls) polylines.push_back(ls); } else if(type == "MULTIPOLYGON") { MultiPolygon mp; - CGAL::IO::read_multi_polygon_WKT(is, mp); + CGAL::IO::read_multi_polygon_WKT(iss, mp); for(const Polygon& poly : mp) polygons.push_back(poly); } } - while(is.good() && !is.eof()); + return !is.fail(); }