vtk單視窗顯示多個源資料程式碼

huffscan發表於2017-10-08
	std::vector<vtkSmartPointer<vtkPolyDataAlgorithm> > geometricObjectSources;

	geometricObjectSources.push_back(vtkSmartPointer<vtkArrowSource>::New());//箭頭
	geometricObjectSources.push_back(vtkSmartPointer<vtkConeSource>::New());//錐體
	geometricObjectSources.push_back(vtkSmartPointer<vtkCubeSource>::New());//立方體
	geometricObjectSources.push_back(vtkSmartPointer<vtkCylinderSource>::New());//缸體
	geometricObjectSources.push_back(vtkSmartPointer<vtkDiskSource>::New());//轉盤
	geometricObjectSources.push_back(vtkSmartPointer<vtkLineSource>::New());//直線
	geometricObjectSources.push_back(vtkSmartPointer<vtkRegularPolygonSource>::New());//正多面體
	geometricObjectSources.push_back(vtkSmartPointer<vtkSphereSource>::New());//球體

	std::vector<vtkSmartPointer<vtkRenderer> > renderers;
	std::vector<vtkSmartPointer<vtkPolyDataMapper> > mappers;
	std::vector<vtkSmartPointer<vtkActor> > actors;
	std::vector<vtkSmartPointer<vtkTextMapper> > textmappers;
	std::vector<vtkSmartPointer<vtkActor2D> > textactors;

	for (unsigned int i = 0; i < geometricObjectSources.size(); i++)
	{
		geometricObjectSources[i]->Update();

		mappers.push_back(vtkSmartPointer<vtkPolyDataMapper>::New());
		mappers[i]->SetInputConnection(geometricObjectSources[i]->GetOutputPort());

		actors.push_back(vtkSmartPointer<vtkActor>::New());
		actors[i]->SetMapper(mappers[i]);

		textmappers.push_back(vtkSmartPointer<vtkTextMapper>::New());
		textmappers[i]->SetInput(geometricObjectSources[i]->GetClassName());

		textactors.push_back(vtkSmartPointer<vtkActor2D>::New());
		textactors[i]->SetMapper(textmappers[i]);
		textactors[i]->SetPosition(10, 10);
	}

	// 設定格網承載形體
	int gridCols = 3;
	int gridRows = 3;
	// 定義渲染範圍
	int rendererSize = 200;

	vtkSmartPointer<vtkRenderWindow> renderWindow =
		vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow->SetSize(rendererSize*gridCols, rendererSize*gridRows);

	vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();

	for (double row = 0; row < gridRows; row++)
	{
		for (double col = 0; col < gridCols; col++)
		{
			double index = row*gridCols + col;

			//為每個cell定義渲染器
			vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
			renderer->SetBackground(.1, .2, .3);
			double viewport[4] = {
				((col)* rendererSize) / (gridCols * rendererSize),
				((gridRows - (row + 1)) * rendererSize) / (gridRows * rendererSize),
				((col + 1)              * rendererSize) / (gridCols * rendererSize),
				((gridRows - (row)) * rendererSize) / (gridRows * rendererSize) };
			renderer->SetViewport(viewport);
			if (index < geometricObjectSources.size())
			{
				renderer->AddActor(actors[index]);
				renderer->AddActor(textactors[index]);
			}
			renderWindow->AddRenderer(renderer);
		}
	}

	vtkSmartPointer<vtkRenderWindowInteractor> interactor =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	interactor->SetRenderWindow(renderWindow);
	renderWindow->Render();
	interactor->Start();





相關文章