vtk 多边形绘制 vtkPolygon 三角形 矩形 多边形

  vtk 可以通过 vtkPolygon 绘制 三角形 矩形 多边形

目录

  vtk 可以通过 vtkPolygon 绘制 三角形 矩形 多边形

效果:

 源码:


效果:

三角形

vtk 多边形绘制 vtkPolygon 三角形 矩形 多边形_第1张图片

 矩形:

vtk 多边形绘制 vtkPolygon 三角形 矩形 多边形_第2张图片

多边形:

vtk 多边形绘制 vtkPolygon 三角形 矩形 多边形_第3张图片

 源码:

#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType)
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
 
vtkSmartPointer  GetTriangle()
{
	const int num_points = 3;
	double top_left[num_points][3] = { {1.0, 0.0, 0.0}, {0.0, 0.0, 0.0},{0.0, 1.0, 0.0}  };
	vtkNew points;
	vtkNew polygon;
	polygon->GetPointIds()->SetNumberOfIds(num_points);
	for (size_t i = 0; i < num_points; i++) {
		points->InsertNextPoint(top_left[i]);
		polygon->GetPointIds()->SetId(i, i);
	}

	vtkNew cells;
	cells->InsertNextCell(polygon);

	vtkNew polygonPolyData;
	polygonPolyData->SetPoints(points);
	polygonPolyData->SetPolys(cells);

	vtkNew filter;
	filter->SetInputData(polygonPolyData);
	filter->Update();
	vtkNew poly;
	poly->DeepCopy(filter->GetOutput());
	return poly;
}

vtkSmartPointer GetCubePolyData()
{
	const int num_points = 4;
	double top_left[num_points][3] = { {0.0, 0.0, 0.0}, {0.0, 1.0, 0.0},{1.0, 1.0, 0.0},{1.0, 0.0, 0.0} };
	vtkNew points;
	vtkNew polygon;
	polygon->GetPointIds()->SetNumberOfIds(num_points);
	for (size_t i = 0; i < num_points; i++) {
		points->InsertNextPoint(top_left[i]);
		polygon->GetPointIds()->SetId(i, i);
	}

	vtkNew cells;
	cells->InsertNextCell(polygon);

	vtkNew polygonPolyData;
	polygonPolyData->SetPoints(points);
	polygonPolyData->SetPolys(cells);

	//vtkNew filter;
	//filter->SetInputData(polygonPolyData);
	//filter->Update();

	vtkNew poly;
	poly->DeepCopy(polygonPolyData );
	return poly;

}

vtkSmartPointer GetPolygonPolyData()
{
	const int num_points = 5;
	double top_left[num_points][3] = { {0.5,0.5,0}, {1,0,0},{1,1,0},{0,1,0},{0,0,0} };
	vtkNew points;
	vtkNew polygon;
	polygon->GetPointIds()->SetNumberOfIds(num_points);
	for (size_t i = 0; i < num_points; i++) {
		points->InsertNextPoint(top_left[i]);
		polygon->GetPointIds()->SetId(i, i);
	}

	vtkNew cells;
	cells->InsertNextCell(polygon);

	vtkNew polygonPolyData;
	polygonPolyData->SetPoints(points);
	polygonPolyData->SetPolys(cells);

	vtkNew poly;
	poly->DeepCopy(polygonPolyData);
	return poly;

}

int main()
{

	vtkSmartPointer poly = GetPolygonPolyData();// GetCubePolyData();//GetTriangle();
	vtkNew mapper;
	mapper->SetInputData(poly);

	vtkNew actor;
	actor->SetMapper(mapper);
	// SetRepresentationToWireframe可以开启显示边框线
	actor->GetProperty()->SetRepresentationToWireframe();

	vtkNew renderer;
	renderer->AddActor(actor);
	renderer->SetBackground(0, 0, 0);

	vtkNew renderWindow;
	renderWindow->AddRenderer(renderer);
	renderWindow->SetSize(800, 800);
	renderWindow->SetWindowName("vtkTriangleFilter");

	vtkNew renderWindowInteractor;
	renderWindowInteractor->SetRenderWindow(renderWindow);

	renderWindow->Render();
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();
 
	return 0;
}

你可能感兴趣的:(VTK,VTK,Polygon,多边形)