实际操作中我们经常会用到通过交互操作改变vtkactor的数据vtkpolydata,比如光滑物体某个三角网格,移动某一个线条,或者某一点,而保持其他的不变,简单来说就是在class 中实现对vtkpolydata的重新写入,再使得重新渲染。实现步骤整理如下:
class callbacknew :public vtkCommand
{public:
static callbacknew *New()
{
return new callbacknew;
}
void SetActor(vtkActor * mainActor)
{this->classActor=mainActor;
this->pd=vtkPolyData::SafeDownCast(mainActor->GetMapper()->GetInputAsDataSet());//或者通过建立public函数进行直接赋值也行
}
virtual void Execute(vtkObject *, unsigned long event, void *)
{ if(event == vtkCommand::LeftButtonPressEvent){
//这里常用函数列出来
vtkSmartPointer
polyda->GetCellPoints(cellid, pts);//不推荐polyda->GetCellPoints(CellId, npt, pIds);
double PointPos[3];
polyda->Getpoints()->GetPoint(PointId, PointPos);//不推荐 double *pos= polyda->GetPoints->GetPoint(PointId);
polyda->Getpoints()->SetPoint(PointId,a,b,c);
this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->RemoveActor(classActor);
mapper->Setinputdata(polyda);
classActor->Setmapper(mapper);
this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(classActor);//先remove在add,不然无法更新actor;
this->interactor->Render();//不要忘了这个
}
}
private:
vtkActor * classActor;
vtkPolyData * pd;
}
在主函数中callback->SetActor(mainActor);即可;