跳转到主要内容

Fast-Quadric-Mesh-Simplification库的包装器。

项目描述

这是一个对Fast-Quadric-Mesh-Simplification库的Python包装。由于遇到了与原作者相同的问题,但需要Python库,该项目旨在扩展原始库的功能,同时将其集成到Python和PyVista项目中。

访问完整文档请访问: https://pyvista.github.io/fast-simplification/

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

安装

Fast Simplification 可以通过 pip 在 Python >= 3.7 上安装

pip install fast-simplification

有关开发细节或如果通过 pip 安装出现问题,请参阅贡献

基本用法

基本接口非常简单直观,可以直接与点数数组和三角形数组一起工作

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)

高级用法

此库支持通过 PyVista 直接与 VTK 集成,从而为库提供简单的接口。因为此库将 VTK 删除算法的效率提高了 4-5 倍。

>>> 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()

与其他库的比较

pyfqmr库相比,此库包装了与该库相同的头文件,并具有类似的功能。在此库中,决定在额外的 C++ 层上编写 Cython 层,而不是直接通过 Cython 包装器进行接口。这导致轻微的性能提升。

重用上面的示例

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

现在,对其进行计时并与该库的非 VTK API 进行比较

>>> 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)

此外,fast-simplification库具有直接插件到pyvista库,这使得读取和写入网格变得容易

>>> import pyvista
>>> import fast_simplification
>>> mesh = pyvista.read('my_mesh.stl')
>>> simple = fast_simplification.simplify_mesh(mesh)
>>> simple.save('my_simple_mesh.stl')

由于这两个库都基于相同的核心 C++ 代码,您可以随意使用性能和互操作性最好的那个。

重新播放删除功能

此库还提供了一个接口,用于跟踪删除过程中发生的连续折叠,并重新播放删除过程。这可以用于不同的应用,例如

  • 将相同的删除应用于具有相同拓扑的多个网格集合

  • 计算原始网格顶点与删除网格顶点之间的对应关系图,例如将场数据从一种传输到另一种

  • 以比用较小的目标减少量删除原始网格更快的速度,以比用较小的目标减少量删除原始网格更快的速度重新播放删除过程

要使用此功能,您需要在调用 simplify 时将 return_collapses 参数设置为 True。这将返回删除过程的连续折叠,以及点和面。

>>> 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)

现在,您可以通过调用 replay_simplification 来重新播放删除过程,并获得原始网格顶点与删除网格顶点之间的映射。

>>> 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')

您还可以使用 replay_simplification 函数以比原始删除量小的目标减少量重新播放删除过程。这比用较小的目标减少量删除原始网格更快。为此,您需要将折叠子集传递给 replay_simplification 函数。例如,要以 50% 的初始速率重新播放删除过程,您可以运行

>>> 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)

如果您有一组具有相同拓扑的网格,您可以通过为每个网格调用 replay_simplification 并使用相同的折叠来应用相同的删除。这确保了删除的网格将具有相同的拓扑。

>>> 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))

贡献

通过将该存储库进行分支并使用以下命令以开发模式安装来为此存储库做出贡献

git clone https://github.com/<USERNAME>/fast-simplification
pip install -e .
pip install -r requirements_test.txt

然后,您可以添加您的功能或提交您的错误修复,并运行您的单元测试

pytest

单元测试将自动执行最低代码覆盖率标准。

接下来,为了确保您的代码符合最低代码风格标准,运行

pip install pre-commit
pre-commit run --all-files

最后,从您的分支创建一个拉取请求,我将确保对其进行审查。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

fast_simplification-0.1.9.tar.gz (26.2 kB 查看哈希值)

上传时间 源代码

构建分发

fast_simplification-0.1.9-cp312-cp312-win_amd64.whl (211.8 kB 查看哈希值)

上传时间 CPython 3.12 Windows x86-64

fast_simplification-0.1.9-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB 查看哈希值)

上传时间 CPython 3.12 manylinux: glibc 2.17+ x86-64

fast_simplification-0.1.9-cp312-cp312-macosx_11_0_arm64.whl (241.1 kB 查看哈希值)

上传时间 CPython 3.12 macOS 11.0+ ARM64

fast_simplification-0.1.9-cp312-cp312-macosx_10_9_x86_64.whl (263.4 kB 查看哈希值)

上传时间 CPython 3.12 macOS 10.9+ x86-64

fast_simplification-0.1.9-cp311-cp311-win_amd64.whl (212.4 kB 查看哈希值)

上传时间 CPython 3.11 Windows x86-64

fast_simplification-0.1.9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB 查看哈希值)

上传时间 CPython 3.11 manylinux: glibc 2.17+ x86-64

fast_simplification-0.1.9-cp311-cp311-macosx_11_0_arm64.whl (241.5 kB 查看哈希值)

上传时间 CPython 3.11 macOS 11.0+ ARM64

fast_simplification-0.1.9-cp311-cp311-macosx_10_9_x86_64.whl (263.3 kB 查看哈希值)

上传于 CPython 3.11 macOS 10.9+ x86-64

fast_simplification-0.1.9-cp310-cp310-win_amd64.whl (212.2 kB 查看哈希值)

上传于 CPython 3.10 Windows x86-64

fast_simplification-0.1.9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB 查看哈希值)

上传于 CPython 3.10 manylinux: glibc 2.17+ x86-64

fast_simplification-0.1.9-cp310-cp310-macosx_11_0_arm64.whl (241.1 kB 查看哈希值)

上传于 CPython 3.10 macOS 11.0+ ARM64

fast_simplification-0.1.9-cp310-cp310-macosx_10_9_x86_64.whl (262.9 kB 查看哈希值)

上传于 CPython 3.10 macOS 10.9+ x86-64

fast_simplification-0.1.9-cp39-cp39-win_amd64.whl (213.3 kB 查看哈希值)

上传于 CPython 3.9 Windows x86-64

fast_simplification-0.1.9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB 查看哈希值)

上传于 CPython 3.9 manylinux: glibc 2.17+ x86-64

fast_simplification-0.1.9-cp39-cp39-macosx_11_0_arm64.whl (242.4 kB 查看哈希值)

上传于 CPython 3.9 macOS 11.0+ ARM64

fast_simplification-0.1.9-cp39-cp39-macosx_10_9_x86_64.whl (264.0 kB 查看哈希值)

上传于 CPython 3.9 macOS 10.9+ x86-64

支持