我正在使用MFC环境和VTK, 在我调用我的程序之后,我调用了VTKRenderWindowInteractor类的“Start”方法。 问题是,在我关闭窗口之后仍然存在进展,我无法重启我的程序,因为有 已经取得了进展。
代码:
extern list<vtkIdType> deleteIds;
// Define interaction style
class InteractorStyle : public vtkInteractorStyleRubberBandPick
{
public:
static InteractorStyle* New();
vtkTypeMacro(InteractorStyle,vtkInteractorStyleRubberBandPick);
InteractorStyle()
{
this->SelectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
this->SelectedActor = vtkSmartPointer<vtkActor>::New();
this->SelectedActor->SetMapper(SelectedMapper);
}
virtual void OnLeftButtonUp()
{
// Forward events
vtkInteractorStyleRubberBandPick::OnLeftButtonUp();
vtkPlanes* frustum = static_cast<vtkAreaPicker*>(this->GetInteractor()->GetPicker())->GetFrustum();
vtkSmartPointer<vtkExtractGeometry> extractGeometry =
vtkSmartPointer<vtkExtractGeometry>::New();
extractGeometry->SetImplicitFunction(frustum);
#if VTK_MAJOR_VERSION <= 5
extractGeometry->SetInput(this->Points);
#else
extractGeometry->SetInputData(this->Points);
#endif
extractGeometry->Update();
vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter =
vtkSmartPointer<vtkVertexGlyphFilter>::New();
glyphFilter->SetInputConnection(extractGeometry->GetOutputPort());
glyphFilter->Update();
vtkPolyData* selected = glyphFilter->GetOutput();
//std::cout << "Selected " << selected->GetNumberOfPoints() << " points." << std::endl;
//std::cout << "Selected " << selected->GetNumberOfCells() << " cells." << std::endl;
#if VTK_MAJOR_VERSION <= 5
this->SelectedMapper->SetInput(selected);
#else
this->SelectedMapper->SetInputData(selected);
#endif
this->SelectedMapper->ScalarVisibilityOff();
vtkIdTypeArray* ids = vtkIdTypeArray::SafeDownCast(selected->GetPointData()->GetArray("OriginalIds"));
if(ids)
{
for(vtkIdType i = 0; i < ids->GetNumberOfTuples(); i++)
{
deleteIds.push_back(ids->GetValue(i));
//std::cout << "Id " << i << " : " << ids->GetValue(i) << std::endl;
}
}
this->SelectedActor->GetProperty()->SetColor(1.0, 0.0, 0.0); //(R,G,B)
this->SelectedActor->GetProperty()->SetPointSize(3);
this->CurrentRenderer->AddActor(SelectedActor);
this->GetInteractor()->GetRenderWindow()->Render();
this->HighlightProp(NULL);
}
void SetPoints(vtkSmartPointer<vtkPolyData> points) {this->Points = points;}
private:
vtkSmartPointer<vtkPolyData> Points;
vtkSmartPointer<vtkActor> SelectedActor;
vtkSmartPointer<vtkDataSetMapper> SelectedMapper;
};
vtkStandardNewMacro(InteractorStyle);
CoalStock::CoalStock(CWnd *pcWnd)
{
this->pcWnd = pcWnd;
this->pvtkMFCWindow = NULL;
// Create the the renderer, window and interactor objects.
this->pvtkRenderer = vtkRenderer::New();
this->pvtkMFCWindow = new vtkMFCWindow(pcWnd);
this->pvtkPoints = vtkPoints::New();
this->pRemovalPoints = vtkPoints::New();
this->polyData = vtkPolyData::New();
this->delaunay2D = vtkDelaunay2D::New();
this->cutEdges = vtkCutter::New();
this->pvtkMFCWindow->GetRenderWindow()->AddRenderer(this->pvtkRenderer);
}
我的班级方法
int CoalStock::PickerDeletePoint()
{
vtkSmartPointer<vtkAreaPicker> areaPicker =
vtkSmartPointer<vtkAreaPicker>::New();
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetPicker(areaPicker);
renderWindowInteractor->SetRenderWindow(this->pvtkRenderer->GetRenderWindow());
string id = "deletePoint";
RemoveActor();
ShapeActorMap::iterator am_it = _actorMap.find(id);
if (am_it != _actorMap.end ())
{
return -10;//if exist return -10
}
int ret = 1;
if(delaunay2D->GetInput() == NULL)
{
ret = DelaunayPoly();
}
if(ret == 0) return ret;
vtkSmartPointer<vtkIdFilter> idFilter =
vtkSmartPointer<vtkIdFilter>::New();
idFilter->SetInput(delaunay2D->GetOutput());
idFilter->SetIdsArrayName("OriginalIds");
idFilter->Update();
vtkSmartPointer<vtkDataSetSurfaceFilter> surfaceFilter =
vtkSmartPointer<vtkDataSetSurfaceFilter>::New();
surfaceFilter->SetInputConnection(idFilter->GetOutputPort());
surfaceFilter->Update();
vtkPolyData* input = surfaceFilter->GetOutput();
vtkSmartPointer<vtkPolyDataMapper> mapPoint = vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
mapPoint->SetInputConnection(input->GetProducerPort());
#else
mapPoint->SetInputData(input);
#endif
vtkSmartPointer<vtkActor> actorPoint = vtkSmartPointer<vtkActor>::New();
actorPoint->SetMapper(mapPoint);
actorPoint->GetProperty()->SetColor(1, 1, 0); //璁惧畾鏇查潰鑳岄潰棰滆壊
mapPoint->ScalarVisibilityOff();
actorPoint->GetProperty()->SetRepresentationToPoints();
this->AddActorToRenderer(actorPoint);
_actorMap[id] = actorPoint;
vtkSmartPointer<InteractorStyle> style =
vtkSmartPointer<InteractorStyle>::New();
style->SetPoints(input);
renderWindowInteractor->SetInteractorStyle( style );
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
调用
void CvtkDLGDlg::OnBnClickedPicker()
{
pCoalStock->PickerDeletePoint();
}