[vtkusers] Cell Picking -> Deletion -> Updating... Problem with new id's

matheus_viana vianamp at gmail.com
Fri Jun 12 06:17:39 EDT 2015


Hi guys. I am writing a little piece of code to edit a network stored as a
PolyData. For now I want to be able of selecting a bunch of cells, delete
them and update the network on screen. To do so, I adapted the code shown in
this example http://www.vtk.org/Wiki/VTK/Examples/Cxx/Picking/CellPicking.
Everything is working great so far except for one thing. After deleting the
first batch of cells, I can't figure out how to update the cells id's so
that next time I select new cells, I will have the new id's.

If you want to download the code+CMakeList+DataFile, please do that using
the following link:

https://www.dropbox.com/s/uyqx07jiy7540vp/CellPicking.zip?dl=0

After running the code, please use left click to select a cell and press "d"
to mark the cell for deletion. After selecting all the cells you want, press
"p" to delete them. If you repeat the process, you will see my problem: the
cells that are going to be deleted are not the same you selected.

Thanks a lot,

#include <list>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <unistd.h>
#include <vtkSmartPointer.h>
#include <vtkRendererCollection.h>
#include <vtkDataSetMapper.h>
#include <vtkUnstructuredGrid.h>
#include <vtkIdTypeArray.h>
#include <vtkTriangleFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkCommand.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyData.h>
#include <vtkPolyDataReader.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkPlaneSource.h>
#include <vtkCellPicker.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkProperty.h>
#include <vtkSelectionNode.h>
#include <vtkSelection.h>
#include <vtkCallbackCommand.h>
#include <vtkExtractSelection.h>
#include <vtkObjectFactory.h>

vtkPolyData *PolyData;

vtkIdType _selected_cell;
double _selected_coordinate[3];

std::vector<vtkIdType> CellsToDelete;
std::vector<vtkIdType> CellsToBreak;
std::vector<double*> CoordinatesToBreak;

void KeypressCallbackFunction (vtkObject* caller, long unsigned int eventId,
void* clientData, void* callData);

// Catch mouse events
class MouseInteractorStyle : public vtkInteractorStyleTrackballCamera {

  public:

    static MouseInteractorStyle* New();

    MouseInteractorStyle() {
      selectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
      selectedActor = vtkSmartPointer<vtkActor>::New();
    }

    virtual void OnLeftButtonDown() {

      // Get the location of the click (in window coordinates)
      int* pos = this->GetInteractor()->GetEventPosition();

      vtkSmartPointer<vtkCellPicker> picker =
vtkSmartPointer<vtkCellPicker>::New();
      picker->SetTolerance(0.01);
  
      // Pick from this location.
      picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer());

      // Scale factor
      pos[0] *= 0.056; pos[1] *= 0.056; pos[2] *= 0.200;

      double* worldPosition = picker->GetPickPosition();
      std::cout << "Cell id is: " << picker->GetCellId() << std::endl;

      if(picker->GetCellId() != -1) {
      
        _selected_cell = picker->GetCellId();
        _selected_coordinate[0] = pos[0]; _selected_coordinate[1] = pos[1];
_selected_coordinate[2] = pos[2];

        std::cout << "Pick position is: " << worldPosition[0] << " " <<
worldPosition[1]
                  << " " << worldPosition[2] << endl;
                  
        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->SetInputData(0, this->Data);
        extractSelection->SetInputData(1, selection);
        extractSelection->Update();

        // In selection
        vtkSmartPointer<vtkUnstructuredGrid> selected =
vtkSmartPointer<vtkUnstructuredGrid>::New();
        selected->ShallowCopy(extractSelection->GetOutput());

        std::cout << "There are " << selected->GetNumberOfPoints()
                  << " points in the selection." << std::endl;
        std::cout << "There are " << selected->GetNumberOfCells()
                  << " cells in the selection." << std::endl;


        selectedMapper->SetInputData(selected);
     
        selectedActor->SetMapper(selectedMapper);
        selectedActor->GetProperty()->EdgeVisibilityOn();
        selectedActor->GetProperty()->SetEdgeColor(1,0,0);
        selectedActor->GetProperty()->SetLineWidth(3);
        
       
this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(selectedActor);
        
        }
      // Forward events
      vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
    }

    vtkSmartPointer<vtkPolyData> Data;
    vtkSmartPointer<vtkDataSetMapper> selectedMapper;
    vtkSmartPointer<vtkActor> selectedActor;

};

vtkStandardNewMacro(MouseInteractorStyle);

int main (int, char *[]) {
  
  vtkSmartPointer<vtkPolyDataReader> Reader =
vtkSmartPointer<vtkPolyDataReader>::New();
  Reader -> SetFileName("../Skell.vtk");
  Reader -> Update();

  PolyData = Reader -> GetOutput();

  vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputData(PolyData);
  mapper->ScalarVisibilityOff();

  vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
  actor->GetProperty()->SetColor(0,1,0);
  actor->SetMapper(mapper);

  vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);

  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindowInteractor->Initialize();

  // Set the custom stype to use for interaction.
  vtkSmartPointer<MouseInteractorStyle> style =
vtkSmartPointer<MouseInteractorStyle>::New();
  style->SetDefaultRenderer(renderer);
  style->Data = PolyData;

  vtkSmartPointer<vtkCallbackCommand> keypressCallback =
vtkSmartPointer<vtkCallbackCommand>::New();
  keypressCallback->SetCallback ( KeypressCallbackFunction );

  renderWindowInteractor->SetInteractorStyle(style);
  renderWindowInteractor->AddObserver ( vtkCommand::KeyPressEvent,
keypressCallback );

  renderer->AddActor(actor);
  renderer->ResetCamera();

  renderer->SetBackground(0,0,0);

  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

void KeypressCallbackFunction ( vtkObject* caller, long unsigned int
vtkNotUsed(eventId), void* vtkNotUsed(clientData), void*
vtkNotUsed(callData) ) {
  std::cout << "Keypress callback" << std::endl;
  vtkRenderWindowInteractor *iren =
static_cast<vtkRenderWindowInteractor*>(caller);
  std::cout << "Pressed: " << iren->GetKeySym() << std::endl;

  // Key "d": adds the selected cell to the deletion list
  if (!strcmp("d",iren->GetKeySym())) {
    CellsToDelete.push_back(_selected_cell);
  }

  // KEY [p]: deletes the cells stored in the deletion list
  if (!strcmp("p",iren->GetKeySym())) {
    printf("Delete these cells:\n");
    for (int c = 0; c < CellsToDelete.size(); c++) {
      printf("\t%d\n",(int)CellsToDelete[c]);

      PolyData -> BuildLinks();
      PolyData -> DeleteCell(CellsToDelete[c]);
      PolyData -> RemoveDeletedCells();
      PolyData -> Modified();

    }
  }
}





--
View this message in context: http://vtk.1045678.n5.nabble.com/Cell-Picking-Deletion-Updating-Problem-with-new-id-s-tp5732305.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list