Skip to content

Commit

Permalink
Suppress time events; fixed triangulation
Browse files Browse the repository at this point in the history
  • Loading branch information
efifogel committed Apr 15, 2024
1 parent 0de0692 commit 5a7b89b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ std::vector<QVector3D> Aos_triangulator::get_all(Aos::Arr_handle arrh) {

std::vector<std::vector<QVector3D>> 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<QVector3D> 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());
Expand Down Expand Up @@ -157,7 +157,9 @@ std::vector<QVector3D> 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<K>;
using Fb = CGAL::Constrained_triangulation_face_base_2<K>;
Expand All @@ -167,19 +169,19 @@ 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<K>;
using Approximate_point_2 = Geom_traits::Approximate_point_2;

auto& arr = *reinterpret_cast<Countries_arr*>(arrh.get());
auto approx = s_traits.approximate_2_object();

// group the faces by their country name
using Face_ = Countries_arr::Face_handle::value_type;
std::map<std::string, std::vector<Face_*>> 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);
}

Expand All @@ -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<QVector3D>;
using Faces_ = std::vector<Face_points>;
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;
Expand All @@ -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<Approximate_point_2> 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));
Expand All @@ -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(),
Expand All @@ -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);

Expand All @@ -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<K::Point_3>(intersection.value());
auto ip2 = plane.to_2d(ip);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Aos_triangulator {
public:
static std::vector<QVector3D> 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);
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 */) {}
Expand All @@ -104,57 +107,56 @@ void Main_widget::initializeGL() {
QVector3D initial_mouse_pos(0, -1, 0);
m_gr_mouse_vertex = std::make_unique<Single_vertex>(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<Triangles>(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<Triangles>(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<Triangles>(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<Triangles>(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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
// -------------------------------
Expand Down

0 comments on commit 5a7b89b

Please sign in to comment.