[vtkusers] Can't execute point picking with following code

Luca Pallozzi Lavorante lplavorante at gmail.com
Tue Jul 10 07:33:35 EDT 2007


Dear Flo, I have had a similar problem and the solution I adopted may not be
elegant but it works. Use a static attribute ( i.e., "This", note the
uppercase "T") that you  can use in the callback. The attribute is a pointer
to a class PLUS. PLUS´s constructor would initialize it to "this" (something
like "This = this"). Another solution would be to make static all the
pointer attributes you need to use in your callback (iren ren, picker and so
on...). I also tried this second path but I found it is a lit more messy.

Hope this help

      Luca



On 7/10/07, Flo <snrf at no-log.org> wrote:
>
> Dear all:
>
> I'm using vtk 5.0 on MaxosX and with Qt 4.0.
>
> I'm trying to convert the annotatePick.cxx example (also attached),
> that works as a single app,  to something that would fit in a broader
> application (i.e. at least within classes and methods !).
> However, I can't handle the fact that the callback function must be
> static within the class while the callback function needs some
> private object of the class PLUS, the fact that the
> vtkCallbackCommand->SetCallback() method need a function pointer.
>
> So far, I have the following error:
>
> error: invalid use of member 'SimpleView::MouseMotion' in static
> member function ....... same for iren, ren and picker !
>
> Can someone point me to some directions or give me a better way to
> get pixel coordinates.
>
> Thanks.
>
> Flo.
>
> Code -
>
> Method that should start the callback
>
> void SimpleView::on_ButStart_clicked()
> {
>
>         MouseMotion = 0;
>
>         sphere = vtkSphereSource::New();
>
>      sphereMapper = vtkPolyDataMapper::New();
>      sphereMapper->SetInput(sphere->GetOutput());
>      sphereMapper->GlobalImmediateModeRenderingOn();
>
>         //VTKF.Load_Sphere *sphereMapper = new VTKF.Load_Sphere ();
>
>      sphereActor = vtkActor::New();
>      sphereActor->SetMapper(sphereMapper);
>
> // create the spikes by glyphing the sphere with a cone.  Create the
> mapper
> // and actor for the glyphs.
>      cone = vtkConeSource::New();
>      glyph = vtkGlyph3D::New();
>      glyph->SetInput(sphere->GetOutput());
>      glyph->SetSource(cone->GetOutput());
>      glyph->SetVectorModeToUseNormal();
>      glyph->SetScaleModeToScaleByVector();
>      glyph->SetScaleFactor(0.25);
>      vtkPolyDataMapper *spikeMapper = vtkPolyDataMapper::New();
>      spikeMapper->SetInput(glyph->GetOutput());
>      spikeActor = vtkActor::New();
>      spikeActor->SetMapper(spikeMapper);
>
>
> // Create a cell picker.
>
>         picker2 = vtkCellPicker::New();
>         pickObserver = PickCommand::New();
>
>         //pickObserver->SetPtr(/*picker2,*/ textActor, textMapper,
> renWin);
>
>      picker2->AddObserver( vtkCommand::EndPickEvent, pickObserver );
>
> // Create a text mapper and actor to display the results of picking.
>      textMapper = vtkTextMapper::New();
>      tprop = textMapper->GetTextProperty();
>      tprop->SetFontFamilyToArial();
>      tprop->SetFontSize(12);
>      tprop->BoldOn();
> //    tprop->ShadowOn();
>      tprop->SetColor(1, 0, 0);
>      textActor = vtkActor2D::New();
>      textActor->VisibilityOff();
>      textActor->SetMapper(textMapper);
>
> // Create the Renderer, RenderWindow, and RenderWindowInteractor
>
>     style = vtkInteractorStyleTrackballCamera::New();
>      pickerCommand = vtkCallbackCommand::New();
>      //pickerCommand->SetCallback(&PickerInteractionCallback);
>         pickerCommand->SetCallback(&Test);
>      style->AddObserver(vtkCommand::LeftButtonPressEvent,
> pickerCommand);
>      style->AddObserver(vtkCommand::MouseMoveEvent, pickerCommand);
>      style->AddObserver(vtkCommand::LeftButtonReleaseEvent,
> pickerCommand);
>
>
>      iren->SetInteractorStyle(style);
>      iren->SetPicker(picker2);
>
> // Add the actors to the renderer, set the background and size
>
>      ren->AddActor2D(textActor);
>
>      ren->AddActor(sphereActor);
>      ren->AddActor(spikeActor);
>      ren->SetBackground(1, 1, 1);
>
>
>         ren->AddActor(sphereActor);
>      ren->AddActor(spikeActor);
>
>         renWin->Render();
>
>
>
>
>
> }
>
> ------------------------------------------
> -----------------------------------------
>
> Header file
>
> class SimpleView : public QMainWindow, private Ui_MainWindow
> {
>      Q_OBJECT
>
> static void Test(vtkObject* vtkNotUsed(object),unsigned long
> event,void* clientdata, void* vtkNotUsed(calldata))
> {
>
>    vtkInteractorStyleTrackballCamera * style =
> (vtkInteractorStyleTrackballCamera*)clientdata;
>
>
>     switch( event )
>      {
>      case vtkCommand::LeftButtonPressEvent:
>          MouseMotion = 0;
>          style->OnLeftButtonDown();
>          break;
>      case vtkCommand::LeftButtonReleaseEvent:
>          if (MouseMotion == 0)
>          {
>              int *pick = iren->GetEventPosition();
>              picker2->Pick((double)pick[0], (double)pick[1], 0.0, ren);
>          }
>          style->OnLeftButtonUp();
>          break;
>      case vtkCommand::MouseMoveEvent:
>          MouseMotion = 1;
>          style->OnMouseMove();
>          break;
>      }
>
>
> }
>
> public:
>
>      // Constructor/Destructor
>      SimpleView(QWidget* parent = 0);
>      ~SimpleView() {};
>
> public slots:
>         //virtual void vtkTest();
>
> private slots:
>         void on_ButStart_clicked();
>
> private:
>       vtkSphereSource* sphere;
>          vtkPolyDataMapper *sphereMapper, *spikeMapper;
>          vtkActor *sphereActor, *spikeActor;
>          vtkConeSource* cone;
>          vtkGlyph3D* glyph;
>
>          PickCommand* pickObserver;
>          vtkTextProperty *tprop;
>          vtkInteractorStyleTrackballCamera *style;
>          vtkCallbackCommand *pickerCommand;
>
>         vtkActor2D *textActor;
>         vtkTextMapper *textMapper;
>
>         vtkRenderWindow *renWin;
>
>         vtkCamera *cam1;
>
>         int MouseMotion;
>      vtkRenderWindowInteractor *iren;
>      vtkRenderer *ren;
>      vtkCellPicker *picker2;
>
>
> };
>
>
>
> class PickCommand : public vtkCommand
> {
>
> public:
>      static PickCommand *New() { return new PickCommand; }
>      void Delete() { delete this; }
>
>
>      virtual void Execute(vtkObject *caller, unsigned long l, void
> *callData)
>      {
>          if (picker->GetCellId() < 0 )
>          {
>              textActor->VisibilityOff();
>          }
>          else
>          {
>              double selpt[3];
>              picker->GetSelectionPoint(selpt);
>              double x = selpt[0];
>              double y = selpt[1];
>              double pickPos[3];
>              picker->GetPickPosition( pickPos );
>              double xp = pickPos[0];
>              double yp = pickPos[1];
>              double zp = pickPos[2];
>
>              char text[120];
>              sprintf( text, "(%5.5f,  %5.5f,  %5.5f)", xp, yp, zp );
>              textMapper->SetInput( text );
>              textActor->SetPosition(x, y);
>              textActor->VisibilityOn();
>          }
>
>          renWin->Render(); // faire un pointeur de meme que pour
> picker !!!!
>
>
>      }
>
>         void SetPtr(vtkCellPicker *pickerptr, vtkActor2D *textActorptr,
> vtkTextMapper *textMapperptr, vtkRenderWindow *renWinptr)
>         {
>                 picker = pickerptr;
>                 textActor = textActorptr;
>                 textMapper = textMapperptr;
>                 renWin = renWinptr;
>         }
>
>         private:
>                 vtkCellPicker *picker;
>                 vtkActor2D *textActor;
>                 vtkTextMapper *textMapper;
>                 vtkRenderWindow *renWin;
>                 vtkRenderWindowInteractor *iren;
> };
>
> --------------------------------
> -------------------------------------
>
> Annotate EXAMPLE
>
> #include "vtkSphereSource.h"
> #include "vtkPolyDataMapper.h"
> #include "vtkLODActor.h"
> #include "vtkConeSource.h"
> #include "vtkGlyph3D.h"
> #include "vtkCellPicker.h"
> #include "vtkTextMapper.h"
> #include "vtkActor2D.h"
> #include "vtkInteractorStyleTrackballCamera.h"
> #include "vtkRenderer.h"
> #include "vtkRenderWindow.h"
> #include "vtkRenderWindowInteractor.h"
> #include "vtkTextProperty.h"
> #include "vtkCallbackCommand.h"
> #include "vtkCamera.h"
>
> int MouseMotion;
> vtkRenderer *ren1;
> vtkRenderWindow *renWin;
> vtkRenderWindowInteractor *iren;
> vtkCellPicker *picker;
> vtkActor2D *textActor;
> vtkTextMapper *textMapper;
>
> class PickCommand : public vtkCommand
> {
> public:
>
>      static PickCommand *New() { return new PickCommand; }
>      void Delete() { delete this; }
>
>      virtual void Execute(vtkObject *caller, unsigned long l, void
> *callData)
>      {
>          if (picker->GetCellId() < 0 )
>          {
>              textActor->VisibilityOff();
>          }
>          else
>          {
>              double selpt[3];
>              picker->GetSelectionPoint(selpt);
>              double x = selpt[0];
>              double y = selpt[1];
>              double pickPos[3];
>              picker->GetPickPosition( pickPos );
>              double xp = pickPos[0];
>              double yp = pickPos[1];
>              double zp = pickPos[2];
>
>              char text[120];
>              sprintf( text, "(%5.5f,  %5.5f,  %5.5f)", xp, yp, zp );
>              textMapper->SetInput( text );
>              textActor->SetPosition(x, y);
>              textActor->VisibilityOn();
>          }
>
>          renWin->Render();
>      }
> };
>
> void PickerInteractionCallback( vtkObject* vtkNotUsed(object),
>                                         unsigned long event,
>                                         void* clientdata,
>                                         void* vtkNotUsed(calldata) )
> {
>      vtkInteractorStyleTrackballCamera * style =
> (vtkInteractorStyleTrackballCamera*)clientdata;
>      switch( event )
>      {
>      case vtkCommand::LeftButtonPressEvent:
>          MouseMotion = 0;
>          style->OnLeftButtonDown();
>          break;
>      case vtkCommand::LeftButtonReleaseEvent:
>          if (MouseMotion == 0)
>          {
>              int *pick = iren->GetEventPosition();
>              picker->Pick((double)pick[0], (double)pick[1], 0.0, ren1);
>          }
>          style->OnLeftButtonUp();
>          break;
>      case vtkCommand::MouseMoveEvent:
>          MouseMotion = 1;
>          style->OnMouseMove();
>          break;
>      }
> }
>
>
> int main(int argc, char* argv)
> {
>      MouseMotion = 0;
>      vtkSphereSource *sphere = vtkSphereSource::New();
>
>      vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
>      sphereMapper->SetInput(sphere->GetOutput());
>      sphereMapper->GlobalImmediateModeRenderingOn();
>      vtkLODActor *sphereActor = vtkLODActor::New();
>      sphereActor->SetMapper(sphereMapper);
>
> // create the spikes by glyphing the sphere with a cone.  Create the
> mapper
> // and actor for the glyphs.
>      vtkConeSource *cone = vtkConeSource::New();
>      vtkGlyph3D *glyph = vtkGlyph3D::New();
>      glyph->SetInput(sphere->GetOutput());
>      glyph->SetSource(cone->GetOutput());
>      glyph->SetVectorModeToUseNormal();
>      glyph->SetScaleModeToScaleByVector();
>      glyph->SetScaleFactor(0.25);
>      vtkPolyDataMapper *spikeMapper = vtkPolyDataMapper::New();
>      spikeMapper->SetInput(glyph->GetOutput());
>      vtkLODActor *spikeActor = vtkLODActor::New();
>      spikeActor->SetMapper(spikeMapper);
>
> // Create a cell picker.
>      PickCommand* pickObserver = PickCommand::New();
>      picker = vtkCellPicker::New();
>      picker->AddObserver( vtkCommand::EndPickEvent, pickObserver );
>
> // Create a text mapper and actor to display the results of picking.
>      textMapper = vtkTextMapper::New();
>      vtkTextProperty *tprop = textMapper->GetTextProperty();
>      tprop->SetFontFamilyToArial();
>      tprop->SetFontSize(12);
>      tprop->BoldOn();
> //    tprop->ShadowOn();
>      tprop->SetColor(1, 0, 0);
>      textActor = vtkActor2D::New();
>      textActor->VisibilityOff();
>      textActor->SetMapper(textMapper);
>
> // Create the Renderer, RenderWindow, and RenderWindowInteractor
>
>      vtkInteractorStyleTrackballCamera *style =
> vtkInteractorStyleTrackballCamera::New();
>      vtkCallbackCommand * pickerCommand = vtkCallbackCommand::New();
>      pickerCommand->SetClientData(style);
>      pickerCommand->SetCallback(&PickerInteractionCallback);
>      style->AddObserver(vtkCommand::LeftButtonPressEvent,
> pickerCommand);
>      style->AddObserver(vtkCommand::MouseMoveEvent, pickerCommand);
>      style->AddObserver(vtkCommand::LeftButtonReleaseEvent,
> pickerCommand);
>      ren1 = vtkRenderer::New();
>      renWin = vtkRenderWindow::New();
>      renWin->AddRenderer(ren1);
>      iren = vtkRenderWindowInteractor::New();
>      iren->SetRenderWindow(renWin);
>      iren->SetInteractorStyle(style);
>      iren->SetPicker(picker);
>
> // Add the actors to the renderer, set the background and size
>
>      ren1->AddActor2D(textActor);
>      ren1->AddActor(sphereActor);
>      ren1->AddActor(spikeActor);
>      ren1->SetBackground(1, 1, 1);
>      renWin->SetSize(300, 300);
>
> // Get the camera and zoom in closer to the image.
>      vtkCamera *cam1 = ren1->GetActiveCamera();
>      cam1->Zoom(1.4);
>
>      iren->Initialize();
>      iren->Start();
>
>      picker->RemoveObserver( pickObserver );
>      sphere->Delete();
>      sphereMapper->Delete();
>      sphereActor->Delete();
>      cone->Delete();
>      glyph->Delete();
>      spikeMapper->Delete();
>      spikeActor->Delete();
>      picker->Delete();
>      textMapper->Delete();
>      textActor->Delete();
>      pickerCommand->Delete();
>      style->Delete();
>      ren1->Delete();
>      renWin->Delete();
>      pickObserver->Delete();
> //    iren->Delete();
> }
>
>
> _______________________________________________
> This is the private VTK discussion list.
> Please keep messages on-topic. Check the FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20070710/b6d03351/attachment.htm>


More information about the vtkusers mailing list