VTK笔记——多边形剪切(vtkSelectPolyData)
这篇笔记和之前的一篇笔记,VTK笔记——多边形剪切(vtkClipPolyData),都是关于多边形处理的,但不同的是后者是用封闭的点线来剪切。点线剪切可以在多边形上任意剪切掉一部分,显得比较灵活,就像下面示意这样。A)可以在多边形的中间剪出一个洞,B)也可在多边形边缘剪掉一块。
我们知道,多边形是由点(points)和面片(cells)组成的。剪切的本身就是对多边形的这些数据进行处理。而在实际应用中,还有更多问题需要考虑。比如,切线是是否是任意的,需不需要过顶点,如果没在顶点上,那是否又需要拆分三角形等等。
vtkSelectPolyData
vtk中,vtkSelectPolyData,有一个用于多边形剪切的类
关于它的描述:
select portion of polygonal mesh; generate selection scalars
vtkSelectPolyData is a filter that selects polygonal data based on defining a “loop” and indicating the region inside of the loop. The mesh within the loop consists of complete cells (the cells are not cut). Alternatively, this filter can be used to generate scalars. These scalar values, which are a distance measure to the loop, can be used to clip, contour. or extract data (i.e., anything that an implicit function can do).
The loop is defined by an array of x-y-z point coordinates. (Coordinates should be in the same coordinate space as the input polygonal data.) The loop can be concave and non-planar, but not self-intersecting. The input to the filter is a polygonal mesh (only surface primitives such as triangle strips and polygons); the output is either a) a portion of the original mesh laying within the selection loop (GenerateSelectionScalarsOff); or b) the same polygonal mesh with the addition of scalar values (GenerateSelectionScalarsOn).
The algorithm works as follows. For each point coordinate in the loop, the closest point in the mesh is found. The result is a loop of closest point ids from the mesh. Then, the edges in the mesh connecting the closest points (and laying along the lines forming the loop) are found. A greedy edge tracking procedure is used as follows. At the current point, the mesh edge oriented in the direction of and whose end point is closest to the line is chosen. The edge is followed to the new end point, and the procedure is repeated. This process continues until the entire loop has been created.
To determine what portion of the mesh is inside and outside of the loop, three options are possible. 1) the smallest connected region, 2) the largest connected region, and 3) the connected region closest to a user specified point. (Set the ivar SelectionMode.)
Once the loop is computed as above, the GenerateSelectionScalars controls the output of the filter. If on, then scalar values are generated based on distance to the loop lines. Otherwise, the cells laying inside the selection loop are output. By default, the mesh laying within the loop is output; however, if InsideOut is on, then the portion of the mesh laying outside of the loop is output.
The filter can be configured to generate the unselected portions of the mesh as output by setting GenerateUnselectedOutput. Use the method GetUnselectedOutput to access this output. (Note: this flag is pertinent only when GenerateSelectionScalars is off.)
简单总结一下:
- 选择多边形网格的一部分,输出部分多边形网格或标量,由GenerateSelectionScalarsOn/Off决定
- 网格的选取是通过一个环(loop)来指定的,可以是凹的和非平面的,但不能自相交
- 要确定选择网格的那个部分,有三种方式:1)最小连通区域,2)最大连通区域,3)最接近用户选取的连通区域。通过SelectionMode设置
- 默认是输出环内的网格,不过,可以通过InsideOut设置输出环外的网格
- 也可以输出未选取的部分,由GenerateUnselectedOutput决定
- 确保选取的点在一个连通的表面上,否则结果将为空。另外,自相交的结果不可预料
主要代码
构造点
1 | vtkSmartPointer<vtkPoints> loopPoints = |
设置剪切
1 | vtkSmartPointer<vtkSelectPolyData> selectPolyData = |
获取剪切数据selectPolyData->GetOutputPort();
selectPolyData->GetUnselectedOutput()
效果
实际使用过程中,loop的点可能是通过交互进行拾取的。
示例代码
1 |
|