ITK  4.4.0
Insight Segmentation and Registration Toolkit
itkVectorIndexSelectionCastImageFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkVectorIndexSelectionCastImageFilter_h
19 #define __itkVectorIndexSelectionCastImageFilter_h
20 
22 
23 namespace itk
24 {
25 namespace Functor
26 {
27 template< class TInput, class TOutput >
29 {
30 public:
33 
34  unsigned int GetIndex() const { return m_Index; }
35  void SetIndex(unsigned int i) { m_Index = i; }
36 
37  bool operator!=(const VectorIndexSelectionCast & other) const
38  {
39  if ( m_Index != other.m_Index )
40  {
41  return true;
42  }
43  return false;
44  }
45 
46  bool operator==(const VectorIndexSelectionCast & other) const
47  {
48  return !( *this != other );
49  }
50 
51  inline TOutput operator()(const TInput & A) const
52  {
53  return static_cast< TOutput >( A[m_Index] );
54  }
55 
56 private:
57  unsigned int m_Index;
58 };
59 }
60 
84 template< class TInputImage, class TOutputImage >
86  public
87  UnaryFunctorImageFilter< TInputImage, TOutputImage,
88  Functor::VectorIndexSelectionCast< typename TInputImage::PixelType,
89  typename TOutputImage::PixelType > >
90 {
91 public:
95  TInputImage, TOutputImage,
96  Functor::VectorIndexSelectionCast< typename TInputImage::PixelType,
97  typename TOutputImage::PixelType > >
101 
103  itkNewMacro(Self);
104 
108 
110  void SetIndex(unsigned int i)
111  {
112  if ( i != this->GetFunctor().GetIndex() )
113  {
114  this->GetFunctor().SetIndex(i);
115  this->Modified();
116  }
117  }
119 
120  unsigned int GetIndex(void) const
121  {
122  return this->GetFunctor().GetIndex();
123  }
124 
125 #ifdef ITK_USE_CONCEPT_CHECKING
126 
127  itkConceptMacro( InputHasNumericTraitsCheck,
129 
131 #endif
132 
133 protected:
136 
137  virtual void BeforeThreadedGenerateData()
138  {
139  const unsigned int index = this->GetIndex();
140  const TInputImage *image = this->GetInput();
141 
142  const unsigned int numberOfRunTimeComponents =
143  image->GetNumberOfComponentsPerPixel();
144 
145  typedef typename TInputImage::PixelType PixelType;
146 
147  typedef typename NumericTraits< PixelType >::RealType
148  PixelRealType;
149 
151  PixelScalarRealType;
152 
153  const unsigned int numberOfCompileTimeComponents =
154  sizeof( PixelRealType ) / sizeof( PixelScalarRealType );
155 
156  unsigned int numberOfComponents = numberOfRunTimeComponents;
157 
158  if ( numberOfCompileTimeComponents > numberOfRunTimeComponents )
159  {
160  numberOfComponents = numberOfCompileTimeComponents;
161  }
162 
163  if ( index >= numberOfComponents )
164  {
165  itkExceptionMacro(
166  << "Selected index = " << index
167  << " is greater than the number of components = "
168  << numberOfComponents);
169  }
170  }
171 
172 private:
173  VectorIndexSelectionCastImageFilter(const Self &); //purposely not implemented
174  void operator=(const Self &); //purposely not implemented
175 };
176 } // end namespace itk
177 
178 #endif
179