ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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
44namespace itk
45{
46
52inline constexpr double DefaultImageCoordinateTolerance = 1e-6;
53inline constexpr double DefaultImageDirectionTolerance = 1e-6;
54
55
113template <unsigned int VImageDimension = 2>
114class ITK_TEMPLATE_EXPORT ImageBase : public DataObject
115{
116public:
117 ITK_DISALLOW_COPY_AND_MOVE(ImageBase);
118
124
126 itkNewMacro(Self);
127
129 itkOverrideGetNameOfClassMacro(ImageBase);
130
132 using ImageDimensionType = unsigned int;
133
138 static constexpr ImageDimensionType ImageDimension = VImageDimension;
139
143
162
172
174 void
175 Initialize() override;
176
178 static constexpr unsigned int
180 {
181 return VImageDimension;
182 }
183
189 itkSetMacro(Origin, PointType);
190 virtual void
191 SetOrigin(const double origin[VImageDimension]);
192 virtual void
193 SetOrigin(const float origin[VImageDimension]);
220 virtual void
221 SetDirection(const DirectionType & direction);
222
226 itkGetConstReferenceMacro(Direction, DirectionType);
227
231 itkGetConstReferenceMacro(InverseDirection, DirectionType);
232
237 itkGetConstReferenceMacro(Spacing, SpacingType);
238
243 itkGetConstReferenceMacro(Origin, PointType);
244
252 virtual void
253 Allocate(bool itkNotUsed(initialize) = false)
254 {}
255
258 void
260 {
261 return this->Allocate(true);
262 }
263
270 virtual void
272
279 virtual const RegionType &
281 {
283 }
284
288 virtual void
290
294 virtual const RegionType &
296 {
297 return m_BufferedRegion;
298 }
299
307 virtual void
309
317 void
318 SetRequestedRegion(const DataObject * data) override;
319
324 virtual const RegionType &
326 {
327 return m_RequestedRegion;
328 }
329
334 virtual void
335 SetRegions(const RegionType & region)
336 {
337 this->SetLargestPossibleRegion(region);
338 this->SetBufferedRegion(region);
339 this->SetRequestedRegion(region);
340 }
341
342 virtual void
343 SetRegions(const SizeType & size)
344 {
345 RegionType region;
346 region.SetSize(size);
347
348 this->Self::SetRegions(region);
349 }
350
351
362 const OffsetValueType *
364 {
365 return m_OffsetTable;
366 }
367
374 inline OffsetValueType
375 ComputeOffset(const IndexType & ind) const
376 {
377 OffsetValueType offset = 0;
378
380 this->GetBufferedRegion().GetIndex(), ind, m_OffsetTable, offset);
381 return offset;
382 /* NON TEMPLATE_META_PROGRAMMING_LOOP_UNROLLING data version
383 * Leaving here for documentation purposes
384 * OffsetValueType ComputeOffset(const IndexType & ind) const
385 * {
386 * // need to add bounds checking for the region/buffer?
387 * OffsetValueType offset = 0;
388 * const IndexType & bufferedRegionIndex = this->GetBufferedRegion().GetIndex();
389 * // data is arranged as [][][][slice][row][col]
390 * // with Index[0] = col, Index[1] = row, Index[2] = slice
391 * for ( int i = VImageDimension - 1; i > 0; i-- )
392 * {
393 * offset += ( ind[i] - bufferedRegionIndex[i] ) * m_OffsetTable[i];
394 * }
395 * offset += ( ind[0] - bufferedRegionIndex[0] );
396 * return offset;
397 * }
398 */
399 }
400
408 inline IndexType
410 {
411 IndexType index;
412 const IndexType & bufferedRegionIndex = this->GetBufferedRegion().GetIndex();
413
415 return index;
416 /* NON TEMPLATE_META_PROGRAMMING_LOOP_UNROLLING data version
417 * Leaving here for documentation purposes
418 * IndexType ComputeIndex(OffsetValueType offset) const
419 * {
420 * IndexType index;
421 * const IndexType & bufferedRegionIndex = this->GetBufferedRegion().GetIndex();
422 * for ( int i = VImageDimension - 1; i > 0; i-- )
423 * {
424 * index[i] = static_cast< IndexValueType >( offset / m_OffsetTable[i] );
425 * offset -= ( index[i] * m_OffsetTable[i] );
426 * index[i] += bufferedRegionIndex[i];
427 * }
428 * index[0] = bufferedRegionIndex[0] + static_cast< IndexValueType >( offset );
429 * return index;
430 * }
431 */
432 }
433
441 virtual void
442 SetSpacing(const SpacingType & spacing);
443 virtual void
444 SetSpacing(const double spacing[VImageDimension]);
445 virtual void
446 SetSpacing(const float spacing[VImageDimension]);
463 template <typename TCoordinate>
464 [[nodiscard]] IndexType
466 {
467 IndexType index;
468
469 for (unsigned int i = 0; i < VImageDimension; ++i)
470 {
471 TCoordinate sum{};
472 for (unsigned int j = 0; j < VImageDimension; ++j)
473 {
474 sum += this->m_PhysicalPointToIndex[i][j] * (point[j] - this->m_Origin[j]);
475 }
477 }
478 return index;
479 }
480
489 template <typename TCoordinate>
490 ITK_NODISCARD("Call the overload which has the point as the only parameter and returns the index")
491 bool TransformPhysicalPointToIndex(const Point<TCoordinate, VImageDimension> & point, IndexType & index) const
492 {
493 index = TransformPhysicalPointToIndex(point);
494
495 // Now, check to see if the index is within allowed bounds
496 const bool isInside = this->GetLargestPossibleRegion().IsInside(index);
497 return isInside;
498 }
499
500
515 template <typename TIndexRep, typename TCoordinate>
518 {
521
522 for (unsigned int k = 0; k < VImageDimension; ++k)
523 {
524 cvector[k] = point[k] - this->m_Origin[k];
525 }
526 cvector = m_PhysicalPointToIndex * cvector;
527 for (unsigned int i = 0; i < VImageDimension; ++i)
528 {
529 index[i] = static_cast<TIndexRep>(cvector[i]);
530 }
531 return index;
532 }
533
542 template <typename TCoordinate, typename TIndexRep>
543 ITK_NODISCARD("Call the overload which has the point as the only parameter and returns the index")
544 bool TransformPhysicalPointToContinuousIndex(const Point<TCoordinate, VImageDimension> & point,
545 ContinuousIndex<TIndexRep, VImageDimension> & index) const
546 {
548
549 // Now, check to see if the index is within allowed bounds
550 const bool isInside = this->GetLargestPossibleRegion().IsInside(index);
551 return isInside;
552 }
553
558 template <typename TCoordinate, typename TIndexRep>
559 void
562 {
563 for (unsigned int r = 0; r < VImageDimension; ++r)
564 {
565 TCoordinate sum{};
566 for (unsigned int c = 0; c < VImageDimension; ++c)
567 {
568 sum += this->m_IndexToPhysicalPoint(r, c) * index[c];
569 }
570 point[r] = sum + this->m_Origin[r];
571 }
572 }
573
578 template <typename TCoordinate, typename TIndexRep>
586
592 template <typename TCoordinate>
593 void
595 {
596 for (unsigned int i = 0; i < VImageDimension; ++i)
597 {
598 point[i] = this->m_Origin[i];
599 for (unsigned int j = 0; j < VImageDimension; ++j)
600 {
601 point[i] += m_IndexToPhysicalPoint[i][j] * index[j];
602 }
603 }
604 }
605
611 template <typename TCoordinate>
614 {
616 TransformIndexToPhysicalPoint(index, point);
617 return point;
618 }
619
634 template <typename TCoordinate>
635 void
637 FixedArray<TCoordinate, VImageDimension> & outputGradient) const
638 {
639 const DirectionType & direction = this->GetDirection();
640
641 itkAssertInDebugAndIgnoreInReleaseMacro(inputGradient.GetDataPointer() != outputGradient.GetDataPointer());
642
643 for (unsigned int i = 0; i < VImageDimension; ++i)
644 {
645 using CoordSumType = typename NumericTraits<TCoordinate>::AccumulateType;
646 CoordSumType sum{};
647 for (unsigned int j = 0; j < VImageDimension; ++j)
648 {
649 sum += direction[i][j] * inputGradient[j];
650 }
651 outputGradient[i] = static_cast<TCoordinate>(sum);
652 }
653 }
654
661 template <typename TVector>
662 [[nodiscard]] TVector
663 TransformLocalVectorToPhysicalVector(const TVector & inputGradient) const
664 {
665 TVector outputGradient;
666 TransformLocalVectorToPhysicalVector(inputGradient, outputGradient);
667 return outputGradient;
668 }
669
670
683 template <typename TCoordinate>
684 void
686 FixedArray<TCoordinate, VImageDimension> & outputGradient) const
687 {
688 const DirectionType & inverseDirection = this->GetInverseDirection();
689
690 itkAssertInDebugAndIgnoreInReleaseMacro(inputGradient.GetDataPointer() != outputGradient.GetDataPointer());
691
692 for (unsigned int i = 0; i < VImageDimension; ++i)
693 {
694 using CoordSumType = typename NumericTraits<TCoordinate>::AccumulateType;
695 CoordSumType sum{};
696 for (unsigned int j = 0; j < VImageDimension; ++j)
697 {
698 sum += inverseDirection[i][j] * inputGradient[j];
699 }
700 outputGradient[i] = static_cast<TCoordinate>(sum);
701 }
702 }
703
710 template <typename TVector>
711 [[nodiscard]] TVector
712 TransformPhysicalVectorToLocalVector(const TVector & inputGradient) const
713 {
714 TVector outputGradient;
715 TransformPhysicalVectorToLocalVector(inputGradient, outputGradient);
716 return outputGradient;
717 }
718
728 void
729 CopyInformation(const DataObject * data) override;
730
741 virtual void
742 Graft(const Self * image);
743
751 void
753
761 void
763
767 void
769
779 bool
781
790 bool
792
796 bool
797 IsCongruentImageGeometry(const ImageBase * otherImage, double coordinateTolerance, double directionTolerance) const;
798
806 bool
808 double coordinateTolerance = DefaultImageCoordinateTolerance,
809 double directionTolerance = DefaultImageDirectionTolerance) const;
810
830 virtual unsigned int
832 virtual void
835protected:
836 ImageBase() = default;
837 ~ImageBase() override = default;
838 void
839 PrintSelf(std::ostream & os, Indent indent) const override;
840
845 void
847
853 virtual void
855
856protected:
876 virtual void
878
890 FastComputeOffset(const IndexType & ind) const
891 {
892 OffsetValueType offset = 0;
894 Self::GetBufferedRegion().GetIndex(), ind, m_OffsetTable, offset);
895 return offset;
896 }
897
909 IndexType
911 {
912 IndexType index;
913 const IndexType & bufferedRegionIndex = Self::GetBufferedRegion().GetIndex();
915 return index;
916 }
917
918 void
919 Graft(const DataObject * data) override;
920
921private:
922 OffsetValueType m_OffsetTable[VImageDimension + 1]{};
923
927};
928} // end namespace itk
929
930#ifndef ITK_MANUAL_INSTANTIATION
931# include "itkImageBase.hxx"
932#endif
933
934#endif
A templated class holding a point in n-Dimensional image space.
Simulate a standard C array with copy semantics.
ValueType * GetDataPointer()
void TransformPhysicalVectorToLocalVector(const FixedArray< TCoordinate, VImageDimension > &inputGradient, FixedArray< TCoordinate, VImageDimension > &outputGradient) const
IndexType FastComputeIndex(OffsetValueType offset) const
Vector< SpacingValueType, VImageDimension > SpacingType
virtual void SetRequestedRegion(const RegionType &region)
IndexType ComputeIndex(OffsetValueType offset) const
virtual void InitializeBufferedRegion()
void SetRequestedRegionToLargestPossibleRegion() override
ImageRegion< VImageDimension > RegionType
ContinuousIndex< TIndexRep, VImageDimension > TransformPhysicalPointToContinuousIndex(const Point< TCoordinate, VImageDimension > &point) const
Returns the continuous index from a physical point.
bool VerifyRequestedRegion() override
SpacePrecisionType SpacingValueType
TVector TransformPhysicalVectorToLocalVector(const TVector &inputGradient) const
IndexType TransformPhysicalPointToIndex(const Point< TCoordinate, VImageDimension > &point) const
virtual void SetDirection(const DirectionType &direction)
bool IsSameImageGeometryAs(const ImageBase *otherImage, double coordinateTolerance=DefaultImageCoordinateTolerance, double directionTolerance=DefaultImageDirectionTolerance) const
virtual const DirectionType & GetDirection() const
void AllocateInitialized()
void PrintSelf(std::ostream &os, Indent indent) const override
virtual const RegionType & GetBufferedRegion() const
bool RequestedRegionIsOutsideOfTheBufferedRegion() override
virtual void SetRegions(const SizeType &size)
virtual void SetBufferedRegion(const RegionType &region)
Index< VImageDimension > IndexType
Offset< VImageDimension > OffsetType
virtual void SetSpacing(const SpacingType &spacing)
void UpdateOutputData() override
virtual void SetNumberOfComponentsPerPixel(unsigned int)
OffsetValueType m_OffsetTable[VImageDimension+1]
OffsetValueType FastComputeOffset(const IndexType &ind) const
virtual void Allocate(bool initialize=false)
TVector TransformLocalVectorToPhysicalVector(const TVector &inputGradient) const
virtual void SetOrigin(const float origin[VImageDimension])
typename OffsetType::OffsetValueType OffsetValueType
typename IndexType::IndexValueType IndexValueType
void UpdateOutputInformation() override
virtual void Graft(const Self *image)
void TransformContinuousIndexToPhysicalPoint(const ContinuousIndex< TIndexRep, VImageDimension > &index, Point< TCoordinate, VImageDimension > &point) const
SmartPointer< const Self > ConstPointer
virtual unsigned int GetNumberOfComponentsPerPixel() const
virtual void SetSpacing(const float spacing[VImageDimension])
const OffsetValueType * GetOffsetTable() const
virtual const RegionType & GetRequestedRegion() const
virtual void ComputeIndexToPhysicalPointMatrices()
void SetRequestedRegion(const DataObject *data) override
static constexpr ImageDimensionType ImageDimension
OffsetValueType ComputeOffset(const IndexType &ind) const
virtual void SetRegions(const RegionType &region)
Matrix< SpacePrecisionType, VImageDimension, VImageDimension > DirectionType
void CopyInformation(const DataObject *data) override
void TransformIndexToPhysicalPoint(const IndexType &index, Point< TCoordinate, VImageDimension > &point) const
virtual void SetOrigin(const double origin[VImageDimension])
Size< VImageDimension > SizeType
virtual void SetSpacing(const double spacing[VImageDimension])
ImageBase()=default
Point< PointValueType, VImageDimension > PointType
Point< TCoordinate, VImageDimension > TransformContinuousIndexToPhysicalPoint(const ContinuousIndex< TIndexRep, VImageDimension > &index) const
virtual void SetLargestPossibleRegion(const RegionType &region)
virtual const RegionType & GetLargestPossibleRegion() const
Point< TCoordinate, VImageDimension > TransformIndexToPhysicalPoint(const IndexType &index) const
void ComputeOffsetTable()
virtual const DirectionType & GetInverseDirection() const
~ImageBase() override=default
void Initialize() override
bool IsCongruentImageGeometry(const ImageBase *otherImage, double coordinateTolerance, double directionTolerance) const
typename SizeType::SizeValueType SizeValueType
void TransformLocalVectorToPhysicalVector(const FixedArray< TCoordinate, VImageDimension > &inputGradient, FixedArray< TCoordinate, VImageDimension > &outputGradient) const
void Graft(const DataObject *data) override
static constexpr unsigned int GetImageDimension()
static void ComputeIndex(const IndexType &bufferedRegionIndex, OffsetValueType offset, const OffsetValueType offsetTable[], IndexType &index)
static void ComputeOffset(const IndexType &bufferedRegionIndex, const IndexType &index, const OffsetValueType offsetTable[], OffsetValueType &offset)
An image region represents a structured region of data.
void SetSize(const SizeType &size)
Vector< SpacingValueType, VImageDimension > SpacingType
Control indentation during Print() invocation.
Definition itkIndent.h:50
A templated class holding a M x N size Matrix.
Definition itkMatrix.h:53
A templated class holding a geometric point in n-Dimensional space.
Definition itkPoint.h:54
Implements transparent reference counting.
Point< PointValueType, VImageDimension > PointType
typename OffsetType::OffsetValueType OffsetValueType
A templated class holding a n-Dimensional vector.
Definition itkVector.h:63
TInput RoundHalfIntegerUp(TInput x) template< typename TReturn
Round towards nearest integer (This is a synonym for RoundHalfIntegerUp)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
constexpr TContainer MakeFilled(typename TContainer::const_reference value)
constexpr double DefaultImageDirectionTolerance
constexpr double DefaultImageCoordinateTolerance
long OffsetValueType
Definition itkIntTypes.h:97
double SpacePrecisionType
Represent a n-dimensional index in a n-dimensional image.
Definition itkIndex.h:69
itk::IndexValueType IndexValueType
Definition itkIndex.h:79
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image.
Definition itkOffset.h:67
itk::OffsetValueType OffsetValueType
Definition itkOffset.h:77
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition itkSize.h:70
itk::SizeValueType SizeValueType
Definition itkSize.h:80