ITK  4.2.0
Insight Segmentation and Registration Toolkit
itkBSplineKernelFunction.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkBSplineKernelFunction_h
19 #define __itkBSplineKernelFunction_h
20 
21 #include "itkKernelFunctionBase.h"
22 
23 namespace itk
24 {
41 template< unsigned int VSplineOrder = 3, typename TRealValueType = double >
42 class ITK_EXPORT BSplineKernelFunction:public KernelFunctionBase<TRealValueType>
43 {
44 public:
49 
50  typedef typename Superclass::RealType RealType;
52  itkNewMacro(Self);
53 
56 
58  itkStaticConstMacro(SplineOrder, unsigned int, VSplineOrder);
59 
61  inline TRealValueType Evaluate(const TRealValueType & u) const
62  {
63  return this->Evaluate(Dispatch< VSplineOrder >(), u);
64  }
65 
66 protected:
69  void PrintSelf(std::ostream & os, Indent indent) const
70  {
71  Superclass::PrintSelf(os, indent);
72  os << indent << "Spline Order: " << SplineOrder << std::endl;
73  }
74 
75 private:
76  BSplineKernelFunction(const Self &); //purposely not implemented
77  void operator=(const Self &); //purposely not implemented
78 
80  struct DispatchBase {};
81  template< unsigned int >
82  struct Dispatch: public DispatchBase {};
83 
85  inline TRealValueType Evaluate(const Dispatch< 0 > &, const TRealValueType & u) const
86  {
87  const TRealValueType absValue = vnl_math_abs(u);
88  if ( absValue < static_cast< TRealValueType >(0.5) )
89  {
91  }
92  else if ( absValue == static_cast< TRealValueType >(0.5) )
93  {
94  return static_cast< TRealValueType >(0.5);
95  }
96  else
97  {
99  }
100  }
102 
104  inline TRealValueType Evaluate(const Dispatch< 1 > &, const TRealValueType & u) const
105  {
106  const TRealValueType absValue = vnl_math_abs(u);
107  if ( absValue < NumericTraits< TRealValueType >::One )
108  {
109  return NumericTraits< TRealValueType >::One - absValue;
110  }
111  else
112  {
114  }
115  }
117 
119  inline TRealValueType Evaluate(const Dispatch< 2 > &, const TRealValueType & u) const
120  {
121  const TRealValueType absValue = vnl_math_abs(u);
122  if ( absValue < static_cast< TRealValueType >(0.5) )
123  {
124  const TRealValueType sqrValue = vnl_math_sqr(absValue);
125  return static_cast< TRealValueType >(0.75) - sqrValue;
126  }
127  else if ( absValue < static_cast< TRealValueType >(1.5) )
128  {
129  const TRealValueType sqrValue = vnl_math_sqr(absValue);
130  // NOTE: 1.0/8.0 == static_cast< TRealValueType >( 0.125 )
131  return ( static_cast< TRealValueType >(9.0) - static_cast< TRealValueType >(12.0) * absValue
132  + static_cast< TRealValueType >(4.0) * sqrValue ) * static_cast< TRealValueType >(0.125);
133  }
134  else
135  {
137  }
138  }
140 
142  inline TRealValueType Evaluate(const Dispatch< 3 > &, const TRealValueType & u) const
143  {
144  const TRealValueType absValue = vnl_math_abs(u);
145  if ( absValue < NumericTraits< TRealValueType >::One )
146  {
147  const TRealValueType sqrValue = vnl_math_sqr(absValue);
148  return ( static_cast< TRealValueType >(4.0) - static_cast< TRealValueType >(6.0) * sqrValue
149  + static_cast< TRealValueType >(3.0) * sqrValue * absValue ) / static_cast< TRealValueType >(6.0);
150  }
151  else if ( absValue < static_cast< TRealValueType >(2.0) )
152  {
153  const TRealValueType sqrValue = vnl_math_sqr(absValue);
154  return ( static_cast< TRealValueType >(8.0) - static_cast< TRealValueType >(12.0) * absValue + static_cast< TRealValueType >(6.0) * sqrValue
155  - sqrValue * absValue ) / static_cast< TRealValueType >(6.0);
156  }
157  else
158  {
160  }
161  }
163 
165  inline TRealValueType Evaluate(const DispatchBase &, const TRealValueType &) const
166  {
167  itkExceptionMacro( "Evaluate not implemented for spline order "
168  << SplineOrder);
169  return NumericTraits< TRealValueType >::Zero; // This is to avoid compiler warning about missing
170  // return statement. It should never be evaluated.
171  }
172 };
173 } // end namespace itk
175 
176 #endif
177