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

目录

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

效果:

 源码:


效果:

三角形

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

 矩形:

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

多边形:

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

 源码:

#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType)
#include <vtkSmartPointer.h>
#include <vtkNew.h>
#include <vtkUnstructuredGrid.h>
#include <vtkDataSetMapper.h>
#include <vtkPoints.h>
#include <vtkPolygon.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkTriangleFilter.h>
#include <vtkTriangle.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
 
 
vtkSmartPointer<vtkPolyData>  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<vtkPoints> points;
	vtkNew<vtkPolygon> 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<vtkCellArray> cells;
	cells->InsertNextCell(polygon);

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

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

vtkSmartPointer<vtkPolyData> 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<vtkPoints> points;
	vtkNew<vtkPolygon> 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<vtkCellArray> cells;
	cells->InsertNextCell(polygon);

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

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

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

}

vtkSmartPointer<vtkPolyData> 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<vtkPoints> points;
	vtkNew<vtkPolygon> 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<vtkCellArray> cells;
	cells->InsertNextCell(polygon);

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

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

}

int main()
{

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

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

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

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

	vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
	renderWindowInteractor->SetRenderWindow(renderWindow);

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