ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkImageFunction.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkImageFunction_h
00019 #define __itkImageFunction_h
00020 
00021 #include "itkFunctionBase.h"
00022 #include "itkIndex.h"
00023 #include "itkImageBase.h"
00024 
00025 namespace itk
00026 {
00053 template<
00054   class TInputImage,
00055   class TOutput,
00056   class TCoordRep = float
00057   >
00058 class ITK_EXPORT ImageFunction:
00059   public FunctionBase< Point< TCoordRep, TInputImage::ImageDimension >, TOutput >
00060 {
00061 public:
00063   itkStaticConstMacro(ImageDimension, unsigned int,
00064                       TInputImage::ImageDimension);
00065 
00067   typedef ImageFunction Self;
00068   typedef FunctionBase<
00069     Point< TCoordRep, itkGetStaticConstMacro(ImageDimension) >,
00070     TOutput >                                                   Superclass;
00071   typedef SmartPointer< Self >       Pointer;
00072   typedef SmartPointer< const Self > ConstPointer;
00073 
00075   itkTypeMacro(ImageFunction, FunctionBase);
00076 
00078   typedef TInputImage InputImageType;
00079 
00081   typedef typename InputImageType::PixelType InputPixelType;
00082 
00084   typedef typename InputImageType::ConstPointer InputImageConstPointer;
00085 
00087   typedef TOutput OutputType;
00088 
00090   typedef TCoordRep CoordRepType;
00091 
00093   typedef typename InputImageType::IndexType      IndexType;
00094   typedef typename InputImageType::IndexValueType IndexValueType;
00095 
00097   typedef ContinuousIndex< TCoordRep, itkGetStaticConstMacro(ImageDimension) >
00098   ContinuousIndexType;
00099 
00101   typedef Point< TCoordRep, itkGetStaticConstMacro(ImageDimension) > PointType;
00102 
00107   virtual void SetInputImage(const InputImageType *ptr);
00108 
00110   const InputImageType * GetInputImage() const
00111   { return m_Image.GetPointer(); }
00112 
00115   virtual TOutput Evaluate(const PointType & point) const = 0;
00116 
00119   virtual TOutput EvaluateAtIndex(const IndexType & index) const = 0;
00120 
00123   virtual TOutput EvaluateAtContinuousIndex(
00124     const ContinuousIndexType & index) const = 0;
00125 
00132   virtual bool IsInsideBuffer(const IndexType & index) const
00133   {
00134     for ( unsigned int j = 0; j < ImageDimension; j++ )
00135       {
00136       if ( index[j] < m_StartIndex[j] )
00137         {
00138         return false;
00139         }
00140       if ( index[j] > m_EndIndex[j] )
00141         {
00142         return false;
00143         }
00144       }
00145     return true;
00146   }
00148 
00152   virtual bool IsInsideBuffer(const ContinuousIndexType & index) const
00153   {
00154     for ( unsigned int j = 0; j < ImageDimension; j++ )
00155       {
00156       /* Test for negative of a positive so we can catch NaN's. */
00157       if ( ! (index[j] >= m_StartContinuousIndex[j] &&
00158              index[j] < m_EndContinuousIndex[j] ) )
00159         {
00160         return false;
00161         }
00162       }
00163     return true;
00164   }
00166 
00170   virtual bool IsInsideBuffer(const PointType & point) const
00171   {
00172     ContinuousIndexType index;
00173     m_Image->TransformPhysicalPointToContinuousIndex(point, index);
00174     /* Call IsInsideBuffer to test against BufferedRegion bounds.
00175      * TransformPhysicalPointToContinuousIndex tests against
00176      * LargestPossibleRegion */
00177     bool isInside = IsInsideBuffer( index );
00178     return isInside;
00179   }
00181 
00183   void ConvertPointToNearestIndex(const PointType & point,
00184                                   IndexType & index) const
00185   {
00186     ContinuousIndexType cindex;
00187 
00188     m_Image->TransformPhysicalPointToContinuousIndex(point, cindex);
00189     this->ConvertContinuousIndexToNearestIndex(cindex, index);
00190   }
00191 
00193   void ConvertPointToContinuousIndex(const PointType & point,
00194                                      ContinuousIndexType & cindex) const
00195   {
00196     m_Image->TransformPhysicalPointToContinuousIndex(point, cindex);
00197   }
00198 
00200   inline void ConvertContinuousIndexToNearestIndex(
00201                                   const ContinuousIndexType & cindex,
00202                                   IndexType &                 index) const
00203   {
00204     index.CopyWithRound(cindex);
00205   }
00206 
00207   itkGetConstReferenceMacro(StartIndex, IndexType);
00208   itkGetConstReferenceMacro(EndIndex, IndexType);
00209 
00210   itkGetConstReferenceMacro(StartContinuousIndex, ContinuousIndexType);
00211   itkGetConstReferenceMacro(EndContinuousIndex, ContinuousIndexType);
00212 protected:
00213   ImageFunction();
00214   ~ImageFunction() {}
00215   void PrintSelf(std::ostream & os, Indent indent) const;
00216 
00218   InputImageConstPointer m_Image;
00219 
00221   IndexType m_StartIndex;
00222   IndexType m_EndIndex;
00223 
00224   ContinuousIndexType m_StartContinuousIndex;
00225   ContinuousIndexType m_EndContinuousIndex;
00226 private:
00227   ImageFunction(const Self &);  //purposely not implemented
00228   void operator=(const Self &); //purposely not implemented
00229 };
00230 } // end namespace itk
00231 
00232 // Define instantiation macro for this template.
00233 #define ITK_TEMPLATE_ImageFunction(_, EXPORT, TypeX, TypeY)             \
00234   namespace itk                                                         \
00235   {                                                                     \
00236   _( 3 ( class EXPORT ImageFunction< ITK_TEMPLATE_3 TypeX > ) )         \
00237   namespace Templates                                                   \
00238   {                                                                     \
00239   typedef ImageFunction< ITK_TEMPLATE_3 TypeX > ImageFunction##TypeY; \
00240   }                                                                     \
00241   }
00242 
00243 #if ITK_TEMPLATE_EXPLICIT
00244 #include "Templates/itkImageFunction+-.h"
00245 #endif
00246 
00247 #if ITK_TEMPLATE_TXX
00248 #include "itkImageFunction.hxx"
00249 #endif
00250 
00251 #endif
00252