VTK-Wight 之 vtkContourWidget简介

VTK中Wights功能的实现需要两个部分的协作:

  1.  事件处理,继承于 vtkAbstractWight 类
  2.  描述几何特征,继承于 vtkWightRespresentation
vtkContourWidget 简介
用一个点集创建一个轮廓。
该类常常用于选取一个点集,绘制点之间的连线。最后一个点的选取决定了,轮廓是闭合的还是是开放的。
vtkContourRepresentation 这个类负责所有点的位置,连线的计算,以及轮廓的操作,点、线的绘制。
关于这个respresentation;
VTK-Wight 之 vtkContourWidget简介_第1张图片
监听事件

   LeftButtonPressEvent - triggers a Select event
   RightButtonPressEvent - triggers a AddFinalPoint event
   MouseMoveEvent - triggers a Move event
   LeftButtonReleaseEvent  - triggers an EndSelect event
   Delete key event - triggers a Delete event
   Shift + Delete key event - triggers a Reset event

还有一些其他的事件

继承图
VTK-Wight 之 vtkContourWidget简介_第2张图片
利用 vtkContourWidget 画一个闭合区域取点原则:最后一个和第一个结点重合

示例
#include
// To setup the ContourWidget and its representation:
#include 
#include 
#include 
// To create the geometry:
#include 
#include 
#include 
#include 
// Usual VTK pipeline elements:
#include 
#include 
#include 
#include 

int main( int argc, char *argv[] )
{
	// Create the contour widget
	vtkSmartPointer contourWidget = vtkSmartPointer::New();

	// Override the default representation for the contour widget to customize its look
	vtkSmartPointer contourRepresentation =
		vtkSmartPointer::New();
	contourRepresentation->GetLinesProperty()->SetColor(1, 0, 0); // Set color to red
	contourWidget->SetRepresentation(contourRepresentation);

	// Generate a set of points arranged in a circle
	int numPts = 10;
	vtkSmartPointer points = vtkSmartPointer::New();
	for (int i = 0; i < numPts; i++)
	{
		// Create numPts points evenly spread around a circumference of radius 0.1
		const double angle = 2.0*vtkMath::Pi()*i/numPts;
		points->InsertPoint(static_cast(i), 0.1*cos(angle), 0.1*sin(angle), 0.0 );
	}

	// Create a cell array to connect the points into meaningful geometry
	vtkIdType* vertexIndices = new vtkIdType[numPts+1];
	for (int i = 0; i < numPts; i++) { vertexIndices[i] = static_cast(i); }
	// Set the last vertex to 0; this means the last line segment will join the 19th point (vertices[19])
	// with the first one (vertices[0]), thus closing the circle.
	vertexIndices[numPts] = 0;
	vtkSmartPointer lines = vtkSmartPointer::New();
	lines->InsertNextCell(numPts+1, vertexIndices);

	// Create polydata to hold the geometry just created, and populate it
	vtkSmartPointer polydata = vtkSmartPointer::New();
	polydata->SetPoints(points);
	polydata->SetLines(lines);

	// Create the renderer to visualize the scene
	vtkSmartPointer renderer =	vtkSmartPointer::New();
	renderer->SetBackground(0.1, 0.2, 0.4);			// Set a dark blue background (default is black)

	// Create the GUI window to hold the rendered scene
	vtkSmartPointer renderWindow =	vtkSmartPointer::New();
	renderWindow->AddRenderer(renderer);

	// Create the events manager for the renderer window
	vtkSmartPointer interactor =	vtkSmartPointer::New();
	interactor->SetRenderWindow(renderWindow);

	// Use the "trackball camera" interactor style, rather than the default "joystick camera"
	vtkSmartPointer style =	vtkSmartPointer::New();
	interactor->SetInteractorStyle(style);

	// Set up the contour widget within the visualization pipeline just assembled
	contourWidget->SetInteractor(interactor);
	contourWidget->On();							// Turn on the interactor observer
	contourWidget->Initialize(polydata);


	std::cout << "the number of nodes is " << contourRepresentation->GetNumberOfNodes() << std::endl;
	// you can get node world position of one node by GetNthNodeWorldPosition(pointID, double_pos[3]) 
	renderer->ResetCamera();						// Reposition camera to fit the scene elements
	
	interactor->Initialize();
	// Start the interaction
	interactor->Start();
	
	return EXIT_SUCCESS;
}



暂时还没有想到还有什么,可能之后再补充吧

你可能感兴趣的:(VTK)