VTK根据三维坐标点集生成点云
阅读原文时间:2023年07月08日阅读:1

一个简单的利用VTK根据三维坐标点集生成点云的例子,仅供参考。

一、环境:vtk-8.1 & vs2013(需自行配置vtk的环境)

二、我所读取的三维坐标点集为txt格式文件,每个点的x,y,z坐标为单独一行。至于读取方法可以根据自己的点集数据结构重写读取方法,源码如下:

#include <fstream>
#include <sstream>
#include <string>
#include <vtkPoints.h>
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkVertexGlyphFilter.h>
#include <vtkElevationFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkAutoInit.h>

VTK_MODULE_INIT(vtkRenderingOpenGL2); //vs和cmake的编译选项不一致,非cmake生成的工程需添加,否则运行报错

int main()
{

    std::string fliename = "../bin/config/pointcloud.txt";
    std::ifstream ifs(fliename.c_str());
    std::string singleLine;
    double x, y, z,low_z = DBL_MAX,high_z = DBL_MIN;

    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//分行读取文件
    while (std::getline(ifs,singleLine))
    {
        std::stringstream linestream;
        linestream << singleLine;
        linestream >> x >> y >> z;
        if (z < low_z)
            low_z = z;
        else if (z > high_z)
            high_z = z;

        points->InsertNextPoint(x, y, z);
    }
    ifs.close();

    vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
    polydata->SetPoints(points);

    vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
    glyphFilter->SetInputData(polydata);
    glyphFilter->Update();

    vtkElevationFilter *elevationFilter = vtkElevationFilter::New();
    elevationFilter->SetInputConnection(glyphFilter->GetOutputPort());
    elevationFilter->SetLowPoint(0, 0, low_z);
    elevationFilter->SetHighPoint(0, 0, high_z);

    vtkPolyDataMapper *dataMapper = vtkPolyDataMapper::New();
    dataMapper->SetInputConnection(elevationFilter->GetOutputPort());

    vtkActor *actor = vtkActor::New();
    actor->SetMapper(dataMapper);

    vtkRenderer *renderer = vtkRenderer::New();
    renderer->AddActor(actor);
    renderer->SetBackground(.0, .0, .0);//设置背景色

    vtkRenderWindow *renderwind = vtkRenderWindow::New();
    renderwind->AddRenderer(renderer);

    vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New();

    vtkRenderWindowInteractor *renderwindIt = vtkRenderWindowInteractor::New();
    renderwindIt->SetRenderWindow(renderwind);
    renderwindIt->SetInteractorStyle(style);

    renderwind->Render();
    renderwindIt->Start();

    return 0;
}

下面附一张读取的部分点云图:

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章