[vtkusers] how to move an actor by keyboard events

Reza Faieghi mfaieghi at westerneng.ca
Thu Jan 26 17:30:41 EST 2017


Hello Everyone,



I want to use arrow keys to move an actor in a scene. The actor is created
from an STL reader from the following code:



       vtkSmartPointer<vtkSTLReader> reamer_reader =

              vtkSmartPointer<vtkSTLReader>::New();

       reamer_reader->SetFileName(inputFilename.c_str());

       reamer_reader->Update();



Then, I have the following transfer matrix and filter to make the
translations:



       vtkSmartPointer<vtkTransform> transform_matrix = vtkSmartPointer<
vtkTransform>::New();

       transform_matrix->Translate(0.0, 0.0, 0.0);

       vtkSmartPointer<vtkTransformPolyDataFilter> transformFilter =

              vtkSmartPointer<vtkTransformPolyDataFilter>::New();

       transformFilter->SetInputConnection(reamer_reader->GetOutputPort());

       transformFilter->SetTransform(transform_matrix);

       transformFilter->Update();



And of course, there are the mapper, actor and renderer as follows:


       vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer
>::New();

       renderer->SetBackground(.3, .6, .3);

       vtkSmartPointer<vtkPolyDataMapper> reamer_mapper = vtkSmartPointer<
vtkPolyDataMapper>::New();

       reamer_mapper->SetInputConnection(reamer_reader->GetOutputPort());

       vtkSmartPointer<vtkActor> reamer_actor = vtkSmartPointer<vtkActor
>::New();

       renderer->AddActor(reamer_actor);



I create a window interactor and then add an observer to look for key press
events:


       vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =

              vtkSmartPointer<vtkRenderWindowInteractor>::New();

       vtkSmartPointer<vtkInteractorStyleTrackballCamera> camera_style =

              vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();

       renderWindowInteractor->SetInteractorStyle(camera_style);

       vtkSmartPointer<vtkCallbackCommand> keypressCallback =

              vtkSmartPointer<vtkCallbackCommand>::New();

       keypressCallback->SetCallback(KeypressCallbackFunction);

       renderWindowInteractor->AddObserver(vtkCommand::KeyPressEvent,
keypressCallback);

       vtkSmartPointer<KeyPressInteractorStyle> keyboard_style =

              vtkSmartPointer<KeyPressInteractorStyle>::New();

       renderWindowInteractor->SetInteractorStyle(keyboard_style);

       vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<
vtkRenderWindow>::New();

       renderWindowInteractor->SetRenderWindow(renderWindow);

       renderWindow->AddRenderer(renderer);

       renderWindow->Render();

       renderWindowInteractor->Start();




and here is the KeypressCallbackFunction itself:



void KeypressCallbackFunction(vtkObject* caller,

       long unsigned int vtkNotUsed(eventId),

       void* vtkNotUsed(clientData),

       void* vtkNotUsed(callData)){



       vtkRenderWindowInteractor *iren = static_cast<
vtkRenderWindowInteractor*>(caller);



       std::cout << "Pressed: " << iren->GetKeySym() << std::endl;



       std::string key = iren->GetKeySym();





       if (key == "Up") {



              //??? What to do here ???



       }



}



I can not figure out what commands should I write when the “Up” key is
pressed. I am assuming that I only need to update the translation matrix
here, but I don’t know where to apply the updated translation matrix to the
actor and I think I need to call renderWindow->Render(), too. I will really
appreciate it if anyone helps me with

(1)      what to write within the if-block?

(2)      how to pass variables such as translation matrix to
KeypressCallbackFunction?

(3)      where to call  renderWindow->Render()?



Many thanks!

Reza

-- 
PhD Candidate,
Spencer Engineering Building Room 37
University of Western Ontario
London, ON, Canada, N6A 5B9
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170126/95b0a28a/attachment.html>


More information about the vtkusers mailing list