Meshoptimizer wrapper with cython
Project description
almeshopt
almeshopt 是 meshoptimizer 的 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_verticesremap_index_buffer(indices, remap)→new_indicesgenerate_shadow_index_buffer(indices, vertices)→shadow_indicesgenerate_adjacency_index_buffer(indices, vertices)→adjacency_indices(每个三角形 6 个索引)
2)渲染性能优化
optimize_vertex_cache(indices, vertex_count)→new_indicesoptimize_vertex_cache_strip(indices, vertex_count)→new_indicesoptimize_overdraw(indices, vertices, threshold=1.05)→new_indicesoptimize_vertex_fetch(indices, vertices)→(new_indices, new_vertices)
3)压缩与解压(索引/顶点)
encode_index_buffer(indices, vertex_count)→bytesdecode_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单位是 字节(例如uint16的XYZ0padding,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_indicesspatial_sort_remap(vertices)→remap
6)分析(统计指标)
analyze_vertex_cache(indices, vertex_count, cache_size=16, warp_size=0, primgroup_size=0)→dictanalyze_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_vertices、meshlet_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_remap、remap_*、simplify、optimize_vertex_fetch的组合使用。
“地形切片 / Cesium Terrain”接口说明
你可能在仓库中看到了 dem_to_cesium_terrain_*.plan.md 之类的规划文档,但当前仓库代码里并没有 TerrainTiler / .terrain 编码器等实现文件(例如 terrain_tiler.py、quantized_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 是包内部的扩展模块名;对用户而言 安装名与导入名都始终是 almeshopt。almeshopt/__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
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file almeshopt-0.1.11-cp314-cp314-win_amd64.whl.
File metadata
- Download URL: almeshopt-0.1.11-cp314-cp314-win_amd64.whl
- Upload date:
- Size: 139.2 kB
- Tags: CPython 3.14, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
084a7f547357c06419e087e6cc09d73226c35ba0d0439f331464b3a3d3d56444
|
|
| MD5 |
ad6d90f2b896f9b4b85abad4958c78a9
|
|
| BLAKE2b-256 |
60c578f8e904a671921c9f36435e63a49734c31991fb064dcf6d718f7bfa46e9
|
File details
Details for the file almeshopt-0.1.11-cp314-cp314-win32.whl.
File metadata
- Download URL: almeshopt-0.1.11-cp314-cp314-win32.whl
- Upload date:
- Size: 117.3 kB
- Tags: CPython 3.14, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4da54c9a307957c06563bd8049cbf1ea7c1a05a00065133d897f48b99b42ec8
|
|
| MD5 |
38c8a9606c0d6fa34b830ed1ad655f5e
|
|
| BLAKE2b-256 |
20231c929a75440d325551607a0318efc5eba519a207fd591e3a55ceba847f3e
|
File details
Details for the file almeshopt-0.1.11-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.
File metadata
- Download URL: almeshopt-0.1.11-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
- Upload date:
- Size: 280.8 kB
- Tags: CPython 3.14, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5fc414639092611989fa9eb70baf95a646d733c1dd8ee9e89fbc4e829615889f
|
|
| MD5 |
e76f2e9794b8d30bd91f69751923fb3b
|
|
| BLAKE2b-256 |
b4c8d56ac1c43fa3444ee3d2b99f219cbec409c137ca371fcccffa1684f41e6a
|
File details
Details for the file almeshopt-0.1.11-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: almeshopt-0.1.11-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 136.1 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0502b547a2f6e09498e567791f57acd8224aed297ff6960005f815aa6fe990c4
|
|
| MD5 |
c0dabbf6896413ede41857793f2f0989
|
|
| BLAKE2b-256 |
7ce6292e0aa5f6054604b674562bb6485ebf6ac61c8a96fdf553bd1470cb1c6f
|
File details
Details for the file almeshopt-0.1.11-cp313-cp313-win32.whl.
File metadata
- Download URL: almeshopt-0.1.11-cp313-cp313-win32.whl
- Upload date:
- Size: 114.3 kB
- Tags: CPython 3.13, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c876f74e2045f12ff66087802727e388aef4e7c50cdaf411b7f6e2bef3b69ab
|
|
| MD5 |
0bf39305e9f3ebc08a2df3f3cc75585d
|
|
| BLAKE2b-256 |
2b7ebf092e84a2a2dd864de640a0cbcae23675487536c358ad613fbdb2a14588
|
File details
Details for the file almeshopt-0.1.11-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.
File metadata
- Download URL: almeshopt-0.1.11-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
- Upload date:
- Size: 280.3 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd6caff1c2fc39000f70973d33d2274df0d48998558a88c420dd2b2d6b753b76
|
|
| MD5 |
469532cb8429d6d711b811d29c8ae0f3
|
|
| BLAKE2b-256 |
a304dd05e5913c3f67254823cf0365bb5b09b5191ab125b4fae920e9f9d52ff3
|
File details
Details for the file almeshopt-0.1.11-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: almeshopt-0.1.11-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 136.0 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f904b0f115ad2fafdbddab796efff6d925d0924ae79aded9b10989b974906f4
|
|
| MD5 |
e040e99a78bc2c6ed6b8ac14705d4e95
|
|
| BLAKE2b-256 |
7c82d76cb54db6a9dbbfc24d6ff62c3f3b70aa48b9d5e7f0e5993bd96cf98be2
|
File details
Details for the file almeshopt-0.1.11-cp312-cp312-win32.whl.
File metadata
- Download URL: almeshopt-0.1.11-cp312-cp312-win32.whl
- Upload date:
- Size: 114.4 kB
- Tags: CPython 3.12, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b03a3f1dabd7cf855f610b6946e5690ee4df3df9ef3407f6cee5371b60bc65ff
|
|
| MD5 |
fca781661bbe16b18f1f9ab63cac7e49
|
|
| BLAKE2b-256 |
d9105cbb517fb049d5ea15c3b1f8682437b089247619429b9083bb5545cd8d13
|
File details
Details for the file almeshopt-0.1.11-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.
File metadata
- Download URL: almeshopt-0.1.11-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
- Upload date:
- Size: 276.1 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2f696a5497828e9dfa134494e4b5ad7cf71ddb34ec8f0aac08c9a8d8b8b4a51
|
|
| MD5 |
66c08aaeea93634b453988e4fba1cabb
|
|
| BLAKE2b-256 |
c28e31fa8c5ae039c303eab3c7ff351938c2f6ed76badb275390b312605b5cc3
|
File details
Details for the file almeshopt-0.1.11-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: almeshopt-0.1.11-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 139.8 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43eb72e4c912a46301b1dd6fbf62a1fd2d998ac10fa8c69722da4c198ea16b22
|
|
| MD5 |
d0da17e5870c4fdb7d6c627d5fdd7398
|
|
| BLAKE2b-256 |
c0e3bc2e1cd9f8da3efb4c00999741a58058eed64d326ef795cf6336ed418ea2
|
File details
Details for the file almeshopt-0.1.11-cp311-cp311-win32.whl.
File metadata
- Download URL: almeshopt-0.1.11-cp311-cp311-win32.whl
- Upload date:
- Size: 116.2 kB
- Tags: CPython 3.11, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
452f9dc8cd11d26cd2a5d68efcea70174cdcdaf351452712f20b0eee26ef7ae8
|
|
| MD5 |
98495db403c40bc7c8a22a587f2b9766
|
|
| BLAKE2b-256 |
d4f2f00193fc2db36bd9576edc8ff304a2b546fde790410d8908254add920572
|
File details
Details for the file almeshopt-0.1.11-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.
File metadata
- Download URL: almeshopt-0.1.11-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
- Upload date:
- Size: 277.1 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
415a5d9d1823b667fc9867f8e30bd523eec15634ec3c60bdaf0b4b8bee9282cd
|
|
| MD5 |
3c98541b47b1bad3aef12f4a9a1af7fe
|
|
| BLAKE2b-256 |
d76854c53ec2a5181adcbf572f2c7bcba5a627961ca224af2faa36262fb75566
|