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

     VTK 中经常涉及到如何去画一条直线,有的时候是为了将医疗数据在可视化的基础上,比如在神经外科上,有时候关于穿刺针进入病人脑部时,需要医生给出进入的路径及方向,这时候就在可视化软件上,可通过直线的方向及长短来更形象的展示医生在医疗软件上的初步规划。另一方面,有时候需要在软件上部署一些小工具,通过工具在软件界面上来测量长度,或角度。(后面将讨论一下vtkDistanceWidget)。

(一) vtkLine画直线  (直线或多边形)

    vtkLine 的官方介绍  https://vtk.org/doc/nightly/html/classvtkLine.html

   实现代码: 

//method 1 vtkLine
#include 
#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 pts =
		vtkSmartPointer::New();
	pts->InsertNextPoint(p0);
	pts->InsertNextPoint(p1);

	vtkSmartPointer line0 =
		vtkSmartPointer::New();
	line0->GetPointIds()->SetId(0, 0); 
	line0->GetPointIds()->SetId(1, 1); 

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

	基于三维空间中四点画四边形
	//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;

	//vtkSmartPointer line0 =
	//	vtkSmartPointer::New();
	//line0->GetPointIds()->SetId(0, 0);
	//line0->GetPointIds()->SetId(1, 1);
	//SetId()的第一个参数是线段的端点的ID,第二个参数是连接点的ID

	//vtkSmartPointer line1 =
	//	vtkSmartPointer::New();
	//line1->GetPointIds()->SetId(0, 1);
	//line1->GetPointIds()->SetId(1, 2);

	//vtkSmartPointer line2 =
	//	vtkSmartPointer::New();
	//line2->GetPointIds()->SetId(0, 2);
	//line2->GetPointIds()->SetId(1, 3);

	//vtkSmartPointer line3 =
	//	vtkSmartPointer::New();
	//line3->GetPointIds()->SetId(0, 3);
	//line3->GetPointIds()->SetId(1, 0);

	//vtkSmartPointer lines =
	//	vtkSmartPointer::New();
	//lines->InsertNextCell(line0);
	//lines->InsertNextCell(line1);
	//lines->InsertNextCell(line2);
	//lines->InsertNextCell(line3);


	// 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_第1张图片 line VTK 中画直线的方法汇总(一) C++ code_第2张图片 quadrilateral

 

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