ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkNthElementPixelAccessor.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 __itkNthElementPixelAccessor_h
19 #define __itkNthElementPixelAccessor_h
20 
21 #include "itkMacro.h"
25 
26 namespace itk
27 {
51 template< class T, class TContainer >
52 class ITK_EXPORT NthElementPixelAccessor
53 {
54 public:
57 
59  typedef T ExternalType;
60 
63  typedef TContainer InternalType;
64 
66  inline void Set(InternalType & output, const ExternalType & input) const
67  { DefaultConvertPixelTraits<InternalType>::SetNthComponent(m_ElementNumber, output, input); }
68 
70  inline ExternalType Get(const InternalType & input) const
71  { return static_cast<ExternalType>( DefaultConvertPixelTraits<InternalType>::GetNthComponent( m_ElementNumber, input ) ); }
72 
74  unsigned int GetElementNumber(void) const
75  { return m_ElementNumber; }
76 
78  void SetElementNumber(unsigned int nth)
79  { m_ElementNumber = nth; }
80 
83  bool operator!=(const Self & accessor) const
84  {
85  return ( m_ElementNumber != accessor.m_ElementNumber );
86  }
87 
89  NthElementPixelAccessor & operator=(const NthElementPixelAccessor & accessor)
90  {
91  m_ElementNumber = accessor.m_ElementNumber;
92  return *this;
93  }
94 
97  {
98  m_ElementNumber = 0;
99  }
100 
101 private:
102  // Identifier of the N-th element to be accessed
103  unsigned int m_ElementNumber;
104 };
105 
106 
107 template< class TOutputPixelType, class TPixelType >
108 class ITK_EXPORT NthElementPixelAccessor< TOutputPixelType, itk::VariableLengthVector<TPixelType> >
109  : private DefaultVectorPixelAccessor< TPixelType >
110 {
111 public:
112 
113  typedef unsigned int VectorLengthType;
114 
117  typedef TOutputPixelType ExternalType;
118 
120  typedef TPixelType InternalType;
121 
123 
124  inline void Set(ActualPixelType output, const ExternalType & input) const
125  {
126  output[m_ElementNumber] = input;
127  }
128 
129  inline void Set(InternalType &output, const ExternalType & input,
130  const unsigned long offset) const
131  {
132  return Set( Superclass::Get( output, offset ), input );
133  }
134 
135  inline ExternalType Get(const ActualPixelType & input) const
136  {
137  ExternalType output;
138 
139  output = static_cast< ExternalType >( input[m_ElementNumber] );
140  return output;
141  }
142 
143  inline ExternalType Get(const InternalType &input, const SizeValueType offset) const
144  {
145  return Get( Superclass::Get(input, offset) );
146  }
147 
148 
150  unsigned int GetElementNumber(void) const
151  { return m_ElementNumber; }
152 
154  void SetElementNumber(unsigned int nth)
155  { m_ElementNumber = nth; }
156 
158  void SetVectorLength(VectorLengthType l)
159  {
160  Superclass::SetVectorLength( l );
161  }
162 
164  VectorLengthType GetVectorLength() const { return Superclass::GetVectorLength(); }
165 
166  NthElementPixelAccessor( unsigned int length = 1)
167  :m_ElementNumber(0)
168  {
169  Superclass::SetVectorLength( length );
170  }
171 
172 protected:
174 
175 private:
177 };
178 
179 } // end namespace itk
180 
181 #endif
182