VTK删除三维模型指定三角形面片
这篇文章内容承接上一篇:VTK框选表面拾取面片——仅选中前表面
实现删除鼠标框选中的三角形面片
效果预览
功能说明
通过R键盘切换交互模式和框选模式,删除模型内框选中的三角形面片。
方法介绍
利用vtkInteractorStyleRubberBandPick交互方式实现框选。(通过R键切换交互模式和框选模式)
利用vtkAreaPicker收集框选的信息。VTK还提供了vtkCellPicker,但是CellPicker只能选中某个对象,不能框选一个集合。
使用vtkCellLocator中的IntersectWithLine函数,用光线投射法寻找靠近摄像头一侧的面片cell,然后利用vtkPolyDataConnectivityFilter找出与最近面片cell相连的表面,即前表面。
前期利用vtkIdFilter处理原模型,将三角形cell的id号提前存到cell的属性里面(即vtkCellData),后续通过其他Filter去处理模型后,都能通过读取cell的属性获取它原来的Cell id号,再通过该id号回到原模型访问自己。
找出要删除的面片,需要先调用vtkPolyData的BuildLinks函数建立cell和point的拓扑链接,才能使用DeleteCell函数删除对应面片,否则会报错,最后使用RemoveDeletedCells函数提交删除操作,移除这些删除的Cell(DeleteCell函数仅仅是对面片做了删除标记,并没有真正的移除),最后调用Modified函数提交模型的修改操作。
源代码中已经写好详细注释。
源代码
完整代码可以直接编译运行
#include <vtkPolyData.h>#include <vtkPolyDataMapper.h>#include <vtkActor.h>#include <vtkRenderer.h>#include <vtkRenderWindow.h>#include <vtkRenderWindowInteractor.h>#include <vtkInteractorStyleRubberBandPick.h>#include <vtkAreaPicker.h>#include <vtkCommand.h>#include <vtkCallbackCommand.h>#include <vtkIdFilter.h>#include <vtkExtractPolyDataGeometry.h>#include <vtkSphereSource.h>#include <vtkPlanes.h>#include <vtkDataSetMapper.h>#include <vtkProperty.h>#include <vtkCellLocator.h>#include <vtkCamera.h>#include <vtkPolyDataConnectivityFilter.h>#include <vtkIdTypeArray.h>#include <vtkCellData.h>/** 定义命令:* 使用vtkInteractorStyleRubberBandPick交互方式时,会自动触发vtkAreaPicker* 交互时框选的信息会存储在vtkAreaPicker中* 将vtkAreaPicker的结束拾取事件绑定vtkPickerCallback* 类似QT中信号与槽的机制* vtkAreaPicker触发结束拾取信号,然后执行vtkPickerCallback中的Execute函数*/class vtkPickerCallback : public vtkCommand{public://@brief 定义New函数(固定格式)static vtkPickerCallback* New() {return new vtkPickerCallback;}//@brief 定义Execute函数(vtkCommand中的Execute为纯虚函数,必须要实现)virtual void Execute(vtkObject* caller, unsigned long, void*);void SetPolyData(vtkPolyData* input);void SetRenderer(vtkRenderer* input);private:vtkPolyData* polyData;//需要处理的几何数据vtkRenderer* renderer;//需要调用的渲染器vtkDataSetMapper* mapper;//用于显示框选面片的mappervtkActor* actor;//用于显示框选面片的actor};int main(){//****************创建球体*****************vtkSphereSource* sphere = vtkSphereSource::New();sphere->SetThetaResolution(18);sphere->SetPhiResolution(18);sphere->SetRadius(10);sphere->SetCenter(0, 0, 0);sphere->Update();//****************创建Mapper***************vtkPolyDataMapper* mapper = vtkPolyDataMapper::New();mapper->SetInputConnection(sphere->GetOutputPort());//换成此语句效果一致:mapper->SetInputData(sphere->GetOutput());//****************创建Actor****************vtkActor* actor = vtkActor::New();actor->SetMapper(mapper);//****************创建渲染器***************vtkRenderer* renderer = vtkRenderer::New();renderer->AddActor(actor);//****************创建渲染窗口*************vtkRenderWindow* renderWindow = vtkRenderWindow::New();renderWindow->AddRenderer(renderer);//****************创建交互器***************vtkRenderWindowInteractor* renderWindowInteractor = vtkRenderWindowInteractor::New();//****************创建交互方式*************vtkInteractorStyleRubberBandPick* interactorStyle = vtkInteractorStyleRubberBandPick::New();//****************创建拾取回调函数*********vtkPickerCallback* callback = vtkPickerCallback::New();callback->SetPolyData(sphere->GetOutput());callback->SetRenderer(renderer);//****************创建区域拾取器***********vtkAreaPicker* areaPicker = vtkAreaPicker::New();areaPicker->AddObserver(vtkCommand::EndPickEvent, callback);//为“结束拾取”事件绑定“拾取回调函数”renderWindowInteractor->SetRenderWindow(renderWindow);//为交互器设置渲染窗口renderWindowInteractor->SetInteractorStyle(interactorStyle);//为交互器设置交互方式renderWindowInteractor->SetPicker(areaPicker);//为交互器设置拾取器renderWindowInteractor->Start();return 0;}void vtkPickerCallback::Execute(vtkObject* caller, unsigned long, void*){//通过反射获取调用者vtkAreaPicker* areaPicker = static_cast<vtkAreaPicker*>(caller);//获取框选的视锥体(由六个面组成)vtkPlanes* frustum = areaPicker->GetFrustum();//提前标记几何数据的CellIdvtkIdFilter* idFilter = vtkIdFilter::New();idFilter->SetInputData(polyData);idFilter->SetCellIdsArrayName("OriginalCellId");idFilter->Update();//提取视锥体内的模型vtkExtractPolyDataGeometry* extract = vtkExtractPolyDataGeometry::New();extract->SetInputConnection(idFilter->GetOutputPort());extract->SetImplicitFunction(frustum);extract->Update();if (!extract->GetOutput()->GetPolys()){return;}//创建面片定位器vtkCellLocator* locator = vtkCellLocator::New();locator->SetDataSet(extract->GetOutput());locator->BuildLocator();//----------利用光线投射的方法寻找更靠近摄像机的面片------------double rayStart[3];//光线起点坐标:设置为摄像机位置double rayDirection[3];//光线方向向量:设置为框选数据包围盒的中心renderer->GetActiveCamera()->GetPosition(rayStart);extract->GetOutput()->GetCenter(rayDirection);double xyz[3];double t;double pcoords[3];int subId;vtkIdType cellId = -1;//记录光线击中的面片Id号locator->IntersectWithLine(rayStart, rayDirection, 0.0001, t, xyz, pcoords, subId, cellId);//-----------利用找到的面片获取相连的面vtkPolyDataConnectivityFilter* connectivity = vtkPolyDataConnectivityFilter::New();connectivity->SetInputConnection(extract->GetOutputPort());connectivity->SetExtractionModeToCellSeededRegions();connectivity->InitializeSeedList();connectivity->AddSeed(cellId);connectivity->Update();//--------删除框选面片----------//提取框选面片的原始面片IDvtkIdTypeArray* ids = dynamic_cast<vtkIdTypeArray*>(connectivity->GetOutput()->GetCellData()->GetArray("OriginalCellId"));//要删除面片前必须先执行建立拓扑链接polyData->BuildLinks();if (!ids){return;}for (int i = 0; i < ids->GetNumberOfValues(); i++){vtkIdType id = ids->GetValue(i);polyData->DeleteCell(id);}//提交删除面片操作polyData->RemoveDeletedCells();polyData->Modified();//刷新渲染器renderer->Render();}void vtkPickerCallback::SetPolyData(vtkPolyData* input){polyData = input;}void vtkPickerCallback::SetRenderer(vtkRenderer* input){renderer = input;//初始化用于显示框选面片的mapper和actormapper = vtkDataSetMapper::New();actor = vtkActor::New();actor->SetMapper(mapper);}
如果觉得《【VTK】VTK删除三维模型指定三角形面片》对你有帮助,请点赞、收藏,并留下你的观点哦!