]> Creatis software - crea.git/blob - src/creaVtk.txx
*** empty log message ***
[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   template <class T>
21   /*CREA_EXPORT*/ vtkImageData* NewVtkImageDataFromRaw( T* data, 
22                                                     int nx, 
23                                                     int ny,
24                                                     int nz,
25                                                     bool do_not_desalloc)
26   {
27     //    std::cout << "NV "<<nx<<" " <<ny<<" " << nz<<std::endl;
28     //    std::cout <<  vtkTypeTraits<T>::SizedName() << std::endl;
29     vtkImageData *image = vtkImageData::New();
30
31     // Shouldn't we pass NumberOfScalarComponents to deal with RGB, RGBA images as well? // JPR
32
33     image->SetNumberOfScalarComponents(1);
34
35     int vtktype = vtkTypeTraits<T>::VTKTypeID();
36     image->SetScalarType(vtktype);
37     image->SetDimensions(nx, ny ,nz);
38     image->SetSpacing(1, 1, 1);
39     image->AllocateScalars();
40     vtkDataArray* array = 0;
41     switch (vtktype)
42       {
43       case VTK_CHAR:
44         array = vtkCharArray::New(); break;
45       case VTK_SIGNED_CHAR:
46         array = vtkSignedCharArray::New(); break;
47       case VTK_UNSIGNED_CHAR:
48         array = vtkUnsignedCharArray::New(); break;
49       case VTK_SHORT:
50         array = vtkShortArray::New(); break;
51       case VTK_UNSIGNED_SHORT:
52         array = vtkUnsignedShortArray::New(); break;
53       case VTK_INT:
54         array = vtkIntArray::New(); break;      
55       case VTK_UNSIGNED_INT:
56         array = vtkUnsignedIntArray::New(); break;
57       case VTK_LONG:
58         array = vtkLongArray::New(); break;
59       case VTK_UNSIGNED_LONG:
60         array = vtkUnsignedLongArray::New(); break;
61       case VTK_FLOAT:
62         array = vtkFloatArray::New(); break;
63       case VTK_DOUBLE:
64         array = vtkDoubleArray::New(); break;
65       default:
66         creaGlobalError("NewVtkImageDataFromRaw : type "
67                   <<vtkTypeTraits<T>::SizedName()
68                   <<" non implemented");
69       }
70     vtkDataArrayTemplate<T>* tarray 
71                 = dynamic_cast<vtkDataArrayTemplate<T>*>(array);
72     array->SetNumberOfComponents( 1 );
73     size_t size = (long)nx*(long)ny*(long)nz;
74     // The last param of SetArray is set to 1 to keep the class from deleting the array 
75     // when it cleans up or reallocates memory.
76     int dndesa = 0;
77     if (do_not_desalloc)
78         dndesa = 1;
79     tarray->SetArray( data, size, dndesa );
80     image->GetPointData( )->SetScalars( tarray );
81     array->Delete( );
82     return image;
83   }
84   
85 }
86