Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkBaseGeometry.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkBaseGeometry_h
14 #define mitkBaseGeometry_h
15 
16 #include <mitkOperationActor.h>
17 #include <MitkCoreExports.h>
18 #include <mitkCommon.h>
19 
20 #include <itkScalableAffineTransform.h>
21 #include <mitkNumericTypes.h>
22 #include <itkBoundingBox.h>
23 #include <itkIndex.h>
24 #include <itkQuaternionRigidTransform.h>
25 #include <mitkAffineTransform3D.h>
26 
28 #include <vtkTransform.h>
29 
30 class vtkMatrix4x4;
31 class vtkMatrixToLinearTransform;
32 class vtkLinearTransform;
33 
34 namespace mitk
35 {
42  typedef itk::BoundingBox<unsigned long, 3, ScalarType> BoundingBox;
43 
47  typedef itk::FixedArray<ScalarType, 2> TimeBounds;
48 
52  typedef itk::FixedArray<ScalarType, 3> FixedArrayType;
53 
93  class MITKCORE_EXPORT BaseGeometry : public itk::Object, public OperationActor
94  {
95  public:
97  itkCloneMacro(Self);
98 
99  // ********************************** TypeDef **********************************
100 
104  typedef itk::BoundingBox<unsigned long, 3, ScalarType> BoundingBoxType;
106  typedef BoundingBoxType::BoundsArrayType BoundsArrayType;
108  typedef BoundingBoxType::Pointer BoundingBoxPointer;
109 
110  // ********************************** Origin, Spacing **********************************
111 
120 
126  void SetOrigin(const Point3D &origin);
127 
134 
145  void SetSpacing(const mitk::Vector3D &aSpacing, bool enforceSetSpacing = false);
146 
154 
155  // ********************************** other functions **********************************
156 
161  itkGetConstMacro(FrameOfReferenceID, unsigned int);
166  itkSetMacro(FrameOfReferenceID, unsigned int);
167 
172  itkGetConstMacro(IndexToWorldTransformLastModified, unsigned long);
173 
183  void Modified() const override;
184 
185  friend class ModifiedLock;
186 
195  virtual bool IsValid() const;
196 
197  // ********************************** Initialize **********************************
198 
206  void Initialize();
207 
216  void InitializeGeometry(Self *newGeometry) const;
217 
218  // ********************************** Transformations Set/Get **********************************
219 
226 
233 
243 
253  void SetIndexToWorldTransformByVtkMatrix(vtkMatrix4x4 *vtkmatrix);
254 
265 
273 
279  vtkMatrix4x4 *GetVtkMatrix();
280 
286  const vtkMatrix4x4* GetVtkMatrix() const;
287 
293  vtkLinearTransform *GetVtkTransform() const;
294 
301  void SetIdentity();
302 
303  // ********************************** Transformations **********************************
304 
317  void Compose(const TransformType *other, bool pre = false);
318 
328  void Compose(const vtkMatrix4x4 *vtkmatrix, bool pre = false);
329 
337  void Translate(const Vector3D &vector);
338 
348  void ExecuteOperation(Operation *operation) override;
349 
361  void WorldToIndex(const mitk::Point3D &pt_mm, mitk::Point3D &pt_units) const;
362 
369  void WorldToIndex(const mitk::Vector3D &vec_mm, mitk::Vector3D &vec_units) const;
370 
381  template <unsigned int VIndexDimension>
382  void WorldToIndex(const mitk::Point3D &pt_mm, itk::Index<VIndexDimension> &index) const
383  {
384  typedef itk::Index<VIndexDimension> IndexType;
385  mitk::Point3D pt_units;
386  this->WorldToIndex(pt_mm, pt_units);
387  int i, dim = index.GetIndexDimension();
388  if (dim > 3)
389  {
390  index.Fill(0);
391  dim = 3;
392  }
393  for (i = 0; i < dim; ++i)
394  {
395  index[i] = itk::Math::RoundHalfIntegerUp<typename IndexType::IndexValueType>(pt_units[i]);
396  }
397  }
398 
405  void IndexToWorld(const mitk::Vector3D &vec_units, mitk::Vector3D &vec_mm) const;
406 
413  void IndexToWorld(const mitk::Point3D &pt_units, mitk::Point3D &pt_mm) const;
414 
422  template <unsigned int VIndexDimension>
423  void IndexToWorld(const itk::Index<VIndexDimension> &index, mitk::Point3D &pt_mm) const
424  {
425  mitk::Point3D pt_units;
426  pt_units.Fill(0);
427  int i, dim = index.GetIndexDimension();
428  if (dim > 3)
429  {
430  dim = 3;
431  }
432  for (i = 0; i < dim; ++i)
433  {
434  pt_units[i] = index[i];
435  }
436 
437  IndexToWorld(pt_units, pt_mm);
438  }
439 
452  template <class TCoordRep>
453  void ItkPhysicalPointToWorld(const itk::Point<TCoordRep, 3> &itkPhysicalPoint, mitk::Point3D &pt_mm) const
454  {
455  mitk::FillArray(pt_mm, itkPhysicalPoint);
456  }
457 
472  template <class TCoordRep>
473  void WorldToItkPhysicalPoint(const mitk::Point3D &pt_mm, itk::Point<TCoordRep, 3> &itkPhysicalPoint) const
474  {
475  mitk::FillArray(itkPhysicalPoint, pt_mm);
476  }
477 
478  // ********************************** BoundingBox **********************************
479 
485  itkGetConstObjectMacro(BoundingBox, BoundingBoxType);
486 
487 // a bit of a misuse, but we want only doxygen to see the following:
488 #ifdef DOXYGEN_SKIP
494  itkGetConstObjectMacro(BoundingBox, BoundingBoxType);
500  const BoundsArrayType GetBounds() const;
501 #endif
507  const BoundsArrayType GetBounds() const;
508 
516  void SetBounds(const BoundsArrayType &bounds);
517 
523  void SetFloatBounds(const float bounds[6]);
524 
530  void SetFloatBounds(const double bounds[6]);
531 
542  VnlVector GetMatrixColumn(unsigned int direction) const;
543 
555  mitk::BoundingBox::Pointer CalculateBoundingBoxRelativeToTransform(const mitk::AffineTransform3D *transform) const;
556 
557 //##Documentation
558 //## @brief Set the time bounds (in ms)
559 // void SetTimeBounds(const TimeBounds& timebounds);
560 
561 // ********************************** Geometry **********************************
562 
563 #ifdef DOXYGEN_SKIP
571  ScalarType GetExtent(unsigned int direction) const;
572 #endif
573 
582  ScalarType GetExtent(unsigned int direction) const;
583 
594  ScalarType GetExtentInMM(int direction) const;
595 
606  Vector3D GetAxisVector(unsigned int direction) const;
607 
617  virtual bool Is2DConvertable();
618 
627 
634  double GetDiagonalLength2() const;
635 
642  double GetDiagonalLength() const;
643 
656  Point3D GetCornerPoint(int id) const;
657 
667  Point3D GetCornerPoint(bool xFront = true, bool yFront = true, bool zFront = true) const;
668 
679  void SetExtentInMM(int direction, ScalarType extentInMM);
680 
689  bool IsInside(const mitk::Point3D &p) const;
690 
700  bool IsIndexInside(const mitk::Point3D &index) const;
701 
712  template <unsigned int VIndexDimension>
713  bool IsIndexInside(const itk::Index<VIndexDimension> &index) const
714  {
715  int i, dim = index.GetIndexDimension();
716  Point3D pt_index;
717  pt_index.Fill(0);
718  for (i = 0; i < dim; ++i)
719  {
720  pt_index[i] = index[i];
721  }
722  return IsIndexInside(pt_index);
723  }
724 
735  Point3D ClampPoint(const Point3D& point) const;
736 
737  // ********************************* Image Geometry ********************************
738 
750  virtual void ChangeImageGeometryConsideringOriginOffset(const bool isAnImageGeometry);
751 
758  itkGetConstMacro(ImageGeometry, bool)
759 
760 
771  itkSetMacro(ImageGeometry, bool);
772 
776  itkBooleanMacro(ImageGeometry);
777 
783  const GeometryTransformHolder *GetGeometryTransformHolder() const;
784 
797  void MapAxesToOrientations(int axes[]) const;
798 
799  protected:
800  // ********************************** Constructor **********************************
802  BaseGeometry(const BaseGeometry &other);
803  ~BaseGeometry() override;
804 
805  void PrintSelf(std::ostream &os, itk::Indent indent) const override;
806 
807  static const std::string GetTransformAsString(TransformType *transformType);
808 
809  itkGetConstMacro(NDimensions, unsigned int);
810 
811  bool IsBoundingBoxNull() const;
812 
813  bool IsIndexToWorldTransformNull() const;
814 
815  void SetVtkMatrixDeepCopy(vtkTransform *vtktransform);
816 
817  void _SetSpacing(const mitk::Vector3D &aSpacing, bool enforceSetSpacing = false);
818 
819  //##Documentation
820  //## @brief PreSetSpacing
821  //##
822  //## These virtual function allows a different beahiour in subclasses.
823  //## Do implement them in every subclass of BaseGeometry. If not needed, use
824  //## {Superclass::PreSetSpacing();};
825  virtual void PreSetSpacing(const mitk::Vector3D & /*aSpacing*/){};
826 
827  //##Documentation
828  //## @brief CheckBounds
829  //##
830  //## This function is called in SetBounds. Assertions can be implemented in this function (see PlaneGeometry.cpp).
831  //## If you implement this function in a subclass, make sure, that all classes were your class inherits from
832  //## have an implementation of CheckBounds
833  //## (e.g. inheritance BaseGeometry <- A <- B. Implementation of CheckBounds in class B needs implementation in A as
834  // well!)
835  virtual void CheckBounds(const BoundsArrayType & /*bounds*/){};
836 
837  //##Documentation
838  //## @brief CheckIndexToWorldTransform
839  //##
840  //## This function is called in SetIndexToWorldTransform. Assertions can be implemented in this function (see
841  // PlaneGeometry.cpp).
842  //## In Subclasses of BaseGeometry, implement own conditions or call Superclass::CheckBounds(bounds);.
843  virtual void CheckIndexToWorldTransform(mitk::AffineTransform3D * /*transform*/){};
844 
845  private:
846  GeometryTransformHolder *m_GeometryTransform;
847 
848  void InitializeGeometryTransformHolder(const BaseGeometry *otherGeometry);
849 
850  //##Documentation
851  //## @brief Bounding Box, which is axes-parallel in intrinsic coordinates
852  //## (often integer indices of pixels)
853  BoundingBoxPointer m_BoundingBox;
854 
855  unsigned int m_FrameOfReferenceID;
856 
857  // mitk::TimeBounds m_TimeBounds;
858 
859  static const unsigned int m_NDimensions = 3;
860 
861  mutable TransformType::Pointer m_InvertedTransform;
862 
863  mutable unsigned long m_IndexToWorldTransformLastModified;
864 
865  bool m_ImageGeometry;
866 
867  //##Documentation
868  //## @brief ModifiedLockFlag is used to prohibit the call of Modified()
869  //##
870  //## For the use of this Flag, see class ModifiedLock. This flag should only be set
871  //## by the ModifiedLock class!
872  bool m_ModifiedLockFlag;
873 
874  //##Documentation
875  //## @brief ModifiedcalledFlag is used to collect calls of Modified().
876  //##
877  //## For the use of this Flag, see class ModifiedLock. This flag should only be set
878  //## by the Modified() function!
879  mutable bool m_ModifiedCalledFlag;
880  };
881 
882  // ********************************** Equal Functions **********************************
883  //
884  // Static compare functions mainly for testing
885  //
905  MITKCORE_EXPORT bool Equal(const mitk::BaseGeometry& leftHandSide,
906  const mitk::BaseGeometry& rightHandSide,
907  ScalarType coordinateEps,
908  ScalarType directionEps,
909  bool verbose = false);
910 
925  MITKCORE_EXPORT bool Equal(const mitk::BaseGeometry &leftHandSide,
926  const mitk::BaseGeometry &rightHandSide,
928  bool verbose = false);
929 
945  const mitk::BaseGeometry::TransformType &rightHandSide,
946  ScalarType eps,
947  bool verbose);
948 
964  const mitk::BaseGeometry::BoundingBoxType &rightHandSide,
965  ScalarType eps,
966  bool verbose);
967 
990  const mitk::BaseGeometry& referenceGeo,
991  ScalarType coordinateEps,
992  ScalarType directionEps,
993  bool verbose = false);
994 
1009  const mitk::BaseGeometry& referenceGeo,
1011  bool verbose = false);
1012 
1013 } // namespace mitk
1014 
1015 #endif
#define MITKCORE_EXPORT
Abstract base class describing the geometry of a data object.
VnlVector GetMatrixColumn(unsigned int direction) const
Get a column of the IndexToWorldTransform matrix as a VnlVector.
mitk::BoundingBox::Pointer CalculateBoundingBoxRelativeToTransform(const mitk::AffineTransform3D *transform) const
Calculate a bounding box relative to a given transform.
void SetFloatBounds(const double bounds[6])
Set the bounding box via a double array.
bool IsIndexInside(const itk::Index< VIndexDimension > &index) const
Test whether a discrete ITK index lies inside the bounding box.
virtual bool IsValid() const
Check whether this geometry is in a valid state.
void IndexToWorld(const mitk::Point3D &pt_units, mitk::Point3D &pt_mm) const
Convert continuous or discrete index coordinates of a point to world coordinates (mm).
void Compose(const TransformType *other, bool pre=false)
Compose the IndexToWorldTransform with another transform.
mitk::Vector3D GetSpacing() const
Get the spacing (voxel size) in mm per unit for each axis.
vtkMatrix4x4 * GetVtkMatrix()
Get the IndexToWorldTransform as a 4x4 VTK matrix.
bool IsIndexInside(const mitk::Point3D &index) const
Test whether a point in continuous index coordinates lies inside the bounding box.
void ExecuteOperation(Operation *operation) override
Execute an affine operation (translate, rotate, scale) on this geometry.
void WorldToIndex(const mitk::Point3D &pt_mm, itk::Index< VIndexDimension > &index) const
Convert world coordinates (mm) of a point to discrete (integer) index coordinates.
const mitk::AffineTransform3D * GetIndexToWorldTransform() const
Get the affine transform that maps index coordinates to world coordinates (const).
bool IsInside(const mitk::Point3D &p) const
Test whether a point (world coordinates, mm) lies inside the bounding box.
VnlVector GetOriginVnl() const
Get the origin as a VNL vector.
void InitializeGeometry(Self *newGeometry) const
Copy the essential geometry properties into another instance.
BoundingBoxType::Pointer BoundingBoxPointer
Smart-pointer type for BoundingBoxType.
Point3D GetOrigin() const
Get the origin of the geometry in world coordinates (mm).
virtual void CheckBounds(const BoundsArrayType &)
CheckBounds.
void ItkPhysicalPointToWorld(const itk::Point< TCoordRep, 3 > &itkPhysicalPoint, mitk::Point3D &pt_mm) const
Convert an ITK physical point to MITK world coordinates.
void Translate(const Vector3D &vector)
Translate the origin by a given vector.
virtual bool Is2DConvertable()
Check if this geometry can be converted to 2D without information loss.
mitkClassMacroItkParent(BaseGeometry, itk::Object)
void SetIdentity()
Reset the geometry to identity.
void SetIndexToWorldTransform(mitk::AffineTransform3D *transform)
Set the affine transform for index-to-world coordinate mapping.
void SetIndexToWorldTransformByVtkMatrix(vtkMatrix4x4 *vtkmatrix)
Set the IndexToWorldTransform via a vtkMatrix4x4.
void IndexToWorld(const itk::Index< VIndexDimension > &index, mitk::Point3D &pt_mm) const
Convert discrete (itk::Index) index coordinates of a point to world coordinates (mm).
void WorldToIndex(const mitk::Vector3D &vec_mm, mitk::Vector3D &vec_units) const
Convert world coordinates (mm) of a vector to continuous index coordinates.
itk::BoundingBox< unsigned long, 3, ScalarType > BoundingBoxType
3D bounding box type parameterized with ScalarType.
BoundingBoxType::BoundsArrayType BoundsArrayType
Fixed-size array of six ScalarType values representing axis-aligned bounds [xmin,xmax,...
void WorldToIndex(const mitk::Point3D &pt_mm, mitk::Point3D &pt_units) const
Convert world coordinates (mm) of a point to continuous index coordinates.
void SetSpacing(const mitk::Vector3D &aSpacing, bool enforceSetSpacing=false)
Set the spacing (voxel size) for each axis.
const BoundsArrayType GetBounds() const
Get the bounding box bounds as a BoundsArrayType in index/unit coordinates.
virtual void ChangeImageGeometryConsideringOriginOffset(const bool isAnImageGeometry)
Switch between image geometry and normal geometry with origin correction.
void Compose(const vtkMatrix4x4 *vtkmatrix, bool pre=false)
Compose the IndexToWorldTransform with a vtkMatrix4x4.
void SetIndexToWorldTransformWithoutChangingSpacing(mitk::AffineTransform3D *transform)
Set the IndexToWorldTransform while preserving the current spacing.
vtkLinearTransform * GetVtkTransform() const
Get the IndexToWorldTransform as a vtkLinearTransform.
double GetDiagonalLength2() const
Get the squared length of the bounding-box diagonal in mm.
void SetExtentInMM(int direction, ScalarType extentInMM)
Set the extent of the bounding box in mm along the specified direction.
double GetDiagonalLength() const
Get the length of the bounding-box diagonal in mm.
void SetBounds(const BoundsArrayType &bounds)
Set the bounding box in index/unit coordinates.
void SetIndexToWorldTransformByVtkMatrixWithoutChangingSpacing(vtkMatrix4x4 *vtkmatrix)
Set the IndexToWorldTransform via a vtkMatrix4x4 while preserving spacing.
ScalarType GetExtentInMM(int direction) const
Get the extent of the bounding box in mm along the specified direction.
mitk::AffineTransform3D * GetIndexToWorldTransform()
Get the affine transform that maps index coordinates to world coordinates.
void WorldToItkPhysicalPoint(const mitk::Point3D &pt_mm, itk::Point< TCoordRep, 3 > &itkPhysicalPoint) const
Convert MITK world coordinates to ITK physical coordinates.
Point3D GetCornerPoint(int id) const
Get the position of corner number id in world coordinates.
void Initialize()
Initialize the geometry to default values.
Point3D ClampPoint(const Point3D &point) const
Clamp a point to lie within the geometry's world-coordinate bounds.
ScalarType GetExtent(unsigned int direction) const
Set the time bounds (in ms)
void SetFloatBounds(const float bounds[6])
Set the bounding box via a float array.
const vtkMatrix4x4 * GetVtkMatrix() const
Get the IndexToWorldTransform as a 4x4 VTK matrix (const).
Point3D GetCenter() const
Get the center of the bounding box in world coordinates (mm).
void SetOrigin(const Point3D &origin)
Set the origin of the geometry in world coordinates (mm).
virtual void CheckIndexToWorldTransform(mitk::AffineTransform3D *)
CheckIndexToWorldTransform.
GeometryTransformHolder::TransformType TransformType
Affine transform type used for index-to-world mapping.
Point3D GetCornerPoint(bool xFront=true, bool yFront=true, bool zFront=true) const
Get the position of a corner in world coordinates.
void Modified() const override
Overloaded Modified() to support the ModifiedLock mechanism.
void IndexToWorld(const mitk::Vector3D &vec_units, mitk::Vector3D &vec_mm) const
Convert index coordinates of a vector to world coordinates (mm).
Vector3D GetAxisVector(unsigned int direction) const
Get a vector along the bounding box in the specified direction in mm.
Helper class that manages transform-related variables for geometries.
itk::ScalableAffineTransform< ScalarType, 3 > TransformType
RAII guard that defers BaseGeometry::Modified() calls until destruction.
abstract class, that can be used by Undo to undo an operation.
Base class for all operations in the undo/redo framework.
Definition: mitkOperation.h:38
Convenience header that includes all MITK numeric types.
Find image slices visible on a given plane.
itk::FixedArray< ScalarType, 3 > FixedArrayType
Fixed-size array of three ScalarType values (e.g., for spacing or axis components).
MITKCORE_EXPORT const ScalarType eps
Epsilon value for floating point comparison (vnl_math::eps * 100).
void FillArray(itk::FixedArray< TCoordRep, NVectorDimension > &toArray, const ArrayType &array)
Methods to copy between itk::FixedArray types (like mitk::Vector and mitk::Point) and ArrayTypes.
Definition: mitkArray.h:39
itk::BoundingBox< unsigned long, 3, ScalarType > BoundingBox
Standard 3D bounding box type.
MITKCORE_EXPORT bool IsSubGeometry(const mitk::BaseGeometry &testGeo, const mitk::BaseGeometry &referenceGeo, ScalarType coordinateEps, ScalarType directionEps, bool verbose=false)
A function checks if a test geometry is a sub geometry of a given reference geometry.
itk::ScalableAffineTransform< ScalarType, 3 > AffineTransform3D
3D affine transform type used throughout MITK.
itk::FixedArray< ScalarType, 2 > TimeBounds
Fixed-size array holding a pair of time bound values [min, max] in milliseconds.
vnl_vector< ScalarType > VnlVector
Definition: mitkVector.h:176
double ScalarType
Scalar type used throughout MITK for geometric computations.
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.