ITK  5.4.0
Insight Toolkit
itkImageToImageFilterDetail.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 /*=========================================================================
19  *
20  * Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  * For complete copyright, license and disclaimer of warranty information
25  * please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #ifndef itkImageToImageFilterDetail_h
29 #define itkImageToImageFilterDetail_h
30 
31 #include "itkImageRegion.h"
32 #include "itkSmartPointer.h"
33 
34 namespace itk
35 {
44 namespace ImageToImageFilterDetail
45 {
46 
56 {};
57 
65 template <bool>
67 {};
68 
77 template <int>
78 struct IntDispatch : public DispatchBase
79 {};
80 
95 template <unsigned int>
97 {};
107 template <bool B1, bool B2>
109 {
110 
115 };
116 
123 template <int D1, int D2>
125 {
126 
131 };
132 
144 template <unsigned int D1, unsigned int D2>
146 {
147 
152 
168  using ComparisonType = IntDispatch<(D1 > D2) - (D1 < D2)>;
172 };
173 
190 template <unsigned int D1, unsigned int D2>
191 void
193  ImageRegion<D1> & destRegion,
194  const ImageRegion<D2> & srcRegion)
195 {
196  destRegion = srcRegion;
197 }
198 
215 template <unsigned int D1, unsigned int D2>
216 void
218  ImageRegion<D1> & destRegion,
219  const ImageRegion<D2> & srcRegion)
220 {
221  // Source dimension is greater than the destination dimension, copy the
222  // first part of the source into the destination
223  unsigned int dim;
224 
225  Index<D1> destIndex;
226  Size<D1> destSize;
227  const Index<D2> & srcIndex = srcRegion.GetIndex();
228  const Size<D2> & srcSize = srcRegion.GetSize();
229 
230  // copy what we can
231  for (dim = 0; dim < D1; ++dim)
232  {
233  destIndex[dim] = srcIndex[dim];
234  destSize[dim] = srcSize[dim];
235  }
236 
237  destRegion.SetIndex(destIndex);
238  destRegion.SetSize(destSize);
239 }
240 
257 template <unsigned int D1, unsigned int D2>
258 void
260  ImageRegion<D1> & destRegion,
261  const ImageRegion<D2> & srcRegion)
262 {
263  // Source dimension is less than the destination dimension, copy source
264  // into the first part of the destination and set zeros elsewhere.
265  unsigned int dim;
266 
267  Index<D1> destIndex;
268  Size<D1> destSize;
269  const Index<D2> & srcIndex = srcRegion.GetIndex();
270  const Size<D2> & srcSize = srcRegion.GetSize();
271 
272  // copy what we can
273  for (dim = 0; dim < D2; ++dim)
274  {
275  destIndex[dim] = srcIndex[dim];
276  destSize[dim] = srcSize[dim];
277  }
278  // fill in the rest of the dimensions with zero/one
279  for (; dim < D1; ++dim)
280  {
281  destIndex[dim] = 0;
282  destSize[dim] = 1;
283  }
284 
285  destRegion.SetIndex(destIndex);
286  destRegion.SetSize(destSize);
287 }
288 
327 template <unsigned int D1, unsigned int D2>
329 {
330 public:
331  virtual void
332  operator()(ImageRegion<D1> & destRegion, const ImageRegion<D2> & srcRegion) const
333  {
334  using ComparisonType = typename BinaryUnsignedIntDispatch<D1, D2>::ComparisonType;
335  ImageToImageFilterDefaultCopyRegion<D1, D2>(ComparisonType(), destRegion, srcRegion);
336  }
337 
338  virtual ~ImageRegionCopier() = default;
339 };
340 
343 template <unsigned int D1, unsigned int D2>
344 std::ostream &
345 operator<<(std::ostream & os, const ImageRegionCopier<D1, D2> &)
346 {
347  os << "ImageRegionCopier: " << typeid(ImageRegionCopier<D1, D2>).name() << std::endl;
348  return os;
349 }
353 template <unsigned int D1, unsigned int D2>
354 bool
356 {
357  return &c1 != &c2;
358 }
359 
360 
361 template <unsigned int D1, unsigned int D2>
362 void
364  ImageBase<D1> * destImage,
365  const ImageBase<D2> * srcImage)
366 {
367  destImage->CopyInformation(srcImage);
368 }
369 
370 
371 template <unsigned int D1, unsigned int D2>
372 void
374  ImageBase<D1> * destImage,
375  const ImageBase<D2> * srcImage)
376 {
377  using DestinationImageType = ImageBase<D1>;
378  using SourceImageType = ImageBase<D2>;
379 
380  // Copy what we can from the image from spacing and origin of the input
381  // This logic needs to be augmented with logic that select which
382  // dimensions to copy
383  const typename SourceImageType::SpacingType & inputSpacing = srcImage->GetSpacing();
384  const typename SourceImageType::PointType & inputOrigin = srcImage->GetOrigin();
385  const typename SourceImageType::DirectionType & inputDirection = srcImage->GetDirection();
386 
387  typename DestinationImageType::SpacingType destSpacing;
388  typename DestinationImageType::PointType destOrigin;
389  typename DestinationImageType::DirectionType destDirection;
390 
391  // copy the input to the output and fill the rest of the
392  // output with zeros.
393  unsigned int i = 0;
394  for (; i < SourceImageType::ImageDimension; ++i)
395  {
396  destSpacing[i] = inputSpacing[i];
397  destOrigin[i] = inputOrigin[i];
398  for (unsigned int j = 0; j < DestinationImageType::ImageDimension; ++j)
399  {
400  if (j < SourceImageType::ImageDimension)
401  {
402  destDirection[j][i] = inputDirection[j][i];
403  }
404  else
405  {
406  destDirection[j][i] = 0.0;
407  }
408  }
409  }
410  for (; i < DestinationImageType::ImageDimension; ++i)
411  {
412  destSpacing[i] = 1.0;
413  destOrigin[i] = 0.0;
414  for (unsigned int j = 0; j < DestinationImageType::ImageDimension; ++j)
415  {
416  if (j == i)
417  {
418  destDirection[j][i] = 1.0;
419  }
420  else
421  {
422  destDirection[j][i] = 0.0;
423  }
424  }
425  }
426 
427  // set the spacing and origin
428  destImage->SetSpacing(destSpacing);
429  destImage->SetOrigin(destOrigin);
430  destImage->SetDirection(destDirection);
431  // propagate vector length info
433 }
434 
435 
447 template <unsigned int D1, unsigned int D2>
449 {
450 public:
451  virtual void
452  operator()(ImageBase<D1> * destImage, const ImageBase<D2> * srcImage) const
453  {
454  using ComparisonType = typename BinaryUnsignedIntDispatch<D1, D2>::ComparisonType;
455  ImageToImageFilterDefaultCopyInformation<D1, D2>(ComparisonType(), destImage, srcImage);
456  }
457 
458  virtual ~ImageInformationCopier() = default;
459 };
460 
461 
462 } // end of namespace ImageToImageFilterDetail
463 } // end namespace itk
464 
465 #endif
itk::ImageBase::GetNumberOfComponentsPerPixel
virtual unsigned int GetNumberOfComponentsPerPixel() const
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:70
itk::GTest::TypedefsAndConstructors::Dimension2::DirectionType
ImageBaseType::DirectionType DirectionType
Definition: itkGTestTypedefsAndConstructors.h:52
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:71
itk::ImageToImageFilterDetail::UnsignedIntDispatch
Templated class to produce a unique type for each unsigned integer (usually a dimension).
Definition: itkImageToImageFilterDetail.h:96
itk::ImageToImageFilterDetail::operator!=
bool operator!=(const ImageRegionCopier< D1, D2 > &c1, const ImageRegionCopier< D1, D2 > &c2)
Definition: itkImageToImageFilterDetail.h:355
itk::ImageBase
Base class for templated image classes.
Definition: itkImageBase.h:114
itk::ImageRegion::GetIndex
const IndexType & GetIndex() const
Definition: itkImageRegion.h:188
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:80
itk::GTest::TypedefsAndConstructors::Dimension2::PointType
ImageBaseType::PointType PointType
Definition: itkGTestTypedefsAndConstructors.h:51
itk::ImageBase::CopyInformation
void CopyInformation(const DataObject *data) override
itk::ImageToImageFilterDetail::ImageToImageFilterDefaultCopyInformation
void ImageToImageFilterDefaultCopyInformation(const typename BinaryUnsignedIntDispatch< D1, D2 >::FirstEqualsSecondType &, ImageBase< D1 > *destImage, const ImageBase< D2 > *srcImage)
Definition: itkImageToImageFilterDetail.h:363
itk::ImageToImageFilterDetail::BooleanDispatch
Templated class to produce a unique type "true" and "false".
Definition: itkImageToImageFilterDetail.h:66
itk::ImageBase::GetOrigin
virtual const PointType & GetOrigin() const
itk::ImageRegion::GetSize
const SizeType & GetSize() const
Definition: itkImageRegion.h:209
itk::ImageToImageFilterDetail::ImageInformationCopier::~ImageInformationCopier
virtual ~ImageInformationCopier()=default
itk::ImageToImageFilterDetail::ImageInformationCopier::operator()
virtual void operator()(ImageBase< D1 > *destImage, const ImageBase< D2 > *srcImage) const
Definition: itkImageToImageFilterDetail.h:452
itk::ImageToImageFilterDetail::BinaryUnsignedIntDispatch
Templated class to produce a unique type for a pairing of unsigned integers (usually two dimensions).
Definition: itkImageToImageFilterDetail.h:145
itk::ImageToImageFilterDetail::ImageToImageFilterDefaultCopyRegion
void ImageToImageFilterDefaultCopyRegion(const typename BinaryUnsignedIntDispatch< D1, D2 >::FirstEqualsSecondType &, ImageRegion< D1 > &destRegion, const ImageRegion< D2 > &srcRegion)
Definition: itkImageToImageFilterDetail.h:192
itkImageRegion.h
itk::ImageToImageFilterDetail::DispatchBase
Base class for a class used to dispatch to dimension specific implementations.
Definition: itkImageToImageFilterDetail.h:55
itk::ImageBase::SetDirection
virtual void SetDirection(const DirectionType &direction)
itk::ImageToImageFilterDetail::ImageRegionCopier::operator()
virtual void operator()(ImageRegion< D1 > &destRegion, const ImageRegion< D2 > &srcRegion) const
Definition: itkImageToImageFilterDetail.h:332
itk::ImageToImageFilterDetail::BinaryIntDispatch
Templated class to produce a unique type for a pairing of integers.
Definition: itkImageToImageFilterDetail.h:124
itk::ImageToImageFilterDetail::IntDispatch
Templated class to produce a unique type for each integer.
Definition: itkImageToImageFilterDetail.h:78
itk::ImageToImageFilterDetail::BinaryBooleanDispatch
Templated class to produce a unique type for a pairing of booleans.
Definition: itkImageToImageFilterDetail.h:108
itk::ImageBase::SetOrigin
virtual void SetOrigin(PointType _arg)
itk::ImageBase::GetSpacing
virtual const SpacingType & GetSpacing() const
itk::ImageToImageFilterDetail::ImageInformationCopier
A Function object used to copy image meta-data of an image.
Definition: itkImageToImageFilterDetail.h:448
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ImageBase::GetDirection
virtual const DirectionType & GetDirection() const
itk::ImageBase::SetSpacing
virtual void SetSpacing(const SpacingType &spacing)
itk::ImageRegion::SetIndex
void SetIndex(const IndexType &index)
Definition: itkImageRegion.h:181
itk::ImageToImageFilterDetail::operator<<
std::ostream & operator<<(std::ostream &os, const ImageRegionCopier< D1, D2 > &)
Definition: itkImageToImageFilterDetail.h:345
itkSmartPointer.h
itk::ImageBase::SetNumberOfComponentsPerPixel
virtual void SetNumberOfComponentsPerPixel(unsigned int)
itk::ImageToImageFilterDetail::ImageRegionCopier
A Function object used to dispatching to a routine to copy a region (start index and size).
Definition: itkImageToImageFilterDetail.h:328
itk::ImageRegion::SetSize
void SetSize(const SizeType &size)
Definition: itkImageRegion.h:202
itk::ImageToImageFilterDetail::ImageRegionCopier::~ImageRegionCopier
virtual ~ImageRegionCopier()=default