From f7b1d1675a55eac14934f098777d80ca89dd0948 Mon Sep 17 00:00:00 2001 From: denizdiktas Date: Fri, 14 Jul 2023 11:50:58 +0300 Subject: [PATCH] Refactor: moved commonly calculated values in the manipulators up to the base class --- .../demo/earth/Camera_manip.cpp | 6 +- .../demo/earth/Camera_manip.h | 3 +- .../demo/earth/Camera_manip_rot.cpp | 7 +- .../demo/earth/Camera_manip_rot_bpa.cpp | 4 +- .../demo/earth/Camera_manip_zoom.cpp | 5 +- .../demo/earth/Camera_manip_zoom.h | 1 - .../demo/earth/Main_widget.cpp | 99 +++++++++++-------- .../demo/earth/Main_widget.h | 7 ++ 8 files changed, 72 insertions(+), 60 deletions(-) diff --git a/Arrangement_on_surface_2/demo/earth/Camera_manip.cpp b/Arrangement_on_surface_2/demo/earth/Camera_manip.cpp index eeb7354f454..26ee1a0a2e2 100644 --- a/Arrangement_on_surface_2/demo/earth/Camera_manip.cpp +++ b/Arrangement_on_surface_2/demo/earth/Camera_manip.cpp @@ -31,13 +31,13 @@ void Camera_manip::mousePressEvent(QMouseEvent* e) } void Camera_manip::mouseMoveEvent(QMouseEvent* e) { - auto current_mouse_pos = QVector2D(e->position()); - const auto diff = current_mouse_pos - m_last_mouse_pos; + m_current_mouse_pos = QVector2D(e->position()); + m_diff = m_current_mouse_pos - m_last_mouse_pos; // call the function overridden by the derived class mouse_move_event(e); - m_last_mouse_pos = current_mouse_pos; + m_last_mouse_pos = m_current_mouse_pos; } void Camera_manip::mouseReleaseEvent(QMouseEvent* e) { diff --git a/Arrangement_on_surface_2/demo/earth/Camera_manip.h b/Arrangement_on_surface_2/demo/earth/Camera_manip.h index 3ae653d41dc..b959e373c78 100644 --- a/Arrangement_on_surface_2/demo/earth/Camera_manip.h +++ b/Arrangement_on_surface_2/demo/earth/Camera_manip.h @@ -32,9 +32,10 @@ class Camera_manip bool m_left_mouse_button_down = false; bool m_middle_mouse_button_down = false; - //QVector2D m_current_mouse_pos; + QVector2D m_current_mouse_pos; QVector2D m_last_mouse_pos; QVector2D m_mouse_press_pos; + QVector2D m_diff; }; diff --git a/Arrangement_on_surface_2/demo/earth/Camera_manip_rot.cpp b/Arrangement_on_surface_2/demo/earth/Camera_manip_rot.cpp index 04287d57e1e..dc312afef67 100644 --- a/Arrangement_on_surface_2/demo/earth/Camera_manip_rot.cpp +++ b/Arrangement_on_surface_2/demo/earth/Camera_manip_rot.cpp @@ -11,12 +11,9 @@ void Camera_manip_rot::mouse_move_event(QMouseEvent* e) { if (m_left_mouse_button_down) { - auto current_mouse_pos = QVector2D(e->position()); - const auto diff = current_mouse_pos - m_last_mouse_pos; - const float rotation_scale_factor = 0.1f; - m_theta += rotation_scale_factor * diff.x(); - m_phi += rotation_scale_factor * diff.y(); + m_theta += rotation_scale_factor * m_diff.x(); + m_phi += rotation_scale_factor * m_diff.y(); m_camera.rotate_from_init_config(-m_theta, -m_phi); } } diff --git a/Arrangement_on_surface_2/demo/earth/Camera_manip_rot_bpa.cpp b/Arrangement_on_surface_2/demo/earth/Camera_manip_rot_bpa.cpp index a980f5e66b3..d6d923d8db4 100644 --- a/Arrangement_on_surface_2/demo/earth/Camera_manip_rot_bpa.cpp +++ b/Arrangement_on_surface_2/demo/earth/Camera_manip_rot_bpa.cpp @@ -17,14 +17,12 @@ void Camera_manip_rot_bpa::mouse_press_event(QMouseEvent* e) } void Camera_manip_rot_bpa::mouse_move_event(QMouseEvent* e) { - auto current_mouse_pos = QVector2D(e->position()); - const auto diff = current_mouse_pos - m_last_mouse_pos; const float rotation_scale_factor = 0.1f; // ROTATION AROUND AN AXIS ORTHOGONAL TO THE BACKPROJECTED DIF-VECTOR //QVector3D p0(m_last_mouse_pos.x(), m_vp_height - m_last_mouse_pos.y(), 0); QVector3D p0(m_mouse_press_pos.x(), m_vp_height - m_mouse_press_pos.y(), 0); - QVector3D p1(current_mouse_pos.x(), m_vp_height - current_mouse_pos.y(), 0); + QVector3D p1(m_current_mouse_pos.x(), m_vp_height - m_current_mouse_pos.y(), 0); auto dp = p1 - p0; // difference vector in OpenGL window coords. QVector3D rdp(-dp.y(), dp.x(), 0); // rotate diff-vector CCW by 90-deg QVector3D rp = p0 + rdp; // r1 rotated CCW by 90 deg diff --git a/Arrangement_on_surface_2/demo/earth/Camera_manip_zoom.cpp b/Arrangement_on_surface_2/demo/earth/Camera_manip_zoom.cpp index e550be240b6..2a52bc11dd2 100644 --- a/Arrangement_on_surface_2/demo/earth/Camera_manip_zoom.cpp +++ b/Arrangement_on_surface_2/demo/earth/Camera_manip_zoom.cpp @@ -11,11 +11,8 @@ void Camera_manip_zoom::mouse_move_event(QMouseEvent* e) { if (m_middle_mouse_button_down) { - auto current_mouse_pos = QVector2D(e->position()); - const auto diff = current_mouse_pos - m_last_mouse_pos; - const float zoom_scale_factor = 0.01f; - const auto distance = zoom_scale_factor * diff.y(); + const auto distance = zoom_scale_factor * m_diff.y(); m_camera.move_forward(distance); } } diff --git a/Arrangement_on_surface_2/demo/earth/Camera_manip_zoom.h b/Arrangement_on_surface_2/demo/earth/Camera_manip_zoom.h index cfd9c57f04c..03515c11c3b 100644 --- a/Arrangement_on_surface_2/demo/earth/Camera_manip_zoom.h +++ b/Arrangement_on_surface_2/demo/earth/Camera_manip_zoom.h @@ -17,7 +17,6 @@ class Camera_manip_zoom : public Camera_manip virtual void mouse_move_event(QMouseEvent* e) override; private: - float m_theta = 0, m_phi = 0; }; #endif diff --git a/Arrangement_on_surface_2/demo/earth/Main_widget.cpp b/Arrangement_on_surface_2/demo/earth/Main_widget.cpp index e12884b0498..0bb5bf07bf1 100644 --- a/Arrangement_on_surface_2/demo/earth/Main_widget.cpp +++ b/Arrangement_on_surface_2/demo/earth/Main_widget.cpp @@ -105,32 +105,52 @@ void Main_widget::keyPressEvent(QKeyEvent* event) } } + +void Main_widget::verify_antarctica_node_is_redundant() +{ + Kml::Node n1(178.277211542064, -84.4725179992025), + n2(180.0, -84.71338), + n3(-179.942499356179, -84.7214433735525); + + // 1) check if it is collinear with its neighboring nodes: + // all of the vectors in 3D must lie in the same plane + auto v1 = n1.get_coords_3f(); + auto v2 = n2.get_coords_3f(); + auto v3 = n3.get_coords_3f(); + auto n = QVector3D::crossProduct(v1, v3); + n.normalize(); + std::cout << "*** DOT PRODUCT = " << QVector3D::dotProduct(n, v2) << std::endl; + + // 2) check if it is between its neighbors (check if r,s > 0) + auto det = [](float ax, float ay, float bx, float by) { return ax * by - ay * bx; }; + auto D = det(v1.x(), v1.y(), v3.x(), v3.y()); + auto Dr = det(v2.x(), v2.y(), v3.x(), v3.y()); + auto Ds = det(v1.x(), v1.y(), v2.x(), v2.y()); + auto r = Dr / D; + auto s = Ds / D; + std::cout << "r = " << r << "\ns=" << s << std::endl; +} +void Main_widget::init_problematic_nodes() +{ + Kml::Nodes prob_nodes = { + {23.8058134294668,8.66631887454253}, + {24.1940677211877,8.7286964724039 }, + {24.5673690121521,8.22918793378547}, + {23.8869795808607,8.61972971293307} + }; + std::vector prob_vertices; + for (const auto& node : prob_nodes) + prob_vertices.push_back(node.get_coords_3f()); + m_problematic_vertices = std::make_unique(prob_vertices); +} + void Main_widget::initializeGL() { // verify that the node (180.0, -84.71338) in Antarctica is redundant - { - Kml::Node n1(178.277211542064, -84.4725179992025), - n2(180.0, -84.71338), - n3(-179.942499356179, -84.7214433735525); - - // 1) check if it is collinear with its neighboring nodes: - // all of the vectors in 3D must lie in the same plane - auto v1 = n1.get_coords_3f(); - auto v2 = n2.get_coords_3f(); - auto v3 = n3.get_coords_3f(); - auto n = QVector3D::crossProduct(v1, v3); - n.normalize(); - std::cout << "*** DOT PRODUCT = " << QVector3D::dotProduct(n, v2) << std::endl; - - // 2) check if it is between its neighbors (check if r,s > 0) - auto det = [](float ax, float ay, float bx, float by) { return ax * by - ay * bx; }; - auto D = det(v1.x(), v1.y(), v3.x(), v3.y()); - auto Dr = det(v2.x(), v2.y(), v3.x(), v3.y()); - auto Ds = det(v1.x(), v1.y(), v2.x(), v2.y()); - auto r = Dr / D; - auto s = Ds / D; - std::cout << "r = " << r << "\ns=" << s << std::endl; - } + verify_antarctica_node_is_redundant(); + + //init_problematic_nodes(); + std::string data_path = "C:/work/gsoc2023/data/"; //std::string shape_file_path = data_path + "ne_110m_admin_0_countries/"; @@ -138,17 +158,16 @@ void Main_widget::initializeGL() //Shapefile::read(shape_file_name); //const auto file_name = data_path + "world_countries.kml"; - const auto file_name = data_path + "ne_110m_admin_0_countries.kml"; - //const auto file_name = data_path + "ne_110m_admin_0_countries_africa.kml"; + //const auto file_name = data_path + "ne_110m_admin_0_countries.kml"; + const auto file_name = data_path + "ne_110m_admin_0_countries_africa.kml"; m_countries = Kml::read(file_name); auto dup_nodes = Kml::get_duplicates(m_countries); - - qDebug() << "identifying duplicate nodes"; //auto all_nodes = Kml::generate_ids(m_countries); // initialize rendering of DUPLICATE VERTICES if(1) { + qDebug() << "identifying duplicate nodes"; std::vector vertices; for (const auto& node : dup_nodes) vertices.push_back(node.get_coords_3f()); @@ -163,20 +182,6 @@ void Main_widget::initializeGL() m_vertices = std::make_unique(created_vertices); } - // init problematic vertices: these are the vertices incident to deg-4 vertex - { - Kml::Nodes prob_nodes = { - {23.8058134294668,8.66631887454253}, - {24.1940677211877,8.7286964724039 }, - {24.5673690121521,8.22918793378547}, - {23.8869795808607,8.61972971293307} - }; - std::vector prob_vertices; - for (const auto& node : prob_nodes) - prob_vertices.push_back(node.get_coords_3f()); - m_problematic_vertices = std::make_unique(prob_vertices); - } - initializeOpenGLFunctions(); @@ -373,6 +378,14 @@ void Main_widget::resizeGL(int w, int h) const auto err = compute_backprojected_error(0.5f); std::cout << "error = " << err << std::endl; } + +template +void draw_safe(T& ptr) +{ + if (ptr) + ptr->draw(); +} + void Main_widget::paintGL() { QMatrix4x4 model; @@ -458,8 +471,8 @@ void Main_widget::paintGL() sp.set_uniform("u_color", QVector4D(0,1,0,1)); glPointSize(2); - m_problematic_vertices->draw(); - + //m_problematic_vertices->draw(); + draw_safe(m_problematic_vertices); sp.unuse(); } diff --git a/Arrangement_on_surface_2/demo/earth/Main_widget.h b/Arrangement_on_surface_2/demo/earth/Main_widget.h index b789d24673a..7697e07bfc8 100644 --- a/Arrangement_on_surface_2/demo/earth/Main_widget.h +++ b/Arrangement_on_surface_2/demo/earth/Main_widget.h @@ -49,12 +49,19 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase void init_camera(); void init_geometry(); void init_shader_programs(); + float compute_backprojected_error(float pixel_error); // Use this to find the approximate of the true minimum projected error. // we are ot using this complicated method, but provide it for completeness. void find_minimum_projected_error_on_sphere(float we); + // verify that the node (180.0, -84.71338) in Antarctica is redundant + void verify_antarctica_node_is_redundant(); + + // init problematic vertices: these are the vertices incident to deg-4 vertex + void init_problematic_nodes(); + private: // Objects in the scene std::unique_ptr m_sphere;