ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkVectorIndexSelectionCastImageFilter.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 __itkVectorIndexSelectionCastImageFilter_h
00019 #define __itkVectorIndexSelectionCastImageFilter_h
00020 
00021 #include "itkUnaryFunctorImageFilter.h"
00022 
00023 namespace itk
00024 {
00025 namespace Functor
00026 {
00027 template< class TInput, class TOutput >
00028 class VectorIndexSelectionCast
00029 {
00030 public:
00031   VectorIndexSelectionCast() { m_Index = 0; }
00032   ~VectorIndexSelectionCast() {}
00033 
00034   unsigned int GetIndex() const { return m_Index; }
00035   void SetIndex(unsigned int i) { m_Index = i; }
00036 
00037   bool operator!=(const VectorIndexSelectionCast & other) const
00038   {
00039     if ( m_Index != other.m_Index )
00040       {
00041       return true;
00042       }
00043     return false;
00044   }
00045 
00046   bool operator==(const VectorIndexSelectionCast & other) const
00047   {
00048     return !( *this != other );
00049   }
00050 
00051   inline TOutput operator()(const TInput & A) const
00052   {
00053     return static_cast< TOutput >( A[m_Index] );
00054   }
00055 
00056 private:
00057   unsigned int m_Index;
00058 };
00059 }
00060 
00082 template< class TInputImage, class TOutputImage >
00083 class ITK_EXPORT VectorIndexSelectionCastImageFilter:
00084   public
00085   UnaryFunctorImageFilter< TInputImage, TOutputImage,
00086                            Functor::VectorIndexSelectionCast< typename TInputImage::PixelType,
00087                                                               typename TOutputImage::PixelType >   >
00088 {
00089 public:
00091   typedef VectorIndexSelectionCastImageFilter Self;
00092   typedef UnaryFunctorImageFilter<
00093     TInputImage, TOutputImage,
00094     Functor::VectorIndexSelectionCast< typename TInputImage::PixelType,
00095                                        typename TOutputImage::PixelType > >
00096   Superclass;
00097   typedef SmartPointer< Self >       Pointer;
00098   typedef SmartPointer< const Self > ConstPointer;
00099 
00101   itkNewMacro(Self);
00102 
00104   itkTypeMacro(VectorIndexSelectionCastImageFilter,
00105                UnaryFunctorImageFilter);
00106 
00108   void SetIndex(unsigned int i)
00109   {
00110     if ( i != this->GetFunctor().GetIndex() )
00111       {
00112       this->GetFunctor().SetIndex(i);
00113       this->Modified();
00114       }
00115   }
00117 
00118   unsigned int GetIndex(void) const
00119   {
00120     return this->GetFunctor().GetIndex();
00121   }
00122 
00123 #ifdef ITK_USE_CONCEPT_CHECKING
00124 
00125   itkConceptMacro( InputHasNumericTraitsCheck,
00126                    ( Concept::HasNumericTraits< typename TInputImage::PixelType::ValueType > ) );
00127 
00129 #endif
00130 protected:
00131   VectorIndexSelectionCastImageFilter() {}
00132   virtual ~VectorIndexSelectionCastImageFilter() {}
00134 
00135   virtual void BeforeThreadedGenerateData()
00136   {
00137     const unsigned int index = this->GetIndex();
00138     const TInputImage *image = this->GetInput();
00139 
00140     const unsigned int numberOfRunTimeComponents =
00141       image->GetNumberOfComponentsPerPixel();
00142 
00143     typedef typename TInputImage::PixelType PixelType;
00144 
00145     typedef typename itk::NumericTraits< PixelType >::RealType
00146     PixelRealType;
00147 
00148     typedef typename itk::NumericTraits< PixelType >::ScalarRealType
00149     PixelScalarRealType;
00150 
00151     const unsigned int numberOfCompileTimeComponents =
00152       sizeof( PixelRealType ) / sizeof( PixelScalarRealType );
00153 
00154     unsigned int numberOfComponents = numberOfRunTimeComponents;
00155 
00156     if ( numberOfCompileTimeComponents > numberOfRunTimeComponents )
00157       {
00158       numberOfComponents = numberOfCompileTimeComponents;
00159       }
00160 
00161     if ( index >= numberOfComponents )
00162       {
00163       itkExceptionMacro(
00164         << "Selected index = " << index
00165         << " is greater than the number of components = "
00166         << numberOfComponents);
00167       }
00168   }
00169 
00170 private:
00171   VectorIndexSelectionCastImageFilter(const Self &); //purposely not implemented
00172   void operator=(const Self &);                      //purposely not implemented
00173 };
00174 } // end namespace itk
00175 
00176 #endif
00177