From: jean-pierre roux <jean-pierre.roux@creatis.insa-lyon.fr> Date: Thu, 16 Jun 2011 12:28:57 +0000 (+0000) Subject: Add converters between std::vector of vtkImageData* and bbitk::anyImagePointer X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=48d234bd8fb5c1251b67a85599f832db414500ad;p=bbtk.git Add converters between std::vector of vtkImageData* and bbitk::anyImagePointer --- diff --git a/packages/itkvtk/src/bbitkvtkitkImage2vtkImageData.cxx b/packages/itkvtk/src/bbitkvtkitkImage2vtkImageData.cxx index 0a68db1..2dd6a03 100644 --- a/packages/itkvtk/src/bbitkvtkitkImage2vtkImageData.cxx +++ b/packages/itkvtk/src/bbitkvtkitkImage2vtkImageData.cxx @@ -2,8 +2,8 @@ Program: bbtk Module: $RCSfile: bbitkvtkitkImage2vtkImageData.cxx,v $ Language: C++ - Date: $Date: 2010/02/08 15:15:25 $ - Version: $Revision: 1.10 $ + Date: $Date: 2011/06/16 12:28:57 $ + Version: $Revision: 1.11 $ =========================================================================*/ /* --------------------------------------------------------------------- @@ -22,11 +22,11 @@ * modify and redistribute granted by the license, users are provided only * with a limited warranty and the software's author, the holder of the * economic rights, and the successive licensors have only limited -* liability. +* liability. * * The fact that you are presently reading this means that you have had * knowledge of the CeCILL-B license and that you accept its terms. -* ------------------------------------------------------------------------ */ +* ------------------------------------------------------------------------ */ /** * \file @@ -37,9 +37,6 @@ #include "bbitkvtkitkImage2vtkImageData.h" #include "bbitkvtkPackage.h" -// VtkToItkConnection -//#include "itkVTKImageToImageFilter.h" -// ItkToVtkConnection #include "itkImageToVTKImageFilter.h" @@ -63,7 +60,7 @@ namespace bbitkvtk <<">()"<<std::endl); typedef T itkImageType; - typedef itkImageType* itkImageTypePointer; + typedef itkImageType* itkImageTypePointer; typedef itk::ImageToVTKImageFilter< itkImageType > ItkToVtkConnection; typename ItkToVtkConnection::Pointer conv; @@ -78,11 +75,11 @@ namespace bbitkvtk else { bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : Converter ok "<<std::endl); - + // Input itkImageType type changed ? // Have to change the converter and set its input conv = dynamic_cast<ItkToVtkConnection*> - ((itk::ProcessObject*)mConverter); + ((itk::ProcessObject*)mConverter); if (!conv) { bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : Converter of wrong input type : reacreating it "<<std::endl); @@ -132,14 +129,14 @@ namespace bbitkvtk } void itkImage2vtkImageData::bbUserInitializeProcessing() - { - } - - void itkImage2vtkImageData::bbUserFinalizeProcessing() - { - } + { + } + + void itkImage2vtkImageData::bbUserFinalizeProcessing() + { + } + - } // eo namespace bbitkvtk diff --git a/packages/itkvtk/src/bbitkvtkitkImage2vtkImageData.h b/packages/itkvtk/src/bbitkvtkitkImage2vtkImageData.h index dd773b9..b0b724a 100644 --- a/packages/itkvtk/src/bbitkvtkitkImage2vtkImageData.h +++ b/packages/itkvtk/src/bbitkvtkitkImage2vtkImageData.h @@ -2,8 +2,8 @@ Program: bbtk Module: $RCSfile: bbitkvtkitkImage2vtkImageData.h,v $ Language: C++ - Date: $Date: 2009/05/14 16:21:15 $ - Version: $Revision: 1.9 $ + Date: $Date: 2011/06/16 12:28:57 $ + Version: $Revision: 1.10 $ =========================================================================*/ /* --------------------------------------------------------------------- @@ -30,11 +30,11 @@ /** * \file - * \brief class itkImage2vtkImageData : converts a generic itkImage to a vtkImageData + * \brief class itkImage2vtkImageData : converts a generic itkImage to a vtkImageData* */ /** * \class itkImage2vtkImageData - * \brief converts a generic itkImage to a vtkImageData + * \brief converts a generic itkImage to a vtkImageData* */ #ifdef _USE_ITK_ #ifdef _USE_VTK_ @@ -55,10 +55,11 @@ namespace bbitkvtk BBTK_BLACK_BOX_INTERFACE(itkImage2vtkImageData, bbtk::AtomicBlackBox); - BBTK_DECLARE_INPUT(In,bbitk::anyImagePointer); + BBTK_DECLARE_INPUT(In, bbitk::anyImagePointer); BBTK_DECLARE_OUTPUT(Out,vtkImageData*); BBTK_PROCESS(Convert); void Convert(); + private: // Convert method template on type of the itk image // embedded in the input itkImage* @@ -78,11 +79,11 @@ namespace bbitkvtk BBTK_INPUT(itkImage2vtkImageData,In, "Input itkImage",bbitk::anyImagePointer,""); BBTK_OUTPUT(itkImage2vtkImageData,Out, - "Output vtkImageData", + "Output vtkImageData*", vtkImageData*,""); BBTK_END_DESCRIBE_BLACK_BOX(itkImage2vtkImageData); //================================================================= - + } //namespace bbitkvtk diff --git a/packages/itkvtk/src/bbitkvtkitkImageVector2vtkImageDataVector.cxx b/packages/itkvtk/src/bbitkvtkitkImageVector2vtkImageDataVector.cxx new file mode 100644 index 0000000..435719c --- /dev/null +++ b/packages/itkvtk/src/bbitkvtkitkImageVector2vtkImageDataVector.cxx @@ -0,0 +1,156 @@ +/*========================================================================= + Program: bbtk + Module: $RCSfile: bbitkvtkitkImageVector2vtkImageDataVector.cxx,v $ + Language: C++ + Date: $Date: 2011/06/16 12:28:57 $ + Version: $Revision: 1.1 $ +=========================================================================*/ + +/* --------------------------------------------------------------------- + +* Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale) +* Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux +* +* This software is governed by the CeCILL-B license under French law and +* abiding by the rules of distribution of free software. You can use, +* modify and/ or redistribute the software under the terms of the CeCILL-B +* license as circulated by CEA, CNRS and INRIA at the following URL +* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +* or in the file LICENSE.txt. +* +* As a counterpart to the access to the source code and rights to copy, +* modify and redistribute granted by the license, users are provided only +* with a limited warranty and the software's author, the holder of the +* economic rights, and the successive licensors have only limited +* liability. +* +* The fact that you are presently reading this means that you have had +* knowledge of the CeCILL-B license and that you accept its terms. +* ------------------------------------------------------------------------ */ + +/** + * \file + * \brief + */ +#ifdef _USE_ITK_ +#ifdef _USE_VTK_ + +#include "bbitkvtkitkImageVector2vtkImageDataVector.h" +#include "bbitkvtkPackage.h" +#include "itkImageToVTKImageFilter.h" + + +namespace bbitkvtk +{ + BBTK_BLACK_BOX_IMPLEMENTATION(itkImageVector2vtkImageDataVector,bbtk::AtomicBlackBox); + + BBTK_ADD_BLACK_BOX_TO_PACKAGE(itkvtk,itkImageVector2vtkImageDataVector); + + void itkImageVector2vtkImageDataVector::Convert() + { + // suppose *all* the images have the same type // JPR + bbtk::TypeInfo t = bbGetInputIn()[0].type(); + BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t,Convert); + } + + template<class T> + void itkImageVector2vtkImageDataVector::Convert() + { + bbtkDebugMessage("process",5,"==> ["<<bbGetFullName()<<"] : Convert<" + <<bbtk::TypeName<T>() + <<">()"<<std::endl); + + typedef T itkImageType; + typedef itkImageType* itkImageTypePointer; + typedef itk::ImageToVTKImageFilter< itkImageType > ItkToVtkConnection; + typename ItkToVtkConnection::Pointer conv; + +int vectorSize = bbGetInputIn().size(); +std::cout << " itkImageVector2vtkImageDataVector vector size :" << vectorSize << std::endl; +for (int i=0; i< vectorSize ; i++) +{ + // No converter yet : create it and set its input + if (!mConverter) + { + bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : No converter yet : creating it"<<std::endl); + conv = ItkToVtkConnection::New(); + mConverter = conv; + conv->SetInput( this->bbGetInputIn()[i].get< itkImageTypePointer >() ); + } + else + { + bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : Converter ok "<<std::endl); + + // Input itkImageType type changed ? + // Have to change the converter and set its input + conv = dynamic_cast<ItkToVtkConnection*> + ((itk::ProcessObject*)mConverter); + if (!conv) + { + bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : Converter of wrong input type : reacreating it "<<std::endl); + + mConverter->UnRegister(); + conv = ItkToVtkConnection::New(); + mConverter = conv; + conv->SetInput( this->bbGetInputIn()[i].get<itkImageTypePointer>() ); + } + // Input image type did not change but input image pointer did: + // set new input + else if ( this->bbGetInputIn()[i].get<itkImageTypePointer>() + != (itkImageType*)(conv->GetExporter()->GetInputs()[0].GetPointer())) + { + bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : Converter input changed : resetting it"<<std::endl); + + conv->SetInput( this->bbGetInputIn()[i].get<itkImageTypePointer>() ); + } + else + { + bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : Nothing changed"<<std::endl); + } + } + try + { + bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : Trying update"<<std::endl); + + conv->Update(); + m_Output_Vector.push_back(conv->GetOutput()); + } + catch( itk::ExceptionObject & e) + { + bbtkError("itkImageVector2vtkImageDataVector<" + <<bbtk::TypeName<T>() + <<">::Convert() : "<<e); + } + bbtkDebugMessage("process",5,"<== ["<<bbGetFullName()<<"] : Convert<" + <<bbtk::TypeName<T>() + <<">() *DONE*"<<std::endl); + +} // end iterate on vector size + +bbSetOutputOut(m_Output_Vector); + +} + + + void itkImageVector2vtkImageDataVector::bbUserSetDefaultValues() + { + // bbSetOutputOut(std::vector<vtkImageData*>); // syntax? JPR + mConverter = 0; + } + + void itkImageVector2vtkImageDataVector::bbUserInitializeProcessing() + { + } + + void itkImageVector2vtkImageDataVector::bbUserFinalizeProcessing() + { + } + + +} +// eo namespace bbitkvtk + +#endif +// _USE_VTK_ +#endif +// _USE_ITK_ diff --git a/packages/itkvtk/src/bbitkvtkitkImageVector2vtkImageDataVector.h b/packages/itkvtk/src/bbitkvtkitkImageVector2vtkImageDataVector.h new file mode 100644 index 0000000..946d0fb --- /dev/null +++ b/packages/itkvtk/src/bbitkvtkitkImageVector2vtkImageDataVector.h @@ -0,0 +1,95 @@ +/*========================================================================= + Program: bbtk + Module: $RCSfile: bbitkvtkitkImageVector2vtkImageDataVector.h,v $ + Language: C++ + Date: $Date: 2011/06/16 12:28:57 $ + Version: $Revision: 1.1 $ +=========================================================================*/ + +/* --------------------------------------------------------------------- + +* Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale) +* Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux +* +* This software is governed by the CeCILL-B license under French law and +* abiding by the rules of distribution of free software. You can use, +* modify and/ or redistribute the software under the terms of the CeCILL-B +* license as circulated by CEA, CNRS and INRIA at the following URL +* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +* or in the file LICENSE.txt. +* +* As a counterpart to the access to the source code and rights to copy, +* modify and redistribute granted by the license, users are provided only +* with a limited warranty and the software's author, the holder of the +* economic rights, and the successive licensors have only limited +* liability. +* +* The fact that you are presently reading this means that you have had +* knowledge of the CeCILL-B license and that you accept its terms. +* ------------------------------------------------------------------------ */ + +/** + * \file + * \brief class itkImage2vtkImageData : converts a std::vector of generic itkImage to a std::vector of vtkImageData* + */ +/** + * \class itkImage2vtkImageData + * \brief converts a std::vector of generic itkImage to a std::vector of vtkImageData* + */ +#ifdef _USE_ITK_ +#ifdef _USE_VTK_ + +#ifndef __bbitkvtkitkImageVector2vtkImageDataVector_h__ +#define __bbitkvtkitkImageVector2vtkImageDataVector_h__ + +#include "bbtkAtomicBlackBox.h" +#include "bbitkImage.h" +#include "vtkImageData.h" + +namespace bbitkvtk +{ + + class itkImageVector2vtkImageDataVector : public bbtk::AtomicBlackBox + { + + BBTK_BLACK_BOX_INTERFACE(itkImageVector2vtkImageDataVector, + bbtk::AtomicBlackBox); + + BBTK_DECLARE_INPUT(In, std::vector<bbitk::anyImagePointer>); + BBTK_DECLARE_OUTPUT(Out, std::vector<vtkImageData*>); + BBTK_PROCESS(Convert); + void Convert(); + private: + // Convert method template on type of the itk image + // embedded in the input itkImage* + template<class T> void Convert(); + // stores the pointer on the template itk::ImageToVTKImageFilter + itk::ProcessObject::Pointer mConverter; + std::vector<vtkImageData*> m_Output_Vector; + }; + + + //================================================================= + // AtomicBlackBox description + BBTK_BEGIN_DESCRIBE_BLACK_BOX(itkImageVector2vtkImageDataVector,bbtk::AtomicBlackBox); + BBTK_NAME("itkImageVector2vtkImageDataVector"); + BBTK_AUTHOR("jpr@creatis.insa-lyon.fr"); + BBTK_DESCRIPTION("Converts a std::vector of generic itkImage to a std::vector of vtkImageData*"); + BBTK_DEFAULT_ADAPTOR(); + BBTK_INPUT(itkImageVector2vtkImageDataVector,In, + "Input std::vector of itkImage",std::vector<bbitk::anyImagePointer>,""); + BBTK_OUTPUT(itkImageVector2vtkImageDataVector,Out, + "Output std::vector of vtkImageData*", + std::vector<vtkImageData*>,""); + BBTK_END_DESCRIBE_BLACK_BOX(itkImageVector2vtkImageDataVector); + //================================================================= + + +} +//namespace bbitkvtk +#endif +// _USE_VTK_ +#endif +// _USE_ITK_ + +#endif diff --git a/packages/itkvtk/src/bbitkvtkvtkImageData2itkImage.cxx b/packages/itkvtk/src/bbitkvtkvtkImageData2itkImage.cxx index c1fb034..ceee695 100644 --- a/packages/itkvtk/src/bbitkvtkvtkImageData2itkImage.cxx +++ b/packages/itkvtk/src/bbitkvtkvtkImageData2itkImage.cxx @@ -1,20 +1,35 @@ /*========================================================================= - + Program: bbtk Module: $RCSfile: bbitkvtkvtkImageData2itkImage.cxx,v $ Language: C++ - Date: $Date: 2009/05/14 16:21:15 $ - Version: $Revision: 1.5 $ - - Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de - l'Image). All rights reserved. See Doc/License.txt or - http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details. - - This software is distributed WITHOUT ANY WARRANTY; without even - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - PURPOSE. See the above copyright notices for more information. - + Date: $Date: 2011/06/16 12:28:57 $ + Version: $Revision: 1.6 $ + =========================================================================*/ + +/* --------------------------------------------------------------------- + +* Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale) +* Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux +* +* This software is governed by the CeCILL-B license under French law and +* abiding by the rules of distribution of free software. You can use, +* modify and/ or redistribute the software under the terms of the CeCILL-B +* license as circulated by CEA, CNRS and INRIA at the following URL +* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +* or in the file LICENSE.txt. +* +* As a counterpart to the access to the source code and rights to copy, +* modify and redistribute granted by the license, users are provided only +* with a limited warranty and the software's author, the holder of the +* economic rights, and the successive licensors have only limited +* liability. +* +* The fact that you are presently reading this means that you have had +* knowledge of the CeCILL-B license and that you accept its terms. +* ------------------------------------------------------------------------ */ + /** * \file * \brief @@ -24,53 +39,49 @@ #include "bbitkvtkvtkImageData2itkImage.h" #include "bbitkvtkPackage.h" -// VtkToItkConnection #include "itkVTKImageToImageFilter.h" -// ItkToVtkConnection -//#include "itkImageToVTKImageFilter.h" namespace bbitkvtk { - + BBTK_BLACK_BOX_IMPLEMENTATION(vtkImageData2itkImage,bbtk::AtomicBlackBox); BBTK_ADD_BLACK_BOX_TO_PACKAGE(itkvtk,vtkImageData2itkImage); - -#define BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,DIM,M) \ - if (I->GetDataDimension()==DIM) \ - { \ - if (I->GetScalarType()==VTK_CHAR) M<char,DIM>(); \ - else if (I->GetScalarType()==VTK_SIGNED_CHAR) M<signed char,DIM>(); \ - else if (I->GetScalarType()==VTK_UNSIGNED_CHAR) M<unsigned char,DIM>(); \ - else if (I->GetScalarType()==VTK_SHORT) M<short,DIM>(); \ + +#define BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,DIM,M) \ + if (I->GetDataDimension()==DIM) \ + { \ + if (I->GetScalarType()==VTK_CHAR) M<char,DIM>(); \ + else if (I->GetScalarType()==VTK_SIGNED_CHAR) M<signed char,DIM>(); \ + else if (I->GetScalarType()==VTK_UNSIGNED_CHAR) M<unsigned char,DIM>(); \ + else if (I->GetScalarType()==VTK_SHORT) M<short,DIM>(); \ else if (I->GetScalarType()==VTK_UNSIGNED_SHORT) M<unsigned short,DIM>(); \ - else if (I->GetScalarType()==VTK_INT) M<int,DIM>(); \ - else if (I->GetScalarType()==VTK_UNSIGNED_INT) M<unsigned int,DIM>(); \ - else if (I->GetScalarType()==VTK_LONG) M<long,DIM>(); \ - else if (I->GetScalarType()==VTK_UNSIGNED_LONG) M<unsigned long,DIM>(); \ - else if (I->GetScalarType()==VTK_FLOAT) M<float,DIM>(); \ - else if (I->GetScalarType()==VTK_DOUBLE) M<double,DIM>(); \ + else if (I->GetScalarType()==VTK_INT) M<int,DIM>(); \ + else if (I->GetScalarType()==VTK_UNSIGNED_INT) M<unsigned int,DIM>(); \ + else if (I->GetScalarType()==VTK_LONG) M<long,DIM>(); \ + else if (I->GetScalarType()==VTK_UNSIGNED_LONG) M<unsigned long,DIM>(); \ + else if (I->GetScalarType()==VTK_FLOAT) M<float,DIM>(); \ + else if (I->GetScalarType()==VTK_DOUBLE) M<double,DIM>(); \ } - + #define BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH(I,M) \ BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,2,M) \ - BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,3,M) + BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,3,M) void vtkImageData2itkImage::Convert() { BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH(bbGetInputIn(),Convert) } - template<class T, unsigned int D> void vtkImageData2itkImage::Convert() - { + { bbtkDebugMessage("process",5,"==> ["<<bbGetFullName()<<"] : Convert<" <<bbtk::TypeName<T>()<<","<<D <<">()"<<std::endl); - + typedef itk::Image<T,D> itkImageType; typedef itk::VTKImageToImageFilter< itkImageType > VtkToItkConnection; @@ -90,10 +101,11 @@ namespace bbitkvtk // Input vtkImage type changed ? // Have to change the converter and set its input conv = dynamic_cast<VtkToItkConnection*> - ((itk::ProcessObject*)mConverter); + ((itk::ProcessObject*)mConverter); if (!conv) { bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : Converter of wrong input type : reacreating it "<<std::endl); + mConverter->UnRegister(); conv = VtkToItkConnection::New(); mConverter = conv; @@ -104,6 +116,7 @@ namespace bbitkvtk else if ( this->bbGetInputIn() != conv->GetExporter()->GetInput()) { bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : Converter input changed : resetting it"<<std::endl); + conv->SetInput( this->bbGetInputIn() ); } else @@ -117,7 +130,7 @@ namespace bbitkvtk bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : Trying update"<<std::endl); conv->Update(); itkImageType* out = const_cast<itkImageType*>(conv->GetOutput()); - bbSetOutputOut(out); + bbSetOutputOut(out); } catch( itk::ExceptionObject & e) { @@ -127,25 +140,23 @@ namespace bbitkvtk } bbtkDebugMessage("process",5,"<== ["<<bbGetFullName()<<"] : Convert<" <<bbtk::TypeName<T>()<<","<<D - <<">() *DONE*"<<std::endl); - } + <<">() *DONE*"<<std::endl); + } + - void vtkImageData2itkImage::bbUserSetDefaultValues() { // bbSetOutputOut(NULL); mConverter = 0; } - + void vtkImageData2itkImage::bbUserInitializeProcessing() { } - + void vtkImageData2itkImage::bbUserFinalizeProcessing() { } - - } diff --git a/packages/itkvtk/src/bbitkvtkvtkImageData2itkImage.h b/packages/itkvtk/src/bbitkvtkvtkImageData2itkImage.h index b1e1f26..71538dc 100644 --- a/packages/itkvtk/src/bbitkvtkvtkImageData2itkImage.h +++ b/packages/itkvtk/src/bbitkvtkvtkImageData2itkImage.h @@ -3,8 +3,8 @@ Program: bbtk Module: $RCSfile: bbitkvtkvtkImageData2itkImage.h,v $ Language: C++ - Date: $Date: 2009/05/18 10:45:46 $ - Version: $Revision: 1.7 $ + Date: $Date: 2011/06/16 12:28:57 $ + Version: $Revision: 1.8 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -36,7 +36,7 @@ namespace bbitkvtk { - + class /*BBTK_EXPORT*/ vtkImageData2itkImage : public bbtk::AtomicBlackBox { @@ -70,7 +70,7 @@ namespace bbitkvtk bbitk::anyImagePointer,""); BBTK_END_DESCRIBE_BLACK_BOX(vtkImageData2itkImage); //================================================================= - + } //namespace bbitkvtk diff --git a/packages/itkvtk/src/bbitkvtkvtkImageDataVector2itkImageVector.cxx b/packages/itkvtk/src/bbitkvtkvtkImageDataVector2itkImageVector.cxx new file mode 100644 index 0000000..678d43c --- /dev/null +++ b/packages/itkvtk/src/bbitkvtkvtkImageDataVector2itkImageVector.cxx @@ -0,0 +1,178 @@ +/*========================================================================= + + Program: bbtk + Module: $RCSfile: bbitkvtkvtkImageDataVector2itkImageVector.cxx,v $ + Language: C++ + Date: $Date: 2011/06/16 12:28:57 $ + Version: $Revision: 1.1 $ + +=========================================================================*/ + +/* --------------------------------------------------------------------- + +* Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale) +* Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux +* +* This software is governed by the CeCILL-B license under French law and +* abiding by the rules of distribution of free software. You can use, +* modify and/ or redistribute the software under the terms of the CeCILL-B +* license as circulated by CEA, CNRS and INRIA at the following URL +* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +* or in the file LICENSE.txt. +* +* As a counterpart to the access to the source code and rights to copy, +* modify and redistribute granted by the license, users are provided only +* with a limited warranty and the software's author, the holder of the +* economic rights, and the successive licensors have only limited +* liability. +* +* The fact that you are presently reading this means that you have had +* knowledge of the CeCILL-B license and that you accept its terms. +* ------------------------------------------------------------------------ */ + +/** + * \file + * \brief + */ +#ifdef _USE_ITK_ +#ifdef _USE_VTK_ + +#include "bbitkvtkvtkImageDataVector2itkImageVector.h" +#include "bbitkvtkPackage.h" +#include "itkVTKImageToImageFilter.h" + + +namespace bbitkvtk +{ + + BBTK_BLACK_BOX_IMPLEMENTATION(vtkImageDataVector2itkImageVector,bbtk::AtomicBlackBox); + + BBTK_ADD_BLACK_BOX_TO_PACKAGE(itkvtk,vtkImageDataVector2itkImageVector); + +#define BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,DIM,M) \ + if (I->GetDataDimension()==DIM) \ + { \ + if (I->GetScalarType()==VTK_CHAR) M<char,DIM>(); \ + else if (I->GetScalarType()==VTK_SIGNED_CHAR) M<signed char,DIM>(); \ + else if (I->GetScalarType()==VTK_UNSIGNED_CHAR) M<unsigned char,DIM>(); \ + else if (I->GetScalarType()==VTK_SHORT) M<short,DIM>(); \ + else if (I->GetScalarType()==VTK_UNSIGNED_SHORT) M<unsigned short,DIM>(); \ + else if (I->GetScalarType()==VTK_INT) M<int,DIM>(); \ + else if (I->GetScalarType()==VTK_UNSIGNED_INT) M<unsigned int,DIM>(); \ + else if (I->GetScalarType()==VTK_LONG) M<long,DIM>(); \ + else if (I->GetScalarType()==VTK_UNSIGNED_LONG) M<unsigned long,DIM>(); \ + else if (I->GetScalarType()==VTK_FLOAT) M<float,DIM>(); \ + else if (I->GetScalarType()==VTK_DOUBLE) M<double,DIM>(); \ + } + +#define BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH(I,M) \ + BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,2,M) \ + BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,3,M) + + void vtkImageDataVector2itkImageVector::Convert() + { + BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH(bbGetInputIn(),Convert) + } + + + template<class T, unsigned int D> + void vtkImageDataVector2itkImageVector::Convert() + { + bbtkDebugMessage("process",5,"==> ["<<bbGetFullName()<<"] : Convert<" + <<bbtk::TypeName<T>()<<","<<D + <<">()"<<std::endl); + + + typedef itk::Image<T,D> itkImageType; + typedef itk::VTKImageToImageFilter< itkImageType > VtkToItkConnection; + typename VtkToItkConnection::Pointer conv; + + int vectorSize = bbGetInputIn().size(); + std::cout << " vtkImageDataVector2itkImageVector vector size :" << vectorSize << std::endl; + for (int i=0; i< vectorSize ; i++) + { + + // No converter yet : create it and set its input + if (!mConverter) + { + bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : No converter yet : creating it"<<std::endl); + conv = VtkToItkConnection::New(); + mConverter = conv; + conv->SetInput( this->bbGetInputIn()[i] ); + } + else + { + bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : Converter ok "<<std::endl); + // Input vtkImage type changed ? + // Have to change the converter and set its input + conv = dynamic_cast<VtkToItkConnection*> + ((itk::ProcessObject*)mConverter); + if (!conv) + { + bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : Converter of wrong input type : reacreating it "<<std::endl); + + mConverter->UnRegister(); + conv = VtkToItkConnection::New(); + mConverter = conv; + conv->SetInput( this->bbGetInputIn()[i] ); + } + // Input image type did not change but input image pointer did: + // set new input + else if ( this->bbGetInputIn()[i] != conv->GetExporter()->GetInput()) + { + bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : Converter input changed : resetting it"<<std::endl); + + conv->SetInput( this->bbGetInputIn()[i] ); + } + else + { + bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : Nothing changed"<<std::endl); + } + } + try + { + + bbtkDebugMessage("process",5," ["<<bbGetFullName()<<"] : Trying update"<<std::endl); + conv->Update(); + itkImageType* out = const_cast<itkImageType*>(conv->GetOutput()); + m_Output_Vector.push_back(conv->GetOutput()); + + } + catch( itk::ExceptionObject & e) + { + bbtkError("vtkImageDataVector2itkImageVector<" + <<bbtk::TypeName<T>()<<","<<D + <<">::Convert() : "<<e); + } + bbtkDebugMessage("process",5,"<== ["<<bbGetFullName()<<"] : Convert<" + <<bbtk::TypeName<T>()<<","<<D + <<">() *DONE*"<<std::endl); +} // end iterate on vector size + +bbSetOutputOut(m_Output_Vector); + +} + + + void vtkImageDataVector2itkImageVector::bbUserSetDefaultValues() + { + // bbSetOutputOut(NULL); + mConverter = 0; + } + + void vtkImageDataVector2itkImageVector::bbUserInitializeProcessing() + { + } + + void vtkImageDataVector2itkImageVector::bbUserFinalizeProcessing() + { + } + + +} +// eo namespace bbtk + +#endif +// _USE_VTK_ +#endif +// _USE_ITK_ diff --git a/packages/itkvtk/src/bbitkvtkvtkImageDataVector2itkImageVector.h b/packages/itkvtk/src/bbitkvtkvtkImageDataVector2itkImageVector.h new file mode 100644 index 0000000..868f22d --- /dev/null +++ b/packages/itkvtk/src/bbitkvtkvtkImageDataVector2itkImageVector.h @@ -0,0 +1,83 @@ +/*========================================================================= + + Program: bbtk + Module: $RCSfile: bbitkvtkvtkImageDataVector2itkImageVector.h,v $ + Language: C++ + Date: $Date: 2011/06/16 12:28:57 $ + Version: $Revision: 1.1 $ + + Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de + l'Image). All rights reserved. See Doc/License.txt or + http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +/** + * \file + * \brief class vtkImageDataVector2itkImageVector : Converts a vtkImageData to a generic itkImage + */ +/** + * \class vtkImageDataVector2itkImageVector + * \brief Converts a vtkImageData to a generic itkImage + */ +#ifdef _USE_ITK_ +#ifdef _USE_VTK_ + +#ifndef __bbitkvtkvtkImageDataVector2itkImageVector_h__ +#define __bbvitkvtktkImageData2itkImage_h__ + +#include "bbtkAtomicBlackBox.h" +#include "bbitkImage.h" +#include "vtkImageData.h" +#include "itkProcessObject.h" + +namespace bbitkvtk +{ + + class /*BBTK_EXPORT*/ vtkImageDataVector2itkImageVector : public bbtk::AtomicBlackBox + { + + BBTK_BLACK_BOX_INTERFACE(vtkImageDataVector2itkImageVector, + bbtk::AtomicBlackBox); + + BBTK_DECLARE_INPUT(In,std::vector<vtkImageData*>); + BBTK_DECLARE_OUTPUT(Out,std::vector<bbitk::anyImagePointer>); + BBTK_PROCESS(Convert); + void Convert(); + + private: + // Convert method : template on type of the itk image to be created + template<class T, unsigned int D> void Convert(); + // stores the pointer on the template itk::VTKImageToImageFilter + itk::ProcessObject::Pointer mConverter; + std::vector<bbitk::anyImagePointer> m_Output_Vector; + }; + + + //================================================================= + // AtomicBlackBox description + BBTK_BEGIN_DESCRIBE_BLACK_BOX(vtkImageDataVector2itkImageVector,bbtk::AtomicBlackBox); + BBTK_NAME("vtkImageDataVector2itkImageVector"); + BBTK_AUTHOR("jpr@creatis.insa-lyon.fr"); + BBTK_DESCRIPTION("Converts a std::vector of vtkImageData* to a std::vector of generic itkImage pointer"); + BBTK_DEFAULT_ADAPTOR(); + BBTK_INPUT(vtkImageDataVector2itkImageVector,In, + "Input std::vector of vtkImageData*",std::vector<vtkImageData*>,""); + BBTK_OUTPUT(vtkImageDataVector2itkImageVector,Out, + "Output std::vector of generic itkImage pointer", + std::vector<bbitk::anyImagePointer>,""); + BBTK_END_DESCRIBE_BLACK_BOX(vtkImageDataVector2itkImageVector); + //================================================================= + + +} +//namespace bbitkvtk +#endif +// _USE_VTK_ +#endif +// _USE_ITK_ + +#endif