]> Creatis software - bbtk.git/blob - packages/itkvtk/src/bbitkvtkvtkImageDataVector2itkImageVector.cxx
Feature #1774
[bbtk.git] / packages / itkvtk / src / bbitkvtkvtkImageDataVector2itkImageVector.cxx
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                                                                                
30   Program:   bbtk
31   Module:    $RCSfile: bbitkvtkvtkImageDataVector2itkImageVector.cxx,v $
32   Language:  C++
33   Date:      $Date: 2012/11/16 08:50:49 $
34   Version:   $Revision: 1.3 $
35
36 =========================================================================*/
37
38
39
40 /**
41  *  \file 
42  *  \brief 
43  */
44 #ifdef _USE_ITK_
45 #ifdef _USE_VTK_
46
47 #include "bbitkvtkvtkImageDataVector2itkImageVector.h"
48 #include "bbitkvtkPackage.h"
49 #include "itkVTKImageToImageFilter.h"
50
51
52 namespace bbitkvtk 
53 {
54
55   BBTK_BLACK_BOX_IMPLEMENTATION(vtkImageDataVector2itkImageVector,bbtk::AtomicBlackBox);
56
57   BBTK_ADD_BLACK_BOX_TO_PACKAGE(itkvtk,vtkImageDataVector2itkImageVector);
58
59 // we assume all the vector elements have the same type
60 #define BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,DIM,M)                           \
61   if (I[0]->GetDataDimension()==DIM)                                               \
62     {                                                                              \
63       if      (I[0]->GetScalarType()==VTK_CHAR) M<char,DIM>();                     \
64       else if (I[0]->GetScalarType()==VTK_SIGNED_CHAR) M<signed char,DIM>();       \
65       else if (I[0]->GetScalarType()==VTK_UNSIGNED_CHAR) M<unsigned char,DIM>();   \
66       else if (I[0]->GetScalarType()==VTK_SHORT) M<short,DIM>();                   \
67       else if (I[0]->GetScalarType()==VTK_UNSIGNED_SHORT) M<unsigned short,DIM>(); \
68       else if (I[0]->GetScalarType()==VTK_INT) M<int,DIM>();                       \
69       else if (I[0]->GetScalarType()==VTK_UNSIGNED_INT) M<unsigned int,DIM>();     \
70       else if (I[0]->GetScalarType()==VTK_LONG) M<long,DIM>();                     \
71       else if (I[0]->GetScalarType()==VTK_UNSIGNED_LONG) M<unsigned long,DIM>();   \
72       else if (I[0]->GetScalarType()==VTK_FLOAT) M<float,DIM>();                   \
73       else if (I[0]->GetScalarType()==VTK_DOUBLE) M<double,DIM>();                 \
74     }
75
76 #define BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH(I,M) \
77   BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,2,M) \
78   BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,3,M)
79
80   void vtkImageDataVector2itkImageVector::Convert()
81   {
82      BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH(bbGetInputIn(),Convert)
83   }
84
85
86    template<class T, unsigned int D>
87    void vtkImageDataVector2itkImageVector::Convert()
88    {
89     bbtkDebugMessage("process",5,"==> ["<<bbGetFullName()<<"] : Convert<"
90                         <<bbtk::TypeName<T>()<<","<<D
91                         <<">()"<<std::endl);
92
93
94     typedef itk::Image<T,D> itkImageType;
95     typedef itk::VTKImageToImageFilter< itkImageType > VtkToItkConnection;
96     typename VtkToItkConnection::Pointer conv;
97
98  int vectorSize = bbGetInputIn().size();
99  std::cout << " vtkImageDataVector2itkImageVector vector size :" << vectorSize << std::endl;    
100  for (int i=0; i< vectorSize ; i++)
101  {
102     
103     // No converter yet : create it and set its input
104     if (!mConverter) 
105       {
106         bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : No converter yet : creating it"<<std::endl);
107         conv = VtkToItkConnection::New();
108         mConverter = conv;
109         conv->SetInput( this->bbGetInputIn()[i] );
110       }
111     else 
112       {
113         bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Converter ok "<<std::endl);
114         // Input vtkImage type changed ? 
115         // Have to change the converter and set its input
116         conv = dynamic_cast<VtkToItkConnection*>
117                            ((itk::ProcessObject*)mConverter);
118         if (!conv) 
119           {
120             bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Converter of wrong input type : reacreating it "<<std::endl);
121
122             mConverter->UnRegister();
123             conv = VtkToItkConnection::New();
124             mConverter = conv;
125             conv->SetInput( this->bbGetInputIn()[i] );
126           }
127         // Input image type did not change but input image pointer did:
128         // set new input
129         else if ( this->bbGetInputIn()[i] != conv->GetExporter()->GetInput())
130           {
131             bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Converter input changed : resetting it"<<std::endl);
132
133             conv->SetInput( this->bbGetInputIn()[i] );
134           }
135         else 
136           {
137             bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Nothing changed"<<std::endl);     
138           }
139       }
140     try
141       {
142         
143         bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Trying update"<<std::endl);  
144         conv->Update();
145         itkImageType* out = const_cast<itkImageType*>(conv->GetOutput());
146         m_Output_Vector.push_back(out);
147
148       }
149     catch( itk::ExceptionObject & e) 
150       {
151         bbtkError("vtkImageDataVector2itkImageVector<"
152                   <<bbtk::TypeName<T>()<<","<<D
153                   <<">::Convert() : "<<e);
154       } 
155     bbtkDebugMessage("process",5,"<== ["<<bbGetFullName()<<"] : Convert<"
156                      <<bbtk::TypeName<T>()<<","<<D
157                      <<">() *DONE*"<<std::endl);
158 } // end iterate on vector size
159
160 bbSetOutputOut(m_Output_Vector);
161
162 }
163
164
165   void vtkImageDataVector2itkImageVector::bbUserSetDefaultValues()
166   {
167     //    bbSetOutputOut(NULL);
168     mConverter = 0;
169   }
170
171   void vtkImageDataVector2itkImageVector::bbUserInitializeProcessing()
172   {
173   }
174
175   void vtkImageDataVector2itkImageVector::bbUserFinalizeProcessing()
176   {
177   }
178
179
180 }
181 // eo namespace bbtk
182
183 #endif
184 // _USE_VTK_
185 #endif
186 // _USE_ITK_