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 #ifndef itkImageSpatialObject_h
19 #define itkImageSpatialObject_h
20 
21 #include "itkImage.h"
22 #include "itkContinuousIndex.h"
23 #include "itkSpatialObject.h"
24 #include "itkNearestNeighborInterpolateImageFunction.h"
25 
26 namespace itk
27 {
28 /** \class ImageSpatialObject
29  * \brief Implementation of an image as spatial object.
30  *
31  * This class combines functionnalities from a spatial object,
32  * and an image.
33  *
34  * \sa SpatialObject CompositeSpatialObject
35  * \ingroup ITKSpatialObjects
36  */
37 
38 template< unsigned int TDimension = 3,
39           typename TPixelType = unsigned char
40           >
41 class ITK_TEMPLATE_EXPORT ImageSpatialObject:
42   public SpatialObject< TDimension >
43 {
44 public:
45   ITK_DISALLOW_COPY_AND_ASSIGN(ImageSpatialObject);
46 
47   using ScalarType = double;
48   using Self = ImageSpatialObject< TDimension, TPixelType >;
49   using Superclass = SpatialObject< TDimension >;
50   using Pointer = SmartPointer< Self >;
51   using ConstPointer = SmartPointer< const Self >;
52 
53   using ContinuousIndexType = ContinuousIndex< double, TDimension >;
54   using PixelType = TPixelType;
55   using ImageType = Image< PixelType, TDimension >;
56   using ImagePointer = typename ImageType::ConstPointer;
57   using IndexType = typename ImageType::IndexType;
58   using PointType = typename Superclass::PointType;
59   using InterpolatorType = InterpolateImageFunction< ImageType >;
60 
61   using NNInterpolatorType = NearestNeighborInterpolateImageFunction<ImageType>;
62 
63   static constexpr unsigned int ObjectDimension = TDimension;
64 
65   /** Method for creation through the object factory. */
66   itkNewMacro(Self);
67 
68   /** Run-time type information (and related methods). */
69   itkTypeMacro(ImageSpatialObject, SpatialObject);
70 
71   /** Reset the spatial object to its initial condition, yet preserves
72    *   Id, Parent, and Child information */
73   void Clear( void ) override;
74 
75   /** Set the image. */
76   void SetImage(const ImageType *image);
77 
78   /** Get a pointer to the image currently attached to the object. */
79   const ImageType * GetImage() const;
80 
81   /** Returns true if the point is inside, false otherwise. */
82   bool IsInsideInObjectSpace(const PointType & point) const override;
83 
84   /* Avoid hiding the overload that supports depth and name arguments */
85   using Superclass::IsInsideInObjectSpace;
86 
87   /** Returns the value of the image at the requested point.
88    *  Returns true if that value is valid */
89   bool ValueAtInObjectSpace(const PointType & point, double & value,
90     unsigned int depth = 0, const std::string & name = "") const override;
91 
92   /** Returns the latest modified time of the object and its component. */
93   ModifiedTimeType GetMTime() const override;
94 
95   /** Set the slice position */
96   itkSetMacro( SliceNumber, IndexType );
97   void SetSliceNumber(unsigned int dimension, int position);
98 
99   /** Get the slice position */
100   itkGetConstMacro( SliceNumber, IndexType );
GetSliceNumber(unsigned int dimension)101   int GetSliceNumber(unsigned int dimension)
102   { return m_SliceNumber[dimension]; }
103 
104 #if !defined(ITK_LEGACY_REMOVE)
itkLegacyMacro(const char * GetPixelTypeName ())105   itkLegacyMacro(const char * GetPixelTypeName())
106   { return m_PixelType.c_str(); }
107 #endif
108 
109   /** Set/Get the interpolator */
110   void SetInterpolator(InterpolatorType *interpolator);
111   itkGetConstMacro(Interpolator, InterpolatorType *);
112 
113 protected:
114   /** Compute the boundaries of the image spatial object. */
115   void ComputeMyBoundingBox() override;
116 
117   ImageSpatialObject();
118   ~ImageSpatialObject() override;
119 
120   void PrintSelf(std::ostream & os, Indent indent) const override;
121 
122   typename LightObject::Pointer InternalClone() const override;
123 
124 private:
125 
126   ImagePointer    m_Image;
127 
128   IndexType       m_SliceNumber;
129 
130 #if !defined(ITK_LEGACY_REMOVE)
131   std::string     m_PixelType;
132 #endif
133 
134   typename InterpolatorType::Pointer m_Interpolator;
135 
136 #if !defined(ITK_LEGACY_REMOVE)
137   template <typename T>
SetPixelTypeName(const T *)138   void SetPixelTypeName(const T *)
139   { itkWarningMacro("itk::ImageSpatialObject() : PixelType not recognized"); }
140 
SetPixelTypeName(const short *)141   void SetPixelTypeName(const short *)
142   { m_PixelType = "short"; }
143 
SetPixelTypeName(const unsigned char *)144   void SetPixelTypeName(const unsigned char *)
145   { m_PixelType = "unsigned char"; }
146 
SetPixelTypeName(const unsigned short *)147   void SetPixelTypeName(const unsigned short *)
148   { m_PixelType = "unsigned short"; }
149 
SetPixelTypeName(const float *)150   void SetPixelTypeName(const float *)
151   { m_PixelType = "float"; }
152 
SetPixelTypeName(const double *)153   void SetPixelTypeName(const double *)
154   { m_PixelType = "double"; }
155 #endif
156 
157 };
158 } // end of namespace itk
159 
160 #ifndef ITK_MANUAL_INSTANTIATION
161 #include "itkImageSpatialObject.hxx"
162 #endif
163 
164 #endif //itkImageSpatialObject_h
165