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