Merge pull request #7112 from afabri/T2_demo-read_WKT-GF

Triangulation_2 Demo: Read files with many WKT entities
This commit is contained in:
Laurent Rineau 2023-01-27 20:50:33 +01:00
commit cda39a3db4
2 changed files with 46 additions and 99 deletions

View File

@ -555,87 +555,32 @@ MainWindow::loadWKT(QString filename)
{
//Polygons todo : make it multipolygons
std::ifstream ifs(qPrintable(filename));
do
{
typedef CGAL::Polygon_with_holes_2<K> Polygon;
typedef CGAL::Point_2<K> Point;
std::vector<Polygon> mps;
CGAL::IO::read_multi_polygon_WKT(ifs, mps);
for(const Polygon& p : mps)
{
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());
typedef CGAL::Polygon_with_holes_2<K> Polygon;
typedef CGAL::Point_2<K> Point;
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<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());
std::deque<Point> points;
std::deque<std::vector<Point>> linestrings;
std::deque<Polygon> polygons;
//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);
CGAL::IO::read_WKT(ifs, points, linestrings, polygons);
cdt.insert(points.begin(),points.end());
for(const std::vector<Point>& 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(), h_it->vertices_end(),true);
}
}while(ifs.good() && !ifs.eof());
}
discoverComponents(cdt, m_seeds);
Q_EMIT( changed());

View File

@ -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();
}