]> Creatis software - bbtk.git/blob - packages/itkvtk/src/bbitkvtkitkImage2vtkImageData.cxx
2dd6a03951257d9c09521c370c09d78c19cd20ca
[bbtk.git] / packages / itkvtk / src / bbitkvtkitkImage2vtkImageData.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbitkvtkitkImage2vtkImageData.cxx,v $
4   Language:  C++
5   Date:      $Date: 2011/06/16 12:28:57 $
6   Version:   $Revision: 1.11 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability.
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
30
31 /**
32  *  \file 
33  *  \brief 
34  */
35 #ifdef _USE_ITK_
36 #ifdef _USE_VTK_
37
38 #include "bbitkvtkitkImage2vtkImageData.h"
39 #include "bbitkvtkPackage.h"
40 #include "itkImageToVTKImageFilter.h"
41
42
43 namespace bbitkvtk 
44 {
45   BBTK_BLACK_BOX_IMPLEMENTATION(itkImage2vtkImageData,bbtk::AtomicBlackBox);
46
47   BBTK_ADD_BLACK_BOX_TO_PACKAGE(itkvtk,itkImage2vtkImageData);
48
49   void itkImage2vtkImageData::Convert()
50   {
51     bbtk::TypeInfo t = bbGetInputIn().type();
52     BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t,Convert);
53   }
54
55    template<class T>
56    void itkImage2vtkImageData::Convert()
57    {
58     bbtkDebugMessage("process",5,"==> ["<<bbGetFullName()<<"] : Convert<"
59                      <<bbtk::TypeName<T>()
60                      <<">()"<<std::endl);
61
62     typedef T itkImageType;
63     typedef itkImageType* itkImageTypePointer;
64     typedef itk::ImageToVTKImageFilter< itkImageType > ItkToVtkConnection;
65     typename ItkToVtkConnection::Pointer conv;
66
67     // No converter yet : create it and set its input
68     if (!mConverter) 
69       {
70         bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : No converter yet : creating it"<<std::endl);
71         conv = ItkToVtkConnection::New();
72         mConverter = conv;
73         conv->SetInput( this->bbGetInputIn().get< itkImageTypePointer >() );
74       }
75     else 
76       {
77         bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Converter ok "<<std::endl);
78
79         // Input itkImageType type changed ? 
80         // Have to change the converter and set its input
81         conv = dynamic_cast<ItkToVtkConnection*>
82                            ((itk::ProcessObject*)mConverter);
83         if (!conv) 
84           {
85             bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Converter of wrong input type : reacreating it "<<std::endl);
86
87             mConverter->UnRegister();
88             conv = ItkToVtkConnection::New();
89             mConverter = conv;
90             conv->SetInput( this->bbGetInputIn().get<itkImageTypePointer>() );
91           }
92         // Input image type did not change but input image pointer did:
93         // set new input
94         else if ( this->bbGetInputIn().get<itkImageTypePointer>() 
95                   != (itkImageType*)(conv->GetExporter()->GetInputs()[0].GetPointer()))
96           {
97             bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Converter input changed : resetting it"<<std::endl);
98
99             conv->SetInput( this->bbGetInputIn().get<itkImageTypePointer>() );
100           }
101         else 
102           {
103             bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Nothing changed"<<std::endl);
104           }
105       }
106     try
107       {
108         
109         bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Trying update"<<std::endl);  
110
111         conv->Update();
112         bbSetOutputOut(conv->GetOutput());
113       }
114     catch( itk::ExceptionObject & e) 
115       {
116         bbtkError("itkImage2vtkImageData<"
117                   <<bbtk::TypeName<T>()
118                   <<">::Convert() : "<<e);
119       } 
120     bbtkDebugMessage("process",5,"<== ["<<bbGetFullName()<<"] : Convert<"
121                      <<bbtk::TypeName<T>()
122                      <<">() *DONE*"<<std::endl);
123  }
124
125   void itkImage2vtkImageData::bbUserSetDefaultValues()
126   {
127     bbSetOutputOut(NULL);
128     mConverter = 0;
129   }
130
131   void itkImage2vtkImageData::bbUserInitializeProcessing()
132   {
133   }
134
135   void itkImage2vtkImageData::bbUserFinalizeProcessing()
136   {
137   }
138
139
140 }
141 // eo namespace bbitkvtk
142
143 #endif
144 // _USE_VTK_
145 #endif
146 // _USE_ITK_