ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkImageToImageFilterDetail.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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  * http://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 
55 struct DispatchBase {};
56 
64 template< bool >
65 struct BooleanDispatch {};
66 
75 template< int >
76 struct IntDispatch:public DispatchBase {};
77 
92 template< unsigned int >
95 
103 template< bool B1, bool B2 >
105 
110 };
111 
118 template< int D1, int D2 >
120 
125 };
126 
138 template< unsigned int D1, unsigned int D2 >
140 
145 
161  typedef IntDispatch < ( D1 > D2 ) - ( D1 < D2 ) > ComparisonType;
165 };
166 
183 template< unsigned int D1, unsigned int D2 >
186  ImageRegion< D1 > & destRegion,
187  const ImageRegion< D2 > & srcRegion)
188 {
189  destRegion = srcRegion;
190 }
191 
208 template< unsigned int D1, unsigned int D2 >
211  ImageRegion< D1 > & destRegion,
212  const ImageRegion< D2 > & srcRegion)
213 {
214  // Source dimension is greater than the destination dimension, copy the
215  // first part of the source into the destination
216  unsigned int dim;
217 
218  Index< D1 > destIndex;
219  Size< D1 > destSize;
220  const Index< D2 > & srcIndex = srcRegion.GetIndex();
221  const Size< D2 > & srcSize = srcRegion.GetSize();
222 
223  // copy what we can
224  for ( dim = 0; dim < D1; ++dim )
225  {
226  destIndex[dim] = srcIndex[dim];
227  destSize[dim] = srcSize[dim];
228  }
229 
230  destRegion.SetIndex(destIndex);
231  destRegion.SetSize(destSize);
232 }
233 
250 template< unsigned int D1, unsigned int D2 >
253  ImageRegion< D1 > & destRegion,
254  const ImageRegion< D2 > & srcRegion)
255 {
256  // Source dimension is less than the destination dimension, copy source
257  // into the first part of the destination and set zeros elsewhere.
258  unsigned int dim;
259 
260  Index< D1 > destIndex;
261  Size< D1 > destSize;
262  const Index< D2 > & srcIndex = srcRegion.GetIndex();
263  const Size< D2 > & srcSize = srcRegion.GetSize();
264 
265  // copy what we can
266  for ( dim = 0; dim < D2; ++dim )
267  {
268  destIndex[dim] = srcIndex[dim];
269  destSize[dim] = srcSize[dim];
270  }
271  // fill in the rest of the dimensions with zero/one
272  for (; dim < D1; ++dim )
273  {
274  destIndex[dim] = 0;
275  destSize[dim] = 1;
276  }
277 
278  destRegion.SetIndex(destIndex);
279  destRegion.SetSize(destSize);
280 }
281 
320 template< unsigned int D1, unsigned int D2 >
322 {
323 public:
324  virtual void operator()(ImageRegion< D1 > & destRegion,
325  const ImageRegion< D2 > & srcRegion) const
326  {
327  typedef typename BinaryUnsignedIntDispatch< D1, D2 >::ComparisonType ComparisonType;
328  ImageToImageFilterDefaultCopyRegion< D1, D2 >(
329  ComparisonType(),
330  destRegion, srcRegion);
331  }
332 
333  virtual ~ImageRegionCopier() {}
334 };
335 
338 template< unsigned int D1, unsigned int D2 >
339 std::ostream & operator<<(std::ostream & os,
341 {
342  os << "ImageRegionCopier: "
343  << typeid( ImageRegionCopier< D1, D2 > ).name() << std::endl;
344  return os;
345 }
347 
349 template< unsigned int D1, unsigned int D2 >
351  const ImageRegionCopier< D1, D2 > & c2)
352 {
353  return &c1 != &c2;
354 }
355 
356 
357 template< unsigned int D1, unsigned int D2 >
360  ImageBase< D1 >* destImage,
361  const ImageBase< D2 >* srcImage)
362 {
363  destImage->CopyInformation(srcImage);
364 }
365 
366 
367 template< unsigned int D1, unsigned int D2 >
370  ImageBase< D1 >* destImage,
371  const ImageBase< D2 >* srcImage)
372 {
373  typedef ImageBase<D1> DestinationImageType;
374  typedef ImageBase<D2> SourceImageType;
375 
376  // Copy what we can from the image from spacing and origin of the input
377  // This logic needs to be augmented with logic that select which
378  // dimensions to copy
379  const typename SourceImageType::SpacingType &inputSpacing = srcImage->GetSpacing();
380  const typename SourceImageType::PointType &inputOrigin = srcImage->GetOrigin();
381  const typename SourceImageType::DirectionType &inputDirection = srcImage->GetDirection();
382 
383  typename DestinationImageType::SpacingType destSpacing;
384  typename DestinationImageType::PointType destOrigin;
385  typename DestinationImageType::DirectionType destDirection;
386 
387  // copy the input to the output and fill the rest of the
388  // output with zeros.
389  unsigned int i = 0;
390  for (; i < SourceImageType::ImageDimension; ++i )
391  {
392  destSpacing[i] = inputSpacing[i];
393  destOrigin[i] = inputOrigin[i];
394  for ( unsigned int j = 0; j < DestinationImageType::ImageDimension; j++ )
395  {
396  if ( j < SourceImageType::ImageDimension )
397  {
398  destDirection[j][i] = inputDirection[j][i];
399  }
400  else
401  {
402  destDirection[j][i] = 0.0;
403  }
404  }
405  }
406  for (; i < DestinationImageType::ImageDimension; ++i )
407  {
408  destSpacing[i] = 1.0;
409  destOrigin[i] = 0.0;
410  for ( unsigned int j = 0; j < DestinationImageType::ImageDimension; j++ )
411  {
412  if ( j == i )
413  {
414  destDirection[j][i] = 1.0;
415  }
416  else
417  {
418  destDirection[j][i] = 0.0;
419  }
420  }
421  }
422 
423  // set the spacing and origin
424  destImage->SetSpacing(destSpacing);
425  destImage->SetOrigin(destOrigin);
426  destImage->SetDirection(destDirection);
427  // propagate vector length info
429 }
430 
431 
443 template< unsigned int D1, unsigned int D2 >
445 {
446 public:
447  virtual void operator()(ImageBase< D1 > * destImage,
448  const ImageBase< D2 > * srcImage) const
449  {
450  typedef typename BinaryUnsignedIntDispatch< D1, D2 >::ComparisonType ComparisonType;
451  ImageToImageFilterDefaultCopyInformation< D1, D2 >(
452  ComparisonType(),
453  destImage, srcImage);
454  }
455 
457 };
458 
459 
460 } // end of namespace ImageToImageFilterDetail
461 } // end namespace itk
462 
463 #endif
bool operator!=(const ImageRegionCopier< D1, D2 > &c1, const ImageRegionCopier< D1, D2 > &c2)
void SetSize(const SizeType &size)
const IndexType & GetIndex() const
virtual void CopyInformation(const DataObject *data) override
A Function object used to dispatching to a routine to copy a region (start index and size)...
Represent the size (bounds) of a n-dimensional image.
Definition: itkSize.h:52
virtual void SetDirection(const DirectionType &direction)
virtual void operator()(ImageRegion< D1 > &destRegion, const ImageRegion< D2 > &srcRegion) const
Templated class to produce a unique type &quot;true&quot; and &quot;false&quot;.
An image region represents a structured region of data.
void ImageToImageFilterDefaultCopyInformation(const typename BinaryUnsignedIntDispatch< D1, D2 >::FirstEqualsSecondType &, ImageBase< D1 > *destImage, const ImageBase< D2 > *srcImage)
virtual void SetSpacing(const SpacingType &spacing)
virtual const SpacingType & GetSpacing() const
A Function object used to copy image meta-data of an image.
virtual void SetNumberOfComponentsPerPixel(unsigned int)
void ImageToImageFilterDefaultCopyRegion(const typename BinaryUnsignedIntDispatch< D1, D2 >::FirstEqualsSecondType &, ImageRegion< D1 > &destRegion, const ImageRegion< D2 > &srcRegion)
virtual unsigned int GetNumberOfComponentsPerPixel() const
Templated class to produce a unique type for a pairing of booleans.
const SizeType & GetSize() const
Templated class to produce a unique type for a pairing of integers.
Templated class to produce a unique type for each unsigned integer (usually a dimension).
Base class for templated image classes.
Definition: itkImageBase.h:114
virtual const DirectionType & GetDirection() const
Templated class to produce a unique type for each integer.
Templated class to produce a unique type for a pairing of unsigned integers (usually two dimensions)...
std::ostream & operator<<(std::ostream &os, const ImageRegionCopier< D1, D2 > &)
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:72
void SetIndex(const IndexType &index)
virtual void operator()(ImageBase< D1 > *destImage, const ImageBase< D2 > *srcImage) const
virtual const PointType & GetOrigin() const
Base class for a class used to dispatch to dimension specific implementations.
virtual void SetOrigin(PointType _arg)