2018-12-24 12:37:51 -08:00
|
|
|
from scipy import linalg
|
2018-03-30 18:19:23 -07:00
|
|
|
import numpy as np
|
2018-03-31 15:11:35 -07:00
|
|
|
|
2019-02-05 11:02:15 -08:00
|
|
|
from manimlib.utils.simple_functions import choose
|
2020-02-04 15:25:08 -08:00
|
|
|
from manimlib.utils.space_ops import find_intersection
|
2018-03-30 18:19:23 -07:00
|
|
|
|
2018-04-17 15:01:25 -07:00
|
|
|
CLOSED_THRESHOLD = 0.001
|
2018-03-30 18:19:23 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def bezier(points):
|
|
|
|
n = len(points) - 1
|
2018-04-06 13:58:59 -07:00
|
|
|
return lambda t: sum([
|
2019-02-05 11:02:15 -08:00
|
|
|
((1 - t)**(n - k)) * (t**k) * choose(n, k) * point
|
2018-03-30 18:19:23 -07:00
|
|
|
for k, point in enumerate(points)
|
|
|
|
])
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def partial_bezier_points(points, a, b):
|
|
|
|
"""
|
2018-04-06 13:58:59 -07:00
|
|
|
Given an array of points which define
|
2018-03-30 18:19:23 -07:00
|
|
|
a bezier curve, and two numbers 0<=a<b<=1,
|
2018-04-06 13:58:59 -07:00
|
|
|
return an array of the same size, which
|
2018-03-30 18:19:23 -07:00
|
|
|
describes the portion of the original bezier
|
|
|
|
curve on the interval [a, b].
|
|
|
|
|
|
|
|
This algorithm is pretty nifty, and pretty dense.
|
|
|
|
"""
|
2019-02-05 15:25:44 -08:00
|
|
|
if a == 1:
|
|
|
|
return [points[-1]] * len(points)
|
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
a_to_1 = np.array([
|
|
|
|
bezier(points[i:])(a)
|
|
|
|
for i in range(len(points))
|
|
|
|
])
|
2019-02-05 15:25:44 -08:00
|
|
|
end_prop = (b - a) / (1. - a)
|
2018-03-30 18:19:23 -07:00
|
|
|
return np.array([
|
2019-02-05 15:25:44 -08:00
|
|
|
bezier(a_to_1[:i + 1])(end_prop)
|
2018-03-30 18:19:23 -07:00
|
|
|
for i in range(len(points))
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
# Linear interpolation variants
|
|
|
|
|
|
|
|
def interpolate(start, end, alpha):
|
2018-04-06 13:58:59 -07:00
|
|
|
return (1 - alpha) * start + alpha * end
|
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
|
2019-02-05 11:02:15 -08:00
|
|
|
def integer_interpolate(start, end, alpha):
|
|
|
|
"""
|
|
|
|
alpha is a float between 0 and 1. This returns
|
|
|
|
an integer between start and end (inclusive) representing
|
|
|
|
appropriate interpolation between them, along with a
|
|
|
|
"residue" representing a new proportion between the
|
|
|
|
returned integer and the next one of the
|
|
|
|
list.
|
|
|
|
|
|
|
|
For example, if start=0, end=10, alpha=0.46, This
|
|
|
|
would return (4, 0.6).
|
|
|
|
"""
|
|
|
|
if alpha >= 1:
|
|
|
|
return (end - 1, 1.0)
|
2019-02-05 15:25:44 -08:00
|
|
|
if alpha <= 0:
|
|
|
|
return (start, 0)
|
2019-02-05 11:02:15 -08:00
|
|
|
value = int(interpolate(start, end, alpha))
|
|
|
|
residue = ((end - start) * alpha) % 1
|
|
|
|
return (value, residue)
|
|
|
|
|
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def mid(start, end):
|
2018-04-06 13:58:59 -07:00
|
|
|
return (start + end) / 2.0
|
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
|
|
|
|
def inverse_interpolate(start, end, value):
|
|
|
|
return np.true_divide(value - start, end - start)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def match_interpolate(new_start, new_end, old_start, old_end, old_value):
|
|
|
|
return interpolate(
|
2018-04-06 13:58:59 -07:00
|
|
|
new_start, new_end,
|
2018-03-30 18:19:23 -07:00
|
|
|
inverse_interpolate(old_start, old_end, old_value)
|
|
|
|
)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
# Figuring out which bezier curves most smoothly connect a sequence of points
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def get_smooth_handle_points(points):
|
|
|
|
points = np.array(points)
|
|
|
|
num_handles = len(points) - 1
|
2018-04-06 13:58:59 -07:00
|
|
|
dim = points.shape[1]
|
2018-03-30 18:19:23 -07:00
|
|
|
if num_handles < 1:
|
|
|
|
return np.zeros((0, dim)), np.zeros((0, dim))
|
2018-04-06 13:58:59 -07:00
|
|
|
# Must solve 2*num_handles equations to get the handles.
|
|
|
|
# l and u are the number of lower an upper diagonal rows
|
|
|
|
# in the matrix to solve.
|
|
|
|
l, u = 2, 1
|
|
|
|
# diag is a representation of the matrix in diagonal form
|
|
|
|
# See https://www.particleincell.com/2012/bezier-splines/
|
|
|
|
# for how to arive at these equations
|
|
|
|
diag = np.zeros((l + u + 1, 2 * num_handles))
|
|
|
|
diag[0, 1::2] = -1
|
|
|
|
diag[0, 2::2] = 1
|
|
|
|
diag[1, 0::2] = 2
|
|
|
|
diag[1, 1::2] = 1
|
|
|
|
diag[2, 1:-2:2] = -2
|
|
|
|
diag[3, 0:-3:2] = 1
|
|
|
|
# last
|
|
|
|
diag[2, -2] = -1
|
|
|
|
diag[1, -1] = 2
|
|
|
|
# This is the b as in Ax = b, where we are solving for x,
|
|
|
|
# and A is represented using diag. However, think of entries
|
|
|
|
# to x and b as being points in space, not numbers
|
|
|
|
b = np.zeros((2 * num_handles, dim))
|
|
|
|
b[1::2] = 2 * points[1:]
|
2018-03-30 18:19:23 -07:00
|
|
|
b[0] = points[0]
|
|
|
|
b[-1] = points[-1]
|
2018-04-06 13:58:59 -07:00
|
|
|
|
|
|
|
def solve_func(b):
|
|
|
|
return linalg.solve_banded((l, u), diag, b)
|
2018-04-17 15:01:25 -07:00
|
|
|
use_closed_solve_function = is_closed(points)
|
|
|
|
if use_closed_solve_function:
|
2018-04-06 13:58:59 -07:00
|
|
|
# Get equations to relate first and last points
|
2018-03-30 18:19:23 -07:00
|
|
|
matrix = diag_to_matrix((l, u), diag)
|
2018-04-06 13:58:59 -07:00
|
|
|
# last row handles second derivative
|
2018-03-30 18:19:23 -07:00
|
|
|
matrix[-1, [0, 1, -2, -1]] = [2, -1, 1, -2]
|
2018-04-06 13:58:59 -07:00
|
|
|
# first row handles first derivative
|
|
|
|
matrix[0, :] = np.zeros(matrix.shape[1])
|
|
|
|
matrix[0, [0, -1]] = [1, 1]
|
|
|
|
b[0] = 2 * points[0]
|
2018-03-30 18:19:23 -07:00
|
|
|
b[-1] = np.zeros(dim)
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-04-17 15:01:25 -07:00
|
|
|
def closed_curve_solve_func(b):
|
2018-04-06 13:58:59 -07:00
|
|
|
return linalg.solve(matrix, b)
|
2018-04-17 15:01:25 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
handle_pairs = np.zeros((2 * num_handles, dim))
|
2018-03-30 18:19:23 -07:00
|
|
|
for i in range(dim):
|
2018-04-17 15:01:25 -07:00
|
|
|
if use_closed_solve_function:
|
|
|
|
handle_pairs[:, i] = closed_curve_solve_func(b[:, i])
|
|
|
|
else:
|
|
|
|
handle_pairs[:, i] = solve_func(b[:, i])
|
2018-03-30 18:19:23 -07:00
|
|
|
return handle_pairs[0::2], handle_pairs[1::2]
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def diag_to_matrix(l_and_u, diag):
|
|
|
|
"""
|
2018-04-06 13:58:59 -07:00
|
|
|
Converts array whose rows represent diagonal
|
2018-03-30 18:19:23 -07:00
|
|
|
entries of a matrix into the matrix itself.
|
|
|
|
See scipy.linalg.solve_banded
|
|
|
|
"""
|
|
|
|
l, u = l_and_u
|
|
|
|
dim = diag.shape[1]
|
|
|
|
matrix = np.zeros((dim, dim))
|
2018-04-06 13:58:59 -07:00
|
|
|
for i in range(l + u + 1):
|
2018-03-30 18:19:23 -07:00
|
|
|
np.fill_diagonal(
|
2018-04-06 13:58:59 -07:00
|
|
|
matrix[max(0, i - u):, max(0, u - i):],
|
|
|
|
diag[i, max(0, u - i):]
|
2018-03-30 18:19:23 -07:00
|
|
|
)
|
|
|
|
return matrix
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def is_closed(points):
|
2019-02-05 11:02:15 -08:00
|
|
|
return np.allclose(points[0], points[-1])
|
2020-02-04 15:25:08 -08:00
|
|
|
|
|
|
|
|
|
|
|
# Given 4 control points for a cubic bezier curve (or arrays of such)
|
|
|
|
# return control points for 2 quadratics (or 2n quadratics) approximating them.
|
|
|
|
def get_quadratic_approximation_of_cubic(a0, h0, h1, a1):
|
|
|
|
a0 = np.array(a0, ndmin=2)
|
|
|
|
h0 = np.array(h0, ndmin=2)
|
|
|
|
h1 = np.array(h1, ndmin=2)
|
|
|
|
a1 = np.array(a1, ndmin=2)
|
|
|
|
mid = (a0 + 3 * h0 + 3 * h1 + a1) / 8
|
|
|
|
# Tangent vectors at the start, middle and end.
|
|
|
|
T0 = h0 - a0
|
|
|
|
Tm = 3 * (-a0 - h0 + h1 + a1) / 4
|
|
|
|
T1 = a1 - h1
|
|
|
|
|
|
|
|
# Intersection between tangent lines at end points and tangent in the middle
|
|
|
|
i0 = find_intersection(a0, T0, mid, Tm)
|
|
|
|
i1 = find_intersection(a1, T1, mid, Tm)
|
|
|
|
|
|
|
|
m, n = np.shape(a0)
|
|
|
|
result = np.zeros((6 * m, n))
|
|
|
|
result[0::6] = a0
|
|
|
|
result[1::6] = i0
|
|
|
|
result[2::6] = mid
|
|
|
|
result[3::6] = mid
|
|
|
|
result[4::6] = i1
|
|
|
|
result[5::6] = a1
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def get_smooth_quadratic_bezier_path_through(points):
|
|
|
|
h0, h1 = get_smooth_handle_points(points)
|
|
|
|
a0 = points[:-1]
|
|
|
|
a1 = points[1:]
|
|
|
|
return get_quadratic_approximation_of_cubic(a0, h0, h1, a1)
|