ITK  6.0.0
Insight Toolkit
itkNthElementPixelAccessor.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 * https://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
26namespace itk
27{
52template <typename T, typename TContainer>
54{
55public:
58
60 using ExternalType = T;
61
64 using InternalType = TContainer;
65
67 inline void
68 Set(InternalType & output, const ExternalType & input) const
69 {
71 }
72
74 inline ExternalType
75 Get(const InternalType & input) const
76 {
78 }
79
81 unsigned int
83 {
84 return m_ElementNumber;
85 }
86
88 void
89 SetElementNumber(unsigned int nth)
90 {
91 m_ElementNumber = nth;
92 }
93
96 bool
97 operator==(const Self & accessor) const
98 {
99 return m_ElementNumber == accessor.m_ElementNumber;
100 }
101
103
107 {
109 return *this;
110 }
111
114
115private:
116 // Identifier of the N-th element to be accessed
117 unsigned int m_ElementNumber;
118};
119
120
121template <typename TOutputPixelType, typename TPixelType>
122class NthElementPixelAccessor<TOutputPixelType, itk::VariableLengthVector<TPixelType>>
123 : private DefaultVectorPixelAccessor<TPixelType>
124{
125public:
128
129 using VectorLengthType = unsigned int;
130
133 using ExternalType = TOutputPixelType;
134
136 using InternalType = TPixelType;
137
139
140 inline void
141 Set(ActualPixelType & output, const ExternalType & input) const
142 {
143 output[m_ElementNumber] = input;
144 }
145
146 inline void
147 Set(InternalType & output, const ExternalType & input, const unsigned long offset) const
148 {
149 // note: v is a reference to the internal buffer, this method of
150 // access relies on return value optimization to work
151 ActualPixelType v = Superclass::Get(output, offset);
152
153 return Set(v, input);
154 }
155
156 inline ExternalType
157 Get(const ActualPixelType & input) const
158 {
159 ExternalType output = static_cast<ExternalType>(input[m_ElementNumber]);
160 return output;
161 }
162
163 inline ExternalType
164 Get(const InternalType & input, const SizeValueType offset) const
165 {
166 return Get(Superclass::Get(input, offset));
167 }
168
169
171 unsigned int
173 {
174 return m_ElementNumber;
175 }
176
178 void
179 SetElementNumber(unsigned int nth)
180 {
181 m_ElementNumber = nth;
182 }
183
185 void
187 {
188 Superclass::SetVectorLength(l);
189 }
190
192 VectorLengthType
194 {
195 return Superclass::GetVectorLength();
196 }
197
198 NthElementPixelAccessor(unsigned int length = 1) { Superclass::SetVectorLength(length); }
199
202 bool
203 operator==(const Self & accessor) const
204 {
205 return m_ElementNumber == accessor.m_ElementNumber;
206 }
207
209
213 {
215 this->SetVectorLength(accessor.GetVectorLength());
216 return *this;
217 }
220protected:
222
223private:
225};
226
227} // end namespace itk
228
229#endif
static ComponentType GetNthComponent(int c, const PixelType &pixel)
static void SetNthComponent(int c, PixelType &pixel, const ComponentType &v)
Give access to partial aspects of a type.
NthElementPixelAccessor & operator=(const NthElementPixelAccessor &accessor)
void Set(InternalType &output, const ExternalType &input, const unsigned long offset) const
ExternalType Get(const InternalType &input, const SizeValueType offset) const
Give access to the N-th of a Container type.
void Set(InternalType &output, const ExternalType &input) const
ExternalType Get(const InternalType &input) const
bool operator==(const Self &accessor) const
NthElementPixelAccessor & operator=(const NthElementPixelAccessor &accessor)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Definition: itkIntTypes.h:86