ITK  5.2.0
Insight Toolkit
itkVectorIndexSelectionCastImageFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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
35  GetIndex() const
36  {
37  return m_Index;
38  }
39  void
40  SetIndex(unsigned int i)
41  {
42  m_Index = i;
43  }
44 
45  bool
46  operator!=(const VectorIndexSelectionCast & other) const
47  {
48  if (m_Index != other.m_Index)
49  {
50  return true;
51  }
52  return false;
53  }
54 
55  bool
56  operator==(const VectorIndexSelectionCast & other) const
57  {
58  return !(*this != other);
59  }
60 
61  inline TOutput
62  operator()(const TInput & A) const
63  {
64  return static_cast<TOutput>(A[m_Index]);
65  }
66 
67 private:
68  unsigned int m_Index;
69 };
70 } // namespace Functor
71 
95 template <typename TInputImage, typename TOutputImage>
97  : public UnaryFunctorImageFilter<
98  TInputImage,
99  TOutputImage,
100  Functor::VectorIndexSelectionCast<typename TInputImage::PixelType, typename TOutputImage::PixelType>>
101 {
102 public:
103  ITK_DISALLOW_COPY_AND_ASSIGN(VectorIndexSelectionCastImageFilter);
104 
108  TInputImage,
109  TOutputImage,
113 
115  itkNewMacro(Self);
116 
119 
121  void
122  SetIndex(unsigned int i)
123  {
124  if (i != this->GetFunctor().GetIndex())
125  {
126  this->GetFunctor().SetIndex(i);
127  this->Modified();
128  }
129  }
131 
132  unsigned int
133  GetIndex() const
134  {
135  return this->GetFunctor().GetIndex();
136  }
137 
138 #ifdef ITK_USE_CONCEPT_CHECKING
139  // Begin concept checking
141  // End concept checking
142 #endif
143 
144 protected:
146  ~VectorIndexSelectionCastImageFilter() override = default;
147 
148  void
150  {
151  const unsigned int index = this->GetIndex();
152  const TInputImage * image = this->GetInput();
153 
154  const unsigned int numberOfRunTimeComponents = image->GetNumberOfComponentsPerPixel();
155 
156  using PixelType = typename TInputImage::PixelType;
157 
158  using PixelRealType = typename NumericTraits<PixelType>::RealType;
159 
160  using PixelScalarRealType = typename NumericTraits<PixelType>::ScalarRealType;
161 
162  const unsigned int numberOfCompileTimeComponents = sizeof(PixelRealType) / sizeof(PixelScalarRealType);
163 
164  unsigned int numberOfComponents = numberOfRunTimeComponents;
165 
166  if (numberOfCompileTimeComponents > numberOfRunTimeComponents)
167  {
168  numberOfComponents = numberOfCompileTimeComponents;
169  }
170 
171  if (index >= numberOfComponents)
172  {
173  itkExceptionMacro(<< "Selected index = " << index
174  << " is greater than the number of components = " << numberOfComponents);
175  }
176  }
177 };
178 } // end namespace itk
179 
180 #endif
itk::VectorIndexSelectionCastImageFilter::SetIndex
void SetIndex(unsigned int i)
Definition: itkVectorIndexSelectionCastImageFilter.h:122
itkUnaryFunctorImageFilter.h
itk::VectorIndexSelectionCastImageFilter::GetIndex
unsigned int GetIndex() const
Definition: itkVectorIndexSelectionCastImageFilter.h:133
itk::Concept::HasNumericTraits
Definition: itkConceptChecking.h:712
itk::UnaryFunctorImageFilter
Implements pixel-wise generic operation on one image.
Definition: itkUnaryFunctorImageFilter.h:50
itk::VectorIndexSelectionCastImageFilter::VectorIndexSelectionCastImageFilter
VectorIndexSelectionCastImageFilter()=default
itk::SmartPointer< Self >
itk::VectorIndexSelectionCastImageFilter
Extracts the selected index of the vector that is the input pixel type.
Definition: itkVectorIndexSelectionCastImageFilter.h:96
itk::ImageSource
Base class for all process objects that output image data.
Definition: itkImageSource.h:67
itk::Functor::VectorIndexSelectionCast::operator!=
bool operator!=(const VectorIndexSelectionCast &other) const
Definition: itkVectorIndexSelectionCastImageFilter.h:46
itk::Functor::VectorIndexSelectionCast::SetIndex
void SetIndex(unsigned int i)
Definition: itkVectorIndexSelectionCastImageFilter.h:40
itk::VectorIndexSelectionCastImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
Definition: itkVectorIndexSelectionCastImageFilter.h:149
itk::Functor::VectorIndexSelectionCast::operator()
TOutput operator()(const TInput &A) const
Definition: itkVectorIndexSelectionCastImageFilter.h:62
itk::Functor::VectorIndexSelectionCast::VectorIndexSelectionCast
VectorIndexSelectionCast()
Definition: itkVectorIndexSelectionCastImageFilter.h:31
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::ImageToImageFilter::GetInput
const InputImageType * GetInput() const
itk::Functor::VectorIndexSelectionCast::operator==
bool operator==(const VectorIndexSelectionCast &other) const
Definition: itkVectorIndexSelectionCastImageFilter.h:56
itkConceptMacro
#define itkConceptMacro(name, concept)
Definition: itkConceptChecking.h:64
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Functor::VectorIndexSelectionCast::GetIndex
unsigned int GetIndex() const
Definition: itkVectorIndexSelectionCastImageFilter.h:35
itk::Functor::VectorIndexSelectionCast::m_Index
unsigned int m_Index
Definition: itkVectorIndexSelectionCastImageFilter.h:68
itk::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:138
itk::Functor::VectorIndexSelectionCast
Definition: itkVectorIndexSelectionCastImageFilter.h:28
itk::UnaryFunctorImageFilter< TInputImage, TOutputImage, Functor::VectorIndexSelectionCast< TInputImage::PixelType, TOutputImage::PixelType > >::GetFunctor
FunctorType & GetFunctor()
Definition: itkUnaryFunctorImageFilter.h:85
itk::VectorIndexSelectionCastImageFilter::~VectorIndexSelectionCastImageFilter
~VectorIndexSelectionCastImageFilter() override=default
itk::Object::Modified
virtual void Modified() const
itk::Functor::VectorIndexSelectionCast::~VectorIndexSelectionCast
~VectorIndexSelectionCast()=default