vtk 对polydata的bool运算

1.vtk可以对implicit data运算, 通常的polydata需要转化为implicit 进行运算。 

 经查,网上有vtkImplicitPolyData类可以将polydata转化。

2. 将这些polydata通过vtkImplicitPolyData过滤后 丢给vtkClipPolydata,作为clippolydata的裁剪函数让clippolydata做裁剪操作

3.clipdata还需要原始的polydata,丢给他。 

4.把裁剪结果丢给polydatamapper。

5.走流水线

完事。

 

上段关键代码: 

// 1.读进来一个牛的模型,丢到renderer里

vtkOBJReader *wavefront = vtkOBJReader::New();
wavefront->SetFileName("E:\\data\\Viewpoint\\cow.obj"); //我在windows平台
wavefront->Update();

vtkPolyDataMapper *cowMapper = vtkPolyDataMapper::New();

cowMapper->SetInput(wavefront->GetOutput());
vtkActor *cow = vtkActor::New();
cow->SetMapper(cowMapper);
ren->AddActor(cow);

 

//2. 创建一个圆柱体,先变换,再三角化(似乎是必须的!)
vtkCylinderSource *cylinderB = vtkCylinderSource::New();
cylinderB->SetRadius(0.2);
cylinderB->SetResolution(21);
cylinderB->CappingOn();

vtkTransform *trans = vtkTransform::New();
trans->Translate(5.1, 2.0, 0.0);
trans->Scale(0.5, 0.5, 0.5);
trans->RotateZ(90);
 

vtkTransformPolyDataFilter *tf = vtkTransformPolyDataFilter::New();

tf->SetInput(cylinderB->GetOutput());
tf->SetTransform(trans);
vtkTriangleFilter *triangle = vtkTriangleFilter::New();
triangle->SetInput(tf->GetOutput());
 

vtkPolyDataNormals *norm = vtkPolyDataNormals::New();

norm->SetInput(triangle->GetOutput());
 

vtkPolyDataMapper *CylinderBMapper = vtkPolyDataMapper::New();

CylinderBMapper->SetInput(norm->GetOutput());
 

// Append 

vtkAppendPolyData *append = vtkAppendPolyData::New(); 
append->AddInput(CylinderBMapper->GetInput());
vtkActor *CylinderBActor = vtkActor::New();
CylinderBActor->SetMapper( CylinderBMapper );
CylinderBActor->GetProperty()->SetColor(0,1,0);

 

        //3.将圆柱体转化为implicitpolydata 。

vtkImplicitPolyData *polyToImplicitDataB = vtkImplicitPolyData::New();
polyToImplicitDataB->SetTolerance(0.5);
polyToImplicitDataB->SetInput(append->GetOutput());
 

        //4.我要裁剪!

vtkClipPolyData *clipper = vtkClipPolyData::New();
clipper->SetInput((vtkPolyData *)cow->GetMapper()->GetInput());  //我要裁剪谁?牛是吧,拿来
clipper->SetClipFunction(polyToImplicitDataB);                                //你要裁剪不是要方程么?也拿来
clipper->InsideOutOff();
clipper->GenerateClipScalarsOn();

 

        //5.剪好了 走流水线去!

vtkPolyDataMapper *newmapper = vtkPolyDataMapper::New();
newmapper->SetInput(clipper->GetOutput());
cow->SetMapper(newmapper); 
ren->AddActor(CylinderBActor);
ren->GetActiveCamera()->Azimuth(50);
ren->ResetCamera();
 

        //done!

renWin->Render();

结果图:

 

 
posted @ 2011-09-22 16:33  潘东子  阅读(5250)  评论(0编辑  收藏  举报