#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