[vtkusers] Pick & delete cell

Massinissa Bandou Massinissa.Bandou at USherbrooke.ca
Mon Feb 25 14:34:49 EST 2013


Hi David,

"Do you mean you're starting with a vtkPolyData and you want to end up with
a vtkPolyData?"

If it's possible, why not?  I started with a vtkPolyData (vtkPolyData* Data)
and I was looking for a way to delete the selected cells, so I thought to do
it with a member function called RemoveDeletedCells (). 
I have the following code to select/deselect cells with Left/Right mouse and
It works. But I'm still struggling to find out how to delete selected cells
and keep the same topology without using decimation.

class MyVtkInteractor: public vtkInteractorStyleTrackballCamera
{
public:
    std::list<int> List;
    static MyVtkInteractor* New();
    vtkTypeMacro(MyVtkInteractor,vtkInteractorStyleTrackballCamera);
    MyVtkInteractor(){
        appendFilter = vtkSmartPointer<vtkAppendFilter>::New();
    }
    virtual void OnLeftButtonDown(){
        int* pos = this->GetInteractor()->GetEventPosition();
        vtkSmartPointer<vtkCellPicker> picker =
vtkSmartPointer<vtkCellPicker>::New();
        picker->SetTolerance(0.0005);
        picker->Pick(pos[0],pos[1],pos[2],this->GetCurrentRenderer());

        if(picker->GetCellId() != -1){
            vtkSmartPointer<vtkIdTypeArray> ids =
vtkSmartPointer<vtkIdTypeArray>::New();
            ids->SetNumberOfComponents(1);
            ids->InsertNextValue(picker->GetCellId());
             
            vtkSmartPointer<vtkSelectionNode> selectionNode =
vtkSmartPointer<vtkSelectionNode>::New();
            selectionNode->SetFieldType(vtkSelectionNode::CELL);
            selectionNode->SetContentType(vtkSelectionNode::INDICES);
            selectionNode->SetSelectionList(ids);
              
            vtkSmartPointer<vtkSelection> selection =
vtkSmartPointer<vtkSelection>::New();
            selection->AddNode(selectionNode);
                
            vtkSmartPointer<vtkExtractSelection> extractSelection =
vtkSmartPointer<vtkExtractSelection>::New();
            extractSelection->SetInput(0,this->Data);
            extractSelection->SetInput(1,selection);
            extractSelection->Update();
            
            vtkSmartPointer<vtkUnstructuredGrid> selected =
vtkSmartPointer<vtkUnstructuredGrid>::New();
            selected->ShallowCopy(extractSelection->GetOutput());
            
            appendFilter->AddInput(selected);
            
            List.push_back(picker->GetCellId());
            int flag = 1;
            for(std::list<int>::iterator it = List.begin(); it!= List.end();
it++){
                if(*it == picker->GetCellId()){
                    flag = 0;
                }
            }
            if(flag == 1){
                List.push_back(picker->GetCellId());
            }

            vtkSmartPointer<vtkDataSetMapper> mapper =
vtkSmartPointer<vtkDataSetMapper>::New();
            vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
            mapper->SetInputConnection(appendFilter->GetOutputPort());
            actor->SetMapper(mapper);
            actor->GetProperty()->EdgeVisibilityOn();
            actor->GetProperty()->SetEdgeColor(1,0,0);
            actor->GetProperty()->SetColor(0.5,0,1);
            actor->GetProperty()->SetLineWidth(3);
               
           
this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(actor);
        }
        vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
    }
    virtual void OnRightButtonDown(){
        int* pos = this->GetInteractor()->GetEventPosition();
        vtkSmartPointer<vtkCellPicker> picker =
vtkSmartPointer<vtkCellPicker>::New();
        picker->SetTolerance(0.0005);
        picker->Pick(pos[0],pos[1],pos[2],this->GetCurrentRenderer());
        if(picker->GetCellId() != -1){
            int i=0;
            for(std::list<int>::iterator it = List.begin(); it !=
List.end(); it++){
                if(*it == picker->GetCellId()){
                    List.remove(*it);
                    break;
                }
                i++;
            }
            vtkSmartPointer<vtkDataSetCollection> collection =
vtkSmartPointer<vtkDataSetCollection>::New();
            collection = appendFilter->GetInputList();
            vtkDataSet* selected = collection->GetItem(i);
            appendFilter->RemoveInput(selected);
            appendFilter->Update();
            collection = appendFilter->GetInputList();
            
            vtkSmartPointer<vtkDataSetMapper> mapper =
vtkSmartPointer<vtkDataSetMapper>::New();
            vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
            mapper->SetInputConnection(appendFilter->GetOutputPort());
            actor->SetMapper(mapper);
            actor->GetProperty()->EdgeVisibilityOn();
            actor->GetProperty()->SetEdgeColor(1,0,0);
            actor->GetProperty()->SetColor(0.5,0,1);
            actor->GetProperty()->SetLineWidth(3);
             
           
this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(actor);
        }
        vtkInteractorStyleTrackballCamera::OnRightButtonDown();
    }
    
    vtkPolyData* Data;
    vtkRenderer* Renderer;
    vtkAppendFilter* appendFilter;
};


I'm trying to create a function that can just delete the selected cells from
the entire object or data (PolyData).
thanks for your help!!!

Massi




--
View this message in context: http://vtk.1045678.n5.nabble.com/Pick-delete-cell-tp5718594p5718824.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list