[vtkusers] VTK animation in the loop
David Gobbi
david.gobbi at gmail.com
Sat Jun 13 18:04:40 EDT 2015
Hi Agata,
The trick with doing animation with a timer is this: there is no loop!
The animation is done by the Execute method of the timer callback.
Every time a TimerEvent is generated, the Execute method must
draw one animation frame. It can use a counter to keep track of
which frame to draw.
- David
On Sat, Jun 13, 2015 at 1:59 PM, agatte <agatakrason at gmail.com> wrote:
> Hi,
>
> I have a question concering animation in VTK toolkit.
> I would like to make an animation.
> I want to read image in the loop and visualize it.
> (I have image series. I have to see how it changes position, moves ect ...)
> I wrote a code but it doesn't work well. It builds and compiles well.
> But it doesn't change simultaneously ... It doesn't change loop .. I have
> to
> click on x in renderwindow....
>
> Could anyone advice me something in VTK ? What I have to change/add to my
> code ?
>
>
> I would be appreciate for any help please.
>
> Here is my code :
>
>
> class vtkTimerCallback : public vtkCommand
> {
> public:
> static vtkTimerCallback *New()
> {
> vtkTimerCallback *cb = new vtkTimerCallback;
> cb->TimerCount = 0;
> return cb;
> }
>
> virtual void Execute(vtkObject *vtkNotUsed(caller), unsigned long
> eventId,
> void *vtkNotUsed(callData))
> {
> if(vtkCommand::TimerEvent == eventId)
> {
> ++this->TimerCount;
> }
>
> cout << this->TimerCount << endl;
> }
>
> private:
> int TimerCount;
>
> };
>
>
> int main (int argc, char *argv[])
> {
>
> if( argc < 3)
> {
> std::cerr << "Usage: " <<argv[0] <<"refFileName
> numberOfFiles"<< std::endl;
> return EXIT_FAILURE;
> }
>
> //string dirName = "images";
> //argv[1]="\images\\image06.mhd";
>
> vtkSmartPointer<vtkMetaImageReader> refReader =
> vtkSmartPointer<vtkMetaImageReader>::New();
> refReader->SetFileName(argv[1]);
> refReader->Update();
>
> vtkSmartPointer<vtkImageData> refVolume =
> vtkSmartPointer<vtkImageData>::New();
> refVolume = refReader->GetOutput();
>
> vtkSmartPointer<vtkContourFilter> refContour =
> vtkSmartPointer<vtkContourFilter>::New();
> refContour->SetInput(refVolume);
> refContour->Update();
> refContour->SetValue(0,1);
>
> vtkSmartPointer<vtkPolyData> refPolydata =
> vtkSmartPointer<vtkPolyData>::New();
> refPolydata= refContour->GetOutput();
>
> vtkSmartPointer<vtkPoints> refPoints = refPolydata->GetPoints();
> vtkSmartPointer<vtkCellArray> refPolys = refPolydata->GetPolys();
> refPolydata->SetPoints(refPoints);
>
> vtkSmartPointer<vtkPolyDataMapper> refMapper =
> vtkSmartPointer<vtkPolyDataMapper>::New();
> refMapper->SetInputConnection(refContour->GetOutputPort());
> refMapper->ScalarVisibilityOn();
>
> vtkSmartPointer<vtkActor> refActor =
> vtkSmartPointer<vtkActor>::New();
> refActor->SetMapper(refMapper);
> // refActor->GetProperty()->SetColor(1.0, 2.0, 3.0);
>
> vtkSmartPointer<vtkRenderer> renderer =
> vtkSmartPointer<vtkRenderer>::New();
> renderer->SetBackground(0, 0, 0);
> vtkSmartPointer<vtkRenderWindow> renderWindow =
> vtkSmartPointer<vtkRenderWindow>::New();
> renderWindow->AddRenderer(renderer);
> vtkSmartPointer<vtkRenderWindowInteractor> interactor =
> vtkSmartPointer<vtkRenderWindowInteractor>::New();
> interactor->SetRenderWindow(renderWindow);
> renderer->AddActor(refActor);
>
> int numberOfFiles = atoi(argv[2]);
> std::cout<<"numberOfFiles : "<< std::endl;
>
> renderer->Render();
> renderWindow->Render();
> interactor->Start();
>
> renderer->UpdateLightsGeometryToFollowCamera();
> renderer->Modified();
>
> // Sign up to receive TimerEvent
> vtkSmartPointer<vtkTimerCallback> cb =
> vtkSmartPointer<vtkTimerCallback>::New();
> interactor->AddObserver(vtkCommand::TimerEvent, cb);
> //interactor->CreateRepeatingTimer(10);
> interactor->SetDesiredUpdateRate(10);
> cb->AbortFlagOff();
>
>
> for(int i=1; i< numberOfFiles; ++i)
> {
>
> string end = static_cast<ostringstream*>( &(ostringstream() << i)
> )->str();
> std::cout <<" "<<end<<std::endl;
> string currentFileName;
> currentFileName = "image0" + end + ".mhd";
> std::cout<<"
> "<<currentFileName<<std::endl;
> vtkSmartPointer<vtkMetaImageReader> moveReader=
> vtkSmartPointer<vtkMetaImageReader>::New();
> moveReader->SetFileName(currentFileName.c_str());
> moveReader->Update();
>
> vtkSmartPointer<vtkImageData> moveVolume =
> vtkSmartPointer<vtkImageData>::New();
> moveVolume = moveReader->GetOutput();
>
> vtkSmartPointer<vtkContourFilter> moveContour =
> vtkSmartPointer<vtkContourFilter>::New();
> moveContour->SetInput(moveVolume);
> moveContour->Update();
> moveContour->SetValue(0,1);
>
> vtkSmartPointer<vtkPolyData> movePolydata =
> vtkSmartPointer<vtkPolyData>::New();
> movePolydata = moveContour->GetOutput();
>
> vtkSmartPointer<vtkPoints> movePoints =
> movePolydata->GetPoints();
> vtkSmartPointer<vtkCellArray> movePolys =
> movePolydata->GetPolys();
> movePolydata->SetPoints(movePoints);
>
> vtkSmartPointer<vtkPolyDataMapper> moveMapper =
> vtkSmartPointer<vtkPolyDataMapper>::New();
> moveMapper->SetInputConnection(moveContour->GetOutputPort());
> moveMapper->ScalarVisibilityOff();
>
> vtkSmartPointer<vtkActor> moveActor =
> vtkSmartPointer<vtkActor>::New();
> moveActor->SetMapper(moveMapper);
> std::cout<< "i = "<<i<<std::endl;
> moveActor->GetProperty()->SetColor(1.0, 2.0, 0.0);
>
> renderer->AddActor(moveActor);
> renderer->Render();
> renderWindow->Render();
> interactor->Start();
> moveActor->SetVisibility(0);
> }
>
> system("Pause");
>
>
> return EXIT_SUCCESS;
> }
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150613/3c5f1adc/attachment.html>
More information about the vtkusers
mailing list