[vtkusers] how can I pick a cell and its neighbors dinamically!!!!
tasnim
hanene-jeder at hotmail.fr
Wed Aug 15 11:17:49 EDT 2012
Hi everybody,
I want to pick a cell and its neighbors. But the cellId will not be fixed
like in this example:
http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/CellPointNeighbors
http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/CellPointNeighbors . I
want that when i pick on the mesh, i will retrieve the cell and all its
neighbors in order to subdivide them then. I tried to modify that example in
order to do it, but i can't , any help please!!!!
Here is my code that i added it to the OnLeftButtonDown() method,but it
didn't work :( :
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.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;
picker->GetCellId();
if(picker->GetCellId() != -1)
{
vtkSmartPointer<vtkIdTypeArray> ids =
vtkSmartPointer<vtkIdTypeArray>::New();
ids->SetNumberOfComponents(1);
ids->InsertNextValue(picker->GetCellId());//identificateur des
arretes de la cellule
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();
// In selection
vtkSmartPointer<vtkUnstructuredGrid> selected =
vtkSmartPointer<vtkUnstructuredGrid>::New();
selected->ShallowCopy(extractSelection->GetOutput());
selectedMapper->SetInputConnection(
selected->GetProducerPort());
vtkSmartPointer<vtkPolyData> newdata =
vtkSmartPointer<vtkPolyData>::New();
newdata->SetPoints(selected->GetPoints());
// copy the cells
newdata->SetPolys(selected->GetCells());
vtkIdType cellId=picker->GetCellId();
vtkSmartPointer< vtkTriangleFilter > triangleFilter =
vtkSmartPointer<vtkTriangleFilter>::New();
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;
this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(selectedActor);
}
// Forward events
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
vtkSmartPointer<vtkPolyData> Data;
vtkSmartPointer<vtkDataSetMapper> selectedMapper;
vtkSmartPointer<vtkActor> selectedActor;
thanks,
Tasnim
--
View this message in context: http://vtk.1045678.n5.nabble.com/how-can-I-pick-a-cell-and-its-neighbors-dinamically-tp5715282.html
Sent from the VTK - Users mailing list archive at Nabble.com.
More information about the vtkusers
mailing list