]> Creatis software - crea.git/blob - src/creaVtk.txx
ce64d91a8f806b91df0b1417af2d9640a9313bce
[crea.git] / src / creaVtk.txx
1 #include <vtkDataArrayTemplate.h>
2
3 #include <vtkCharArray.h>
4 #include <vtkSignedCharArray.h>
5 #include <vtkUnsignedCharArray.h>
6 #include <vtkShortArray.h>
7 #include <vtkUnsignedShortArray.h>
8 #include <vtkIntArray.h>
9 #include <vtkUnsignedIntArray.h>
10 #include <vtkLongArray.h>
11 #include <vtkUnsignedLongArray.h>
12 #include <vtkFloatArray.h>
13 #include <vtkDoubleArray.h>
14
15 #include <vtkPointData.h>
16 #include <vtkTypeTraits.h>
17 #include <creaMessageManager.h>
18 namespace crea
19 {
20   
21   template <class T>
22   vtkImageData* NewVtkImageDataFromRaw( T* data, 
23                                                     int nx, 
24                                                     int ny,
25                                                     int nz,
26                                                     bool do_not_desalloc)
27   {
28     //    std::cout << "NV "<<nx<<" " <<ny<<" " << nz<<std::endl;
29     //    std::cout <<  vtkTypeTraits<T>::SizedName() << std::endl;
30     vtkImageData *image = vtkImageData::New();    
31     image->SetNumberOfScalarComponents(1);
32     int vtktype = vtkTypeTraits<T>::VTKTypeID();
33     image->SetScalarType(vtktype);
34     image->SetDimensions(nx,ny,nz);
35     image->SetSpacing(1,1,1);
36     image->AllocateScalars();
37     vtkDataArray* array = 0;
38     switch (vtktype)
39       {
40       case VTK_CHAR:
41         array = vtkCharArray::New(); break;
42       case VTK_SIGNED_CHAR:
43         array = vtkSignedCharArray::New(); break;
44       case VTK_UNSIGNED_CHAR:
45         array = vtkUnsignedCharArray::New(); break;
46       case VTK_SHORT:
47         array = vtkShortArray::New(); break;
48       case VTK_UNSIGNED_SHORT:
49         array = vtkUnsignedShortArray::New(); break;
50       case VTK_INT:
51         array = vtkIntArray::New(); break;      
52       case VTK_UNSIGNED_INT:
53         array = vtkUnsignedIntArray::New(); break;
54       case VTK_LONG:
55         array = vtkLongArray::New(); break;
56       case VTK_UNSIGNED_LONG:
57         array = vtkUnsignedLongArray::New(); break;
58       case VTK_FLOAT:
59         array = vtkFloatArray::New(); break;
60       case VTK_DOUBLE:
61         array = vtkDoubleArray::New(); break;
62       default:
63         creaGlobalError("NewVtkImageDataFromRaw : type "
64                   <<vtkTypeTraits<T>::SizedName()
65                   <<" non implemented");
66       }
67     vtkDataArrayTemplate<T>* tarray 
68       = dynamic_cast<vtkDataArrayTemplate<T>*>(array);
69     array->SetNumberOfComponents( 1 );
70     size_t size = (long)nx*(long)ny*(long)nz;
71     // The last param of SetArray is set to 1 to keep the class from deleting the array 
72     // when it cleans up or reallocates memory.
73         int dndesa = 0;
74         if (do_not_desalloc) dndesa = 1;
75     tarray->SetArray( data, size, dndesa );
76     image->GetPointData( )->SetScalars( tarray );
77     array->Delete( );
78     return image;
79   }
80   
81 }
82