Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.1k
Improve RectangleSelector Rotation revisited and rebased#26833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
8063e15728a74923e3bc078d0f025ba3b5fb7f12abc3b7e4bfc74f9527c1c11File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Co-authored-by: David Stansby <dstansby@gmail.com>
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -82,19 +82,20 @@ def test_rectangle_selector(ax, kwargs): | ||
| # purposely drag outside of axis for release | ||
| do_event(tool, 'release', xdata=250, ydata=250, button=1) | ||
| do_event(tool, 'onmove', xdata=250, ydata=250, button=1) | ||
| if kwargs.get('drawtype', None) not in ['line', 'none']: | ||
| assert_allclose(tool.geometry, | ||
| [[100, 100, 199, 199, 100], | ||
| [100, 199, 199, 100, 100]], | ||
| err_msg=tool.geometry) | ||
| onselect.assert_called_once() | ||
| (epress, erelease), kwargs = onselect.call_args | ||
| assert epress.xdata == 100 | ||
| assert epress.ydata == 100 | ||
| assert erelease.xdata ==200 | ||
| assert erelease.ydata ==200 | ||
| assert kwargs == {} | ||
| @@ -181,10 +182,16 @@ def test_rectangle_selector_set_props_handle_props(ax): | ||
| assert artist.get_alpha() == 0.3 | ||
| # Should give same results if rectangle is created from any two | ||
| # opposite corners | ||
| @pytest.mark.parametrize('start, end', [[(0, 10), (100, 120)], | ||
| [(100, 120), (0, 10)], | ||
| [(0, 120), (100, 10)], | ||
| [(100, 10), (0, 120)]]) | ||
| def test_rectangle_resize(ax, start, end): | ||
| tool = widgets.RectangleSelector(ax, interactive=True) | ||
| # Create rectangle | ||
| click_and_drag(tool, start=start,end=end) | ||
| assert_allclose(tool.extents, (0.0, 100.0, 10.0, 120.0)) | ||
| # resize NE handle | ||
| @@ -305,16 +312,16 @@ def test_rectangle_resize_center(ax, add_state): | ||
| xdata_new, ydata_new = xdata + xdiff, ydata + ydiff | ||
| click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new), | ||
| key=use_key) | ||
| assert_allclose(tool.extents, (xdata_new, extents[1] - xdiff, | ||
| ydata_new, extents[3] - ydiff)) | ||
| @pytest.mark.parametrize('add_state', [True, False]) | ||
| def test_rectangle_resize_square(ax, add_state): | ||
| tool = widgets.RectangleSelector(ax, interactive=True) | ||
| # Create rectangle | ||
| click_and_drag(tool, start=(70, 65), end=(120, 115)) | ||
| assert_allclose(tool.extents,(70.0, 120.0, 65.0, 115.0)) | ||
| if add_state: | ||
| tool.add_state('square') | ||
| @@ -329,8 +336,8 @@ def test_rectangle_resize_square(ax, add_state): | ||
| xdata_new, ydata_new = xdata + xdiff, ydata + ydiff | ||
| click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new), | ||
| key=use_key) | ||
| assert_allclose(tool.extents, (extents[0], xdata_new, | ||
| extents[2], extents[3] + xdiff)) | ||
| # resize E handle | ||
| extents = tool.extents | ||
| @@ -379,11 +386,12 @@ def test_rectangle_resize_square(ax, add_state): | ||
| xdata_new, ydata_new = xdata + xdiff, ydata + ydiff | ||
| click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new), | ||
| key=use_key) | ||
| assert_allclose(tool.extents, (xdata_new, extents[1], | ||
| extents[2] + xdiff, extents[3])) | ||
| def test_rectangle_resize_square_center(ax): | ||
| ax.set_aspect(1) | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. What's the reason for having to add this? (here and in some other tests too) | ||
| tool = widgets.RectangleSelector(ax, interactive=True) | ||
| # Create rectangle | ||
| click_and_drag(tool, start=(70, 65), end=(120, 115)) | ||
| @@ -449,6 +457,7 @@ def test_rectangle_resize_square_center(ax): | ||
| @pytest.mark.parametrize('selector_class', | ||
| [widgets.RectangleSelector, widgets.EllipseSelector]) | ||
| def test_rectangle_rotate(ax, selector_class): | ||
| ax.set_aspect(1) | ||
| tool = selector_class(ax, interactive=True) | ||
| # Draw rectangle | ||
| click_and_drag(tool, start=(100, 100), end=(130, 140)) | ||
| @@ -462,19 +471,19 @@ def test_rectangle_rotate(ax, selector_class): | ||
| click_and_drag(tool, start=(130, 140), end=(120, 145)) | ||
| do_event(tool, 'on_key_press', key='r') | ||
| assert len(tool._state) == 0 | ||
| # Extents change asthe selector remains rigid in display coordinates | ||
| assert_allclose(tool.extents, (110.10, 119.90, 95.49, 144.51), atol=0.01) | ||
| assert_allclose(tool.rotation, 25.56, atol=0.01) | ||
| tool.rotation = 45 | ||
| assert tool.rotation == 45 | ||
| # Corners should move | ||
| assert_allclose(tool.corners, | ||
| np.array([[110.10, 131.31, 103.03, 81.81], | ||
| [95.49, 116.70, 144.98, 123.77]]), atol=0.01) | ||
| # Scale using top-right corner | ||
| click_and_drag(tool, start=(110, 145), end=(110, 160)) | ||
| assert_allclose(tool.extents, (110, 110, 145, 160), atol=0.01) | ||
| if selector_class == widgets.RectangleSelector: | ||
| with pytest.raises(ValueError): | ||
| @@ -496,36 +505,38 @@ def test_rectangle_add_remove_set(ax): | ||
| @pytest.mark.parametrize('use_data_coordinates', [False, True]) | ||
| def test_rectangle_resize_square_center_aspect(ax, use_data_coordinates): | ||
| ax = get_ax() | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Why the call to | ||
| ax.set_aspect(0.5) | ||
| # Need to call a draw to update ax.transData | ||
| plt.gcf().canvas.draw() | ||
| tool = widgets.RectangleSelector(ax, interactive=True, | ||
| use_data_coordinates=use_data_coordinates) | ||
| tool.add_state('square') | ||
| tool.add_state('center') | ||
| # Create rectangle, width 50 in data coordinates | ||
| click_and_drag(tool, start=(70, 65), end=(120, 75)) | ||
| if use_data_coordinates: | ||
| assert_allclose(tool.extents, (20, 120, 15, 115)) | ||
| else: | ||
| assert_allclose(tool.extents, (20, 120, -35, 165)) | ||
| # resize E handle | ||
| extents = tool.extents | ||
| xdata, ydata = extents[1], extents[3] | ||
| xdiff = 10 | ||
| xdata_new, ydata_new = xdata + xdiff, ydata | ||
| if use_data_coordinates: | ||
| # In data coordinates the difference should be equal in both directions | ||
| ydiff = xdiff | ||
| else: | ||
| # In display coordinates, the change in data coordinates should be | ||
| # different in each direction | ||
| ydiff = xdiff / tool.ax._get_aspect_ratio() | ||
| click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new)) | ||
| assert_allclose(tool.extents, [extents[0] - xdiff, xdata_new, | ||
| extents[2] - ydiff, extents[3] + ydiff]) | ||
| def test_ellipse(ax): | ||
| @@ -1631,6 +1642,7 @@ def test_polygon_selector_verts_setter(fig_test, fig_ref, draw_bounding_box): | ||
| ] | ||
| for (etype, event_args) in event_sequence: | ||
| do_event(tool_ref, etype, **event_args) | ||
| np.testing.assert_allclose(tool_ref.verts, verts) | ||
| def test_polygon_selector_box(ax): | ||
| @@ -1645,10 +1657,19 @@ def test_polygon_selector_box(ax): | ||
| *polygon_place_vertex(*verts[0]), | ||
| ] | ||
| # Set smaller axes limits to reduce errors in converting from data to | ||
| # display coords. The canvas size is 640 x 640, so we need a tolerance of | ||
| # (data width / canvas width) = 50 / 640 ~ 0.08 when comparing points in | ||
| # data space | ||
| ax.set_xlim(-5, 45) | ||
| ax.set_ylim(-5, 45) | ||
| atol = 0.08 | ||
| # Create selector | ||
| tool = widgets.PolygonSelector(ax, draw_bounding_box=True) | ||
| for (etype, event_args) in event_sequence: | ||
| do_event(tool, etype, **event_args) | ||
| np.testing.assert_allclose(tool.verts, verts, atol=atol) | ||
| # In order to trigger the correct callbacks, trigger events on the canvas | ||
| # instead of the individual tools | ||
| @@ -1663,7 +1684,7 @@ def test_polygon_selector_box(ax): | ||
| MouseEvent( | ||
| "button_release_event", canvas, *t.transform((20, 20)), 1)._process() | ||
| np.testing.assert_allclose( | ||
| tool.verts, [(10, 0), (0, 10), (10, 20), (20, 10)], atol=atol) | ||
| # Move using the center of the bounding box | ||
| MouseEvent( | ||
| @@ -1673,20 +1694,20 @@ def test_polygon_selector_box(ax): | ||
| MouseEvent( | ||
| "button_release_event", canvas, *t.transform((30, 30)), 1)._process() | ||
| np.testing.assert_allclose( | ||
| tool.verts, [(30, 20), (20, 30), (30, 40), (40, 30)], atol=atol) | ||
| # Remove a point from the polygon and check that the box extents update | ||
| np.testing.assert_allclose( | ||
| tool._box.extents, (20.0, 40.0, 20.0, 40.0), atol=atol) | ||
| MouseEvent( | ||
| "button_press_event", canvas, *t.transform((30, 20)), 3)._process() | ||
| MouseEvent( | ||
| "button_release_event", canvas, *t.transform((30, 20)), 3)._process() | ||
| np.testing.assert_allclose( | ||
| tool.verts, [(20, 30), (30, 40), (40, 30)], atol=atol) | ||
| np.testing.assert_allclose( | ||
| tool._box.extents, (20.0, 40.0, 30.0, 40.0), atol=atol) | ||
| def test_polygon_selector_clear_method(ax): | ||