ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkJoinImageFilter.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkJoinImageFilter_h
00019 #define __itkJoinImageFilter_h
00020 
00021 #include "itkBinaryFunctorImageFilter.h"
00022 #include "itkPixelTraits.h"
00023 
00024 namespace itk
00025 {
00026 namespace Functor
00027 {
00047 template< class TPixel1, class TPixel2 >
00048 class JoinFunctor
00049 {
00050 public:
00051   JoinFunctor() {}
00052   ~JoinFunctor() {}
00054 
00056   typedef JoinFunctor Self;
00057 
00059   typedef typename PixelTraits< TPixel1 >::ValueType               ValueType1;
00060   typedef typename PixelTraits< TPixel2 >::ValueType               ValueType2;
00061   typedef typename JoinTraits< ValueType1, ValueType2 >::ValueType JoinValueType;
00062 
00064   itkStaticConstMacro(Dimension1, unsigned int,
00065                       PixelTraits< TPixel1 >::Dimension);
00066   itkStaticConstMacro(Dimension2, unsigned int,
00067                       PixelTraits< TPixel2 >::Dimension);
00068   itkStaticConstMacro(JoinDimension, unsigned int,
00069                       Dimension1 + Dimension2);
00071 
00073   typedef Vector< JoinValueType, itkGetStaticConstMacro(JoinDimension) > JoinType;
00074 
00075   bool operator!=(const JoinFunctor &) const
00076   {
00077     return false;
00078   }
00079 
00080   bool operator==(const JoinFunctor & other) const
00081   {
00082     return !( *this != other );
00083   }
00084 
00086   inline JoinType operator()(const TPixel1 & A, const TPixel2 & B) const
00087   {
00088     JoinType out;
00089 
00090     // Copy A into the output, casting as necessary
00091     this->FirstCopier(out, 0, A);
00092 
00093     // Copy B into the output, starting where A left off,casting as necessary
00094     this->SecondCopier(out, Dimension1, B);
00095 
00096     return out;
00097   }
00098 
00099 private:
00106   struct CopierDispatchBase {};
00107   template< unsigned int VDimension >
00108   struct CopierDispatch:public CopierDispatchBase {};
00109 
00120   void FirstCopier(JoinType & out, unsigned int idx, const TPixel1 & A) const
00121   {
00122     FirstCopier(CopierDispatch< Dimension1 >(), out, idx, A);
00123   }
00124 
00126   void FirstCopier(CopierDispatchBase,
00127                    JoinType & out, unsigned int idx, const TPixel1 & A) const
00128   {
00129     for ( unsigned int i = 0; i < Dimension1; i++, idx++ )
00130       {
00131       out[idx] = static_cast< JoinValueType >( A[i] );
00132       }
00133   }
00135 
00137   void FirstCopier(CopierDispatch< 1 >,
00138                    JoinType & out, unsigned int idx, const TPixel1 & A) const
00139   { out[idx] = static_cast< JoinValueType >( A ); }
00140 
00146   void SecondCopier(JoinType & out, unsigned int idx, const TPixel2 & B) const
00147   {
00148     SecondCopier(CopierDispatch< Dimension2 >(), out, idx, B);
00149   }
00150 
00152   void SecondCopier(CopierDispatchBase,
00153                     JoinType & out, unsigned int idx, const TPixel2 & B) const
00154   {
00155     for ( unsigned int i = 0; i < Dimension2; i++, idx++ )
00156       {
00157       out[idx] = static_cast< JoinValueType >( B[i] );
00158       }
00159   }
00161 
00163   void SecondCopier(CopierDispatch< 1 >,
00164                     JoinType & out, unsigned int idx, const TPixel2 & B) const
00165   {
00166     out[idx] = static_cast< JoinValueType >( B );
00167   }
00168 };  //class JoinFunction
00170 
00171 template< typename TImage1, typename TImage2 >
00172 struct MakeJoin {
00173   typedef JoinFunctor< typename TImage1::PixelType,
00174                        typename TImage2::PixelType > FunctorType;
00175   typedef Image< typename FunctorType::JoinType,
00176                  ::itk::GetImageDimension< TImage1 >::ImageDimension > ImageType;
00177 };
00178 } //namespace functor
00179 
00208 template< class TInputImage1, class TInputImage2 >
00209 class ITK_EXPORT JoinImageFilter:
00210   public BinaryFunctorImageFilter< TInputImage1,
00211                                    TInputImage2,
00212                                    typename
00213                                    Functor::MakeJoin< TInputImage1,
00214                                                       TInputImage2 >::ImageType,
00215                                    typename
00216                                    Functor::MakeJoin< TInputImage1,
00217                                                       TInputImage2 >::FunctorType >
00218 {
00219 public:
00221   itkStaticConstMacro(OutputImageDimension, unsigned int,
00222                       TInputImage1::ImageDimension);
00223 
00225   typedef JoinImageFilter Self;
00226 
00228   typedef typename Functor::MakeJoin< TInputImage1,
00229                                       TInputImage2 >::FunctorType FunctorType;
00230   typedef typename Functor::MakeJoin< TInputImage1,
00231                                       TInputImage2 >::ImageType OutputImageType;
00232   typedef typename FunctorType::JoinType OutputImagePixelType;
00233 
00235   typedef BinaryFunctorImageFilter< TInputImage1, TInputImage2, OutputImageType,
00236                                     FunctorType > Superclass;
00237 
00238   typedef SmartPointer< Self >       Pointer;
00239   typedef SmartPointer< const Self > ConstPointer;
00240 
00242   itkNewMacro(Self);
00243 
00245   itkTypeMacro(JoinImageFilter, BinaryFunctorImageFilter);
00246 
00247 #ifdef ITK_USE_CONCEPT_CHECKING
00248 
00249   itkConceptMacro( Input1HasPixelTraitsCheck,
00250                    ( Concept::HasPixelTraits< typename TInputImage1::PixelType > ) );
00251   itkConceptMacro( Input2HasPixelTraitsCheck,
00252                    ( Concept::HasPixelTraits< typename TInputImage2::PixelType > ) );
00253   itkConceptMacro( Input1Input2HasJoinTraitsCheck,
00254                    ( Concept::HasJoinTraits< typename PixelTraits< typename TInputImage1::PixelType >::ValueType,
00255                                              typename PixelTraits< typename TInputImage2::PixelType >::ValueType > ) );
00256 
00258 #endif
00259 protected:
00260   JoinImageFilter() {}
00261   virtual ~JoinImageFilter() {}
00262 private:
00263   JoinImageFilter(const Self &); //purposely not implemented
00264   void operator=(const Self &);  //purposely not implemented
00265 };
00266 } // end namespace itk
00268 
00269 #endif
00270