ITK  4.9.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 #include "itkMath.h"
23 
24 namespace itk
25 {
42 template< unsigned int VSplineOrder = 3, typename TRealValueType = double >
43 class BSplineKernelFunction:public KernelFunctionBase<TRealValueType>
44 {
45 public:
50 
51  typedef typename Superclass::RealType RealType;
53  itkNewMacro(Self);
54 
57 
59  itkStaticConstMacro(SplineOrder, unsigned int, VSplineOrder);
60 
62  inline TRealValueType Evaluate(const TRealValueType & u) const ITK_OVERRIDE
63  {
64  return this->Evaluate(Dispatch< VSplineOrder >(), u);
65  }
66 
67 protected:
70  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE
71  {
72  Superclass::PrintSelf(os, indent);
73  os << indent << "Spline Order: " << SplineOrder << std::endl;
74  }
75 
76 private:
77  BSplineKernelFunction(const Self &) ITK_DELETE_FUNCTION;
78  void operator=(const Self &) ITK_DELETE_FUNCTION;
79 
81  struct DispatchBase {};
82  template< unsigned int >
83  struct Dispatch: public DispatchBase {};
84 
86  inline TRealValueType Evaluate(const Dispatch< 0 > &, const TRealValueType & u) const
87  {
88  const TRealValueType absValue = vnl_math_abs(u);
89  if ( absValue < static_cast< TRealValueType >(0.5) )
90  {
92  }
93  else if ( Math::ExactlyEquals(absValue, static_cast< TRealValueType >(0.5)) )
94  {
95  return static_cast< TRealValueType >(0.5);
96  }
97  else
98  {
100  }
101  }
103 
105  inline TRealValueType Evaluate(const Dispatch< 1 > &, const TRealValueType & u) const
106  {
107  const TRealValueType absValue = vnl_math_abs(u);
109  {
110  return NumericTraits< TRealValueType >::OneValue() - absValue;
111  }
112  else
113  {
115  }
116  }
118 
120  inline TRealValueType Evaluate(const Dispatch< 2 > &, const TRealValueType & u) const
121  {
122  const TRealValueType absValue = vnl_math_abs(u);
123  if ( absValue < static_cast< TRealValueType >(0.5) )
124  {
125  const TRealValueType sqrValue = vnl_math_sqr(absValue);
126  return static_cast< TRealValueType >(0.75) - sqrValue;
127  }
128  else if ( absValue < static_cast< TRealValueType >(1.5) )
129  {
130  const TRealValueType sqrValue = vnl_math_sqr(absValue);
131  // NOTE: 1.0/8.0 == static_cast< TRealValueType >( 0.125 )
132  return ( static_cast< TRealValueType >(9.0) - static_cast< TRealValueType >(12.0) * absValue
133  + static_cast< TRealValueType >(4.0) * sqrValue ) * static_cast< TRealValueType >(0.125);
134  }
135  else
136  {
138  }
139  }
141 
143  inline TRealValueType Evaluate(const Dispatch< 3 > &, const TRealValueType & u) const
144  {
145  const TRealValueType absValue = vnl_math_abs(u);
147  {
148  const TRealValueType sqrValue = vnl_math_sqr(absValue);
149  return ( static_cast< TRealValueType >(4.0) - static_cast< TRealValueType >(6.0) * sqrValue
150  + static_cast< TRealValueType >(3.0) * sqrValue * absValue ) / static_cast< TRealValueType >(6.0);
151  }
152  else if ( absValue < static_cast< TRealValueType >(2.0) )
153  {
154  const TRealValueType sqrValue = vnl_math_sqr(absValue);
155  return ( static_cast< TRealValueType >(8.0) - static_cast< TRealValueType >(12.0) * absValue + static_cast< TRealValueType >(6.0) * sqrValue
156  - sqrValue * absValue ) / static_cast< TRealValueType >(6.0);
157  }
158  else
159  {
161  }
162  }
164 
166  inline TRealValueType Evaluate(const DispatchBase &, const TRealValueType &) const
167  {
168  itkExceptionMacro( "Evaluate not implemented for spline order "
169  << SplineOrder);
170  return NumericTraits< TRealValueType >::ZeroValue(); // This is to avoid compiler warning about missing
171  // return statement. It should never be evaluated.
172  }
173 };
174 } // end namespace itk
176 
177 #endif
void PrintSelf(std::ostream &os, Indent indent) const override
bool ExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Return the result of an exact comparison between two scalar values of potetially different types...
Definition: itkMath.h:665
TRealValueType Evaluate(const Dispatch< 3 > &, const TRealValueType &u) const
Kernel used for density estimation and nonparameteric regression.
TRealValueType Evaluate(const Dispatch< 1 > &, const TRealValueType &u) const
BSpline kernel used for density estimation and nonparameteric regression.
TRealValueType Evaluate(const Dispatch< 0 > &, const TRealValueType &u) const
static const unsigned int SplineOrder
TRealValueType Evaluate(const TRealValueType &u) const override
void PrintSelf(std::ostream &os, Indent indent) const override
Control indentation during Print() invocation.
Definition: itkIndent.h:49
Define additional traits for native types such as int or float.
TRealValueType Evaluate(const DispatchBase &, const TRealValueType &) const
TRealValueType Evaluate(const Dispatch< 2 > &, const TRealValueType &u) const
KernelFunctionBase< TRealValueType > Superclass