Fix and simplify code

This commit is contained in:
Andreas Fabri 2022-12-08 16:23:10 +00:00
parent 8da2cd9a34
commit aa8da893c2
2 changed files with 45 additions and 109 deletions

View File

@ -555,8 +555,7 @@ MainWindow::loadWKT(QString filename)
{ {
//Polygons todo : make it multipolygons //Polygons todo : make it multipolygons
std::ifstream ifs(qPrintable(filename)); std::ifstream ifs(qPrintable(filename));
do
{
typedef CGAL::Polygon_with_holes_2<K> Polygon; typedef CGAL::Polygon_with_holes_2<K> Polygon;
typedef CGAL::Point_2<K> Point; typedef CGAL::Point_2<K> Point;
@ -568,85 +567,20 @@ MainWindow::loadWKT(QString filename)
cdt.insert(points.begin(),points.end()); cdt.insert(points.begin(),points.end());
for(const std::vector<Point>& line){ for(const std::vector<Point>& line : linestrings){
cdt.insert_constraint(line.begin(), line.end()); cdt.insert_constraint(line.begin(), line.end());
} }
for(const Polygon& p : polygons) for(const Polygon& p : polygons){
{
if(p.outer_boundary().is_empty()) if(p.outer_boundary().is_empty())
continue; continue;
for(Point point : p.outer_boundary().container()) cdt.insert_constraint(p.outer_boundary().vertices_begin(), p.outer_boundary().vertices_end(),true);
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 = for(Polygon::Hole_const_iterator h_it = p.holes_begin(); h_it != p.holes_end(); ++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(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<K::Point_2> LineString;
std::vector<LineString> 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<K::Point_2> 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); discoverComponents(cdt, m_seeds);
Q_EMIT( changed()); Q_EMIT( changed());

View File

@ -503,72 +503,74 @@ bool read_WKT(std::istream& is,
if(!is.good()) if(!is.good())
return false; return false;
do while(is.good() && !is.eof())
{ {
typedef typename MultiPoint::value_type Point; typedef typename MultiPoint::value_type Point;
typedef typename MultiLineString::value_type LineString; typedef typename MultiLineString::value_type LineString;
typedef typename MultiPolygon::value_type Polygon; typedef typename MultiPolygon::value_type Polygon;
std::string line; std::string line;
std::streampos input_pos = is.tellg();
std::getline(is, line); std::getline(is, line);
std::istringstream iss(line); std::string::size_type header_end = line.find("("); // }
std::string t; if(header_end == std::string::npos){
std::string type=""; continue;
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 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") if(type == "POINT")
{ {
Point p; Point p;
CGAL::IO::read_point_WKT(is, p); CGAL::IO::read_point_WKT(iss, p);
points.push_back(p); points.push_back(p);
} }
else if(type == "LINESTRING") else if(type == "LINESTRING")
{ {
LineString l; LineString l;
CGAL::IO::read_linestring_WKT(is, l); CGAL::IO::read_linestring_WKT(iss, l);
polylines.push_back(l); polylines.push_back(l);
} }
else if(type == "POLYGON") else if(type == "POLYGON")
{ {
Polygon p; Polygon p;
CGAL::IO::read_polygon_WKT(is, p); CGAL::IO::read_polygon_WKT(iss, p);
if(!p.outer_boundary().is_empty()) if(!p.outer_boundary().is_empty())
polygons.push_back(p); polygons.push_back(p);
} }
else if(type == "MULTIPOINT") else if(type == "MULTIPOINT")
{ {
MultiPoint mp; MultiPoint mp;
CGAL::IO::read_multi_point_WKT(is, mp); CGAL::IO::read_multi_point_WKT(iss, mp);
for(const Point& point : mp) for(const Point& point : mp)
points.push_back(point); points.push_back(point);
} }
else if(type == "MULTILINESTRING") else if(type == "MULTILINESTRING")
{ {
MultiLineString mls; MultiLineString mls;
CGAL::IO::read_multi_linestring_WKT(is, mls); CGAL::IO::read_multi_linestring_WKT(iss, mls);
for(const LineString& ls : mls) for(const LineString& ls : mls)
polylines.push_back(ls); polylines.push_back(ls);
} }
else if(type == "MULTIPOLYGON") else if(type == "MULTIPOLYGON")
{ {
MultiPolygon mp; MultiPolygon mp;
CGAL::IO::read_multi_polygon_WKT(is, mp); CGAL::IO::read_multi_polygon_WKT(iss, mp);
for(const Polygon& poly : mp) for(const Polygon& poly : mp)
polygons.push_back(poly); polygons.push_back(poly);
} }
} }
while(is.good() && !is.eof());
return !is.fail(); return !is.fail();
} }