[vtkusers] Animation of vtkUnstructuredGrid

Сергеев Ефим efim.sergeev at yandex.ru
Tue Jun 8 09:46:56 EDT 2010


I have some sophisticated code on C++, it seems must animate vtkUnstrucuredGrid which loaded  from C:\VTK\Data\blow.vtk file.
But code doesn't work. I not understand where error is located.


class vtkTimerCallback : public vtkCommand
{
  public:
    static vtkTimerCallback *New()
    {
      vtkTimerCallback *cb = new vtkTimerCallback;
      cb->TimerCount = 0;
      return cb;
    }

    virtual void Execute(vtkObject *caller, unsigned long eventId,
                         void * vtkNotUsed(callData))
    {
      if (vtkCommand::TimerEvent == eventId)
        {
        ++this->TimerCount;
        }
	  else
	  {
		return;
	  }

         vtkSmartPointer<vtkDataArray> displacements;
         const char *v_names  = reader->GetVectorsNameInFile(TimerCount);
         displacements = grid->GetPointData()->GetVectors(v_names);
         int Size = displacements->GetSize();
        std::cout << v_names << std::endl;
        std::cout << Size << std::endl;

        double point[3];
		
		
       for(vtkIdType i = 0; i < grid->GetNumberOfPoints(); i++)
        {

	    grid->GetPoint(i,point);
            
            double *touple;

            touple = displacements->GetTuple3(i);

            
            point[0]=point[0]+touple[0];
            point[1]=point[1]+touple[1];
            point[2]=point[2]+touple[2];

            grid->GetPoints()->SetPoint(i,point[0],point[1],point[2]);
            cout << this->TimerCount << endl;
        }

		grid->Update();
		actor->Modified();
		
		renderer->Render();
		
    }

  private:
    int TimerCount;
  public:
	vtkSmartPointer<vtkActor> actor;
        vtkSmartPointer<vtkUnstructuredGrid> grid;
        vtkSmartPointer<vtkUnstructuredGridReader> reader;
        vtkSmartPointer<vtkUnstructuredGridGeometryFilter> geometryFilter;
	vtkSmartPointer<vtkRenderer> renderer;
	vtkSmartPointer<vtkRenderWindow> renderWindow;
	vtkSmartPointer<vtkRenderWindowInteractor> iren;
	vtkSmartPointer<vtkMapper> mapper;
};


int main(int, char *[])
{

   vtkstd::string filename = "C://vtk//DATA//blow.vtk";

  //read all the data from the file
  vtkSmartPointer<vtkUnstructuredGridReader> reader = vtkSmartPointer<vtkUnstructuredGridReader>::New();
  reader->SetFileName(filename.c_str());
  reader->GetReadAllVectors();
  reader->ReadAllVectorsOn();

  reader->Update();
  vtkUnstructuredGrid* unstructuredGrid = reader->GetOutput();



  vtkSmartPointer<vtkUnstructuredGridGeometryFilter> geometryFilter = vtkSmartPointer<vtkUnstructuredGridGeometryFilter>::New();
  geometryFilter->SetInputConnection(unstructuredGrid->GetProducerPort());

  geometryFilter->Update();

  //Create a mapper and actor
  vtkSmartPointer<vtkDataSetMapper> mapper =   vtkSmartPointer<vtkDataSetMapper>::New();
  mapper->SetInputConnection(geometryFilter->GetOutputPort());
  vtkSmartPointer<vtkActor> actor =  vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);


  //Create a renderer, render window, and interactor
  vtkSmartPointer<vtkRenderer> renderer =  vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  //Add the actor to the scene
  renderer->AddActor(actor);
  //renderer->SetBackground(1,1,1); // Background color white

  //Render and interact

  renderWindow->Render();

  renderWindowInteractor->Initialize();
  

    // Sign up to receive TimerEvent
  vtkSmartPointer<vtkTimerCallback> cb =  vtkSmartPointer<vtkTimerCallback>::New();
   //init  cb
  cb->actor = actor;
  cb->grid = unstructuredGrid;
  cb->reader = reader;
  cb->renderWindow = renderWindow;
  cb->iren = renderWindowInteractor;
  cb->renderer = renderer;
  cb->mapper = mapper;
  cb->geometryFilter = geometryFilter;
  

  renderWindowInteractor->AddObserver(vtkCommand::TimerEvent, cb);

  int timerId = renderWindowInteractor->CreateRepeatingTimer(1000);
  std::cout << "timerId: " << timerId << std::endl;
 

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

Any direction, suggestions would be welcome!

Sincerely,

Efim Sergeev



More information about the vtkusers mailing list