Skip to content

Commit

Permalink
refactor: moved country picking to a separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Aug 17, 2023
1 parent ddc8cdd commit 706e7ea
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 73 deletions.
11 changes: 6 additions & 5 deletions Arrangement_on_surface_2/demo/earth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ source_group( "Graphics_Geometry" FILES ${source_files_graphics_geometry} )

# GUI
file(GLOB source_files_gui
Camera_manip.h Camera_manip.cpp
Camera_manip_rot.h Camera_manip_rot.cpp
Camera_manip_rot_bpa.h Camera_manip_rot_bpa.cpp
Camera_manip_zoom.h Camera_manip_zoom.cpp
GUI_event_handler.h GUI_event_handler.cpp
Camera_manip.h Camera_manip.cpp
Camera_manip_rot.h Camera_manip_rot.cpp
Camera_manip_rot_bpa.h Camera_manip_rot_bpa.cpp
Camera_manip_zoom.h Camera_manip_zoom.cpp
GUI_country_pick_handler.h GUI_country_pick_handler.cpp
GUI_event_handler.h GUI_event_handler.cpp
)
source_group( "GUI" FILES ${source_files_gui} )

Expand Down
117 changes: 117 additions & 0 deletions Arrangement_on_surface_2/demo/earth/GUI_country_pick_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright(c) 2012, 2020 Tel - Aviv University(Israel).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s): Engin Deniz Diktas <[email protected]>

#include "GUI_country_pick_handler.h"

//#include <qvector3d.h>


GUI_country_pick_handler::GUI_country_pick_handler(Main_widget& main_widget) :
m_main_widget(main_widget),
m_camera(main_widget.get_camera())
{

}

void GUI_country_pick_handler::mouse_press_event(QMouseEvent* e)
{
// handle country selection
if (e->button() == Qt::RightButton)
{
auto p = e->pos();
QVector3D sp0(p.x(), m_vp_height - p.y(), 0);
QVector3D sp1(p.x(), m_vp_height - p.y(), 1);

auto proj = m_camera.get_projection_matrix();
auto view = m_camera.get_view_matrix();
auto model_view = view * m_main_widget.get_model_matrix();
QRect viewport(0, 0, m_vp_width, m_vp_height);
auto wp0 = sp0.unproject(model_view, proj, viewport);
auto wp1 = sp1.unproject(model_view, proj, viewport);

// ASSERTION!!!
m_main_widget.set_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;

// 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);
auto c = QVector3D::dotProduct(o, o) - 1;
auto d = b * b - 4 * a * c;

float ti = -1;
if (abs(d) < std::numeric_limits<float>::epsilon())
{
// single intersection
ti = -b / (2 * a);
}
else
{
if (d < 0)
{
// no intersection
return;
}
else
{
// two intersections
auto sd = sqrt(d);
auto t1 = (-b - sd) / (2 * a);
auto t2 = (-b + sd) / (2 * a);
if (t1 > 0 && t2 > 0)
ti = std::min(t1, t2);
else if (t1 > 0)
ti = t1;
else
ti = t2;
}
}

//m_mouse_pos = o + ti * u;
auto pos = o + ti * u;
m_main_widget.set_mouse_pos(pos);
static std::string prev_picked_country;
auto& arrh = m_main_widget.get_arr_handle();
auto picked_country = Aos::locate_country(arrh, pos);

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

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

// prev_picked_country = picked_country;
}
}
void GUI_country_pick_handler::resize(int w, int h)
{
m_vp_width = w;
m_vp_height = h;
}
35 changes: 35 additions & 0 deletions Arrangement_on_surface_2/demo/earth/GUI_country_pick_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright(c) 2012, 2020 Tel - Aviv University(Israel).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s): Engin Deniz Diktas <[email protected]>

#ifndef GUI_COUNTRY_PICK_HANDLER_H
#define GUI_COUNTRY_PICK_HANDLER_H

#include <qevent.h>
#include <qvector2d.h>

#include "GUI_event_handler.h"
#include "Main_widget.h"


class GUI_country_pick_handler : public GUI_event_handler
{
public:
GUI_country_pick_handler(Main_widget& main_widget);

protected:
virtual void mouse_press_event(QMouseEvent* e) override;
virtual void resize(int w, int h) override;

Main_widget& m_main_widget;
Camera& m_camera;
int m_vp_width, m_vp_height;
};


#endif
Loading

0 comments on commit 706e7ea

Please sign in to comment.