ITK  6.0.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  static inline 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  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  static inline 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  }
128  return TRealValueType{ 0.0 };
129  }
130 
132  static inline TRealValueType
133  Evaluate(const Dispatch<2> &, const TRealValueType & u)
134  {
135  const TRealValueType absValue = itk::Math::abs(u);
136  if (absValue < TRealValueType{ 0.5 })
137  {
138  const TRealValueType sqrValue = itk::Math::sqr(absValue);
139  return TRealValueType{ 0.75 } - sqrValue;
140  }
141  if (absValue < TRealValueType{ 1.5 })
142  {
143  const TRealValueType sqrValue = itk::Math::sqr(absValue);
144  // NOTE: 1.0/8.0 == 0.125
145  return (TRealValueType{ 9.0 } - TRealValueType{ 12.0 } * absValue + TRealValueType{ 4.0 } * sqrValue) *
146  TRealValueType{ 0.125 };
147  }
148  else
149  {
150  return TRealValueType{ 0.0 };
151  }
152  }
156  static inline TRealValueType
157  Evaluate(const Dispatch<3> &, const TRealValueType & u)
158  {
159  const TRealValueType absValue = itk::Math::abs(u);
160  if (absValue < TRealValueType{ 1.0 })
161  {
162  const TRealValueType sqrValue = itk::Math::sqr(absValue);
163  return (TRealValueType{ 4.0 } - TRealValueType{ 6.0 } * sqrValue + TRealValueType{ 3.0 } * sqrValue * absValue) /
164  TRealValueType{ 6.0 };
165  }
166  if (absValue < TRealValueType{ 2.0 })
167  {
168  const TRealValueType sqrValue = itk::Math::sqr(absValue);
169  return (TRealValueType{ 8.0 } - TRealValueType{ 12.0 } * absValue + TRealValueType{ 6.0 } * sqrValue -
170  sqrValue * absValue) /
171  TRealValueType{ 6.0 };
172  }
173  else
174  {
175  return TRealValueType{ 0.0 };
176  }
177  }
181  static inline TRealValueType
182  Evaluate(const DispatchBase &, const TRealValueType &)
183  {
184  itkGenericExceptionMacro("Evaluate not implemented for spline order " << SplineOrder);
185  }
186 };
187 } // end namespace itk
190 #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:157
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:722
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:182
itk::Math::abs
bool abs(bool x)
Definition: itkMath.h:839
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: itkAnatomicalOrientation.h:29
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:133
itkMath.h