VTK实时变化刷新,动态点显示

VTK实时变化刷新,动态点显示

最近使用C++代码读取机床的一些数据,想要实时显示点的变化,于是尝试用VTK作动态显示,结果发现通用的显示过程(point->actor->mapper->rander->randerwindow)直接改变point,再调用

randerwinder->rander();

并不能达到效果,在网上找了很久,没有一个有用的,感叹为什么这么简单的问题为什么搜出来的全是些来回抄的且没有用的东西。
自己写了一个简单的实现,方法比较笨,由于直接调用rander()没有效果,干脆每次都重新new一下整个链,反正能达到视觉效果,代码如下


#include 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkPolyDataMapper.h"
#include "vtkInteractorStyle.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkCellArray.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkCamera.h"

#include 
#include 
#include 
using namespace std;


int main()
{

	double camera_pos[3] = { 0,0,1 };
	double camera_foc[3] = { 0,0,0 };
	double camera_ang = 120;

	double point_path[12][2] = { {0,0},{0.3,0.5},{0.6,0.8},{1.,1.},{1.4,0.8},{1.7,0.5},
	{2.,0.},{1.7,-0.5},{1.4,-0.8},{1,-1.},{0.7,-0.8},{0.3,-0.5} };

	vtkRenderWindow *renderWindow = vtkRenderWindow::New();

	for (int i = 0; i < 100; i++) {

		vtkIdType idtype;
		double x, y, z;

		vtkPoints *points = vtkPoints::New();
		vtkCellArray *cells = vtkCellArray::New();

		//可以自定义数据来源,例如从传感器采集,从标准输入读入,文件读入等
		//string line;
		//string num;
		//cin >> line;
		//if (line.size() == 1)break;
		//stringstream ss(line);
		//getline(ss, num, ','); x = atof(num.c_str());
		//getline(ss, num, ','); y = atof(num.c_str());
		//getline(ss, num, ','); z = atof(num.c_str());

		x = point_path[i % 12][0] - 0.02*i;
		y = point_path[i%12][1];
		z = 0;
		idtype = points->InsertNextPoint(x, y, z);
		cells->InsertNextCell(1, &idtype);

		vtkPolyData *polyData = vtkPolyData::New();
		polyData->SetPoints(points);
		polyData->SetVerts(cells);
		vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
		mapper->SetInputData(polyData);
		vtkActor *actor = vtkActor::New();
		actor->SetMapper(mapper);
		actor->GetProperty()->SetColor(0.0, 1.0, 0.0);
		actor->GetProperty()->SetPointSize(3);
		vtkRenderer *renderer = vtkRenderer::New();
		renderer->AddActor(actor);
		renderer->GetActiveCamera()->SetPosition(camera_pos);
		renderer->GetActiveCamera()->SetFocalPoint(camera_foc);
		renderer->GetActiveCamera()->SetViewAngle(camera_ang);

		renderWindow->AddRenderer(renderer);
		renderWindow->Render();
		Sleep(50);


		cells->Delete();
		points->Delete();
		polyData->Delete();
		mapper->Delete();
		actor->Delete();
		renderer->Delete();
	}

	renderWindow->Delete();

	return 0;
}

执行效果是一个运动的点,方法粗略,希望有懂vtk的教一下有没有好点的办法,不胜感激。

你可能感兴趣的:(VTK,实时,动点,c++,实时音视频)