Factor out approximate smoothing to VMobject.make_approximately_smooth function

This commit is contained in:
Grant Sanderson 2021-02-05 19:48:43 -08:00
parent 66817c4e2b
commit 71f328c7ef
3 changed files with 27 additions and 14 deletions

View file

@ -30,7 +30,7 @@ class Homotopy(Animation):
class SmoothedVectorizedHomotopy(Homotopy): class SmoothedVectorizedHomotopy(Homotopy):
def interpolate_submobject(self, submob, start, alpha): def interpolate_submobject(self, submob, start, alpha):
Homotopy.interpolate_submobject(self, submob, start, alpha) Homotopy.interpolate_submobject(self, submob, start, alpha)
submob.make_smooth() submob.make_approximately_smooth()
class ComplexHomotopy(Homotopy): class ComplexHomotopy(Homotopy):

View file

@ -40,7 +40,7 @@ class ParametricCurve(VMobject):
points = np.array([self.t_func(t) for t in t_range]) points = np.array([self.t_func(t) for t in t_range])
self.start_new_path(points[0]) self.start_new_path(points[0])
self.add_points_as_corners(points[1:]) self.add_points_as_corners(points[1:])
self.make_smooth(true_smooth=False) self.make_approximately_smooth()
return self return self

View file

@ -405,11 +405,11 @@ class VMobject(Mobject):
def set_points_smoothly(self, points, true_smooth=False): def set_points_smoothly(self, points, true_smooth=False):
self.set_points_as_corners(points) self.set_points_as_corners(points)
self.make_smooth(true_smooth) self.make_smooth()
return self return self
def change_anchor_mode(self, mode): def change_anchor_mode(self, mode):
assert(mode in ["jagged", "approx_smooth", "true_smooth"]) assert(mode in ("jagged", "approx_smooth", "true_smooth"))
nppc = self.n_points_per_curve nppc = self.n_points_per_curve
for submob in self.family_members_with_points(): for submob in self.family_members_with_points():
subpaths = submob.get_subpaths() subpaths = submob.get_subpaths()
@ -428,23 +428,35 @@ class VMobject(Mobject):
submob.refresh_triangulation() submob.refresh_triangulation()
return self return self
def make_smooth(self, true_smooth=True): def make_smooth(self):
""" """
If true_smooth is set to True, the number of points This will double the number of points in the mobject,
in the mobject will double, but the effect will be so should not be called repeatedly. It also means
a genuinely smooth (C2) curve. Otherwise, it may not transforming between states before and after calling
becomes perfectly smooth, but the number of points this might have strange artifacts
will stay the same.
""" """
mode = "true_smooth" if true_smooth else "approx_smooth" self.change_anchor_mode("true_smooth")
return self.change_anchor_mode(mode) return self
def make_approximately_smooth(self):
"""
Unlike make_smooth, this will not change the number of
points, but it also does not result in a perfectly smooth
curve. It's most useful when the points have been
sampled at a not-too-low rate from a continuous function,
as in the case of ParametricCurve
"""
self.change_anchor_mode("approx_smooth")
return self
def make_jagged(self): def make_jagged(self):
return self.change_anchor_mode("jagged") self.change_anchor_mode("jagged")
return self
def add_subpath(self, points): def add_subpath(self, points):
assert(len(points) % self.n_points_per_curve == 0) assert(len(points) % self.n_points_per_curve == 0)
self.append_points(points) self.append_points(points)
return self
def append_vectorized_mobject(self, vectorized_mobject): def append_vectorized_mobject(self, vectorized_mobject):
new_points = list(vectorized_mobject.get_points()) new_points = list(vectorized_mobject.get_points())
@ -454,6 +466,7 @@ class VMobject(Mobject):
# a new path # a new path
self.resize_data(len(self.get_points() - 1)) self.resize_data(len(self.get_points() - 1))
self.append_points(new_points) self.append_points(new_points)
return self
# #
def consider_points_equals(self, p0, p1): def consider_points_equals(self, p0, p1):
@ -835,7 +848,7 @@ class VMobject(Mobject):
def apply_function(self, function): def apply_function(self, function):
super().apply_function(function) super().apply_function(function)
if self.make_smooth_after_applying_functions: if self.make_smooth_after_applying_functions:
self.make_smooth(true_smooth=False) self.make_approximately_smooth()
return self return self
@triggers_refreshed_triangulation @triggers_refreshed_triangulation