VTK对点进行采样

在VTK中对三维点进行采样,并保存采样后的结果,在同一个窗口显示采样前与采样后的点,采样前的点以红色显示,采样后的点以绿色显示

如果是二维点,则读取二维点坐标就是了

 

#include <iostream>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkCleanPolyData.h>
#include <vtkPolyDataWriter.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkCellArray.h>
#include <vtkInteractorStyleTrackball.h>
void main()
{
	vtkSmartPointer<vtkPoints>m_points=vtkSmartPointer<vtkPoints>::New();
	vtkSmartPointer<vtkCellArray>m_vertices=vtkSmartPointer<vtkCellArray>::New();
	vtkSmartPointer<vtkRenderer>ren1=vtkSmartPointer<vtkRenderer>::New();
	vtkSmartPointer<vtkRenderWindow>renWin=vtkSmartPointer<vtkRenderWindow>::New();
	vtkSmartPointer<vtkRenderWindowInteractor>iren=vtkSmartPointer<vtkRenderWindowInteractor>::New();
	FILE *fp=NULL;
	fp=fopen("右心室浮点型.txt","r");
	if (!fp)
	{
		cout<<"打开文件失败"<<endl;
	}
	double x=0,y=0,z=0;
	int i=0;
	while(!feof(fp))
	{
		fscanf(fp,"%lf	%lf	%lf",&x,&y,&z);
		m_points->InsertPoint(i,x,y,z);
		m_vertices->InsertNextCell(1);
		m_vertices->InsertCellPoint(i);
		i++;
	}
	fclose(fp);
	vtkSmartPointer<vtkPolyData>m_polydata=vtkSmartPointer<vtkPolyData>::New();
	m_polydata->SetPoints(m_points);
	m_polydata->SetVerts(m_vertices);

	vtkSmartPointer<vtkCleanPolyData>m_cleanpoint=vtkSmartPointer<vtkCleanPolyData>::New();
	m_cleanpoint->SetInput(m_polydata);
	m_cleanpoint->SetTolerance(0.05);        //设置采样度
	m_cleanpoint->Update();

	vtkSmartPointer<vtkPolyDataWriter>pointcaiyang=vtkSmartPointer<vtkPolyDataWriter>::New();
	pointcaiyang->SetFileName("采样后的点.vtp");    //保存采样后的结果
	pointcaiyang->SetInput(m_cleanpoint->GetOutput());
	pointcaiyang->Write();

	vtkSmartPointer<vtkPolyDataMapper>caiyangqian=vtkSmartPointer<vtkPolyDataMapper>::New();
	caiyangqian->SetInputConnection(m_polydata->GetProducerPort());

	vtkSmartPointer<vtkPolyDataMapper>caiyanghou=vtkSmartPointer<vtkPolyDataMapper>::New();
	caiyanghou->SetInputConnection(m_cleanpoint->GetOutputPort());

	vtkSmartPointer<vtkActor>actor_qian=vtkSmartPointer<vtkActor>::New();
	actor_qian->SetMapper(caiyangqian);
	actor_qian->GetProperty()->SetColor(1,0,0);
//	actor_qian->GetProperty()->SetPointSize(2);

	vtkSmartPointer<vtkActor>actor_hou=vtkSmartPointer<vtkActor>::New();
	actor_hou->SetMapper(caiyanghou);
	actor_hou->GetProperty()->SetColor(0,1,0);
	actor_hou->GetProperty()->SetPointSize(4);

	ren1->AddActor(actor_qian);
	ren1->AddActor(actor_hou);

	ren1->SetBackground(1,1,1);
	renWin->AddRenderer(ren1);
	renWin->SetSize(600,600);
	iren->SetRenderWindow(renWin);
	renWin->Render();
	renWin->Start();
	getchar();
}


 

如果您觉得这篇博文有用,请访问我的个人站:http://www.stubbornhuang.com,更多博文干货等着您。

 

HW140701 CSDN认证博客专家 CSDN博客砖家
拥有C/C++超过5年开发经验。C/C艹、计算机图形学、三维重建、图形渲染等一生之敌。高中是文科生,本科、研究生是理科生,有机会读博士我想选工科!博客基本上都是写给自己备完的,不喜勿喷。
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页