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;
160
161 output = static_cast<ExternalType>(input[m_ElementNumber]);
162 return output;
163 }
164
165 inline ExternalType
166 Get(const InternalType & input, const SizeValueType offset) const
167 {
168 return Get(Superclass::Get(input, offset));
169 }
170
171
173 unsigned int
175 {
176 return m_ElementNumber;
177 }
178
180 void
181 SetElementNumber(unsigned int nth)
182 {
183 m_ElementNumber = nth;
184 }
185
187 void
189 {
190 Superclass::SetVectorLength(l);
191 }
192
194 VectorLengthType
196 {
197 return Superclass::GetVectorLength();
198 }
199
200 NthElementPixelAccessor(unsigned int length = 1) { Superclass::SetVectorLength(length); }
201
204 bool
205 operator==(const Self & accessor) const
206 {
207 return m_ElementNumber == accessor.m_ElementNumber;
208 }
209
211
215 {
217 this->SetVectorLength(accessor.GetVectorLength());
218 return *this;
219 }
222protected:
224
225private:
227};
228
229} // end namespace itk
230
231#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