KWStyle - itkFiniteDifferenceFunction.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkFiniteDifferenceFunction.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:35 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 DEF #ifndef __itkFiniteDifferenceFunction_h_
18 DEF #define __itkFiniteDifferenceFunction_h_
19
20 #include "itkLightObject.h"
21 #include "itkConstNeighborhoodIterator.h"
22 #include "itkZeroFluxNeumannBoundaryCondition.h"
23 #include "itkVector.h"
24 #include "itkFixedArray.h"
25
26 namespace itk {
27
28 /**
29  * \class FiniteDifferenceFunction
30  *
31  * This class is a component object of the finite difference solver hierarchy
32  * (see itkFiniteDifferenceImageFilter).  It defines a generic interface for a
33  * function object that computes a single scalar value from a neighborhood of
34  * values.  Examples of the application of this class are the various flavors
35  * of AnisotropicDiffusionFunctions and LevelSetFunction objects.
36  *
37  * These functions calculate the incremental change at a pixel in the solution
38  * image from one iteration of the p.d.e. solver to the next.
39  * 
40  * \par
41  * Subclasses of FiniteDifferenceImageFilter (solvers) call the
42  * ComputeUpdate() method of this class to compute \f$ \Delta u^n_{\mathbf{i}}
43  * \f$ at each \f$ i \f$.  in \f$ u \f$.  Because the size of the time step for
44  * each iteration of the p.d.e. solution depends on the particular calculations
45  * done, this function object is also responsible for computing that time step
46  * (see ComputeGlobalTimeStep).
47  *
48  * \par How to use this class
49  * FiniteDifferenceFunction must be subclassed to add functionality for
50  * ComputeUpdate, ComputeGlobalTimeStep, and Get/ReleaseGlobalDataPointer.
51  *
52  * \par A note on thread safety.
53 template<class TImageType>
54  * The ComputeUpdate() methods of this filter are declared as const to enforce
55  * thread-safety during execution of FiniteDifferenceImageFilter solver
56  * algorithms.  The InitializeIteration() method is intended to provide a safe
57  * way to modify the state of the object between threaded calculations of
58  * solvers.
59  *
60  * \todo Possibly subclass this object from Function.  Stumbling blocks here
61  * are the specialized api of FiniteDifferenceFunction.
62  *
63  * \ingroup Functions */
64 template<class TImageType>
65 class ITK_EXPORT FiniteDifferenceFunction : public LightObject
66 {
67 public:
68   /** Standard class typedefs. */
69   typedef FiniteDifferenceFunction Self;
70 TDA   typedef LightObject Superclass;
71
72   typedef SmartPointer<Self> Pointer;
73 TDA   typedef SmartPointer<const Self> ConstPointer;
74
75   /** Run-time type information (and related methods) */
76   itkTypeMacro( FiniteDifferenceFunction, LightObject );
77
78   /** Extract some parameters from the image type */
79   typedef TImageType ImageType;
80 TDA   typedef typename ImageType::PixelType       PixelType;
81 TDA   typedef double PixelRealType;
82   
83   /** Save image dimension. */
84   itkStaticConstMacro(ImageDimension, unsigned int, ImageType::ImageDimension);
85
86   /** Define the TimeStepType to always be double. */
87   typedef double  TimeStepType;
88
89   /** The default boundary condition for finite difference
90    * functions that is used unless overridden in the Evaluate() method. */
91   typedef ZeroFluxNeumannBoundaryCondition<ImageType>
92 IND ****DefaultBoundaryConditionType;
93
94   /** Neighborhood radius type */
95   typedef typename ConstNeighborhoodIterator<TImageType>::RadiusType RadiusType;
96
97   /** The type of data structure that is passed to this function object
98    * to evaluate at a pixel that does not lie on a data set boundary. */
99 LEN   typedef ConstNeighborhoodIterator<TImageType, DefaultBoundaryConditionType> NeighborhoodType;
100
101   /** A floating point offset from an image grid location. Used for
102    * interpolation among grid values in a neighborhood. */
103   typedef Vector<float,itkGetStaticConstMacro(ImageDimension)> FloatOffsetType;
104   
105   /** This method allows the function to set its state before each iteration
106    *  of the finite difference solver (image filter) that uses it.  This is
107    *  a thread-safe time to manipulate the object's state.
108    *
109    * An example of how this can be used: the Anisotropic diffusion class of
110    * FiniteDifferenceFunctions use this method to pre-calculate an average
111    * gradient magnitude across the entire image region.  This value is set in
112    * the function object and used by the ComputeUpdate methods that are called
113    * at each pixel as a constant. */
114   virtual void InitializeIteration() {};
115
116   /** This method is called by a finite difference solver image filter at
117    * each pixel that does not lie on a data set boundary.  The width of the
118    * data set boundary is defined by the width of the neighborhood being
119    * evaluated.
120    *
121    * The neighborhood argument is the neighborhood data.
122    * The globalData argument is a pointer to a data structure that holds
123    * values that need to be persistent across calls to this function, but
124    * cannot be stored in the function object itself for thread-safety reasons.
125    * Examples are values needed to compute the time-step for an iteration
126    * of the solver.
127    * \sa InitializeIteration
128    * \sa ComputeGlobalTimeStep */
129   virtual PixelType  ComputeUpdate(const NeighborhoodType &neighborhood,
130                                    void *globalData,
131 LEN                                    const FloatOffsetType &offset = FloatOffsetType(0.0)) = 0;
132
133
134   /** Sets the radius of the neighborhood this FiniteDifferenceFunction
135    * needs to perform its calculations. */
136   void SetRadius(const RadiusType &r)
137     { m_Radius = r; }
138
139   /** Returns the radius of the neighborhood this FiniteDifferenceFunction
140    * needs to perform its calculations. */
141   const RadiusType &GetRadius() const
142     { return m_Radius; }
143
144   /** Set the ScaleCoefficients for the difference
145    * operators. The defaults a 1.0. These can be set to take the image
146    * spacing into account. */
147   void SetScaleCoefficients (PixelRealType vals[ImageDimension])
148     {
149 IND ******for (unsigned int i = 0; i < ImageDimension; i++)
150 IND ********{
151 IND ********m_ScaleCoefficients[i] = vals[i];
152 IND ********}
153     }
154
155   /** Computes the time step for an update given a global data structure.
156    * The data used in the computation may take different forms depending on
157    * the nature of the equations.  This global data cannot be kept in the
158    * instance of the equation object itself since the equation object must
159    * remain stateless for thread safety.  The global data is therefore managed
160    * for each thread by the finite difference solver filters. */
161   virtual TimeStepType ComputeGlobalTimeStep(void *GlobalData) const =0;
162
163   /** Returns a pointer to a global data structure that is passed to this
164    * object from the solver at each calculation.  The idea is that the
165    * solver holds the state of any global values needed to calculate the
166    * time step, while the equation object performs the actual calculations.
167    *
168    * The global data should also be initialized in this method.
169    * */
170   virtual void *GetGlobalDataPointer() const =0;
171
172   /** When the finite difference solver filter has finished using a global
173    * data pointer, it passes it to this method, which frees the memory.
174    *
175    * The solver cannot free the memory because it does not know the type
176    * to which the pointer points. */
177   virtual void ReleaseGlobalDataPointer(void *GlobalData) const =0;
178   
179 protected:
180   FiniteDifferenceFunction() 
181 IND **{
182     // initialize variables
183     m_Radius.Fill( 0 );
184     for (unsigned int i = 0; i < ImageDimension; i++)
185       {
186       m_ScaleCoefficients[i] = 1.0;
187       }
188 IND **}
189   ~FiniteDifferenceFunction() {}
190   void PrintSelf(std::ostream& os, Indent indent) const;
191
192   RadiusType m_Radius;
193   PixelRealType m_ScaleCoefficients[ImageDimension];
194
195 private:
196   FiniteDifferenceFunction(const Self&); //purposely not implemented
197   void operator=(const Self&); //purposely not implemented
198 };
199   
200 // end namespace itk
201
202 #ifndef ITK_MANUAL_INSTANTIATION
203 #include "itkFiniteDifferenceFunction.txx"
204 #endif
205
206 #endif
207

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:07PM
© Kitware Inc.