ITK  5.3.0
Insight Toolkit
itkBSplineInterpolateImageFunction.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 /*=========================================================================
19  *
20  * Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  * For complete copyright, license and disclaimer of warranty information
25  * please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #ifndef itkBSplineInterpolateImageFunction_h
29 #define itkBSplineInterpolateImageFunction_h
30 
32 #include "vnl/vnl_matrix.h"
33 
35 #include "itkConceptChecking.h"
36 #include "itkCovariantVector.h"
37 
38 #include <memory> // For unique_ptr.
39 #include <vector>
40 
41 namespace itk
42 {
82 template <typename TImageType, typename TCoordRep = double, typename TCoefficientType = double>
83 class ITK_TEMPLATE_EXPORT BSplineInterpolateImageFunction : public InterpolateImageFunction<TImageType, TCoordRep>
84 {
85 public:
86  ITK_DISALLOW_COPY_AND_MOVE(BSplineInterpolateImageFunction);
87 
93 
96 
98  itkNewMacro(Self);
99 
101  using typename Superclass::OutputType;
102 
104  using typename Superclass::InputImageType;
105 
107  static constexpr unsigned int ImageDimension = Superclass::ImageDimension;
108 
110  using typename Superclass::IndexType;
111 
113  using typename Superclass::SizeType;
114 
116  using typename Superclass::ContinuousIndexType;
117 
119  using typename Superclass::PointType;
120 
123 
125  using CoefficientDataType = TCoefficientType;
127 
131 
134 
143  OutputType
144  Evaluate(const PointType & point) const override
145  {
146  const ContinuousIndexType index =
147  this->GetInputImage()->template TransformPhysicalPointToContinuousIndex<TCoordRep>(point);
148  // No thread info passed in, so call method that doesn't need thread ID.
149  return (this->EvaluateAtContinuousIndex(index));
150  }
153  virtual OutputType
154  Evaluate(const PointType & point, ThreadIdType threadId) const
155  {
156  const ContinuousIndexType index =
157  this->GetInputImage()->template TransformPhysicalPointToContinuousIndex<TCoordRep>(point);
158  return (this->EvaluateAtContinuousIndex(index, threadId));
159  }
160 
161  OutputType
162  EvaluateAtContinuousIndex(const ContinuousIndexType & index) const override
163  {
164  // Don't know thread information, make evaluateIndex, weights on the stack.
165  // Slower, but safer.
166  vnl_matrix<long> evaluateIndex(ImageDimension, (m_SplineOrder + 1));
167  vnl_matrix<double> weights(ImageDimension, (m_SplineOrder + 1));
168 
169  // Pass evaluateIndex, weights by reference. They're only good as long
170  // as this method is in scope.
171  return this->EvaluateAtContinuousIndexInternal(index, evaluateIndex, weights);
172  }
173 
174  virtual OutputType
176  {
177  // Pass evaluateIndex, weights by reference. Different threadIDs get different instances.
178  return this->EvaluateAtContinuousIndexInternal(x, m_ThreadedEvaluateIndex[threadId], m_ThreadedWeights[threadId]);
179  }
180 
181  CovariantVectorType
183  {
184  ContinuousIndexType index;
185 
186  this->GetInputImage()->TransformPhysicalPointToContinuousIndex(point, index);
187  // No thread info passed in, so call method that doesn't need thread ID.
188  return (this->EvaluateDerivativeAtContinuousIndex(index));
189  }
190 
191  CovariantVectorType
193  {
194  const ContinuousIndexType index =
195  this->GetInputImage()->template TransformPhysicalPointToContinuousIndex<TCoordRep>(point);
196  return (this->EvaluateDerivativeAtContinuousIndex(index, threadId));
197  }
198 
199  CovariantVectorType
201  {
202  // Don't know thread information, make evaluateIndex, weights,
203  // weightsDerivative
204  // on the stack.
205  // Slower, but safer.
206  vnl_matrix<long> evaluateIndex(ImageDimension, (m_SplineOrder + 1));
207  vnl_matrix<double> weights(ImageDimension, (m_SplineOrder + 1));
208  vnl_matrix<double> weightsDerivative(ImageDimension, (m_SplineOrder + 1));
209 
210  // Pass evaluateIndex, weights, weightsDerivative by reference. They're only
211  // good
212  // as long as this method is in scope.
213  return this->EvaluateDerivativeAtContinuousIndexInternal(x, evaluateIndex, weights, weightsDerivative);
214  }
215 
216  CovariantVectorType
218  {
219  return this->EvaluateDerivativeAtContinuousIndexInternal(
220  x, m_ThreadedEvaluateIndex[threadId], m_ThreadedWeights[threadId], m_ThreadedWeightsDerivative[threadId]);
221  }
222 
223  void
225  {
226  ContinuousIndexType index;
227 
228  this->GetInputImage()->TransformPhysicalPointToContinuousIndex(point, index);
229 
230  // No thread info passed in, so call method that doesn't need thread ID.
231  this->EvaluateValueAndDerivativeAtContinuousIndex(index, value, deriv);
232  }
233 
234  void
236  OutputType & value,
237  CovariantVectorType & deriv,
238  ThreadIdType threadId) const
239  {
240  const ContinuousIndexType index =
241  this->GetInputImage()->template TransformPhysicalPointToContinuousIndex<TCoordRep>(point);
242  this->EvaluateValueAndDerivativeAtContinuousIndex(index, value, deriv, threadId);
243  }
244 
245  void
247  OutputType & value,
248  CovariantVectorType & deriv) const
249  {
250  // Don't know thread information, make evaluateIndex, weights,
251  // weightsDerivative
252  // on the stack.
253  // Slower, but safer.
254  vnl_matrix<long> evaluateIndex(ImageDimension, (m_SplineOrder + 1));
255  vnl_matrix<double> weights(ImageDimension, (m_SplineOrder + 1));
256  vnl_matrix<double> weightsDerivative(ImageDimension, (m_SplineOrder + 1));
257 
258  // Pass evaluateIndex, weights, weightsDerivative by reference. They're only
259  // good
260  // as long as this method is in scope.
261  this->EvaluateValueAndDerivativeAtContinuousIndexInternal(
262  x, value, deriv, evaluateIndex, weights, weightsDerivative);
263  }
264 
265  void
267  OutputType & value,
268  CovariantVectorType & derivativeValue,
269  ThreadIdType threadId) const
270  {
271  this->EvaluateValueAndDerivativeAtContinuousIndexInternal(x,
272  value,
273  derivativeValue,
274  m_ThreadedEvaluateIndex[threadId],
275  m_ThreadedWeights[threadId],
276  m_ThreadedWeightsDerivative[threadId]);
277  }
278 
281  void
282  SetSplineOrder(unsigned int SplineOrder);
283 
284  itkGetConstMacro(SplineOrder, unsigned int);
285 
286  void
287  SetNumberOfWorkUnits(ThreadIdType numThreads);
288 
289  itkGetConstMacro(NumberOfWorkUnits, ThreadIdType);
290 
292  void
293  SetInputImage(const TImageType * inputData) override;
294 
305  itkSetMacro(UseImageDirection, bool);
306  itkGetConstMacro(UseImageDirection, bool);
307  itkBooleanMacro(UseImageDirection);
310  SizeType
311  GetRadius() const override
312  {
313  return SizeType::Filled(m_SplineOrder + 1);
314  }
315 
316 protected:
335  virtual OutputType
336  EvaluateAtContinuousIndexInternal(const ContinuousIndexType & x,
337  vnl_matrix<long> & evaluateIndex,
338  vnl_matrix<double> & weights) const;
339 
340  virtual void
341  EvaluateValueAndDerivativeAtContinuousIndexInternal(const ContinuousIndexType & x,
342  OutputType & value,
343  CovariantVectorType & derivativeValue,
344  vnl_matrix<long> & evaluateIndex,
345  vnl_matrix<double> & weights,
346  vnl_matrix<double> & weightsDerivative) const;
347 
348  virtual CovariantVectorType
349  EvaluateDerivativeAtContinuousIndexInternal(const ContinuousIndexType & x,
350  vnl_matrix<long> & evaluateIndex,
351  vnl_matrix<double> & weights,
352  vnl_matrix<double> & weightsDerivative) const;
353 
355  ~BSplineInterpolateImageFunction() override = default;
356  void
357  PrintSelf(std::ostream & os, Indent indent) const override;
358 
359  // These are needed by the smoothing spline routine.
360  // temp storage for processing of Coefficients
361  std::vector<CoefficientDataType> m_Scratch;
362  // Image size
364  // User specified spline order (3rd or cubic is the default)
365  unsigned int m_SplineOrder;
366 
367  // Spline coefficients
369 
370 private:
372  void
373  SetInterpolationWeights(const ContinuousIndexType & x,
374  const vnl_matrix<long> & EvaluateIndex,
375  vnl_matrix<double> & weights,
376  unsigned int splineOrder) const;
377 
379  void
380  SetDerivativeWeights(const ContinuousIndexType & x,
381  const vnl_matrix<long> & EvaluateIndex,
382  vnl_matrix<double> & weights,
383  unsigned int splineOrder) const;
384 
387  void
388  GeneratePointsToIndex();
389 
391  void
392  DetermineRegionOfSupport(vnl_matrix<long> & evaluateIndex,
393  const ContinuousIndexType & x,
394  unsigned int splineOrder) const;
395 
398  void
399  ApplyMirrorBoundaryConditions(vnl_matrix<long> & evaluateIndex, unsigned int splineOrder) const;
400 
401  Iterator m_CIterator; // Iterator for
402  // traversing spline
403  // coefficients.
404  unsigned long m_MaxNumberInterpolationPoints; // number of
405  // neighborhood
406  // points used for
407  // interpolation
408  std::vector<IndexType> m_PointsToIndex; // Preallocation of
409  // interpolation
410  // neighborhood
411  // indices
412 
414 
415  // flag to take or not the image direction into account when computing the
416  // derivatives.
418 
420  std::unique_ptr<vnl_matrix<long>[]> m_ThreadedEvaluateIndex;
421  std::unique_ptr<vnl_matrix<double>[]> m_ThreadedWeights;
422  std::unique_ptr<vnl_matrix<double>[]> m_ThreadedWeightsDerivative;
423 };
424 } // namespace itk
425 
426 #ifndef ITK_MANUAL_INSTANTIATION
427 # include "itkBSplineInterpolateImageFunction.hxx"
428 #endif
429 
430 #endif
itk::BSplineInterpolateImageFunction::m_ThreadedWeights
std::unique_ptr< vnl_matrix< double >[]> m_ThreadedWeights
Definition: itkBSplineInterpolateImageFunction.h:421
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:92
itk::BSplineInterpolateImageFunction::m_CoefficientFilter
CoefficientFilterPointer m_CoefficientFilter
Definition: itkBSplineInterpolateImageFunction.h:413
itk::BSplineInterpolateImageFunction::EvaluateDerivativeAtContinuousIndex
CovariantVectorType EvaluateDerivativeAtContinuousIndex(const ContinuousIndexType &x, ThreadIdType threadId) const
Definition: itkBSplineInterpolateImageFunction.h:217
itkCovariantVector.h
itk::BSplineInterpolateImageFunction::m_PointsToIndex
std::vector< IndexType > m_PointsToIndex
Definition: itkBSplineInterpolateImageFunction.h:408
itk::BSplineInterpolateImageFunction::Evaluate
virtual OutputType Evaluate(const PointType &point, ThreadIdType threadId) const
Definition: itkBSplineInterpolateImageFunction.h:154
itk::BSplineInterpolateImageFunction
Evaluates the B-Spline interpolation of an image. Spline order may be from 0 to 5.
Definition: itkBSplineInterpolateImageFunction.h:83
itk::BSplineDecompositionImageFilter
Calculates the B-Spline coefficients of an image. Spline order may be from 0 to 5.
Definition: itkBSplineDecompositionImageFilter.h:74
itk::BSplineInterpolateImageFunction::GetRadius
SizeType GetRadius() const override
Definition: itkBSplineInterpolateImageFunction.h:311
itk::BSplineInterpolateImageFunction::m_NumberOfWorkUnits
ThreadIdType m_NumberOfWorkUnits
Definition: itkBSplineInterpolateImageFunction.h:419
itk::ImageLinearIteratorWithIndex< TImageType >
itk::BSplineInterpolateImageFunction::EvaluateAtContinuousIndex
OutputType EvaluateAtContinuousIndex(const ContinuousIndexType &index) const override
Definition: itkBSplineInterpolateImageFunction.h:162
itk::GTest::TypedefsAndConstructors::Dimension2::PointType
ImageBaseType::PointType PointType
Definition: itkGTestTypedefsAndConstructors.h:51
itk::BSplineInterpolateImageFunction::m_MaxNumberInterpolationPoints
unsigned long m_MaxNumberInterpolationPoints
Definition: itkBSplineInterpolateImageFunction.h:404
itkConceptChecking.h
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::BSplineInterpolateImageFunction::EvaluateValueAndDerivativeAtContinuousIndex
void EvaluateValueAndDerivativeAtContinuousIndex(const ContinuousIndexType &x, OutputType &value, CovariantVectorType &deriv) const
Definition: itkBSplineInterpolateImageFunction.h:246
itk::BSplineInterpolateImageFunction< TImageType, TCoordRep, TImageType::PixelType >::CoefficientFilterPointer
typename CoefficientFilter::Pointer CoefficientFilterPointer
Definition: itkBSplineInterpolateImageFunction.h:130
itk::BSplineInterpolateImageFunction::m_ThreadedEvaluateIndex
std::unique_ptr< vnl_matrix< long >[]> m_ThreadedEvaluateIndex
Definition: itkBSplineInterpolateImageFunction.h:420
itk::BSplineInterpolateImageFunction::EvaluateValueAndDerivative
void EvaluateValueAndDerivative(const PointType &point, OutputType &value, CovariantVectorType &deriv) const
Definition: itkBSplineInterpolateImageFunction.h:224
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::FunctionBase< Point< TCoordRep, TImageType ::ImageDimension >, NumericTraits< TImageType ::PixelType >::RealType >::OutputType
NumericTraits< TImageType ::PixelType >::RealType OutputType
Definition: itkFunctionBase.h:62
itk::BSplineInterpolateImageFunction::m_DataLength
TImageType::SizeType m_DataLength
Definition: itkBSplineInterpolateImageFunction.h:363
itk::BSplineInterpolateImageFunction::m_Coefficients
CoefficientImageType::ConstPointer m_Coefficients
Definition: itkBSplineInterpolateImageFunction.h:368
itk::BSplineInterpolateImageFunction::EvaluateDerivativeAtContinuousIndex
CovariantVectorType EvaluateDerivativeAtContinuousIndex(const ContinuousIndexType &x) const
Definition: itkBSplineInterpolateImageFunction.h:200
itkBSplineDecompositionImageFilter.h
itk::ThreadIdType
unsigned int ThreadIdType
Definition: itkIntTypes.h:99
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::BSplineInterpolateImageFunction::m_Scratch
std::vector< CoefficientDataType > m_Scratch
Definition: itkBSplineInterpolateImageFunction.h:361
itk::BSplineInterpolateImageFunction::EvaluateValueAndDerivative
void EvaluateValueAndDerivative(const PointType &point, OutputType &value, CovariantVectorType &deriv, ThreadIdType threadId) const
Definition: itkBSplineInterpolateImageFunction.h:235
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::BSplineInterpolateImageFunction::m_CIterator
Iterator m_CIterator
Definition: itkBSplineInterpolateImageFunction.h:401
itk::point
*par Constraints *The filter requires an image with at least two dimensions and a vector *length of at least The theory supports extension to scalar but *the implementation of the itk vector classes do not **The template parameter TRealType must be floating point(float or double) or *a user-defined "real" numerical type with arithmetic operations defined *sufficient to compute derivatives. **\par Performance *This filter will automatically multithread if run with *SetUsePrincipleComponents
itk::BSplineInterpolateImageFunction::m_ThreadedWeightsDerivative
std::unique_ptr< vnl_matrix< double >[]> m_ThreadedWeightsDerivative
Definition: itkBSplineInterpolateImageFunction.h:422
itk::BSplineInterpolateImageFunction::EvaluateDerivative
CovariantVectorType EvaluateDerivative(const PointType &point) const
Definition: itkBSplineInterpolateImageFunction.h:182
itk::BSplineInterpolateImageFunction::EvaluateValueAndDerivativeAtContinuousIndex
void EvaluateValueAndDerivativeAtContinuousIndex(const ContinuousIndexType &x, OutputType &value, CovariantVectorType &derivativeValue, ThreadIdType threadId) const
Definition: itkBSplineInterpolateImageFunction.h:266
itk::CovariantVector
A templated class holding a n-Dimensional covariant vector.
Definition: itkCovariantVector.h:70
itk::BSplineInterpolateImageFunction::EvaluateDerivative
CovariantVectorType EvaluateDerivative(const PointType &point, ThreadIdType threadId) const
Definition: itkBSplineInterpolateImageFunction.h:192
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ContinuousIndex< TCoordRep, Self::ImageDimension >
itk::BSplineInterpolateImageFunction::m_UseImageDirection
bool m_UseImageDirection
Definition: itkBSplineInterpolateImageFunction.h:417
itk::Point
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:53
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
itk::BSplineInterpolateImageFunction::Evaluate
OutputType Evaluate(const PointType &point) const override
Definition: itkBSplineInterpolateImageFunction.h:144
itkInterpolateImageFunction.h
itk::BSplineInterpolateImageFunction::EvaluateAtContinuousIndex
virtual OutputType EvaluateAtContinuousIndex(const ContinuousIndexType &x, ThreadIdType threadId) const
Definition: itkBSplineInterpolateImageFunction.h:175
itk::BSplineInterpolateImageFunction< TImageType, TCoordRep, TImageType::PixelType >::CoefficientDataType
TImageType::PixelType CoefficientDataType
Definition: itkBSplineInterpolateImageFunction.h:125
itk::InterpolateImageFunction
Base class for all image interpolators.
Definition: itkInterpolateImageFunction.h:45
itk::BSplineInterpolateImageFunction::m_SplineOrder
unsigned int m_SplineOrder
Definition: itkBSplineInterpolateImageFunction.h:365