[vtkusers] Animation of vtkUnstructuredGrid
Сергеев Ефим
efim.sergeev at yandex.ru
Tue Jun 8 09:48:32 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 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 actor;
vtkSmartPointer grid;
vtkSmartPointer reader;
vtkSmartPointer geometryFilter;
vtkSmartPointer renderer;
vtkSmartPointer renderWindow;
vtkSmartPointer iren;
vtkSmartPointer mapper;
};
int main(int, char *[])
{
vtkstd::string filename = "C://vtk//DATA//blow.vtk";
//read all the data from the file
vtkSmartPointer reader = vtkSmartPointer::New();
reader->SetFileName(filename.c_str());
reader->GetReadAllVectors();
reader->ReadAllVectorsOn();
reader->Update();
vtkUnstructuredGrid* unstructuredGrid = reader->GetOutput();
vtkSmartPointer geometryFilter = vtkSmartPointer::New();
geometryFilter->SetInputConnection(unstructuredGrid->GetProducerPort());
geometryFilter->Update();
//Create a mapper and actor
vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInputConnection(geometryFilter->GetOutputPort());
vtkSmartPointer actor = vtkSmartPointer::New();
actor->SetMapper(mapper);
//Create a renderer, render window, and interactor
vtkSmartPointer renderer = vtkSmartPointer::New();
vtkSmartPointer renderWindow = vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer renderWindowInteractor = vtkSmartPointer::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 cb = vtkSmartPointer::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