[vtkusers] how can I pick a cell and its neighbors dinamically!!!!

tasnim hanene-jeder at hotmail.fr
Wed Aug 15 15:30:35 EDT 2012


thanks David, 

David Doria-2-3 wrote
> 
> 
>  "but it didn't work :( " isn't is a very helpful clue for us. What 
> didn't work? Was there a compiler error? Was there a crash (if so, 
> which line?)? Was the output not what you expected? 
> 
Ok, when i execute my program , i visualize my mesh,but when i pick on it, i
got this message:
Unhandled exception at 0x001a250c in program.exe: 0xC0000005: Access
violation reading location 0xfffffffc.
I close it , and i didn't get neither picking nor error , and here is my
code:

class MouseInteractorStyle : public vtkInteractorStyleTrackballCamera
{
  public:
  static MouseInteractorStyle* New();
 
  MouseInteractorStyle()
  {
    selectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
    selectedActor = vtkSmartPointer<vtkActor>::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], 0, this->GetDefaultRenderer());
 
      double* worldPosition = picker->GetPickPosition();
      std::cout << "Cell id is: " << picker->GetCellId() << std::endl;
	  picker->GetCellId();

	    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());
 
        selectedMapper->SetInputConnection(
          selected->GetProducerPort());
 vtkSmartPointer<vtkPolyData> newdata = 
vtkSmartPointer<vtkPolyData>::New();
            newdata->SetPoints(selected->GetPoints());
             newdata->SetPolys(selected->GetCells());
            vtkIdType cellId=picker->GetCellId();
            vtkSmartPointer< vtkTriangleFilter > triangleFilter =            
vtkSmartPointer<vtkTriangleFilter>::New();
			triangleFilter->SetInputConnection(newdata->GetProducerPort());
			         
		   vtkSmartPointer<vtkIdList> cellPointIds =
         vtkSmartPointer<vtkIdList>::New();
         triangleFilter->GetOutput()->GetCellPoints(cellId, cellPointIds);

		   std::list<vtkIdType> neighbors;
 
  for(vtkIdType i = 0; i < cellPointIds->GetNumberOfIds(); i++)
    {
    vtkSmartPointer<vtkIdList> idList =
      vtkSmartPointer<vtkIdList>::New();
    idList->InsertNextId(cellPointIds->GetId(i));

    vtkSmartPointer<vtkIdList> neighborCellIds =
      vtkSmartPointer<vtkIdList>::New();
 
    triangleFilter->GetOutput()->GetCellNeighbors(cellId, idList,
neighborCellIds);
 
    for(vtkIdType j = 0; j < neighborCellIds->GetNumberOfIds(); j++)
      {
      neighbors.push_back(neighborCellIds->GetId(j));
      }
    }
  
       
this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(selectedActor);
        }
      vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
    }
    vtkSmartPointer<vtkPolyData> Data;
    vtkSmartPointer<vtkDataSetMapper> selectedMapper;
    vtkSmartPointer<vtkActor> selectedActor;
};
 
vtkStandardNewMacro(MouseInteractorStyle);
 
int main (int, char *[])
{
vtkSmartPointer<vtkSphereSource> sphereSource =
    vtkSmartPointer<vtkSphereSource>::New();
  planeSource->Update();
 
  vtkSmartPointer<vtkTriangleFilter> triangleFilter =
    vtkSmartPointer<vtkTriangleFilter>::New();
  triangleFilter->SetInputConnection(sphereSource->GetOutputPort());
  triangleFilter->Update();
 
  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(triangleFilter->GetOutputPort());
 
  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  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();
 
  vtkSmartPointer<MouseInteractorStyle> style =
    vtkSmartPointer<MouseInteractorStyle>::New();
  style->SetDefaultRenderer(renderer);
  style->Data = triangleFilter->GetOutput();
 
  renderWindowInteractor->SetInteractorStyle(style);
  renderer->AddActor(actor);
  renderer->ResetCamera();
  
  renderWindow->Render();
  renderWindowInteractor->Start();
 
  return EXIT_SUCCESS;
}




--
View this message in context: http://vtk.1045678.n5.nabble.com/how-can-I-pick-a-cell-and-its-neighbors-dinamically-tp5715282p5715296.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list