]> Creatis software - crea.git/blob - src/creaVtk.txx
a7d6a04733b9805a2180f6232302054d85a9be92
[crea.git] / src / creaVtk.txx
1 #include <vtkDataArrayTemplate.h>
2 #include <vtkPointData.h>
3
4 namespace crea
5 {
6   
7   template <class T>
8   vtkImageData* CREA_EXPORT NewVtkImageDataFromRaw( T* data,
9                                                     size_t size)
10   {
11     vtkImageData *image = vtkImageData::New();    
12     image->SetNumberOfScalarComponents(1);
13     image->SetScalarType(GetVtkIdType<T>(*data));
14     image->AllocateScalars();
15     vtkDataArrayTemplate<T>* array 
16       = (vtkDataArrayTemplate<T>*)vtkDataArrayTemplate<T>::New();
17     array->SetNumberOfComponents( 1 );
18     // The last param of SetArray is set to 1 to keep the class from deleting the array when it cleans up or reallocates memory.
19     array->SetArray( data, size, 1 );
20     //Pay attention in particular to the last param of SetArray:
21     //http://www.vtk.org/doc/nightly/html/classvtkUnsignedShortArray.html#z798_0
22     image->GetPointData( )->SetScalars( array );
23     array->Delete( );
24     return image;
25   }
26   
27 }
28