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.6.tar.gz (26.2 kB view details)

Uploaded Source

Built Distributions

fast_simplification-0.1.6-cp312-cp312-win_amd64.whl (211.7 kB view details)

Uploaded CPython 3.12 Windows x86-64

fast_simplification-0.1.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

fast_simplification-0.1.6-cp312-cp312-macosx_10_9_x86_64.whl (259.1 kB view details)

Uploaded CPython 3.12 macOS 10.9+ x86-64

fast_simplification-0.1.6-cp312-cp312-macosx_10_9_universal2.whl (488.2 kB view details)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

fast_simplification-0.1.6-cp311-cp311-win_amd64.whl (211.9 kB view details)

Uploaded CPython 3.11 Windows x86-64

fast_simplification-0.1.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

fast_simplification-0.1.6-cp311-cp311-macosx_10_9_x86_64.whl (265.3 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

fast_simplification-0.1.6-cp311-cp311-macosx_10_9_universal2.whl (493.7 kB view details)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

fast_simplification-0.1.6-cp310-cp310-win_amd64.whl (211.8 kB view details)

Uploaded CPython 3.10 Windows x86-64

fast_simplification-0.1.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

fast_simplification-0.1.6-cp310-cp310-macosx_10_9_x86_64.whl (263.9 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

fast_simplification-0.1.6-cp310-cp310-macosx_10_9_universal2.whl (492.3 kB view details)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

fast_simplification-0.1.6-cp39-cp39-win_amd64.whl (212.3 kB view details)

Uploaded CPython 3.9 Windows x86-64

fast_simplification-0.1.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

fast_simplification-0.1.6-cp39-cp39-macosx_10_9_x86_64.whl (264.8 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

fast_simplification-0.1.6-cp39-cp39-macosx_10_9_universal2.whl (493.9 kB view details)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

fast_simplification-0.1.6-cp38-cp38-win_amd64.whl (213.0 kB view details)

Uploaded CPython 3.8 Windows x86-64

fast_simplification-0.1.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

fast_simplification-0.1.6-cp38-cp38-macosx_10_9_x86_64.whl (263.6 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

fast_simplification-0.1.6-cp38-cp38-macosx_10_9_universal2.whl (491.9 kB view details)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

File details

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

File metadata

  • Download URL: fast_simplification-0.1.6.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for fast_simplification-0.1.6.tar.gz
Algorithm Hash digest
SHA256 ec7197a4e6594f6f4473eab216dce689e26bb8cba6d759c1b2819613a521532b
MD5 c9c6da92db713384f4d0852067f45e92
BLAKE2b-256 efba1995457fd55793c4fa8438c379117d0d817436a5448c8fee01d86daf73e1

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 2be597e0fac40f60fa4ba113a85c9c86d16f8bcac792ad37804901e5cb3ce7d7
MD5 913b7e1c4fb25264d04cf61d89e9a1cd
BLAKE2b-256 d895ef62cc332b49ec83be2fc87a02bf3aae6eec684527c568dae842a85a1771

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8d72c7a7d881873e713f7633bbbb328e81babfa7105ff8fb57bbae670aec164d
MD5 90082ae15475bbec488347358b9c851b
BLAKE2b-256 6211e56ac4e7801a9e1e11e27cb2b1d6677b5df12f35c3a4df0a98f6bb160d36

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 266c0809120adbcec2c4caa72d11f58524adf7633657ff436756be215175b512
MD5 7548ed920869e547c926389fc9577aea
BLAKE2b-256 1e098aab53d29725cac782e8585340d05b105ba2c39019a47eafd3d0aebf8810

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp312-cp312-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 bb499111b3a305ebf559a6e06cb25fd9ad9d0e4777350051d52a06425c880786
MD5 0ae65b97ecd5d3d86e8da52a81ff02a7
BLAKE2b-256 417b170505a41f26d50c406e3f10a727570b2d6c40735ff7e70853fc6f9d6360

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 94c96c8b3baa515e9deb9dac0d59d3f01bd12ecd639a04c9ff750703f784ab22
MD5 6df8954ad3f6cec2b364de646c9ed743
BLAKE2b-256 c4911ac5f1994b2b3a7f7d9c01f7c63c123601448a5afc3e09535843302d9726

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fadc9b5164eeff2694d1c6a9e4484080e658e3973af94a1a4d091e76f29661f8
MD5 94634d30bfbf6b0d4f3fe5430a0bec06
BLAKE2b-256 e9608929bdfc2f543452b76c154b1bb1c5b5593cb8844b4b2d3fa3a0937f254f

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ebcbf4887c2106554b90e3c0701d778d45e3614028dcebbd404b382a1e14c462
MD5 b7daa1f0e5da302093a5e559d4e12712
BLAKE2b-256 ff5ec35bb912f2e29c8a0a3d4acead39fa02c3c589f434d1d980c3b2858e23fe

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 183aa863679d1c2fd2c798193e6873ead4b6fa387d2c63ab499d195e14e55d5d
MD5 cb5e4be407e5fb1382b30eb7f17c566b
BLAKE2b-256 feb2ee582b6cc0856b8683d9f671d4431dca3760bd6c61bb3ecabf33fd99660f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 46e6996e569bf39a294f1e6995733de8d7fc08377aa500ff161e543121d5898a
MD5 616c091d4380af90a672d5d82a91cd95
BLAKE2b-256 a2c41e98d67b78b81ada6966231d5a8522ba5ca6a5d5766a297a829e68521ce0

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cf58a0f259e7f141d116b44813c293925144d7fdc112373c1dcfe284ad9082c8
MD5 07c822a081035f51cf4c8704aaa6b1d3
BLAKE2b-256 9a184cd3bbb8ce001ea0db1f5084e90b9bd50bd2a94fb518d5f34d82c3e33975

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c439ce220c2847df10c5e477b51aba663aa5dd015f9ee482ddd2424e3572955b
MD5 0740b9b4a4d6e790fd82386d9dc7f0d5
BLAKE2b-256 1a34c6143b4bc970ec3d743d5096a5dcf1086dcd5dc3178a279a6115dea41050

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 7077727cfe1a7017fbf57b2d279994dc2f3243ebb965a0f8c11ef79eaa2697f5
MD5 a5d7b3e5caccb4d00fe50edd75b78067
BLAKE2b-256 173677cc34e83bf3968014cdaf3b3027f0a8e1c70bc918d5df9f80abac27f047

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 2472d8617e947818dbbb5531536bd3f71d4aa1c47c269efa793656440600662a
MD5 fc4e007d1dba89f1d3561886019fbaad
BLAKE2b-256 9512e0dc02198dd708cb51e9f30a64195cdf3e1af754148b0eed12a673f6d56d

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e8ff3b2f3976384bfa74719361b421d027bb386a19033c036aa6dd296da7fbed
MD5 757bfe52b9e048442c22894d4839f744
BLAKE2b-256 d7415bfe3d58c62c9d56130ec5474fa93f9af78706c6d01d3d3c642027ecb166

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 95c36ed8f553827d7eee8ed2a90133f8371805d91d06fa5f3ee9a9f2d9c51ad2
MD5 e40e02495bc87f8396ceb618443f6567
BLAKE2b-256 297486da8051d918c7f2c7fc91b6c753801c5bbb48f34ec85f41b56ce86aa372

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp39-cp39-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp39-cp39-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 58fd8778e2a408536d36b365ab593a4542e2a203a44b07b82bc078081ff91c23
MD5 29a088ad3f426a64daaa1c80c1e5a065
BLAKE2b-256 12320f9d49f44f907ab95ed55d224dcd530fbcd6bd4aa52adc69118db7b70b76

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 f01f68d4a4b32206dc7c4aee828a4961e39df35d57b7948b02065e19c362c402
MD5 45af51a495c0d1aae25e2612e39a3363
BLAKE2b-256 fc2bc940b3a87e0a3fa059bab5aea7ac1e575cd274c2f11cf1b654d7cbae751a

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8be718a85dcf6c7bc6b86f2c3731d56e8061a5671df8b53d3851820558f82ad2
MD5 c69654f5a8a2e1321b7ec45da4503f59
BLAKE2b-256 1dd7a90fb644a2a05b41d1af2de8eb929e06813c4ff6fb0d0c19099816e926a7

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a0ebbceb9f82850087975ce64638c2b834400589a63b66bc120e158582f52425
MD5 60b06d6a95d24e309a91427564473e04
BLAKE2b-256 ab96606c1641a128c5677bee4d108be85f9fb2c3f414d5722e96db668f8a0f81

See more details on using hashes here.

File details

Details for the file fast_simplification-0.1.6-cp38-cp38-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for fast_simplification-0.1.6-cp38-cp38-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 5f1aca2ecfedac2bde03334a4894c8b09227c6b2b5215c0a62e778730bc54d0e
MD5 68e4c18d847fbae8f00ee57e8fb415f9
BLAKE2b-256 2934ed5a37bd1862834aed39797cef87e08253c4e3213a05f25004862b4ba7ac

See more details on using hashes here.

Supported by

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