VTK 中画直线的方法汇总(二) C++ code

VTK中画直线有时候可通过vtkLineSource 或 vtkPolyLine

(二) vtkLineSource画直线 

   vtkLineSource直接通过设置直线的两个端点(point1和point2)来确定一条直线。

//method 2 vtkLineSource
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int, char *[]) {

	//基于三维空间中两点画直线
	double p0[3] = { 100.0, 100.0, 0.0 };
	double p1[3] = { 300.0, 300.0, 0.0 };

	vtkSmartPointer lineSource =
		vtkSmartPointer::New();
	lineSource->SetPoint1(p0);
	lineSource->SetPoint2(p1);
	lineSource->Update();

	vtkSmartPointer mapper =
		vtkSmartPointer::New();
	mapper->SetInputConnection(lineSource->GetOutputPort());

	vtkSmartPointer lineActor =
		vtkSmartPointer::New();
	lineActor->SetMapper(mapper);
	lineActor->GetProperty()->SetColor(1, 0, 0);
	lineActor->GetProperty()->SetLineWidth(3);

	vtkSmartPointer renderer =
		vtkSmartPointer::New();

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(renderer);

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	renderWindowInteractor->SetRenderWindow(renderWindow);

	renderer->AddActor(lineActor);
	renderWindow->Render();

	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

结果:

VTK 中画直线的方法汇总(二) C++ code_第1张图片 vtkLineSource

(三) vtkPolyLine画直线(直线或多边形或无闭合图形)

vtkPolyLine画直线的方法与vtkLine相似。区别在于vtkPolyLine常用于画多条线(集合),而vtkLine则用于画单条线。

//method 3  vtkpolyline
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include

int main(int, char *[]) {
	基于三维空间中四点画三角形
	double p0[3] = { 100.0, 100.0, 100 };
	double p1[3] = { 100.0, 200.0, 0.0 };
	double p2[3] = { 300.0, 500.0, 0.0 };
	double p3[3] = { 300.0, 300.0, 20 };
	vtkSmartPointer pts =
		vtkSmartPointer::New();
	pts->InsertNextPoint(p0);//ID = 0;
	pts->InsertNextPoint(p1);//ID = 1;
	pts->InsertNextPoint(p2);//ID = 2;
	pts->InsertNextPoint(p3);//ID = 3;

   //基于vtkpolyline画三角形
	vtkSmartPointer polyLine =
		vtkSmartPointer::New();
	polyLine->GetPointIds()->SetNumberOfIds(4);//画直线时将参数改为2
	for (unsigned int i = 0; i < 4; i++)//画直线时将参数改为2
	{
		polyLine->GetPointIds()->SetId(i, i);
	}
	polyLine->GetPointIds()->SetId(3, 0);//画直线或不闭合图形时省去

	vtkSmartPointer lines =
		vtkSmartPointer::New();
	lines->InsertNextCell(polyLine);

	// create the visualized line
	vtkSmartPointer linesPolyData =
		vtkSmartPointer::New();
	linesPolyData->SetPoints(pts);
	//点数据(pts)所定义的一系列点构成数据集(vtkDataSet)的几何结构
	linesPolyData->SetLines(lines);
	//点数据的连接形式(lines)形成的单元数据(cell Data)构成数据集拓扑结构

	vtkSmartPointer mapper =
		vtkSmartPointer::New();
	mapper->SetInputData(linesPolyData);

	vtkSmartPointer lineActor =
		vtkSmartPointer::New();
	lineActor->SetMapper(mapper);
	lineActor->GetProperty()->SetColor(1, 0, 0);
	lineActor->GetProperty()->SetLineWidth(3);

	vtkSmartPointer renderer =
		vtkSmartPointer::New();

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(renderer);

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	renderWindowInteractor->SetRenderWindow(renderWindow);

	renderer->AddActor(lineActor);
	renderWindow->Render();

	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

结果:

VTK 中画直线的方法汇总(二) C++ code_第2张图片 triangle VTK 中画直线的方法汇总(二) C++ code_第3张图片 irregular figure

 

你可能感兴趣的:(VTK,VTK,vtkLineSource,vtkPolyLine,source,code,C++)