Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fastplotlib/layouts/_plot_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def get_rect(self) -> tuple[float, float, float, float]:

def map_screen_to_world(
self, pos: tuple[float, float] | pygfx.PointerEvent
) -> np.ndarray:
) -> np.ndarray | None:
Comment thread
FlynnOConnell marked this conversation as resolved.
"""
Map screen position to world position

Expand Down
45 changes: 35 additions & 10 deletions fastplotlib/utils/_plot_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from ..graphics._base import Graphic
from ..graphics._collection_base import GraphicCollection


def get_nearest_graphics(
pos: tuple[float, float] | tuple[float, float, float],
graphics: Sequence[Graphic] | GraphicCollection,
) -> np.ndarray[Graphic]:
def get_nearest_graphics_indices(
pos: tuple[float, float] | tuple[float, float, float],
graphics: Sequence[Graphic] | GraphicCollection,
) -> np.ndarray[int]:
"""
Returns the nearest ``graphics`` to the passed position ``pos`` in world space.
Uses the distance between ``pos`` and the center of the bounding sphere for each graphic.
Returns indices of the nearest ``graphics`` to the passed position ``pos`` in world space
in order of closest to furtherst. Uses the distance between ``pos`` and the center of the
bounding sphere for each graphic.

Parameters
----------
Expand All @@ -25,11 +25,10 @@ def get_nearest_graphics(

Returns
-------
tuple[Graphic]
nearest graphics to ``pos`` in order
ndarray[int]
indices of the nearest nearest graphics to ``pos`` in order

"""

if isinstance(graphics, GraphicCollection):
graphics = graphics.graphics

Expand All @@ -50,4 +49,30 @@ def get_nearest_graphics(
distances = np.linalg.norm(centers[:, : len(pos)] - pos, ord=2, axis=1)

sort_indices = np.argsort(distances)
return sort_indices

def get_nearest_graphics(
pos: tuple[float, float] | tuple[float, float, float],
graphics: Sequence[Graphic] | GraphicCollection,
) -> np.ndarray[Graphic]:
"""
Returns the nearest ``graphics`` to the passed position ``pos`` in world space.
Uses the distance between ``pos`` and the center of the bounding sphere for each graphic.

Parameters
----------
pos: (x, y) | (x, y, z)
position in world space, z-axis is ignored when calculating L2 norms if ``pos`` is 2D

graphics: Sequence, i.e. array, list, tuple, etc. of Graphic | GraphicCollection
the graphics from which to return a sorted array of graphics in order of closest
to furthest graphic

Returns
-------
ndarray[Graphic]
nearest graphics to ``pos`` in order

"""
sort_indices = get_nearest_graphics_indices(pos, graphics)
return np.asarray(graphics)[sort_indices]