[vtkusers] Picking and Moving 2D Actors an Text

Emiliano Beronich emiliano at veccsa.com
Fri Sep 10 10:00:00 EDT 2004


Hi Andrew,

I have worked with picking and dragging 2D actors on VTK. I develop a 
class descendant from vtkInteractorStyle that detect a pick and moves 
the picked Actor. The files are attached. You can start from this point. 
But this is not the way I work at now. I found a better approach for 
dragging 2D actors like text or a cursor over an image that works well 
on Builder C++. I made a class descendant from the VCL family for text, 
cursors. The advantages are most fonts available, more efficient (it's 
only a 2D pick, the calcule needed is simple, totally different to a 3D 
pick). The routines for moving and picking are managed for the messages 
of Builder. You can try to emulate this behaviour on MFC.

HTH
Emiliano


Andrew Wilford wrote:
> Hi,
> 
> I have recently added 2D actors (simple shapes) and text to my viewer. I am
> having great difficulty in picking and moving these actors. The text does
> not even appear to be pickable and there is limited data on the bounding
> boxes of these actors, so that I could try to implement my own pick/move
> routine in MFC after detecting which actors are under the mouse pick point.
> 
> Can anyway point me in the right direction with this? Have there been recent
> additions to VTK which would help me?
> 
> Thanks,
> Andrew Wilford
> 
> _______________________________________________
> This is the private VTK discussion list. 
> Please keep messages on-topic. Check the FAQ at: <http://public.kitware.com/cgi-bin/vtkfaq>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
> 
> 
-------------- next part --------------
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    $RCSfile: vtkInteractorStyleDrager2D.h,v $
  Language:  C++

  Copyright (c) 2003 Emiliano Beronich
  All rights reserved.

=========================================================================*/

#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();
  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
-------------- next part --------------
//---------------------------------------------------------------------------

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

#include "vtkPicker.h"
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkWin32OpenGLRenderWindow.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();
  Picked = false;
}
//---------------------------------------------------------------------------
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);

  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()) {
//    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) {
    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
    if (prop) {
      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;
    }
  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);
    vtkWin32OpenGLRenderWindow  *RW= (vtkWin32OpenGLRenderWindow *) this->CurrentRenderer->GetRenderWindow();
    if (RW)
      RW->Render();
  }
}
//----------------------------------------------------------------------------
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()
{
}
//---------------------------------------------------------------------------

#pragma package(smart_init)


More information about the vtkusers mailing list