ITK  6.0.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 
64 template <bool>
66 {};
67 
75 template <int>
76 struct IntDispatch : public DispatchBase
77 {};
78 
92 template <unsigned int>
94 {};
95 
102 template <bool B1, bool B2>
104 {
109 };
110 
116 template <int D1, int D2>
118 {
123 };
124 
135 template <unsigned int D1, unsigned int D2>
137 {
142 
158  using ComparisonType = IntDispatch<(D1 > D2) - (D1 < D2)>;
162 };
163 
180 template <unsigned int D1, unsigned int D2>
181 void
183  ImageRegion<D1> & destRegion,
184  const ImageRegion<D2> & srcRegion)
185 {
186  destRegion = srcRegion;
187 }
188 
205 template <unsigned int D1, unsigned int D2>
206 void
208  ImageRegion<D1> & destRegion,
209  const ImageRegion<D2> & srcRegion)
210 {
211  // Source dimension is greater than the destination dimension, copy the
212  // first part of the source into the destination
213  unsigned int dim;
214 
215  Index<D1> destIndex;
216  Size<D1> destSize;
217  const Index<D2> & srcIndex = srcRegion.GetIndex();
218  const Size<D2> & srcSize = srcRegion.GetSize();
219 
220  // copy what we can
221  for (dim = 0; dim < D1; ++dim)
222  {
223  destIndex[dim] = srcIndex[dim];
224  destSize[dim] = srcSize[dim];
225  }
226 
227  destRegion.SetIndex(destIndex);
228  destRegion.SetSize(destSize);
229 }
230 
247 template <unsigned int D1, unsigned int D2>
248 void
250  ImageRegion<D1> & destRegion,
251  const ImageRegion<D2> & srcRegion)
252 {
253  // Source dimension is less than the destination dimension, copy source
254  // into the first part of the destination and set zeros elsewhere.
255  unsigned int dim;
256 
257  Index<D1> destIndex;
258  Size<D1> destSize;
259  const Index<D2> & srcIndex = srcRegion.GetIndex();
260  const Size<D2> & srcSize = srcRegion.GetSize();
261 
262  // copy what we can
263  for (dim = 0; dim < D2; ++dim)
264  {
265  destIndex[dim] = srcIndex[dim];
266  destSize[dim] = srcSize[dim];
267  }
268  // fill in the rest of the dimensions with zero/one
269  for (; dim < D1; ++dim)
270  {
271  destIndex[dim] = 0;
272  destSize[dim] = 1;
273  }
274 
275  destRegion.SetIndex(destIndex);
276  destRegion.SetSize(destSize);
277 }
278 
317 template <unsigned int D1, unsigned int D2>
319 {
320 public:
321  virtual void
322  operator()(ImageRegion<D1> & destRegion, const ImageRegion<D2> & srcRegion) const
323  {
324  using ComparisonType = typename BinaryUnsignedIntDispatch<D1, D2>::ComparisonType;
325  ImageToImageFilterDefaultCopyRegion<D1, D2>(ComparisonType(), destRegion, srcRegion);
326  }
327 
328  virtual ~ImageRegionCopier() = default;
329 };
330 
333 template <unsigned int D1, unsigned int D2>
334 std::ostream &
335 operator<<(std::ostream & os, const ImageRegionCopier<D1, D2> &)
336 {
337  os << "ImageRegionCopier: " << typeid(ImageRegionCopier<D1, D2>).name() << std::endl;
338  return os;
339 }
343 template <unsigned int D1, unsigned int D2>
344 bool
346 {
347  return &c1 != &c2;
348 }
349 
350 
351 template <unsigned int D1, unsigned int D2>
352 void
354  ImageBase<D1> * destImage,
355  const ImageBase<D2> * srcImage)
356 {
357  destImage->CopyInformation(srcImage);
358 }
359 
360 
361 template <unsigned int D1, unsigned int D2>
362 void
364  ImageBase<D1> * destImage,
365  const ImageBase<D2> * srcImage)
366 {
367  using DestinationImageType = ImageBase<D1>;
368  using SourceImageType = ImageBase<D2>;
369 
370  // Copy what we can from the image from spacing and origin of the input
371  // This logic needs to be augmented with logic that select which
372  // dimensions to copy
373  const typename SourceImageType::SpacingType & inputSpacing = srcImage->GetSpacing();
374  const typename SourceImageType::PointType & inputOrigin = srcImage->GetOrigin();
375  const typename SourceImageType::DirectionType & inputDirection = srcImage->GetDirection();
376 
377  typename DestinationImageType::SpacingType destSpacing;
378  typename DestinationImageType::PointType destOrigin;
379  typename DestinationImageType::DirectionType destDirection;
380 
381  // copy the input to the output and fill the rest of the
382  // output with zeros.
383  unsigned int i = 0;
384  for (; i < SourceImageType::ImageDimension; ++i)
385  {
386  destSpacing[i] = inputSpacing[i];
387  destOrigin[i] = inputOrigin[i];
388  for (unsigned int j = 0; j < DestinationImageType::ImageDimension; ++j)
389  {
390  if (j < SourceImageType::ImageDimension)
391  {
392  destDirection[j][i] = inputDirection[j][i];
393  }
394  else
395  {
396  destDirection[j][i] = 0.0;
397  }
398  }
399  }
400  for (; i < DestinationImageType::ImageDimension; ++i)
401  {
402  destSpacing[i] = 1.0;
403  destOrigin[i] = 0.0;
404  for (unsigned int j = 0; j < DestinationImageType::ImageDimension; ++j)
405  {
406  if (j == i)
407  {
408  destDirection[j][i] = 1.0;
409  }
410  else
411  {
412  destDirection[j][i] = 0.0;
413  }
414  }
415  }
416 
417  // set the spacing and origin
418  destImage->SetSpacing(destSpacing);
419  destImage->SetOrigin(destOrigin);
420  destImage->SetDirection(destDirection);
421  // propagate vector length info
423 }
424 
425 
437 template <unsigned int D1, unsigned int D2>
439 {
440 public:
441  virtual void
442  operator()(ImageBase<D1> * destImage, const ImageBase<D2> * srcImage) const
443  {
444  using ComparisonType = typename BinaryUnsignedIntDispatch<D1, D2>::ComparisonType;
445  ImageToImageFilterDefaultCopyInformation<D1, D2>(ComparisonType(), destImage, srcImage);
446  }
447 
448  virtual ~ImageInformationCopier() = default;
449 };
450 
451 
452 } // end of namespace ImageToImageFilterDetail
453 } // end namespace itk
454 
455 #endif
itk::ImageBase::GetNumberOfComponentsPerPixel
virtual unsigned int GetNumberOfComponentsPerPixel() const
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:68
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:69
itk::ImageToImageFilterDetail::UnsignedIntDispatch
Templated class to produce a unique type for each unsigned integer (usually a dimension).
Definition: itkImageToImageFilterDetail.h:93
itk::ImageToImageFilterDetail::operator!=
bool operator!=(const ImageRegionCopier< D1, D2 > &c1, const ImageRegionCopier< D1, D2 > &c2)
Definition: itkImageToImageFilterDetail.h:345
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:353
itk::ImageToImageFilterDetail::BooleanDispatch
Templated class to produce a unique type "true" and "false".
Definition: itkImageToImageFilterDetail.h:65
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:442
itk::ImageToImageFilterDetail::BinaryUnsignedIntDispatch
Templated class to produce a unique type for a pairing of unsigned integers (usually two dimensions).
Definition: itkImageToImageFilterDetail.h:136
itk::ImageToImageFilterDetail::ImageToImageFilterDefaultCopyRegion
void ImageToImageFilterDefaultCopyRegion(const typename BinaryUnsignedIntDispatch< D1, D2 >::FirstEqualsSecondType &, ImageRegion< D1 > &destRegion, const ImageRegion< D2 > &srcRegion)
Definition: itkImageToImageFilterDetail.h:182
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:322
itk::ImageToImageFilterDetail::BinaryIntDispatch
Templated class to produce a unique type for a pairing of integers.
Definition: itkImageToImageFilterDetail.h:117
itk::ImageToImageFilterDetail::IntDispatch
Templated class to produce a unique type for each integer.
Definition: itkImageToImageFilterDetail.h:76
itk::ImageToImageFilterDetail::BinaryBooleanDispatch
Templated class to produce a unique type for a pairing of booleans.
Definition: itkImageToImageFilterDetail.h:103
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:438
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
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:335
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:318
itk::ImageRegion::SetSize
void SetSize(const SizeType &size)
Definition: itkImageRegion.h:202
itk::ImageToImageFilterDetail::ImageRegionCopier::~ImageRegionCopier
virtual ~ImageRegionCopier()=default