[vtkusers] Getting pointer to currently picked actor

Miro Drahos mdrahos at robodoc.com
Wed Aug 13 15:57:36 EDT 2014


Hi Maarten,
I would suggest creating a callback and registering it with the 
interactor's *ButtonPressEvent. The callback would manage its own 
picker. Something like:

//================================================
class PickCallback : public vtkCommand
{
public :
   static PickCallback * New() { return new PickCallback; }
   void SetRenderer(vtkRenderer * r) { this->ren = r; }
   void Execute(vtkObject *caller, unsigned long event, void *)
   {
     if (this->ren == NULL)
       return;

     if (event ==  vtkCommand::LeftButtonPressEvent)
       {
         vtkRenderWindowInteractor * rwi = 
vtkRenderWindowInteractor::SafeDownCast(caller);
         if (rwi)
           {
             this->picker->Pick(x, y, this->ren);
             vtkAssemblyPath * path = this->picker->GetPath();
             if (path)
               {
                 // Something was picked for sure.
                 vtkProp * pickedProp = path->GetLastNode()->GetViewProp();

                 // do something with the prop
               }
           }
       }
   }

private :
   PickCallback() {
     this->picker = vtkSmartPointer<vtkCellPicker>::New();
     this->ren    = NULL;
   }

   vtkRenderer                   * ren;
   vtkSmartPointer<vtkCellPicker>  picker;
};

//---------------------------------------------------------------------
// How to use the callback:
vtkSmartPointer<PickCallback> cbk = vtkSmartPointer<PickCallback>::New();
cbk->SetRenderer(ren);
interactor->AddObserver(vtkCommand::LeftButtonPressEvent, cbk);
//================================================

Hope this helps,
Miro



On 08/13/2014 09:29 AM, Maarten Beek via vtkusers wrote:
> Hi all,
>
> I noticed that getting the currently picked actor from the picker in 
> the interactor doesn't always work.
> Some interactor styles and widgets (e.g. 
> vtkInteractorstyleTrackballActor) have their own picker which is used 
> for the picking but which is different from the one in the interactor.
>
> I wrote a class that has awareness of the interactor.
> I would like to use this->Interactor->GetPicker() to get access to the 
> currently picked object. However this doesn't work with observers like 
> vtkInteractorstyleTrackballActor. Using 
> this->Interactor->GetInteractorStyle()->GetPicker() is also not an 
> option because not all observers use their own picker (and a 
> GetPicker() function does not exist in vtkInteractorstyleTrackballActor).
>
> The hack I made was to derive from vtkInteractorstyleTrackballActor 
> and overload the SetInteractor()  function:
>
> void MyClass::SetInteractor(vtkRenderWindowInteractor* rwi)
> {
> this->Superclass::SetInteractor(rwi);
>     if( this->Interactor != NULL )
>     {
>     vtkCellPicker* picker = vtkCellPicker::New;
> picker->SetTolerance(0.001);
> this->Interactor->SetPicker(picker);
>     picker->Delete();
>     }
> }
>
> This works. But I'd like to use MyClass with different observers w/o 
> having to derive from all of them...
>
> I've also looked at the PickingManager class since all observers seems 
> to register their pickers, but firstly it is not enabled by default 
> and secondly to get the current selection I have to call 
> GetAssemblyPath() which performs a pick which I don't want because 
> this invokes a PickEvent.. I use the event in MyClass but only when I 
> pick an object, not when I want the currently picked object. Giving 
> vtkPickingManager a public SelectedPicker() function (like
> vtkPickingManager::Internal has) would do the trick here..
>
> I guess my question is:
> How would a write code to get the currently picked object that works 
> with various observers when I only have awareness of the interactor?
>
> Maarten
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20140813/6699b150/attachment.html>


More information about the vtkusers mailing list