[vtkusers] Segmentation Fault error when trying to animate a vector of actors

Lucia Pavan lpavan.pd at gmail.com
Wed Jan 16 06:26:01 EST 2013


Hi vtkusers,

I'm new to VTK, and not very expert in C++ neither. 
I want to use VTK to trace the movement of a number of particles, each of which is moving linearly along a given direction
(my final aim is to visualize the evolution of a stream of particles).
For this I am trying to merge the two C++ examples AnimateActors and VectorOfActors that are on the vtk website.
The code compiles smoothly, but I end up with a Segmentation Fault error when trying to animate the vector of actors (and none of the actors in the vector is appearing in the rendering window).
The code I'm using is here below, and AnimateActors.h is in attachment.

Could somebody help me in understanding where is my error? 
Even suggestions on a simpler method /different approach to the problem are welcome!

many thanks,
Lucia



-------------------------------------------------------------------
Lucia Pavan

ISDC - Science data center for Astrophysics   
Ch. d'Ecogia 16, 
CH-1290 Versoix
Switzerland


--------- my code --------
#include "AnimateActors.h"
#include <vtkSmartPointer.h>
#include <vtkAnimationCue.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkCommand.h>
#include <vtkAnimationScene.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkCamera.h>
#include "vtkProperty.h"
#include <stdio.h>
#include <math.h>

#include <vector>


int main(int argc, char *argv[])
{
  // Create the graphics structure. The renderer renders into the
  // render window.
  vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
  vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
  renWin->SetSize(600,600); //(width, height)
  renWin->SetMultiSamples(0);
  iren->SetRenderWindow(renWin);
  renWin->AddRenderer(ren1);
  
  // Generate a sphere
  vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->SetRadius(0.25);
  sphereSource->SetCenter(0.0,2.5,0.0);
	
  vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
  sphereMapper->SetInputConnection( sphereSource->GetOutputPort());
  vtkSmartPointer<vtkActor> sphere = vtkSmartPointer<vtkActor>::New();
  sphere->SetMapper(sphereMapper);
	
  ren1->AddActor(sphere);


   // Generate a vector of actors (particles)
   std::vector<vtkSmartPointer<vtkActor> > actors;	
   for(unsigned int i = 0; i < 3; i++)
    {
	vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
	sphereSource->SetCenter(0.0,2.5,0.0);//i/10.0, sin (i*PI/180), 0.0);
	sphereSource->SetRadius(0.2);
	
	vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	mapper->SetInputConnection(sphereSource->GetOutputPort());
	
	vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
	actor->SetMapper(mapper);
	actor->GetProperty()->SetColor(1.0,1.0,1.0);
	actors.push_back(actor);
	
	ren1->AddActor(actors[i]);
    }
	
	
  // Create an Animation Scene
  vtkSmartPointer<vtkAnimationScene> scene = vtkSmartPointer<vtkAnimationScene>::New();
  if(argc>=2 && strcmp(argv[1],"-real")==0)
    {
    cout << "real-time mode" << endl;
    scene->SetModeToRealTime();
    }
  else
    {
    cout << "sequence mode" << endl;
    scene->SetModeToSequence();
    }
  scene->SetLoop(0);
  scene->SetFrameRate(5);
  scene->SetStartTime(0);
  scene->SetEndTime(10);
  
  vtkSmartPointer<AnimationSceneObserver> sceneObserver = vtkSmartPointer<AnimationSceneObserver>::New();
  sceneObserver->SetRenderWindow(renWin);
  scene->AddObserver(vtkCommand::AnimationCueTickEvent,sceneObserver);

  // Create an Animation Cue for each actor

  vtkSmartPointer<vtkAnimationCue> cue1 = vtkSmartPointer<vtkAnimationCue>::New();
  cue1->SetStartTime(5);
  cue1->SetEndTime(10);
  scene->AddCue(cue1);

	std::vector<vtkSmartPointer<vtkAnimationCue> > cues;
	for(unsigned int i = 0; i < 2; i++)
    {
	vtkSmartPointer<vtkAnimationCue> cue = vtkSmartPointer<vtkAnimationCue>::New();
	cue->SetStartTime(5);
	cue->SetEndTime(10);
	cues.push_back(cue);
	
	scene->AddCue(cues[i]);
	}	
	
	// Create an ActorAnimator for each actor;
	ActorAnimator animateSphere;
	animateSphere.SetActor(sphere);
	std::vector<double> endSphere(3);
        endSphere[0] = 0;
        endSphere[1] = -2.5;
        endSphere[2] = 0;
	animateSphere.SetEndPosition(endSphere);
	
	animateSphere.AddObserversToCue(cue1);
	
	

    for(unsigned int i = 0; i < 2; i++)
    {
		ActorAnimator animatePart;
		animatePart.SetActor(actors[i]);
		std::vector<double> endPart(3);
		endPart[0] = 1.0;
		endPart[1] = 2.5;
		endPart[2] = 0;
		animatePart.SetEndPosition(endPart);
		
		animatePart.AddObserversToCue(cues[i]);
	}	

  renWin->Render();
  ren1->ResetCamera();
  ren1->GetActiveCamera()->Dolly(0.05);
  ren1->ResetCameraClippingRange();

  // Create Cue observer.
  scene->Play();
  scene->Stop();
  
  iren->Start();
  return EXIT_SUCCESS;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130116/78334f1c/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: AnimateActors.h
Type: application/octet-stream
Size: 4322 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130116/78334f1c/attachment.obj>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130116/78334f1c/attachment-0001.htm>


More information about the vtkusers mailing list