STL与PLY格式转化

一、STL转化为PLY

利用PCL库中vtk_lib_io实现,#include <pcl/io/vtk_lib_io.h>,C++语言。

提供一个用于测试的数据:

通过网盘分享的文件:ply_stl

链接: https://pan.baidu.com/s/1xnO5s2kiUf0Cs35XVyfTHA?pwd=xmax 提取码: xmax

cpp 复制代码
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/vtk_lib_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <vector>
#include <iostream>

using namespace pcl;
using namespace std;

int main()
{
	// 读取STL文件
	vtkSmartPointer<vtkSTLReader> reader4 = vtkSmartPointer<vtkSTLReader>::New();
	reader4->SetFileName("mesh.stl");
	reader4->Update();

	// stl 格式 转出到 ply 格式
	vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
	polydata = reader4->GetOutput();
	polydata->GetNumberOfPoints();

	// 保存ply数据
	vtkSmartPointer<vtkPLYWriter> plyWriter = vtkSmartPointer<vtkPLYWriter>::New();
	plyWriter->SetFileName("stl2plyData.ply");
	plyWriter->SetInputConnection(reader4->GetOutputPort());
	plyWriter->SetFileTypeToASCII();
	plyWriter->SetColorModeToOff();
	plyWriter->Update();
	plyWriter->Write();

	// 从 ply 格式 转为 pcd 格式
	PointCloud<PointXYZ>::Ptr cloudPcd(new PointCloud<PointXYZ>());
	io::vtkPolyDataToPointCloud(polydata, *cloudPcd);

	// 保存pcd数据
	io::savePCDFileASCII("ply2PcdData.pcd", *cloudPcd);

	visualization::PCLVisualizer vis("cloud visualization");
	vis.getRenderWindow()->GlobalWarningDisplayOff();
	vis.addCoordinateSystem(5.0);
	vis.addPointCloud(cloudPcd);
	while (!vis.wasStopped())
	{
		vis.spinOnce();
	}

	return 0;
}

二、PLY转化为STL

利用python语言,需要安装numpy-stl安装包。

提供一个用于测试的数据:

通过网盘分享的文件:ply_stl

链接: https://pan.baidu.com/s/1xnO5s2kiUf0Cs35XVyfTHA?pwd=xmax 提取码: xmax

python 复制代码
import numpy as np
from stl import mesh


def read_ply(file_path):
    with open(file_path, 'rb') as f:
        header = f.readline()
        while not header.startswith(b'element vertex'):
            header = f.readline()
        vertex_count = int(header.split()[-1])
        print("vertex_count = ", vertex_count)

        while not header.startswith(b'element face'):
            header = f.readline()
        face_count = int(header.split()[-1])
        print("face_count = ", face_count)

        while not header.startswith(b'end_header'):
            header = f.readline()

        # 读取顶点数据
        vertices = []
        for i in range(vertex_count):
            header = f.readline()
            header = header.split()[:3]
            for j in range(len(header)):
                vertices.append(float(header[j]))
        vertices = np.array(vertices).reshape(-1, 3)

        # 读取面数据
        faces = []
        for i in range(face_count):
            header = f.readline()
            header = header.split()[1:]
            for j in range(len(header)):
                faces.append(int(header[j]))
        faces = np.array(faces).reshape(-1, 3)

        return vertices, faces


def create_stl(vertices, faces, output_file):
    cube = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
    for i, f in enumerate(faces):
        for j in range(3):
            cube.vectors[i][j] = vertices[f[j], :]
    # save stl file
    cube.save(output_file)


if __name__ == '__main__':
    vertice, face = read_ply('mesh.ply')
    print(vertice.shape)
    print(face.shape)

    create_stl(vertice, face, 'mesh.stl')

ply数据格式展示

相关推荐
源码之家23 分钟前
计算机毕业设计:Python汽车销量数据采集分析可视化系统 Flask框架 requests爬虫 可视化 车辆 大数据 机器学习 hadoop(建议收藏)✅
大数据·爬虫·python·django·flask·课程设计·美食
计算机安禾27 分钟前
【数据结构与算法】第16篇:串(String)的定长顺序存储与朴素模式匹配
c语言·数据结构·c++·学习·算法·visual studio code·visual studio
Roselind_Yi28 分钟前
【吴恩达2026 Agentic AI】面试向+项目实战(含面试题+项目案例)-2
人工智能·python·机器学习·面试·职场和发展·langchain·agent
2401_8274999928 分钟前
python核心语法01-数据存储与运算
java·数据结构·python
一直会游泳的小猫29 分钟前
ClaudeCode完整学习指南
python·ai编程·claude code·claude code指南
第一程序员30 分钟前
Python与容器化:Docker和Kubernetes实战
python·github
JaydenAI36 分钟前
[RAG在LangChain中的实现-04]常用的向量存储和基于向量存储的检索器
python·langchain·ai编程
Roselind_Yi38 分钟前
【吴恩达2026 Agentic AI】面试向+项目实战(含面试题+项目案例)-1
人工智能·python·面试·职场和发展·langchain·gpt-3·agent
·心猿意码·1 小时前
C++ 链接陷阱与底层溯源:ODR、inline 与匿名命名空间的那些坑
c++
Alan GEO实施教练1 小时前
专利申请是否找代理机构:核心考量与决策逻辑拆解
大数据·人工智能·python