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

David Doria daviddoria at gmail.com
Wed Aug 22 16:14:01 EDT 2012


On Wed, Aug 22, 2012 at 4:04 PM, tasnim <hanene-jeder at hotmail.fr> wrote:
>
>
> 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

I think you might be missing a line like :

style->SetDefaultRenderer(renderer);

(from this example:
http://www.vtk.org/Wiki/VTK/Examples/Cxx/Interaction/Picking ).

When you're stuck, I'd always suggest starting with one of the
examples (there is almost always an example very similar to what you
are trying to do), and then modifying it to morph it into the thing
you are trying to do.

I imagine what is happening is that GetInteractor() is returning NULL
(you should always check things for NULL if you are getting a crash).

Also, when you post code, "fully compilable" means we can
copy+paste+compile. You have not included the necessary #include
statements for that process to work.

David



More information about the vtkusers mailing list