[vtkusers] vtkPropPicker abd Actor2D - Desperate

Zynique Schoeman zynique.schoeman at gmail.com
Thu Jul 28 03:09:37 EDT 2011


Good day

This is the first time I am trying to post here, so hope it works :)

I've been struggling with this problem for an entire week now :( and
REALLY need help. Basically I am trying to use vtkPropPicker to pick
an Actor2D, but it only picks the actor when you click on the bottom
left corner of the screen (thus around 0,0).

the strange thing is that vtkPropPicker works fine with objects like
vtkCaptionActor2D - which is derived from vtkActor2D - So, where am I
going wrong??

I have googled this problem and could find no solution. I did find one
post where someone had made an interactor for picking and dragging
Actor2D objects - but it had the exact same results.

I've attached some sample code of this for in case it might help...

--------------------------------------The
Interactor-----------------------------------
HEADER:
#ifndef vtkInteractorStyleDrager2DH
#define vtkInteractorStyleDrager2DH

#include "vtkInteractorStyle.h"

class vtkCellPicker;
class vtkPropPicker;
class vtkPicker;
class vtkPropAssembly;

class /*VTK_RENDERING_EXPORT*/ vtkInteractorStyleDrager2D : public
vtkInteractorStyle
{
public:
  static vtkInteractorStyleDrager2D *New();
  vtkTypeMacro(vtkInteractorStyleDrager2D, vtkInteractorStyle);
  //vtkTypeRevisionMacro(vtkInteractorStyleDrager2D,vtkInteractorStyle);
//  void PrintSelf(ostream& os, vtkIndent indent);

  void OnLeftButtonUp();
  void OnLeftButtonDown();
  void OnMouseMove();

/*
  void OnLeftButtonUp();
  void OnMiddleButtonDown();
  void OnMiddleButtonUp();
  void OnRightButtonDown();
  void OnRightButtonUp();
*/
  void Translate();

protected:
   vtkInteractorStyleDrager2D();
   ~vtkInteractorStyleDrager2D();

private:
  vtkInteractorStyleDrager2D(const vtkInteractorStyleDrager2D&);  //
Not implemented.
  void operator=(const vtkInteractorStyleDrager2D&);  // Not implemented.

  bool Picked;
  vtkPropPicker *InteractionPicker;
  vtkActor2D *InteractionActor2D;
  vtkPropAssembly *InteractionAssembly;
  int PosicionInicialX;
  int PosicionInicialY;
  double *PosicionActor;
  int PickInicialX;
  int PickInicialY;

};

//---------------------------------------------------------------------------
#endif

//---------------------------------------------------------------------------

----------------------------------------------------------------------------------------
AND:
-------------------------------------------------------------------------------------


#include "vtkInteractorStyleDrager2D.h"
#include "vtkObjectFactory.h"

#include "vtkPicker.h"
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include<vtkRenderWindow.h>

#include <vtkActor2D.h>
#include <vtkCellPicker.h>
#include <vtkPropPicker.h>
#include <vtkPicker.h>
#include <vtkAssembly.h>
#include <vtkAssemblyNode.h>
#include <vtkAssemblyPath.h>
#include <vtkPropAssembly.h>
#include <vtkPropCollection.h>



//vtkCxxRevisionMacro(vtkInteractorStyleDrager2D, "$Revision: 1.00 $");
//vtkStandardNewMacro(vtkInteractorStyleDrager2D);
//---------------------------------------------------------------------------
vtkInteractorStyleDrager2D::vtkInteractorStyleDrager2D()
{
  this->InteractionPicker = vtkPropPicker::New();
  //  this->InteractionPicker->PickFromListOn();
 // this->InteractionPicker->InitializePickList();
     // this->InteractionPicker->AddPickList( this->Prop );
//
  Picked = false;
  std::cout<<"ini"<<endl;
}
//---------------------------------------------------------------------------
vtkInteractorStyleDrager2D :: ~vtkInteractorStyleDrager2D()
{
  this->InteractionPicker->Delete();
}
//---------------------------------------------------------------------------
void vtkInteractorStyleDrager2D :: OnLeftButtonDown()
{
  int x, y;
//  Interactor->GetMousePosition(&x, &y);
  x = this->Interactor->GetEventPosition()[0];
  y = this->Interactor->GetEventPosition()[1];
  FindPokedRenderer(x, y);
  std::cout<<"LEFT BUTTON DOWN"<<endl;

  //vtkWin32OpenGLRenderWindow *RW= (vtkWin32OpenGLRenderWindow
*)this->CurrentRenderer->GetRenderWindow();

/*  if (RW)
    RW->Render();
*/
  this->InteractionPicker->Pick(x, y, 0.0, this->CurrentRenderer);
