剪切
与抽取轮廓类似,剪切(Cut)也是指获取数据集中的部分数据。不过剪切是从一个完全不同的角度来处理的。剪切可以控制数据获取的区域,而抽取轮廓做不到这一点。
剪切操作需要一把”刀“——隐函数,并且要指剪切的位置——剪切值。给定了这两个参数后,就确定了切面——隐函数对应的函数曲面。剪切的处理过程是首先将数据集中的点坐标带入隐函数中进行计算,然后将算出的函数值等于剪切值的部分(包括属性数据)取出来,即已知函数值,求自变量。可以看出,剪切是基于坐标的操作。注意,剪切只是剪下了”刀面“部分的数据,并不是将数据集分开。
抽取轮廓与剪切的区别:
- 抽取的轮廓形状完全取决于数据集中标量数据的分布;剪切剪下来的形状则是完全是由”刀“的形状决定的。
- 抽取轮廓可以设置多个抽取值,产生的各个轮廓形状一般不相似;剪切也可以设置多个剪切值,但各个剪切结果都=是相似的。
- 同一轮廓上的标量值都是相等,直接进行颜色映射时,轮廓上的颜色只有一种;剪切的结果不存在类似问题。
示例演示
CMakeLists.txt文件代码如下:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(CutExample)
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
ADD_EXECUTABLE(CutExample cut.cpp)
TARGET_LINK_LIBRARIES(CutExample ${VTK_LIBRARIES})
C++代码:
#include "vtkActor.h"
#include "vtkXMLPolyDataReader.h"
#include "vtkCutter.h"
#include "vtkPlane.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkPolyDataNormals.h"
#include "vtkProperty.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkStripper.h"
#include "vtkTriangleFilter.h"
#include "vtkSmartPointer.h"
#include "vtkInteractorStyleTrackballCamera.h"
int main()
{
//read input data
vtkSmartPointer<vtkXMLPolyDataReader> reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
reader->SetFileName("E:\\TestData\\cow.vtp");
vtkSmartPointer<vtkPolyDataNormals> cow_normals = vtkSmartPointer<vtkPolyDataNormals>::New();
cow_normals->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkPlane> plane = vtkSmartPointer<vtkPlane>::New();
plane->SetOrigin(0.25, 0.0, 0.0);
plane->SetNormal(-1, -1, 0);
vtkSmartPointer<vtkCutter> cutEdges = vtkSmartPointer<vtkCutter>::New();
cutEdges->SetInputConnection(cow_normals->GetOutputPort());
cutEdges->SetCutFunction(plane);
cutEdges->GenerateCutScalarsOn();
cutEdges->SetValue(0, 0.5);
vtkSmartPointer<vtkStripper> cutStrips = vtkSmartPointer<vtkStripper>::New();
cutStrips->SetInputConnection(cutEdges->GetOutputPort());
cutStrips->Update();
vtkSmartPointer<vtkPolyData> cutPolyData = vtkSmartPointer<vtkPolyData>::New();
cutPolyData->SetPoints(cutStrips->GetOutput()->GetPoints());
cutPolyData->SetPolys(cutStrips->GetOutput()->GetLines());
vtkSmartPointer<vtkTriangleFilter> cutTriangles = vtkSmartPointer<vtkTriangleFilter>::New();
cutTriangles->SetInputData(cutPolyData);
vtkSmartPointer<vtkPolyDataMapper> cut_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
cut_mapper->SetInputConnection(cutEdges->GetOutputPort());
cut_mapper->ScalarVisibilityOff();
vtkSmartPointer<vtkActor> cut_actor = vtkSmartPointer<vtkActor>::New();
cut_actor->SetMapper(cut_mapper);
cut_actor->GetProperty()->SetColor(1.0, 0.0, 0.0);
vtkSmartPointer<vtkPolyDataMapper> cow_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
cow_mapper->SetInputConnection(cow_normals->GetOutputPort());
cow_mapper->ScalarVisibilityOff();
vtkSmartPointer<vtkActor> cow_actor = vtkSmartPointer<vtkActor>::New();
cow_actor->SetMapper(cow_mapper);
cow_actor->GetProperty()->SetRepresentationToWireframe();
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(cut_actor);
renderer->AddActor(cow_actor);
renderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(renderer);
renWin->SetSize(640, 480);
renWin->Render();
renWin->SetWindowName("CutExample");
vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
iren->SetInteractorStyle(style);
iren->Initialize();
iren->Start();
return EXIT_SUCCESS;
}
运行结果:
图中红色线是剪切结果。