[vtkusers] How to obtain the coordinates (double and not int type) mouse picking over an image?

Dean Inglis inglis.dl at gmail.com
Fri Apr 22 15:13:39 EDT 2016


HI Georges,

you should see if the left mouse button actually clicked on your image
actor,
something like this:

  picker->PickFromListOn();
  picker->AddPickList(imageActor);
  picker->Pick( X, Y, 0.0, this->CurrentRenderer );
  vtkAssemblyPath* path = this->Picker->GetPath();

  vtkProp* pickedProp = 0;
  if( path  )
  {
    // Deal with the possibility that we may be using a shared picker
    vtkCollectionSimpleIterator sit;
    path->InitTraversal( sit );
    vtkAssemblyNode *node;
    for( int i = 0; i < path->GetNumberOfItems(); ++i )
    {
      node = path->GetNextNode( sit );
      pickedProp = node->GetViewProp();
      if( imageActor == pickedProp )
      {
        break;
      }
    }
  }

  if( !pickedProp ) return;
  double q[3];
  picker->GetPickPosition(q);

  What I do with picking on a vtkImageActor is to clamp the picked position
  to the bounds of the actor, then convert the clamped position
  from world coordinates back into image data voxel/pixel coordinate using
 the origin, spacing and extent of the vtkImageData that is being
 displayed by the actor.  You probably will need to NOT set the actor
  position: ImageActor->SetPosition(0,0,0);

  - Dean




On Fri, Apr 22, 2016 at 2:58 PM, Georges <Siqueiros.jorge at outlook.com>
wrote:

> Hi everybody, I'm new in vtk, this is my first vtk project. I'm working
> over
> C++ in VisualStudio with vtk 5.10.1 version. I'm making a SDI-MFC
> aplication
> and I'm trying to obtain the coordinates (image coordinates) of the left
> mouse click event when this click is over an image. I've reviewed the
> examples pickpixel, pickpixel2 and picking, but pickpixel2 gave me the
> coordiantes in int type variable and I need the coordinates in double type
> variable. I've implemented the picking example (link:
> http://www.vtk.org/Wiki/VTK/Examples/Cxx/Interaction/Picking) but when I
> run
> the app over my program I get an Assertion and I can´t find the error. I
> now
> attach my programm. I appreciate your help in explaining  how could I make
> the picking detection event or find the program mistake.
>
> *//
> *************************************************************************
> // FUNCTION TO LOAD AN IMAGE IN FORMATS *,jpg. *.bmp, *.tiff, *.png
> //
> **************************************************************************
> void COSSysView::OssysLoadImagevtk(CString m_ImagePath)
> {
>   //Create an image reader
>   vtkImageFileReader *ImageReader = vtkImageFileReader::New();
>   ImageReader->SetFileName(m_ImagePath);
>   ImageReader->Update();
>
>   // ###Create an image mapper
>   vtkImageMapper3D *imageMapper;
>   vtkImageSliceMapper *imageSMapper = vtkImageSliceMapper::New();
>   imageMapper = imageSMapper;
>   imageMapper->SetInputConnection(ImageReader->GetOutputPort());
>
>   // Create an actor
>   vtkImageActor *ImageActor = vtkImageActor::New();
>   ImageActor->SetInput(ImageReader->GetOutput());
>   ImageActor->SetPosition(0,0,0);
>   ImageActor->SetMapper(imageMapper);
>
>   GetRenderer()->RemoveAllViewProps(); // Remove all the actors
>
>   // Setup renderer
>   GetRenderer()->AddActor(ImageActor);
>   GetRenderer()->ResetCamera();
>
>   // Setup render window
>   GetRenderer()->GetRenderWindow()->Render();
>
>   // Setup render window interactor
>   vtkRenderWindowInteractor *renderWindowInteractor =
> vtkRenderWindowInteractor::New();
>   MouseInteractorStyle2        *style                      =
> MouseInteractorStyle2::New();
>
>   renderWindowInteractor->SetInteractorStyle(style);
>
>   // Render and start interaction
>
> renderWindowInteractor->SetRenderWindow(GetRenderer()->GetRenderWindow());
>   renderWindowInteractor->Initialize();
>   renderWindowInteractor->Start();
>
> }
> *//
> **************************************************************************
>
> *//
> *************************************************************************
> // CLASS TO HANDLE MOUSE EVENT
> //
> **************************************************************************
>
> class MouseInteractorStyle2 : public vtkInteractorStyleTrackballCamera
> {
>   public:
>     static MouseInteractorStyle2* New();
>     vtkTypeMacro(MouseInteractorStyle2, vtkInteractorStyleTrackballCamera);
>
>     virtual void OnLeftButtonDown()
>     {
>       int* clickPos = this->GetInteractor()->GetEventPosition();
>
>       // Pick from this location.
>       vtkSmartPointer<vtkPropPicker>  picker =
> vtkSmartPointer<vtkPropPicker>::New();
>       picker->Pick(clickPos[0], clickPos[1], 0,
> this->GetDefaultRenderer());
> // IN THIS PART THE ASSERTION APPEARS
>
>       double* pos = picker->GetPickPosition();
>       std::cout << "Pick position (world coordinates) is: "
>                 << pos[0] << " " << pos[1]
>                 << " " << pos[2] << std::endl;
>
>       std::cout << "Picked actor: " << picker->GetActor() << std::endl;
>
> //Create a sphere
>       vtkSmartPointer<vtkSphereSource> sphereSource =
> vtkSmartPointer<vtkSphereSource>::New();
>       sphereSource->SetCenter(pos[0], pos[1], pos[2]);
>       sphereSource->SetRadius(0.1);
>
>       //Create a mapper and actor
>       vtkSmartPointer<vtkPolyDataMapper> mapper =
> vtkSmartPointer<vtkPolyDataMapper>::New();
>       mapper->SetInputConnection(sphereSource->GetOutputPort());
>
>       vtkSmartPointer<vtkActor> actorsphere =
> vtkSmartPointer<vtkActor>::New();
>       actorsphere->SetMapper(mapper);
>       this->GetDefaultRenderer()->AddActor(actorsphere);
>       // Forward events
>       vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
>     }
>
> };
>
> vtkStandardNewMacro(MouseInteractorStyle2);
>
>
>
>
> --
> View this message in context:
> http://vtk.1045678.n5.nabble.com/How-to-obtain-the-coordinates-double-and-not-int-type-mouse-picking-over-an-image-tp5737850.html
> Sent from the VTK - Users mailing list archive at Nabble.com.
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160422/6e24f01a/attachment.html>


More information about the vtkusers mailing list