@@ -468,31 +468,37 @@ void ScatterplotPlugin::samplePoints()
468468 std::vector<std::uint32_t > localGlobalIndices;
469469
470470 _positionDataset->getGlobalIndices (localGlobalIndices);
471-
472- auto & zoomRectangleAction = _scatterPlotWidget->getNavigationAction ().getZoomRectangleAction ();
473-
474- const auto width = selectionAreaImage.width ();
475- const auto height = selectionAreaImage.height ();
476- const auto size = width < height ? width : height;
477- const auto uvOffset = QPoint ((selectionAreaImage.width () - size) / 2 .0f , (selectionAreaImage.height () - size) / 2 .0f );
478-
479- QPointF pointUvNormalized;
480-
481- QPoint pointUv, mouseUv = _scatterPlotWidget->mapFromGlobal (QCursor::pos ());
482471
483472 std::vector<char > focusHighlights (_positions.size ());
484473
485474 std::vector<std::pair<float , std::uint32_t >> sampledPoints;
486475
476+ auto & pointRenderer = _scatterPlotWidget->_pointRenderer ;
477+ auto & navigator = pointRenderer.getNavigator ();
478+
479+ const auto zoomRectangleWorld = navigator.getZoomRectangleWorld ();
480+ const auto screenRectangle = QRect (QPoint (), pointRenderer.getRenderSize ());
481+ const auto mousePositionWorld = pointRenderer.getScreenPointToWorldPosition (pointRenderer.getNavigator ().getViewMatrix (), _scatterPlotWidget->mapFromGlobal (QCursor::pos ()));
482+
483+ // Go over all points in the dataset to see if they should be sampled
487484 for (std::uint32_t localPointIndex = 0 ; localPointIndex < _positions.size (); localPointIndex++) {
488- pointUvNormalized = QPointF ((_positions[localPointIndex].x - zoomRectangleAction.getLeft ()) / zoomRectangleAction.getWidth (), (zoomRectangleAction.getTop () - _positions[localPointIndex].y ) / zoomRectangleAction.getHeight ());
489- pointUv = uvOffset + QPoint (pointUvNormalized.x () * size, pointUvNormalized.y () * size);
490485
491- if (pointUv.x () >= selectionAreaImage.width () || pointUv.x () < 0 || pointUv.y () >= selectionAreaImage.height () || pointUv.y () < 0 )
486+ // Compute the offset of the point in the world space
487+ const auto pointOffsetWorld = QPointF (_positions[localPointIndex].x - zoomRectangleWorld.left (), _positions[localPointIndex].y - zoomRectangleWorld.top ());
488+
489+ // Normalize it
490+ const auto pointOffsetWorldNormalized = QPointF (pointOffsetWorld.x () / zoomRectangleWorld.width (), pointOffsetWorld.y () / zoomRectangleWorld.height ());
491+
492+ // Convert it to screen space
493+ const auto pointOffsetScreen = QPoint (pointOffsetWorldNormalized.x () * screenRectangle.width (), screenRectangle.height () - pointOffsetWorldNormalized.y () * screenRectangle.height ());
494+
495+ // Continue to next point if the point is outside the screen
496+ if (!screenRectangle.contains (pointOffsetScreen))
492497 continue ;
493498
494- if (selectionAreaImage.pixelColor (pointUv).alpha () > 0 ) {
495- const auto sample = std::pair<float , std::uint32_t >((QVector2D (mouseUv) - QVector2D (pointUv)).length (), localPointIndex);
499+ // If the corresponding pixel is not transparent, add the point to the selection
500+ if (selectionAreaImage.pixelColor (pointOffsetScreen).alpha () > 0 ) {
501+ const auto sample = std::pair ((QVector2D (_positions[localPointIndex].x , _positions[localPointIndex].y ) - mousePositionWorld.toVector2D ()).length (), localPointIndex);
496502
497503 sampledPoints.emplace_back (sample);
498504 }
0 commit comments