//  vtkProp *prop = this->InteractionPicker->GetProp();

  vtkAssemblyPath * Path;
  InteractionAssembly = NULL;
  vtkProp *prop = NULL;
  if (this->InteractionPicker->GetPropAssembly()) {
	  std::cout<<"sigh"<<endl;
//    Path = this->InteractionPicker->GetPath()->GetProp();
//    prop = Path->GetFirstNode()->GetProp();
    prop = this->InteractionPicker->GetPath()->GetFirstNode()->GetProp();
    InteractionAssembly = vtkPropAssembly::SafeDownCast(prop);
/*
    vtkPropCollection *col = Assemb->GetParts();
    col->InitTraversal();
    prop = col->GetNextProp();
    InteractionActor2D = vtkActor2D::SafeDownCast(prop);
    prop = col->GetLastProp();
    InteractionActor2D = vtkActor2D::SafeDownCast(prop); */
  }
  else{
//  if (this->InteractionPicker->GetProp())
	  prop = this->InteractionPicker->GetProp();}

  if (InteractionAssembly) {
	  std::cout<<"IF"<<endl ;
    Picked = true;
    vtkPropCollection *col = InteractionAssembly->GetParts();
    col->InitTraversal();
    InteractionActor2D = vtkActor2D::SafeDownCast(col->GetNextProp());
    PosicionActor = this->InteractionActor2D->GetPosition();
    PosicionInicialX = PosicionActor[0];
    PosicionInicialY = PosicionActor[1];
    PickInicialX = x;
    PickInicialY = y;
  } else{std::cout<<"else"<<endl;
    if (prop) {
		std::cout<<"if2"<<endl;
		prop->Print(std::cout);
      Picked = true;
      this->InteractionActor2D = vtkActor2D::SafeDownCast(prop);
//      PosicionActor =
this->InteractionActor2D->GetPositionCoordinate()->GetComputedWorldValue(this->CurrentRenderer);
      PosicionActor = this->InteractionActor2D->GetPosition();
      PosicionInicialX = PosicionActor[0];
      PosicionInicialY = PosicionActor[1];
      PickInicialX = x;
      PickInicialY = y;
	}
  if(!prop)
  {
	  std::cout<<":("<<endl;
  }}
  this->CurrentRenderer->GetRenderWindow()->Render();
}
//----------------------------------------------------------------------------
void vtkInteractorStyleDrager2D :: OnMouseMove()
{
  int x = this->Interactor->GetEventPosition()[0];
  int y = this->Interactor->GetEventPosition()[1];
  if (Picked) {
    int DeltaX = x - PickInicialX;
    int DeltaY = y - PickInicialY;
    if (InteractionAssembly) {
      vtkPropCollection *col = InteractionAssembly->GetParts();
      col->InitTraversal();
      vtkProp *prop = col->GetNextProp();
      while (prop) {
        InteractionActor2D = vtkActor2D::SafeDownCast(prop);
        this->InteractionActor2D->SetPosition(PosicionInicialX + DeltaX,
                                              PosicionInicialY + DeltaY);
        prop = col->GetNextProp();
      }
    } else
      this->InteractionActor2D->SetPosition(PosicionInicialX + DeltaX,
                                            PosicionInicialY + DeltaY);

  }
}
//----------------------------------------------------------------------------
void vtkInteractorStyleDrager2D :: OnLeftButtonUp()
{
  if (Picked) {
    Picked = false;
    int x = this->Interactor->GetEventPosition()[0];
    int y = this->Interactor->GetEventPosition()[1];
    int DeltaX = x - PickInicialX;
    int DeltaY = y - PickInicialY;
    if (InteractionAssembly) {
      vtkPropCollection *col = InteractionAssembly->GetParts();
      col->InitTraversal();
      vtkProp *prop = col->GetNextProp();
      while (prop) {
        InteractionActor2D = vtkActor2D::SafeDownCast(prop);
        this->InteractionActor2D->SetPosition(PosicionInicialX + DeltaX,
                                              PosicionInicialY + DeltaY);
        prop = col->GetNextProp();
      }
    } else
      this->InteractionActor2D->SetPosition(PosicionInicialX + DeltaX,
                                            PosicionInicialY + DeltaY);
//    this->InteractionActor2D->SetPosition(PosicionInicialX + DeltaX,
PosicionInicialY + DeltaY);
  }
  this->CurrentRenderer->GetRenderWindow()->Render();
}
//----------------------------------------------------------------------------
void vtkInteractorStyleDrager2D ::Translate()
{
}
//---------------------------------------------------------------------------



More information about the vtkusers mailing list