[vtkusers] problem with picker->GetCellId() on vtkPolyData structure
Luboz, Vincent
v.luboz at imperial.ac.uk
Wed Sep 12 05:35:22 EDT 2007
Good morning
I am using VTK 5.0 with Visual C++ 2005.
I wrote the following code to display 3D lines and pick them with the left button of the mouse:
// First, I create the renderer and the interactor
vtkRenderer *globalRenderer3D = vtkRenderer::New();
vtkRenderWindow *globalRenWin3D = vtkRenderWindow::New();
globalRenWin3D->AddRenderer(globalRenderer3D);
vtkRenderWindowInteractor *iren3D = vtkRenderWindowInteractor::New();
iren3D->SetRenderWindow(globalRenWin3D);
// changing the interactor style
vtkInteractorStyleTrackballCamera *interactSwitch = vtkInteractorStyleTrackballCamera::New();
iren3D->SetInteractorStyle(interactSwitch);
// function called every time the picker is activated
vtkCallbackCommand *callBackCom = vtkCallbackCommand::New();
callBackCom->SetCallback(pickingFunction);
// linking the left button to the picking function
iren3D->AddObserver(vtkCommand::LeftButtonReleaseEvent, callBackCom);
// creating the cell picker
vtkCellPicker *picker = vtkCellPicker::New();
iren3D->SetPicker(picker);
// Then I create a polydata and fill it:
// acquiring the lines
for (int idStruct = 0; idStruct5 < nbStruct; idStruct5++)
{
//reading the number of lines for this structure
scanf("%d\n", &nbStructLines);
// creating a polydata for each structures
vtkPolyData *polyLine = vtkPolyData::New();
polyLine->Allocate(nbStructLines, idStruct);
for (int idLinesPerStruct = 0; idLinesPerStruct < nbStructLines; idLinesPerStruct++)
{
// reading the number of nodes for this line
scanf("%d\n", &nbLineNodes);
// creating the structures for the points and lines of the current structure
vtkPoints *points = vtkPoints::New();
vtkPolyLine *lines = vtkPolyLine::New();
// setting the number of nodes in the line
points->SetNumberOfPoints(nbLineNodes);
// setting the number of indexes in the line lines->GetPointIds()->SetNumberOfIds(nbLineNodes);
for (int idNodesLine = 0; idNodesLine <= nbLineNodes; idNodesLine++)
{
// reading the index and the coordinates of the current node
int id;
double x, y, z;
scanf("%d %f %f %f\n", &id, &x, &y, &z);
// filling the nodes info
points->InsertPoint(idNodesLine, x, y, z);
lines->GetPointIds()->SetId(idNodesLine, idNodesLine);
}
// assigning the line just created in the polydata, after the already created lines
polyLine->SetPoints(points);
polyLine->InsertNextCell(lines->GetCellType(), lines->GetPointIds());
}
// adding each complete polydata to a mapper and displaying it
vtkDataSetMapper *polyLineMapper = vtkDataSetMapper::New();
polyLineMapper->SetInput(polyLine);
vtkActor *polyLineActor = vtkActor::New();
polyLineActor->SetMapper(polyLineMapper);
globalRenderer3D->AddActor(skeletonPolyLineActor);
}
// displaying the whole scene
globalRenWin3D->Render();
And I have a function to deal with the picking event:
void pickingFunction(vtkObject* obj, unsigned long, void*, void*)
{
vtkRenderWindowInteractor *iren = reinterpret_cast<vtkRenderWindowInteractor*>(obj);
// checking if an event happened in the iren window
if (iren)
{
vtkCellPicker *picker = (vtkCellPicker *)iren->GetPicker();
vtkInteractorStyleTrackballCamera *irenStyle= (vtkInteractorStyleTrackballCamera *)iren->GetInteractorStyle();
iren->SetKeyCode(int ('p'));
irenStyle->OnChar();
if (picker->GetCellId() >= 0 )
{
// assigning the values to the editing variables
int globalPickedLineId = picker->GetCellId();
vtkActor *globalPickedLineActor = vtkActor::New();
globalPickedLineActor = picker->GetActor();
}
globalRenWin3D->Render();
}
}
My problem is that whatever cell of the polydata I click on, the pointer picker->GetCellId() always returns 0!
I checked picker->GetSubId() and it changes. It actually gives me the index of the line in the polydata fine. For example, if I click on the 3rd line of the 2nd polydata having 4 lines, picker->GetCellId() will give me 0 (it should be 1) and picker->GetSubId() will tell me 2 (which is correct).
I tried to change my vtkPolyData into a vtkUnstructuredGrid, but same results
I checked on the vtk website if someone else had the same problem but can not find something to help.
Am I allocating the vtkPolyData the wrong way? Should I also give each polydata an index? (like I am doing for vtkPoints and vtkPolyLines?) Should I not use InsertNextCell for each new line in the vtkPolyData? But then what should I use instead?
Or is there a problem with the picker? Is it not set correctly? Should I not use the cell picker to pick a polydata?
Thank you very much for your help,
Vincent Luboz.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20070912/b11972f8/attachment.htm>
More information about the vtkusers
mailing list