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

Uploaded Source

Built Distributions

fast_simplification-0.1.7-cp312-cp312-win_amd64.whl (211.6 kB view details)

Uploaded CPython 3.12 Windows x86-64

fast_simplification-0.1.7-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.7-cp312-cp312-macosx_10_9_x86_64.whl (259.3 kB view details)

Uploaded CPython 3.12 macOS 10.9+ x86-64

fast_simplification-0.1.7-cp312-cp312-macosx_10_9_universal2.whl (488.3 kB view details)

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

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

Uploaded CPython 3.11 Windows x86-64

fast_simplification-0.1.7-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.7-cp311-cp311-macosx_10_9_x86_64.whl (265.5 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

fast_simplification-0.1.7-cp311-cp311-macosx_10_9_universal2.whl (493.8 kB view details)

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

fast_simplification-0.1.7-cp310-cp310-win_amd64.whl (211.7 kB view details)

Uploaded CPython 3.10 Windows x86-64

fast_simplification-0.1.7-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.7-cp310-cp310-macosx_10_9_x86_64.whl (264.1 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

fast_simplification-0.1.7-cp310-cp310-macosx_10_9_universal2.whl (492.4 kB view details)

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

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

Uploaded CPython 3.9 Windows x86-64

fast_simplification-0.1.7-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.7-cp39-cp39-macosx_10_9_x86_64.whl (265.0 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

fast_simplification-0.1.7-cp39-cp39-macosx_10_9_universal2.whl (494.1 kB view details)

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

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

Uploaded CPython 3.8 Windows x86-64

fast_simplification-0.1.7-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.7-cp38-cp38-macosx_10_9_x86_64.whl (263.7 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

fast_simplification-0.1.7-cp38-cp38-macosx_10_9_universal2.whl (492.0 kB view details)

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

File details

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

File metadata

  • Download URL: fast_simplification-0.1.7.tar.gz
  • Upload date:
  • Size: 26.3 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.7.tar.gz
Algorithm Hash digest
SHA256 5e44a71fc7d3321688c999952eb2c8dcc3ddbad5ba3f847b2b50eda50155fbe2
MD5 00c5f93722d1bd4ad83b84b2555b9d73
BLAKE2b-256 7da4fdff959db6fa460ae94f8571c93cd18c92a8eb8be32dd7b2ca837f3587ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4deeeffc0a289efdcad89c8595a57cd05b60a73d74e4a030965c47322480e6b4
MD5 b322faca0ab9a38a72421a4c7d5024c5
BLAKE2b-256 ca4e1775329202a494c8a22f337862823c0710858f10f31ba6acf54db600b74c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3305c6435d6bc4f90a17d812104c7d4c1a528e63bd8d3f42e40e357b778a99b8
MD5 3a92622b5e96c8bdd988c9f5691310bb
BLAKE2b-256 2af0541a5f78286e3318d770107d4107f893f2f0462819fb6cf1b4fffa1a7c76

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8a999b236dc6ff9535895c9d36449aa896e88fda9c1775e1f869fb7a68238d04
MD5 c9f6a40ed6e49ac88435fc60503b589d
BLAKE2b-256 ac2b0de8530a23b356b849e5a3849823eed793815001d57a7655be9ad4020591

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 4a790983063194876b438d2e5beb73f4488821a9e52d31e711edc0fd2d815fdd
MD5 1afaaa2e1f6acb09f8207ed0de0baa76
BLAKE2b-256 4d6e3461459f501944cacbf267c3b1eaa6729e03f4fc66264ecb7b8989c29f7d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 0ea7e65946be93a2891587631f4dd4f66f6bdeb879a7d3a0f8d218c703e67d11
MD5 7999fe4fc26b5238a3a248671c29edd3
BLAKE2b-256 5ce6b5edd1fe1f8073e0944bc046ad44ad5c048ba38a381e5d225686416f6354

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 008e8459225e1d824f7ee53cc4a3859d025db1846397674b55e880df639fd731
MD5 8243e5fc7dbf4f78a6c243ce36a88f8f
BLAKE2b-256 db6917bacd1895c4fc8ac2e16cef268b39a85d37aeb40a2e71e522989241b288

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 df4119fe98cee6ef439ec390aea3b2bbb22a8e918d00ecd0807036700aeafe9c
MD5 ff3375be4a2c3eb1d1e7883f19d843a0
BLAKE2b-256 a21223c5a37b94a7cd941cad9a94f61fab2b33fce15f20a7fb153589a7300810

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 b9db12e907ccb6400fd3922cd59432e8477ad72c00cb27983b303457dd18797f
MD5 edab7609880a22794c65865a01094283
BLAKE2b-256 0166e42ef879f01db2f266884c50c7eb8f351a0921b028ce8e3a499a28cfedcd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4316f80e271ffa3514153649f3fcff5cd5da952ba99641a8049e893df39982da
MD5 4a7f00d9fb5b0fab786c0573623abef2
BLAKE2b-256 f05fa1fd8676fc382f3f9d9ee472b89a32c7f9ff9e38176d1489a2a439cf97e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 64848c4beb3f805e99840c08b3e16085c03cb06ea82d13a87e64a3ed8d3b7431
MD5 db15ade4d95a378928b4752dbca54980
BLAKE2b-256 7c6fd38a52418721a4e51260ecfbffd08ef134865f2a0b5b17c48bf23d5b03bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a19af01ed0b7b853f0d360caca108ee662e71a8c294e288e82d321aa0c94a457
MD5 6c81fdbd9a1b9369504fe115b112a6d1
BLAKE2b-256 8152b8a26f5839498350d0b4e4c53dcb3833912bfaf237227ccf8d8fa873e922

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 4b9ec0688f8e7fac810dc438bd1ea337a72b6fa60f86b7fae0a20090bf42f812
MD5 036f8fb58afc6c8be4db9752c118521b
BLAKE2b-256 2002d9d028c1d03980462c8a36c4359f7fe8e609756c566881c04f02115f439e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 0619375a552e838ff0215b52717430f6c620ad8bad98c9631ea9a19a0b582ac9
MD5 babfb189c0fd2042ca0e9ad18bc54bbd
BLAKE2b-256 4f528f854984aa637d4c79329546223832ffde78c6aba2aba8eb6443ad42b904

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 27431a73a71621f0151b2b47530537ebd2a3beeb01951355077da3c68eb459d0
MD5 baac09a06d7d2a1dd307ec0bc93bd504
BLAKE2b-256 b4028654a34be0520c950faef133d53aaad3c558c1f6e6f45c5b4d43d4a245a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 99139a70a302d0d4307b9768e6b6b6a149445fde4ff135c93cf6d98e09c9f0e0
MD5 57dcc21f0c9cfd69715f6de18f58e66f
BLAKE2b-256 ecb9f677eeac11876b27cdfdbf690cfbfde8cf8744deddef3344a7a293be7f43

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp39-cp39-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 129c7d6156c5dc8ab70230f528ea238c97dad69b3871f5c1e3165600a30f371f
MD5 898369affdad6d43971013ccc0891414
BLAKE2b-256 e2fe68aafde8cbd1b9fce99338adb84d1394bae25a43d5ca3021e466bd2ad44a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 e42d5439ad2776a738af20b09527ebc67087ff9f3c9f0dc557905c7fac5d85e1
MD5 b2db8a9dfab74eff70807eb8bddcfd11
BLAKE2b-256 edc927c2ba94879d46d814c98825dee1ea7edc9ec9c9b5d0bec95f4b1f3c4c40

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ec2c6d39712d585723a82fcbdc400a70215d3321f80824f61463b50e4f5f1082
MD5 9eefbf34c1ceb06914c76757fcba6d13
BLAKE2b-256 8b0053c4cb3ce4c354ded3021d4ba420ff82f8a57a3b2f2a5f446d072b24c0a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d844830669d0ac2f78e6335b707dd4a4c14a9ce29f92842ff5b8b3236858a32a
MD5 27040c7be899483cb4d75c80098a8506
BLAKE2b-256 b5bd914689a27631b32a01cf8a35c466c21918d0e24ca641959641ad14abed79

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_simplification-0.1.7-cp38-cp38-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 973404120db63f1e5c8e80700f0e8692f073da457fe11e47768d5291d4d10175
MD5 0a2eeb9cf6b0cda8c695d358dbb11273
BLAKE2b-256 d39b328aa1d65b19968b81e6abc35c643cc61e62321d3becac6d7a0306568841

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