ITK
4.1.0
Insight Segmentation and Registration Toolkit
|
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 & other ) const 00047 { 00048 return true; 00049 } 00050 00051 inline TOutput operator()( const TNumerator & n, const TDenominator & d ) 00052 { 00053 if ( d < m_Threshold ) 00054 { 00055 return m_Constant; 00056 } 00057 return static_cast< TOutput >( n ) / static_cast< TOutput >( d ); 00058 } 00059 TDenominator m_Threshold; 00060 TOutput m_Constant; 00061 }; 00062 } // end namespace functor 00063 00064 00075 template< typename TInputImage1, 00076 typename TInputImage2=TInputImage1, 00077 typename TOutputImage=TInputImage1 > 00078 class ITK_EXPORT DivideOrZeroOutImageFilter : 00079 public BinaryFunctorImageFilter< TInputImage1, TInputImage2, TOutputImage, 00080 Functor::DivideOrZeroOut< 00081 typename TInputImage1::PixelType, 00082 typename TInputImage2::PixelType, 00083 typename TOutputImage::PixelType > > 00084 { 00085 public: 00087 typedef DivideOrZeroOutImageFilter Self; 00088 typedef BinaryFunctorImageFilter<TInputImage1, TInputImage2, TOutputImage, 00089 Functor::DivideOrZeroOut< 00090 typename TInputImage1::PixelType, 00091 typename TInputImage2::PixelType, 00092 typename TOutputImage::PixelType > > Superclass; 00093 typedef SmartPointer< Self > Pointer; 00094 typedef SmartPointer< const Self > ConstPointer; 00095 00096 typedef typename TInputImage1::PixelType NumeratorPixelType; 00097 typedef typename TInputImage2::PixelType DenominatorPixelType; 00098 typedef typename TOutputImage::PixelType OutputPixelType; 00099 00101 itkNewMacro(Self); 00102 00104 itkTypeMacro(DivideOrZeroOutImageFilter, BinaryFunctorImageFilter); 00105 00107 void PrintSelf(std::ostream& os, Indent indent) const 00108 { 00109 Superclass::PrintSelf(os, indent); 00110 os << indent << "Threshold: " << GetThreshold() << std::endl; 00111 } 00113 00116 void SetThreshold( DenominatorPixelType threshold ) 00117 { 00118 if ( threshold != this->GetFunctor().m_Threshold ) 00119 { 00120 this->GetFunctor().m_Threshold = threshold; 00121 this->Modified(); 00122 } 00123 } 00124 DenominatorPixelType GetThreshold() const 00125 { 00126 return this->GetFunctor().m_Threshold; 00127 } 00129 00132 void SetConstant( OutputPixelType constant ) 00133 { 00134 if ( constant != this->GetFunctor().m_Constant ) 00135 { 00136 this->GetFunctor().m_Constant = constant; 00137 this->Modified(); 00138 } 00139 } 00140 OutputPixelType GetConstant() const 00141 { 00142 return this->GetFunctor().m_Constant; 00143 } 00145 00146 protected: 00147 DivideOrZeroOutImageFilter() {}; 00148 virtual ~DivideOrZeroOutImageFilter() {}; 00149 00150 private: 00151 DivideOrZeroOutImageFilter(const Self&); //purposely not implemented 00152 void operator=(const Self&); //purposely not implemented 00153 }; 00154 00155 } // end namespace itk 00156 #endif 00157