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

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.

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.1.tar.gz (18.5 kB view hashes)

Uploaded Source

Built Distributions

fast_simplification-0.1.1-cp311-cp311-win_amd64.whl (103.9 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

fast_simplification-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (810.0 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

fast_simplification-0.1.1-cp311-cp311-macosx_10_9_x86_64.whl (121.0 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

fast_simplification-0.1.1-cp311-cp311-macosx_10_9_universal2.whl (225.7 kB view hashes)

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

fast_simplification-0.1.1-cp310-cp310-win_amd64.whl (103.9 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

fast_simplification-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (768.6 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

fast_simplification-0.1.1-cp310-cp310-macosx_10_9_x86_64.whl (120.1 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

fast_simplification-0.1.1-cp310-cp310-macosx_10_9_universal2.whl (224.7 kB view hashes)

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

fast_simplification-0.1.1-cp39-cp39-win_amd64.whl (104.1 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

fast_simplification-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (770.7 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

fast_simplification-0.1.1-cp39-cp39-macosx_10_9_x86_64.whl (120.6 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

fast_simplification-0.1.1-cp39-cp39-macosx_10_9_universal2.whl (225.4 kB view hashes)

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

fast_simplification-0.1.1-cp38-cp38-win_amd64.whl (104.4 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

fast_simplification-0.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (783.7 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

fast_simplification-0.1.1-cp38-cp38-macosx_10_9_x86_64.whl (120.6 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

fast_simplification-0.1.1-cp38-cp38-macosx_10_9_universal2.whl (225.7 kB view hashes)

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

fast_simplification-0.1.1-cp37-cp37m-win_amd64.whl (104.3 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

fast_simplification-0.1.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (741.0 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

fast_simplification-0.1.1-cp37-cp37m-macosx_10_9_x86_64.whl (120.6 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

Supported by

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