Wrapper around the Fast-Quadric-Mesh-Simplification library.
Project description
This is a python wrapping of the Fast-Quadric-Mesh-Simplification Library. Having arrived at the same problem as the original author, but needing a Python library, this project seeks to extend the work of the original library while adding integration to Python and the PyVista project.
For the full documentation visit: https://pyvista.github.io/fast-simplification/
Installation
Fast Simplification can be installed from PyPI using pip on Python >= 3.7:
pip install fast-simplification
See the Contributing for more details regarding development or if the installation through pip doesn’t work out.
Basic Usage
The basic interface is quite straightforward and can work directly with arrays of points and triangles:
points = [[ 0.5, -0.5, 0.0],
[ 0.0, -0.5, 0.0],
[-0.5, -0.5, 0.0],
[ 0.5, 0.0, 0.0],
[ 0.0, 0.0, 0.0],
[-0.5, 0.0, 0.0],
[ 0.5, 0.5, 0.0],
[ 0.0, 0.5, 0.0],
[-0.5, 0.5, 0.0]]
faces = [[0, 1, 3],
[4, 3, 1],
[1, 2, 4],
[5, 4, 2],
[3, 4, 6],
[7, 6, 4],
[4, 5, 7],
[8, 7, 5]]
points_out, faces_out = fast_simplification.simplify(points, faces, 0.5)
Advanced Usage
This library supports direct integration with VTK through PyVista to provide a simplistic interface to the library. As this library provides a 4-5x improvement to the VTK decimation algorithms.
>>> from pyvista import examples
>>> mesh = examples.download_nefertiti()
>>> out = fast_simplification.simplify_mesh(mesh, target_reduction=0.9)
Compare with built-in VTK/PyVista methods:
>>> fas_sim = fast_simplification.simplify_mesh(mesh, target_reduction=0.9)
>>> dec_std = mesh.decimate(0.9) # vtkQuadricDecimation
>>> dec_pro = mesh.decimate_pro(0.9) # vtkDecimatePro
>>> pv.set_plot_theme('document')
>>> pl = pv.Plotter(shape=(2, 2), window_size=(1000, 1000))
>>> pl.add_text('Original', 'upper_right', color='w')
>>> pl.add_mesh(mesh, show_edges=True)
>>> pl.camera_position = cpos
>>> pl.subplot(0, 1)
>>> pl.add_text(
... 'Fast-Quadric-Mesh-Simplification\n~2.2 seconds', 'upper_right', color='w'
... )
>>> pl.add_mesh(fas_sim, show_edges=True)
>>> pl.camera_position = cpos
>>> pl.subplot(1, 0)
>>> pl.add_mesh(dec_std, show_edges=True)
>>> pl.add_text(
... 'vtkQuadricDecimation\n~9.5 seconds', 'upper_right', color='w'
... )
>>> pl.camera_position = cpos
>>> pl.subplot(1, 1)
>>> pl.add_mesh(dec_pro, show_edges=True)
>>> pl.add_text(
... 'vtkDecimatePro\n11.4~ seconds', 'upper_right', color='w'
... )
>>> pl.camera_position = cpos
>>> pl.show()
Comparison to other libraries
The pyfqmr library wraps the same header file as this library and has similar capabilities. In this library, the decision was made to write the Cython layer on top of an additional C++ layer rather than directly interfacing with wrapper from Cython. This results in a mild performance improvement.
Reusing the example above:
Set up a timing function.
>>> import pyfqmr
>>> vertices = mesh.points
>>> faces = mesh.faces.reshape(-1, 4)[:, 1:]
>>> def time_pyfqmr():
... mesh_simplifier = pyfqmr.Simplify()
... mesh_simplifier.setMesh(vertices, faces)
... mesh_simplifier.simplify_mesh(
... target_count=out.n_faces, aggressiveness=7, verbose=0
... )
... vertices_out, faces_out, normals_out = mesh_simplifier.getMesh()
... return vertices_out, faces_out, normals_out
Now, time it and compare with the non-VTK API of this library:
>>> timeit time_pyfqmr()
2.75 s ± 5.35 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
>>> timeit vout, fout = fast_simplification.simplify(vertices, faces, 0.9)
2.05 s ± 3.18 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Additionally, the fast-simplification library has direct plugins to the pyvista library, making it easy to read and write meshes:
>>> import pyvista
>>> import fast_simplification
>>> mesh = pyvista.read('my_mesh.stl')
>>> simple = fast_simplification.simplify_mesh(mesh)
>>> simple.save('my_simple_mesh.stl')
Since both libraries are based on the same core C++ code, feel free to use whichever gives you the best performance and interoperability.
Replay decimation functionality
This library also provides an interface to keep track of the successive collapses that occur during the decimation process and to replay the decimation process. This can be useful for different applications, such as:
applying the same decimation to a collection of meshes that share the same topology
computing a correspondence map between the vertices of the original mesh and the vertices of the decimated mesh, to transfer field data from one to the other for example
replaying the decimation process with a smaller target reduction than the original one, faster than decimating the original mesh with the smaller target reduction
To use this functionality, you need to set the return_collapses parameter to True when calling simplify. This will return the successive collapses of the decimation process in addition to points and faces.
>>> import fast_simplification
>>> import pyvista
>>> mesh = pyvista.Sphere()
>>> points, faces = mesh.points, mesh.faces.reshape(-1, 4)[:, 1:]
>>> points_out, faces_out, collapses = fast_simplification.simplify(points, faces, 0.9, return_collapses=True)
Now you can call replay_simplification to replay the decimation process and obtain the mapping between the vertices of the original mesh and the vertices of the decimated mesh.
>>> points_out, faces_out, indice_mapping = fast_simplification.replay_simplification(points, faces, collapses)
>>> i = 3
>>> print(f'Vertex {i} of the original mesh is mapped to {indice_mapping[i]} of the decimated mesh')
You can also use the replay_simplification function to replay the decimation process with a smaller target reduction than the original one. This is faster than decimating the original mesh with the smaller target reduction. To do so, you need to pass a subset of the collapses to the replay_simplification function. For example, to replay the decimation process with a target reduction of 50% the initial rate, you can run:
>>> import numpy as np
>>> collapses_half = collapses[:int(0.5 * len(collapses))]
>>> points_out, faces_out, indice_mapping = fast_simplification.replay_simplification(points, faces, collapses_half)
If you have a collection of meshes that share the same topology, you can apply the same decimation to all of them by calling replay_simplification with the same collapses for each mesh. This ensure that the decimated meshes will share the same topology.
>>> import numpy as np
>>> # Assume that you have a collection of meshes stored in a list meshes
>>> _, _, collapses = fast_simplification.simplify(meshes[0].points, meshes[0].faces,
... 0.9, return_collapses=True)
>>> decimated_meshes = []
>>> for mesh in meshes:
... points_out, faces_out, _ = fast_simplification.replay_simplification(mesh.points, mesh.faces, collapses)
... decimated_meshes.append(pyvista.PolyData(points_out, faces_out))
Contributing
Contribute to this repository by forking this repository and installing in development mode with:
git clone https://github.com/<USERNAME>/fast-simplification pip install -e . pip install -r requirements_test.txt
You can then add your feature or commit your bug fix and then run your unit testing with:
pytest
Unit testing will automatically enforce minimum code coverage standards.
Next, to ensure your code meets minimum code styling standards, run:
pip install pre-commit pre-commit run --all-files
Finally, create a pull request from your fork and I’ll be sure to review it.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file fast_simplification-0.1.9.tar.gz
.
File metadata
- Download URL: fast_simplification-0.1.9.tar.gz
- Upload date:
- Size: 26.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1303e082d6c8a8c0c4a4b96f8d6625f9da92d9001ea128af54eacd6c96871c8 |
|
MD5 | 69f5017d1d3d4b1e59e48f5cb885fd01 |
|
BLAKE2b-256 | 3c54f1f375ab48e04f713a880eb8d5fd0ff31184c8e09324ec492365dc873573 |
File details
Details for the file fast_simplification-0.1.9-cp312-cp312-win_amd64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 211.8 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8820f5a5fd10f90c75aae2157fe5ff3ae534bcb0ddfd94d8cd6de7b23c59bf4e |
|
MD5 | 899f7ec6082a0b43a314756a18be5be3 |
|
BLAKE2b-256 | d2ec002433ccae77daf86dc6525516e7b38fa2f321d5232434797b9090340ee0 |
File details
Details for the file fast_simplification-0.1.9-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa2943d38a3f03e00daff1bc418353bd02493eda2c387831df2991c65b7e6ab5 |
|
MD5 | 2a78fbccae46b65dfc3a44e960106d58 |
|
BLAKE2b-256 | 9460f5bce2df657c7591651593e29d33e4b02c67fcfdb03e0be0a0ce9d461692 |
File details
Details for the file fast_simplification-0.1.9-cp312-cp312-macosx_11_0_arm64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 241.1 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d8c848bdf35d0e6f87364fbfc39273ab3bdfd23d5f73f85fabab4c69419f519 |
|
MD5 | acc2db37460a4221cda1dcd0a9fdce7e |
|
BLAKE2b-256 | 7e39d9941b50413eb521b104bda1aa147466f557b54ccbd74e4a6e4ea9ad680a |
File details
Details for the file fast_simplification-0.1.9-cp312-cp312-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp312-cp312-macosx_10_9_x86_64.whl
- Upload date:
- Size: 263.4 kB
- Tags: CPython 3.12, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2125c2fbd0438e42f6ab1c0cda1b9203783012115e1cd54d2efbcb0c2f5eead1 |
|
MD5 | 937647b92be2043348fe4b8332614102 |
|
BLAKE2b-256 | 82f7f969a65f423c223a421e397b7e41f904c551d90e023c06917d318509e555 |
File details
Details for the file fast_simplification-0.1.9-cp311-cp311-win_amd64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 212.4 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bfaf68475955ffb4c09b177a5ab4b3479291e0f89c1da4b020ce2d284d352d3a |
|
MD5 | e130e7f2f8b44fba777ba73e6f230ece |
|
BLAKE2b-256 | e108988bd09de4b990628921abeb1549743eb43df7fa8f6441072a11b1a1c7c2 |
File details
Details for the file fast_simplification-0.1.9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 153a9fc728191102609d9e9589f7f8eeb3c2f331e711a8406bdd1043ceee4ea3 |
|
MD5 | b6f169d3bf29511b15a239ca21aa880b |
|
BLAKE2b-256 | fbb850802109b759489653ec1daf9a7618d437c43835533078a1232541161ba3 |
File details
Details for the file fast_simplification-0.1.9-cp311-cp311-macosx_11_0_arm64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 241.5 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b8cf827407d92e5f9bb57123b9d19b52b916dd40a52da316d09f65b68d393a1 |
|
MD5 | bb5cab7ae2902b7c2860183339911ce6 |
|
BLAKE2b-256 | 1c574f5fc7c68c4be251c0bf4b35073fd5609e36fd2ab9b6f74d20662e65776e |
File details
Details for the file fast_simplification-0.1.9-cp311-cp311-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp311-cp311-macosx_10_9_x86_64.whl
- Upload date:
- Size: 263.3 kB
- Tags: CPython 3.11, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ab05418db5b1912ad1da48d21c369ef73d08e62c061beb4a2acb6e21b6ba8e6 |
|
MD5 | 00a1a468292eb8e021720610beaee6f1 |
|
BLAKE2b-256 | 615173b4314b498c354e008eaef83dce2080c503754640229eee344a470caada |
File details
Details for the file fast_simplification-0.1.9-cp310-cp310-win_amd64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 212.2 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 741b3fd2554536555f60bea826ba42c8d36962416330a062de475d916d756e3e |
|
MD5 | 54c2ba4e613937ac6838e0b825bbb89a |
|
BLAKE2b-256 | 8ae8187ff0126ae0b02858344370eab416aa0c2ea746d8e02c47a7f624a5493a |
File details
Details for the file fast_simplification-0.1.9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d64dceaaa58684ddcdb489b3a5b23cc5046d5890954f3f591c2080cda12381ee |
|
MD5 | 4c17a25a7c209caf44de3a981b5e0f20 |
|
BLAKE2b-256 | 7b6711feb25ffc4534de87df08dbfa6ce9f3d08a90fc37c87178a8f57dc9ee20 |
File details
Details for the file fast_simplification-0.1.9-cp310-cp310-macosx_11_0_arm64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 241.1 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d45ce70f06b89a970b49a3d47b15dbb8dec76586ec9ac18a1a4a3db4cb6d3988 |
|
MD5 | 2038b97272d46c13d8ce5aac6946e2b8 |
|
BLAKE2b-256 | bbb92998d9027b901e53fe136142b5bedda26abda58eef4adeaeed6c502a3b57 |
File details
Details for the file fast_simplification-0.1.9-cp310-cp310-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp310-cp310-macosx_10_9_x86_64.whl
- Upload date:
- Size: 262.9 kB
- Tags: CPython 3.10, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b9e34fbcb73c673f6cf300529b8037846c05cbe589e093d8d0d40e242cde679 |
|
MD5 | f37eb5e0d28df589ad8bd5622752b48b |
|
BLAKE2b-256 | 6ab013f48ef2fe4102b7f716501a81db6b874a09fd591c476ad0d0b938c392d1 |
File details
Details for the file fast_simplification-0.1.9-cp39-cp39-win_amd64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 213.3 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48a5dfc3a036b5a98c4047f4b654ce8ecf5ee7ed9a9a637b2f2ca92eb2de5a88 |
|
MD5 | 4d78012989ca3ef1af82869e7cb667db |
|
BLAKE2b-256 | 8cb3232483ddaeec91d808740901f1708a8c8cd3e6b812b6d629fda5d19a3cf3 |
File details
Details for the file fast_simplification-0.1.9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea46ec50e1e1588ba850d49ccf130d688a975fb334cd2020d75360327ab97d37 |
|
MD5 | 2b23eb829f74b1a8dd77d95caee9e188 |
|
BLAKE2b-256 | 6a346bb0dc737a8d91010d3d1b0ca2e2785e8ffbeee43e7ed10882a32106992f |
File details
Details for the file fast_simplification-0.1.9-cp39-cp39-macosx_11_0_arm64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp39-cp39-macosx_11_0_arm64.whl
- Upload date:
- Size: 242.4 kB
- Tags: CPython 3.9, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63304bcdc804aae672157ddc6146e14d1fd227a141326bfda13711589b1d3016 |
|
MD5 | 177095adf1a0c003f6e637c7206a7044 |
|
BLAKE2b-256 | 342d53f552384b5370bd737758c9ec2e0319a171541e30f0e5d01c0faed8c6c9 |
File details
Details for the file fast_simplification-0.1.9-cp39-cp39-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: fast_simplification-0.1.9-cp39-cp39-macosx_10_9_x86_64.whl
- Upload date:
- Size: 264.0 kB
- Tags: CPython 3.9, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b9cf91e69b6ecd3c61907ae6620b133c94890122c3aa512c33575529c784a88 |
|
MD5 | 1ee5e845bd652dcae9196862281d586f |
|
BLAKE2b-256 | ce4a73213d70ff1e3acd4f061876e267ec578c51f1509a2470be23b58a4f2ce3 |