Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fix :meth:`~.VMobject.point_from_proportion` to account for the length of curves. (#1274)

friedkeenan4402ef9ee81b
diff --git a/manim/mobject/types/opengl_vectorized_mobject.py b/manim/mobject/types/opengl_vectorized_mobject.py
index e73c896ca..3abaecffb 100644
--- a/manim/mobject/types/opengl_vectorized_mobject.py
+++ b/manim/mobject/types/opengl_vectorized_mobject.py
@@ -549,14 +549,88 @@ class OpenGLVMobject(OpenGLMobject):
def get_nth_curve_function(self, n):
return bezier(self.get_nth_curve_points(n))
+ def get_nth_curve_function_with_length(
+ self, n: int, n_sample_points: int = 10
+ ) -> typing.Tuple[typing.Callable[[float], np.ndarray], float]:
+ """Returns the expression of the nth curve along with its (approximate) length.
+
+ Parameters
+ ----------
+ n
+ The index of the desired curve.
+ n_sample_points
+ The number of points to sample to find the length.
+
+ Returns
+ -------
+ curve : typing.Callable[[float], np.ndarray]
+ The function for the nth curve.
+ length : :class:`float`
+ The length of the nth curve.
+ """
+
+ curve = self.get_nth_curve_function(n)
+
+ points = np.array([curve(a) for a in np.linspace(0, 1, n_sample_points)])
+ diffs = points[1:] - points[:-1]
+ norms = np.apply_along_axis(get_norm, 1, diffs)
+
+ length = np.sum(norms)
+
+ return curve, length
+
def get_num_curves(self):
return self.get_num_points() // self.n_points_per_curve
- def point_from_proportion(self, alpha):
+ def get_curve_functions(
+ self,
+ ) -> typing.Iterable[typing.Callable[[float], np.ndarray]]:
+ """Gets the functions for the curves of the mobject.
+
+ Returns
+ -------
+ typing.Iterable[typing.Callable[[float], np.ndarray]]
+ The functions for the curves.
+ """
+
num_curves = self.get_num_curves()
- n, residue = integer_interpolate(0, num_curves, alpha)
- curve_func = self.get_nth_curve_function(n)
- return curve_func(residue)
+
+ for n in range(num_curves):
+ yield self.get_nth_curve_function(n)
+
+ def get_curve_functions_with_lengths(
+ self,
+ ) -> typing.Iterable[typing.Tuple[typing.Callable[[float], np.ndarray], float]]:
+ """Gets the functions and lengths of the curves for the mobject.
+
+ Returns
+ -------
+ typing.Iterable[typing.Tuple[typing.Callable[[float], np.ndarray], float]]
+ The functions and lengths of the curves.
+ """
+
+ num_curves = self.get_num_curves()
+
+ for n in range(num_curves):
+ yield self.get_nth_curve_function_with_length(n)
+
+ def point_from_proportion(self, alpha):
+ curves_with_lengths = list(self.get_curve_functions_with_lengths())
+
+ total_length = np.sum(length for _, length in curves_with_lengths)
+ target_length = alpha * total_length
+ current_length = 0
+
+ for curve, length in curves_with_lengths:
+ if current_length + length >= target_length:
+ if length != 0:
+ residue = (target_length - current_length) / length
+ else:
+ residue = 0
+
+ return curve(residue)
+
+ current_length += length
def get_anchors_and_handles(self):
"""
diff --git a/manim/mobject/types/vectorized_mobject.py b/manim/mobject/types/vectorized_mobject.py
index 48f00e3ff..6a5e9f79e 100644
--- a/manim/mobject/types/vectorized_mobject.py
+++ b/manim/mobject/types/vectorized_mobject.py
@@ -895,6 +895,36 @@ class VMobject(Mobject):
"""
return bezier(self.get_nth_curve_points(n))
+ def get_nth_curve_function_with_length(
+ self, n: int, n_sample_points: int = 10
+ ) -> typing.Tuple[typing.Callable[[float], np.ndarray], float]:
+ """Returns the expression of the nth curve along with its (approximate) length.
+
+ Parameters
+ ----------
+ n
+ The index of the desired curve.
+ n_sample_points
+ The number of points to sample to find the length.
+
+ Returns
+ -------
+ curve : typing.Callable[[float], np.ndarray]
+ The function for the nth curve.
+ length : :class:`float`
+ The length of the nth curve.
+ """
+
+ curve = self.get_nth_curve_function(n)
+
+ points = np.array([curve(a) for a in np.linspace(0, 1, n_sample_points)])
+ diffs = points[1:] - points[:-1]
+ norms = np.apply_along_axis(get_norm, 1, diffs)
+
+ length = np.sum(norms)
+
+ return curve, length
+
def get_num_curves(self) -> int:
"""Returns the number of curves of the vmobject.
@@ -906,6 +936,38 @@ class VMobject(Mobject):
nppcc = self.n_points_per_cubic_curve
return len(self.points) // nppcc
+ def get_curve_functions(
+ self,
+ ) -> typing.Iterable[typing.Callable[[float], np.ndarray]]:
+ """Gets the functions for the curves of the mobject.
+
+ Returns
+ -------
+ typing.Iterable[typing.Callable[[float], np.ndarray]]
+ The functions for the curves.
+ """
+
+ num_curves = self.get_num_curves()
+
+ for n in range(num_curves):
+ yield self.get_nth_curve_function(n)
+
+ def get_curve_functions_with_lengths(
+ self,
+ ) -> typing.Iterable[typing.Tuple[typing.Callable[[float], np.ndarray], float]]:
+ """Gets the functions and lengths of the curves for the mobject.
+
+ Returns
+ -------
+ typing.Iterable[typing.Tuple[typing.Callable[[float], np.ndarray], float]]
+ The functions and lengths of the curves.
+ """
+
+ num_curves = self.get_num_curves()
+
+ for n in range(num_curves):
+ yield self.get_nth_curve_function_with_length(n)
+
def point_from_proportion(self, alpha: float) -> np.ndarray:
"""Get the bezier curve evaluated at a position P,
where P is the point corresponding to the proportion defined by the given alpha.
@@ -920,10 +982,23 @@ class VMobject(Mobject):
np.ndarray
Point evaluated.
"""
- num_cubics = self.get_num_curves()
- n, residue = integer_interpolate(0, num_cubics, alpha)
- curve = self.get_nth_curve_function(n)
- return curve(residue)
+
+ curves_with_lengths = list(self.get_curve_functions_with_lengths())
+
+ total_length = np.sum(length for _, length in curves_with_lengths)
+ target_length = alpha * total_length
+ current_length = 0
+
+ for curve, length in curves_with_lengths:
+ if current_length + length >= target_length:
+ if length != 0:
+ residue = (target_length - current_length) / length
+ else:
+ residue = 0
+
+ return curve(residue)
+
+ current_length += length
def get_anchors_and_handles(self) -> typing.Iterable[np.ndarray]:
"""Returns anchors1, handles1, handles2, anchors2,
diff --git a/tests/test_vectorized_mobject.py b/tests/test_vectorized_mobject.py
index b9507682f..57d8de38a 100644
--- a/tests/test_vectorized_mobject.py
+++ b/tests/test_vectorized_mobject.py
@@ -1,8 +1,26 @@
+import numpy as np
import pytest
from manim import Line, Mobject, VDict, VGroup, VMobject
+def test_vmobject_point_from_propotion():
+ obj = VMobject()
+
+ # One long line, one short line
+ obj.set_points_as_corners(
+ [
+ np.array([0, 0, 0]),
+ np.array([4, 0, 0]),
+ np.array([4, 2, 0]),
+ ]
+ )
+
+ # Total length of 6, so halfway along the object
+ # would be at length 3, which lands in the first, long line.
+ assert np.all(obj.point_from_proportion(0.5) == np.array([3, 0, 0]))
+
+
def test_vgroup_init():
"""Test the VGroup instantiation."""
VGroup()