Skip to content

Commit

Permalink
started working on sphere-line intersection in GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Aug 14, 2023
1 parent 3b8aa1d commit b028dca
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 13 deletions.
1 change: 1 addition & 0 deletions Arrangement_on_surface_2/demo/earth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ source_group( "Graphics" FILES ${source_files_graphics} )
# GRAPHICS-GEOMETRY (Graphics-related)
file(GLOB source_files_graphics_geometry
Line_strips.h Line_strips.cpp
SingleVertex.h SingleVertex.cpp
Sphere.h Sphere.cpp
Triangles.h Triangles.cpp
Vertices.h Vertices.cpp
Expand Down
90 changes: 84 additions & 6 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,74 @@ void Main_widget::mousePressEvent(QMouseEvent* e)
// forward the event to the camera manipulators
m_camera_manip_rot->mousePressEvent(e);
m_camera_manip_zoom->mousePressEvent(e);

// handle country selection
if (e->button() == Qt::RightButton)
{
qDebug() << "RIGHT MOUSE BUTTON PRESSED!!";

auto p = e->pos();
QVector3D sp0(p.x(), m_vp_height - p.y(), 0);
QVector3D sp1(p.x(), m_vp_height - p.y(), 1);

auto proj = m_camera.get_projection_matrix();
auto view = m_camera.get_view_matrix();
auto model_view = view * m_model;
QRect viewport(0, 0, m_vp_width, m_vp_height);
auto wp0 = sp0.unproject(model_view, proj, viewport);
auto wp1 = sp1.unproject(model_view, proj, viewport);

// ASSERTION!!!
m_mouse_pos = wp0;

// define a ray from the camera pos to the world-point
//auto o = m_camera.get_pos();
//auto u = wp - o;
auto o = wp0;
auto u = wp1 - wp0;

std::cout << "camera pos = " << o << std::endl;

// solve the quadratic equation to check for intersection of ray with sphere
auto a = QVector3D::dotProduct(u, u);
auto b = 2 * QVector3D::dotProduct(u, o);
auto c = QVector3D::dotProduct(o, o) - 1;
auto d = b * b - 4 * a * c;

float ti = -1;
if (abs(d) < std::numeric_limits<float>::epsilon())
{
ti = -b / (2 * a);
qDebug() << "*** NUM INTERSECTIONS= 1, ti = " << ti;
}
else
{
if (d < 0)
{
qDebug() << "* NUM INTERSECTIONS = 0";

// no intersection
return;
}
else
{

auto sd = sqrt(d);
auto t1 = (-b - d) / (2 * a);
auto t2 = (-b + d) / (2 * a);
if (t1 > 0 && t2 > 0)
ti = std::min(t1, t2);
else if (t1 > 0)
ti = t1;
else
ti = t2;

qDebug() << "*** NUM INTERSECTIONS= 2, ti = " << ti;
}
}

m_mouse_pos = o + ti * u;
}
}
void Main_widget::mouseMoveEvent(QMouseEvent* e)
{
Expand Down Expand Up @@ -164,16 +232,16 @@ void Main_widget::init_problematic_nodes()
}



std::unique_ptr<Line_strips> new_faces;


void Main_widget::initializeGL()
{
// verify that the node (180.0, -84.71338) in Antarctica is redundant
//verify_antarctica_node_is_redundant();

//init_problematic_nodes();
m_mouse_pos = QVector3D(0, -1.1, 0);
m_mouse_vertex = std::make_unique<SingleVertex>(m_mouse_pos);


std::string data_path = "C:/work/gsoc2023/data/";
Expand Down Expand Up @@ -314,6 +382,9 @@ void Main_widget::init_camera()
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);

// this makes z-axes point upwards!
//m_model.rotate(-90, 1, 0, 0);

// register the zoom-changed function
Message_manager::add("zoom_changed", [&]
Expand Down Expand Up @@ -525,17 +596,15 @@ void Main_widget::paintGL()
m_update_approx_error = false;
}

QMatrix4x4 model;
model.rotate(-90, 1,0,0); // this makes z-axes point upwards!
const auto view = m_camera.get_view_matrix();
const auto projection = m_camera.get_projection_matrix();
const auto mvp = projection * view * model;
const auto mvp = projection * view * m_model;

// compute the cutting plane
// remember that we are passing the local vertex positions of the sphere
// between the vertex and fragment shader stages, so we need to convert
// the camera-pos in world coords to sphere's local coords!
auto c = model.inverted() * m_camera.get_pos();
auto c = m_model.inverted() * m_camera.get_pos();
const auto d = c.length();
const auto r = 1.0f;
const auto sin_alpha = r / d;
Expand Down Expand Up @@ -641,6 +710,15 @@ void Main_widget::paintGL()
sp.set_uniform("u_color", QVector4D(1, 0, 0, 1));
//new_faces->draw();

