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();

结果图:

http://www.cnblogs.com/hawking81/archive/2011/09/22/2185265.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值