]> Creatis software - bbtk.git/blob - packages/itkvtk/src/bbitkvtkvtkImageDataVector2itkImageVector.cxx
Fix mistyping
[bbtk.git] / packages / itkvtk / src / bbitkvtkvtkImageDataVector2itkImageVector.cxx
1 /*=========================================================================
2                                                                                
3   Program:   bbtk
4   Module:    $RCSfile: bbitkvtkvtkImageDataVector2itkImageVector.cxx,v $
5   Language:  C++
6   Date:      $Date: 2011/06/16 13:38:49 $
7   Version:   $Revision: 1.2 $
8
9 =========================================================================*/
10
11 /* ---------------------------------------------------------------------
12
13 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
14 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
15 *
16 *  This software is governed by the CeCILL-B license under French law and 
17 *  abiding by the rules of distribution of free software. You can  use, 
18 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
19 *  license as circulated by CEA, CNRS and INRIA at the following URL 
20 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
21 *  or in the file LICENSE.txt.
22 *
23 *  As a counterpart to the access to the source code and  rights to copy,
24 *  modify and redistribute granted by the license, users are provided only
25 *  with a limited warranty  and the software's author,  the holder of the
26 *  economic rights,  and the successive licensors  have only  limited
27 *  liability.
28 *
29 *  The fact that you are presently reading this means that you have had
30 *  knowledge of the CeCILL-B license and that you accept its terms.
31 * ------------------------------------------------------------------------ */
32
33 /**
34  *  \file 
35  *  \brief 
36  */
37 #ifdef _USE_ITK_
38 #ifdef _USE_VTK_
39
40 #include "bbitkvtkvtkImageDataVector2itkImageVector.h"
41 #include "bbitkvtkPackage.h"
42 #include "itkVTKImageToImageFilter.h"
43
44
45 namespace bbitkvtk 
46 {
47
48   BBTK_BLACK_BOX_IMPLEMENTATION(vtkImageDataVector2itkImageVector,bbtk::AtomicBlackBox);
49
50   BBTK_ADD_BLACK_BOX_TO_PACKAGE(itkvtk,vtkImageDataVector2itkImageVector);
51
52 // we assume all the vector elements have the same type
53 #define BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,DIM,M)                           \
54   if (I[0]->GetDataDimension()==DIM)                                               \
55     {                                                                              \
56       if      (I[0]->GetScalarType()==VTK_CHAR) M<char,DIM>();                     \
57       else if (I[0]->GetScalarType()==VTK_SIGNED_CHAR) M<signed char,DIM>();       \
58       else if (I[0]->GetScalarType()==VTK_UNSIGNED_CHAR) M<unsigned char,DIM>();   \
59       else if (I[0]->GetScalarType()==VTK_SHORT) M<short,DIM>();                   \
60       else if (I[0]->GetScalarType()==VTK_UNSIGNED_SHORT) M<unsigned short,DIM>(); \
61       else if (I[0]->GetScalarType()==VTK_INT) M<int,DIM>();                       \
62       else if (I[0]->GetScalarType()==VTK_UNSIGNED_INT) M<unsigned int,DIM>();     \
63       else if (I[0]->GetScalarType()==VTK_LONG) M<long,DIM>();                     \
64       else if (I[0]->GetScalarType()==VTK_UNSIGNED_LONG) M<unsigned long,DIM>();   \
65       else if (I[0]->GetScalarType()==VTK_FLOAT) M<float,DIM>();                   \
66       else if (I[0]->GetScalarType()==VTK_DOUBLE) M<double,DIM>();                 \
67     }
68
69 #define BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH(I,M) \
70   BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,2,M) \
71   BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,3,M)
72
73   void vtkImageDataVector2itkImageVector::Convert()
74   {
75      BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH(bbGetInputIn(),Convert)
76   }
77
78
79    template<class T, unsigned int D>
80    void vtkImageDataVector2itkImageVector::Convert()
81    {
82     bbtkDebugMessage("process",5,"==> ["<<bbGetFullName()<<"] : Convert<"
83                         <<bbtk::TypeName<T>()<<","<<D
84                         <<">()"<<std::endl);
85
86
87     typedef itk::Image<T,D> itkImageType;
88     typedef itk::VTKImageToImageFilter< itkImageType > VtkToItkConnection;
89     typename VtkToItkConnection::Pointer conv;
90
91  int vectorSize = bbGetInputIn().size();
92  std::cout << " vtkImageDataVector2itkImageVector vector size :" << vectorSize << std::endl;    
93  for (int i=0; i< vectorSize ; i++)
94  {
95     
96     // No converter yet : create it and set its input
97     if (!mConverter) 
98       {
99         bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : No converter yet : creating it"<<std::endl);
100         conv = VtkToItkConnection::New();
101         mConverter = conv;
102         conv->SetInput( this->bbGetInputIn()[i] );
103       }
104     else 
105       {
106         bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Converter ok "<<std::endl);
107         // Input vtkImage type changed ? 
108         // Have to change the converter and set its input
109         conv = dynamic_cast<VtkToItkConnection*>
110                            ((itk::ProcessObject*)mConverter);
111         if (!conv) 
112           {
113             bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Converter of wrong input type : reacreating it "<<std::endl);
114
115             mConverter->UnRegister();
116             conv = VtkToItkConnection::New();
117             mConverter = conv;
118             conv->SetInput( this->bbGetInputIn()[i] );
119           }
120         // Input image type did not change but input image pointer did:
121         // set new input
122         else if ( this->bbGetInputIn()[i] != conv->GetExporter()->GetInput())
123           {
124             bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Converter input changed : resetting it"<<std::endl);
125
126             conv->SetInput( this->bbGetInputIn()[i] );
127           }
128         else 
129           {
130             bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Nothing changed"<<std::endl);     
131           }
132       }
133     try
134       {
135         
136         bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Trying update"<<std::endl);  
137         conv->Update();
138         itkImageType* out = const_cast<itkImageType*>(conv->GetOutput());
139         m_Output_Vector.push_back(out);
140
141       }
142     catch( itk::ExceptionObject & e) 
143       {
144         bbtkError("vtkImageDataVector2itkImageVector<"
145                   <<bbtk::TypeName<T>()<<","<<D
146                   <<">::Convert() : "<<e);
147       } 
148     bbtkDebugMessage("process",5,"<== ["<<bbGetFullName()<<"] : Convert<"
149                      <<bbtk::TypeName<T>()<<","<<D
150                      <<">() *DONE*"<<std::endl);
151 } // end iterate on vector size
152
153 bbSetOutputOut(m_Output_Vector);
154
155 }
156
157
158   void vtkImageDataVector2itkImageVector::bbUserSetDefaultValues()
159   {
160     //    bbSetOutputOut(NULL);
161     mConverter = 0;
162   }
163
164   void vtkImageDataVector2itkImageVector::bbUserInitializeProcessing()
165   {
166   }
167
168   void vtkImageDataVector2itkImageVector::bbUserFinalizeProcessing()
169   {
170   }
171
172
173 }
174 // eo namespace bbtk
175
176 #endif
177 // _USE_VTK_
178 #endif
179 // _USE_ITK_