Skip to content

Commit

Permalink
reduced color-set (neighboring countries got different colors from a …
Browse files Browse the repository at this point in the history
…minimal set of colors
  • Loading branch information
denizdiktas committed Aug 13, 2023
1 parent d97d364 commit 08a695b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 4 deletions.
83 changes: 81 additions & 2 deletions Arrangement_on_surface_2/demo/earth/Aos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<Countries_arr*>(arrh);

// group the faces by their country name,
std::vector<std::string> all_countries;
using Face_ = Countries_arr::Face_handle::value_type;
std::map<std::string, std::vector<Face_*>> 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<std::string, std::set<std::string>> 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;
}
3 changes: 3 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Aos.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class Aos

using Country_triangles_map = std::map<std::string, std::vector<QVector3D>>;
static Country_triangles_map get_triangles_by_country(Arr_handle arrh);

using Country_color_map = std::map<std::string, int>;
static Country_color_map get_color_mapping(Arr_handle arrh);
};


Expand Down
17 changes: 15 additions & 2 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Triangles>(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<Triangles>(triangle_points);

Expand Down

0 comments on commit 08a695b

Please sign in to comment.