Skip to main content

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/

https://github.com/pyvista/fast-simplification/raw/main/doc/images/simplify_demo.png

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fast_simplification-0.1.13.tar.gz (27.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

fast_simplification-0.1.13-cp311-abi3-win_amd64.whl (378.3 kB view details)

Uploaded CPython 3.11+Windows x86-64

fast_simplification-0.1.13-cp311-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

fast_simplification-0.1.13-cp311-abi3-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (1.6 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

fast_simplification-0.1.13-cp311-abi3-macosx_11_0_arm64.whl (221.6 kB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

fast_simplification-0.1.13-cp311-abi3-macosx_10_9_x86_64.whl (225.7 kB view details)

Uploaded CPython 3.11+macOS 10.9+ x86-64

fast_simplification-0.1.13-cp310-cp310-win_amd64.whl (388.2 kB view details)

Uploaded CPython 3.10Windows x86-64

fast_simplification-0.1.13-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

fast_simplification-0.1.13-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (1.7 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

fast_simplification-0.1.13-cp310-cp310-macosx_11_0_arm64.whl (235.5 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

fast_simplification-0.1.13-cp310-cp310-macosx_10_9_x86_64.whl (247.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

Details for the file fast_simplification-0.1.13.tar.gz.

File metadata

  • Download URL: fast_simplification-0.1.13.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fast_simplification-0.1.13.tar.gz
Algorithm Hash digest
SHA256 5e95c966ad4ddf04b0a49013c92e5a06a3b888c6e02bb3c7ab3817fc8ba0f03f
MD5 5a4e6bd584f1990420cd67b968812691
BLAKE2b-256 e1c7e000571f3f271fb88b92f2bf0fbe48a12993c77cde8093c490997df46a6d

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_simplification-0.1.13.tar.gz:

Publisher: testing-and-deployment.yml on pyvista/fast-simplification

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_simplification-0.1.13-cp311-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.13-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 94565811e708828f77c0a30eb8f05278e8f5826f2cbf1791cc470f6471dd0dce
MD5 53189920f80e097b822bc1d15040a22b
BLAKE2b-256 33443208d9c3f76b5cadcc43b96a0db21c62926b3aa35be2cf16deb88cc7d4d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_simplification-0.1.13-cp311-abi3-win_amd64.whl:

Publisher: testing-and-deployment.yml on pyvista/fast-simplification

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_simplification-0.1.13-cp311-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.13-cp311-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c694f0f0bbafbaf29acf7043de603b4c85259f2eae7b802d4946d47976e9959d
MD5 1f3a143eb7e2f59128e6770f57bf8894
BLAKE2b-256 8c44ae0b556d62ed6a8c6f290435f4764fae9944f6857171b60613ab35f0f1a7

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_simplification-0.1.13-cp311-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: testing-and-deployment.yml on pyvista/fast-simplification

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_simplification-0.1.13-cp311-abi3-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.13-cp311-abi3-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 3ed45b1f07d718832aec76ae92c91ab8e84001d935055379ce0089b0cfb87870
MD5 8e61ba9c4a4ca4da75d6c7e73fae9a37
BLAKE2b-256 34644ecbf504f3207a36e7713013b473d40fb56d1b665b7d6ee80ffd3b217f81

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_simplification-0.1.13-cp311-abi3-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: testing-and-deployment.yml on pyvista/fast-simplification

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_simplification-0.1.13-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.13-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f3bcd10b5de2a85fe41d4ba27ccdf3adc723c2a0f5a1afb47f01e95c6c158651
MD5 29d32877207555133d90f269f05bc6fa
BLAKE2b-256 491cea8c057ebf02a3b24eddc79619bdc1c844a2354dbdf66176d86750755846

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_simplification-0.1.13-cp311-abi3-macosx_11_0_arm64.whl:

Publisher: testing-and-deployment.yml on pyvista/fast-simplification

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_simplification-0.1.13-cp311-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.13-cp311-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4a89d1d0ecbb547528048ab6333bf2287767dbba727fa0553b18a138802d9b61
MD5 50b85bc626fdc0eb3e35d7125a8b2427
BLAKE2b-256 6f81ea0026164dd9dbaf6904176dbc716b8361548bbff7c0a273c14e2137ed27

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_simplification-0.1.13-cp311-abi3-macosx_10_9_x86_64.whl:

Publisher: testing-and-deployment.yml on pyvista/fast-simplification

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_simplification-0.1.13-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.13-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c57db49c27452168a60b00c6ab643ff8ed97b8ebb2d121315ec2553992adc6c0
MD5 998260bd1702f2bdb5f5baaaa56ae907
BLAKE2b-256 8968745d696cdffa34a383a388d0b0f2e73ed7e43dedfe9c423a64cdf2f4f91d

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_simplification-0.1.13-cp310-cp310-win_amd64.whl:

Publisher: testing-and-deployment.yml on pyvista/fast-simplification

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_simplification-0.1.13-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.13-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a6c82a25fff7892b39eb4147b6286fea4ff42dd62d1137591197d0b6b2f68a78
MD5 9d62b5f3de59efd36dc7fac15094756a
BLAKE2b-256 7d4c5fbce7b06b15815be0e8f54cc9456e1ada9a26f5c4df236526c0d7344dea

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_simplification-0.1.13-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: testing-and-deployment.yml on pyvista/fast-simplification

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_simplification-0.1.13-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.13-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 da89a8787fdc76ed5dea5d373dc95f941044e658dce65e23fca32a624ce5d440
MD5 e63af2137d4a550aa70008ae56325e35
BLAKE2b-256 9e1cc596ad29a46665ce8f77579446fae46df2a23a8ac7f8bb62bf104fb24643

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_simplification-0.1.13-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: testing-and-deployment.yml on pyvista/fast-simplification

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_simplification-0.1.13-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.13-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9fad0599c569fc8d56429a42812431f51f732cd56046b06f5e061f8cb6e7a54d
MD5 393a8895e91d8ac60ea6b8cbc18d43e8
BLAKE2b-256 1d6b0de466b754eab24c04ced934ac5912e780a7ea2bd07e7ecd328a4dae115e

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_simplification-0.1.13-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: testing-and-deployment.yml on pyvista/fast-simplification

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_simplification-0.1.13-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.13-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3c95bd92a4817eb96d994e792df38ead3457ea01d58a095d81d53cb0fb4e1578
MD5 144ce82855ba07975e07a4f7128510c5
BLAKE2b-256 3f97812aa10436c0d0d397ad9738cd4c98cf185438b9eccc4f6e53c19a7019e7

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_simplification-0.1.13-cp310-cp310-macosx_10_9_x86_64.whl:

Publisher: testing-and-deployment.yml on pyvista/fast-simplification

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page