Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
itkShortestPathImageFilter.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 #ifndef itkShortestPathImageFilter_h
13 #define itkShortestPathImageFilter_h
14 
15 #include <itkImageToImageFilter.h>
17 #include <itkShortestPathNode.h>
18 #include <itkImageRegionIteratorWithIndex.h>
19 
20 #include <itkMacro.h>
21 
22 namespace itk
23 {
58  template <class TInputImageType, class TOutputImageType>
59  class ShortestPathImageFilter : public ImageToImageFilter<TInputImageType, TOutputImageType>
60  {
61  public:
62  // Standard Typedefs
64  typedef ImageToImageFilter<TInputImageType, TOutputImageType> Superclass;
67 
68  // Typedefs for metric
71 
72  // More typedefs for convenience
73  typedef TInputImageType InputImageType;
74  typedef typename TInputImageType::Pointer InputImagePointer;
75  typedef typename TInputImageType::PixelType InputImagePixelType;
76  typedef typename TInputImageType::SizeType InputImageSizeType;
77  typedef typename TInputImageType::IndexType IndexType;
78  typedef typename itk::ImageRegionIteratorWithIndex<InputImageType> InputImageIteratorType;
79 
80  typedef TOutputImageType OutputImageType;
81  typedef typename TOutputImageType::Pointer OutputImagePointer;
82  typedef typename TOutputImageType::PixelType OutputImagePixelType;
83  typedef typename TOutputImageType::IndexType OutputImageIndexType;
84  typedef ImageRegionIteratorWithIndex<OutputImageType> OutputImageIteratorType;
85  typedef itk::ShapedNeighborhoodIterator<TInputImageType> itkShapedNeighborhoodIteratorType;
86 
87  // New Macro for smartpointer instantiation
88  itkFactorylessNewMacro(Self);
89  itkCloneMacro(Self);
90 
91  // Run-time type information
92  itkTypeMacro(ShortestPathImageFilter, ImageToImageFilter);
93 
99  void PrintSelf(std::ostream &os, Indent indent) const override;
100 
109  {
111  };
112 
118  void SetStartIndex(const IndexType &StartIndex);
119 
131  void AddEndIndex(const IndexType &index);
132 
138  void SetEndIndex(const IndexType &EndIndex);
139 
146  itkSetMacro(FullNeighborsMode, bool);
148  itkGetMacro(FullNeighborsMode, bool);
149 
156  itkSetMacro(Graph_fullNeighbors, bool);
157 
165  itkSetMacro(MakeOutputImage, bool);
167  itkGetMacro(MakeOutputImage, bool);
168 
175  itkSetMacro(StoreVectorOrder, bool);
177  itkGetMacro(StoreVectorOrder, bool);
178 
188  itkSetMacro(CalcAllDistances, bool);
190  itkGetMacro(CalcAllDistances, bool);
191 
199  itkSetMacro(ActivateTimeOut, bool);
201  itkGetMacro(ActivateTimeOut, bool);
202 
209  std::vector<IndexType> GetVectorPath();
210 
221  std::vector<std::vector<IndexType>> GetMultipleVectorPaths();
222 
233 
244 
252 
259  void CleanUp();
260 
262  itkSetObjectMacro(CostFunction, CostFunctionType);
264  itkGetObjectMacro(CostFunction, CostFunctionType);
265 
273  void SetUseCostFunction(bool doUseCostFunction) { m_useCostFunction = doUseCostFunction; };
274 
280 
281  protected:
282  std::vector<IndexType>
283  m_endPoints; // if you fill this vector, the algo will not rest until all endPoints have been reached
284  std::vector<IndexType> m_endPointsClosed;
285 
286  ShortestPathNode *m_Nodes; // main list that contains all nodes
292  std::vector<ShortestPathNode *> m_Graph_DiscoveredNodeList;
293  ShortestPathImageFilter(Self &); // intentionally not implemented
294  void operator=(const Self &); // intentionally not implemented
295  const static int BACKGROUND = 0;
296  const static int FOREGROUND = 255;
298 
300  bool m_StoreVectorOrder; // Store an Vector of Order, so you can call getVectorOrderImage after update
301  bool m_CalcAllDistances; // Calculate all Distances, so you can call getDistanceImage after update (warning algo
302  // will take a long time)
303 
305 
306  bool m_ActivateTimeOut; // if true, then i search max. 30 secs. then abort
307 
309 
312  std::vector<IndexType> m_VectorPath;
313  std::vector<std::vector<IndexType>> m_MultipleVectorPaths;
314 
315  std::vector<NodeNumType> m_VectorOrder;
316 
318 
320 
321  // \brief Create all the outputs
322  void MakeOutputs();
323 
324  // \brief Generate Data
325  void GenerateData() override;
326 
327  // \brief gets the estimate costs from pixel a to target.
329 
330  typename InputImageType::Pointer m_magnitudeImage;
331 
332  // \brief Convert a indexnumber of a node in m_Nodes to image coordinates
333  typename TInputImageType::IndexType NodeToCoord(NodeNumType);
334 
335  // \brief Convert image coordinate to a indexnumber of a node in m_Nodes
336  unsigned int CoordToNode(IndexType);
337 
338  // \brief Returns the neighbors of a node
339  std::vector<ShortestPathNode *> GetNeighbors(NodeNumType nodeNum, bool FullNeighbors);
340 
341  // \brief Check if coords are in bounds of image
343 
344  // \brief Initializes the graph
345  void InitGraph();
346 
347  // \brief Start ShortestPathSearch
349  };
350 
351 } // end of namespace itk
352 
353 #include <itkShortestPathImageFilter.tpp>
354 
355 #endif
Abstract base class for cost functions used by ShortestPathImageFilter.
ITK image filter that computes the shortest path between pixels using A*.
TInputImageType::Pointer InputImagePointer
CostFunctionType::Pointer CostFunctionTypePointer
std::vector< std::vector< IndexType > > m_MultipleVectorPaths
void AddEndIndex(const IndexType &index)
Add an additional endpoint for multi-target shortest path search.
std::vector< IndexType > GetVectorPath()
Get the computed shortest path as a vector of pixel indices.
ShortestPathCostFunction< TInputImageType > CostFunctionType
void SetUseCostFunction(bool doUseCostFunction)
Enable or disable the custom cost function.
TOutputImageType::IndexType OutputImageIndexType
ImageRegionIteratorWithIndex< OutputImageType > OutputImageIteratorType
TInputImageType::IndexType NodeToCoord(NodeNumType)
SmartPointer< const Self > ConstPointer
itk::ShapedNeighborhoodIterator< TInputImageType > itkShapedNeighborhoodIteratorType
void PrintSelf(std::ostream &os, Indent indent) const override
Print object information to an output stream.
bool GetUseCostFunction()
Query whether the custom cost function is enabled.
ImageToImageFilter< TInputImageType, TOutputImageType > Superclass
TInputImageType::IndexType IndexType
TInputImageType::SizeType InputImageSizeType
void MakeShortestPathVector()
Trace back from end to start and fill m_VectorPath.
OutputImagePointer GetVectorOrderImage()
Get an image showing the pixel visit order.
bool CoordIsInBounds(IndexType)
void SetStartIndex(const IndexType &StartIndex)
Set the start pixel index for the shortest path computation.
std::vector< IndexType > m_endPointsClosed
std::vector< std::vector< IndexType > > GetMultipleVectorPaths()
Get multiple shortest paths for multi-endpoint searches.
void CleanUp()
Clean up allocated resources.
OutputImagePointer GetDistanceImage()
Get the distance image showing shortest distances from the start.
TOutputImageType::PixelType OutputImagePixelType
std::vector< ShortestPathNode * > GetNeighbors(NodeNumType nodeNum, bool FullNeighbors)
InputImageType::Pointer m_magnitudeImage
void operator=(const Self &)
void SetEndIndex(const IndexType &EndIndex)
Set the (single) endpoint for the shortest path computation.
itk::ImageRegionIteratorWithIndex< InputImageType > InputImageIteratorType
double getEstimatedCostsToTarget(const IndexType &a)
std::vector< NodeNumType > m_VectorOrder
void GenerateData() override
TInputImageType::PixelType InputImagePixelType
std::vector< ShortestPathNode * > m_Graph_DiscoveredNodeList
unsigned int CoordToNode(IndexType)
TOutputImageType::Pointer OutputImagePointer
A node in the shortest path graph used by ShortestPathImageFilter.
DistanceType distAndEst
Sum of distance and estimated remaining cost to the target (used by A*).
unsigned int NodeNumType
Type used for node indexing.
bool operator()(ShortestPathNode *a, ShortestPathNode *b)