首先贴下代码:
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCylinder.h"
#include "vtkPlane.h"
#include "vtkImplicitBoolean.h"
#include "vtkPolyDataMapper.h"
#include "vtkSphereSource.h"
#include "vtkProperty.h"
#include "vtkClipPolyData.h"
#include "vtkTransformPolyDataFilter.h"
#include "vtkTransform.h"
#include "vtkInteractorStyleTrackballCamera.h"
int main()
{
vtkSphereSource *sphere = vtkSphereSource::New();
sphere->SetCenter(0, 0, 0);
sphere->SetRadius(10);
sphere->SetThetaResolution(40);
sphere->SetPhiResolution(40);
vtkCylinder *cylinder = vtkCylinder::New();//圆柱
cylinder->SetCenter(0, 0, 0);
cylinder->SetRadius(3);
vtkPlane *vPlane = vtkPlane::New();//横截面
vPlane->SetOrigin(0, 0, 0);
vPlane->SetNormal(0, -1, 0);
vtkImplicitBoolean *cuted_cylinder = vtkImplicitBoolean::New();
cuted_cylinder->SetOperationTypeToIntersection();
cuted_cylinder->AddFunction(cylinder);
cuted_cylinder->AddFunction(vPlane);
vtkClipPolyData *clipper = vtkClipPolyData::New();
clipper->SetInputConnection(sphere->GetOutputPort());
clipper->SetClipFunction(cylinder);
clipper->GenerateClipScalarsOn();
clipper->GenerateClippedOutputOn();
clipper->SetValue(0.5);
vtkTransform *transform = vtkTransform::New();
transform->Translate(7, 0, 0);
vtkTransformPolyDataFilter *filter = vtkTransformPolyDataFilter::New();
filter->SetInputConnection(clipper->GetOutputPort());
filter->SetTransform(transform);
vtkClipPolyData *clipper2 = vtkClipPolyData::New();
clipper2->SetInputConnection(filter->GetOutputPort());
clipper2->SetClipFunction(cuted_cylinder);
clipper2->GenerateClipScalarsOn();
clipper2->GenerateClippedOutputOn();
clipper2->SetValue(0.5);
vtkPolyDataMapper *map = vtkPolyDataMapper::New();
map->SetInputConnection(clipper2->GetOutputPort());
map->ScalarVisibilityOff();
vtkActor *actor = vtkActor::New();
actor->SetMapper(map);
actor->GetProperty()->SetColor(0, 1, 1);
//actor->GetProperty()->SetAmbientColor(0.4,0.5,0.6);
//actor->GetProperty()->SetDiffuseColor(0.8,0.6,0.2);
actor->RotateX(40);
vtkRenderer *ren = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
ren->AddActor(actor);
ren->SetBackground(1, 1, 1);
renWin->SetSize(450, 450);
vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New();
iren->SetInteractorStyle(style);
iren->Initialize();
renWin->Render();
iren->Start();
}
代码的主要作用:
生成一个球体,并用圆柱和被平面截断的圆柱去切割球体,将切割完的球体进行显示。
分步解释:
1、生成一个球体、一个圆柱体和一个横截面
2、用vtkImplicitBoolean执行布尔运算,将横截面与圆柱体的相交,得到半个圆柱体cuted_cylinder
3、用vtkClipPolyData进行网格剪裁操作,对球体用最初生成的圆柱体进行剪裁。此时球体应该是中间被圆柱体透过变成一个带洞的球体。
4、生成一个transform,对剪裁过的球体进行平移,因为下一步想要用半个圆柱体对球体进行剪裁,如果不移动的话,剪裁路径与3中圆柱体一致,剪裁后也看不到什么区别,对球体进行平移后,半个圆柱体将不会透过球体,而是剪裁掉球体的一部分。
5、再实例化一个vtkClipPolyData,用cuted_cylinder对剪裁后的球体进行剪裁,在之后就是对剪裁后的结果进行显示了。
显示结果:
可以看到,最终球体中间是被圆柱体穿透了,而左边并未被圆柱体穿透。
总结:
1、VTK中,布尔运算vtkImplicitBoolean的输入与vtkClipPolyData的Clip对象输入都是隐函数,隐函数在VTK里面就是对三维空间中的任意几何体的抽象对象,如球体、圆锥体、圆柱体、平面等,它可以简单设置几个参数就完成目标在三维空间中的具体表示。
2、VTK中,模型移动是需要先生成一个transform,然后再实例化一个vtkTransformPolyDataFilter来对目标模型进行变化的,类似先生成一个坐标转换函数,然后通过vtkTransformPolyDataFilter来完成每个顶点的坐标转换。