Skip to content

Optimized interpolate and bezier in manim.utils.bezier #3960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 19, 2024

Conversation

chopan050
Copy link
Contributor

@chopan050 chopan050 commented Oct 16, 2024

Overview: What does this pull request change?

Slightly optimized interpolate and bezier in manim.utils.bezier.
Related PR: #3281

interpolate

Rewriting this function to use 1 product instead of 2, makes a speedup of around 30%:

def interpolate(start, end, alpha):
    # return (1 - alpha) * start + alpha * end # original
    return start + alpha * (end - start) # new

bezier

First of all, I added the cases for Bézier curves of grade 0 and 1. This prevents them from falling back to the expensive general nth grade case:

def bezier(points):
    P = np.asarray(points)
    degree = P.shape[0] - 1

    if degree == 0:

        def zero_bezier(t):
            return np.ones_like(t) * P[0]

        return zero_bezier

    if degree == 1:

        def linear_bezier(t):
            return P[0] + t * (P[1] - P[0])

        return linear_bezier

    # ...

Then, I made a very slight optimization for the cubic Bézier case:

    if degree == 3:

        def cubic_bezier(t):
            t2 = t * t
            t3 = t2 * t
            mt = 1 - t
            mt2 = mt * mt
            mt3 = mt2 * mt
            return mt3 * P[0] + 3 * t * mt2 * P[1] + 3 * t2 * mt * P[2] + t3 * P[3]

        return cubic_bezier

It reported a small speedup of around 10% - 15%. Not too bad! I did something similar with the quadratic Bézier:

    if degree == 2:

        def quadratic_bezier(t):
            t2 = t * t
            mt = 1 - t
            mt2 = mt * mt
            return mt2 * P[0] + 2 * t * mt * P[1] + t2 * P[2]

        return quadratic_bezier

Finally, the general nth grade Bézier case. I avoided the extensive use of the choose function which can be expensive, and instead used the recursive definition of Béziers I used for functions like partial_bezier_points and subdivide_bezier.

Also, bezier now returns named functions instead of lambdas, which is good for debugging and profiling: now you can know which function is taking what part of the time when profiling your code.

Links to added or changed documentation pages

bezier
https://manimce--3960.org.readthedocs.build/en/3960/reference/manim.utils.bezier.html#manim.utils.bezier.bezier

interpolate
https://manimce--3960.org.readthedocs.build/en/3960/reference/manim.utils.bezier.html#manim.utils.bezier.interpolate

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

@behackl behackl added enhancement Additions and improvements in general performance labels Oct 19, 2024
@behackl behackl added this to the v0.19.0 milestone Oct 19, 2024
Copy link
Member

@behackl behackl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@behackl behackl enabled auto-merge (squash) October 19, 2024 21:16
@behackl behackl merged commit 75a7525 into ManimCommunity:main Oct 19, 2024
17 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Additions and improvements in general performance
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants