ITK  5.3.0
Insight Toolkit
itkImageBase.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 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 #include "vnl/vnl_matrix_fixed.hxx" // Get the templates
43 
44 namespace itk
45 {
46 
104 template <unsigned int VImageDimension = 2>
105 class ITK_TEMPLATE_EXPORT ImageBase : public DataObject
106 {
107 public:
108  ITK_DISALLOW_COPY_AND_MOVE(ImageBase);
109 
111  using Self = ImageBase;
115 
117  itkNewMacro(Self);
118 
120  itkTypeMacro(ImageBase, DataObject);
121 
123  using ImageDimensionType = unsigned int;
124 
129  static constexpr ImageDimensionType ImageDimension = VImageDimension;
130 
134 
139 
143 
146 
153 
158 
163 
165  void
166  Initialize() override;
167 
169  static unsigned int
171  {
172  return VImageDimension;
173  }
174 
179  itkSetMacro(Origin, PointType);
180  virtual void
181  SetOrigin(const double origin[VImageDimension]);
182  virtual void
183  SetOrigin(const float origin[VImageDimension]);
211  virtual void
212  SetDirection(const DirectionType & direction);
213 
217  itkGetConstReferenceMacro(Direction, DirectionType);
218 
222  itkGetConstReferenceMacro(InverseDirection, DirectionType);
223 
228  itkGetConstReferenceMacro(Spacing, SpacingType);
229 
234  itkGetConstReferenceMacro(Origin, PointType);
235 
243  virtual void
244  Allocate(bool initialize = false);
245 
252  virtual void
253  SetLargestPossibleRegion(const RegionType & region);
254 
261  virtual const RegionType &
263  {
264  return m_LargestPossibleRegion;
265  }
266 
270  virtual void
271  SetBufferedRegion(const RegionType & region);
272 
276  virtual const RegionType &
278  {
279  return m_BufferedRegion;
280  }
281 
289  virtual void
290  SetRequestedRegion(const RegionType & region);
291 
299  void
300  SetRequestedRegion(const DataObject * data) override;
301 
306  virtual const RegionType &
308  {
309  return m_RequestedRegion;
310  }
311 
315  virtual void
316  SetRegions(const RegionType & region)
317  {
318  this->SetLargestPossibleRegion(region);
319  this->SetBufferedRegion(region);
320  this->SetRequestedRegion(region);
321  }
324  virtual void
325  SetRegions(const SizeType & size)
326  {
327  RegionType region;
328  region.SetSize(size);
329 
330  this->Self::SetRegions(region);
331  }
332 
343  const OffsetValueType *
345  {
346  return m_OffsetTable;
347  }
356  inline OffsetValueType
357  ComputeOffset(const IndexType & ind) const
358  {
359  OffsetValueType offset = 0;
360 
362  this->GetBufferedRegion().GetIndex(), ind, m_OffsetTable, offset);
363  return offset;
364  /* NON TEMPLATE_META_PROGRAMMING_LOOP_UNROLLING data version
365  * Leaving here for documentation purposes
366  * OffsetValueType ComputeOffset(const IndexType & ind) const
367  * {
368  * // need to add bounds checking for the region/buffer?
369  * OffsetValueType offset = 0;
370  * const IndexType & bufferedRegionIndex = this->GetBufferedRegion().GetIndex();
371  * // data is arranged as [][][][slice][row][col]
372  * // with Index[0] = col, Index[1] = row, Index[2] = slice
373  * for ( int i = VImageDimension - 1; i > 0; i-- )
374  * {
375  * offset += ( ind[i] - bufferedRegionIndex[i] ) * m_OffsetTable[i];
376  * }
377  * offset += ( ind[0] - bufferedRegionIndex[0] );
378  * return offset;
379  * }
380  */
381  }
382 
390  inline IndexType
392  {
393  IndexType index;
394  const IndexType & bufferedRegionIndex = this->GetBufferedRegion().GetIndex();
397  ImageHelper<VImageDimension, VImageDimension>::ComputeIndex(bufferedRegionIndex, offset, m_OffsetTable, index);
398  return index;
399  /* NON TEMPLATE_META_PROGRAMMING_LOOP_UNROLLING data version
400  * Leaving here for documentation purposes
401  * IndexType ComputeIndex(OffsetValueType offset) const
402  * {
403  * IndexType index;
404  * const IndexType & bufferedRegionIndex = this->GetBufferedRegion().GetIndex();
405  * for ( int i = VImageDimension - 1; i > 0; i-- )
406  * {
407  * index[i] = static_cast< IndexValueType >( offset / m_OffsetTable[i] );
408  * offset -= ( index[i] * m_OffsetTable[i] );
409  * index[i] += bufferedRegionIndex[i];
410  * }
411  * index[0] = bufferedRegionIndex[0] + static_cast< IndexValueType >( offset );
412  * return index;
413  * }
414  */
415  }
416 
423  virtual void
424  SetSpacing(const SpacingType & spacing);
425  virtual void
426  SetSpacing(const double spacing[VImageDimension]);
427  virtual void
428  SetSpacing(const float spacing[VImageDimension]);
446  template <typename TCoordRep>
447  IndexType
449  {
450  IndexType index;
453  for (unsigned int i = 0; i < VImageDimension; ++i)
454  {
455  TCoordRep sum = NumericTraits<TCoordRep>::ZeroValue();
456  for (unsigned int j = 0; j < VImageDimension; ++j)
457  {
458  sum += this->m_PhysicalPointToIndex[i][j] * (point[j] - this->m_Origin[j]);
459  }
460  index[i] = Math::RoundHalfIntegerUp<IndexValueType>(sum);
461  }
462  return index;
463  }
464 
469  template <typename TCoordRep>
470  bool
472  {
473  index = TransformPhysicalPointToIndex(point);
474 
475  // Now, check to see if the index is within allowed bounds
476  const bool isInside = this->GetLargestPossibleRegion().IsInside(index);
477  return isInside;
478  }
479 
480 
495  template <typename TIndexRep, typename TCoordRep>
498  {
501 
502  for (unsigned int k = 0; k < VImageDimension; ++k)
503  {
504  cvector[k] = point[k] - this->m_Origin[k];
505  }
506  cvector = m_PhysicalPointToIndex * cvector;
507  for (unsigned int i = 0; i < VImageDimension; ++i)
508  {
509  index[i] = static_cast<TIndexRep>(cvector[i]);
510  }
511  return index;
512  }
513 
518  template <typename TCoordRep, typename TIndexRep>
519  bool
522  {
523  index = TransformPhysicalPointToContinuousIndex<TIndexRep>(point);
524 
525  // Now, check to see if the index is within allowed bounds
526  const bool isInside = this->GetLargestPossibleRegion().IsInside(index);
527  return isInside;
528  }
529 
534  template <typename TCoordRep, typename TIndexRep>
535  void
538  {
539  for (unsigned int r = 0; r < VImageDimension; ++r)
540  {
541  TCoordRep sum = NumericTraits<TCoordRep>::ZeroValue();
542  for (unsigned int c = 0; c < VImageDimension; ++c)
543  {
544  sum += this->m_IndexToPhysicalPoint(r, c) * index[c];
545  }
546  point[r] = sum + this->m_Origin[r];
547  }
548  }
555  template <typename TCoordRep, typename TIndexRep>
558  {
560  TransformContinuousIndexToPhysicalPoint(index, point);
561  return point;
562  }
570  template <typename TCoordRep>
571  void
573  {
574  for (unsigned int i = 0; i < VImageDimension; ++i)
575  {
576  point[i] = this->m_Origin[i];
577  for (unsigned int j = 0; j < VImageDimension; ++j)
578  {
579  point[i] += m_IndexToPhysicalPoint[i][j] * index[j];
580  }
581  }
582  }
590  template <typename TCoordRep>
593  {
595  TransformIndexToPhysicalPoint(index, point);
596  return point;
597  }
614  template <typename TCoordRep>
615  void
617  FixedArray<TCoordRep, VImageDimension> & outputGradient) const
618  {
619  const DirectionType & direction = this->GetDirection();
620 
621  itkAssertInDebugAndIgnoreInReleaseMacro(inputGradient.GetDataPointer() != outputGradient.GetDataPointer());
622 
623  for (unsigned int i = 0; i < VImageDimension; ++i)
624  {
625  using CoordSumType = typename NumericTraits<TCoordRep>::AccumulateType;
626  CoordSumType sum = NumericTraits<CoordSumType>::ZeroValue();
627  for (unsigned int j = 0; j < VImageDimension; ++j)
628  {
629  sum += direction[i][j] * inputGradient[j];
630  }
631  outputGradient[i] = static_cast<TCoordRep>(sum);
632  }
633  }
634 
641  template <typename TVector>
642  TVector
643  TransformLocalVectorToPhysicalVector(const TVector & inputGradient) const
644  {
645  TVector outputGradient;
646  TransformLocalVectorToPhysicalVector(inputGradient, outputGradient);
647  return outputGradient;
648  }
664  template <typename TCoordRep>
665  void
667  FixedArray<TCoordRep, VImageDimension> & outputGradient) const
668  {
669  const DirectionType & inverseDirection = this->GetInverseDirection();
670 
671  itkAssertInDebugAndIgnoreInReleaseMacro(inputGradient.GetDataPointer() != outputGradient.GetDataPointer());
672 
673  for (unsigned int i = 0; i < VImageDimension; ++i)
674  {
675  using CoordSumType = typename NumericTraits<TCoordRep>::AccumulateType;
676  CoordSumType sum = NumericTraits<CoordSumType>::ZeroValue();
677  for (unsigned int j = 0; j < VImageDimension; ++j)
678  {
679  sum += inverseDirection[i][j] * inputGradient[j];
680  }
681  outputGradient[i] = static_cast<TCoordRep>(sum);
682  }
683  }
684 
691  template <typename TVector>
692  TVector
693  TransformPhysicalVectorToLocalVector(const TVector & inputGradient) const
694  {
695  TVector outputGradient;
696  TransformPhysicalVectorToLocalVector(inputGradient, outputGradient);
697  return outputGradient;
698  }
710  void
711  CopyInformation(const DataObject * data) override;
712 
723  virtual void
724  Graft(const Self * image);
725 
733  void
734  UpdateOutputInformation() override;
735 
743  void
744  UpdateOutputData() override;
745 
749  void
750  SetRequestedRegionToLargestPossibleRegion() override;
751 
761  bool
762  RequestedRegionIsOutsideOfTheBufferedRegion() override;
763 
772  bool
773  VerifyRequestedRegion() override;
774 
793  virtual unsigned int
794  GetNumberOfComponentsPerPixel() const;
795  virtual void
796  SetNumberOfComponentsPerPixel(unsigned int);
799 protected:
800  ImageBase() = default;
801  ~ImageBase() override = default;
802  void
803  PrintSelf(std::ostream & os, Indent indent) const override;
804 
809  void
810  ComputeOffsetTable();
811 
817  virtual void
818  ComputeIndexToPhysicalPointMatrices();
819 
820 protected:
824  SpacingType m_Spacing{ MakeFilled<SpacingType>(1.0) };
825  PointType m_Origin{};
826  DirectionType m_Direction{ DirectionType::GetIdentity() };
827  DirectionType m_InverseDirection{ DirectionType::GetIdentity() };
832  DirectionType m_IndexToPhysicalPoint{ DirectionType::GetIdentity() };
833  DirectionType m_PhysicalPointToIndex{ DirectionType::GetIdentity() };
840  virtual void
841  InitializeBufferedRegion();
842 
854  FastComputeOffset(const IndexType & ind) const
855  {
856  OffsetValueType offset = 0;
858  Self::GetBufferedRegion().GetIndex(), ind, m_OffsetTable, offset);
859  return offset;
860  }
874  IndexType
876  {
877  IndexType index;
878  const IndexType & bufferedRegionIndex = Self::GetBufferedRegion().GetIndex();
879  ImageHelper<VImageDimension, VImageDimension>::ComputeIndex(bufferedRegionIndex, offset, m_OffsetTable, index);
880  return index;
881  }
884  void
885  Graft(const DataObject * data) override;
886 
887 private:
888  OffsetValueType m_OffsetTable[VImageDimension + 1]{};
889 
893 };
894 } // end namespace itk
895 
896 #ifndef ITK_MANUAL_INSTANTIATION
897 # include "itkImageBase.hxx"
898 #endif
899 
900 #endif
itk::ImageBase::TransformPhysicalPointToContinuousIndex
ContinuousIndex< TIndexRep, VImageDimension > TransformPhysicalPointToContinuousIndex(const Point< TCoordRep, VImageDimension > &point) const
Returns the continuous index from a physical point.
Definition: itkImageBase.h:497
itk::ImageBase< TImage::ImageDimension >::OffsetValueType
typename OffsetType::OffsetValueType OffsetValueType
Definition: itkImageBase.h:138
itk::ImageBase::GetBufferedRegion
virtual const RegionType & GetBufferedRegion() const
Definition: itkImageBase.h:277
itk::ImageBase::GetImageDimension
static unsigned int GetImageDimension()
Definition: itkImageBase.h:170
itkObjectFactory.h
itk::ImageBase::TransformLocalVectorToPhysicalVector
TVector TransformLocalVectorToPhysicalVector(const TVector &inputGradient) const
Definition: itkImageBase.h:643
itk::Index< VImageDimension >
itk::ImageBase::SetRegions
virtual void SetRegions(const SizeType &size)
Definition: itkImageBase.h:325
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
itkMatrix.h
itk::ImageBase::GetOffsetTable
const OffsetValueType * GetOffsetTable() const
Definition: itkImageBase.h:344
itk::ImageHelper::ComputeIndex
static void ComputeIndex(const IndexType &bufferedRegionIndex, OffsetValueType offset, const OffsetValueType offsetTable[], IndexType &index)
Definition: itkImageHelper.h:65
itkOffset.h
itkFloatTypes.h
itk::ImageBase
Base class for templated image classes.
Definition: itkImageBase.h:105
itkImageHelper.h
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:69
itk::ImageBase< TImage::ImageDimension >::SizeValueType
typename SizeType::SizeValueType SizeValueType
Definition: itkImageBase.h:142
itk::ImageBase< TImage::ImageDimension >::PointValueType
SpacePrecisionType PointValueType
Definition: itkImageBase.h:156
itk::GTest::TypedefsAndConstructors::Dimension2::PointType
ImageBaseType::PointType PointType
Definition: itkGTestTypedefsAndConstructors.h:51
itk::Vector< SpacingValueType, VImageDimension >
itk::NumericTraits::AccumulateType
double AccumulateType
Definition: itkNumericTraits.h:74
itk::ImageBase::TransformIndexToPhysicalPoint
void TransformIndexToPhysicalPoint(const IndexType &index, Point< TCoordRep, VImageDimension > &point) const
Definition: itkImageBase.h:572
itk::SmartPointer< Self >
itk::ImageBase
class ITK_TEMPLATE_EXPORT ImageBase
Definition: itkImageHelper.h:51
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itkDataObject.h
itk::ImageBase::m_LargestPossibleRegion
RegionType m_LargestPossibleRegion
Definition: itkImageBase.h:890
itk::ImageBase< TImage::ImageDimension >::IndexValueType
typename IndexType::IndexValueType IndexValueType
Definition: itkImageBase.h:133
itk::FixedArray::GetDataPointer
ValueType * GetDataPointer()
Definition: itkFixedArray.h:285
itk::ImageBase::TransformContinuousIndexToPhysicalPoint
void TransformContinuousIndexToPhysicalPoint(const ContinuousIndex< TIndexRep, VImageDimension > &index, Point< TCoordRep, VImageDimension > &point) const
Definition: itkImageBase.h:536
itkImageRegion.h
itk::IndexValueType
long IndexValueType
Definition: itkIntTypes.h:90
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::ImageBase::FastComputeIndex
IndexType FastComputeIndex(OffsetValueType offset) const
Definition: itkImageBase.h:875
itk::Index::GetIndex
const IndexValueType * GetIndex() const
Definition: itkIndex.h:232
itk::ImageBase::ComputeOffset
OffsetValueType ComputeOffset(const IndexType &ind) const
Definition: itkImageBase.h:357
itk::point
*par Constraints *The filter requires an image with at least two dimensions and a vector *length of at least The theory supports extension to scalar but *the implementation of the itk vector classes do not **The template parameter TRealType must be floating point(float or double) or *a user-defined "real" numerical type with arithmetic operations defined *sufficient to compute derivatives. **\par Performance *This filter will automatically multithread if run with *SetUsePrincipleComponents
itk::ImageBase::TransformContinuousIndexToPhysicalPoint
Point< TCoordRep, VImageDimension > TransformContinuousIndexToPhysicalPoint(const ContinuousIndex< TIndexRep, VImageDimension > &index) const
Definition: itkImageBase.h:557
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::SpacePrecisionType
double SpacePrecisionType
Definition: itkFloatTypes.h:30
itk::ImageBase::m_BufferedRegion
RegionType m_BufferedRegion
Definition: itkImageBase.h:892
itkFixedArray.h
itk::ImageBase::m_RequestedRegion
RegionType m_RequestedRegion
Definition: itkImageBase.h:891
itk::ImageBase::TransformPhysicalVectorToLocalVector
TVector TransformPhysicalVectorToLocalVector(const TVector &inputGradient) const
Definition: itkImageBase.h:693
itk::ImageBase::TransformPhysicalPointToContinuousIndex
bool TransformPhysicalPointToContinuousIndex(const Point< TCoordRep, VImageDimension > &point, ContinuousIndex< TIndexRep, VImageDimension > &index) const
Get the continuous index from a physical point.
Definition: itkImageBase.h:520
itk::OffsetValueType
long OffsetValueType
Definition: itkIntTypes.h:94
itk::ImageBase::SetRegions
virtual void SetRegions(const RegionType &region)
Definition: itkImageBase.h:316
itk::ImageBase::TransformPhysicalVectorToLocalVector
void TransformPhysicalVectorToLocalVector(const FixedArray< TCoordRep, VImageDimension > &inputGradient, FixedArray< TCoordRep, VImageDimension > &outputGradient) const
Definition: itkImageBase.h:666
itk::DataObject
class ITK_FORWARD_EXPORT DataObject
Definition: itkDataObject.h:42
itk::FixedArray
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:53
itk::Matrix< SpacePrecisionType, VImageDimension, VImageDimension >
itk::ImageBase::GetLargestPossibleRegion
virtual const RegionType & GetLargestPossibleRegion() const
Definition: itkImageBase.h:262
itk::Offset
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image.
Definition: itkOffset.h:69
itk::ImageBase::GetRequestedRegion
virtual const RegionType & GetRequestedRegion() const
Definition: itkImageBase.h:307
itk::ImageHelper::ComputeOffset
static void ComputeOffset(const IndexType &bufferedRegionIndex, const IndexType &index, const OffsetValueType offsetTable[], OffsetValueType &offset)
Definition: itkImageHelper.h:103
itk::ImageBase< TImage::ImageDimension >::SpacingValueType
SpacePrecisionType SpacingValueType
Definition: itkImageBase.h:151
itk::ImageBase::TransformPhysicalPointToIndex
IndexType TransformPhysicalPointToIndex(const Point< TCoordRep, VImageDimension > &point) const
Definition: itkImageBase.h:448
itk::NumericTraits::ZeroValue
static T ZeroValue()
Definition: itkNumericTraits.h:148
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ContinuousIndex
A templated class holding a point in n-Dimensional image space.
Definition: itkContinuousIndex.h:46
itk::ImageBase::TransformLocalVectorToPhysicalVector
void TransformLocalVectorToPhysicalVector(const FixedArray< TCoordRep, VImageDimension > &inputGradient, FixedArray< TCoordRep, VImageDimension > &outputGradient) const
Definition: itkImageBase.h:616
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
itk::ImageBase::TransformIndexToPhysicalPoint
Point< TCoordRep, VImageDimension > TransformIndexToPhysicalPoint(const IndexType &index) const
Definition: itkImageBase.h:592
itk::ImageBase::FastComputeOffset
OffsetValueType FastComputeOffset(const IndexType &ind) const
Definition: itkImageBase.h:854
itk::Point< PointValueType, VImageDimension >
itk::ImageBase::ComputeIndex
IndexType ComputeIndex(OffsetValueType offset) const
Definition: itkImageBase.h:391
itk::ImageBase::TransformPhysicalPointToIndex
bool TransformPhysicalPointToIndex(const Point< TCoordRep, VImageDimension > &point, IndexType &index) const
Definition: itkImageBase.h:471
AddImageFilter
Definition: itkAddImageFilter.h:80
itk::ImageRegion::SetSize
void SetSize(const SizeType &size)
Definition: itkImageRegion.h:174
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293