ITK  5.2.0
Insight Toolkit
itkBSplineKernelFunction.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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:
46  ITK_DISALLOW_COPY_AND_MOVE(BSplineKernelFunction);
47 
52 
53  using RealType = typename Superclass::RealType;
55  itkNewMacro(Self);
56 
59 
61  static constexpr unsigned int SplineOrder = VSplineOrder;
62 
64  TRealValueType
65  Evaluate(const TRealValueType & u) const override
66  {
67  return this->Evaluate(Dispatch<VSplineOrder>(), u);
68  }
69 
70 protected:
71  BSplineKernelFunction() = default;
72  ~BSplineKernelFunction() override = default;
73  void
74  PrintSelf(std::ostream & os, Indent indent) const override
75  {
76  Superclass::PrintSelf(os, indent);
77  os << indent << "Spline Order: " << SplineOrder << std::endl;
78  }
79 
80 private:
82  struct DispatchBase
83  {};
84  template <unsigned int>
85  struct Dispatch : public DispatchBase
86  {};
87 
89  inline TRealValueType
90  Evaluate(const Dispatch<0> &, const TRealValueType & u) const
91  {
92  const TRealValueType absValue = itk::Math::abs(u);
93  if (absValue < static_cast<TRealValueType>(0.5))
94  {
96  }
97  else if (Math::ExactlyEquals(absValue, static_cast<TRealValueType>(0.5)))
98  {
99  return static_cast<TRealValueType>(0.5);
100  }
101  else
102  {
104  }
105  }
107 
109  inline TRealValueType
110  Evaluate(const Dispatch<1> &, const TRealValueType & u) const
111  {
112  const TRealValueType absValue = itk::Math::abs(u);
114  {
115  return NumericTraits<TRealValueType>::OneValue() - absValue;
116  }
117  else
118  {
120  }
121  }
123 
125  inline TRealValueType
126  Evaluate(const Dispatch<2> &, const TRealValueType & u) const
127  {
128  const TRealValueType absValue = itk::Math::abs(u);
129  if (absValue < static_cast<TRealValueType>(0.5))
130  {
131  const TRealValueType sqrValue = itk::Math::sqr(absValue);
132  return static_cast<TRealValueType>(0.75) - sqrValue;
133  }
134  else if (absValue < static_cast<TRealValueType>(1.5))
135  {
136  const TRealValueType sqrValue = itk::Math::sqr(absValue);
137  // NOTE: 1.0/8.0 == static_cast< TRealValueType >( 0.125 )
138  return (static_cast<TRealValueType>(9.0) - static_cast<TRealValueType>(12.0) * absValue +
139  static_cast<TRealValueType>(4.0) * sqrValue) *
140  static_cast<TRealValueType>(0.125);
141  }
142  else
143  {
145  }
146  }
148 
150  inline TRealValueType
151  Evaluate(const Dispatch<3> &, const TRealValueType & u) const
152  {
153  const TRealValueType absValue = itk::Math::abs(u);
155  {
156  const TRealValueType sqrValue = itk::Math::sqr(absValue);
157  return (static_cast<TRealValueType>(4.0) - static_cast<TRealValueType>(6.0) * sqrValue +
158  static_cast<TRealValueType>(3.0) * sqrValue * absValue) /
159  static_cast<TRealValueType>(6.0);
160  }
161  else if (absValue < static_cast<TRealValueType>(2.0))
162  {
163  const TRealValueType sqrValue = itk::Math::sqr(absValue);
164  return (static_cast<TRealValueType>(8.0) - static_cast<TRealValueType>(12.0) * absValue +
165  static_cast<TRealValueType>(6.0) * sqrValue - sqrValue * absValue) /
166  static_cast<TRealValueType>(6.0);
167  }
168  else
169  {
171  }
172  }
174 
176  inline TRealValueType
177  Evaluate(const DispatchBase &, const TRealValueType &) const
178  {
179  itkExceptionMacro("Evaluate not implemented for spline order " << SplineOrder);
180  }
181 };
182 } // end namespace itk
184 
185 #endif
itk::BSplineKernelFunction
BSpline kernel used for density estimation and nonparametric regression.
Definition: itkBSplineKernelFunction.h:43
itk::KernelFunctionBase::RealType
TRealValueType RealType
Definition: itkKernelFunctionBase.h:52
itk::Math::ExactlyEquals
bool ExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Return the result of an exact comparison between two scalar values of potentially different types.
Definition: itkMath.h:723
itk::KernelFunctionBase
Kernel used for density estimation and nonparametric regression.
Definition: itkKernelFunctionBase.h:43
itk::BSplineKernelFunction::Evaluate
TRealValueType Evaluate(const DispatchBase &, const TRealValueType &) const
Definition: itkBSplineKernelFunction.h:177
itkKernelFunctionBase.h
itk::BSplineKernelFunction::Evaluate
TRealValueType Evaluate(const Dispatch< 1 > &, const TRealValueType &u) const
Definition: itkBSplineKernelFunction.h:110
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::BSplineKernelFunction::Evaluate
TRealValueType Evaluate(const Dispatch< 0 > &, const TRealValueType &u) const
Definition: itkBSplineKernelFunction.h:90
itk::BSplineKernelFunction::Dispatch
Definition: itkBSplineKernelFunction.h:85
itk::BSplineKernelFunction::Evaluate
TRealValueType Evaluate(const TRealValueType &u) const override
Definition: itkBSplineKernelFunction.h:65
itk::NumericTraits::OneValue
static T OneValue()
Definition: itkNumericTraits.h:156
itk::BSplineKernelFunction::PrintSelf
void PrintSelf(std::ostream &os, Indent indent) const override
Definition: itkBSplineKernelFunction.h:74
itk::BSplineKernelFunction::Evaluate
TRealValueType Evaluate(const Dispatch< 3 > &, const TRealValueType &u) const
Definition: itkBSplineKernelFunction.h:151
itk::BSplineKernelFunction::Evaluate
TRealValueType Evaluate(const Dispatch< 2 > &, const TRealValueType &u) const
Definition: itkBSplineKernelFunction.h:126
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::NumericTraits::ZeroValue
static T ZeroValue()
Definition: itkNumericTraits.h:148
itk::BSplineKernelFunction::DispatchBase
Definition: itkBSplineKernelFunction.h:82
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itkMath.h
itk::BSplineKernelFunction::RealType
typename Superclass::RealType RealType
Definition: itkBSplineKernelFunction.h:53