3636#include < fstream>
3737
3838
39+ namespace
40+ {
41+ template <typename T>
42+ static constexpr bool isVariableLengthVector = std::is_same_v<T, itk::VariableLengthVector<typename T::ValueType>>;
43+ }
44+
3945// Helper function to initialize an image with random values
4046template <typename TImage>
4147typename TImage::Pointer
@@ -94,12 +100,24 @@ CopyScanlineIterator(const TInputImage * inputPtr, TOutputImage * outputPtr)
94100 while (!inputIt.IsAtEndOfLine ())
95101 {
96102 const InputPixelType & inputPixel = inputIt.Get ();
97- OutputPixelType value (outputIt. Get ());
98- for ( unsigned int k = 0 ; k < componentsPerPixel; ++k )
103+
104+ if constexpr (isVariableLengthVector<OutputPixelType> )
99105 {
100- value[k] = static_cast <typename OutputPixelType::ValueType>(inputPixel[k]);
106+ OutputPixelType value (outputIt.Get ());
107+ for (unsigned int k = 0 ; k < componentsPerPixel; ++k)
108+ {
109+ value[k] = static_cast <typename OutputPixelType::ValueType>(inputPixel[k]);
110+ }
111+ }
112+ else
113+ {
114+ OutputPixelType value;
115+ for (unsigned int k = 0 ; k < componentsPerPixel; ++k)
116+ {
117+ value[k] = static_cast <typename OutputPixelType::ValueType>(inputPixel[k]);
118+ }
119+ outputIt.Set (value);
101120 }
102- outputIt.Set (value);
103121
104122 ++inputIt;
105123 ++outputIt;
@@ -133,12 +151,23 @@ CopyScanlineIteratorNumericTraits(const TInputImage * inputPtr, TOutputImage * o
133151 {
134152 const InputPixelType & inputPixel = inputIt.Get ();
135153
136- OutputPixelType value{ outputIt.Get () };
137- for (unsigned int k = 0 ; k < componentsPerPixel; ++k)
154+ if constexpr (isVariableLengthVector<OutputPixelType>)
155+ {
156+ OutputPixelType value (outputIt.Get ());
157+ for (unsigned int k = 0 ; k < componentsPerPixel; ++k)
158+ {
159+ value[k] = static_cast <typename OutputPixelType::ValueType>(inputPixel[k]);
160+ }
161+ }
162+ else
138163 {
139- value[k] = static_cast <typename OutputPixelType::ValueType>(inputPixel[k]);
164+ OutputPixelType value;
165+ for (unsigned int k = 0 ; k < componentsPerPixel; ++k)
166+ {
167+ value[k] = static_cast <typename OutputPixelType::ValueType>(inputPixel[k]);
168+ }
169+ outputIt.Set (value);
140170 }
141- outputIt.Set (value);
142171
143172 ++inputIt;
144173 ++outputIt;
@@ -171,12 +200,13 @@ CopyImageRegionRange(const TInputImage * inputPtr, TOutputImage * outputPtr)
171200 while (inputIt != inputEnd)
172201 {
173202 const InputPixelType & inputPixel = *inputIt;
174- OutputPixelType outputPixel{ *outputIt };
203+ std::conditional_t <isVariableLengthVector<OutputPixelType>, OutputPixelType, OutputPixelType &> outputPixel{
204+ *outputIt
205+ };
175206 for (unsigned int k = 0 ; k < componentsPerPixel; ++k)
176207 {
177208 outputPixel[k] = static_cast <typename OutputPixelType::ValueType>(inputPixel[k]);
178209 }
179- *outputIt = outputPixel;
180210
181211 ++inputIt;
182212 ++outputIt;
@@ -206,12 +236,13 @@ CopyImageRegionRangeNumericTraits(const TInputImage * inputPtr, TOutputImage * o
206236 while (inputIt != inputEnd)
207237 {
208238 const InputPixelType & inputPixel = *inputIt;
209- OutputPixelType outputPixel{ *outputIt };
239+ std::conditional_t <isVariableLengthVector<OutputPixelType>, OutputPixelType, OutputPixelType &> outputPixel{
240+ *outputIt
241+ };
210242 for (unsigned int k = 0 ; k < componentsPerPixel; ++k)
211243 {
212244 outputPixel[k] = static_cast <typename OutputPixelType::ValueType>(inputPixel[k]);
213245 }
214- *outputIt = outputPixel;
215246 ++inputIt;
216247 ++outputIt;
217248 }
@@ -235,12 +266,13 @@ CopyImageRegionRangeNumericTraitsAsRange(const TInputImage * inputPtr, TOutputIm
235266 const unsigned int componentsPerPixel = itk::NumericTraits<OutputPixelType>::GetLength (*outputIt);
236267 for (const InputPixelType & inputPixel : itk::ImageRegionRange<const TInputImage>(*inputPtr, inputRegion))
237268 {
238- OutputPixelType outputPixel{ *outputIt };
269+ std::conditional_t <isVariableLengthVector<OutputPixelType>, OutputPixelType, OutputPixelType &> outputPixel{
270+ *outputIt
271+ };
239272 for (unsigned int k = 0 ; k < componentsPerPixel; ++k)
240273 {
241274 outputPixel[k] = static_cast <typename OutputPixelType::ValueType>(inputPixel[k]);
242275 }
243- *outputIt = outputPixel;
244276 ++outputIt;
245277 }
246278}
0 commit comments