Skip to content

Commit

Permalink
Added: Message_manager to notify changes in zoom (trying to recompute…
Browse files Browse the repository at this point in the history
… the country boundaries results in strange OpenGL problems)
  • Loading branch information
denizdiktas committed Jul 17, 2023
1 parent 92c6c17 commit 13111f4
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 11 deletions.
7 changes: 4 additions & 3 deletions Arrangement_on_surface_2/demo/earth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ source_group( "GUI" FILES ${source_files_gui} )
#SOURCE FILES (NOT CATEGORIZED YET)
file(GLOB source_files
Common_defs.h
main.cpp
Main_widget.h Main_widget.cpp
main.cpp
Main_widget.h Main_widget.cpp
Message_manager.h Message_manager.cpp
Timer.h
Tools.h Tools.cpp
Tools.h Tools.cpp
)
source_group( "Source Files" FILES ${source_files} )

Expand Down
9 changes: 9 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Camera_manip_zoom.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#include "Camera_manip_zoom.h"

#include "Message_manager.h"


Camera_manip_zoom::Camera_manip_zoom(Camera& camera) :
Camera_manip(camera)
Expand All @@ -16,4 +18,11 @@ void Camera_manip_zoom::mouse_move_event(QMouseEvent* e)
m_camera.move_forward(distance);
}
}
void Camera_manip_zoom::mouse_release_event(QMouseEvent* e)
{
if (e->button() == Qt::MiddleButton)
{
Message_manager::notify_all("zoom_changed");
}
}

1 change: 1 addition & 0 deletions Arrangement_on_surface_2/demo/earth/Camera_manip_zoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Camera_manip_zoom : public Camera_manip

protected:
virtual void mouse_move_event(QMouseEvent* e) override;
virtual void mouse_release_event(QMouseEvent* e) override;

private:
};
Expand Down
35 changes: 28 additions & 7 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Camera_manip_rot_bpa.h"
#include "Camera_manip_zoom.h"
#include "Kml_reader.h"
#include "Message_manager.h"
#include "Shapefile.h"
#include "Timer.h"
#include "Tools.h"
Expand Down Expand Up @@ -47,6 +48,15 @@ void Main_widget::timerEvent(QTimerEvent*)
update();
}

//class GUI_event_handler
//{
//public:
// void keyPressEvent(QKeyEvent* event);
//
//protected:
// virtual void key_press_event(QKeyEvent* event);
//};

void Main_widget::keyPressEvent(QKeyEvent* event)
{
switch (event->key())
Expand Down Expand Up @@ -192,10 +202,8 @@ void Main_widget::initializeGL()
init_geometry();
init_shader_programs();

{
ScopedTimer("init_country_borders");
init_country_borders();
}
init_country_borders(0.001);
init_country_selection();

glClearColor(0, 0, 0, 1);
glEnable(GL_DEPTH_TEST); // Enable depth buffer
Expand All @@ -205,13 +213,22 @@ void Main_widget::initializeGL()
m_timer.start(12, this);
}


void Main_widget::init_camera()
{
m_camera.set_pos(0, 0, 3);
m_camera_manip_rot = std::make_unique<Camera_manip_rot>(m_camera);
//m_camera_manip_rot = std::make_unique<Camera_manip_rot_bpa>(m_camera);
m_camera_manip_zoom = std::make_unique<Camera_manip_zoom>(m_camera);

// register the zoom-changed function
Message_manager::add("zoom_changed", [&]
{
qDebug() << "ZOOM CHANGED!!!";
const auto error = compute_backprojected_error(0.5);
qDebug() << "new error = " << error;
//qDebug() << "re-initializing the country borders..";
//init_country_borders(error);
});
}
void Main_widget::init_geometry()
{
Expand Down Expand Up @@ -239,23 +256,27 @@ void Main_widget::init_shader_programs()
m_sp_arc.init_with_vs_fs("arc");
}

void Main_widget::init_country_borders()
void Main_widget::init_country_borders(float error)
{
// TO-DO: move this code to resizeGL (when viewport is initialized)
// has to be defined after camera has been defined:
// because we want to compute the error based on camera parameters!
//Geodesic_arcs ga;
const double error = 0.001; // calculate this from cam parameters!
//const double error = 0.001; // calculate this from cam parameters!
//auto lsa = Aos::get_approx_arcs(countries, error);
//auto lsa = Aos::get_approx_arcs(error);
//m_geodesic_arcs = std::make_unique<Line_strips>(lsa);
m_country_borders.clear();
for (const auto& country : m_countries)
{
m_country_names.push_back(country.name);
auto approx_arcs = Aos::get_approx_arcs(country, error);
auto country_border = std::make_unique<Line_strips>(approx_arcs);
m_country_borders.push_back(std::move(country_border));
}
}
void Main_widget::init_country_selection()
{
m_selected_country_index = 0;
//m_selected_country_index = 159; // ANTARCTICA
m_selected_country = &m_countries[m_selected_country_index];
Expand Down
3 changes: 2 additions & 1 deletion Arrangement_on_surface_2/demo/earth/Main_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase
void init_geometry();
void init_shader_programs();

void init_country_borders();
void init_country_borders(float error);
void init_country_selection();


float compute_backprojected_error(float pixel_error);
Expand Down
26 changes: 26 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Message_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

#include "Message_manager.h"


std::map<std::string, Message_manager::Callbacks>
Message_manager::s_message_map;


void Message_manager::add(const std::string& msg_name,
std::function<void()> callback)
{
s_message_map[msg_name].push_back(callback);
}

void Message_manager::notify_all(const std::string& msg_name)
{
auto it = s_message_map.find(msg_name);
if (s_message_map.cend() != it)
{
auto& callbacks = it->second;
for (auto& cb : callbacks)
cb();
}
}


23 changes: 23 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Message_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#ifndef MESSAGE_MANAGER_H
#define MESSAGE_MANAGER_H

#include <functional>
#include <map>
#include <string>


class Message_manager
{
public:
static void add(const std::string& msg_name, std::function<void()> callback);
static void notify_all(const std::string& msg_name);

protected:
using Callbacks = std::vector<std::function<void()>>;
static std::map<std::string, Callbacks> s_message_map;

};


#endif

0 comments on commit 13111f4

Please sign in to comment.