mirror of https://github.com/CGAL/cgal
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:
commit
cda39a3db4
|
|
@ -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)
|
||||
{
|
||||
|
||||
std::deque<Point> points;
|
||||
std::deque<std::vector<Point>> linestrings;
|
||||
std::deque<Polygon> polygons;
|
||||
|
||||
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;
|
||||
|
||||
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());
|
||||
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)
|
||||
{
|
||||
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());
|
||||
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());
|
||||
//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);
|
||||
Q_EMIT( changed());
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue