Skip to main content

Meshoptimizer wrapper with cython

Project description

almeshopt

almeshoptmeshoptimizer 的 Python 封装(Cython + CMake + scikit-build-core),提供面向 NumPy 的网格索引/顶点重排、渲染性能优化、压缩编解码、简化(LOD)、meshlet 构建等接口。

  • 安装pip install almeshopt
  • 导入import almeshopt
  • 实现:优先加载包内扩展模块 almeshopt_avx2(见 src/almeshopt/__init__.py

说明:当前发布策略为 只发布 AVX2 版 wheel(构建时 USE_AVX2=ON)。源码中保留了非 AVX2 的扩展实现入口,但目前不发布对应 wheel。


快速开始

安装

pip install -U almeshopt numpy

典型用法:简化 + 顶点/索引重排

import numpy as np
import almeshopt

# indices: (N*3,) uint32
# vertices: (V,3) float32,必须 C-contiguous
indices = np.array([0, 1, 2, 2, 3, 0], dtype=np.uint32)
vertices = np.array([[0,0,0],[1,0,0],[1,1,0],[0,1,0]], dtype=np.float32)

# 目标三角形数量(这里演示:减半)
new_indices, error = almeshopt.simplify(
    indices,
    vertices,
    target_count=len(indices)//2,
    target_error=1e-2,
)

# 进一步优化:顶点 fetch 重排(返回 new_indices, new_vertices)
opt_indices, opt_vertices = almeshopt.optimize_vertex_fetch(new_indices, vertices)
print(opt_indices.shape, opt_vertices.shape, error)

API 参考(当前仓库实际封装的接口)

下列函数均来自扩展模块(src/almeshopt/almeshopt.pyx / src/almeshopt/almeshopt_avx2.pyx),导入统一使用 import almeshopt

约定:indices 通常为 np.uint32 的一维数组(长度为三角形数 × 3);vertices 通常为二维数组(例如 (V, 3)np.float32)。多数接口要求数组为 C-contiguous

1)重映射与索引生成

  • generate_vertex_remap(indices, vertices)(remap, unique_count)
    根据 indices + vertices 生成顶点重映射表。
  • remap_vertex_buffer(vertices, remap)new_vertices
  • remap_index_buffer(indices, remap)new_indices
  • generate_shadow_index_buffer(indices, vertices)shadow_indices
  • generate_adjacency_index_buffer(indices, vertices)adjacency_indices(每个三角形 6 个索引)

2)渲染性能优化

  • optimize_vertex_cache(indices, vertex_count)new_indices
  • optimize_vertex_cache_strip(indices, vertex_count)new_indices
  • optimize_overdraw(indices, vertices, threshold=1.05)new_indices
  • optimize_vertex_fetch(indices, vertices)(new_indices, new_vertices)

3)压缩与解压(索引/顶点)

  • encode_index_buffer(indices, vertex_count)bytes
  • decode_index_buffer(encoded_data, index_count)np.ndarray[np.uint32]
  • encode_vertex_buffer(vertices, level=2)bytes
    支持任意 C-contiguous 的 numpy 数组(例如 float32/uint16/uint8),level 越高压缩率越好但更慢。
  • decode_vertex_buffer(encoded_data, vertex_count, vertex_stride, dtype=np.float32)np.ndarray
    vertex_stride 单位是 字节(例如 uint16XYZ0 padding,stride=8)。
  • encode_filter_oct(data, bits=8)np.ndarray[np.uint8]
    Octahedral 编码(目前封装为返回字节数组;使用前请确保输入数据形状/stride符合预期)。

4)简化(LOD)

  • simplify(indices, vertices, target_count, target_error=0.01, sloppy=False, vertex_lock=None)(new_indices, error)
  • simplify_with_attributes(indices, vertices, attributes, weights, target_count, target_error=0.01, vertex_lock=None)(new_indices, error)
  • simplify_points(vertices, target_count, colors=None, color_weight=1.0)indices

5)空间排序

  • spatial_sort_triangles(indices, vertices)new_indices
  • spatial_sort_remap(vertices)remap

6)分析(统计指标)

  • analyze_vertex_cache(indices, vertex_count, cache_size=16, warp_size=0, primgroup_size=0)dict
  • analyze_overdraw(indices, vertices)dict

7)Meshlets(Mesh Shading)

  • build_meshlets(indices, vertices, max_vertices=64, max_triangles=124, cone_weight=0.0)dict
    返回键:meshlets(Nx4)、meshlet_verticesmeshlet_triangles
  • compute_meshlet_bounds(meshlet_vertices, meshlet_triangles, vertices)dict

8)Triangle Strip

  • stripify(indices, vertex_count)strip_indices

示例(仓库内 demos)

目录:demos/

  • 点云压缩(带颜色)demos/ply_compress.py / demos/ply_decompress.py
    主要展示 encode_vertex_buffer / decode_vertex_buffer 的用法(uint16 几何 + uint8 颜色)。
  • GLTF/GLB 简化(带 UV)demos/mesh_simp_with_texture.py
    展示 generate_vertex_remapremap_*simplifyoptimize_vertex_fetch 的组合使用。

“地形切片 / Cesium Terrain”接口说明

你可能在仓库中看到了 dem_to_cesium_terrain_*.plan.md 之类的规划文档,但当前仓库代码里并没有 TerrainTiler / .terrain 编码器等实现文件(例如 terrain_tiler.pyquantized_mesh.*terrain_mesher.* 均不存在)。
因此,本包当前对外 API 只包含 meshoptimizer 封装(见上文 API 参考),不包含地形瓦片管线接口。

如果你希望把地形切片管线也纳入 almeshopt 并发布为 wheel,我们可以再按“规划文档 → 实际代码”补齐对应 C++/Cython/Python 层实现与测试。


构建与发布(维护者)

构建 wheel(Windows + Linux manylinux via Docker Desktop)

仓库提供 PowerShell 脚本,目录:scripts/

  • 一键构建并上传(默认 cp311/cp312/cp313/cp314;Windows+Linux)
.\scripts\release.ps1
  • 只构建 Windows wheel
.\scripts\release.ps1 -Platform windows -SkipUpload
  • 只构建 Linux manylinux x86_64(Docker Desktop)
.\scripts\release.ps1 -Platform linux -SkipUpload

上传到 PyPI

推荐使用环境变量 PYPI_API_TOKEN

$env:PYPI_API_TOKEN="pypi-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
.\scripts\upload_pypi.ps1 -SkipExisting

中文终端显示:脚本中已包含 chcp 65001 + UTF-8 输出设置,并默认禁用 twine 进度条以避免 GBK 控制台编码问题。


常见问题(FAQ)

1)为什么 pip install almeshopt 但内部模块叫 almeshopt_avx2

almeshopt_avx2 是包内部的扩展模块名;对用户而言 安装名与导入名都始终是 almeshoptalmeshopt/__init__.py 会优先加载 AVX2 扩展。

2)数组需要什么 dtype / shape?

不同接口对 dtype/shape 有要求;最通用的组合是:

  • indices: np.uint32,形状 (T*3,)
  • vertices: np.float32,形状 (V,3) 或拼接属性后的 (V, k) 并确保 np.ascontiguousarray(...)

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

almeshopt-0.1.9-cp314-cp314-win_amd64.whl (130.5 kB view details)

Uploaded CPython 3.14Windows x86-64

almeshopt-0.1.9-cp314-cp314-win32.whl (112.2 kB view details)

Uploaded CPython 3.14Windows x86

