ITK  5.3.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 
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  os << indent << "Spline Order: " << SplineOrder << std::endl;
86  }
87 
88 private:
90  struct DispatchBase
91  {};
92  template <unsigned int>
93  struct Dispatch : public DispatchBase
94  {};
95 
97  inline static TRealValueType
98  Evaluate(const Dispatch<0> &, const TRealValueType & u)
99  {
100  const TRealValueType absValue = itk::Math::abs(u);
101  if (absValue < TRealValueType{ 0.5 })
102  {
103  return TRealValueType{ 1.0 };
104  }
105  else if (Math::ExactlyEquals(absValue, TRealValueType{ 0.5 }))
106  {
107  return TRealValueType{ 0.5 };
108  }
109  else
110  {
111  return TRealValueType{ 0.0 };
112  }
113  }
117  inline static TRealValueType
118  Evaluate(const Dispatch<1> &, const TRealValueType & u)
119  {
120  const TRealValueType absValue = itk::Math::abs(u);
121  if (absValue < TRealValueType{ 1.0 })
122  {
123  return TRealValueType{ 1.0 } - absValue;
124  }
125  else
126  {
127  return TRealValueType{ 0.0 };
128  }
129  }
133  inline static TRealValueType
134  Evaluate(const Dispatch<2> &, const TRealValueType & u)
135  {
136  const TRealValueType absValue = itk::Math::abs(u);
137  if (absValue < TRealValueType{ 0.5 })
138  {
139  const TRealValueType sqrValue = itk::Math::sqr(absValue);
140  return TRealValueType{ 0.75 } - sqrValue;
141  }
142  else if (absValue < TRealValueType{ 1.5 })
143  {
144  const TRealValueType sqrValue = itk::Math::sqr(absValue);
145  // NOTE: 1.0/8.0 == 0.125
146  return (TRealValueType{ 9.0 } - TRealValueType{ 12.0 } * absValue + TRealValueType{ 4.0 } * sqrValue) *
147  TRealValueType{ 0.125 };
148  }
149  else
150  {
151  return TRealValueType{ 0.0 };
152  }
153  }
157  inline static TRealValueType
158  Evaluate(const Dispatch<3> &, const TRealValueType & u)
159  {
160  const TRealValueType absValue = itk::Math::abs(u);
161  if (absValue < TRealValueType{ 1.0 })
162  {
163  const TRealValueType sqrValue = itk::Math::sqr(absValue);
164  return (TRealValueType{ 4.0 } - TRealValueType{ 6.0 } * sqrValue + TRealValueType{ 3.0 } * sqrValue * absValue) /
165  TRealValueType{ 6.0 };
166  }
167  else if (absValue < TRealValueType{ 2.0 })
168  {
169  const TRealValueType sqrValue = itk::Math::sqr(absValue);
170  return (TRealValueType{ 8.0 } - TRealValueType{ 12.0 } * absValue + TRealValueType{ 6.0 } * sqrValue -
171  sqrValue * absValue) /
172  TRealValueType{ 6.0 };
173  }
174  else
175  {
176  return TRealValueType{ 0.0 };
177  }
178  }
182  inline static TRealValueType
183  Evaluate(const DispatchBase &, const TRealValueType &)
184  {
185  itkGenericExceptionMacro("Evaluate not implemented for spline order " << SplineOrder);
186  }
187 };
188 } // end namespace itk
191 #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:118
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:158
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
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:183
itk::Math::abs
bool abs(bool x)
Definition: itkMath.h:851
itk::BSplineKernelFunction::Dispatch
Definition: itkBSplineKernelFunction.h:93
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:90
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:98
itk::BSplineKernelFunction::Evaluate
static TRealValueType Evaluate(const Dispatch< 2 > &, const TRealValueType &u)
Definition: itkBSplineKernelFunction.h:134
itkMath.h