ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkMultiphaseFiniteDifferenceImageFilter.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 itkMultiphaseFiniteDifferenceImageFilter_h
19 #define itkMultiphaseFiniteDifferenceImageFilter_h
20 
21 #include "itkInPlaceImageFilter.h"
23 #include "vnl/vnl_vector.h"
24 #include "itkImageRegionIterator.h"
25 
26 #include "itkListSample.h"
27 #include "itkKdTreeGenerator.h"
28 
29 namespace itk
30 {
160 template< typename TInputImage,
161  typename TFeatureImage,
162  typename TOutputImage,
163  typename TFiniteDifferenceFunction = FiniteDifferenceFunction< TOutputImage >,
164  typename TIdCell = unsigned int >
165 class ITK_TEMPLATE_EXPORT MultiphaseFiniteDifferenceImageFilter:
166  public InPlaceImageFilter< TFeatureImage, TOutputImage >
167 {
168 public:
169  ITK_DISALLOW_COPY_AND_ASSIGN(MultiphaseFiniteDifferenceImageFilter);
170 
176 
179 
181  static constexpr unsigned int ImageDimension = TOutputImage::ImageDimension;
182 
184  using InputImageType = TInputImage;
185  using InputImagePointer = typename InputImageType::Pointer;
187  using InputCoordRepType = typename InputPointType::CoordRepType;
193  using InputPixelType = typename InputImageType::PixelType;
194  using InputSpacingType = typename InputImageType::SpacingType;
196 
197  using FeatureImageType = TFeatureImage;
198  using FeatureImagePointer = typename FeatureImageType::Pointer;
201  using FeatureSpacingType = typename FeatureImageType::SpacingType;
203  using FeaturePixelType = typename FeatureImageType::PixelType;
204 
205  using OutputImageType = TOutputImage;
206  using OutputImagePointer = typename OutputImageType::Pointer;
207  using OutputPixelType = typename OutputImageType::PixelType;
213 
214  using IdCellType = TIdCell;
215  using VectorIdCellType = std::vector< IdCellType >;
216 
220  using FiniteDifferenceFunctionType = TFiniteDifferenceFunction;
221  using FiniteDifferenceFunctionPointer = typename FiniteDifferenceFunctionType::Pointer;
222  using TimeStepType = typename FiniteDifferenceFunctionType::TimeStepType;
223  using TimeStepVectorType = typename std::vector< TimeStepType >;
224  using RadiusType = typename FiniteDifferenceFunctionType::RadiusType;
225 
226  using CentroidVectorType =
232  using KdTreePointer = typename KdTreeType::Pointer;
233 
239  const IdCellType & functionIndex) const
240  {
241  if ( functionIndex < m_FunctionCount )
242  {
243  return ( this->m_DifferenceFunctions[functionIndex] );
244  }
245  else
246  {
247  return nullptr;
248  }
249  }
251 
256  virtual void SetDifferenceFunction(const IdCellType & functionIndex,
258  {
259  if ( functionIndex < m_FunctionCount )
260  {
261  this->m_DifferenceFunctions[functionIndex] = function;
262  }
263  }
265 
267  itkSetMacro(NumberOfIterations, unsigned int);
268  itkGetConstReferenceMacro(NumberOfIterations, unsigned int);
270 
273  itkSetMacro(UseImageSpacing, bool);
274  itkBooleanMacro(UseImageSpacing);
275  itkGetConstReferenceMacro(UseImageSpacing, bool);
277 
280  itkSetMacro(MaximumRMSError, double);
281  itkGetConstReferenceMacro(MaximumRMSError, double);
283 
286  itkSetMacro(RMSChange, double);
287  itkGetConstReferenceMacro(RMSChange, double);
289 
291  itkSetMacro(InitializedState, bool);
292  itkGetConstReferenceMacro(InitializedState, bool);
293  itkBooleanMacro(InitializedState);
295 
298  itkSetMacro(ManualReinitialization, bool);
299  itkGetConstReferenceMacro(ManualReinitialization, bool);
300  itkBooleanMacro(ManualReinitialization);
302 
304  itkSetMacro(ElapsedIterations, unsigned int);
305 
307  itkGetConstReferenceMacro(ElapsedIterations, unsigned int);
308 
309  void SetLevelSet(const IdCellType & i, const InputImageType *levelSet)
310  {
311  m_LevelSet[i] = InputImageType::New();
312  m_LevelSet[i]->SetRequestedRegion( levelSet->GetRequestedRegion() );
313  m_LevelSet[i]->SetBufferedRegion( levelSet->GetBufferedRegion() );
314  m_LevelSet[i]->SetLargestPossibleRegion( levelSet->GetLargestPossibleRegion() );
315  m_LevelSet[i]->Allocate();
316  m_LevelSet[i]->CopyInformation(levelSet);
317 
318  ImageRegionConstIterator< InputImageType > in ( levelSet, levelSet->GetBufferedRegion() );
319  ImageRegionIterator< InputImageType > cp ( m_LevelSet[i], levelSet->GetBufferedRegion() );
320 
321  in.GoToBegin();
322  cp.GoToBegin();
323 
324  while ( !in.IsAtEnd() )
325  {
326  cp.Set( in.Get() );
327  ++in;
328  ++cp;
329  }
330  }
331 
333  {
334  if ( i >= m_FunctionCount )
335  {
336  itkExceptionMacro("Request for level set #" << i
337  << " but there are only " << m_FunctionCount);
338  }
339  else
340  {
341  return m_LevelSet[i];
342  }
343  }
344 
346  {
347  this->m_Lookup = lookup;
348  }
349 
350  void SetKdTree(KdTreeType *kdtree)
351  {
352  this->m_KdTree = kdtree;
353  }
354 
355  void SetFunctionCount(const IdCellType & n)
356  {
357  m_FunctionCount = n;
358 
359  m_DifferenceFunctions.resize(m_FunctionCount, nullptr);
360 
361  RadiusType radius;
362  radius.Fill(1);
363 
364  for ( unsigned int i = 0; i < this->m_FunctionCount; i++ )
365  {
366  this->m_DifferenceFunctions[i] = FiniteDifferenceFunctionType::New();
367  this->m_DifferenceFunctions[i]->Initialize(radius);
368  }
369 
370  // Initialize the images
371  m_LevelSet.resize(m_FunctionCount, nullptr);
372 
373  // Initialize the lookup table
374  this->m_Lookup.resize(m_FunctionCount);
375 
376  IdCellType k = 1;
377 
378  {
379  auto it = this->m_Lookup.begin();
380  auto _end = this->m_Lookup.end();
381  while ( it != _end )
382  {
383  *it = k;
384  ++it;
385  ++k;
386  }
387  }
388  }
389 
390 protected:
392  {
393  this->m_KdTree = nullptr;
394  this->m_ElapsedIterations = 0;
395  this->m_MaximumRMSError = itk::Math::eps;
396  this->m_RMSChange = NumericTraits< double >::max();
397  this->m_UseImageSpacing = true;
398  this->m_ManualReinitialization = false;
399  this->m_InitializedState = false;
400  this->m_NumberOfIterations = NumericTraits< unsigned int >::max();
401  this->m_FunctionCount = 0;
402  this->InPlaceOff();
403  }
404 
406 
408  std::vector< InputImagePointer > m_LevelSet;
411 
412  unsigned int m_ElapsedIterations;
414  double m_RMSChange;
415  unsigned int m_NumberOfIterations;
416 
418  std::vector< FiniteDifferenceFunctionPointer > m_DifferenceFunctions;
419 
423 
424  void PrintSelf(std::ostream & os, Indent indent) const override;
425 
427  virtual void AllocateUpdateBuffer() = 0;
428 
432  virtual void ApplyUpdate(TimeStepType dt) = 0;
433 
439  virtual TimeStepType CalculateChange() = 0;
440 
444  virtual void CopyInputToOutput() = 0;
445 
449  void GenerateData() override;
450 
462  void GenerateInputRequestedRegion() override;
463 
466  virtual bool Halt();
467 
477  virtual bool ThreadedHalt( void *itkNotUsed(threadInfo) )
478  {
479  return this->Halt();
480  }
481 
487  virtual void Initialize() {}
488 
495  virtual void InitializeIteration()
496  {
497  for ( IdCellType i = 0; i < this->m_FunctionCount; i++ )
498  {
499  this->m_DifferenceFunctions[i]->InitializeIteration();
500  }
501  }
503 
516  inline TimeStepType ResolveTimeStep(const TimeStepVectorType & timeStepList,
517  const std::vector< bool > & valid);
518 
521  virtual void PostProcessOutput() {}
522 
523 private:
527 
530 };
531 } // end namespace itk
532 
533 #ifndef ITK_MANUAL_INSTANTIATION
534 #include "itkMultiphaseFiniteDifferenceImageFilter.hxx"
535 #endif
536 
537 #endif
typename OutputImageType::Pointer OutputImagePointer
Define numeric traits for std::vector.
unsigned long SizeValueType
Definition: itkIntTypes.h:83
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Base class for all process objects that output image data.
virtual const FiniteDifferenceFunctionPointer GetDifferenceFunction(const IdCellType &functionIndex) const
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
A multi-dimensional iterator templated over image type that walks a region of pixels.
TOutputImage OutputImageType
signed long IndexValueType
Definition: itkIntTypes.h:90
This class generates a KdTree object without centroid information.
static constexpr double eps
Definition: itkMath.h:94
void SetLevelSet(const IdCellType &i, const InputImageType *levelSet)
This class is the native implementation of the a Sample with an STL container.
Definition: itkListSample.h:51
Control indentation during Print() invocation.
Definition: itkIndent.h:49
virtual void SetDifferenceFunction(const IdCellType &functionIndex, FiniteDifferenceFunctionPointer function)
Base class for filters that take an image as input and overwrite that image as the output...
std::vector< FiniteDifferenceFunctionPointer > m_DifferenceFunctions
This class provides methods for k-nearest neighbor search and related data structures for a k-d tree...
Definition: itkKdTree.h:483
signed long OffsetValueType
Definition: itkIntTypes.h:94
A multi-dimensional iterator templated over image type that walks a region of pixels.