ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkBSplineKernelFunction.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 __itkBSplineKernelFunction_h
00019 #define __itkBSplineKernelFunction_h
00020 
00021 #include "itkKernelFunctionBase.h"
00022 
00023 namespace itk
00024 {
00041 template< unsigned int VSplineOrder = 3, typename TRealValueType = double >
00042 class ITK_EXPORT BSplineKernelFunction:public KernelFunctionBase<TRealValueType>
00043 {
00044 public:
00046   typedef BSplineKernelFunction              Self;
00047   typedef KernelFunctionBase<TRealValueType> Superclass;
00048   typedef SmartPointer< Self >               Pointer;
00049 
00050   typedef typename Superclass::RealType  RealType;
00052   itkNewMacro(Self);
00053 
00055   itkTypeMacro(BSplineKernelFunction, KernelFunctionBase);
00056 
00058   itkStaticConstMacro(SplineOrder, unsigned int, VSplineOrder);
00059 
00061   inline TRealValueType Evaluate(const TRealValueType & u) const
00062   {
00063     return this->Evaluate(Dispatch< VSplineOrder >(), u);
00064   }
00065 
00066 protected:
00067   BSplineKernelFunction(){}
00068   virtual ~BSplineKernelFunction(){}
00069   void PrintSelf(std::ostream & os, Indent indent) const
00070   {
00071     Superclass::PrintSelf(os, indent);
00072     os << indent  << "Spline Order: " << SplineOrder << std::endl;
00073   }
00074 
00075 private:
00076   BSplineKernelFunction(const Self &); //purposely not implemented
00077   void operator=(const Self &);        //purposely not implemented
00078 
00080   struct DispatchBase {};
00081   template< unsigned int >
00082   struct Dispatch: public DispatchBase {};
00083 
00085   inline TRealValueType Evaluate(const Dispatch< 0 > &, const TRealValueType & u) const
00086   {
00087     const TRealValueType absValue = vnl_math_abs(u);
00088     if ( absValue  < static_cast< TRealValueType >(0.5) )
00089       {
00090       return NumericTraits< TRealValueType >::One;
00091       }
00092     else if ( absValue == static_cast< TRealValueType >(0.5) )
00093       {
00094       return static_cast< TRealValueType >(0.5);
00095       }
00096     else
00097       {
00098       return NumericTraits< TRealValueType >::Zero;
00099       }
00100   }
00102 
00104   inline TRealValueType Evaluate(const Dispatch< 1 > &, const TRealValueType & u) const
00105   {
00106     const TRealValueType absValue = vnl_math_abs(u);
00107     if ( absValue  < NumericTraits< TRealValueType >::One )
00108       {
00109       return NumericTraits< TRealValueType >::One - absValue;
00110       }
00111     else
00112       {
00113       return NumericTraits< TRealValueType >::Zero;
00114       }
00115   }
00117 
00119   inline TRealValueType Evaluate(const Dispatch< 2 > &, const TRealValueType & u) const
00120   {
00121     const TRealValueType absValue = vnl_math_abs(u);
00122     if ( absValue  < static_cast< TRealValueType >(0.5) )
00123       {
00124       const TRealValueType sqrValue = vnl_math_sqr(absValue);
00125       return static_cast< TRealValueType >(0.75) - sqrValue;
00126       }
00127     else if ( absValue < static_cast< TRealValueType >(1.5) )
00128       {
00129       const TRealValueType sqrValue = vnl_math_sqr(absValue);
00130       // NOTE: 1.0/8.0 == static_cast< TRealValueType >( 0.125 )
00131       return ( static_cast< TRealValueType >(9.0) - static_cast< TRealValueType >(12.0) * absValue
00132         + static_cast< TRealValueType >(4.0) * sqrValue ) * static_cast< TRealValueType >(0.125);
00133       }
00134     else
00135       {
00136       return NumericTraits< TRealValueType >::Zero;
00137       }
00138   }
00140 
00142   inline TRealValueType Evaluate(const Dispatch< 3 > &, const TRealValueType & u) const
00143   {
00144     const TRealValueType absValue = vnl_math_abs(u);
00145     if ( absValue  < NumericTraits< TRealValueType >::One )
00146       {
00147       const TRealValueType sqrValue = vnl_math_sqr(absValue);
00148       return ( static_cast< TRealValueType >(4.0) - static_cast< TRealValueType >(6.0) * sqrValue
00149         + static_cast< TRealValueType >(3.0) * sqrValue * absValue ) / static_cast< TRealValueType >(6.0);
00150       }
00151     else if ( absValue < static_cast< TRealValueType >(2.0) )
00152       {
00153       const TRealValueType sqrValue = vnl_math_sqr(absValue);
00154       return ( static_cast< TRealValueType >(8.0) - static_cast< TRealValueType >(12.0) * absValue + static_cast< TRealValueType >(6.0) * sqrValue
00155                - sqrValue * absValue ) / static_cast< TRealValueType >(6.0);
00156       }
00157     else
00158       {
00159       return NumericTraits< TRealValueType >::Zero;
00160       }
00161   }
00163 
00165   inline TRealValueType Evaluate(const DispatchBase &, const TRealValueType &) const
00166   {
00167     itkExceptionMacro( "Evaluate not implemented for spline order "
00168       << SplineOrder);
00169     return NumericTraits< TRealValueType >::Zero; // This is to avoid compiler warning about missing
00170                 // return statement.  It should never be evaluated.
00171   }
00172 };
00173 } // end namespace itk
00175 
00176 #endif
00177