From: jean-pierre roux 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 <<">()"< ItkToVtkConnection; typename ItkToVtkConnection::Pointer conv; @@ -78,11 +75,11 @@ namespace bbitkvtk else { bbtkDebugMessage("process",5," ["< - ((itk::ProcessObject*)mConverter); + ((itk::ProcessObject*)mConverter); if (!conv) { bbtkDebugMessage("process",5," ["< + void itkImageVector2vtkImageDataVector::Convert() + { + bbtkDebugMessage("process",5,"==> ["<() + <<">()"< 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," ["<SetInput( this->bbGetInputIn()[i].get< itkImageTypePointer >() ); + } + else + { + bbtkDebugMessage("process",5," ["< + ((itk::ProcessObject*)mConverter); + if (!conv) + { + bbtkDebugMessage("process",5," ["<UnRegister(); + conv = ItkToVtkConnection::New(); + mConverter = conv; + conv->SetInput( this->bbGetInputIn()[i].get() ); + } + // Input image type did not change but input image pointer did: + // set new input + else if ( this->bbGetInputIn()[i].get() + != (itkImageType*)(conv->GetExporter()->GetInputs()[0].GetPointer())) + { + bbtkDebugMessage("process",5," ["<SetInput( this->bbGetInputIn()[i].get() ); + } + else + { + bbtkDebugMessage("process",5," ["<Update(); + m_Output_Vector.push_back(conv->GetOutput()); + } + catch( itk::ExceptionObject & e) + { + bbtkError("itkImageVector2vtkImageDataVector<" + <() + <<">::Convert() : "<() + <<">() *DONE*"<); // 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); + BBTK_DECLARE_OUTPUT(Out, std::vector); + BBTK_PROCESS(Convert); + void Convert(); + private: + // Convert method template on type of the itk image + // embedded in the input itkImage* + template void Convert(); + // stores the pointer on the template itk::ImageToVTKImageFilter + itk::ProcessObject::Pointer mConverter; + std::vector 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,""); + BBTK_OUTPUT(itkImageVector2vtkImageDataVector,Out, + "Output std::vector of vtkImageData*", + std::vector,""); + 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(); \ - else if (I->GetScalarType()==VTK_SIGNED_CHAR) M(); \ - else if (I->GetScalarType()==VTK_UNSIGNED_CHAR) M(); \ - else if (I->GetScalarType()==VTK_SHORT) M(); \ + +#define BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,DIM,M) \ + if (I->GetDataDimension()==DIM) \ + { \ + if (I->GetScalarType()==VTK_CHAR) M(); \ + else if (I->GetScalarType()==VTK_SIGNED_CHAR) M(); \ + else if (I->GetScalarType()==VTK_UNSIGNED_CHAR) M(); \ + else if (I->GetScalarType()==VTK_SHORT) M(); \ else if (I->GetScalarType()==VTK_UNSIGNED_SHORT) M(); \ - else if (I->GetScalarType()==VTK_INT) M(); \ - else if (I->GetScalarType()==VTK_UNSIGNED_INT) M(); \ - else if (I->GetScalarType()==VTK_LONG) M(); \ - else if (I->GetScalarType()==VTK_UNSIGNED_LONG) M(); \ - else if (I->GetScalarType()==VTK_FLOAT) M(); \ - else if (I->GetScalarType()==VTK_DOUBLE) M(); \ + else if (I->GetScalarType()==VTK_INT) M(); \ + else if (I->GetScalarType()==VTK_UNSIGNED_INT) M(); \ + else if (I->GetScalarType()==VTK_LONG) M(); \ + else if (I->GetScalarType()==VTK_UNSIGNED_LONG) M(); \ + else if (I->GetScalarType()==VTK_FLOAT) M(); \ + else if (I->GetScalarType()==VTK_DOUBLE) M(); \ } - + #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 void vtkImageData2itkImage::Convert() - { + { bbtkDebugMessage("process",5,"==> ["<()<<","<()"< 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 - ((itk::ProcessObject*)mConverter); + ((itk::ProcessObject*)mConverter); if (!conv) { bbtkDebugMessage("process",5," ["<UnRegister(); conv = VtkToItkConnection::New(); mConverter = conv; @@ -104,6 +116,7 @@ namespace bbitkvtk else if ( this->bbGetInputIn() != conv->GetExporter()->GetInput()) { bbtkDebugMessage("process",5," ["<SetInput( this->bbGetInputIn() ); } else @@ -117,7 +130,7 @@ namespace bbitkvtk bbtkDebugMessage("process",5," ["<Update(); itkImageType* out = const_cast(conv->GetOutput()); - bbSetOutputOut(out); + bbSetOutputOut(out); } catch( itk::ExceptionObject & e) { @@ -127,25 +140,23 @@ namespace bbitkvtk } bbtkDebugMessage("process",5,"<== ["<()<<","<() *DONE*"<() *DONE*"<GetDataDimension()==DIM) \ + { \ + if (I->GetScalarType()==VTK_CHAR) M(); \ + else if (I->GetScalarType()==VTK_SIGNED_CHAR) M(); \ + else if (I->GetScalarType()==VTK_UNSIGNED_CHAR) M(); \ + else if (I->GetScalarType()==VTK_SHORT) M(); \ + else if (I->GetScalarType()==VTK_UNSIGNED_SHORT) M(); \ + else if (I->GetScalarType()==VTK_INT) M(); \ + else if (I->GetScalarType()==VTK_UNSIGNED_INT) M(); \ + else if (I->GetScalarType()==VTK_LONG) M(); \ + else if (I->GetScalarType()==VTK_UNSIGNED_LONG) M(); \ + else if (I->GetScalarType()==VTK_FLOAT) M(); \ + else if (I->GetScalarType()==VTK_DOUBLE) M(); \ + } + +#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 + void vtkImageDataVector2itkImageVector::Convert() + { + bbtkDebugMessage("process",5,"==> ["<()<<","<()"< 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," ["<SetInput( this->bbGetInputIn()[i] ); + } + else + { + bbtkDebugMessage("process",5," ["< + ((itk::ProcessObject*)mConverter); + if (!conv) + { + bbtkDebugMessage("process",5," ["<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," ["<SetInput( this->bbGetInputIn()[i] ); + } + else + { + bbtkDebugMessage("process",5," ["<Update(); + itkImageType* out = const_cast(conv->GetOutput()); + m_Output_Vector.push_back(conv->GetOutput()); + + } + catch( itk::ExceptionObject & e) + { + bbtkError("vtkImageDataVector2itkImageVector<" + <()<<","<::Convert() : "<()<<","<() *DONE*"<); + BBTK_DECLARE_OUTPUT(Out,std::vector); + BBTK_PROCESS(Convert); + void Convert(); + + private: + // Convert method : template on type of the itk image to be created + template void Convert(); + // stores the pointer on the template itk::VTKImageToImageFilter + itk::ProcessObject::Pointer mConverter; + std::vector 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,""); + BBTK_OUTPUT(vtkImageDataVector2itkImageVector,Out, + "Output std::vector of generic itkImage pointer", + std::vector,""); + BBTK_END_DESCRIBE_BLACK_BOX(vtkImageDataVector2itkImageVector); + //================================================================= + + +} +//namespace bbitkvtk +#endif +// _USE_VTK_ +#endif +// _USE_ITK_ + +#endif