Skip to content
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

Efficient Laplacian calculations #2362

Merged
merged 3 commits into from
Feb 26, 2025

Conversation

nickeopti
Copy link
Contributor

The current implementation of trimesh.smoothing.laplacian_calculation relies on list comprehensions. The case of equal_weight=True, at least, can be computed directly using sparse matrix operations. This PR does that, which turns out to be nearly 20 times faster.

I have been unable to figure out a way of computing the equal_weight=False case using sparse matrix operations, so I have let the current implementation of that be. This implies quite a performance difference between the two flag values. This is noted in the docstring, which is now also more descriptive.

Correctness tests are added, which include a verbatim copy of the original implementation to test against.

Performance characteristics:

In [1]: import trimesh

In [2]: from tests import test_smoothing

In [3]: m = trimesh.creation.icosphere(8, 10)

In [4]: m.vertices.shape, m.faces.shape
Out[4]: ((655362, 3), (1310720, 3))

In [5]: %timeit trimesh.smoothing.laplacian_calculation(m, equal_weight=True)
44.4 ms ± 783 μs per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [6]: %timeit test_smoothing.explicit_laplacian_calculation(m, equal_weight=True)
790 ms ± 9.89 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [7]: %timeit trimesh.smoothing.laplacian_calculation(m, equal_weight=False)
2.87 s ± 31.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [8]: %timeit test_smoothing.explicit_laplacian_calculation(m, equal_weight=False)
2.91 s ± 34.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

where test_smoothing.explicit_laplacian_calculation is the original implementation.

@mikedh
Copy link
Owner

mikedh commented Feb 26, 2025

Looks like a huge improvement, thanks for the PR and great test! It seems like since equal_weight=False is not called by the internal uses of the function it's pretty reasonable for the flag to have a large performance penalty, since people should know what they're asking for.

Don't worry about the CI failing, it's just some minor housekeeping.

@mikedh mikedh changed the base branch from main to release/svg February 26, 2025 21:15
@mikedh mikedh merged commit b525d4a into mikedh:release/svg Feb 26, 2025
2 of 9 checks passed
@mikedh mikedh mentioned this pull request Feb 26, 2025
mikedh added a commit that referenced this pull request Feb 27, 2025
- support most of the [basic
shapes](https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Basic_Shapes)
in SVG imports rather than just path strings. Unsupported are ellipses
and rounded rectangles. Fixes #2361
- release #2362
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants