[vtkusers] using VTK with keyboard interaction

David Doria daviddoria at gmail.com
Tue Mar 27 08:19:40 EDT 2012


On Tue, Mar 27, 2012 at 5:08 AM, Jonas cv Fan
<work2compilation at gmail.com> wrote:
> Dear vtk users,
>
> I'm newbie in VTK library. I decided to use it as a renderer of a content
> captured by the Kinect sensor. I grab first the depth maps using opencv
> after that I render it by VTK. All working fine until now. However I wanna
> add some keyboard buttons to interact easily with the displayed content (3D
> cloud): it's really basic:
>
> 1-Push '*d*' to display the 3D point cloud by VTK.
> 2-Push *'k*' to return to default camera settings.
> 3-Push '*q*' to quite.
>
> here is a snapping code that I wrote:
>
> // VTK headers
> *#include <vtkSmartPointer.h>
> .... etc*
> *int main()
> {*
>
> // 3D rendering rotines
>
>                //VTK Pipeline
>                *vtkSmartPointer<vtkRenderer> ren =
> vtkSmartPointer<vtkRenderer>::
> New();
>                renwin = vtkSmartPointer<vtkRenderWindow>::New();
>                renwin1 = vtkSmartPointer<vtkRenderWindow>::New();
>
>                vtkSmartPointer<vtkRenderWindowInteractor> iren =
> vtkSmartPointer<vtkRenderWindowInteractor>::New();
>
>                renwin->AddRenderer(ren);
>                renwin->SetInteractor(iren);
>                renwin->SetSize(1280,800);
>                iren->Initialize();*
>
> // Main loop
>        while (1)
> // To capture the content from the kinect
> .....
> key= cvWaitKey(1);  // for opencv capture
>
> *if (key == 'q')* break;            // To quite
>                switch(key) {
>            /* '1' pressed, display the original image */
>          *  case 'd':*
>                cout<<"The decomposition is running..."<<endl;
>
>                        Display_cloud( renwin,pDepthMap);  // a function to
> display a cloud
> points
>                         renwin->Render();
>
>                        //Set up camera
>                        ren->ResetCamera();
>                        ren->GetActiveCamera()->Roll(180.0);
>                        ren->GetActiveCamera()->Azimuth(180.0);
>                        ren->GetActiveCamera()->Zoom(2.0);
>
>                        iren->Start();
>
>       *case 'k':*           //  to return to default camera settings.
>                ren->ResetCamera();
>                        ren->GetActiveCamera()->Roll(180.0);
>                        ren->GetActiveCamera()->Azimuth(180.0);
>                        ren->GetActiveCamera()->Zoom(2.0);
>
>                        iren->Start();
>                break;
> }
> }
>
> So I have two problems:
> 1-When I push *'q'* the program doesn't close correctly, visual studio send
> me to the vtkInformation.cxx to the line " *i->first->Report(this,
> collector);*"  in "void
> *vtkInformation::ReportReferences(vtkGarbageCollector* collector)"*
> function?!
> How can I avoid that?
>
> 2-When push 'k' to return to default camera paramters it doesn't work..
> could you help me please?
> Thank you so much and sorry if my post is very long for you .
>
> Cheers for everyone
> Jo

I wrote something similar for a different time of flight camera:
https://github.com/daviddoria/vtkHokuyo

I'm not sure if there is any benefit of using a VTK timer like I did
versus your while(1) loop.

Also, it looks like you have taken a "get the data, then draw it"
approach. In VTK the idea would change to "display the data always,
and update it when necessary". You use an "InteractorStyle" to handle
the keypress events:
http://www.vtk.org/Wiki/VTK/Examples/Cxx/Interaction/KeypressEvents

To update the display once you get new data, you would just update the
PolyData that is already attached to a mapper->actor->renderer (and
call Modified() on it if necessary) and the renderer would
automatically display the new points.

Additionally, you could check out PCL (pointclouds.org) - I think they
already have this sort of thing written for the Kinect.

Good luck,

David



More information about the vtkusers mailing list