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

tasnim hanene-jeder at hotmail.fr
Thu Aug 23 09:51:07 EDT 2012


David Doria-2-3 wrote
> 
> 
> 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
> 
I had make that line in my program (style->SetDefaultRenderer(renderer)),
 
David Doria-2-3 wrote
> 
> 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.
 sorry, i forgot to make the #include statements and this is the full
program:
#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyData.h>
#include <vtkIdList.h>
#include <vtkCell.h>
#include <vtkTriangleFilter.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkIdTypeArray.h>
#include <vtkSelectionNode.h>
#include <vtkSelection.h>
#include <vtkExtractSelection.h>
#include <vtkProperty.h>
#include <list>
#include <vtkRendererCollection.h>
#include <vtkUnstructuredGrid.h>
#include <vtkIdTypeArray.h>
#include <vtkPolyDataMapper.h>
#include <vtkCommand.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkCellPicker.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkSelectionNode.h>
#include <vtkSelection.h>
#include <vtkObjectFactory.h>
#include <vtkPropPicker.h>
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 again,



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



More information about the vtkusers mailing list