ITK  4.10.0
Insight Segmentation and Registration Toolkit
itkImageBase.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 itkImageBase_h
29 #define itkImageBase_h
30 
31 #include "itkDataObject.h"
32 
33 #include "itkImageRegion.h"
34 #include "itkMatrix.h"
35 #include "itkObjectFactory.h"
36 #include "itkOffset.h"
37 #include "itkFixedArray.h"
38 #include "itkImageHelper.h"
39 #include "itkFloatTypes.h"
40 
41 #include <vxl_version.h>
42 #if VXL_VERSION_DATE_FULL < 20160229
43 #include "vnl/vnl_matrix_fixed.txx" // Get the templates
44 #else
45 #include "vnl/vnl_matrix_fixed.hxx" // Get the templates
46 #endif
47 
49 
50 namespace itk
51 {
52 
53 /* Forward declaration (ImageTransformHelper include's ImageBase) */
54 template< unsigned int NImageDimension, unsigned int R, unsigned int C, typename TPointValue, typename TMatrixValue >
56 
114 template< unsigned int VImageDimension = 2 >
115 class ImageBase:public DataObject
116 {
117 public:
119  typedef ImageBase Self;
123 
125  itkNewMacro(Self);
126 
128  itkTypeMacro(ImageBase, DataObject);
129 
131  typedef unsigned int ImageDimensionType;
132 
137  itkStaticConstMacro(ImageDimension, ImageDimensionType, VImageDimension);
138 
142 
147 
151 
154 
161 
166 
171 
173  virtual void Initialize() ITK_OVERRIDE;
174 
176  static unsigned int GetImageDimension()
177  { return VImageDimension; }
178 
183  itkSetMacro(Origin, PointType);
184  virtual void SetOrigin(const double origin[VImageDimension]);
185  virtual void SetOrigin(const float origin[VImageDimension]);
187 
213  virtual void SetDirection(const DirectionType & direction);
214 
218  itkGetConstReferenceMacro(Direction, DirectionType);
219 
223  itkGetConstReferenceMacro(InverseDirection, DirectionType);
224 
229  itkGetConstReferenceMacro(Spacing, SpacingType);
230 
235  itkGetConstReferenceMacro(Origin, PointType);
236 
244  virtual void Allocate(bool initialize=false);
245 
252  virtual void SetLargestPossibleRegion(const RegionType & region);
253 
260  virtual const RegionType & GetLargestPossibleRegion() const
261  { return m_LargestPossibleRegion; }
262 
266  virtual void SetBufferedRegion(const RegionType & region);
267 
271  virtual const RegionType & GetBufferedRegion() const
272  { return m_BufferedRegion; }
273 
281  virtual void SetRequestedRegion(const RegionType & region);
282 
290  virtual void SetRequestedRegion( const DataObject *data ) ITK_OVERRIDE;
291 
296  virtual const RegionType & GetRequestedRegion() const
297  { return m_RequestedRegion; }
298 
302  virtual void SetRegions(const RegionType& region)
303  {
304  this->SetLargestPossibleRegion(region);
305  this->SetBufferedRegion(region);
306  this->SetRequestedRegion(region);
307  }
309 
310  virtual void SetRegions(const SizeType& size)
311  {
312  RegionType region; region.SetSize(size);
313 
314  this->SetLargestPossibleRegion(region);
315  this->SetBufferedRegion(region);
316  this->SetRequestedRegion(region);
317  }
318 
329  const OffsetValueType * GetOffsetTable() const { return m_OffsetTable; }
331 
338  inline OffsetValueType ComputeOffset(const IndexType & ind) const
339  {
340  OffsetValueType offset = 0;
341 
343  ind,
345  offset);
346  return offset;
347  /* NON TEMPLATE_META_PROGRAMMING_LOOP_UNROLLING data version
348  * Leaving here for documentation purposes
349  * OffsetValueType ComputeOffset(const IndexType & ind) const
350  * {
351  * // need to add bounds checking for the region/buffer?
352  * OffsetValueType offset = 0;
353  * const IndexType & bufferedRegionIndex = this->GetBufferedRegion().GetIndex();
354  * // data is arranged as [][][][slice][row][col]
355  * // with Index[0] = col, Index[1] = row, Index[2] = slice
356  * for ( int i = VImageDimension - 1; i > 0; i-- )
357  * {
358  * offset += ( ind[i] - bufferedRegionIndex[i] ) * m_OffsetTable[i];
359  * }
360  * offset += ( ind[0] - bufferedRegionIndex[0] );
361  * return offset;
362  * }
363  */
364  }
365 
373  inline IndexType ComputeIndex(OffsetValueType offset) const
374  {
375  IndexType index;
376  const IndexType & bufferedRegionIndex = this->GetBufferedRegion().GetIndex();
378 
380  offset,
382  index);
383  return index;
384  /* NON TEMPLATE_META_PROGRAMMING_LOOP_UNROLLING data version
385  * Leaving here for documentation purposes
386  * IndexType ComputeIndex(OffsetValueType offset) const
387  * {
388  * IndexType index;
389  * const IndexType & bufferedRegionIndex = this->GetBufferedRegion().GetIndex();
390  * for ( int i = VImageDimension - 1; i > 0; i-- )
391  * {
392  * index[i] = static_cast< IndexValueType >( offset / m_OffsetTable[i] );
393  * offset -= ( index[i] * m_OffsetTable[i] );
394  * index[i] += bufferedRegionIndex[i];
395  * }
396  * index[0] = bufferedRegionIndex[0] + static_cast< IndexValueType >( offset );
397  * return index;
398  * }
399  */
400 
401  }
402 
409  virtual void SetSpacing(const SpacingType & spacing);
410  virtual void SetSpacing(const double spacing[VImageDimension]);
411  virtual void SetSpacing(const float spacing[VImageDimension]);
413 
418  template< typename TCoordRep >
421  IndexType & index) const
422  {
425 
426  // Now, check to see if the index is within allowed bounds
427  const bool isInside = this->GetLargestPossibleRegion().IsInside(index);
428  return isInside;
429  /* NON TEMPLATE_META_PROGRAMMING_LOOP_UNROLLING data version
430  * Leaving here for documentation purposes
431  * template< typename TCoordRep >
432  * bool TransformPhysicalPointToIndex(
433  * const Point< TCoordRep, VImageDimension > & point,
434  * IndexType & index) const
435  * {
436  * for ( unsigned int i = 0; i < VImageDimension; i++ )
437  * {
438  * TCoordRep sum = NumericTraits< TCoordRep >::ZeroValue();
439  * for ( unsigned int j = 0; j < VImageDimension; j++ )
440  * {
441  * sum += this->m_PhysicalPointToIndex[i][j] * ( point[j] - this->m_Origin[j] );
442  * }
443  * index[i] = Math::RoundHalfIntegerUp< IndexValueType >(sum);
444  * }
445  * // Now, check to see if the index is within allowed bounds
446  * const bool isInside = this->GetLargestPossibleRegion().IsInside(index);
447  * return isInside;
448  * }
449  */
450  }
451 
456  template< typename TCoordRep, typename TIndexRep >
460  {
462 
463  for ( unsigned int k = 0; k < VImageDimension; ++k )
464  {
465  cvector[k] = point[k] - this->m_Origin[k];
466  }
467  cvector = m_PhysicalPointToIndex * cvector;
468  for ( unsigned int i = 0; i < VImageDimension; ++i )
469  {
470  index[i] = static_cast< TIndexRep >( cvector[i] );
471  }
472 
473  // Now, check to see if the index is within allowed bounds
474  const bool isInside = this->GetLargestPossibleRegion().IsInside(index);
475 
476  return isInside;
477  }
478 
483  template< typename TCoordRep, typename TIndexRep >
487  {
488  for ( unsigned int r = 0; r < VImageDimension; ++r )
489  {
490  TCoordRep sum = NumericTraits< TCoordRep >::ZeroValue();
491  for ( unsigned int c = 0; c < VImageDimension; ++c )
492  {
493  sum += this->m_IndexToPhysicalPoint(r, c) * index[c];
494  }
495  point[r] = sum + this->m_Origin[r];
496  }
497  }
499 
505  template< typename TCoordRep >
507  const IndexType & index,
509  {
512  /* NON TEMPLATE_META_PROGRAMMING_LOOP_UNROLLING data version
513  * Leaving here for documentation purposes
514  * template< typename TCoordRep >
515  * void TransformIndexToPhysicalPoint(
516  * const IndexType & index,
517  * Point< TCoordRep, VImageDimension > & point) const
518  * {
519  * for ( unsigned int i = 0; i < VImageDimension; ++i )
520  * {
521  * point[i] = this->m_Origin[i];
522  * for ( unsigned int j = 0; j < VImageDimension; ++j )
523  * {
524  * point[i] += m_IndexToPhysicalPoint[i][j] * index[j];
525  * }
526  * }
527  * }
528  */
529  }
531 
543  template< typename TCoordRep >
545  const FixedArray< TCoordRep, VImageDimension > & inputGradient,
546  FixedArray< TCoordRep, VImageDimension > & outputGradient) const
547  {
548  //
549  //TODO: This temporary implementation should be replaced with Template
550  // MetaProgramming.
551  //
552  const DirectionType & direction = this->GetDirection();
553 
554  for ( unsigned int i = 0; i < VImageDimension; ++i )
555  {
556  typedef typename NumericTraits< TCoordRep >::AccumulateType CoordSumType;
557  CoordSumType sum = NumericTraits< CoordSumType >::ZeroValue();
558  for ( unsigned int j = 0; j < VImageDimension; ++j )
559  {
560  sum += direction[i][j] * inputGradient[j];
561  }
562  outputGradient[i] = static_cast< TCoordRep >( sum );
563  }
564  }
565 
574  template< typename TCoordRep >
576  const FixedArray< TCoordRep, VImageDimension > & inputGradient,
577  FixedArray< TCoordRep, VImageDimension > & outputGradient) const
578  {
579  //
580  //TODO: This temporary implementation should be replaced with Template
581  // MetaProgramming.
582  //
583  const DirectionType & inverseDirection = this->GetInverseDirection();
584 
585  for ( unsigned int i = 0; i < VImageDimension; ++i )
586  {
587  typedef typename NumericTraits< TCoordRep >::AccumulateType CoordSumType;
588  CoordSumType sum = NumericTraits< CoordSumType >::ZeroValue();
589  for ( unsigned int j = 0; j < VImageDimension; ++j )
590  {
591  sum += inverseDirection[i][j] * inputGradient[j];
592  }
593  outputGradient[i] = static_cast< TCoordRep >( sum );
594  }
595  }
596 
606  virtual void CopyInformation(const DataObject *data) ITK_OVERRIDE;
607 
618  virtual void Graft(const DataObject *data) ITK_OVERRIDE;
619 
627  virtual void UpdateOutputInformation() ITK_OVERRIDE;
628 
636  virtual void UpdateOutputData() ITK_OVERRIDE;
637 
641  virtual void SetRequestedRegionToLargestPossibleRegion() ITK_OVERRIDE;
642 
652  virtual bool RequestedRegionIsOutsideOfTheBufferedRegion() ITK_OVERRIDE;
653 
662  virtual bool VerifyRequestedRegion() ITK_OVERRIDE;
663 
682  virtual unsigned int GetNumberOfComponentsPerPixel() const;
683  virtual void SetNumberOfComponentsPerPixel(unsigned int);
685 
686 protected:
687  ImageBase();
688  ~ImageBase();
689  virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
690 
695  void ComputeOffsetTable();
696 
703 
704 protected:
708  SpacingType m_Spacing;
709  PointType m_Origin;
710  DirectionType m_Direction;
711  DirectionType m_InverseDirection;
712 
715  DirectionType m_IndexToPhysicalPoint;
716  DirectionType m_PhysicalPointToIndex;
717 
722  virtual void InitializeBufferedRegion();
723 
734  OffsetValueType FastComputeOffset(const IndexType &ind) const
735  {
736  OffsetValueType offset = 0;
738  ind,
740  offset);
741  return offset;
742  }
744 
756  IndexType FastComputeIndex(OffsetValueType offset) const
757  {
758  IndexType index;
759  const IndexType &bufferedRegionIndex = Self::GetBufferedRegion().GetIndex();
761  offset,
763  index);
764  return index;
765  }
767 
768 private:
769  ImageBase(const Self &) ITK_DELETE_FUNCTION;
770  void operator=(const Self &) ITK_DELETE_FUNCTION;
771 
772  void InternalSetSpacing(const SpacingValueType spacing[VImageDimension])
773  {
774  SpacingType s(spacing);
775  this->SetSpacing(s);
776  }
777 
778  template <typename TSpacingValue>
779  void InternalSetSpacing(const TSpacingValue spacing[VImageDimension])
780  {
782  SpacingType s;
783  s.CastFrom(sf);
784  this->SetSpacing(s);
785  }
786 
787  OffsetValueType m_OffsetTable[VImageDimension + 1];
788 
790  RegionType m_RequestedRegion;
791  RegionType m_BufferedRegion;
792 };
793 } // end namespace itk
794 
795 #ifndef ITK_MANUAL_INSTANTIATION
796 #include "itkImageBase.hxx"
797 #endif
798 
799 #endif
void SetSize(const SizeType &size)
static void TransformIndexToPhysicalPoint(const MatrixType &matrix, const OriginType &origin, const IndexType &index, DoublePoint &point)
virtual void SetRegions(const RegionType &region)
Definition: itkImageBase.h:302
virtual bool RequestedRegionIsOutsideOfTheBufferedRegion() override
OffsetValueType m_OffsetTable[VImageDimension+1]
Definition: itkImageBase.h:787
const OffsetValueType * GetOffsetTable() const
Definition: itkImageBase.h:329
Index< VImageDimension > IndexType
Definition: itkImageBase.h:140
const IndexType & GetIndex() const
Represent the offset between two n-dimensional indexes in a n-dimensional image.
Definition: itkOffset.h:55
Fast index/physical index computation.
Definition: itkImageBase.h:55
static void ComputeOffset(const IndexType &bufferedRegionIndex, const IndexType &index, const OffsetValueType offsetTable[], OffsetValueType &offset)
itk::SizeValueType SizeValueType
Definition: itkSize.h:60
virtual void CopyInformation(const DataObject *data) override
Represent the size (bounds) of a n-dimensional image.
Definition: itkSize.h:52
RegionType m_BufferedRegion
Definition: itkImageBase.h:791
virtual void SetDirection(const DirectionType &direction)
virtual void Allocate(bool initialize=false)
bool IsInside(const IndexType &index) const
virtual const RegionType & GetLargestPossibleRegion() const
Definition: itkImageBase.h:260
IndexType FastComputeIndex(OffsetValueType offset) const
Definition: itkImageBase.h:756
SpacingType m_Spacing
Definition: itkImageBase.h:708
An image region represents a structured region of data.
RegionType m_RequestedRegion
Definition: itkImageBase.h:790
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes...
Definition: itkArray.h:30
SpacePrecisionType SpacingValueType
Definition: itkImageBase.h:159
void TransformIndexToPhysicalPoint(const IndexType &index, Point< TCoordRep, VImageDimension > &point) const
Definition: itkImageBase.h:506
virtual void UpdateOutputInformation() override
PointType m_Origin
Definition: itkImageBase.h:709
DirectionType m_IndexToPhysicalPoint
Definition: itkImageBase.h:715
static void ComputeIndex(const IndexType &bufferedRegionIndex, OffsetValueType offset, const OffsetValueType offsetTable[], IndexType &index)
virtual void Initialize() override
Size< VImageDimension > SizeType
Definition: itkImageBase.h:149
DirectionType m_InverseDirection
Definition: itkImageBase.h:711
Point< PointValueType, VImageDimension > PointType
Definition: itkImageBase.h:165
virtual void SetSpacing(const SpacingType &spacing)
Matrix< SpacePrecisionType, VImageDimension, VImageDimension > DirectionType
Definition: itkImageBase.h:170
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:50
virtual void SetRegions(const SizeType &size)
Definition: itkImageBase.h:310
unsigned int ImageDimensionType
Definition: itkImageBase.h:128
IndexType ComputeIndex(OffsetValueType offset) const
Definition: itkImageBase.h:373
DirectionType m_Direction
Definition: itkImageBase.h:710
::itk::IndexValueType IndexValueType
Definition: itkIndex.h:79
virtual void InitializeBufferedRegion()
bool TransformPhysicalPointToContinuousIndex(const Point< TCoordRep, VImageDimension > &point, ContinuousIndex< TIndexRep, VImageDimension > &index) const
Get the continuous index from a physical point.
Definition: itkImageBase.h:457
virtual void SetNumberOfComponentsPerPixel(unsigned int)
void ComputeOffsetTable()
virtual void Graft(const DataObject *data) override
virtual const RegionType & GetBufferedRegion() const
Definition: itkImageBase.h:271
Offset< VImageDimension > OffsetType
Definition: itkImageBase.h:145
virtual const DirectionType & GetInverseDirection() const
SmartPointer< const Self > ConstPointer
Definition: itkImageBase.h:122
virtual unsigned int GetNumberOfComponentsPerPixel() const
Vector< SpacingValueType, VImageDimension > SpacingType
Definition: itkImageBase.h:160
void TransformLocalVectorToPhysicalVector(const FixedArray< TCoordRep, VImageDimension > &inputGradient, FixedArray< TCoordRep, VImageDimension > &outputGradient) const
Definition: itkImageBase.h:544
OffsetValueType FastComputeOffset(const IndexType &ind) const
Definition: itkImageBase.h:734
void CastFrom(const Vector< TCoordRepB, NVectorDimension > &pa)
Definition: itkVector.h:243
static const ImageDimensionType ImageDimension
Definition: itkImageBase.h:137
void TransformPhysicalVectorToLocalVector(const FixedArray< TCoordRep, VImageDimension > &inputGradient, FixedArray< TCoordRep, VImageDimension > &outputGradient) const
Definition: itkImageBase.h:575
static unsigned int GetImageDimension()
Definition: itkImageBase.h:176
SmartPointer< Self > Pointer
Definition: itkImageBase.h:121
SizeType::SizeValueType SizeValueType
Definition: itkImageBase.h:150
OffsetType::OffsetValueType OffsetValueType
Definition: itkImageBase.h:146
virtual void PrintSelf(std::ostream &os, Indent indent) const override
void InternalSetSpacing(const TSpacingValue spacing[VImageDimension])
Definition: itkImageBase.h:779
DataObject Superclass
Definition: itkImageBase.h:120
virtual void SetLargestPossibleRegion(const RegionType &region)
Base class for templated image classes.
Definition: itkImageBase.h:115
A templated class holding a point in n-Dimensional image space.
void TransformContinuousIndexToPhysicalPoint(const ContinuousIndex< TIndexRep, VImageDimension > &index, Point< TCoordRep, VImageDimension > &point) const
Definition: itkImageBase.h:484
DirectionType m_PhysicalPointToIndex
Definition: itkImageBase.h:716
virtual const DirectionType & GetDirection() const
virtual const RegionType & GetRequestedRegion() const
Definition: itkImageBase.h:296
virtual void ComputeIndexToPhysicalPointMatrices()
Control indentation during Print() invocation.
Definition: itkIndent.h:49
void InternalSetSpacing(const SpacingValueType spacing[VImageDimension])
Definition: itkImageBase.h:772
OffsetValueType ComputeOffset(const IndexType &ind) const
Definition: itkImageBase.h:338
double SpacePrecisionType
Definition: itkFloatTypes.h:30
virtual void UpdateOutputData() override
ImageRegion< VImageDimension > RegionType
Definition: itkImageBase.h:153
ImageBase Self
Definition: itkImageBase.h:119
itk::OffsetValueType OffsetValueType
Definition: itkOffset.h:69
SpacePrecisionType PointValueType
Definition: itkImageBase.h:164
Base class for all data objects in ITK.
virtual void SetRequestedRegionToLargestPossibleRegion() override
IndexType::IndexValueType IndexValueType
Definition: itkImageBase.h:141
RegionType m_LargestPossibleRegion
Definition: itkImageBase.h:789
virtual bool VerifyRequestedRegion() override
bool TransformPhysicalPointToIndex(const Point< TCoordRep, VImageDimension > &point, IndexType &index) const
Definition: itkImageBase.h:419
virtual void SetRequestedRegion(const RegionType &region)
void operator=(const Self &) ITK_DELETE_FUNCTION
virtual void SetBufferedRegion(const RegionType &region)
virtual void SetOrigin(PointType _arg)