Skip to content

Commit e00c780

Browse files
authored
Optimized :meth:.Axes.coords_to_point (ManimCommunity#3286)
* Optimized Axes.coords_to_point * Changed variable name to are_coordinates_transposed and added more commentaries * Added some commas to the tuple in the last comment in Axes.coords_to_pointC
1 parent 7c9f98c commit e00c780

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

manim/mobject/graphing/coordinate_systems.py

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,40 +1995,47 @@ def construct(self):
19951995
19961996
self.add(plane, dot_scene, ax, dot_axes, lines)
19971997
"""
1998+
coords = np.asarray(coords)
19981999
origin = self.x_axis.number_to_point(
19992000
self._origin_shift([self.x_axis.x_min, self.x_axis.x_max]),
20002001
)
20012002

2002-
coords = np.asarray(coords)
2003-
2004-
# if called like coords_to_point(1, 2, 3), then coords is a 1x3 array
2005-
transposed = False
2006-
if coords.ndim == 1:
2007-
# original implementation of coords_to_point for performance in the legacy case
2008-
result = np.array(origin)
2009-
for axis, number in zip(self.get_axes(), coords):
2010-
result += axis.number_to_point(number) - origin
2011-
return result
2012-
# if called like coords_to_point([1, 2, 3],[4, 5, 6]), then it shall be used as [1,4], [2,5], [3,6] and return the points as ([x_0,x_1],[y_0,y_1],[z_0,z_1])
2013-
elif coords.ndim == 2:
2014-
coords = coords.T
2015-
transposed = True
2016-
# if called like coords_to_point(np.array([[1, 2, 3],[4,5,6]])), reduce dimension by 1
2017-
elif coords.ndim == 3:
2018-
coords = np.squeeze(coords)
2019-
# else the coords is a Nx1, Nx2, Nx3 array so we do not need to modify the array
2020-
2021-
points = origin + np.sum(
2022-
[
2023-
axis.number_to_point(number) - origin
2024-
for number, axis in zip(coords.T, self.get_axes())
2025-
],
2026-
axis=0,
2027-
)
2028-
# if called with single coord, then return a point instead of a list of points
2029-
if transposed:
2030-
return points.T
2031-
return points
2003+
# Is coords in the format ([[x1 y1 z1] [x2 y2 z2] ...])? (True)
2004+
# Or is coords in the format (x, y, z) or ([x1 x2 ...], [y1 y2 ...], [z1 z2 ...])? (False)
2005+
# The latter is preferred.
2006+
are_coordinates_transposed = False
2007+
2008+
# If coords is in the format ([[x1 y1 z1] [x2 y2 z2] ...]):
2009+
if coords.ndim == 3:
2010+
# Extract from original tuple: now coords looks like [[x y z]] or [[x1 y1 z1] [x2 y2 z2] ...].
2011+
coords = coords[0]
2012+
# If there's a single coord (coords = [[x y z]]), extract it so that
2013+
# coords = [x y z] and coords_to_point returns a single point.
2014+
if coords.shape[0] == 1:
2015+
coords = coords[0]
2016+
# Else, if coords looks more like [[x1 y1 z1] [x2 y2 z2] ...], transform them (by
2017+
# transposing) into the format [[x1 x2 ...] [y1 y2 ...] [z1 z2 ...]] for later processing.
2018+
else:
2019+
coords = coords.T
2020+
are_coordinates_transposed = True
2021+
# Otherwise, coords already looked like (x, y, z) or ([x1 x2 ...], [y1 y2 ...], [z1 z2 ...]),
2022+
# so no further processing is needed.
2023+
2024+
# Now coords should either look like [x y z] or [[x1 x2 ...] [y1 y2 ...] [z1 z2 ...]],
2025+
# so it can be iterated directly. Each element is either a float representing a single
2026+
# coordinate, or a float ndarray of coordinates corresponding to a single axis.
2027+
# Although "points" and "nums" are in plural, there might be a single point or number.
2028+
points = self.x_axis.number_to_point(coords[0])
2029+
other_axes = self.axes.submobjects[1:]
2030+
for axis, nums in zip(other_axes, coords[1:]):
2031+
points += axis.number_to_point(nums) - origin
2032+
2033+
# Return points as is, except if coords originally looked like
2034+
# ([x1 x2 ...], [y1 y2 ...], [z1 z2 ...]), which is determined by the conditions below. In
2035+
# that case, the current implementation requires that the results have to be transposed.
2036+
if are_coordinates_transposed or points.ndim == 1:
2037+
return points
2038+
return points.T
20322039

20332040
def point_to_coords(self, point: Sequence[float]) -> np.ndarray:
20342041
"""Accepts a point from the scene and returns its coordinates with respect to the axes.

0 commit comments

Comments
 (0)