ITK  4.13.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 ITK_TEMPLATE_EXPORT 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  TRealValueType Evaluate(const TRealValueType & u) const ITK_OVERRIDE
63  {
64  return this->Evaluate(Dispatch< VSplineOrder >(), u);
65  }
66 
67 protected:
69  virtual ~BSplineKernelFunction() ITK_OVERRIDE {}
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  ITK_DISALLOW_COPY_AND_ASSIGN(BSplineKernelFunction);
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 = itk::Math::abs(u);
88  if ( absValue < static_cast< TRealValueType >(0.5) )
89  {
91  }
92  else if ( Math::ExactlyEquals(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 = itk::Math::abs(u);
108  {
109  return NumericTraits< TRealValueType >::OneValue() - absValue;
110  }
111  else
112  {
114  }
115  }
117 
119  inline TRealValueType Evaluate(const Dispatch< 2 > &, const TRealValueType & u) const
120  {
121  const TRealValueType absValue = itk::Math::abs(u);
122  if ( absValue < static_cast< TRealValueType >(0.5) )
123  {
124  const TRealValueType sqrValue = itk::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 = itk::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 = itk::Math::abs(u);
146  {
147  const TRealValueType sqrValue = itk::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 = itk::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 >::ZeroValue(); // 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
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:710
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.
virtual ~BSplineKernelFunction() override
TRealValueType Evaluate(const Dispatch< 0 > &, const TRealValueType &u) const
bool sqr(const bool x)
Definition: itkMath.h:824
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
bool abs(const bool x)
Definition: itkMath.h:806
KernelFunctionBase< TRealValueType > Superclass