[vtkusers] vtkcommand and vtkFFMPEGWritter

Sergio Aguirre sergio.aguirre at gmail.com
Fri Jun 5 20:28:20 EDT 2009


Hello everyone

I am attempting to link a vtkcommand to use as a way of writing a movie
every time an interaction event is called from a vtkremderwindowinteractor

However I am not sure I am connecting the elements correctly. Also from the
examples I have seen, the movie writers need and "end()" call to close its
file. If I am to use a vtk command is this possible?

Any suggestion is welcomed.

Thank you

Sergio
ps. below my code.

/*=========================================================================
=========================================================================*/
//
//

// First include the required header files for the VTK classes we are using.
#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkCommand.h"
#include "vtkBoxWidget.h"
#include "vtkTransform.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkFFMPEGWriter.h"
#include "vtkWindowToImageFilter.h"


//
// Similar to Cone2.cxx, we define a callback for interaction.
//
class vtkMyCallback : public vtkCommand
{
public:
  static vtkMyCallback *New()
    { return new vtkMyCallback; }
  virtual void Execute(vtkObject *caller, unsigned long, void*)
    {
      vtkFFMPEGWriter *movie = reinterpret_cast<vtkFFMPEGWriter*>(caller);
      movie->Write();
      movie->End();
    }
};

int main()
{
  //
  //
  vtkConeSource *cone = vtkConeSource::New();
  cone->SetHeight( 3.0 );
  cone->SetRadius( 1.0 );
  cone->SetResolution( 10 );

  //
  //
  vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
  coneMapper->SetInputConnection( cone->GetOutputPort() );

  //
  //
  vtkActor *coneActor = vtkActor::New();
  coneActor->SetMapper( coneMapper );

  //
  //
  vtkRenderer *ren1= vtkRenderer::New();
  ren1->AddActor( coneActor );
  ren1->SetBackground( 0.1, 0.2, 0.4 );

  //
  //
  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer( ren1 );
  renWin->SetSize( 320, 240 );

  //
  // The vtkRenderWindowInteractor class watches for events (e.g., keypress,
  // mouse) in the vtkRenderWindow. These events are translated into
  // event invocations that VTK understands (see VTK/Common/vtkCommand.h
  // for all events that VTK processes). Then observers of these VTK
  // events can process them as appropriate.
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  iren->SetRenderWindow(renWin);

  //
  // By default the vtkRenderWindowInteractor instantiates an instance
  // of vtkInteractorStyle. vtkInteractorStyle translates a set of events
  // it observes into operations on the camera, actors, and/or properties
  // in the vtkRenderWindow associated with the vtkRenderWinodwInteractor.
  // Here we specify a particular interactor style.
  vtkInteractorStyleTrackballCamera *style =
    vtkInteractorStyleTrackballCamera::New();
  iren->SetInteractorStyle(style);

  //
  // Here we use a vtkBoxWidget to transform the underlying coneActor (by
  // manipulating its transformation matrix). Many other types of widgets
  // are available for use, see the documentation for more details.
  //
  // The SetInteractor method is how 3D widgets are associated with the
render
  // window interactor. Internally, SetInteractor sets up a bunch of
callbacks
  // using the Command/Observer mechanism (AddObserver()). The place factor
  // controls the initial size of the widget with respect to the bounding
box
  // of the input to the widget.
  vtkBoxWidget *boxWidget = vtkBoxWidget::New();
  boxWidget->SetInteractor(iren);
  boxWidget->SetPlaceFactor(5.25);

  vtkWindowToImageFilter  *w2if = vtkWindowToImageFilter::New();
   w2if->SetInput(renWin);

  vtkFFMPEGWriter *moviewriter1 = vtkFFMPEGWriter::New();
  moviewriter1->SetFileName("test1.avi");
  moviewriter1->SetInputConnection(w2if->GetOutputPort());
  moviewriter1->Start();
  //
  //
  boxWidget->SetProp3D(coneActor);
  boxWidget->PlaceWidget();

  vtkMyCallback *callback = vtkMyCallback::New();
  moviewriter1->AddObserver(vtkCommand::InteractionEvent, callback);

  //
  // Normally the user presses the "i" key to bring a 3D widget to life.
Here
  // we will manually enable it so it appears with the cone.
  //
  boxWidget->On();

  iren->Initialize();
  iren->Start();
  moviewriter1->End();
  //

  cone->Delete();
  coneMapper->Delete();
  coneActor->Delete();
  callback->Delete();
  boxWidget->Delete();
  ren1->Delete();
  renWin->Delete();
  iren->Delete();
  style->Delete();

  return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20090605/f5c919fc/attachment.htm>


More information about the vtkusers mailing list