ITK  5.4.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  * https://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 typename Superclass::RealType;
55  itkNewMacro(Self);
56 
58  itkOverrideGetNameOfClassMacro(BSplineKernelFunction);
59 
61  static constexpr unsigned int SplineOrder = VSplineOrder;
62 
65  static TRealValueType
66  FastEvaluate(const TRealValueType u)
67  {
68  return Self::Evaluate(Dispatch<VSplineOrder>(), u);
69  }
70 
72  TRealValueType
73  Evaluate(const TRealValueType & u) const override
74  {
75  return Self::FastEvaluate(u);
76  }
77 
78 protected:
79  BSplineKernelFunction() = default;
80  ~BSplineKernelFunction() override = default;
81  void
82  PrintSelf(std::ostream & os, Indent indent) const override
83  {
84  Superclass::PrintSelf(os, indent);
85 
86  os << indent << "SplineOrder: " << SplineOrder << std::endl;
87  }
88 
89 private:
91  struct DispatchBase
92  {};
93  template <unsigned int>
94  struct Dispatch : public DispatchBase
95  {};
96 
98  inline static TRealValueType
99  Evaluate(const Dispatch<0> &, const TRealValueType & u)
100  {
101  const TRealValueType absValue = itk::Math::abs(u);
102  if (absValue < TRealValueType{ 0.5 })
103  {
104  return TRealValueType{ 1.0 };
105  }
106  else if (Math::ExactlyEquals(absValue, TRealValueType{ 0.5 }))
107  {
108  return TRealValueType{ 0.5 };
109  }
110  else
111  {
112  return TRealValueType{ 0.0 };
113  }
114  }
118  inline static TRealValueType
119  Evaluate(const Dispatch<1> &, const TRealValueType & u)
120  {
121  const TRealValueType absValue = itk::Math::abs(u);
122  if (absValue < TRealValueType{ 1.0 })
123  {
124  return TRealValueType{ 1.0 } - absValue;
125  }
126  else
127  {
128  return TRealValueType{ 0.0 };
129  }
130  }
134  inline static TRealValueType
135  Evaluate(const Dispatch<2> &, const TRealValueType & u)
136  {
137  const TRealValueType absValue = itk::Math::abs(u);
138  if (absValue < TRealValueType{ 0.5 })
139  {
140  const TRealValueType sqrValue = itk::Math::sqr(absValue);
141  return TRealValueType{ 0.75 } - sqrValue;
142  }
143  else if (absValue < TRealValueType{ 1.5 })
144  {
145  const TRealValueType sqrValue = itk::Math::sqr(absValue);
146  // NOTE: 1.0/8.0 == 0.125
147  return (TRealValueType{ 9.0 } - TRealValueType{ 12.0 } * absValue + TRealValueType{ 4.0 } * sqrValue) *
148  TRealValueType{ 0.125 };
149  }
150  else
151  {
152  return TRealValueType{ 0.0 };
153  }
154  }
158  inline static TRealValueType
159  Evaluate(const Dispatch<3> &, const TRealValueType & u)
160  {
161  const TRealValueType absValue = itk::Math::abs(u);
162  if (absValue < TRealValueType{ 1.0 })
163  {
164  const TRealValueType sqrValue = itk::Math::sqr(absValue);
165  return (TRealValueType{ 4.0 } - TRealValueType{ 6.0 } * sqrValue + TRealValueType{ 3.0 } * sqrValue * absValue) /
166  TRealValueType{ 6.0 };
167  }
168  else if (absValue < TRealValueType{ 2.0 })
169  {
170  const TRealValueType sqrValue = itk::Math::sqr(absValue);
171  return (TRealValueType{ 8.0 } - TRealValueType{ 12.0 } * absValue + TRealValueType{ 6.0 } * sqrValue -
172  sqrValue * absValue) /
173  TRealValueType{ 6.0 };
174  }
175  else
176  {
177  return TRealValueType{ 0.0 };
178  }
179  }
183  inline static TRealValueType
184  Evaluate(const DispatchBase &, const TRealValueType &)
185  {
186  itkGenericExceptionMacro("Evaluate not implemented for spline order " << SplineOrder);
187  }
188 };
189 } // end namespace itk
192 #endif
itk::BSplineKernelFunction
BSpline kernel used for density estimation and nonparametric regression.
Definition: itkBSplineKernelFunction.h:43
itk::BSplineKernelFunction::Evaluate
static TRealValueType Evaluate(const Dispatch< 1 > &, const TRealValueType &u)
Definition: itkBSplineKernelFunction.h:119
itk::BSplineKernelFunction::FastEvaluate
static TRealValueType FastEvaluate(const TRealValueType u)
Definition: itkBSplineKernelFunction.h:66
itk::KernelFunctionBase::RealType
TRealValueType RealType
Definition: itkKernelFunctionBase.h:52
itk::BSplineKernelFunction::Evaluate
static TRealValueType Evaluate(const Dispatch< 3 > &, const TRealValueType &u)
Definition: itkBSplineKernelFunction.h:159
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:726
itk::KernelFunctionBase
Kernel used for density estimation and nonparametric regression.
Definition: itkKernelFunctionBase.h:43
itkKernelFunctionBase.h
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::BSplineKernelFunction::Evaluate
static TRealValueType Evaluate(const DispatchBase &, const TRealValueType &)
Definition: itkBSplineKernelFunction.h:184
itk::Math::abs
bool abs(bool x)
Definition: itkMath.h:843
itk::BSplineKernelFunction::Dispatch
Definition: itkBSplineKernelFunction.h:94
itk::BSplineKernelFunction::Evaluate
TRealValueType Evaluate(const TRealValueType &u) const override
Definition: itkBSplineKernelFunction.h:73
itk::BSplineKernelFunction::PrintSelf
void PrintSelf(std::ostream &os, Indent indent) const override
Definition: itkBSplineKernelFunction.h:82
itk::BSplineKernelFunction::DispatchBase
Definition: itkBSplineKernelFunction.h:91
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::BSplineKernelFunction::Evaluate
static TRealValueType Evaluate(const Dispatch< 0 > &, const TRealValueType &u)
Definition: itkBSplineKernelFunction.h:99
itk::BSplineKernelFunction::Evaluate
static TRealValueType Evaluate(const Dispatch< 2 > &, const TRealValueType &u)
Definition: itkBSplineKernelFunction.h:135
itkMath.h