ENH: Support open polygonal chains in PolygonSelector#31623
ENH: Support open polygonal chains in PolygonSelector#31623lucaznch wants to merge 1 commit intomatplotlib:mainfrom
Conversation
Extend PolygonSelector with open polygonal chain support by adding a new parameter to explicitly control whether the chain is open or closed. The selection of an open polygonal chain is completed by pressing the Enter key after inserting a vertex. The existing interactive editing functionality also applies to open polygonal chains. Extend existing tests to cover the new mode. Closes matplotlib#28421 Co-authored-by: Mafalda Botelho <mafaldabotelho@tecnico.ulisboa.pt>
|
Opening this PR as a draft since it's still in progress. The core implementation is in place but it still needs more tests and documentation, and possibly some small code improvements. And it is also meant to allow early feedback on the chosen approach and related aspects. |
|
I didn't look at the code yet, but the extension conceptually makes sense, and the implementation via a |
|
Thanks for the feedback! When starting the implementation we were actually leaning towards the third approach mentioned
Although it would make the PR a bit more complex, we believe it would be worthwhile since it would centralize the shared logic and provide a cleaner architecture with clearer semantics for both cases. We also believe the level of difficulty would still be appropriate for us. We ended up going with the current approach because it seemed like the simplest and least invasive starting point, but we are very interested in hearing thoughts about this alternative approach as well. |
PR summary
PolygonSelectoris a widget class used to select a polygonal region within an Axes. More precisely, it enables the selection of a closed polygonal chain, i.e., a sequence of vertices connected by line segments where the first and last vertices are connected. These chains may be non-self-intersecting or self-intersecting. Before or after a polygon is defined, the class provides interactive editing capabilities such as repositioning and removing vertices.An open polygonal chain is a sequence of vertices connected by line segments but where the first and last vertices are not connected. The proposed feature is to enable both programmatic and interactive selection of open polygonal chains within a widget class, including both non-self-intersecting and self-intersecting chains.
The approach is to extend the current
PolygonSelectorclass by adding a new parameter,to explicitly control whether the polygonal chain is open or closed. By default the parameter selects closed polygonal regions, preserving the current behavior.
The selection of an open polygonal chain is completed by pressing the Enter key after inserting a vertex. The existing interactive editing functionality is preserved and applies consistently to both modes.
Why is this change necessary / what problem does it solve?
PolygonSelectorcurrently supports only closed polygonal regions, whileLassoSelectorprovides freeform selection without vertex control. There is no widget that supports structured, vertex-based selection of open polygonal chains, which are useful for tasks such as defining paths, annotations, or piecewise geometries where closure is not desired.What is the reasoning for this implementation?
Several implementation approaches were considered in addition to the current one:
Introduce an
OpenPolygonalChainSelectorclass dedicated to open chains.Introduce a
PolygonalChainSelectorclass supporting both open and closed chains.Introduce an internal
_PolygonalChainSelectorbase class to provide common functionality for the existingPolygonSelectorand a newOpenPolygonalChainSelector.Other design approaches were also considered:
For a class supporting both chain types, infer whether the chain is open or closed based on user interaction, instead of relying on a new explicit parameter.
Use the Strategy behavioral design pattern in the current implementation to remove the conditional branches (
if self.closed ... else ...) added to handle open and closed chain behavior.The chosen approach is the simplest one. It introduces a minimal API change and serves as an effective starting point. It preserves the current default behavior and has explicit, predictable behavior, which facilitates testing and documentation. The main drawback is a slight semantic mismatch, as
PolygonSelectornow supports both closed and open chains.Example
Co-authored-by: @maf2310
Closes #28421
AI Disclosure
AI was used to help proofread the grammar of this PR description and to understand some parts of the existing codebase during development. The code changes and design decisions were developed independently.
PR checklist