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

#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/io/vtk_io.h>
#include <pcl/io/vtk_lib_io.h>
#include <pcl/search/kdtree.h>
#include <pcl/features/normal_3d.h>
#include <pcl/surface/gp3.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>
#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();

	// 展示stl
	boost::shared_ptr<visualization::PCLVisualizer> stl_viewer(new visualization::PCLVisualizer("STL Viewer"));
	stl_viewer->addModelFromPolyData(polydata, "mesh");
	// 创建新的线程显示STL窗口
	boost::thread stl_thread([&stl_viewer]() {
		while (!stl_viewer->wasStopped()) {
			stl_viewer->spinOnce(100);
			boost::this_thread::sleep(boost::posix_time::microseconds(100000));
		}
		});

	// 保存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

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数据格式展示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值