Skip to content

Commit

Permalink
Write vector_to method
Browse files Browse the repository at this point in the history
  • Loading branch information
willGraham01 committed Jan 30, 2025
1 parent 57a38a9 commit e1b1c36
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions movement/roi/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,69 @@ def distance_to(self, point: ArrayLike, boundary: bool = False) -> float:
"""
from_where = self.region.boundary if boundary else self.region
return shapely.distance(from_where, shapely.Point(point))

@broadcastable_method(
only_broadcastable_along="space", new_dimension_name="vector to"
)
def vector_to(
self,
point: ArrayLike,
boundary: bool = False,
direction: Literal[
"point to region", "region to point"
] = "point to region",
unit: bool = True,
) -> np.ndarray:
"""Compute the vector from a point to the region.
Specifically, the vector is directed from the given ``point`` to the
nearest point within the region. Points within the region return the
zero vector.
Parameters
----------
point : ArrayLike
Coordinates of a point to compute the vector to (or from) the
region.
boundary : bool
If True, finds the vector to the nearest point on the boundary of
the region, instead of the nearest point within the region.
(See Notes). Default is False.
direction : Literal["point to region", "region to point"]
Which direction the returned vector should point in. Default is
"point to region".
unit : bool
If True, the unit vector in the appropriate direction is returned,
otherwise the displacement vector is returned. Default is False.
Returns
-------
np.ndarray
Vector directed between the point and the region.
Notes
-----
If given a ``point`` in the interior of the region, the vector from
this ``point`` to the region is treated as the zero vector. The
``boundary`` argument can be used to force the method to find the
distance from the ``point`` to the nearest point on the boundary of the
region, if so desired. Note that a ``point`` on the boundary still
returns the zero vector.
"""
from_where = self.region.boundary if boundary else self.region

Check warning on line 340 in movement/roi/base.py

View check run for this annotation

Codecov / codecov/patch

movement/roi/base.py#L340

Added line #L340 was not covered by tests

# "point to region" by virtue of order of arguments to shapely call
directed_line = shapely.shortest_line(shapely.Point(point), from_where)

Check warning on line 343 in movement/roi/base.py

View check run for this annotation

Codecov / codecov/patch

movement/roi/base.py#L343

Added line #L343 was not covered by tests

displacement_vector = np.array(directed_line.coords[1]) - np.array(

Check warning on line 345 in movement/roi/base.py

View check run for this annotation

Codecov / codecov/patch

movement/roi/base.py#L345

Added line #L345 was not covered by tests
directed_line.coords[0]
)
if direction == "region to point":
displacement_vector *= -1.0
if unit:
norm = np.sqrt(np.sum(displacement_vector**2))

Check warning on line 351 in movement/roi/base.py

View check run for this annotation

Codecov / codecov/patch

movement/roi/base.py#L348-L351

Added lines #L348 - L351 were not covered by tests
# Cannot normalise the 0 vector
if norm != 0.0:
displacement_vector /= norm
return displacement_vector

Check warning on line 355 in movement/roi/base.py

View check run for this annotation

Codecov / codecov/patch

movement/roi/base.py#L353-L355

Added lines #L353 - L355 were not covered by tests

0 comments on commit e1b1c36

Please sign in to comment.