Skip to content

Commit

Permalink
Refactor: moved commonly calculated values in the manipulators up to …
Browse files Browse the repository at this point in the history
…the base class
  • Loading branch information
denizdiktas committed Jul 14, 2023
1 parent 70247a1 commit f7b1d16
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 60 deletions.
6 changes: 3 additions & 3 deletions Arrangement_on_surface_2/demo/earth/Camera_manip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
3 changes: 2 additions & 1 deletion Arrangement_on_surface_2/demo/earth/Camera_manip.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};


Expand Down
7 changes: 2 additions & 5 deletions Arrangement_on_surface_2/demo/earth/Camera_manip_rot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
4 changes: 1 addition & 3 deletions Arrangement_on_surface_2/demo/earth/Camera_manip_rot_bpa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions Arrangement_on_surface_2/demo/earth/Camera_manip_zoom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
1 change: 0 additions & 1 deletion Arrangement_on_surface_2/demo/earth/Camera_manip_zoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
99 changes: 56 additions & 43 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,50 +105,69 @@ 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<QVector3D> prob_vertices;
for (const auto& node : prob_nodes)
prob_vertices.push_back(node.get_coords_3f());
m_problematic_vertices = std::make_unique<Vertices>(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/";
//auto shape_file_name = shape_file_path + "ne_110m_admin_0_countries.shp";
//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<QVector3D> vertices;
for (const auto& node : dup_nodes)
vertices.push_back(node.get_coords_3f());
Expand All @@ -163,20 +182,6 @@ void Main_widget::initializeGL()
m_vertices = std::make_unique<Vertices>(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<QVector3D> prob_vertices;
for (const auto& node : prob_nodes)
prob_vertices.push_back(node.get_coords_3f());
m_problematic_vertices = std::make_unique<Vertices>(prob_vertices);
}


initializeOpenGLFunctions();

Expand Down Expand Up @@ -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<typename T>
void draw_safe(T& ptr)
{
if (ptr)
ptr->draw();
}

void Main_widget::paintGL()
{
QMatrix4x4 model;
Expand Down Expand Up @@ -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();
}
Expand Down
7 changes: 7 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Main_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sphere> m_sphere;
Expand Down

0 comments on commit f7b1d16

Please sign in to comment.