almeshopt-0.1.9-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (277.0 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

almeshopt-0.1.9-cp313-cp313-win_amd64.whl (127.1 kB view details)

Uploaded CPython 3.13Windows x86-64

almeshopt-0.1.9-cp313-cp313-win32.whl (109.5 kB view details)

Uploaded CPython 3.13Windows x86

almeshopt-0.1.9-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (276.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

almeshopt-0.1.9-cp312-cp312-win_amd64.whl (126.9 kB view details)

Uploaded CPython 3.12Windows x86-64

almeshopt-0.1.9-cp312-cp312-win32.whl (109.5 kB view details)

Uploaded CPython 3.12Windows x86

almeshopt-0.1.9-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (275.1 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

almeshopt-0.1.9-cp311-cp311-win_amd64.whl (130.2 kB view details)

Uploaded CPython 3.11Windows x86-64

almeshopt-0.1.9-cp311-cp311-win32.whl (110.9 kB view details)

Uploaded CPython 3.11Windows x86

almeshopt-0.1.9-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (275.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

File details

Details for the file almeshopt-0.1.9-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: almeshopt-0.1.9-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 130.5 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for almeshopt-0.1.9-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 617b1d5dfd0b76bc5bae59225572798156c94fb98c7196f2abc987038c76e2ac
MD5 915829cbfbaec1b20bf29a826a0bb1a1
BLAKE2b-256 7ce1e9ea5b26da972425c8d40dec8c96fb44d3d58f074d88e00279cd8528e1e2

See more details on using hashes here.

File details

Details for the file almeshopt-0.1.9-cp314-cp314-win32.whl.

File metadata

  • Download URL: almeshopt-0.1.9-cp314-cp314-win32.whl
  • Upload date:
  • Size: 112.2 kB
  • Tags: CPython 3.14, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for almeshopt-0.1.9-cp314-cp314-win32.whl
Algorithm Hash digest
SHA256 1aaf9ae1c23425403f2a69e21073a7324c496a1df9ceaf7820981e655bad46b7
MD5 c245c917a89d903c57f68718e3ed7e51
BLAKE2b-256 901a617db2f4737d8616cea45740492069b89a33094c88e3cc811a51051973c3

See more details on using hashes here.

File details

Details for the file almeshopt-0.1.9-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for almeshopt-0.1.9-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 b123ef75a2ba31fc3ef05bd3b5739716fcd0e8452f1e3f9a8368196c928c9b2a
MD5 53cbe79391bb07f160d9e6853fc1f886
BLAKE2b-256 5396dda738f9d43df6d318e0c080d45c5ccf6856c7e814a3b9f854582b136808

See more details on using hashes here.

File details

Details for the file almeshopt-0.1.9-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: almeshopt-0.1.9-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 127.1 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for almeshopt-0.1.9-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 9b29f954c1de083d71e500976cad986302cecccda17e6bf1262dc2a91dd12809
MD5 43070d76e844cdb125bc5988190a4a8c
BLAKE2b-256 20d9dba2a7b85fa19fe527f2ac64ab9b83d1c90b9f1666279b1e50bcce76398b

See more details on using hashes here.

File details

Details for the file almeshopt-0.1.9-cp313-cp313-win32.whl.

File metadata

  • Download URL: almeshopt-0.1.9-cp313-cp313-win32.whl
  • Upload date:
  • Size: 109.5 kB
  • Tags: CPython 3.13, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for almeshopt-0.1.9-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 a7fbe67e6ab0194047215ef68e804e59e242dc3871f67a02fd2ece2d07d032eb
MD5 50a8481fae01b704dbc5d0add018244e
BLAKE2b-256 373991636857d0ea1c0d2337d2d5f28f35c595337029867f8b48c0b2067a261d

See more details on using hashes here.

File details

Details for the file almeshopt-0.1.9-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for almeshopt-0.1.9-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 67e77eafc23377208f516de08be600bb1747c7ef0418cc6f94c479cb799922fa
MD5 ea6ceb7badd199e8af9be9b99f0e7bc8
BLAKE2b-256 34da9861e4c5dc61ab7bc9c4de2d27751ae43f3a58af860ed1a23e65ff781ebd

See more details on using hashes here.

File details

Details for the file almeshopt-0.1.9-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: almeshopt-0.1.9-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 126.9 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for almeshopt-0.1.9-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 227f33d8ff6541522369f881955ca13d74b109fe8652a972c5d7862c4c9efc7d
MD5 229bac30b5f48a562c6ac6750332edb0
BLAKE2b-256 b2d7ceef46944bcc3f70feee1b30c76aafe953f51ad61d9ad1cc9455f1b28faa

See more details on using hashes here.

File details

Details for the file almeshopt-0.1.9-cp312-cp312-win32.whl.

File metadata

  • Download URL: almeshopt-0.1.9-cp312-cp312-win32.whl
  • Upload date:
  • Size: 109.5 kB
  • Tags: CPython 3.12, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for almeshopt-0.1.9-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 01598e87865605fc4bd6df7009fe9f4d5854f01b5f7aaf2086b9736a4ae9127a
MD5 d43bf8bcb3aa905a984cee580f65d482
BLAKE2b-256 65b6ea15d07cd886db39d91216d99595fbc7a5b3b38461b571871e823715fbc7

See more details on using hashes here.

File details

Details for the file almeshopt-0.1.9-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for almeshopt-0.1.9-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 aced5935acd8fba3eded618cb88947be773d4f4270033b3554f64cb784d8c056
MD5 519f4fb00a1b93928924f41e63b15292
BLAKE2b-256 09cbed4854915425e5650997e74f71722c1a52acd2b9e917255335da2f1d5738

See more details on using hashes here.

File details

Details for the file almeshopt-0.1.9-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: almeshopt-0.1.9-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 130.2 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for almeshopt-0.1.9-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d3f663722bc466bfc87c21d2565776aeeb0341a8aed4f43b933fd987b69ed1b2
MD5 64620a72417449f01413f9f1cfb75d67
BLAKE2b-256 25371d80242cdbcd63c4600089a69802c79f1cd53e2d609182dd6cd80fb0edc6

See more details on using hashes here.

File details

Details for the file almeshopt-0.1.9-cp311-cp311-win32.whl.

File metadata

  • Download URL: almeshopt-0.1.9-cp311-cp311-win32.whl
  • Upload date:
  • Size: 110.9 kB
  • Tags: CPython 3.11, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for almeshopt-0.1.9-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 3b9c8ba5ce63d69f1a4dcb7c8f6d8cafed8a0245dd2928e9fcbf1f0d053b0b11
MD5 b7c8a5190fa506105640144d7f0f9d1e
BLAKE2b-256 05ac04a1daa3dfd59a2b1d70b04b9ec298dcf388a3cc6946c10e0733e08cb194

See more details on using hashes here.

File details

Details for the file almeshopt-0.1.9-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for almeshopt-0.1.9-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 9293b3deb7f277ab9b352d148ee94c64f90263084474380db51405788512c4b5
MD5 fce0b6d7908fe4a32705f64cbcd28371
BLAKE2b-256 6b03d8329a7343baa8207ef10059e968f3561457dec07b02bd7c88f03837bc3f

See more details on using hashes here.

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