<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7652.24">
<TITLE>problem with picker->GetCellId() on vtkPolyData structure</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->
<P><FONT SIZE=2>Good morning<BR>
<BR>
I am using VTK 5.0 with Visual C++ 2005.<BR>
I wrote the following code to display 3D lines and pick them with the left button of the mouse:<BR>
<BR>
<BR>
// First, I create the renderer and the interactor<BR>
<BR>
vtkRenderer *globalRenderer3D = vtkRenderer::New();<BR>
vtkRenderWindow *globalRenWin3D = vtkRenderWindow::New();<BR>
globalRenWin3D->AddRenderer(globalRenderer3D);<BR>
<BR>
vtkRenderWindowInteractor *iren3D = vtkRenderWindowInteractor::New();<BR>
iren3D->SetRenderWindow(globalRenWin3D);<BR>
<BR>
// changing the interactor style <BR>
vtkInteractorStyleTrackballCamera *interactSwitch = vtkInteractorStyleTrackballCamera::New();<BR>
iren3D->SetInteractorStyle(interactSwitch);<BR>
<BR>
// function called every time the picker is activated<BR>
vtkCallbackCommand *callBackCom = vtkCallbackCommand::New();<BR>
callBackCom->SetCallback(pickingFunction);<BR>
<BR>
// linking the left button to the picking function<BR>
iren3D->AddObserver(vtkCommand::LeftButtonReleaseEvent, callBackCom);<BR>
<BR>
// creating the cell picker<BR>
vtkCellPicker *picker = vtkCellPicker::New();<BR>
iren3D->SetPicker(picker);<BR>
<BR>
<BR>
// Then I create a polydata and fill it:<BR>
<BR>
// acquiring the lines<BR>
for (int idStruct = 0; idStruct5 < nbStruct; idStruct5++)<BR>
{<BR>
//reading the number of lines for this structure<BR>
scanf("%d\n", &nbStructLines);<BR>
<BR>
// creating a polydata for each structures<BR>
vtkPolyData *polyLine = vtkPolyData::New();<BR>
polyLine->Allocate(nbStructLines, idStruct);<BR>
<BR>
for (int idLinesPerStruct = 0; idLinesPerStruct < nbStructLines; idLinesPerStruct++)<BR>
{<BR>
// reading the number of nodes for this line<BR>
scanf("%d\n", &nbLineNodes);<BR>
<BR>
// creating the structures for the points and lines of the current structure<BR>
vtkPoints *points = vtkPoints::New();<BR>
vtkPolyLine *lines = vtkPolyLine::New();<BR>
<BR>
// setting the number of nodes in the line<BR>
points->SetNumberOfPoints(nbLineNodes);<BR>
<BR>
// setting the number of indexes in the line lines->GetPointIds()->SetNumberOfIds(nbLineNodes);<BR>
<BR>
for (int idNodesLine = 0; idNodesLine <= nbLineNodes; idNodesLine++)<BR>
{<BR>
// reading the index and the coordinates of the current node<BR>
int id;<BR>
double x, y, z; <BR>
scanf("%d %f %f %f\n", &id, &x, &y, &z);<BR>
<BR>
// filling the node’s info<BR>
points->InsertPoint(idNodesLine, x, y, z);<BR>
<BR>
lines->GetPointIds()->SetId(idNodesLine, idNodesLine);<BR>
}<BR>
<BR>
// assigning the line just created in the polydata, after the already created lines<BR>
polyLine->SetPoints(points);<BR>
polyLine->InsertNextCell(lines->GetCellType(), lines->GetPointIds());<BR>
} <BR>
<BR>
// adding each complete polydata to a mapper and displaying it<BR>
vtkDataSetMapper *polyLineMapper = vtkDataSetMapper::New();<BR>
polyLineMapper->SetInput(polyLine);<BR>
<BR>
vtkActor *polyLineActor = vtkActor::New();<BR>
polyLineActor->SetMapper(polyLineMapper);<BR>
<BR>
globalRenderer3D->AddActor(skeletonPolyLineActor);<BR>
}<BR>
<BR>
// displaying the whole scene<BR>
globalRenWin3D->Render();<BR>
<BR>
<BR>
<BR>
And I have a function to deal with the picking event:<BR>
<BR>
void pickingFunction(vtkObject* obj, unsigned long, void*, void*)<BR>
{<BR>
vtkRenderWindowInteractor *iren = reinterpret_cast<vtkRenderWindowInteractor*>(obj);<BR>
<BR>
// checking if an event happened in the iren window<BR>
if (iren)<BR>
{<BR>
vtkCellPicker *picker = (vtkCellPicker *)iren->GetPicker();<BR>
<BR>
vtkInteractorStyleTrackballCamera *irenStyle= (vtkInteractorStyleTrackballCamera *)iren->GetInteractorStyle();<BR>
<BR>
iren->SetKeyCode(int ('p'));<BR>
irenStyle->OnChar();<BR>
<BR>
if (picker->GetCellId() >= 0 )<BR>
{<BR>
// assigning the values to the editing variables<BR>
int globalPickedLineId = picker->GetCellId();<BR>
vtkActor *globalPickedLineActor = vtkActor::New();<BR>
globalPickedLineActor = picker->GetActor();<BR>
}<BR>
<BR>
globalRenWin3D->Render();<BR>
} <BR>
}<BR>
<BR>
My problem is that whatever cell of the polydata I click on, the pointer picker->GetCellId() always returns 0!<BR>
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).<BR>
I tried to change my vtkPolyData into a vtkUnstructuredGrid, but same results…<BR>
I checked on the vtk website if someone else had the same problem but can not find something to help.<BR>
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?<BR>
Or is there a problem with the picker? Is it not set correctly? Should I not use the cell picker to pick a polydata?<BR>
<BR>
Thank you very much for your help,<BR>
<BR>
Vincent Luboz.<BR>
</FONT>
</P>
</BODY>
</HTML>