Have make_smooth work directly from the quadratic bezier curves, without going via cubics and doubling the size of the points list

This commit is contained in:
Grant Sanderson 2020-06-09 16:58:16 -07:00
parent 1f129f4a23
commit 3e913b6649
2 changed files with 25 additions and 33 deletions

View file

@ -477,16 +477,11 @@ class VMobject(Mobject):
submob.clear_points() submob.clear_points()
for subpath in subpaths: for subpath in subpaths:
anchors = np.vstack([subpath[::nppc], subpath[-1:]]) anchors = np.vstack([subpath[::nppc], subpath[-1:]])
new_subpath = np.array(subpath)
if mode == "smooth": if mode == "smooth":
h1, h2 = get_smooth_cubic_bezier_handle_points(anchors) new_subpath[1::nppc] = get_smooth_quadratic_bezier_handle_points(anchors)
new_subpath = get_quadratic_approximation_of_cubic(
anchors[:-1], h1, h2, anchors[1:]
)
elif mode == "jagged": elif mode == "jagged":
new_subpath = np.array(subpath) new_subpath[1::nppc] = 0.5 * (anchors[:-1] + anchors[1:])
new_subpath[1::nppc] = interpolate(
anchors[:-1], anchors[1:], 0.5
)
submob.append_points(new_subpath) submob.append_points(new_subpath)
submob.refresh_triangulation() submob.refresh_triangulation()
return self return self

View file

@ -87,32 +87,28 @@ def match_interpolate(new_start, new_end, old_start, old_end, old_value):
# Figuring out which bezier curves most smoothly connect a sequence of points # Figuring out which bezier curves most smoothly connect a sequence of points
def get_smooth_quadratic_bezier_handle_points(points): def get_smooth_quadratic_bezier_handle_points(points):
# Alas, this function does not actually work very well. n = len(points)
# # Top matrix sets the constraint h_i + h_{i + 1} = 2 * P_i
# For each point P_i, where 1 <= i <= n, draw a line through top_mat = np.zeros((n - 2, n - 1))
# P_i parallel to the line through (P_{i-1}, P_{i+1}). The np.fill_diagonal(top_mat, 1)
# intersection of these lines form most of the handles. np.fill_diagonal(top_mat[:, 1:], 1)
#
# What remains are those near the end points. For that, we want # Lower matrix sets the constraint that 2(h1 - h0)= p2 - p0 and 2(h_{n-1}- h_{n-2}) = p_n - p_{n-2}
# the handle between P_0 and P_1 to be closest to (P_0 + P_1) / 2, low_mat = np.zeros((2, n - 1))
# which will minimize the second derivative of that curve. Likewise low_mat[0, :2] = [-2, 2]
# for the last handle point. low_mat[1, -2:] = [-2, 2]
t01 = points[1] - points[0]
t12 = points[2] - points[1] # Use the pseudoinverse to find a near solution to these constraints
tm2 = points[-2] - points[-3] full_mat = np.vstack([top_mat, low_mat])
tm1 = points[-1] - points[-2] full_mat_pinv = np.linalg.pinv(full_mat)
tangents = np.vstack([
rotate_vector(t01, PI / 2, cross(t01, t12)), rhs = np.vstack([
points[2:] - points[:-2], 2 * points[1:-1],
rotate_vector(tm1, PI / 2, cross(tm1, tm2)) [points[2] - points[0]],
[points[-1] - points[-3]],
]) ])
alt_points = np.array(points)
alt_points[0] = points[:2].mean(0) return np.dot(full_mat_pinv, rhs)
alt_points[-1] = points[-2:].mean(0)
return find_intersection(
alt_points[:-1], tangents[:-1],
alt_points[1:], tangents[1:],
)
def get_smooth_cubic_bezier_handle_points(points): def get_smooth_cubic_bezier_handle_points(points):
@ -148,6 +144,7 @@ def get_smooth_cubic_bezier_handle_points(points):
def solve_func(b): def solve_func(b):
return linalg.solve_banded((l, u), diag, b) return linalg.solve_banded((l, u), diag, b)
use_closed_solve_function = is_closed(points) use_closed_solve_function = is_closed(points)
if use_closed_solve_function: if use_closed_solve_function:
# Get equations to relate first and last points # Get equations to relate first and last points