]> Creatis software - crea.git/blob - src/creaVtk.txx
4d39387958debb94ce4b6316ccdff53b8986af9d
[crea.git] / src / creaVtk.txx
1 #include <vtkDataArrayTemplate.h>
2 #include <vtkPointData.h>
3 #include <typeinfo> 
4 namespace crea
5 {
6   
7   template <class T>
8   vtkImageData* CREA_EXPORT NewVtkImageDataFromRaw( T* data,
9                                                     int nx, 
10                                                     int ny,
11                                                     int nz)
12   {
13     std::cout << "NV "<<nx<<" " <<ny<<" " << nz<<std::endl;
14     std::cout << typeid(T).name() << std::endl;
15     vtkImageData *image = vtkImageData::New();    
16     image->SetNumberOfScalarComponents(1);
17     image->SetScalarType(GetVtkIdType<T>(*data));
18     image->SetDimensions(nx,ny,nz);
19     image->SetSpacing(1,1,1);
20     std::cout << "alloc"<<std::endl;
21     image->AllocateScalars();
22     std::cout << "alloc ok"<<std::endl;
23     vtkDataArrayTemplate<T>* array 
24       = (vtkDataArrayTemplate<T>*)vtkDataArrayTemplate<T>::New();
25     std::cout << "data array alloc ok"<<std::endl;
26     //    array->SetNumberOfComponents( 1 );
27     std::cout << "nb compo ok"<<std::endl;
28     // The last param of SetArray is set to 1 to keep the class from deleting the array when it cleans up or reallocates memory.
29     size_t size = (long)nx*(long)ny*(long)nz;
30     std::cout << "SetArray"<<std::endl;
31     array->SetArray( data, size, 1 );
32     //Pay attention in particular to the last param of SetArray:
33     //http://www.vtk.org/doc/nightly/html/classvtkUnsignedShortArray.html#z798_0
34    std::cout << "SetScalars"<<std::endl;
35      image->GetPointData( )->SetScalars( array );
36    std::cout << "Delete"<<std::endl;
37     array->Delete( );
38     return image;
39   }
40   
41 }
42