// Perform pick operation with selection point provided. Normally the
// first two values for the selection point are x-y pixel coordinate, and
// the third value is =0. Return non-zero if something was successfully picked.
int vtkPicker::Pick(double selectionX, double selectionY, double selectionZ,
vtkRenderer *renderer)
{
int i;
vtkProp *prop;
vtkCamera *camera;
vtkAbstractMapper3D *mapper = NULL;
double p1World[4], p2World[4], p1Mapper[4], p2Mapper[4];
int winSize[2] = {1, 1};
double x, y, t;
double *viewport;
double cameraPos[4], cameraFP[4];//相机的位置,焦点坐标
double *displayCoords, *worldCoords;
double *clipRange;//相机远近裁剪平面参数
double ray[3], rayLength;
int pickable;
int LODId;
double windowLowerLeft[4], windowUpperRight[4];
double bounds[6], tol;
double tF, tB;
double hitPosition[3];
double cameraDOP[3];//相机投影方向
bounds[0] = bounds[1] = bounds[2] = bounds[3] = bounds[4] = bounds[5] = 0;
// Initialize picking process
this->Initialize();//初始化当前vtkPicker,为接下来进行的pick操作做准备
this->Renderer = renderer;//
pick occurred in this renderer's viewport
this->SelectionPoint[0] = selectionX;
//selection point in window (pixel) coordinates
this->SelectionPoint[1] = selectionY;
this->SelectionPoint[2] = selectionZ;
// Invoke start pick method if defined
this->InvokeEvent(vtkCommand::StartPickEvent,NULL);//开始pick
if ( renderer == NULL )//如果renderer为null的话没法pick直接返回
{
vtkErrorMacro(<<"Must specify renderer!");
return 0;
}
// Get camera focal point and position. Convert to display (screen)
// coordinates. We need a depth value for z-buffer.
//
camera = renderer->GetActiveCamera();
camera->GetPosition(cameraPos);//获得相机位置
cameraPos[3] = 1.0;
camera->GetFocalPoint(cameraFP);//获得相机视点
cameraFP[3] = 1.0;
//以视点坐标为基准获取z buffer值
renderer->SetWorldPoint(cameraFP[0],cameraFP[1],cameraFP[2],cameraFP[3]);
renderer->WorldToDisplay();
displayCoords = renderer->GetDisplayPoint();
selectionZ = displayCoords[2];
// Convert the selection point into world coordinates.
//根据获得的z buffer值将selection point 转化为世界坐标系下的Pick Position
renderer->SetDisplayPoint(selectionX, selectionY, selectionZ);
renderer->DisplayToWorld();
worldCoords = renderer->GetWorldPoint();