VTK study notes one

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

VTK Notes-Understanding VTK_Black Mountain Laoyao's Notebook Blog-CSDN Blog

1. VTK general process

 1. source data source

VTK Basic Tutorial (1) - Introduction to vtkPolyData_TGTSTTG's Blog-CSDN Blog

vtkSource data source, personal understanding is the inspiration of the story, from ancient Greece, ancient Rome, and trivial things in life, the common ones are vtkPointSource vtkCylinderSource

2. mapper mapper

vtkMapper is to reinterpret the inspiration of the above story and reinterpret it after turning it into a complete story.

vtkPolyDataMapper

3. actor actor

vtkActor is an actor. I’m talking about old artists with acting skills, not those in the traffic white horse club. The story needs people to interpret and present to the audience

4. vtkRenderer rendering

vtkRenderer means that when there are actors, you have to make up the actors to create a stage atmosphere, just like a costume play, you wear a suit, 37 cents of glue is shining, and the threads are not messy, which always makes people feel very good.

This class inherits from vtkViewport and has 2 subclasses: vtkMesaRender, vtkOpenGLRender.

5. window window

vtkRenderWindow is a stage, and a drama is a theater.
This class inherits from vtkWindow and has 2 subclasses: vtkMesaWindow, vtkOpenGLWindow.

6. vtkRenderWindowInteractor (window interactor)

I think this should be the director, who is used to command and schedule the switching of actors and scenes, such as controlling the roll-out and zoom-in of the camera. Those places need to be close-up, which is the hand of God who controls the stage.

7. vtkInteractorObserver (observer):

I think this should be the audience in the auditorium. Each audience pays attention to details and scenes are different. Some audiences may make some records when the camera rotates, and some audiences may bring them when watching the performance. Polarizer.

2. The first vtk program


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


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

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

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

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


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

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

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

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

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


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

	return 0;
}

vtk display point cloud


int  Test_vtk_PointCloud()
{

	// 1 设置点云数据
	vtkNew<vtkNamedColors> colors;

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

	// 设置第二个窗口的数据
	vtkNew<vtkCleanPolyData> 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<double>(cleanPts) / pts) * 100.0 << "%"<< std::endl;


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


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

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



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

	//  一个导演  2个演员
	vtkNew<vtkRenderWindowInteractor> 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<vtkRenderer> leftRenderer;
	renderWindow->AddRenderer(leftRenderer.GetPointer());
	leftRenderer->SetViewport(leftViewport);
	leftRenderer->SetBackground(colors->GetColor3d("van_dyke_brown").GetData());

	// 下采样点云的渲染
	vtkNew<vtkRenderer> 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;
}

Guess you like

Origin blog.csdn.net/weixin_39354845/article/details/131579008