Skip to content

Commit fe19581

Browse files
authored
Point Cloud Editor: fix select2D after switch to QOpenGLWidget (#5685)
* Point Cloud Editor: fix select2D after switch to QOpenGLWidget In commit 38cbd62 the CloudEditorWidget was switched from QGLWidget to QOpenGLWidget to be compatible with Qt6. The select2D tool needs the viewport and the projection matrix, which are loaded with `glGetIntegerv(GL_VIEWPORT,viewport)` and `glGetFloatv(GL_PROJECTION_MATRIX, project)` (they are set in `CloudEditorWidget::resizeGL`). However, since the switch to QOpenGLWidget, the received projection matrix is just the identity matrix, and the viewport does not look correct either. It seems that QOpenGLWidget overwrites them. These changes introduce variables in CloudEditorWidget to safely store the viewport and projection matrix, and passes a function to the select2D tool to ask for them when needed. * Add include for windows * Additional fix for hdpi mac displays
1 parent f2b9f23 commit fe19581

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

apps/point_cloud_editor/include/pcl/apps/point_cloud_editor/cloudEditorWidget.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,5 +319,9 @@ class CloudEditorWidget : public QOpenGLWidget
319319
/// a dialog displaying the statistics of the cloud editor
320320
StatisticsDialog stat_dialog_;
321321

322+
/// the viewport, set by resizeGL
323+
std::array<GLint, 4> viewport_;
322324

325+
/// the projection matrix, set by resizeGL
326+
std::array<GLfloat, 16> projection_matrix_;
323327
};

apps/point_cloud_editor/include/pcl/apps/point_cloud_editor/select2DTool.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class Select2DTool : public ToolInterface
5757
/// @brief Constructor
5858
/// @param selection_ptr a shared pointer pointing to the selection object.
5959
/// @param cloud_ptr a shared pointer pointing to the cloud object.
60-
Select2DTool (SelectionPtr selection_ptr, CloudPtr cloud_ptr);
60+
/// @param get_viewport_and_projection_mat a function that can be used to get the viewport and the projection matrix
61+
Select2DTool (SelectionPtr selection_ptr, CloudPtr cloud_ptr, std::function<void(GLint*,GLfloat*)> get_viewport_and_projection_mat);
6162

6263
/// @brief Destructor
6364
~Select2DTool () override;
@@ -154,4 +155,6 @@ class Select2DTool : public ToolInterface
154155
/// switch for selection box rendering
155156
bool display_box_;
156157

158+
/// function to get the viewport and the projection matrix (initialized by ctor)
159+
std::function<void(GLint*,GLfloat*)> get_viewport_and_projection_mat_;
157160
};

apps/point_cloud_editor/src/cloudEditorWidget.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
#ifdef OPENGL_IS_A_FRAMEWORK
4848
# include <OpenGL/glu.h>
4949
#else
50+
# ifdef _WIN32
51+
# include <windows.h>
52+
# endif // _WIN32
5053
# include <GL/glu.h>
5154
#endif
5255

@@ -201,7 +204,7 @@ CloudEditorWidget::select2D ()
201204
if (!cloud_ptr_)
202205
return;
203206
tool_ptr_ = std::shared_ptr<Select2DTool>(new Select2DTool(selection_ptr_,
204-
cloud_ptr_));
207+
cloud_ptr_, [this](GLint * viewport, GLfloat * projection_matrix){ std::copy_n(this->viewport_.begin(), 4, viewport); std::copy_n(this->projection_matrix_.begin(), 16, projection_matrix); }));
205208
update();
206209
}
207210

@@ -472,11 +475,16 @@ CloudEditorWidget::paintGL ()
472475
void
473476
CloudEditorWidget::resizeGL (int width, int height)
474477
{
478+
const auto ratio = this->devicePixelRatio();
479+
width = static_cast<int>(width*ratio);
480+
height = static_cast<int>(height*ratio);
475481
glViewport(0, 0, width, height);
482+
viewport_ = {0, 0, width, height};
476483
cam_aspect_ = double(width) / double(height);
477484
glMatrixMode(GL_PROJECTION);
478485
glLoadIdentity();
479486
gluPerspective(cam_fov_, cam_aspect_, cam_near_, cam_far_);
487+
glGetFloatv(GL_PROJECTION_MATRIX, projection_matrix_.data());
480488
glMatrixMode(GL_MODELVIEW);
481489
glLoadIdentity();
482490
}

apps/point_cloud_editor/src/select2DTool.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ const float Select2DTool::DEFAULT_TOOL_DISPLAY_COLOR_GREEN_ = 1.0f;
4848
const float Select2DTool::DEFAULT_TOOL_DISPLAY_COLOR_BLUE_ = 1.0f;
4949

5050

51-
Select2DTool::Select2DTool (SelectionPtr selection_ptr, CloudPtr cloud_ptr)
52-
: selection_ptr_(std::move(selection_ptr)), cloud_ptr_(std::move(cloud_ptr)), display_box_(false)
51+
Select2DTool::Select2DTool (SelectionPtr selection_ptr, CloudPtr cloud_ptr, std::function<void(GLint*,GLfloat*)> get_viewport_and_projection_mat)
52+
: selection_ptr_(std::move(selection_ptr)), cloud_ptr_(std::move(cloud_ptr)), display_box_(false), get_viewport_and_projection_mat_(get_viewport_and_projection_mat)
5353
{
5454
}
5555

@@ -88,10 +88,9 @@ Select2DTool::end (int x, int y, BitMask modifiers, BitMask)
8888
return;
8989

9090
GLint viewport[4];
91-
glGetIntegerv(GL_VIEWPORT,viewport);
9291
IndexVector indices;
9392
GLfloat project[16];
94-
glGetFloatv(GL_PROJECTION_MATRIX, project);
93+
get_viewport_and_projection_mat_(viewport, project);
9594

9695
Point3DVector ptsvec;
9796
cloud_ptr_->getDisplaySpacePoints(ptsvec);

0 commit comments

Comments
 (0)