]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Base/Algorithm.h
662bdf1434273c7866abd12a18e073b074b0d08a
[FrontAlgorithms.git] / lib / fpa / Base / Algorithm.h
1 #ifndef __fpa__Base__Algorithm__h__
2 #define __fpa__Base__Algorithm__h__
3
4 #include <vector>
5 #include <itkFunctionBase.h>
6 #include <fpa/Config.h>
7 #include <fpa/Base/Events.h>
8
9 namespace fpa
10 {
11   namespace Base
12   {
13     /**
14      */
15     template< class _TFilter, class _TVertex, class _TOutput >
16     class Algorithm
17       : public _TFilter
18     {
19     public:
20       typedef Algorithm                       Self;
21       typedef _TFilter                        Superclass;
22       typedef itk::SmartPointer< Self >       Pointer;
23       typedef itk::SmartPointer< const Self > ConstPointer;
24
25       typedef _TVertex      TVertex;
26       typedef _TOutput      TOutput;
27       typedef unsigned long TFrontId;
28
29       // Different functions
30       typedef std::vector< TVertex >                      TNeighborhood;
31       typedef itk::FunctionBase< TVertex, TNeighborhood > TNeighborhoodFunction;
32
33       // Minigraph to represent collisions
34       typedef std::pair< _TVertex, bool >   TCollision;
35       typedef std::vector< TCollision >     TCollisionsRow;
36       typedef std::vector< TCollisionsRow > TCollisions;
37
38     protected:
39       struct _TQueueNode
40       {
41         TVertex  Vertex;
42         TVertex  Parent;
43         TOutput  Result;
44         TFrontId FrontId;
45         _TQueueNode( );
46         _TQueueNode( const TVertex& v );
47         _TQueueNode( const TVertex& v, const _TQueueNode& n );
48       };
49
50     public:
51       itkTypeMacro( Self, _TFilter );
52
53       fpa_Base_NewEvent( TStartEvent );
54       fpa_Base_NewEvent( TEndEvent );
55       fpa_Base_NewEvent( TStartLoopEvent );
56       fpa_Base_NewEvent( TEndLoopEvent );
57       fpa_Base_NewEventWithVertex( TPushEvent, TVertex );
58       fpa_Base_NewEventWithVertex( TPopEvent, TVertex );
59       fpa_Base_NewEventWithVertex( TMarkEvent, TVertex );
60
61       itkGetConstMacro( InitResult, _TOutput );
62       itkSetMacro( InitResult, _TOutput );
63
64       itkBooleanMacro( StopAtOneFront );
65       itkGetConstMacro( StopAtOneFront, bool );
66       itkSetMacro( StopAtOneFront, bool );
67
68       itkGetObjectMacro( NeighborhoodFunction, TNeighborhoodFunction );
69       itkSetObjectMacro( NeighborhoodFunction, TNeighborhoodFunction );
70
71     public:
72       void AddSeed( const TVertex& seed, const TOutput& value );
73
74     protected:
75       Algorithm( );
76       virtual ~Algorithm( );
77
78       virtual void GenerateData( ) fpa_OVERRIDE;
79
80       // Particular methods
81       virtual void _Loop( );
82
83       virtual void _BeforeGenerateData( );
84       virtual void _AfterGenerateData( );
85       virtual void _BeforeLoop( );
86       virtual void _AfterLoop( );
87
88       virtual bool _ValidLoop( ) const;
89       virtual void _UpdateCollisions( const TVertex& a, const TVertex& b );
90
91       virtual void _InitMarks( ) = 0;
92       virtual void _InitResults( const TOutput& init_value ) = 0;
93       virtual bool _IsMarked( const _TVertex& v ) const = 0;
94       virtual void _Mark( const _TQueueNode& n ) = 0;
95       virtual TFrontId _GetMark( const _TVertex& v ) const = 0;
96       virtual void _UpdateResult( const _TQueueNode& n ) = 0;
97
98       virtual bool _UpdateValue( _TQueueNode& v, const _TQueueNode& p ) = 0;
99       virtual unsigned long _QueueSize( ) const = 0;
100       virtual void _QueueClear( ) = 0;
101       virtual void _QueuePush( const _TQueueNode& node ) = 0;
102       virtual _TQueueNode _QueuePop( ) = 0;
103
104     private:
105       // Purposely not implemented.
106       Algorithm( const Self& other );
107       Self& operator=( const Self& other );
108
109     protected:
110       _TOutput m_InitResult;
111       bool m_StopAtOneFront;
112       typename TNeighborhoodFunction::Pointer m_NeighborhoodFunction;
113       std::vector< _TQueueNode > m_Seeds;
114       TCollisions m_Collisions;
115       unsigned int m_NumberOfFronts;
116     };
117
118   } // ecaseman
119
120 } // ecaseman
121
122 #ifndef ITK_MANUAL_INSTANTIATION
123 #  include <fpa/Base/Algorithm.hxx>
124 #endif // ITK_MANUAL_INSTANTIATION
125
126 #endif // __fpa__Base__Algorithm__h__
127
128 // eof - $RCSfile$