【vtk】 裁剪

使用vtkClipPolyData進行裁剪

效果:



#include "stdafx.h"
#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~14行包含相應頭文件

18~22行定義一個球,其圓心在原點,半徑爲10,設置經緯方向的三角片數均爲40;

24~26行定義圓柱的隱函數,設定半徑爲3,中心在原點,在vtk中的圓柱隱函數是沒有設定長度的,其方向爲沿y軸方向,此處的圓柱用於對剛纔定義的球進行裁剪,裁剪效果就是把球給打通了,兩面都進行的裁剪,如果要只裁剪球的一面,則用半個圓柱。接下來的28~30行定義了一個平面,用平面與圓柱求交可得出半個圓柱。

32~35行將平面和圓柱進行Bool求交運算(SetOperationTypeToIntersection())得出半個圓柱的隱函數表示cuted_cylinder,如果是求和運算則用SetOperationTypeToUnion()。

37~42行定義了一個vtkClipPolyData對象clipper,將其輸入設定爲18~24行建立的球,裁剪函數設置爲圓柱cylinder,接下來的三行40~42行似乎沒有什麼用處。

上面得到的只是用一個圓柱裁剪球的效果,但是爲了同時顯示用半個圓柱裁剪的效果,將剛纔的裁剪體演x軸移動一定位置,然後用剛纔Bool運算獲得的半個圓柱進行裁剪,以便於比較。對幾何對象的移動需要指定一個變換transform,然後定義一個vtkTransformPolyDataFilter對象filter,然後將filter的輸入設定爲要移動的幾何對象,變換設定爲transform。44~48行實現了將裁剪球的移動,其移動結果在filter中。

49~54行將移動後得到的filter作爲新一次裁剪的輸入,以半個圓柱cuted_cylinder爲裁剪函數進行裁剪,得到裁剪結果clipper2













發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章