diff --git a/Modules/GraphAlgorithms/itkShortestPathCostFunction.cpp b/Modules/GraphAlgorithms/itkShortestPathCostFunction.cpp index f3e9896bd9..446f14f0ec 100644 --- a/Modules/GraphAlgorithms/itkShortestPathCostFunction.cpp +++ b/Modules/GraphAlgorithms/itkShortestPathCostFunction.cpp @@ -1,51 +1,51 @@ -#ifndef __itkShortestPathCostFunction_txx -#define __itkShortestPathCostFunction_txx +#ifndef __itkShortestPathCostFunction_cpp +#define __itkShortestPathCostFunction_cpp #include "itkShortestPathCostFunction.h" namespace itk { // Constructor template ShortestPathCostFunction ::ShortestPathCostFunction() { } template void ShortestPathCostFunction ::PrintSelf( std::ostream& os, Indent indent ) const { Superclass::PrintSelf(os,indent); } template void ShortestPathCostFunction:: SetStartIndex (const typename TInputImageType::IndexType &StartIndex) { for (unsigned int i=0;i void ShortestPathCostFunction:: SetEndIndex (const typename TInputImageType::IndexType &EndIndex) { for (unsigned int i=0;i #include namespace itk { // \brief this is a pure virtual superclass for all cost function for the itkShortestPathImageFilter template class MitkGraphAlgorithms_EXPORT ShortestPathCostFunction : public Object { public: /** Standard class typedefs. */ typedef ShortestPathCostFunction Self; typedef Object Superclass; typedef SmartPointer Pointer; typedef SmartPointer ConstPointer; typedef ShapedNeighborhoodIterator< TInputImageType > itkShapedNeighborhoodIteratorType; + + + /** Run-time type information (and related methods). */ itkTypeMacro(ShortestPathCostFunction, Object); /** Type definition for the input image. */ typedef TInputImageType ImageType; // More typdefs for convenience typedef typename TInputImageType::Pointer ImagePointer; typedef typename TInputImageType::ConstPointer ImageConstPointer; typedef typename TInputImageType::PixelType PixelType; typedef typename TInputImageType::IndexType IndexType; /** Set the input image. */ itkSetConstObjectMacro(Image,TInputImageType); // \brief calculates the costs for going from pixel1 to pixel2 virtual double GetCost( IndexType p1, IndexType p2) = 0; // \brief returns the minimal costs possible (needed for A*) virtual double GetMinCost() = 0; // \brief Initialize the metric virtual void Initialize () = 0; // \brief Set Starpoint for Path void SetStartIndex (const IndexType & StartIndex); // \brief Set Endpoint for Path void SetEndIndex(const IndexType & EndIndex); ShortestPathCostFunction(); protected: virtual ~ShortestPathCostFunction() {}; void PrintSelf(std::ostream& os, Indent indent) const; ImageConstPointer m_Image; IndexType m_StartIndex, m_EndIndex; private: ShortestPathCostFunction(const Self&); //purposely not implemented void operator=(const Self&); //purposely not implemented }; } // end namespace itk - - +#include "itkShortestPathCostFunction.cpp" #endif /* __itkShortestPathCostFunction_h */ diff --git a/Modules/GraphAlgorithms/itkShortestPathCostFunctionTbss.cpp b/Modules/GraphAlgorithms/itkShortestPathCostFunctionTbss.cpp index 41e6df7fab..2a3925d00e 100644 --- a/Modules/GraphAlgorithms/itkShortestPathCostFunctionTbss.cpp +++ b/Modules/GraphAlgorithms/itkShortestPathCostFunctionTbss.cpp @@ -1,74 +1,74 @@ -#ifndef __itkShortestPathCostFunctionTbss_txx -#define __itkShortestPathCostFunctionTbss_txx +#ifndef __itkShortestPathCostFunctionTbss_cpp +#define __itkShortestPathCostFunctionTbss_cpp #include "itkShortestPathCostFunctionTbss.h" #include namespace itk { // Constructor template ShortestPathCostFunctionTbss ::ShortestPathCostFunctionTbss() { } template double ShortestPathCostFunctionTbss ::GetCost(IndexType p1 ,IndexType p2) { // Very simple Metric: // The squared difference of both values is defined as cost double a,b,c; ConstIteratorType it( this->m_Image, this->m_Image->GetRequestedRegion()); it.SetIndex(p1); a = it.Get(); it.SetIndex(p2); b = it.Get(); if(this->m_Image->GetPixel(p2) < m_Threshold) { c = std::numeric_limits::max( ); } else { double dxSqt = (p1[0]-p2[0]) * (p1[0]-p2[0]);// * (p1[0]-p2[0]); double dySqt = (p1[1]-p2[1]) * (p1[1]-p2[1]); double dzSqt = (p1[2]-p2[2]) * (p1[2]-p2[2]); double weight = this->m_Image->GetPixel(p2); c = (dxSqt + dySqt + dzSqt) + 10000 * (1-weight); } return c; } template void ShortestPathCostFunctionTbss ::Initialize() { } template double ShortestPathCostFunctionTbss ::GetMinCost() { return 1; } } // end namespace itk #endif // __itkShortestPathCostFunctionSimple_txx diff --git a/Modules/GraphAlgorithms/itkShortestPathCostFunctionTbss.h b/Modules/GraphAlgorithms/itkShortestPathCostFunctionTbss.h index e26a3acd99..3c8298bb4d 100644 --- a/Modules/GraphAlgorithms/itkShortestPathCostFunctionTbss.h +++ b/Modules/GraphAlgorithms/itkShortestPathCostFunctionTbss.h @@ -1,73 +1,74 @@ #ifndef __itkShortestPathCostFunctionTbss_h #define __itkShortestPathCostFunctionTbss_h #include "itkObject.h" #include "itkObjectFactory.h" #include "itkShapedNeighborhoodIterator.h" #include "itkShortestPathCostFunction.h" // Superclass of Metrics #include #include - +#include namespace itk { template class MitkGraphAlgorithms_EXPORT ShortestPathCostFunctionTbss : public ShortestPathCostFunction { public: /** Standard class typedefs. */ typedef ShortestPathCostFunctionTbss Self; typedef ShortestPathCostFunction Superclass; typedef SmartPointer Pointer; typedef SmartPointer ConstPointer; typedef itk::ImageRegionConstIterator ConstIteratorType; typedef typename TInputImageType::IndexType IndexType; typedef itk::Image FloatImageType; /** Method for creation through the object factory. */ itkNewMacro(Self); /** Run-time type information (and related methods). */ - itkTypeMacro(ShortestPathCostFunctionTbss, Object); + itkTypeMacro(Self, Superclass); // \brief calculates the costs for going from p1 to p2 virtual double GetCost( IndexType p1, IndexType p2); // \brief Initialize the metric virtual void Initialize (); // \brief returns the minimal costs possible (needed for A*) virtual double GetMinCost(); - ShortestPathCostFunctionTbss(); + void SetThreshold(double t) { m_Threshold = t; } protected: + ShortestPathCostFunctionTbss(); virtual ~ShortestPathCostFunctionTbss() {}; double m_Threshold; private: }; } // end namespace itk - +#include "itkShortestPathCostFunctionTbss.cpp" #endif /* __itkShortestPathCostFunctionTbss_h */ diff --git a/Modules/GraphAlgorithms/itkShortestPathImageFilter.h b/Modules/GraphAlgorithms/itkShortestPathImageFilter.h index 55c6b3dd14..45319b2acf 100644 --- a/Modules/GraphAlgorithms/itkShortestPathImageFilter.h +++ b/Modules/GraphAlgorithms/itkShortestPathImageFilter.h @@ -1,213 +1,214 @@ #ifndef __itkShortestPathImageFilter_h_ #define __itkShortestPathImageFilter_h_ #include "itkImageToImageFilter.h" #include "itkShortestPathCostFunction.h" #include "itkShortestPathNode.h" #include #include +#include // ------- INFORMATION ---------- /// SET FUNCTIONS //void SetInput( ItkImage ) // Compulsory //void SetStartIndex (const IndexType & StartIndex); // Compulsory //void SetEndIndex(const IndexType & EndIndex); // Compulsory //void SetFullNeighborsMode(bool) // Optional (default=false), if false N4, if true N26 //void SetActivateTimeOut(bool) // Optional (default=false), for debug issues: after 30s algorithms terminates. You can have a look at the VectorOrderImage to see how far it came //void SetMakeOutputImage(bool) // Optional (default=true), Generate an outputimage of the path. You can also get the path directoy with GetVectorPath() //void SetCalcAllDistances(bool) // Optional (default=false), Calculate Distances over the whole image. CAREFUL, algorithm time extends a lot. Necessary for GetDistanceImage //void SetStoreVectorOrder(bool) // Optional (default=false), Stores in which order the pixels were checked. Necessary for GetVectorOrderImage //void AddEndIndex(const IndexType & EndIndex) //Optional. By calling this function you can add several endpoints! The algorithm will look for several shortest Pathes. From Start to all Endpoints. // /// GET FUNCTIONS //std::vector< itk::Index<3> > GetVectorPath(); // returns the shortest path as vector //std::vector< std::vector< itk::Index<3> > GetMultipleVectorPathe(); // returns a vector of shortest Pathes (which are vectors of points) //GetDistanceImage // Returns the distance image //GetVectorOrderIMage // Returns the Vector Order image // // EXAMPLE USE // pleae see qmitkmitralvalvesegmentation4dtee bundle namespace itk { template class MitkGraphAlgorithms_EXPORT ShortestPathImageFilter : public ImageToImageFilter { public: //Standard Typedefs typedef ShortestPathImageFilter Self; typedef ImageToImageFilter Superclass; typedef SmartPointer Pointer; typedef SmartPointer ConstPointer; // Typdefs for metric typedef ShortestPathCostFunction< TInputImageType > CostFunctionType; - typedef typename CostFunctionType::Pointer CostFunctionTypePointer; + typedef typename CostFunctionType::Pointer CostFunctionTypePointer; // More typdefs for convenience typedef TInputImageType InputImageType; typedef typename TInputImageType::Pointer InputImagePointer; typedef typename TInputImageType::PixelType InputImagePixelType; typedef typename TInputImageType::SizeType InputImageSizeType; typedef typename TInputImageType::IndexType IndexType; typedef typename itk::ImageRegionIteratorWithIndex< InputImageType > InputImageIteratorType; typedef TOutputImageType OutputImageType; typedef typename TOutputImageType::Pointer OutputImagePointer; typedef typename TOutputImageType::PixelType OutputImagePixelType; typedef typename TOutputImageType::IndexType OutputImageIndexType; typedef ImageRegionIteratorWithIndex< OutputImageType > OutputImageIteratorType; typedef itk::ShapedNeighborhoodIterator< TInputImageType > itkShapedNeighborhoodIteratorType; // New Macro for smartpointer instantiation - itkNewMacro(Self); + itkNewMacro(Self) // Run-time type information - itkTypeMacro(ShortestPathImageFilter, ImageToImageFilter); + itkTypeMacro(ShortestPathImageFilter, ImageToImageFilter) // Display void PrintSelf( std::ostream& os, Indent indent ) const; // Compare function for A_STAR struct CompareNodeStar { bool operator()(ShortestPathNode *a, ShortestPathNode *b) { return (a->distAndEst > b->distAndEst); } }; // \brief Set Starpoint for ShortestPath Calculation void SetStartIndex (const IndexType & StartIndex); // \brief Adds Endpoint for multiple ShortestPath Calculation void AddEndIndex (const IndexType & index); // \brief Set Endpoint for ShortestPath Calculation void SetEndIndex(const IndexType & EndIndex); // \brief Set FullNeighborsMode. false = no diagonal neighbors, in 2D this means N4 Neigborhood. true = would be N8 in 2D itkSetMacro (FullNeighborsMode, bool); itkGetMacro (FullNeighborsMode, bool); // \brief (default=true), Produce output image, which shows the shortest path. But you can also get the shortest Path directly as vector with the function GetVectorPath itkSetMacro (MakeOutputImage, bool); itkGetMacro (MakeOutputImage, bool); // \brief (default=false), Store an Vector of Order, so you can call getVectorOrderImage after update itkSetMacro (StoreVectorOrder, bool); itkGetMacro (StoreVectorOrder, bool); // \brief (default=false), // Calculate all Distances to all pixels, so you can call getDistanceImage after update (warning algo will take a long time) itkSetMacro (CalcAllDistances, bool); itkGetMacro (CalcAllDistances, bool); // \brief (default=false), for debug issues: after 30s algorithms terminates. You can have a look at the VectorOrderImage to see how far it came itkSetMacro (ActivateTimeOut, bool); itkGetMacro (ActivateTimeOut, bool); // \brief returns shortest Path as vector std::vector< itk::Index<3> > GetVectorPath(); // \brief returns Multiple shortest Paths. You can call this function, when u performed a multiple shortest path search (one start, several ends) std::vector< std::vector< itk::Index<3> > > GetMultipleVectorPaths(); // \brief returns the vector order image. It shows in which order the pixels were checked. good for debugging. Be sure to have m_StoreVectorOrder=true OutputImagePointer GetVectorOrderImage(); // \brief returns the distance image. It shows the distances from the startpoint to all other pixels. Be sure to have m_CalcAllDistances=true OutputImagePointer GetDistanceImage(); // \brief cleans up the filter void CleanUp(); itkSetObjectMacro( CostFunction, CostFunctionType ); // itkSetObjectMacro = set function that uses pointer as parameter itkGetObjectMacro( CostFunction, CostFunctionType ); protected: std::vector< IndexType > m_endPoints; // if you fill this vector, the algo will not rest until all endPoints have been reached std::vector< IndexType > m_endPointsClosed; ShortestPathNode* m_Nodes; // main list that contains all nodes NodeNumType m_Graph_NumberOfNodes; NodeNumType m_Graph_StartNode; NodeNumType m_Graph_EndNode; int m_ImageDimensions; bool m_Graph_fullNeighbors; std::vector m_Graph_DiscoveredNodeList; ShortestPathImageFilter(Self&); // intentionally not implemented void operator=(const Self&); // intentionally not implemented const static int BACKGROUND = 0; const static int FOREGROUND = 255; bool m_FullNeighborsMode; bool m_MakeOutputImage; bool m_StoreVectorOrder; // Store an Vector of Order, so you can call getVectorOrderImage after update bool m_CalcAllDistances; // Calculate all Distances, so you can call getDistanceImage after update (warning algo will take a long time) bool multipleEndPoints; bool m_ActivateTimeOut; // if true, then i search max. 30 secs. then abort CostFunctionTypePointer m_CostFunction; IndexType m_StartIndex, m_EndIndex; std::vector< itk::Index<3> > m_VectorPath; std::vector< std::vector< itk::Index<3> > > m_MultipleVectorPaths; std::vector< NodeNumType > m_VectorOrder; - ShortestPathImageFilter (); + ShortestPathImageFilter(); // \brief Fill m_VectorPath void MakeShortestPathVector(); // \brief Create all the outputs void MakeOutputs(); // \brief Generate Data void GenerateData(); // \brief gets the estimate costs from pixel a to target. double getEstimatedCostsToTarget(const IndexType & a); typename InputImageType::Pointer m_magnitudeImage; // \brief Convert a indexnumber of a node in m_Nodes to image coordinates typename TInputImageType::IndexType NodeToCoord(NodeNumType); // \brief Convert image coordinate to a indexnumber of a node in m_Nodes unsigned int CoordToNode(IndexType); // \brief Returns the neighbors of a node std::vector GetNeighbors(NodeNumType nodeNum, bool FullNeighbors); // \brief Check if coords are in bounds of image bool CoordIsInBounds(IndexType); // \brief Initializes the graph void InitGraph(); // \brief Start ShortestPathSearch void StartShortestPathSearch(); }; } // end of namespace itk - +#include "itkShortestPathImageFilter.cpp" #endif