[vtkusers] How to forward mouse move event to polydata algorithm and get there direction of projection

Jochen K. jochen.kling at email.de
Wed Jun 20 06:47:34 EDT 2012


Hi Jana,

much better!

Here we go:

#include <vtkVersion.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyData.h>
#include <vtkSphereSource.h>
#include <vtkOrientationMarkerWidget.h>
#include <vtkAxesActor.h>
#include <vtkPropAssembly.h>
#include <vtkSmartPointer.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCommand.h>
#include <vtkCamera.h>
#include <vtkCallbackCommand.h>
#include <vtkObjectFactory.h>
#include <vtkMatrix4x4.h>

class MySphereSource : public vtkSphereSource {
public:
  vtkTypeMacro(MySphereSource, vtkSphereSource);
  static MySphereSource* New(); 
  
  void MySphereSource::printDirectionOfProjection(double* dirOfProj)
  {
    cout << "direction of view: " << dirOfProj[0] << " "
<< dirOfProj[1] << " " << dirOfProj[2] << endl;
  }
    
protected:
    MySphereSource(){};
   ~MySphereSource(){};

};

vtkStandardNewMacro(MySphereSource);



// Command
class vtkCameraObserver : public vtkCommand
{
public:
  static vtkCameraObserver *New()
  {return new vtkCameraObserver();};

  void SetPolyDataAlgorithm(MySphereSource* mySphereSource) {
    this->mySphereSource = mySphereSource;
  }

    
protected:
  vtkCameraObserver() {};
  ~vtkCameraObserver(){}
    
  virtual void Execute(vtkObject* caller, unsigned long event, void
*calldata)
  {
    cout << "Camera modified event" << endl;
    vtkCamera* cam = reinterpret_cast<vtkCamera*> (caller);
    double* direction = cam->GetDirectionOfProjection();
    mySphereSource->printDirectionOfProjection(direction);
    
    // In case you would need this too:
    //vtkMatrix4x4* mat = cam->GetViewTransformMatrix();
     
    //cout << "View matrix: " << endl; 
    //cout <<
"=================================================================================
" << endl; 
    //cout << mat->GetElement(0, 0) << "\t" <<
mat->GetElement(0, 1) << "\t" << mat->GetElement(0, 2)
<< "\t" << mat->GetElement(0, 3) << "\t" << endl;
    //cout << mat->GetElement(1, 0) << "\t" <<
mat->GetElement(1, 1) << "\t" << mat->GetElement(1, 2)
<< "\t" << mat->GetElement(1, 3) << "\t" << endl;
    //cout << mat->GetElement(2, 0) << "\t" <<
mat->GetElement(2, 1) << "\t" << mat->GetElement(2, 2)
<< "\t" << mat->GetElement(2, 3) << "\t" << endl;
    //cout << mat->GetElement(3, 0) << "\t" <<
mat->GetElement(3, 1) << "\t" << mat->GetElement(3, 2)
<< "\t" << mat->GetElement(3, 3) << "\t" << endl;
    //cout <<
"=================================================================================
" << endl; 
    //double* x = cam->GetOrientation();
    //cout << "Orientation : " << x[0] << " " <<
x[1] << " " << x[2] << endl;
  }

private :
  MySphereSource* mySphereSource;
};



int main (int, char *[])
{
  vtkSmartPointer<MySphereSource> sphereSource = 
  vtkSmartPointer<MySphereSource>::New();
  sphereSource->SetCenter(0.0, 0.0, 0.0);
  sphereSource->SetRadius(1.23);
  sphereSource->Update();
    
  vtkPolyData* polydata = sphereSource->GetOutput();
    
  // Create a mapper
  vtkSmartPointer<vtkPolyDataMapper> mapper = 
  vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
  mapper->SetInput(polydata);
  #else
  mapper->SetInputData(polydata);
#endif
    
  // Create an actor
  vtkSmartPointer<vtkActor> actor = 
  vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);
    
  // A renderer and render window
  vtkSmartPointer<vtkRenderer> renderer = 
  vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow = 
  vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
    
  // An interactor
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
  vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);
    
  vtkInteractorStyleTrackballCamera *style = 
  vtkInteractorStyleTrackballCamera::New();
  renderWindowInteractor->SetInteractorStyle(style);
    
  // Add the actors to the scene
  renderer->AddActor(actor);
  renderer->SetBackground(.2, .3, .4);
 
//*************************************************************************************
  // Create observer
  vtkSmartPointer<vtkCameraObserver> observer =
vtkSmartPointer<vtkCameraObserver>::New();
  // tell observer on which object to operate on
  observer->SetPolyDataAlgorithm(sphereSource);
  // hook observer into camera's modified event
  vtkCamera* cam = renderer->GetActiveCamera();
  cam->AddObserver(vtkCommand::ModifiedEvent, observer);
 
//*************************************************************************************
    
  // Add widget
  vtkSmartPointer<vtkAxesActor> axes = 
  vtkSmartPointer<vtkAxesActor>::New();
    
  vtkSmartPointer<vtkOrientationMarkerWidget> widget = 
  vtkSmartPointer<vtkOrientationMarkerWidget>::New();
  widget->SetOutlineColor( 0.9300, 0.5700, 0.1300 );
  widget->SetOrientationMarker( axes );
  widget->SetInteractor( renderWindowInteractor );
  widget->SetViewport( 0.0, 0.0, 0.4, 0.4 );
  widget->SetEnabled( 1 );
  widget->InteractiveOn();
    
  renderer->ResetCamera();
  renderWindow->Render();
    
  // Begin mouse interaction
  renderWindowInteractor->Start();
    
  return EXIT_SUCCESS;
}

best regards

Jochen

--
View this message in context: http://vtk.1045678.n5.nabble.com/How-to-forward-mouse-move-event-to-polydata-algorithm-and-get-there-direction-of-projection-tp5714021p5714029.html
Sent from the VTK - Users mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120620/a642bfcf/attachment.htm>


More information about the vtkusers mailing list