VTK-Wight 之 vtkContourWidget简介

来源:互联网 发布:易语言远程桌面源码 编辑:程序博客网 时间:2024/06/08 13:29

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

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

监听事件

   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

还有一些其他的事件

继承图

利用 vtkContourWidget 画一个闭合区域取点原则:最后一个和第一个结点重合

示例
#include <vtkSmartPointer.h>
// To setup the ContourWidget and its representation:#include <vtkContourWidget.h>#include <vtkProperty.h>#include <vtkOrientedGlyphContourRepresentation.h>// To create the geometry:#include <vtkPolyData.h>#include <vtkCellArray.h>#include <vtkPoints.h>#include <vtkMath.h>// Usual VTK pipeline elements:#include <vtkRenderer.h>#include <vtkRenderWindow.h>#include <vtkRenderWindowInteractor.h>#include <vtkInteractorStyleTrackballCamera.h>int main( int argc, char *argv[] ){// Create the contour widgetvtkSmartPointer<vtkContourWidget> contourWidget = vtkSmartPointer<vtkContourWidget>::New();// Override the default representation for the contour widget to customize its lookvtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRepresentation =vtkSmartPointer<vtkOrientedGlyphContourRepresentation>::New();contourRepresentation->GetLinesProperty()->SetColor(1, 0, 0); // Set color to redcontourWidget->SetRepresentation(contourRepresentation);// Generate a set of points arranged in a circleint numPts = 10;vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();for (int i = 0; i < numPts; i++){// Create numPts points evenly spread around a circumference of radius 0.1const double angle = 2.0*vtkMath::Pi()*i/numPts;points->InsertPoint(static_cast<vtkIdType>(i), 0.1*cos(angle), 0.1*sin(angle), 0.0 );}// Create a cell array to connect the points into meaningful geometryvtkIdType* vertexIndices = new vtkIdType[numPts+1];for (int i = 0; i < numPts; i++) { vertexIndices[i] = static_cast<vtkIdType>(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<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();lines->InsertNextCell(numPts+1, vertexIndices);// Create polydata to hold the geometry just created, and populate itvtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();polydata->SetPoints(points);polydata->SetLines(lines);// Create the renderer to visualize the scenevtkSmartPointer<vtkRenderer> renderer =vtkSmartPointer<vtkRenderer>::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 scenevtkSmartPointer<vtkRenderWindow> renderWindow =vtkSmartPointer<vtkRenderWindow>::New();renderWindow->AddRenderer(renderer);// Create the events manager for the renderer windowvtkSmartPointer<vtkRenderWindowInteractor> interactor =vtkSmartPointer<vtkRenderWindowInteractor>::New();interactor->SetRenderWindow(renderWindow);// Use the "trackball camera" interactor style, rather than the default "joystick camera"vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();interactor->SetInteractorStyle(style);// Set up the contour widget within the visualization pipeline just assembledcontourWidget->SetInteractor(interactor);contourWidget->On();// Turn on the interactor observercontourWidget->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 elementsinteractor->Initialize();// Start the interactioninteractor->Start();return EXIT_SUCCESS;}



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

0 0
原创粉丝点击