Skip to content

Commit

Permalink
Added: country selection
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Aug 15, 2023
1 parent 88b2e91 commit 60a9ce2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 16 deletions.
38 changes: 38 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Aos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1897,4 +1897,42 @@ Aos::Country_color_map Aos::get_color_mapping(Arr_handle arrh)
}

return result;
}

#include <CGAL/Arr_naive_point_location.h>
#include <CGAL/Arr_point_location_result.h>

std::string Aos::locate_country(Arr_handle arrh, const QVector3D& point)
{
using Point_2 = Countries_arr::Point_2;
using Naive_pl = CGAL::Arr_naive_point_location<Countries_arr>;

auto& arr = *reinterpret_cast<Countries_arr*>(arrh);
Point_2 query_point(Dir3(point.x(), point.y(), point.z()),
Point_2::Location_type::NO_BOUNDARY_LOC);

Naive_pl npl(arr);
auto obj = npl.locate(query_point);

using Arrangement_2 = Countries_arr;
using Vertex_const_handle = typename Arrangement_2::Vertex_const_handle;
using Halfedge_const_handle = typename Arrangement_2::Halfedge_const_handle;
using Face_const_handle = typename Arrangement_2::Face_const_handle;
//const Vertex_const_handle* v;
//const Halfedge_const_handle* e;
//const Face_const_handle* f;
//std::cout << "The point (" << query_point << ") is located ";
if (auto f = boost::get<Face_const_handle>(&obj)) // located inside a face
{
const auto country_name = f->ptr()->data();
return country_name;
}
//else if (auto e = boost::get<Halfedge_const_handle>(&obj)) // located on an edge
// std::cout << "on an edge: " << (*e)->curve() << std::endl;
//else if (auto v = boost::get<Vertex_const_handle>(&obj)) // located on a vertex
// std::cout << "on " << (((*v)->is_isolated()) ? "an isolated" : "a")
// << " vertex: " << (*v)->point() << std::endl;
//else CGAL_error_msg("Invalid object.");

return "";
}
2 changes: 2 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Aos.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class Aos

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

static std::string locate_country(Arr_handle arrh, const QVector3D& point);
};


Expand Down
56 changes: 41 additions & 15 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Main_widget::~Main_widget()
doneCurrent();
}

float dimming_factor = 0.4;

void Main_widget::mousePressEvent(QMouseEvent* e)
{
// forward the event to the camera manipulators
Expand All @@ -42,8 +44,6 @@ void Main_widget::mousePressEvent(QMouseEvent* e)
// handle country selection
if (e->button() == Qt::RightButton)
{
qDebug() << "RIGHT MOUSE BUTTON PRESSED!!";

auto p = e->pos();
QVector3D sp0(p.x(), m_vp_height - p.y(), 0);
QVector3D sp1(p.x(), m_vp_height - p.y(), 1);
Expand All @@ -55,17 +55,15 @@ void Main_widget::mousePressEvent(QMouseEvent* e)
auto wp0 = sp0.unproject(model_view, proj, viewport);
auto wp1 = sp1.unproject(model_view, proj, viewport);

// ASSERTION!!!
m_mouse_pos = wp0;
// ASSERTION!!!
m_mouse_pos = wp0;

// define a ray from the camera pos to the world-point
//auto o = m_camera.get_pos();
//auto u = wp - o;
auto o = wp0;
auto u = wp1 - wp0;

std::cout << "camera pos = " << o << std::endl;

// solve the quadratic equation to check for intersection of ray with sphere
auto a = QVector3D::dotProduct(u, u);
auto b = 2 * QVector3D::dotProduct(u, o);
Expand Down Expand Up @@ -101,6 +99,31 @@ void Main_widget::mousePressEvent(QMouseEvent* e)
}

m_mouse_pos = o + ti * u;
static std::string prev_selected_country;
auto selected_country = Aos::locate_country(m_arrh, m_mouse_pos);

if(!prev_selected_country.empty())
{
// dim the previous country color
auto& prev_country = m_country_triangles[prev_selected_country];
auto color = prev_country->get_color();
color *= dimming_factor;
color.setW(1);
prev_country->set_color(color);
}

if (!selected_country.empty())
{
// highlight the current country color
auto& curr_country = m_country_triangles[selected_country];
auto color = curr_country->get_color();
color /= dimming_factor;
color.setW(1);
curr_country->set_color(color);
qDebug() << "SELECTED COUNTRY: " << selected_country;
}

prev_selected_country = selected_country;
}
}
void Main_widget::mouseMoveEvent(QMouseEvent* e)
Expand Down Expand Up @@ -297,17 +320,17 @@ void Main_widget::initializeGL()
{
qDebug() << "constructiong arr..";
//auto arrh = Aos::construct(m_countries);
auto arrh = Aos::load_arr("C:/work/gsoc2023/ne_110m_admin_0_countries.json");
if (arrh == nullptr)
m_arrh = Aos::load_arr("C:/work/gsoc2023/ne_110m_admin_0_countries.json");
if (m_arrh == nullptr)
{
qDebug() << "** FAILED TO LOAD THE ARRANGEMENT!!!";
exit(1);
}

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);
auto country_triangles_map = Aos::get_triangles_by_country(m_arrh);
auto color_map = Aos::get_color_mapping(m_arrh);
qDebug() << "color map size = " << color_map.size();
qDebug() << "num countries = " << country_triangles_map.size();
auto rndm = [] {return rand() / double(RAND_MAX); };
Expand All @@ -321,11 +344,14 @@ void Main_widget::initializeGL()
for (auto& [country_name, triangle_points] : country_triangles_map)
{
auto country_triangles = std::make_unique<Triangles>(triangle_points);
const float c = 0.5; // dimming factor
auto country_color = QVector4D(c * rndm(), c * rndm(), c * rndm(), 1);
country_triangles->set_color(country_color);
auto color = QVector4D(rndm(), rndm(), rndm(), 1);
auto m = std::max(color.x(), std::max(color.y(), color.z()));
color /= m;
color *= dimming_factor;
color.setW(1);
country_triangles->set_color(color);
//country_triangles->set_color(colors[color_map[country_name]]);
m_country_triangles.push_back(std::move(country_triangles));
m_country_triangles.emplace(country_name, std::move(country_triangles));
}

//qDebug() << "num triangles = " << triangle_points.size() / 3;
Expand Down Expand Up @@ -635,7 +661,7 @@ void Main_widget::paintGL()
sp.set_uniform("u_plane", plane);
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//m_all_triangles->draw();
for (auto& country : m_country_triangles)
for (auto& [country_name, country] : m_country_triangles)
{
sp.set_uniform("u_color", country->get_color());
country->draw();
Expand Down
6 changes: 5 additions & 1 deletion Arrangement_on_surface_2/demo/earth/Main_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <qopenglwidget.h>

#include "Aos.h"
#include "Camera.h"
#include "Camera_manip.h"
#include "Common_defs.h"
Expand Down Expand Up @@ -74,6 +75,9 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase
void init_problematic_nodes();

private:
// ARRANGEMENT
Aos::Arr_handle m_arrh;

// Objects in the scene
std::unique_ptr<Sphere> m_sphere;
std::unique_ptr<World_coord_axes> m_world_coord_axes;
Expand All @@ -97,7 +101,7 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase

// TRIANGLES for rendering the countries in solid
std::unique_ptr<Triangles> m_all_triangles;
std::vector<std::unique_ptr<Triangles>> m_country_triangles;
std::map<std::string, std::unique_ptr<Triangles>> m_country_triangles;


// Shaders
Expand Down

0 comments on commit 60a9ce2

Please sign in to comment.