VTK 学习笔记一

https://www.cnblogs.com/yangai/p/5955614.html

VTK笔记-了解VTK_黑山老妖的笔记本的博客-CSDN博客

一、VTK一般流程

 1、source 数据源

VTK基础教程(1)- vtkPolyData 介绍_TGTSTTG的博客-CSDN博客

vtkSource 数据源,个人理解的就是故事灵感,来自于古希腊、古罗马、生活中的琐事,常见的有vtkPointSource  vtkCylinderSource

2、mapper 映射器

vtkMapper 就是将将上面的故事灵感 现演绎,变成故事完整话之后重新演绎。

vtkPolyDataMapper

3、actor 演员

vtkActor 就是一个演员,我说的是有演技的老艺术家,不是流量白马会里面的那些人。故事需要人来演绎呈现给观众

4、vtkRenderer 渲染

vtkRenderer 就是当有演员之后你要给演员化妆,营造舞台的氛围,就像一个古装戏,你穿着西装,37分发胶铮亮,纹丝不乱,总感觉让人很出戏。

该类继承于vtkViewport,有2个子类:vtkMesaRender,vtkOpenGLRender.

5、window 窗口

vtkRenderWindow 就是一个舞台,话剧来说就是剧场。
该类继承于vtkWindow,有2个子类:vtkMesaWindow,vtkOpenGLWindow.

6、vtkRenderWindowInteractor(窗口交互器)

我觉得这个应该是导演,用于指挥调度演员和场景的切换,比如控制镜头的推出和拉近,那些地方要给个特写,是一个操控舞台的上帝之手

7、vtkInteractorObserver(观察者) :

我觉得这里应该是观众席上的观众们,每个不同的观众关注的细节,关注的场景都不一样,有的观众或会镜头旋转时做一些记录,有的观众可能在看表演时带上偏光镜。

二、第一个vtk 程序


int vtk_Creater_Sphere_Show()
{
	vtkNew colors;
	// 设置背景颜色
	std::array bkg{ { 26, 51, 102, 255 } };
	colors->SetColor("BkgColor", bkg.data());


     // 1、创建一个
	vtkNew sphereSource;
	sphereSource->SetThetaResolution(30);
	sphereSource->SetPhiResolution(15);
	sphereSource->Update();

	vtkSmartPointer sphereData;
	sphereData = sphereSource->GetOutput();

	// 2 、做映射, 将灵感生成剧本
	vtkNew shpereMapper;
	shpereMapper->SetInputData(sphereData);

	// 这种方法也是可以的
	vtkNew shpereMapper2;
	shpereMapper2->SetInputConnection(sphereSource->GetOutputPort());


	// 3、演员
	vtkNew shpereActor;
	shpereActor->SetMapper(shpereMapper.GetPointer());  // 指针
	// 设置球的颜色
	shpereActor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData());
	shpereActor->RotateX(30.0);
	shpereActor->RotateY(-40.0);

	// 4 render  渲染氛围
	vtkNew shpereRender;
	shpereRender->AddActor(shpereActor.GetPointer());
	// 设置背景色 
	shpereRender->SetBackground(colors->GetColor3d("BkgColor").GetData());

	shpereRender->ResetCamera();
	shpereRender->GetActiveCamera()->Zoom(1.5);

	 //  5、窗口
	vtkNew window;
	window->SetSize(500,500);
	window->AddRenderer(shpereRender.GetPointer());
	window->SetWindowName("MyFirstVtk");

	// 6 、定义一个鼠标拖动的事件 
	vtkNew mouseInteractor;
	mouseInteractor->SetRenderWindow(window.GetPointer());


    // 启动
	window->Render();
	mouseInteractor->Start();

	return 0;
}

VTK 学习笔记一_第1张图片

vtk 显示点云


int  Test_vtk_PointCloud()
{

	// 1 设置点云数据
	vtkNew colors;

	vtkNew pointSource;
	pointSource->SetNumberOfPoints(50000);
	pointSource->SetRadius(10.0);
	pointSource->Update();
	 // 点云的点数
	auto pts = pointSource->GetNumberOfPoints();

	// 设置第二个窗口的数据
	vtkNew cleanPolyData;
	cleanPolyData->SetInputConnection(pointSource->GetOutputPort());

	cleanPolyData->SetTolerance(0.2);
	cleanPolyData->Update();
	// 第二个窗口 点云的点数
	auto cleanPts = cleanPolyData->GetOutput()->GetNumberOfPoints();

	std::cout << "Original points" << pts << std::endl;
	std::cout << "Cleaned points " << cleanPts << std::endl;
	std::cout << "Reduction      "	<< (1.0 - static_cast(cleanPts) / pts) * 100.0 << "%"<< std::endl;


	// 原始点云  ------------------------------------------------------------
	vtkNew inputMapper;
	inputMapper->SetInputConnection(pointSource->GetOutputPort());
	 //  原始点云的actor -----
	vtkNew inputActor;
	inputActor->SetMapper(inputMapper.GetPointer());
	inputActor->GetProperty()->SetColor(colors->GetColor3d("Lime").GetData());
	inputActor->GetProperty()->SetPointSize(3);
	//==========================================================


	// 第二个窗口的点云  ------------------------------------------------------------
	vtkNew cleanedMapper;
	cleanedMapper->SetInputConnection(cleanPolyData->GetOutputPort());

	vtkNew cleanedActor;
	cleanedActor->SetMapper(cleanedMapper.GetPointer());
	cleanedActor->GetProperty()->SetColor(colors->GetColor3d("Lime").GetData());
	cleanedActor->GetProperty()->SetPointSize(3);
	 //============================================================



	//   定义一个窗口
	vtkNew renderWindow;
	renderWindow->SetSize(800, 400);
	renderWindow->SetWindowName("Down sample_PointCloud");

	//  一个导演  2个演员
	vtkNew interactor;
	interactor->SetRenderWindow(renderWindow.GetPointer());

	//  定义原始点云和 下采样点云的窗口位置
	double leftViewport[4] = { 0.0, 0.0, 0.5, 1.0 };
	double rightViewport[4] = { 0.5, 0.0, 1.0, 1.0 };

	//  原始电源的渲染
	vtkNew leftRenderer;
	renderWindow->AddRenderer(leftRenderer.GetPointer());
	leftRenderer->SetViewport(leftViewport);
	leftRenderer->SetBackground(colors->GetColor3d("van_dyke_brown").GetData());

	// 下采样点云的渲染
	vtkNew rightRenderer;
	renderWindow->AddRenderer(rightRenderer.GetPointer());
	rightRenderer->SetViewport(rightViewport);
	rightRenderer->SetBackground(colors->GetColor3d("ultramarine").GetData());

	leftRenderer->AddActor(inputActor.GetPointer());   // 原始点云
	rightRenderer->AddActor(cleanedActor.GetPointer());  // 下采样点云



	leftRenderer->ResetCamera();
	rightRenderer->ResetCamera();


	  // 一个窗口  启动
	renderWindow->Render();
	interactor->Start();

	return 0;
}

VTK 学习笔记一_第2张图片

你可能感兴趣的:(3D,C++,vtk,3d,算法)