vtkWorldPointPicker.cxx 3.06 KB
Newer Older
Bill Lorensen's avatar
Bill Lorensen committed
1 2 3 4 5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkWorldPointPicker.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
Bill Lorensen's avatar
Bill Lorensen committed
9

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
Bill Lorensen's avatar
Bill Lorensen committed
13 14 15

=========================================================================*/
#include "vtkWorldPointPicker.h"
16 17

#include "vtkCamera.h"
Ken Martin's avatar
Ken Martin committed
18
#include "vtkCommand.h"
19 20
#include "vtkObjectFactory.h"
#include "vtkRenderer.h"
21

Ken Martin's avatar
Ken Martin committed
22
vtkCxxRevisionMacro(vtkWorldPointPicker, "1.21");
Brad King's avatar
Brad King committed
23
vtkStandardNewMacro(vtkWorldPointPicker);
24

Bill Lorensen's avatar
Bill Lorensen committed
25 26 27 28 29 30
vtkWorldPointPicker::vtkWorldPointPicker()
{
}

// Perform pick operation with selection point provided. The z location
// is recovered from the zBuffer. Always returns 0 since no actors are picked.
Ken Martin's avatar
Ken Martin committed
31 32
int vtkWorldPointPicker::Pick(double selectionX, double selectionY, 
                              double selectionZ, vtkRenderer *renderer)
Bill Lorensen's avatar
Bill Lorensen committed
33 34
{
  vtkCamera *camera;
Ken Martin's avatar
Ken Martin committed
35
  double cameraFP[4];
Ken Martin's avatar
Ken Martin committed
36 37
  double display[3], *world;
  double *displayCoord;
Ken Martin's avatar
Ken Martin committed
38
  double z;
Bill Lorensen's avatar
Bill Lorensen committed
39

Will Schroeder's avatar
Will Schroeder committed
40 41 42 43 44 45 46
  // Initialize the picking process
  this->Initialize();
  this->Renderer = renderer;
  this->SelectionPoint[0] = selectionX;
  this->SelectionPoint[1] = selectionY;
  this->SelectionPoint[2] = selectionZ;

47
  // Invoke start pick method if defined
Ken Martin's avatar
Ken Martin committed
48
  this->InvokeEvent(vtkCommand::StartPickEvent,NULL);
49

Jeff Lee's avatar
Jeff Lee committed
50 51 52 53 54 55 56 57
  z = renderer->GetZ ((int) selectionX, (int) selectionY);
  
  // if z is 1.0, we assume the user has picked a point on the
  // screen that has not been rendered into. Use the camera's focal
  // point for the z value. The test value .999999 has to be used
  // instead of 1.0 because for some reason our SGI Infinite Reality
  // engine won't return a 1.0 from the zbuffer
  if (z < 0.999999)
Bill Lorensen's avatar
Bill Lorensen committed
58
    {
Jeff Lee's avatar
Jeff Lee committed
59 60
    selectionZ = z;
    vtkDebugMacro(<< " z from zBuffer: " << selectionZ);
Bill Lorensen's avatar
Bill Lorensen committed
61 62 63
    }
  else
    {
Jeff Lee's avatar
Jeff Lee committed
64 65 66
    // Get camera focal point and position. Convert to display (screen) 
    // coordinates. We need a depth value for z-buffer.
    camera = renderer->GetActiveCamera();
Ken Martin's avatar
Ken Martin committed
67 68 69
    camera->GetFocalPoint(cameraFP); cameraFP[3] = 1.0;

    renderer->SetWorldPoint(cameraFP[0],cameraFP[1],cameraFP[2],cameraFP[3]);
Jeff Lee's avatar
Jeff Lee committed
70 71 72 73
    renderer->WorldToDisplay();
    displayCoord = renderer->GetDisplayPoint();
    selectionZ = displayCoord[2];
    vtkDebugMacro(<< "computed z from focal point: " << selectionZ);
Bill Lorensen's avatar
Bill Lorensen committed
74 75 76 77 78 79 80 81 82 83 84 85
    }

  // now convert the display point to world coordinates
  display[0] = selectionX;
  display[1] = selectionY;
  display[2] = selectionZ;

  renderer->SetDisplayPoint (display);
  renderer->DisplayToWorld ();
  world = renderer->GetWorldPoint ();
  
  for (int i=0; i < 3; i++) 
Bill Lorensen's avatar
Bill Lorensen committed
86
    {
Bill Lorensen's avatar
Bill Lorensen committed
87
    this->PickPosition[i] = world[i] / world[3];
Bill Lorensen's avatar
Bill Lorensen committed
88
    }
Bill Lorensen's avatar
Bill Lorensen committed
89

90
  // Invoke end pick method if defined
Ken Martin's avatar
Ken Martin committed
91
  this->InvokeEvent(vtkCommand::EndPickEvent,NULL);
92

Bill Lorensen's avatar
Bill Lorensen committed
93 94 95
  return 0;
}

96
void vtkWorldPointPicker::PrintSelf(ostream& os, vtkIndent indent)
Bill Lorensen's avatar
Bill Lorensen committed
97
{
Brad King's avatar
Brad King committed
98
  this->Superclass::PrintSelf(os,indent);
Bill Lorensen's avatar
Bill Lorensen committed
99
}