]> Creatis software - FrontAlgorithms.git/blobdiff - lib/fpa/Base/Algorithm.h
...
[FrontAlgorithms.git] / lib / fpa / Base / Algorithm.h
index 11cd4475f7b85485f75fc777d66bcde49c90c5d3..1a2394c3e270ea3a931f3f5f3fec12ddaf1e6035 100644 (file)
@@ -1,9 +1,9 @@
-#ifndef __FPA__BASE__ALGORITHM__H__
-#define __FPA__BASE__ALGORITHM__H__
+#ifndef __fpa__Base__Algorithm__h__
+#define __fpa__Base__Algorithm__h__
 
-#include <map>
 #include <vector>
-#include <utility>
+#include <itkFunctionBase.h>
+#include <fpa/Config.h>
 #include <fpa/Base/Events.h>
 
 namespace fpa
@@ -11,146 +11,120 @@ namespace fpa
   namespace Base
   {
     /**
-     * Base front propagation algorithm. From a series of start seeds with
-     * costs, a priority queue is filled and emptied updating costs. Each
-     * vertex could be marked as "visited", "in the front", "not yet there"
-     * or "freezed".
-     *
-     * @param T Traits used for this algorithm
      */
-    template< class T, class B >
+    template< class _TFilter, class _TVertex, class _TOutput >
     class Algorithm
-      : public B
+      : public _TFilter
     {
     public:
-      // Standard class typdedefs
       typedef Algorithm                       Self;
-      typedef B                               Superclass;
+      typedef _TFilter                        Superclass;
       typedef itk::SmartPointer< Self >       Pointer;
       typedef itk::SmartPointer< const Self > ConstPointer;
 
-      /// Templated types
-      typedef T TTraits;
-      typedef B TBaseFilter;
-      typedef typename T::TCost        TCost;
-      typedef typename T::TResult      TResult;
-      typedef typename T::TVertex      TVertex;
-      typedef typename T::TVertexValue TVertexValue;
+      typedef _TVertex      TVertex;
+      typedef _TOutput      TOutput;
+      typedef unsigned long TFrontId;
 
-    protected:
-      typedef typename T::TFrontId   _TFrontId;
-      typedef typename T::TNode      _TNode;
-      typedef typename T::TNodes     _TNodes;
-      typedef typename T::TVertexCmp _TVertexCmp;
+      // Different functions
+      typedef std::vector< TVertex >                      TNeighborhood;
+      typedef itk::FunctionBase< TVertex, TNeighborhood > TNeighborhoodFunction;
 
-      typedef std::map< TVertex, _TNode, _TVertexCmp >   _TMarks;
+      // Minigraph to represent collisions
+      typedef std::pair< _TVertex, bool >   TCollision;
+      typedef std::vector< TCollision >     TCollisionsRow;
+      typedef std::vector< TCollisionsRow > TCollisions;
 
-      typedef std::pair< TVertex, bool >         _TCollision;
-      typedef std::vector< _TCollision >         _TCollisionSitesRow;
-      typedef std::vector< _TCollisionSitesRow > _TCollisionSites;
+    protected:
+      struct _TQueueNode
+      {
+        TVertex  Vertex;
+        TVertex  Parent;
+        TOutput  Result;
+        TFrontId FrontId;
+        _TQueueNode( );
+        _TQueueNode( const TVertex& v );
+        _TQueueNode( const TVertex& v, const _TQueueNode& n );
+      };
 
     public:
-      typedef BaseEvent< _TNode >          TEvent;
-      typedef FrontEvent< _TNode >         TFrontEvent;
-      typedef MarkEvent< _TNode >          TMarkEvent;
-      typedef CollisionEvent< _TNode >     TCollisionEvent;
-      typedef EndEvent< _TNode >           TEndEvent;
-      typedef BacktrackingEvent< TVertex > TBacktrackingEvent;
+      itkTypeMacro( Self, _TFilter );
 
-    public:
-      itkTypeMacro( Algorithm, itkProcessObject );
+      fpa_Base_NewEvent( TStartEvent );
+      fpa_Base_NewEvent( TEndEvent );
+      fpa_Base_NewEvent( TStartLoopEvent );
+      fpa_Base_NewEvent( TEndLoopEvent );
+      fpa_Base_NewEventWithVertex( TPushEvent, TVertex );
+      fpa_Base_NewEventWithVertex( TPopEvent, TVertex );
+      fpa_Base_NewEventWithVertex( TMarkEvent, TVertex );
 
-      itkBooleanMacro( StopAtOneFront );
-      itkBooleanMacro( ThrowEvents );
+      itkGetConstMacro( InitResult, _TOutput );
+      itkSetMacro( InitResult, _TOutput );
 
+      itkBooleanMacro( StopAtOneFront );
       itkGetConstMacro( StopAtOneFront, bool );
-      itkGetConstMacro( ThrowEvents, bool );
-
       itkSetMacro( StopAtOneFront, bool );
-      itkSetMacro( ThrowEvents, bool );
 
-    public:
-      virtual void InvokeEvent( const itk::EventObject& e );
-      virtual void InvokeEvent( const itk::EventObject& e ) const;
+      itkGetObjectMacro( NeighborhoodFunction, TNeighborhoodFunction );
+      itkSetObjectMacro( NeighborhoodFunction, TNeighborhoodFunction );
 
-      /// Seeds manipulation
-      void AddSeed( const TVertex& s, const TResult& v );
-      const TVertex& GetSeed( const unsigned long& i ) const;
+    public:
       void ClearSeeds( );
-      unsigned long GetNumberOfSeeds( ) const;
+      void AddSeed( const TVertex& seed, const TOutput& value );
 
     protected:
       Algorithm( );
       virtual ~Algorithm( );
 
-      /// itk::ProcessObject
-      virtual void GenerateData( );
-
-      /// Base interface
-      virtual void _BeforeMainLoop     ( );
-      virtual void _AfterMainLoop      ( );
-      virtual void _BeforeLoop         ( );
-      virtual void _AfterLoop          ( );
-      virtual void _Loop               ( );
-      virtual bool _CheckCollisions    ( const _TNode& a, const _TNode& b );
-      virtual bool _CheckStopCondition ( );
-      virtual bool _UpdateResult       ( _TNode& n );
-
-      /// Marks management
-      virtual      void _InitializeMarks ( );
-      virtual      bool _IsMarked        ( const TVertex& v ) const;
-      virtual _TFrontId _FrontId         ( const TVertex& v ) const;
-      virtual   TVertex _Parent          ( const TVertex& v ) const;
-      virtual      void _Mark            ( const _TNode& n );
-
-      /// Pure virtual interface: vertices
-      virtual unsigned long _NumberOfVertices ( ) const = 0;
-      virtual  TVertexValue _Value            ( const TVertex& v ) const = 0;
-      virtual       TResult _Result           ( const TVertex& v ) const = 0;
-
-      /// Pure virtual interface: edges
-      virtual double _Norm ( const TVertex& a, const TVertex& b ) const = 0;
-      virtual   bool _Edge ( const TVertex& a, const TVertex& b ) const = 0;
-      virtual  TCost _Cost ( const TVertex& a, const TVertex& b ) const = 0;
-
-      /// Pure virtual interface: neighborhood
-      virtual void _Neighs      ( const _TNode& n, _TNodes& N ) const = 0;
-      virtual bool _UpdateNeigh ( _TNode& nn, const _TNode& n ) = 0;
-      virtual void _NeighsInDim ( const _TNode& n,
-                                  const unsigned int& d,
-                                  _TNodes& N ) = 0;
-
-      /// Pure virtual interface: queue
-      virtual   void _InitializeQueue ( ) = 0;
-      virtual   bool _IsQueueEmpty    ( ) const = 0;
-      virtual   void _QueuePush       ( const _TNode& n ) = 0;
-      virtual _TNode _QueuePop        ( ) = 0;
-      virtual   void _QueueClear      ( ) = 0;
-
-      /// Pure virtual interface: results
-      virtual void _InitializeResults ( ) = 0;
+      virtual void GenerateData( ) override;
+
+      // Particular methods
+      virtual bool _ContinueGenerateData( );
+      virtual void _Loop( );
+
+      virtual void _BeforeGenerateData( );
+      virtual void _AfterGenerateData( );
+      virtual void _BeforeLoop( );
+      virtual void _AfterLoop( );
+
+      virtual bool _ValidLoop( ) const;
+      virtual void _UpdateCollisions( const TVertex& a, const TVertex& b );
+
+      virtual void _InitMarks( ) = 0;
+      virtual void _InitResults( const TOutput& init_value ) = 0;
+      virtual bool _IsMarked( const _TVertex& v ) const = 0;
+      virtual void _Mark( const _TQueueNode& n ) = 0;
+      virtual TFrontId _GetMark( const _TVertex& v ) const = 0;
+      virtual void _UpdateResult( const _TQueueNode& n ) = 0;
+
+      virtual bool _UpdateValue( _TQueueNode& v, const _TQueueNode& p ) = 0;
+      virtual unsigned long _QueueSize( ) const = 0;
+      virtual void _QueueClear( ) = 0;
+      virtual void _QueuePush( const _TQueueNode& node ) = 0;
+      virtual _TQueueNode _QueuePop( ) = 0;
 
     private:
-      // Purposely not implemented
-      Algorithm( const Self& );
-      void operator=( const Self& );
+      // Purposely not implemented.
+      Algorithm( const Self& other );
+      Self& operator=( const Self& other );
 
     protected:
+      _TOutput m_InitResult;
       bool m_StopAtOneFront;
-      bool m_ThrowEvents;
-
-      _TNodes m_Seeds;
-      _TMarks m_Marks;
-
-      _TCollisionSites m_CollisionSites;
+      typename TNeighborhoodFunction::Pointer m_NeighborhoodFunction;
+      std::vector< _TQueueNode > m_Seeds;
+      TCollisions m_Collisions;
+      unsigned int m_NumberOfFronts;
     };
 
-  } // ecapseman
+  } // ecaseman
 
-} // ecapseman
+} // ecaseman
 
-#include <fpa/Base/Algorithm.hxx>
+#ifndef ITK_MANUAL_INSTANTIATION
+#  include <fpa/Base/Algorithm.hxx>
+#endif // ITK_MANUAL_INSTANTIATION
 
-#endif // __FPA__BASE__ALGORITHM__H__
+#endif // __fpa__Base__Algorithm__h__
 
 // eof - $RCSfile$