[vtkusers] SphereSource as input to the TriangleFilter in a function!!!!!

tasnim hanene-jeder at hotmail.fr
Wed Aug 22 16:07:42 EDT 2012


David Doria-2-3 wrote
> 
> Just create a member variable in the InteractoryStyle class to store 
> the SphereSource pointer. Please provide the shortest possible, 
> compilable example if you cannot get it to work. 
> 
> David 
> 
ok, i tried your suggestion. But, surely, i have a mistake somewhere. when i
execute the program , i have an unhandled exception, the program stopped in
this line:
*// Get the location of the click (in window coordinates)
 int* pos = this->GetInteractor()->GetEventPosition();*

 this is my compilable code:
// Catch mouse events
class MouseInteractorStyle : public vtkInteractorStyleTrackballCamera
{
  public:
  static MouseInteractorStyle* New();
 
  MouseInteractorStyle()
  {
    selectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
    selectedActor = vtkSmartPointer<vtkActor>::New();
  }
 
    virtual void OnLeftButtonDown(vtkSmartPointer<vtkSphereSource> sphere)
    {
		vtkSmartPointer<vtkTriangleFilter> triangleFilter =
    vtkSmartPointer<vtkTriangleFilter>::New();
  triangleFilter->SetInputConnection(sphere->GetOutputPort());
  triangleFilter->Update();

      // Get the location of the click (in window coordinates):  the program
is blocked here
      int* pos = this->GetInteractor()->GetEventPosition();
 
      vtkSmartPointer<vtkCellPicker> picker =
        vtkSmartPointer<vtkCellPicker>::New();
      picker->SetTolerance(0.0005);
 
      // Pick from this location
      picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer());
 
      double* worldPosition = picker->GetPickPosition();
      std::cout << "Cell id is: " << picker->GetCellId() << std::endl;	 
	  vtkIdType cellId=picker->GetCellId();
	  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));
 
    //get the neighbors of the cell
    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));
      }
 
    }
 
  std::cout << "Point neighbor ids are: " << std::endl;
 
  for(std::list<vtkIdType>::iterator it1 = neighbors.begin(); it1 !=
neighbors.end(); it1++)
    {
    std::cout << " " << *it1;
    }
  std::cout << std::endl;
 
 
  vtkSmartPointer<vtkDataSetMapper> sphereMapper =
    vtkSmartPointer<vtkDataSetMapper>::New();
  sphereMapper->SetInputConnection(sphere->GetOutputPort());
  vtkSmartPointer<vtkActor> sphereActor =
    vtkSmartPointer<vtkActor>::New();
  sphereActor->SetMapper(sphereMapper);
  sphereActor->GetProperty()->SetEdgeColor(0, 0, 0);
  sphereActor->GetProperty()->EdgeVisibilityOn();
 
  vtkSmartPointer<vtkDataSetMapper> mainCellMapper =
    vtkSmartPointer<vtkDataSetMapper>::New();
 
  vtkSmartPointer<vtkDataSetMapper> neighborCellsMapper =
    vtkSmartPointer<vtkDataSetMapper>::New();
 
  // Create a dataset with the cell of interest
  {
    vtkSmartPointer<vtkIdTypeArray> ids =
        vtkSmartPointer<vtkIdTypeArray>::New();
    ids->SetNumberOfComponents(1);
    ids->InsertNextValue(cellId);
 
    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->SetInputConnection(0, sphere->GetOutputPort());

    extractSelection->Update();
 
    mainCellMapper->SetInputConnection(extractSelection->GetOutputPort());
 
  }
 
  vtkSmartPointer<vtkActor> mainCellActor =
    vtkSmartPointer<vtkActor>::New();
  mainCellActor->SetMapper(mainCellMapper);
  mainCellActor->GetProperty()->SetColor(1,0,0);
 
  {
    vtkSmartPointer<vtkIdTypeArray> ids =
        vtkSmartPointer<vtkIdTypeArray>::New();
    ids->SetNumberOfComponents(1);
    for(std::list<vtkIdType>::iterator it1 = neighbors.begin(); it1 !=
neighbors.end(); it1++)
      {
      ids->InsertNextValue(*it1);
      }
 
    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->SetInputConnection(0, sphere->GetOutputPort());
    extractSelection->Update();
   
neighborCellsMapper->SetInputConnection(extractSelection->GetOutputPort());
 
  }
  vtkSmartPointer<vtkActor> neighborCellsActor =
    vtkSmartPointer<vtkActor>::New();
  neighborCellsActor->SetMapper(neighborCellsMapper);
  neighborCellsActor->GetProperty()->SetColor(0,1,0);
 
       
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();
  sphereSource->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->GetProperty()->SetColor(0,1,0); //green
  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->OnLeftButtonDown(sphereSource);
  renderWindowInteractor->SetInteractorStyle(style);
  renderer->AddActor(actor);
  renderer->ResetCamera();
  renderer->SetBackground(0,0,1); // Blue
  renderWindow->Render();
  renderWindowInteractor->Start();
  return EXIT_SUCCESS;
}


thanks



--
View this message in context: http://vtk.1045678.n5.nabble.com/SphereSource-as-input-to-the-TriangleFilter-in-a-function-tp5715415p5715495.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list