Minor performance improvement in bezier.py with preallocating array. (#3083)

* added some njit

* clear points refactored

* removing numba again because no speedup present

* adding constraint back again for size in interpolate in opengl_mobject

* removed numba import

* removed logger import in bezier

* maybe let's undo that for a second
This commit is contained in:
Tristan Schulz 2022-12-27 11:17:36 +01:00 committed by GitHub
commit 3a09c07b7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 2102 additions and 2125 deletions

View file

@ -583,7 +583,7 @@ class OpenGLMobject:
self.set_points(mobject.points)
def clear_points(self):
self.resize_points(0)
self.points = np.empty((0, 3))
def get_num_points(self):
return len(self.points)

View file

@ -101,6 +101,7 @@ def partial_bezier_points(points: np.ndarray, a: float, b: float) -> np.ndarray:
# Shortened version of partial_bezier_points just for quadratics,
# since this is called a fair amount
def partial_quadratic_bezier_points(points, a, b):
points = np.asarray(points, dtype=np.float64)
if a == 1:
return 3 * [points[-1]]
@ -120,7 +121,7 @@ def partial_quadratic_bezier_points(points, a, b):
return [h0, h1, h2]
def split_quadratic_bezier(points: Iterable[float], t: float) -> np.ndarray:
def split_quadratic_bezier(points: np.ndarray, t: float) -> np.ndarray:
"""Split a quadratic Bézier curve at argument ``t`` into two quadratic curves.
Parameters
@ -166,13 +167,14 @@ def subdivide_quadratic_bezier(points: Iterable[float], n: int) -> np.ndarray:
.. image:: /_static/bezier_subdivision_example.png
"""
beziers = []
beziers = np.empty((n, 3, 3))
current = points
for i in range(n, 0, -1):
for j in range(0, n):
i = n - j
tmp = split_quadratic_bezier(current, 1 / i)
beziers.append(tmp[:3])
beziers[j] = tmp[:3]
current = tmp[3:]
return np.asarray(beziers).reshape(-1, 3)
return beziers.reshape(-1, 3)
def quadratic_bezier_remap(
@ -183,7 +185,7 @@ def quadratic_bezier_remap(
Parameters
----------
triplets
The triplets of the quadratic bezier curves to be remapped
The triplets of the quadratic bezier curves to be remapped shape(n, 3, 3)
new_number_of_curves
The number of curves that the output will contain. This needs to be higher than the current number.
@ -232,9 +234,7 @@ def quadratic_bezier_remap(
# Linear interpolation variants
def interpolate(start: int, end: int, alpha: float) -> float:
def interpolate(start: np.ndarray, end: np.ndarray, alpha: float) -> np.ndarray:
return (1 - alpha) * start + alpha * end

5749
poetry.lock generated

File diff suppressed because it is too large Load diff