diff --git a/Arrangement_on_surface_2/demo/earth/Aos.cpp b/Arrangement_on_surface_2/demo/earth/Aos.cpp index 0c2a3d1535d..0a073fbfbc0 100644 --- a/Arrangement_on_surface_2/demo/earth/Aos.cpp +++ b/Arrangement_on_surface_2/demo/earth/Aos.cpp @@ -1711,9 +1711,13 @@ Aos::Country_triangles_map Aos::get_triangles_by_country(Arr_handle arrh) { auto& face = *fit; const auto& country_name = fit->data(); + // skipping spherical-face + if (country_name.empty()) + continue; country_faces_map[country_name].push_back(&face); } + std::cout << "triangulating individual faces\n"; Country_triangles_map result; for (auto& [country_name, faces] : country_faces_map) { @@ -1743,8 +1747,6 @@ Aos::Country_triangles_map Aos::get_triangles_by_country(Arr_handle arrh) // RESULTING TRIANGLE POINTS (every 3 point => triangle) auto& triangles = result[country_name]; - std::cout << "triangulating individual faces\n"; - // loop on all approximated faces for (auto& face_points : all_faces_of_current_country) { @@ -1817,5 +1819,82 @@ Aos::Country_triangles_map Aos::get_triangles_by_country(Arr_handle arrh) } } + return result; +} + + +Aos::Country_color_map Aos::get_color_mapping(Arr_handle arrh) +{ + auto& arr = *reinterpret_cast(arrh); + + // group the faces by their country name, + std::vector all_countries; + using Face_ = Countries_arr::Face_handle::value_type; + std::map> country_faces_map; + for (auto fit = arr.faces_begin(); fit != arr.faces_end(); ++fit) + { + auto& face = *fit; + const auto& country_name = fit->data(); + // skipping spherical-face + if (country_name.empty()) + continue; + country_faces_map[country_name].push_back(&face); + all_countries.push_back(country_name); + } + + // prepare a map of neighboring countries + std::map> country_neighbors_map; + for (auto& [country_name, faces] : country_faces_map) + { + // loop on all of the faces of the current country + for (auto* face : faces) + { + auto first = face->outer_ccb(); + auto curr = first; + do { + const auto& neighbor_country_name = curr->twin()->face()->data(); + + // skip the spherical face + if (neighbor_country_name.empty()) + continue; + + country_neighbors_map[country_name].insert(neighbor_country_name); + } while (++curr != first); + } + } + + // find a color index for each country by looking at its neighbors + Country_color_map result; + for(const auto& country_name : all_countries) + { + // first: find a free color index + bool color_used[5] = { false, false, false, false, false }; + auto& neighbor_set = country_neighbors_map[country_name]; + for (auto& neighbor : neighbor_set) + { + auto it = result.find(neighbor); + // if there is a country in the map, then it must have been assigned one! + if (it != result.end()) + { + auto used_color_index = it->second; + color_used[used_color_index] = true; + } + } + + // find the first color index not used + bool found = false; + for (int i = 0; i < 5; i++) + { + if (color_used[i] == false) + { + found = true; + result[country_name] = i; + } + } + // assertion check!!! + if(!found) + std::cout << "*** ASSERTION ERROR: NO INDEX FOUND!!!\n"; + } + return result; } \ No newline at end of file diff --git a/Arrangement_on_surface_2/demo/earth/Aos.h b/Arrangement_on_surface_2/demo/earth/Aos.h index 8565c445e1e..7b87d493c82 100644 --- a/Arrangement_on_surface_2/demo/earth/Aos.h +++ b/Arrangement_on_surface_2/demo/earth/Aos.h @@ -69,6 +69,9 @@ public: using Country_triangles_map = std::map>; static Country_triangles_map get_triangles_by_country(Arr_handle arrh); + + using Country_color_map = std::map; + static Country_color_map get_color_mapping(Arr_handle arrh); }; diff --git a/Arrangement_on_surface_2/demo/earth/Main_widget.cpp b/Arrangement_on_surface_2/demo/earth/Main_widget.cpp index 7942bb72b9b..1aa6a56cea6 100644 --- a/Arrangement_on_surface_2/demo/earth/Main_widget.cpp +++ b/Arrangement_on_surface_2/demo/earth/Main_widget.cpp @@ -244,14 +244,27 @@ void Main_widget::initializeGL() qDebug() << "generating triangles.."; //auto triangle_points = Aos::get_triangles(arrh); auto country_triangles_map = Aos::get_triangles_by_country(arrh); + auto color_map = Aos::get_color_mapping(arrh); + qDebug() << "color map size = " << color_map.size(); qDebug() << "num countries = " << country_triangles_map.size(); - auto rndm = [] {return rand() / double(RAND_MAX); }; + //auto rndm = [] {return rand() / double(RAND_MAX); }; + QVector4D colors[] = { + QVector4D(1,0,0,1), + QVector4D(0,1,0,1), + QVector4D(0,0,1,1), + QVector4D(1,1,0,1), + QVector4D(1,0,1,1) + }; for (auto& [country_name, triangle_points] : country_triangles_map) { auto country_triangles = std::make_unique(triangle_points); - country_triangles->set_color(QVector4D(rndm(), rndm(), rndm(), 1)); + //country_triangles->set_color(QVector4D(rndm(), rndm(), rndm(), 1)); + country_triangles->set_color(colors[color_map[country_name]]); g_country_triangles.push_back(std::move(country_triangles)); } + + + //qDebug() << "num triangles = " << triangle_points.size() / 3; //g_all_triangles = std::make_unique(triangle_points);