From daf548c58cbb17363a800889cad19cda980d4054 Mon Sep 17 00:00:00 2001 From: denizdiktas Date: Fri, 7 Jul 2023 17:35:53 +0300 Subject: [PATCH] refactor --- .../demo/earth/Kml_reader.cpp | 55 +++++++++++++++++++ .../demo/earth/Kml_reader.h | 8 +++ .../demo/earth/Main_widget.cpp | 4 +- 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/Arrangement_on_surface_2/demo/earth/Kml_reader.cpp b/Arrangement_on_surface_2/demo/earth/Kml_reader.cpp index 0c2dbc32041..e59f7a00c2b 100644 --- a/Arrangement_on_surface_2/demo/earth/Kml_reader.cpp +++ b/Arrangement_on_surface_2/demo/earth/Kml_reader.cpp @@ -1,6 +1,7 @@ #include "Kml_reader.h" +#include #include #include @@ -212,6 +213,51 @@ Kml::Nodes Kml::get_duplicates(const Placemarks& placemarks) return duplicate_nodes; } + +Kml::Nodes Kml::generate_ids(Placemarks& placemarks) +{ + // collect all nodes into a single vector + int polygon_count = 0; + std::vector nodes; + for (auto& pm : placemarks) + { + for (auto& polygon : pm.polygons) + { + polygon_count++; + + std::vector linear_rings; + linear_rings.push_back(&polygon.outer_boundary); + for (auto& inner_boundary : polygon.inner_boundaries) + linear_rings.push_back(&inner_boundary); + + for (auto* lring : linear_rings) + { + for (const auto& node : lring->nodes) + { + // check if the node is in the nodes + auto it = std::find(nodes.begin(), nodes.end(), node); + if (nodes.end() == it) + { + // insert new node + nodes.push_back(node); + const int node_id = nodes.size() - 1; + lring->ids.push_back(node_id); + } + else + { + // get the existing node + const int node_id = std::distance(nodes.begin(), it); + lring->ids.push_back(node_id); + } + } + } + } + } + + return nodes; +} + + Kml::Nodes Kml::Polygon::get_all_nodes() const { Nodes all_nodes; @@ -228,6 +274,15 @@ Kml::Nodes Kml::Polygon::get_all_nodes() const return all_nodes; } +std::vector Kml::Polygon::get_all_boundaries() +{ + std::vector linear_rings; + linear_rings.push_back(&outer_boundary); + for (auto& inner_boundary : inner_boundaries) + linear_rings.push_back(&inner_boundary); + + return linear_rings; +} Kml::Nodes Kml::Placemark::get_all_nodes() const { diff --git a/Arrangement_on_surface_2/demo/earth/Kml_reader.h b/Arrangement_on_surface_2/demo/earth/Kml_reader.h index d51cca15ea8..76c74573ef7 100644 --- a/Arrangement_on_surface_2/demo/earth/Kml_reader.h +++ b/Arrangement_on_surface_2/demo/earth/Kml_reader.h @@ -38,6 +38,7 @@ class Kml struct LinearRing { std::vector nodes; + std::vector ids; Arcs get_arcs() const; void get_arcs(Arcs& arcs) const; @@ -53,6 +54,8 @@ class Kml // when collecting nodes start from the outer boundary and then get nodes // from individual inner boundaries in the order Nodes get_all_nodes() const; + + std::vector get_all_boundaries(); }; @@ -72,6 +75,11 @@ class Kml static Placemarks read(const std::string& file_name); static Nodes get_duplicates(const Placemarks& placemarks); + + + // Outputs all used nodes without duplications! + // NOTE: this function modifies Placemarks data-structure! + static Nodes generate_ids(Placemarks& placemarks); }; diff --git a/Arrangement_on_surface_2/demo/earth/Main_widget.cpp b/Arrangement_on_surface_2/demo/earth/Main_widget.cpp index 7fa8301c4c5..17243e770d7 100644 --- a/Arrangement_on_surface_2/demo/earth/Main_widget.cpp +++ b/Arrangement_on_surface_2/demo/earth/Main_widget.cpp @@ -212,9 +212,11 @@ void Main_widget::initializeGL() const auto file_name = "C:/work/gsoc2023/data/ne_110m_admin_0_countries.kml"; m_countries = Kml::read(file_name); auto dup_nodes = Kml::get_duplicates(m_countries); + + auto all_nodes = Kml::generate_ids(m_countries); // initialize rendering of DUPLICATE VERTICES - if(0) + if(1) { std::vector vertices; for (const auto& node : dup_nodes)