初始化:RMUL 2027 哨兵导航仿真工程
- Gazebo Sim (Jetty 10.6) + ROS 2 Lyrical + Nav2 全栈 - 整车模型:URDF/SDF + 减面网格(SolidWorks 装配体实测尺寸) - 场地:RMUL 2027 官方包(2D 栅格地图 + 3D 场地模型),放在 field/ - sentry_nav C++ 包:self_filter 点云自滤 / patrol 航点巡逻 - 文档:README(含本机开发环境实测)、MID-360 接入说明 已排除:build/ install/ log/ 缓存目录,以及 115 MB 的原始高模 (仿真实际用的是 meshes/base.stl 减面版,约 25.7 万面)
This commit is contained in:
1 parent
ed31ccc39f
commit
f7412ec369
142 files changed
+58958
-2
No files matched your search
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env python3
|
||||
"""STL 顶点聚类减面(不依赖 MeshLab/Blender,纯 numpy)。
|
||||
|
||||
原理:把顶点量化到边长 h 的网格,用网格中心代替原顶点,丢弃退化三角形。
|
||||
对「视觉用」网格足够:形状保持,面数可降一个数量级。
|
||||
|
||||
用法:
|
||||
python3 tools/decimate.py 输入.stl 输出.stl --grid 0.002
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import struct
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
def read_stl(path):
|
||||
size = os.path.getsize(path)
|
||||
with open(path, 'rb') as f:
|
||||
head = f.read(84)
|
||||
n = struct.unpack('<I', head[80:84])[0]
|
||||
if 84 + n * 50 != size:
|
||||
raise ValueError(f'{path} 不是二进制 STL')
|
||||
f.seek(84)
|
||||
buf = f.read(n * 50)
|
||||
rec = np.frombuffer(buf, dtype=np.dtype([('n', '<3f4'),
|
||||
('v', '<9f4'),
|
||||
('a', '<u2')]))
|
||||
return rec['v'].reshape(-1, 3, 3).astype(np.float32)
|
||||
|
||||
|
||||
def write_stl(path, tris):
|
||||
n = len(tris)
|
||||
v0, v1, v2 = tris[:, 0], tris[:, 1], tris[:, 2]
|
||||
normals = np.cross(v1 - v0, v2 - v0)
|
||||
leng = np.linalg.norm(normals, axis=1)
|
||||
leng[leng == 0] = 1.0
|
||||
normals = normals / leng[:, None]
|
||||
rec = np.zeros(n, dtype=np.dtype([('n', '<3f4'), ('v', '<9f4'), ('a', '<u2')]))
|
||||
rec['n'] = normals
|
||||
rec['v'] = tris.reshape(n, 9)
|
||||
with open(path, 'wb') as f:
|
||||
f.write(b'sentry_sim decimated'.ljust(80, b' '))
|
||||
f.write(struct.pack('<I', n))
|
||||
f.write(rec.tobytes())
|
||||
|
||||
|
||||
def decimate(tris, grid):
|
||||
verts = tris.reshape(-1, 3)
|
||||
cells = np.floor(verts / grid).astype(np.int64)
|
||||
# 每个网格单元 -> 该单元内顶点的平均值
|
||||
_, inverse = np.unique(cells, axis=0, return_inverse=True)
|
||||
inverse = inverse.reshape(-1)
|
||||
ncell = inverse.max() + 1
|
||||
sums = np.zeros((ncell, 3), dtype=np.float64)
|
||||
np.add.at(sums, inverse, verts)
|
||||
counts = np.bincount(inverse, minlength=ncell)[:, None]
|
||||
centers = (sums / counts).astype(np.float32)
|
||||
|
||||
faces = inverse.reshape(-1, 3)
|
||||
# 去掉退化面(三个顶点落在同一/两个单元)
|
||||
keep = ((faces[:, 0] != faces[:, 1]) &
|
||||
(faces[:, 1] != faces[:, 2]) &
|
||||
(faces[:, 0] != faces[:, 2]))
|
||||
faces = faces[keep]
|
||||
return centers[faces]
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument('src')
|
||||
ap.add_argument('dst')
|
||||
ap.add_argument('--grid', type=float, default=0.002,
|
||||
help='网格边长(米),越大越省面、越粗糙')
|
||||
args = ap.parse_args()
|
||||
|
||||
tris = read_stl(args.src)
|
||||
print(f'输入 {len(tris):,} 面')
|
||||
out = decimate(tris, args.grid)
|
||||
print(f'输出 {len(out):,} 面(网格 {args.grid*1000:.1f} mm,压缩到 {len(out)/len(tris)*100:.1f}%)')
|
||||
write_stl(args.dst, out)
|
||||
print(f'已写入 {args.dst}({os.path.getsize(args.dst)/1e6:.1f} MB)')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in new issue
Block a user