Skip to content

Commit bcab4b2

Browse files
committed
use remapping instead of copying points
1 parent f5dc22a commit bcab4b2

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

manim/mobject/opengl/opengl_mobject.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2348,7 +2348,7 @@ def construct(self):
23482348
continue
23492349

23502350
if key in ("points", "bounding_box"):
2351-
self.data[key] = path_func(
2351+
self.data[key][:] = path_func(
23522352
mobject1.data[key], mobject2.data[key], alpha
23532353
)
23542354
else:

manim/mobject/opengl/opengl_vectorized_mobject.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
interpolate,
2222
partial_quadratic_bezier_points,
2323
proportions_along_bezier_curve_for_point,
24+
quadratic_bezier_remap,
25+
subdivide_quadratic_bezier,
2426
)
2527
from manim.utils.color import *
2628
from manim.utils.config_ops import _Data
@@ -1281,10 +1283,7 @@ def interpolate(self, mobject1, mobject2, alpha, *args, **kwargs):
12811283
return self
12821284

12831285
def pointwise_become_partial(
1284-
self,
1285-
vmobject: OpenGLVMobject,
1286-
a: float,
1287-
b: float,
1286+
self, vmobject: OpenGLVMobject, a: float, b: float, remap=True
12881287
) -> OpenGLVMobject:
12891288
"""Given two bounds a and b, transforms the points of the self vmobject into the points of the vmobject
12901289
passed as parameter with respect to the bounds. Points here stand for control points of the bezier curves (anchors and handles)
@@ -1297,6 +1296,9 @@ def pointwise_become_partial(
12971296
upper-bound.
12981297
b : float
12991298
lower-bound
1299+
remap : bool
1300+
if the point amount should be kept the same (True)
1301+
This option should be manually set to False if keeping the number of points is not needed
13001302
"""
13011303
assert isinstance(vmobject, OpenGLVMobject)
13021304
# Partial curve includes three portions:
@@ -1314,7 +1316,6 @@ def pointwise_become_partial(
13141316
# Ex: if lower_index is 3, and lower_residue is 0.4, then the algorithm will append to the points 0.4 of the third bezier curve
13151317
lower_index, lower_residue = integer_interpolate(0, num_quadratics, a)
13161318
upper_index, upper_residue = integer_interpolate(0, num_quadratics, b)
1317-
13181319
self.clear_points()
13191320
if num_quadratics == 0:
13201321
return self
@@ -1333,8 +1334,14 @@ def pointwise_become_partial(
13331334
),
13341335
)
13351336
# TODO: replace by smooth_bezier_remap(triplets[li+1:ui], num_quadratics-2) -> [points; len(ui-li+1)] : #2742
1336-
for triplet in bezier_triplets[lower_index + 1 : upper_index]:
1337-
self.append_points(triplet)
1337+
inner_points = bezier_triplets[lower_index + 1 : upper_index]
1338+
1339+
if remap:
1340+
new_triplets = quadratic_bezier_remap(inner_points, num_quadratics - 2)
1341+
else:
1342+
new_triplets = bezier_triplets
1343+
1344+
self.append_points(np.asarray(new_triplets).reshape(-1, 3))
13381345
self.append_points(
13391346
partial_quadratic_bezier_points(
13401347
bezier_triplets[upper_index], 0, upper_residue

manim/utils/bezier.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,20 @@ def subdivide_quadratic_bezier(points: Iterable[float], n: int) -> np.ndarray:
175175
return np.asarray(beziers).reshape(-1, 3)
176176

177177

178+
def quadratic_bezier_remap(triplets, new_number_of_curves):
179+
new_triplets = []
180+
difference = new_number_of_curves - len(triplets)
181+
for triplet in triplets:
182+
if difference > 0:
183+
tmp = subdivide_quadratic_bezier(triplet, 2)
184+
new_triplets.append(tmp[:3])
185+
new_triplets.append(tmp[3:])
186+
difference -= 1
187+
else:
188+
new_triplets.append(triplet)
189+
return new_triplets
190+
191+
178192
# Linear interpolation variants
179193

180194

0 commit comments

Comments
 (0)