ITK  5.0.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< typename TInput, typename TOutput >
29 {
30 public:
32  ~VectorIndexSelectionCast() = default;
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< typename TInputImage, typename TOutputImage >
86  public
87  UnaryFunctorImageFilter< TInputImage, TOutputImage,
88  Functor::VectorIndexSelectionCast< typename TInputImage::PixelType,
89  typename TOutputImage::PixelType > >
90 {
91 public:
92  ITK_DISALLOW_COPY_AND_ASSIGN(VectorIndexSelectionCastImageFilter);
93 
97  TInputImage, TOutputImage,
98  Functor::VectorIndexSelectionCast< typename TInputImage::PixelType,
99  typename TOutputImage::PixelType > >;
102 
104  itkNewMacro(Self);
105 
109 
111  void SetIndex(unsigned int i)
112  {
113  if ( i != this->GetFunctor().GetIndex() )
114  {
115  this->GetFunctor().SetIndex(i);
116  this->Modified();
117  }
118  }
120 
121  unsigned int GetIndex() const
122  {
123  return this->GetFunctor().GetIndex();
124  }
125 
126 #ifdef ITK_USE_CONCEPT_CHECKING
127  // Begin concept checking
128  itkConceptMacro( InputHasNumericTraitsCheck,
130  // End concept checking
131 #endif
132 
133 protected:
135  ~VectorIndexSelectionCastImageFilter() override = default;
136 
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  using PixelType = typename TInputImage::PixelType;
146 
147  using PixelRealType = typename NumericTraits<PixelType>::RealType;
148 
149  using PixelScalarRealType = typename NumericTraits<PixelType>::ScalarRealType;
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 } // end namespace itk
171 
172 #endif
~VectorIndexSelectionCastImageFilter() override=default
Define numeric traits for std::vector.
bool operator!=(const VectorIndexSelectionCast &other) const
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Base class for all process objects that output image data.
bool operator==(const VectorIndexSelectionCast &other) const
const InputImageType * GetInput() const
virtual void Modified() const
Implements pixel-wise generic operation on one image.
Extracts the selected index of the vector that is the input pixel type.
#define itkConceptMacro(name, concept)