]> Creatis software - bbtk.git/blob - packages/itkvtk/src/bbitkvtkitkImageVector2vtkImageDataVector.cxx
Feature #1774
[bbtk.git] / packages / itkvtk / src / bbitkvtkitkImageVector2vtkImageDataVector.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   Program:   bbtk
30   Module:    $RCSfile: bbitkvtkitkImageVector2vtkImageDataVector.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:50:49 $
33   Version:   $Revision: 1.2 $
34 =========================================================================*/
35
36
37
38 /**
39  *  \file 
40  *  \brief 
41  */
42 #ifdef _USE_ITK_
43 #ifdef _USE_VTK_
44
45 #include "bbitkvtkitkImageVector2vtkImageDataVector.h"
46 #include "bbitkvtkPackage.h"
47 #include "itkImageToVTKImageFilter.h"
48
49
50 namespace bbitkvtk 
51 {
52   BBTK_BLACK_BOX_IMPLEMENTATION(itkImageVector2vtkImageDataVector,bbtk::AtomicBlackBox);
53
54   BBTK_ADD_BLACK_BOX_TO_PACKAGE(itkvtk,itkImageVector2vtkImageDataVector);
55
56   void itkImageVector2vtkImageDataVector::Convert()
57   {
58   // suppose *all* the images have the same type // JPR
59     bbtk::TypeInfo t = bbGetInputIn()[0].type();
60     BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t,Convert);
61   }
62
63    template<class T>
64    void itkImageVector2vtkImageDataVector::Convert()
65    {
66     bbtkDebugMessage("process",5,"==> ["<<bbGetFullName()<<"] : Convert<"
67                      <<bbtk::TypeName<T>()
68                      <<">()"<<std::endl);
69
70     typedef T itkImageType;
71     typedef itkImageType* itkImageTypePointer;
72     typedef itk::ImageToVTKImageFilter< itkImageType > ItkToVtkConnection;
73     typename ItkToVtkConnection::Pointer conv;
74
75 int vectorSize = bbGetInputIn().size();
76 std::cout << " itkImageVector2vtkImageDataVector vector size :" << vectorSize << std::endl;
77 for (int i=0; i< vectorSize ; i++)
78 {
79     // No converter yet : create it and set its input
80     if (!mConverter) 
81       {
82         bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : No converter yet : creating it"<<std::endl);
83         conv = ItkToVtkConnection::New();
84         mConverter = conv;
85         conv->SetInput( this->bbGetInputIn()[i].get< itkImageTypePointer >() );
86       }
87     else 
88       {
89         bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Converter ok "<<std::endl);
90         
91         // Input itkImageType type changed ? 
92         // Have to change the converter and set its input
93         conv = dynamic_cast<ItkToVtkConnection*>
94           ((itk::ProcessObject*)mConverter);
95         if (!conv) 
96           {
97             bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Converter of wrong input type : reacreating it "<<std::endl);
98
99             mConverter->UnRegister();
100             conv = ItkToVtkConnection::New();
101             mConverter = conv;
102             conv->SetInput( this->bbGetInputIn()[i].get<itkImageTypePointer>() );
103           }
104         // Input image type did not change but input image pointer did:
105         // set new input
106         else if ( this->bbGetInputIn()[i].get<itkImageTypePointer>() 
107                   != (itkImageType*)(conv->GetExporter()->GetInputs()[0].GetPointer()))
108           {
109             bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Converter input changed : resetting it"<<std::endl);
110
111             conv->SetInput( this->bbGetInputIn()[i].get<itkImageTypePointer>() );
112           }
113         else 
114           {
115             bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Nothing changed"<<std::endl);
116           }
117       }
118     try
119       {
120         bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Trying update"<<std::endl);  
121
122         conv->Update();
123         m_Output_Vector.push_back(conv->GetOutput());
124       }
125     catch( itk::ExceptionObject & e) 
126       {
127         bbtkError("itkImageVector2vtkImageDataVector<"
128                   <<bbtk::TypeName<T>()
129                   <<">::Convert() : "<<e);
130       } 
131     bbtkDebugMessage("process",5,"<== ["<<bbGetFullName()<<"] : Convert<"
132                      <<bbtk::TypeName<T>()
133                      <<">() *DONE*"<<std::endl);
134                      
135 } // end iterate on vector size
136
137 bbSetOutputOut(m_Output_Vector);
138    
139 }
140
141
142   void itkImageVector2vtkImageDataVector::bbUserSetDefaultValues()
143   {
144    // bbSetOutputOut(std::vector<vtkImageData*>); // syntax? JPR
145     mConverter = 0;
146   }
147
148   void itkImageVector2vtkImageDataVector::bbUserInitializeProcessing()
149         {
150         }
151         
152   void itkImageVector2vtkImageDataVector::bbUserFinalizeProcessing()    
153         {
154         }
155
156         
157 }
158 // eo namespace bbitkvtk
159
160 #endif
161 // _USE_VTK_
162 #endif
163 // _USE_ITK_