From 5a7b89b9fdc5027314a7408576c7403b0c516f15 Mon Sep 17 00:00:00 2001 From: Efi Fogel Date: Mon, 15 Apr 2024 10:40:18 +0300 Subject: [PATCH] Suppress time events; fixed triangulation --- .../Aos_triangulator.cpp | 43 +++++----- .../Aos_triangulator.h | 2 +- .../Main_widget.cpp | 82 ++++++++++--------- .../Main_widget.h | 4 +- 4 files changed, 69 insertions(+), 62 deletions(-) diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Aos_triangulator.cpp b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Aos_triangulator.cpp index 8fb3f5223cf5..1d7fa7e51452 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Aos_triangulator.cpp +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Aos_triangulator.cpp @@ -62,16 +62,16 @@ std::vector Aos_triangulator::get_all(Aos::Arr_handle arrh) { std::vector> all_faces; // loop on all faces of the arrangement - for (auto fit = arr.faces_begin(); fit != arr.faces_end(); ++fit) { + for (auto fh : arr.face_handles()) { // skip any face with no OUTER-CCB - if (0 == fit->number_of_outer_ccbs()) + if (0 == fh->number_of_outer_ccbs()) continue; // COMPUTE THE CENTROID OF ALL FACE-POINTS std::vector face_points; // loop on the egdes of the current outer-ccb - auto first = fit->outer_ccb(); + auto first = fh->outer_ccb(); auto curr = first; do { auto ap = approx(curr->source()->point()); @@ -157,7 +157,9 @@ std::vector Aos_triangulator::get_all(Aos::Arr_handle arrh) { } //! \brief -Country_triangles_map Aos_triangulator::get_by_country(Aos::Arr_handle arrh) { +Country_triangles_map Aos_triangulator::get_by_country(Aos::Arr_handle arrh, + float error) { + using K = CGAL::Exact_predicates_inexact_constructions_kernel; using Vb = CGAL::Triangulation_vertex_base_2; using Fb = CGAL::Constrained_triangulation_face_base_2; @@ -167,6 +169,7 @@ Country_triangles_map Aos_triangulator::get_by_country(Aos::Arr_handle arrh) { using Face_handle = CDT::Face_handle; // using Point = CDT::Point; using Polygon_2 = CGAL::Polygon_2; + using Approximate_point_2 = Geom_traits::Approximate_point_2; auto& arr = *reinterpret_cast(arrh.get()); auto approx = s_traits.approximate_2_object(); @@ -174,12 +177,11 @@ Country_triangles_map Aos_triangulator::get_by_country(Aos::Arr_handle arrh) { // group the faces by their country name using Face_ = Countries_arr::Face_handle::value_type; std::map> country_faces_map; - for (auto fit = arr.faces_begin(); fit != arr.faces_end(); ++fit) { - auto& face = *fit; - const auto& country_name = fit->data(); + for (auto fh : arr.face_handles()) { + auto& face = *fh; + const auto& country_name = fh->data(); // skipping spherical-face - if (country_name.empty()) - continue; + if (country_name.empty()) continue; country_faces_map[country_name].push_back(&face); } @@ -189,7 +191,7 @@ Country_triangles_map Aos_triangulator::get_by_country(Aos::Arr_handle arrh) { // CONVERT the face-points to QVector3D using Face_points = std::vector; using Faces_ = std::vector; - Faces_ all_faces_of_current_country; + Faces_ all_faces_of_current_country; for (auto* face : faces) { // skip any face with no OUTER-CCB if (0 == face->number_of_outer_ccbs()) continue; @@ -199,10 +201,15 @@ Country_triangles_map Aos_triangulator::get_by_country(Aos::Arr_handle arrh) { auto first = face->outer_ccb(); auto curr = first; do { - auto ap = approx(curr->source()->point()); - QVector3D p(ap.dx(), ap.dy(), ap.dz()); - p.normalize(); - face_points.push_back(p); + std::vector apx_points; + const auto& xcv = curr->curve(); + approx(xcv, error, std::back_insert_iterator(apx_points), + curr->direction() == CGAL::ARR_LEFT_TO_RIGHT); + for (auto it = apx_points.begin(); it != apx_points.end()-1; ++it) { + const auto& apx_p = *it; + const QVector3D p(apx_p.dx(), apx_p.dy(), apx_p.dz()); + face_points.push_back(p); + } } while (++curr != first); all_faces_of_current_country.push_back(std::move(face_points)); @@ -212,7 +219,6 @@ Country_triangles_map Aos_triangulator::get_by_country(Aos::Arr_handle arrh) { auto& triangles = result[country_name]; // loop on all approximated faces for (auto& face_points : all_faces_of_current_country) { - //std::cout << "num face points = " << face_points.size() << std::endl; // no need to triangulate if the number of points is 3 if (face_points.size() == 3) { triangles.insert(triangles.end(), face_points.begin(), @@ -222,13 +228,12 @@ Country_triangles_map Aos_triangulator::get_by_country(Aos::Arr_handle arrh) { // find the centroid of all face-points QVector3D centroid(0, 0, 0); - for (const auto& fp : face_points) - centroid += fp; + for (const auto& fp : face_points) centroid += fp; centroid /= face_points.size(); centroid.normalize(); auto normal = centroid; - K::Point_3 plane_origin(centroid.x(), centroid.y(), centroid.z()); + K::Point_3 plane_origin(centroid.x(), centroid.y(), centroid.z()); K::Vector_3 plane_normal(normal.x(), normal.y(), normal.z()); K::Plane_3 plane(plane_origin, plane_normal); @@ -242,7 +247,7 @@ Country_triangles_map Aos_triangulator::get_by_country(Aos::Arr_handle arrh) { K::Ray_3 ray(origin, current_point); auto intersection = CGAL::intersection(plane, ray); - if (!intersection.has_value()) + if (! intersection.has_value()) std::cout << "INTERSECTION ASSERTION ERROR!!!\n"; auto ip = std::get(intersection.value()); auto ip2 = plane.to_2d(ip); diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Aos_triangulator.h b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Aos_triangulator.h index 7cdbbd99ff9a..8a49ea62aca5 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Aos_triangulator.h +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Aos_triangulator.h @@ -23,7 +23,7 @@ class Aos_triangulator { public: static std::vector get_all(Aos::Arr_handle arrh); - static Country_triangles_map get_by_country(Aos::Arr_handle arrh); + static Country_triangles_map get_by_country(Aos::Arr_handle arrh, float error); }; diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Main_widget.cpp b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Main_widget.cpp index 0511137f4ae3..61c2b68b7114 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Main_widget.cpp +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Main_widget.cpp @@ -73,6 +73,7 @@ void Main_widget::mousePressEvent(QMouseEvent* e) { m_camera_manip_rot->mousePressEvent(e); m_camera_manip_zoom->mousePressEvent(e); m_pick_handler->mousePressEvent(e); + update(); } //! \brief @@ -81,6 +82,7 @@ void Main_widget::mouseMoveEvent(QMouseEvent* e) { m_camera_manip_rot->mouseMoveEvent(e); m_camera_manip_zoom->mouseMoveEvent(e); m_pick_handler->mouseMoveEvent(e); + update(); } //! \brief @@ -89,10 +91,11 @@ void Main_widget::mouseReleaseEvent(QMouseEvent* e) { m_camera_manip_rot->mouseReleaseEvent(e); m_camera_manip_zoom->mouseReleaseEvent(e); m_pick_handler->mouseReleaseEvent(e); + update(); } //! \brief -void Main_widget::timerEvent(QTimerEvent*) { update(); } +// void Main_widget::timerEvent(QTimerEvent* event) { update(); } //! \brief void Main_widget::keyPressEvent(QKeyEvent* /* event */) {} @@ -104,57 +107,56 @@ void Main_widget::initializeGL() { QVector3D initial_mouse_pos(0, -1, 0); m_gr_mouse_vertex = std::make_unique(initial_mouse_pos); - // triangulation - { - qDebug() << "loading arrangement.."; - m_arrh = Aos::load_arr(m_file_name.toStdString()); - if (m_arrh == nullptr) { - std::string msg("Error: failed to load file "); - msg += m_file_name.toStdString(); - throw std::runtime_error(msg); - return; - } - - qDebug() << "generating triangles.."; - //auto triangle_points = Aos::get_triangles(arrh); - //auto triangle_points = Aos_triangulator::get_all(arrh); - //auto country_triangles_map = Aos::get_triangles_by_country(m_arrh); - auto country_triangles_map = Aos_triangulator::get_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); }; - for (auto& [country_name, triangle_points] : country_triangles_map) { - auto country_triangles = std::make_unique(triangle_points); - auto color = QVector4D(rndm(), rndm(), rndm(), 1); - auto m = std::max(color.x(), std::max(color.y(), color.z())); - color /= m; - color *= m_dimming_factor; - color.setW(1); - country_triangles->set_color(color); - //country_triangles->set_color(colors[color_map[country_name]]); - m_gr_country_triangles.emplace(country_name, std::move(country_triangles)); - } - - //qDebug() << "num triangles = " << triangle_points.size() / 3; - //m_gr_all_triangles = std::make_unique(triangle_points); - } - initializeOpenGLFunctions(); - init_camera(); init_geometry(); init_shader_programs(); m_current_approx_error = 0.001f; + + qDebug() << "loading arrangement.."; + m_arrh = Aos::load_arr(m_file_name.toStdString()); + if (m_arrh == nullptr) { + std::string msg("Error: failed to load file "); + msg += m_file_name.toStdString(); + throw std::runtime_error(msg); + return; + } + init_country_borders(m_current_approx_error); + qDebug() << "generating triangles.."; + //auto triangle_points = Aos::get_triangles(arrh); + //auto triangle_points = Aos_triangulator::get_all(arrh); + //auto country_triangles_map = Aos::get_triangles_by_country(m_arrh); + auto country_triangles_map = + Aos_triangulator::get_by_country(m_arrh, m_current_approx_error); + //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); }; + for (auto& [country_name, triangle_points] : country_triangles_map) { + auto country_triangles = std::make_unique(triangle_points); + auto color = QVector4D(rndm(), rndm(), rndm(), 1); + auto m = std::max(color.x(), std::max(color.y(), color.z())); + color /= m; + color *= m_dimming_factor; + color.setW(1); + country_triangles->set_color(color); + //country_triangles->set_color(colors[color_map[country_name]]); + m_gr_country_triangles.emplace(country_name, std::move(country_triangles)); + } + country_triangles_map.clear(); + + //qDebug() << "num triangles = " << triangle_points.size() / 3; + //m_gr_all_triangles = std::make_unique(triangle_points); + glClearColor(0, 0, 0, 1); glEnable(GL_DEPTH_TEST); // Enable depth buffer - //glEnable(GL_CULL_FACE); // Enable back face culling + glEnable(GL_CULL_FACE); // Enable back face culling // Use QBasicTimer because its faster than QTimer - m_timer.start(12, this); + // m_timer.start(12, this); } //! \brief diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Main_widget.h b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Main_widget.h index c2f69c2d0878..1a04f78bd757 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Main_widget.h +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2_earth/Main_widget.h @@ -56,7 +56,7 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase { void mousePressEvent(QMouseEvent* e) override; void mouseMoveEvent(QMouseEvent* e) override; void mouseReleaseEvent(QMouseEvent* e) override; - void timerEvent(QTimerEvent* e) override; + // void timerEvent(QTimerEvent* e) override; void keyPressEvent(QKeyEvent* event) override; void initializeGL() override; @@ -129,7 +129,7 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase { float m_current_approx_error; // Timer for continuous screen-updates - QBasicTimer m_timer; + // QBasicTimer m_timer; // <-- COMMON SETUP FOR ALL SCENES // -------------------------------