ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkBSplineDerivativeKernelFunction.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 __itkBSplineDerivativeKernelFunction_h
00019 #define __itkBSplineDerivativeKernelFunction_h
00020 
00021 #include "itkBSplineKernelFunction.h"
00022 
00023 namespace itk
00024 {
00041 template< unsigned int VSplineOrder = 3, typename TRealValueType = double >
00042 class ITK_EXPORT BSplineDerivativeKernelFunction:public KernelFunctionBase<TRealValueType>
00043 {
00044 public:
00046   typedef BSplineDerivativeKernelFunction    Self;
00047   typedef KernelFunctionBase<TRealValueType> Superclass;
00048   typedef SmartPointer< Self >               Pointer;
00049 
00050   typedef typename Superclass::RealType  RealType;
00052   itkNewMacro(Self);
00053 
00055   itkTypeMacro(BSplineDerivativeKernelFunction, 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   BSplineDerivativeKernelFunction() {}
00068   virtual ~BSplineDerivativeKernelFunction(){}
00069 
00070   void PrintSelf(std::ostream & os, Indent indent) const
00071     {
00072     Superclass::PrintSelf(os, indent);
00073     os << indent  << "Spline Order: " << SplineOrder << std::endl;
00074     }
00075 
00076 private:
00077   BSplineDerivativeKernelFunction(const Self &); //purposely not implemented
00078   void operator=(const Self &);                  //purposely not implemented
00079 
00081   struct DispatchBase {};
00082   template< unsigned int >
00083   struct Dispatch: public DispatchBase {};
00084 
00086   inline TRealValueType Evaluate( const Dispatch<0>&, const TRealValueType & itkNotUsed( u ) )
00087     const
00088     {
00089     return NumericTraits< TRealValueType >::Zero;
00090     }
00091 
00093   inline TRealValueType Evaluate( const Dispatch<1>&, const TRealValueType& u ) const
00094     {
00095     if( u == -NumericTraits< TRealValueType >::One )
00096       {
00097       return static_cast< TRealValueType >(0.5);
00098       }
00099     else if( ( u > -NumericTraits< TRealValueType >::One ) && ( u < NumericTraits< TRealValueType >::Zero ) )
00100       {
00101       return NumericTraits< TRealValueType >::One;
00102       }
00103     else if( u == NumericTraits< TRealValueType >::Zero )
00104       {
00105       return NumericTraits< TRealValueType >::Zero;
00106       }
00107     else if( ( u > NumericTraits< TRealValueType >::Zero ) && ( u < NumericTraits< TRealValueType >::One ) )
00108       {
00109       return -NumericTraits< TRealValueType >::One;
00110       }
00111     else if( u == NumericTraits< TRealValueType >::One )
00112       {
00113       return static_cast< TRealValueType >(-0.5);
00114       }
00115     else
00116       {
00117       return NumericTraits< TRealValueType >::Zero;
00118       }
00119     }
00121 
00123   inline TRealValueType Evaluate( const Dispatch<2>&, const TRealValueType& u) const
00124     {
00125     if( ( u > static_cast< TRealValueType >(-0.5) ) && ( u < static_cast< TRealValueType >(0.5) ) )
00126       {
00127       return ( static_cast< TRealValueType >(-2.0) * u );
00128       }
00129     else if( ( u >= static_cast< TRealValueType >(0.5) ) && ( u < static_cast< TRealValueType >(1.5) ) )
00130       {
00131       return ( static_cast< TRealValueType >(-1.5) + u );
00132       }
00133     else if( ( u > static_cast< TRealValueType >(-1.5) ) && ( u <= static_cast< TRealValueType >(-0.5) ) )
00134       {
00135       return ( static_cast< TRealValueType >(1.5) + u );
00136       }
00137     else
00138       {
00139       return NumericTraits< TRealValueType >::Zero;
00140       }
00141     }
00143 
00145   inline TRealValueType Evaluate( const Dispatch<3>&, const TRealValueType& u ) const
00146     {
00147     if( ( u >= NumericTraits< TRealValueType >::Zero ) && ( u < NumericTraits< TRealValueType >::One ) )
00148       {
00149       return ( static_cast< TRealValueType >(-2.0)* u + static_cast< TRealValueType >(1.5) * u * u );
00150       }
00151     else if( ( u > -NumericTraits< TRealValueType >::One ) && ( u < NumericTraits< TRealValueType >::Zero ) )
00152       {
00153       return ( static_cast< TRealValueType >(-2.0) * u - static_cast< TRealValueType >(1.5) * u * u );
00154       }
00155     else if( ( u >= NumericTraits< TRealValueType >::One ) && ( u < static_cast< TRealValueType >(2.0) ) )
00156       {
00157       return ( static_cast< TRealValueType >(-2.0) + static_cast< TRealValueType >(2.0) * u - static_cast< TRealValueType >(0.5) * u * u );
00158       }
00159     else if( ( u > static_cast< TRealValueType >(-2.0) ) && ( u <= -NumericTraits< TRealValueType >::One ) )
00160       {
00161       return ( static_cast< TRealValueType >(2.0) + static_cast< TRealValueType >(2.0) * u + static_cast< TRealValueType >(0.5) * u * u );
00162       }
00163     else
00164       {
00165       return NumericTraits< TRealValueType >::Zero;
00166       }
00167     }
00169 
00171   inline TRealValueType Evaluate( const DispatchBase&, const TRealValueType& ) const
00172     {
00173     itkExceptionMacro( "Evaluate not implemented for spline order " << SplineOrder );
00174     return NumericTraits< TRealValueType >::Zero; // This is to avoid compiler warning about missing
00175     // return statement. It should never be evaluated.
00176     }
00177 };
00178 } // end namespace itk
00180 
00181 #endif
00182