-
Notifications
You must be signed in to change notification settings - Fork 54
Get nearest graphics #519
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
Get nearest graphics #519
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
56a22c1
bugfix
kushalkolar 39ce0af
center works, need to figure out what's wrong with edge
kushalkolar d1cc1ca
allow passing graphic collection to subset
kushalkolar 12a3990
Merge branch 'main' of https://github.com/fastplotlib/fastplotlib
kushalkolar fee3d8d
allow passing graphic collection to subset
kushalkolar 5db9d3e
move get_nearest_graphics to utils
kushalkolar 5aef21d
move stuff
kushalkolar 440d9f8
test for get nearest
kushalkolar 3a79eb7
Merge branch 'get-nearest' of https://github.com/fastplotlib/fastplot…
kushalkolar 7b41a1e
black
kushalkolar 196972b
more black
kushalkolar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from typing import Sequence | ||
|
||
import numpy as np | ||
|
||
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]: | ||
""" | ||
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 | ||
------- | ||
tuple[Graphic] | ||
nearest graphics to ``pos`` in order | ||
|
||
""" | ||
|
||
if isinstance(graphics, GraphicCollection): | ||
graphics = graphics.graphics | ||
|
||
if not all(isinstance(g, Graphic) for g in graphics): | ||
raise TypeError("all elements of `graphics` must be Graphic objects") | ||
|
||
pos = np.asarray(pos) | ||
|
||
if pos.shape != (2,) or not pos.shape != (3,): | ||
raise TypeError | ||
|
||
# get centers | ||
centers = np.empty(shape=(len(graphics), len(pos))) | ||
for i in range(centers.shape[0]): | ||
centers[i] = graphics[i].world_object.get_world_bounding_sphere()[: len(pos)] | ||
|
||
# l2 | ||
distances = np.linalg.norm(centers[:, : len(pos)] - pos, ord=2, axis=1) | ||
|
||
sort_indices = np.argsort(distances) | ||
return np.asarray(graphics)[sort_indices] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import numpy as np | ||
import fastplotlib as fpl | ||
|
||
|
||
def make_circle(center, radius: float, n_points: int = 75) -> np.ndarray: | ||
theta = np.linspace(0, 2 * np.pi, n_points) | ||
xs = radius * np.sin(theta) | ||
ys = radius * np.cos(theta) | ||
|
||
return np.column_stack([xs, ys]) + center | ||
|
||
|
||
def test_get_nearest_graphics(): | ||
circles = list() | ||
|
||
centers = [[0, 0], [0, 20], [20, 0], [20, 20]] | ||
|
||
for center in centers: | ||
circles.append(make_circle(center, 5, n_points=75)) | ||
|
||
fig = fpl.Figure() | ||
|
||
lines = fig[0, 0].add_line_collection(circles, cmap="jet", thickness=5) | ||
|
||
fig[0, 0].add_scatter(np.array([[0, 12, 0]])) | ||
|
||
# check distances | ||
nearest = fpl.utils.get_nearest_graphics((0, 12), lines) | ||
assert nearest[0] is lines[1] # closest | ||
assert nearest[1] is lines[0] | ||
assert nearest[2] is lines[3] | ||
assert nearest[3] is lines[2] # furthest | ||
assert nearest[-1] is lines[2] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there also be a check for if all the graphics are in the same subplot? Or would that not make a difference?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We leave that to the user, so it's just a very simple function unaware of plot areas