ITK  4.2.0
Insight Segmentation and Registration Toolkit
itkDivideOrZeroOutImageFilter.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 __itkDivideOrZeroOutImageFilter_h
00019 #define __itkDivideOrZeroOutImageFilter_h
00020 
00021 #include "itkBinaryFunctorImageFilter.h"
00022 #include "itkNumericTraits.h"
00023 
00024 namespace itk
00025 {
00026 
00027 namespace Functor {
00028 
00029 template< class TNumerator, class TDenominator=TNumerator, class TOutput=TNumerator >
00030 class DivideOrZeroOut
00031 {
00032 public:
00033   DivideOrZeroOut()
00034   {
00035     m_Threshold = 1e-5 * NumericTraits< TDenominator >::OneValue();
00036     m_Constant = NumericTraits< TOutput >::ZeroValue();
00037   };
00038 
00039   ~DivideOrZeroOut() {};
00040 
00041   bool operator!=( const DivideOrZeroOut & other ) const
00042   {
00043     return !(*this == other);
00044   }
00045 
00046   bool operator==( const DivideOrZeroOut & itkNotUsed(other) ) const
00047   {
00048     // Always return true for now.  Do a comparison to m_Threshold if it is
00049     // every made set-able.
00050     return true;
00051   }
00052 
00053   inline TOutput operator()( const TNumerator & n, const TDenominator & d )
00054   {
00055     if ( d < m_Threshold )
00056       {
00057       return m_Constant;
00058       }
00059     return static_cast< TOutput >( n ) / static_cast< TOutput >( d );
00060   }
00061   TDenominator m_Threshold;
00062   TOutput      m_Constant;
00063 };
00064 }  // end namespace functor
00065 
00066 
00077 template< typename TInputImage1,
00078           typename TInputImage2=TInputImage1,
00079           typename TOutputImage=TInputImage1 >
00080 class ITK_EXPORT DivideOrZeroOutImageFilter :
00081   public BinaryFunctorImageFilter< TInputImage1, TInputImage2, TOutputImage,
00082                                    Functor::DivideOrZeroOut<
00083                                      typename TInputImage1::PixelType,
00084                                      typename TInputImage2::PixelType,
00085                                      typename TOutputImage::PixelType > >
00086 {
00087 public:
00089   typedef DivideOrZeroOutImageFilter       Self;
00090   typedef BinaryFunctorImageFilter<TInputImage1, TInputImage2, TOutputImage,
00091     Functor::DivideOrZeroOut<
00092       typename TInputImage1::PixelType,
00093       typename TInputImage2::PixelType,
00094       typename TOutputImage::PixelType > > Superclass;
00095   typedef SmartPointer< Self >             Pointer;
00096   typedef SmartPointer< const Self >       ConstPointer;
00097 
00098   typedef typename TInputImage1::PixelType NumeratorPixelType;
00099   typedef typename TInputImage2::PixelType DenominatorPixelType;
00100   typedef typename TOutputImage::PixelType OutputPixelType;
00101 
00103   itkNewMacro(Self);
00104 
00106   itkTypeMacro(DivideOrZeroOutImageFilter, BinaryFunctorImageFilter);
00107 
00109   void PrintSelf(std::ostream& os, Indent indent) const
00110   {
00111     Superclass::PrintSelf(os, indent);
00112     os << indent << "Threshold: "  << GetThreshold() << std::endl;
00113   }
00115 
00118   void SetThreshold( DenominatorPixelType threshold  )
00119   {
00120     if ( threshold != this->GetFunctor().m_Threshold )
00121       {
00122       this->GetFunctor().m_Threshold = threshold;
00123       this->Modified();
00124       }
00125   }
00126   DenominatorPixelType GetThreshold() const
00127   {
00128     return this->GetFunctor().m_Threshold;
00129   }
00131 
00134   void SetConstant( OutputPixelType constant )
00135   {
00136     if ( constant != this->GetFunctor().m_Constant )
00137       {
00138       this->GetFunctor().m_Constant = constant;
00139       this->Modified();
00140       }
00141   }
00142   OutputPixelType GetConstant() const
00143   {
00144     return this->GetFunctor().m_Constant;
00145   }
00147 
00148 protected:
00149   DivideOrZeroOutImageFilter() {};
00150   virtual ~DivideOrZeroOutImageFilter() {};
00151 
00152 private:
00153   DivideOrZeroOutImageFilter(const Self&); //purposely not implemented
00154   void operator=(const Self&); //purposely not implemented
00155 };
00156 
00157 } // end namespace itk
00158 #endif
00159