Skip to content

Get nearest graphics indices #699

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all 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:
"""
Map screen position to world position

Expand Down
41 changes: 34 additions & 7 deletions fastplotlib/utils/_plot_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
from ..graphics._collection_base import GraphicCollection


def get_nearest_graphics(
def get_nearest_graphics_indices(
pos: tuple[float, float] | tuple[float, float, float],
graphics: Sequence[Graphic] | GraphicCollection,
) -> np.ndarray[Graphic]:
) -> 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 +26,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 +50,31 @@ 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]
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy