[vtkusers] how to use MouseInteractorStyle in the qvtkWidget ???

Tasnim hanene-jeder at hotmail.fr
Sun Sep 16 10:10:53 EDT 2012


David Doria-2-3 wrote
> 
> On Sun, Sep 16, 2012 at 7:35 AM, Tasnim <hanene-jeder@> wrote:
> 
> As usual, a minimal example is the key to getting a response:
> http://www.vtk.org/Wiki/VTK/MinimalExample
> 
> 
ok, I have a QCombobox which contains a list of files names. when i choose
one of these names, i will display the my mesh in the qvtkWidget. this is my
simpleviewUI.cxx class :

#include "SimpleViewUI.h"
 
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkSphereSource.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkUnstructuredGrid.h>
#include <vtkCell.h>
#include <vtkCellArray.h>
#include <vtkIdList.h>
#include <vtkPointData.h>
#include <string>
#include <vtkPLYReader.h>
#include <vtkRendererCollection.h>
#include <vtkDataSetMapper.h>
#include <vtkIdTypeArray.h>
#include <vtkTriangleFilter.h>
#include <vtkPolyData.h>
#include <vtkPointPicker.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkProperty.h>
#include "vtkPLYReader.h"
 Constructor
SimpleView::SimpleView()
{
  this->setupUi(this);
 
  
  // Sphere
 
};
void SimpleView:: comboitem(QString a)
{
class MouseInteractorStyle : public vtkInteractorStyleTrackballCamera
{
  public:
  static MouseInteractorStyle* New();
  vtkSmartPointer<vtkPLYReader> reader ;
  std::string fichier;
  vtkSmartPointer<vtkRenderer> renderer;

  MouseInteractorStyle()
  {
    selectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
    selectedActor = vtkSmartPointer<vtkActor>::New();
	vtkSmartPointer<vtkUnstructuredGrid> selected =
          vtkSmartPointer<vtkUnstructuredGrid>::New();
	selected = vtkSmartPointer<vtkUnstructuredGrid>::New();
	selectedActor->SetMapper(selectedMapper);
}
virtual void OnLeftButtonDown()
    {


vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New(); 
polydata=reader->GetOutput();
int* pos = this->GetInteractor()->GetEventPosition();
 
  vtkSmartPointer<vtkPointPicker> picker =
        vtkSmartPointer<vtkPointPicker>::New();
 picker->Pick(pos[0], pos[1], pos[2], this->GetDefaultRenderer());
 
 double* worldPosition = picker->GetPickPosition();
	  vtkIdType pointId=picker->GetPointId();
	  if(pointId!= -1)
	  {
vtkSmartPointer<vtkSphereSource> sphereSource =
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->Update();		
vtkSmartPointer<vtkPolyDataMapper> mapper2 =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper2->SetInputConnection(sphereSource->GetOutputPort());
 
  vtkSmartPointer<vtkActor> actor2 =
    vtkSmartPointer<vtkActor>::New();
 // actor->GetProperty()->SetColor(0,1,0); //green
  actor2->SetMapper(mapper2);
  renderer->AddActor(actor2);
	};

       
this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(selectedActor);
 
      vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
    }
 
    vtkSmartPointer<vtkPolyData> Data;
    vtkSmartPointer<vtkDataSetMapper> selectedMapper;
    vtkSmartPointer<vtkActor> selectedActor;
	  vtkSmartPointer<vtkUnstructuredGrid> selected;
 
};
 vtkStandardNewMacro(MouseInteractorStyle);

  QString file="G:/qtvtk/"+a;
std::string filename = file.toStdString();  // type de filename est string
  vtkSmartPointer<vtkPLYReader> reader =
    vtkSmartPointer<vtkPLYReader>::New();
  reader->SetFileName(filename.c_str());
  reader->Update();
vtkSmartPointer<vtkPolyDataMapper> sphereMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  sphereMapper->SetInputConnection(reader->GetOutputPort());
  vtkSmartPointer<vtkActor> sphereActor =
    vtkSmartPointer<vtkActor>::New();
  sphereActor->SetMapper(sphereMapper);
 
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();

  // VTK Renderer
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(sphereActor);
 vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindowInteractor->Initialize();
 
  // Set the custom style to use for interaction.
  vtkSmartPointer<MouseInteractorStyle> style =
    vtkSmartPointer<MouseInteractorStyle>::New();
  style->SetDefaultRenderer(renderer);
//  style->Data = triangleFilter->GetOutput();
  style->reader=reader; 
  style->renderer=renderer;
  renderWindowInteractor->SetInteractorStyle(style);

 
this->qvtkWidget->GetRenderWindow()->SetInteractor(renderWindowInteractor);
  // VTK/Qt wedded
  this->qvtkWidget->GetRenderWindow()->AddRenderer(renderer);
  
  
 
  // Set up action signals and slots
  connect(this->actionExit, SIGNAL(triggered()), this, SLOT(slotExit()));
 
}

void SimpleView::slotExit()
{
  qApp->exit();
}


and thanks for replying. Hope to get the solution

----
Tasnim



--
View this message in context: http://vtk.1045678.n5.nabble.com/how-to-use-MouseInteractorStyle-in-the-qvtkWidget-tp5716110p5716112.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list