ITK  4.2.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 
82 template< class TInputImage, class TOutputImage >
84  public
85  UnaryFunctorImageFilter< TInputImage, TOutputImage,
86  Functor::VectorIndexSelectionCast< typename TInputImage::PixelType,
87  typename TOutputImage::PixelType > >
88 {
89 public:
93  TInputImage, TOutputImage,
94  Functor::VectorIndexSelectionCast< typename TInputImage::PixelType,
95  typename TOutputImage::PixelType > >
99 
101  itkNewMacro(Self);
102 
106 
108  void SetIndex(unsigned int i)
109  {
110  if ( i != this->GetFunctor().GetIndex() )
111  {
112  this->GetFunctor().SetIndex(i);
113  this->Modified();
114  }
115  }
117 
118  unsigned int GetIndex(void) const
119  {
120  return this->GetFunctor().GetIndex();
121  }
122 
123 #ifdef ITK_USE_CONCEPT_CHECKING
124 
125  itkConceptMacro( InputHasNumericTraitsCheck,
127 
129 #endif
130 protected:
134 
135  virtual void BeforeThreadedGenerateData()
136  {
137  const unsigned int index = this->GetIndex();
138  const TInputImage *image = this->GetInput();
139 
140  const unsigned int numberOfRunTimeComponents =
141  image->GetNumberOfComponentsPerPixel();
142 
143  typedef typename TInputImage::PixelType PixelType;
144 
146  PixelRealType;
147 
149  PixelScalarRealType;
150 
151  const unsigned int numberOfCompileTimeComponents =
152  sizeof( PixelRealType ) / sizeof( PixelScalarRealType );
153 
154  unsigned int numberOfComponents = numberOfRunTimeComponents;
155 
156  if ( numberOfCompileTimeComponents > numberOfRunTimeComponents )
157  {
158  numberOfComponents = numberOfCompileTimeComponents;
159  }
160 
161  if ( index >= numberOfComponents )
162  {
163  itkExceptionMacro(
164  << "Selected index = " << index
165  << " is greater than the number of components = "
166  << numberOfComponents);
167  }
168  }
169 
170 private:
171  VectorIndexSelectionCastImageFilter(const Self &); //purposely not implemented
172  void operator=(const Self &); //purposely not implemented
173 };
174 } // end namespace itk
175 
176 #endif
177