PCL与VTK学习记录二: vtkVertexGlyphFilter顶点与vtkElevationFilter着色

#include "pch.h"
#include <iostream>
#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); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
 
int main()
{
    std::string fliename = "./pointCloud.txt";
    std::ifstream ifs(fliename.c_str());
    std::string singleLine;
    //double x, y, z, low_z = DBL_MAX, high_z = DBL_MIN;
    double x, y, z, low_z = 6635, high_z = -1100;
    double max_value = 0, min_value = 1000;
    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    //分行读取文件
    while (std::getline(ifs, singleLine))
    {
        std::stringstream linestream;
        linestream << singleLine;
        linestream >> x >> y >> z;
        if (z > max_value)
            max_value = z;
        if (z < min_value)
            min_value = z;
        if (z < low_z)
            low_z = z;
        else if (z > high_z)
            high_z = z;
        points->InsertNextPoint(x, y, z);
    }
    ifs.close();
    //计算Z轴最大值最小值,方便下次设置low_z, high_z以得到更好的颜色对应高度信息
    cout << "max = " << max_value << endl;
    cout << "min = " << min_value << endl;
 
    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;
}
————————————————

                            本文为Color Space原创,未经允许禁止转载!博客地址:https://blog.csdn.net/stq054188?spm=3001.5343
                        
原文链接:https://blog.csdn.net/stq054188/article/details/108797263

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值