| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkFiniteDifferenceSparseImageFilter.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 __itkFiniteDifferenceSparseImageFilter_h_ |
| 18 |
DEF |
#define __itkFiniteDifferenceSparseImageFilter_h_ |
| 19 |
|
|
| 20 |
|
#include "itkFiniteDifferenceSparseImageFunction.h" |
| 21 |
|
#include "itkFiniteDifferenceImageFilter.h" |
| 22 |
|
#include "itkMultiThreader.h" |
| 23 |
|
#include "itkSparseImage.h" |
| 24 |
|
|
| 25 |
|
namespace itk { |
| 26 |
|
|
| 27 |
|
/** |
| 28 |
|
* \class FiniteDifferenceSparseImageFilter |
| 29 |
|
* |
| 30 |
|
* \brief This class implements a multi-threaded base class for Image to |
| 31 |
|
* SparseImage finite difference processes. |
| 32 |
|
* |
| 33 |
|
* \par |
| 34 |
|
* This class implements a multi-threading mechanism for implementing finite |
| 35 |
|
* difference PDE's on sparse image types. The sparse image is a image of |
| 36 |
|
* pointers to node variables at valid pixel locations and null pointers at |
| 37 |
|
* others. The node variable type must have the following members: m_Index, |
| 38 |
|
* m_Data and m_Update. |
| 39 |
|
* |
| 40 |
|
* \par |
| 41 |
|
* This class also adds precomputing support to the finite difference image |
| 42 |
|
* filter scheme. This support can be used by certain filters to speed up the |
| 43 |
|
* processing. The m_PrecomputeFlag should be set to true to use this and the |
| 44 |
|
* Function object must provide a PrecomputeSparseUpdate method. |
| 45 |
|
* |
| 46 |
|
* \par INPUTS |
| 47 |
|
* The input to this filter is either a regular or sparse image. Subclasses |
| 48 |
|
* should provide a way of copying this information to the output sparse image |
| 49 |
|
* or initializing the output image nodes from the input image. |
| 50 |
|
* |
| 51 |
|
* \par OUTPUTS |
| 52 |
|
* The output is a sparse image. The output will be in the m_Data members of |
| 53 |
|
* the nodes of the sparse image. |
| 54 |
|
* |
| 55 |
|
* \par IMPORTANT |
| 56 |
|
* The output sparse image type must be templated with a node type that at |
| 57 |
|
* least has the following member variables: m_Index, m_Data and m_Update. |
| 58 |
|
*/ |
| 59 |
|
|
| 60 |
|
template <class TInputImageType, class TSparseOutputImageType> |
| 61 |
|
class FiniteDifferenceSparseImageFilter |
| 62 |
IND |
**: public FiniteDifferenceImageFilter <TInputImageType, |
| 63 |
|
TSparseOutputImageType> |
| 64 |
|
{ |
| 65 |
|
public: |
| 66 |
|
/** Standard class typedef */ |
| 67 |
|
typedef FiniteDifferenceSparseImageFilter Self; |
| 68 |
|
typedef FiniteDifferenceImageFilter<TInputImageType, |
| 69 |
TDA |
TSparseOutputImageType> Superclass; |
| 70 |
TDA |
typedef SmartPointer<Self> Pointer; |
| 71 |
TDA |
typedef SmartPointer<const Self> ConstPointer; |
| 72 |
|
|
| 73 |
|
/** Run-time type information (and related methods) */ |
| 74 |
|
itkTypeMacro(FiniteDifferenceSparseImageFilter, FiniteDifferenceImageFilter); |
| 75 |
|
|
| 76 |
|
/**Typedefs from the superclass */ |
| 77 |
|
typedef typename Superclass::InputImageType InputImageType; |
| 78 |
|
typedef typename Superclass::OutputImageType SparseOutputImageType; |
| 79 |
|
typedef typename Superclass::PixelType PixelType; |
| 80 |
|
typedef typename Superclass::TimeStepType TimeStepType; |
| 81 |
|
typedef typename Superclass::FiniteDifferenceFunctionType |
| 82 |
TDA |
FiniteDifferenceFunctionType; |
| 83 |
|
// the PixelType is from output image; therefore, it is a pointer |
| 84 |
|
|
| 85 |
|
/** Dimensionality of input and output data is assumed to be the same. |
| 86 |
|
* It is inherited from the superclass. */ |
| 87 |
|
itkStaticConstMacro(ImageDimension, unsigned int,Superclass::ImageDimension); |
| 88 |
|
|
| 89 |
|
/** Typedefs from the sparse output image type. */ |
| 90 |
|
typedef typename SparseOutputImageType::IndexType IndexType; |
| 91 |
|
typedef typename SparseOutputImageType::NodeType OutputNodeType; |
| 92 |
|
typedef typename SparseOutputImageType::NodeListType NodeListType; |
| 93 |
|
|
| 94 |
|
/** The type for the data variable of OutputNodeType. */ |
| 95 |
|
typedef typename OutputNodeType::NodeDataType NodeDataType; |
| 96 |
|
|
| 97 |
|
/** The basic scalar variable type used in OutputNodeType. Expected to be |
| 98 |
|
* float or double. If NodeDataType is a scalar, then this is the same type as |
| 99 |
|
* that. */ |
| 100 |
|
typedef typename OutputNodeType::NodeValueType NodeValueType; |
| 101 |
|
|
| 102 |
|
/** The sparse image finite difference function type used in this class. */ |
| 103 |
|
typedef FiniteDifferenceSparseImageFunction <SparseOutputImageType> |
| 104 |
|
SparseFunctionType; |
| 105 |
|
|
| 106 |
|
/** Sets the function object that will be called for computing updates. */ |
| 107 |
|
void SetSparseFunction( SparseFunctionType *sf ); |
| 108 |
|
|
| 109 |
|
itkSetMacro(PrecomputeFlag, bool); |
| 110 |
|
itkGetMacro(PrecomputeFlag, bool); |
| 111 |
|
|
| 112 |
|
protected: |
| 113 |
|
FiniteDifferenceSparseImageFilter(); |
| 114 |
|
~FiniteDifferenceSparseImageFilter() {} |
| 115 |
|
void PrintSelf(std::ostream& os, Indent indent) const; |
| 116 |
|
|
| 117 |
|
/** This method splits the active pixels of the sparse image into equal size |
| 118 |
|
* lists for multi-threading. These lists remain constant throughout the |
| 119 |
|
* operation of this filter.*/ |
| 120 |
|
virtual void Initialize(); |
| 121 |
|
|
| 122 |
|
/** This class does not use AllocateUpdateBuffer to allocate memory for its |
| 123 |
|
* narrow band. All memory is handled through the SparseImage class. */ |
| 124 |
|
virtual void AllocateUpdateBuffer() {}; |
| 125 |
|
|
| 126 |
|
/** This function can be used to implements constraints on the range of data |
| 127 |
|
* values. Default is no constraint. */ |
| 128 |
|
virtual NodeDataType DataConstraint( const NodeDataType &data ) const |
| 129 |
IND |
**{ return data; } |
| 130 |
|
|
| 131 |
|
private: |
| 132 |
|
/** The type of region used in multithreading. */ |
| 133 |
|
struct ThreadRegionType |
| 134 |
IND |
**{ |
| 135 |
|
// this is the first element |
| 136 |
|
typename NodeListType::Iterator first; |
| 137 |
|
// this is one past the last element |
| 138 |
|
typename NodeListType::Iterator last; |
| 139 |
IND |
**}; |
| 140 |
|
|
| 141 |
|
protected: |
| 142 |
|
/** This function returns a single region for use in multi-threading. */ |
| 143 |
|
int GetSplitRegion( int i, int num, ThreadRegionType &splitRegion ); |
| 144 |
|
|
| 145 |
|
/** This function updates the m_Data variable in the output image nodes using |
| 146 |
IND |
******the update values computed by CalculateChange. */ |
| 147 |
|
virtual void ApplyUpdate( TimeStepType dt ); |
| 148 |
|
|
| 149 |
|
/** Multi-threaded implementation of ApplyUpdate. */ |
| 150 |
|
static ITK_THREAD_RETURN_TYPE ApplyUpdateThreaderCallback( void *arg ); |
| 151 |
|
virtual void ThreadedApplyUpdate(TimeStepType dt, |
| 152 |
|
const ThreadRegionType ®ionToProcess, |
| 153 |
|
int threadId); |
| 154 |
|
|
| 155 |
|
/** This method computes changes to the output image using the |
| 156 |
IND |
******ComputeSparseUpdate method in the Sparse Function object. */ |
| 157 |
|
virtual TimeStepType CalculateChange(); |
| 158 |
|
|
| 159 |
|
/** Multuthreaded implementation of CalculateChange */ |
| 160 |
|
static ITK_THREAD_RETURN_TYPE CalculateChangeThreaderCallback( void *arg ); |
| 161 |
|
virtual TimeStepType ThreadedCalculateChange |
| 162 |
|
(const ThreadRegionType ®ionToProcess, int threadId); |
| 163 |
|
|
| 164 |
|
/** This method provides a means of performing a first pass for computing the |
| 165 |
|
* change and storing intermediate values that will then be used by |
| 166 |
|
* CalculateChange. This can be used to speed up certain update rules. */ |
| 167 |
|
virtual void PrecalculateChange(); |
| 168 |
|
|
| 169 |
|
/** Multithreaded implementation of PrecalculateChange */ |
| 170 |
|
static ITK_THREAD_RETURN_TYPE PrecalculateChangeThreaderCallback( void *arg ); |
| 171 |
|
virtual void ThreadedPrecalculateChange |
| 172 |
|
(const ThreadRegionType ®ionToProcess, int threadId); |
| 173 |
|
|
| 174 |
|
/** Structure for passing information into static callback methods. |
| 175 |
|
* Used in the subclasses' threading mechanisms. */ |
| 176 |
|
struct FDThreadStruct |
| 177 |
IND |
**{ |
| 178 |
|
FiniteDifferenceSparseImageFilter *Filter; |
| 179 |
|
TimeStepType TimeStep; |
| 180 |
|
TimeStepType *TimeStepList; |
| 181 |
|
bool *ValidTimeStepList; |
| 182 |
IND |
**}; |
| 183 |
|
|
| 184 |
|
private: |
| 185 |
|
/** Flag to let the class know whether or not to call PrecalculateChange. */ |
| 186 |
|
bool m_PrecomputeFlag; |
| 187 |
|
|
| 188 |
|
/** The Sparse function type. */ |
| 189 |
|
SparseFunctionType *m_SparseFunction; |
| 190 |
|
|
| 191 |
|
/** A list of subregions of the active set of pixels in the sparse image |
| 192 |
IND |
******which are passed to each thread for parallel processing. */ |
| 193 |
|
typename NodeListType::RegionListType m_RegionList; |
| 194 |
|
|
| 195 |
|
FiniteDifferenceSparseImageFilter(const Self&); //purposely not implemented |
| 196 |
|
void operator=(const Self&); //purposely not implemented |
| 197 |
|
}; |
| 198 |
|
|
| 199 |
|
} // end namespace itk |
| 200 |
|
|
| 201 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 202 |
|
#include "itkFiniteDifferenceSparseImageFilter.txx" |
| 203 |
|
#endif |
| 204 |
|
|
| 205 |
|
#endif |
| 206 |
|
|