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