]> Creatis software - crea.git/blob - src/creaVtk.txx
#3395 complit creaVtk_MACROS
[crea.git] / src / creaVtk.txx
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ 
26 */                                                                         
27
28
29 //EED 2022-07-21
30 // https://www.slicer.org/wiki/Documentation/Labs/VTK7
31 // #include <vtkDataArrayTemplate.h>
32  #include <vtkAOSDataArrayTemplate.h>
33
34 #include <vtkCharArray.h>
35 #include <vtkSignedCharArray.h>
36 #include <vtkUnsignedCharArray.h>
37 #include <vtkShortArray.h>
38 #include <vtkUnsignedShortArray.h>
39 #include <vtkIntArray.h>
40 #include <vtkUnsignedIntArray.h>
41 #include <vtkLongArray.h>
42 #include <vtkUnsignedLongArray.h>
43 #include <vtkFloatArray.h>
44 #include <vtkDoubleArray.h>
45
46 #include <vtkPointData.h>
47 #include <vtkTypeTraits.h>
48 #include <creaMessageManager.h>
49 namespace crea
50 {  
51   template <typename T>
52   /*CREA_EXPORT*/ vtkImageData* NewVtkImageDataFromRaw( T* data, 
53                                                     int nx, 
54                                                     int ny,
55                                                     int nz,
56                                                     bool do_not_desalloc)
57   {
58     //    std::cout << "NV "<<nx<<" " <<ny<<" " << nz<<std::endl;
59     //    std::cout <<  vtkTypeTraits<T>::SizedName() << std::endl;
60     vtkImageData *image = vtkImageData::New();
61
62     // Shouldn't we pass NumberOfScalarComponents to deal with RGB, RGBA images as well? // JPR
63
64
65     int vtktype = vtkTypeTraits<T>::VTKTypeID();
66     image->SetDimensions(nx, ny ,nz);
67     image->SetSpacing(1, 1, 1);
68
69 //EED 2017-01-01 Migration VTK7
70 #if (VTK_MAJOR_VERSION <= 5) 
71     image->SetNumberOfScalarComponents(1);
72     image->SetScalarType(vtktype);
73     image->AllocateScalars();
74 #endif
75 #if (VTK_MAJOR_VERSION >= 6) 
76     image->AllocateScalars(vtktype,1);
77 #endif
78
79
80     vtkDataArray* array = 0;
81     switch (vtktype)
82       {
83       case VTK_CHAR:
84         array = vtkCharArray::New(); break;
85       case VTK_SIGNED_CHAR:
86         array = vtkSignedCharArray::New(); break;
87       case VTK_UNSIGNED_CHAR:
88         array = vtkUnsignedCharArray::New(); break;
89       case VTK_SHORT:
90         array = vtkShortArray::New(); break;
91       case VTK_UNSIGNED_SHORT:
92         array = vtkUnsignedShortArray::New(); break;
93       case VTK_INT:
94         array = vtkIntArray::New(); break;      
95       case VTK_UNSIGNED_INT:
96         array = vtkUnsignedIntArray::New(); break;
97       case VTK_LONG:
98         array = vtkLongArray::New(); break;
99       case VTK_UNSIGNED_LONG:
100         array = vtkUnsignedLongArray::New(); break;
101       case VTK_FLOAT:
102         array = vtkFloatArray::New(); break;
103       case VTK_DOUBLE:
104         array = vtkDoubleArray::New(); break;
105       default:
106         creaGlobalError("NewVtkImageDataFromRaw : type "
107                   <<vtkTypeTraits<T>::SizedName()
108                   <<" non implemented");
109       }
110     
111 //EED 2022-07-21
112 // https://www.slicer.org/wiki/Documentation/Labs/VTK7
113 //    vtkDataArrayTemplate<T>* tarray = dynamic_cast<vtkDataArrayTemplate<T>*>(array);
114     vtkAOSDataArrayTemplate<T>* tarray = dynamic_cast<vtkAOSDataArrayTemplate<T>*>(array);
115     
116     array->SetNumberOfComponents( 1 );
117     size_t size = (long)nx*(long)ny*(long)nz;
118     // The last param of SetArray is set to 1 to keep the class from deleting the array 
119     // when it cleans up or reallocates memory.
120     int dndesa = 0;
121     if (do_not_desalloc)
122         dndesa = 1;
123     tarray->SetArray( data, size, dndesa );
124     image->GetPointData( )->SetScalars( tarray );
125     array->Delete( );
126     return image;
127   }
128   
129 }
130