{
glPointSize(5);
sp.set_uniform("u_color", QVector4D(1, 0, 0, 1));
//auto pos = m_mouse_vertex->get_pos();
//pos.setX(pos.x() + 0.01);
//m_mouse_vertex->set_pos(pos);
m_mouse_vertex->set_pos(m_mouse_pos);
draw_safe(m_mouse_vertex);
}

sp.unuse();
}
Expand Down
5 changes: 5 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Main_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "Kml_reader.h"
#include "Line_strips.h"
#include "Shader_program.h"
#include "SingleVertex.h"
#include "Sphere.h"
#include "Triangles.h"
#include "Vertices.h"
Expand Down Expand Up @@ -79,6 +80,9 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase
std::unique_ptr<Line_strips> m_geodesic_arcs;
std::unique_ptr<Vertices> m_vertices, m_problematic_vertices;
std::unique_ptr<Line_strips> m_identification_curve;

QVector3D m_mouse_pos;
std::unique_ptr<SingleVertex> m_mouse_vertex;

// COUNTRY DATA
Kml::Placemarks m_countries;
Expand All @@ -105,6 +109,7 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase
Camera m_camera;
std::unique_ptr<Camera_manip> m_camera_manip_rot;
std::unique_ptr<Camera_manip> m_camera_manip_zoom;
QMatrix4x4 m_model;

// view-port
int m_vp_width = 0, m_vp_height = 0;
Expand Down
81 changes: 81 additions & 0 deletions Arrangement_on_surface_2/demo/earth/SingleVertex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright(c) 2012, 2020 Tel - Aviv University(Israel).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s): Engin Deniz Diktas <[email protected]>

#include "SingleVertex.h"


SingleVertex::SingleVertex(const QVector3D& pos)
{
initializeOpenGLFunctions();

m_pos = pos;
m_visible = true;

// DEFINE OPENGL BUFFERS
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);

// Vertex Buffer
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
auto vertex_buffer_size = sizeof(m_pos);
auto vertex_buffer_data = reinterpret_cast<const void*>(&m_pos);
glBufferData(GL_ARRAY_BUFFER,
vertex_buffer_size,
vertex_buffer_data,
GL_DYNAMIC_DRAW);

// Position Vertex-Attribute
GLint position_attrib_index = 0;
const void* position_offset = 0;
GLsizei stride = 0;
glVertexAttribPointer(position_attrib_index,
3,
GL_FLOAT, GL_FALSE,
stride,
position_offset);
glEnableVertexAttribArray(position_attrib_index);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}


void SingleVertex::set_visible(bool flag)
{
m_visible = flag;
}
void SingleVertex::set_pos(const QVector3D& pos)
{
m_pos = pos;
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
auto vertex_buffer_size = sizeof(m_pos);
auto vertex_buffer_data = reinterpret_cast<const void*>(&m_pos);
auto offset = 0;
glBufferSubData(GL_ARRAY_BUFFER,
offset,
vertex_buffer_size,
vertex_buffer_data);
}
const QVector3D& SingleVertex::get_pos() const
{
return m_pos;
}


void SingleVertex::draw()
{
if (m_visible)
{
glBindVertexArray(m_vao);
glDrawArrays(GL_POINTS, 0, 1);
glBindVertexArray(0);
}
}

36 changes: 36 additions & 0 deletions Arrangement_on_surface_2/demo/earth/SingleVertex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright(c) 2012, 2020 Tel - Aviv University(Israel).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s): Engin Deniz Diktas <[email protected]>

#ifndef SINGLE_VERTEX_H
#define SINGLE_VERTEX_H

#include <vector>
#include <qvector3d.h>
#include "Common_defs.h"


class SingleVertex : protected OpenGLFunctionsBase
{
public:
SingleVertex(const QVector3D& pos);

void set_visible(bool flag);
void set_pos(const QVector3D& pos);
const QVector3D& get_pos() const;

void draw();

private:
bool m_visible;
GLuint m_vao, m_vbo;
QVector3D m_pos;
};


#endif
14 changes: 7 additions & 7 deletions Arrangement_on_surface_2/demo/earth/Vertices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ Vertices::Vertices(const std::vector<QVector3D>& vertices)
auto vertex_buffer_size = sizeof(QVector3D) * vertex_data.size();
auto vertex_buffer_data = reinterpret_cast<const void*>(vertex_data.data());
glBufferData(GL_ARRAY_BUFFER,
vertex_buffer_size,
vertex_buffer_data,
GL_STATIC_DRAW);
vertex_buffer_size,
vertex_buffer_data,
GL_STATIC_DRAW);

// Position Vertex-Attribute
GLint position_attrib_index = 0;
const void* position_offset = 0;
GLsizei stride = 0;
glVertexAttribPointer(position_attrib_index,
3,
GL_FLOAT, GL_FALSE,
stride,
position_offset);
3,
GL_FLOAT, GL_FALSE,
stride,
position_offset);
glEnableVertexAttribArray(position_attrib_index);

glBindBuffer(GL_ARRAY_BUFFER, 0);
Expand Down

0 comments on commit b028dca

Please sign in to comment.