[vtkusers] vtkPointPicker Class
Amy Squillacote
amy.squillacote at kitware.com
Mon May 14 10:02:06 EDT 2007
Please be more specific than saying "it doesn't work". What doesn't
work? What behavior do you see, and what behavior do you expect?
- Amy
张涛 wrote:
> Hello
> I want to use the class vtkPointPicker to pick a point in a
> pointcloud,but it doesn't work
> Bellow is the snippet of my program,Please tell me what's the problem.
>
> #include "vtkActor.h"
> #include "vtkPolyData.h"
> #include "vtkUnstructuredGrid.h"
> #include "vtkRenderWindow.h"
> #include "vtkRenderer.h"
> #include "vtkPointPicker.h "
> #include "vtkRenderWindowInteractor.h"
> #include "vtkPolyDataMapper.h"
> #include "vtkSphereSource.h"
> #include "vtkPolyDataReader.h"
> #include "vtkPolyDataMapper.h "
> #include "vtkShrinkFilter.h"
> #include "vtkDataSetMapper.h"
> #include "vtkRenderWindowInteractor.h"
> #include "vtkIdList.h"
>
> static void PickCells(void *);
> static vtkActor *sphereActor;
> static vtkPolyData *plateOutput;
> static vtkUnstructuredGrid *cells;
> static vtkRenderWindow *renWin;
> static vtkActor *cellsActor, *plateActor;
>
> //#include "SaveImage.h"
>
> int main( int argc, char *argv[] )
> {
> vtkRenderer *renderer = vtkRenderer::New();
> renWin = vtkRenderWindow::New();
> renWin->AddRenderer(renderer);
>
> vtkPointPicker *picker = vtkPointPicker::New();
> picker->SetTolerance(0.01);
>
> vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
> iren->SetRenderWindow(renWin);
> iren->SetPicker(picker);
>
> // read data file
> // vtkPolyDataReader *plate = vtkPolyDataReader::New();
> // plate->SetFileName("../../../vtkdata/plate.vtk");
> //plate->DebugOn();
> int t,s;
> s = 0;
> FILE *fpread;
> float point[3];
> fpread = fopen("Inspection_Three.dat","r");
> vtkPolyData *PolyData = vtkPolyData::New();
> vtkPoints *pointsData = vtkPoints::New();
> vtkCellArray *PointsArray = vtkCellArray::New();
> while(!feof(fpread))
> {
> for(t = 0;t < 3;t++)
> {
> fscanf(fpread,"%f",&point[t]);
> }
> pointsData->InsertPoint(s,point[0],point[1],point[2]);
>
> PointsArray->InsertNextCell(1);
> PointsArray->InsertCellPoint(s);
>
> s++;
> }
>
> fclose(fpread);
> // PolyData->SetPoints(pointsData);
> // PolyData->SetVerts(PointsArray);
> // plateOutput = plate->GetOutput();
> plateOutput = vtkPolyData::New();
> plateOutput->SetPoints(pointsData);
> plateOutput->SetVerts(PointsArray);
> vtkPolyDataMapper *plateMapper = vtkPolyDataMapper::New();
> plateMapper->SetInput(plateOutput);
> plateActor = vtkActor::New();
> plateActor->SetMapper(plateMapper);
> plateActor->GetProperty()->SetColor(0.5000,0.5400,0.5300);
>
> // create marker for pick
> vtkSphereSource *sphere = vtkSphereSource::New();
> sphere->SetThetaResolution(8); sphere->SetPhiResolution(8);
> sphere->SetRadius(0.01);
> vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
> sphereMapper->SetInput(sphere->GetOutput());
> sphereActor = vtkActor::New();
> sphereActor->SetMapper(sphereMapper);
> sphereActor->GetProperty()->SetColor(1,0,0);
> sphereActor->PickableOff();
>
> // create actor and mapper to display picked cells
> cells = vtkUnstructuredGrid::New();
> vtkShrinkFilter *shrink = vtkShrinkFilter::New();
> shrink->SetInput(cells);
> shrink->SetShrinkFactor( 0.75);
> vtkDataSetMapper *cellsMapper = vtkDataSetMapper::New();
> cellsMapper->SetInput(shrink->GetOutput());
> cellsActor = vtkActor::New();
> cellsActor->SetMapper(cellsMapper);
> cellsActor->PickableOff();
> cellsActor->VisibilityOff();
> cellsActor->GetProperty()->SetColor(0.5000,0.5400,0.5300);
>
> renderer->AddActor(cellsActor);
> renderer->AddActor(plateActor);
> // renderer->AddActor(sphereActor);
> renderer->SetBackground(1,1,1);
> renderer->GetActiveCamera()->Elevation(30.0);
> renderer->GetActiveCamera()->Azimuth(30.0);
> renderer->GetActiveCamera()->Zoom(0.75);
>
> renWin->SetSize(300,300);
>
> // interact with data
> renWin->Render();
>
> // SAVEIMAGE( renWin );
>
> iren->SetEndPickMethod(PickCells,(void *)iren);
> iren->Start();
>
> // Clean up
> renderer->Delete();
> renWin->Delete();
> picker->Delete();
> iren->Delete();
> // plate->Delete();
> plateMapper->Delete();
> plateActor->Delete();
> sphere->Delete();
> sphereMapper->Delete();
> sphereActor->Delete();
> cells->Delete();
> shrink->Delete();
> cellsMapper->Delete();
> cellsActor->Delete();
> }
>
> static void PickCells(void *arg)
> {
> vtkRenderWindowInteractor *iren = (vtkRenderWindowInteractor *)arg;
> vtkPointPicker *picker = (vtkPointPicker *)iren->GetPicker();
> int i, cellId;
> vtkIdList *cellIds = vtkIdList::New(); cellIds->Allocate(12);
> vtkIdList *ptIds = vtkIdList::New(); ptIds->Allocate(12);
>
> sphereActor->SetPosition(picker->GetPickPosition());
>
> if ( picker->GetPointId() >= 0 )
> {
> cout << "Point id: " << picker->GetPointId() << "\n";
> cellsActor->VisibilityOn();
> plateActor->VisibilityOff();
>
> cells->Initialize();
> cells->Allocate(100);
> cells->SetPoints(plateOutput->GetPoints());
>
> plateOutput->GetPointCells(picker->GetPointId(), cellIds);
> for (i=0; i < cellIds->GetNumberOfIds(); i++)
> {
> cellId = cellIds->GetId(i);
> plateOutput->GetCellPoints(cellId, ptIds);
> cells->InsertNextCell(plateOutput->GetCellType(cellId), ptIds);
> }
> }
> else
> {
> cellsActor->VisibilityOff();
> plateActor->VisibilityOn();
> }
>
> renWin->Render();
> cellIds->Delete();
> ptIds->Delete();
> }
>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> This is the private VTK discussion list.
> Please keep messages on-topic. Check the FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
--
Amy Squillacote
Kitware, Inc.
28 Corporate Drive
Clifton Park, NY 12065
Phone: (518) 371-3971 x106
More information about the vtkusers
mailing list