ITK  5.0.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:
46  ITK_DISALLOW_COPY_AND_ASSIGN(BSplineKernelFunction);
47 
52 
53  using RealType = typename Superclass::RealType;
55  itkNewMacro(Self);
56 
59 
61  static constexpr unsigned int SplineOrder = VSplineOrder;
62 
64  TRealValueType Evaluate(const TRealValueType & u) const override
65  {
66  return this->Evaluate(Dispatch< VSplineOrder >(), u);
67  }
68 
69 protected:
70  BSplineKernelFunction()= default;
71  ~BSplineKernelFunction() override = default;
72  void PrintSelf(std::ostream & os, Indent indent) const override
73  {
74  Superclass::PrintSelf(os, indent);
75  os << indent << "Spline Order: " << SplineOrder << std::endl;
76  }
77 
78 private:
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  }
170 };
171 } // end namespace itk
173 
174 #endif
Define numeric traits for std::vector.
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:707
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.
TRealValueType Evaluate(const Dispatch< 0 > &, const TRealValueType &u) const
typename Superclass::RealType RealType
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
TRealValueType Evaluate(const DispatchBase &, const TRealValueType &) const
TRealValueType Evaluate(const Dispatch< 2 > &, const TRealValueType &u) const