1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkDenseFiniteDifferenceImageFilter.txx,v $
Language: C++
Date: $Date: 2006-10-27 14:52:31 $
Version: $Revision: 1.29 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __itkDenseFiniteDifferenceImageFilter_txx_
#define __itkDenseFiniteDifferenceImageFilter_txx_
#include "itkDenseFiniteDifferenceImageFilter.h"
#include <list>
#include "itkImageRegionConstIterator.h"
#include "itkImageRegionIterator.h"
#include "itkNumericTraits.h"
#include "itkNeighborhoodAlgorithm.h"
namespace itk {
template <class TInputImage, class TOutputImage>
void
DenseFiniteDifferenceImageFilter<TInputImage, TOutputImage>
::CopyInputToOutput()
{
typename TInputImage::ConstPointer input = this->GetInput();
typename TOutputImage::Pointer output = this->GetOutput();
if ( !input || !output )
{
itkExceptionMacro(<< "Either input and/or output is NULL.");
}
// Check if we are doing in-place filtering
if ( this->GetInPlace() && (typeid(TInputImage) == typeid(TOutputImage)) )
{
typename TInputImage::Pointer tempPtr =
dynamic_cast<TInputImage *>( output.GetPointer() );
if ( tempPtr && tempPtr->GetPixelContainer() == input->GetPixelContainer() )
{
// the input and output container are the same - no need to copy
return;
}
}
ImageRegionConstIterator<TInputImage> in(input, output->GetRequestedRegion());
ImageRegionIterator<TOutputImage> out(output, output->GetRequestedRegion());
while( ! out.IsAtEnd() )
{
out.Value() = static_cast<PixelType>(in.Get()); // Supports input image adaptors only
++in;
++out;
}
}
template <class TInputImage, class TOutputImage>
void
DenseFiniteDifferenceImageFilter<TInputImage, TOutputImage>
::AllocateUpdateBuffer()
{
// The update buffer looks just like the output.
typename TOutputImage::Pointer output = this->GetOutput();
m_UpdateBuffer->SetSpacing(output->GetSpacing());
m_UpdateBuffer->SetOrigin(output->GetOrigin());
m_UpdateBuffer->SetLargestPossibleRegion(output->GetLargestPossibleRegion());
m_UpdateBuffer->SetRequestedRegion(output->GetRequestedRegion());
m_UpdateBuffer->SetBufferedRegion(output->GetBufferedRegion());
m_UpdateBuffer->Allocate();
}
template<class TInputImage, class TOutputImage>
void
DenseFiniteDifferenceImageFilter<TInputImage, TOutputImage>
::ApplyUpdate(TimeStepType dt)
{
// Set up for multithreaded processing.
DenseFDThreadStruct str;
str.Filter = this;
str.TimeStep = dt;
this->GetMultiThreader()->SetNumberOfThreads(this->GetNumberOfThreads());
this->GetMultiThreader()->SetSingleMethod(this->ApplyUpdateThreaderCallback,
&str);
// Multithread the execution
this->GetMultiThreader()->SingleMethodExecute();
}
template<class TInputImage, class TOutputImage>
ITK_THREAD_RETURN_TYPE
DenseFiniteDifferenceImageFilter<TInputImage, TOutputImage>
::ApplyUpdateThreaderCallback( void * arg )
{
DenseFDThreadStruct * str;
int total, threadId, threadCount;
threadId = ((MultiThreader::ThreadInfoStruct *)(arg))->ThreadID;
threadCount = ((MultiThreader::ThreadInfoStruct *)(arg))->NumberOfThreads;
str = (DenseFDThreadStruct *)(((MultiThreader::ThreadInfoStruct *)(arg))->UserData);
// Execute the actual method with appropriate output region
// first find out how many pieces extent can be split into.
// Using the SplitRequestedRegion method from itk::ImageSource.
ThreadRegionType splitRegion;
total = str->Filter->SplitRequestedRegion(threadId, threadCount,
splitRegion);
if (threadId < total)
{
str->Filter->ThreadedApplyUpdate(str->TimeStep, splitRegion, threadId);
}
return ITK_THREAD_RETURN_VALUE;
}
template <class TInputImage, class TOutputImage>
typename
DenseFiniteDifferenceImageFilter<TInputImage, TOutputImage>::TimeStepType
DenseFiniteDifferenceImageFilter<TInputImage, TOutputImage>
::CalculateChange()
{
int threadCount;
TimeStepType dt;
// Set up for multithreaded processing.
DenseFDThreadStruct str;
str.Filter = this;
str.TimeStep = NumericTraits<TimeStepType>::Zero; // Not used during the
// calculate change step.
this->GetMultiThreader()->SetNumberOfThreads(this->GetNumberOfThreads());
this->GetMultiThreader()->SetSingleMethod(this->CalculateChangeThreaderCallback,
&str);
// Initialize the list of time step values that will be generated by the
// various threads. There is one distinct slot for each possible thread,
// so this data structure is thread-safe.
threadCount = this->GetMultiThreader()->GetNumberOfThreads();
str.TimeStepList = new TimeStepType[threadCount];
str.ValidTimeStepList = new bool[threadCount];
for (int i =0; i < threadCount; ++i)
{ str.ValidTimeStepList[i] = false; }
// Multithread the execution
this->GetMultiThreader()->SingleMethodExecute();
// Resolve the single value time step to return
dt = this->ResolveTimeStep(str.TimeStepList, str.ValidTimeStepList, threadCount);
delete [] str.TimeStepList;
delete [] str.ValidTimeStepList;
return dt;
}
template <class TInputImage, class TOutputImage>
ITK_THREAD_RETURN_TYPE
DenseFiniteDifferenceImageFilter<TInputImage, TOutputImage>
::CalculateChangeThreaderCallback( void * arg )
{
DenseFDThreadStruct * str;
int total, threadId, threadCount;
threadId = ((MultiThreader::ThreadInfoStruct *)(arg))->ThreadID;
threadCount = ((MultiThreader::ThreadInfoStruct *)(arg))->NumberOfThreads;
str = (DenseFDThreadStruct *)(((MultiThreader::ThreadInfoStruct *)(arg))->UserData);
// Execute the actual method with appropriate output region
// first find out how many pieces extent can be split into.
// Using the SplitRequestedRegion method from itk::ImageSource.
ThreadRegionType splitRegion;
total = str->Filter->SplitRequestedRegion(threadId, threadCount,
splitRegion);
if (threadId < total)
{
str->TimeStepList[threadId]
= str->Filter->ThreadedCalculateChange(splitRegion, threadId);
str->ValidTimeStepList[threadId] = true;
}
return ITK_THREAD_RETURN_VALUE;
}
template <class TInputImage, class TOutputImage>
void
DenseFiniteDifferenceImageFilter<TInputImage, TOutputImage>
::ThreadedApplyUpdate(TimeStepType dt, const ThreadRegionType ®ionToProcess,
int)
{
ImageRegionIterator<UpdateBufferType> u(m_UpdateBuffer, regionToProcess);
ImageRegionIterator<OutputImageType> o(this->GetOutput(), regionToProcess);
u = u.Begin();
o = o.Begin();
while ( !u.IsAtEnd() )
{
o.Value() += static_cast<PixelType>(u.Value() * dt); // no adaptor support here
++o;
++u;
}
}
template <class TInputImage, class TOutputImage>
typename
DenseFiniteDifferenceImageFilter<TInputImage, TOutputImage>::TimeStepType
DenseFiniteDifferenceImageFilter<TInputImage, TOutputImage>
::ThreadedCalculateChange(const ThreadRegionType ®ionToProcess, int)
{
typedef typename OutputImageType::RegionType RegionType;
typedef typename OutputImageType::SizeType SizeType;
typedef typename OutputImageType::SizeValueType SizeValueType;
typedef typename OutputImageType::IndexType IndexType;
typedef typename OutputImageType::IndexValueType IndexValueType;
typedef typename FiniteDifferenceFunctionType::NeighborhoodType
NeighborhoodIteratorType;
typedef ImageRegionIterator<UpdateBufferType> UpdateIteratorType;
typename OutputImageType::Pointer output = this->GetOutput();
TimeStepType timeStep;
void *globalData;
// Get the FiniteDifferenceFunction to use in calculations.
const typename FiniteDifferenceFunctionType::Pointer df
= this->GetDifferenceFunction();
const SizeType radius = df->GetRadius();
// Break the input into a series of regions. The first region is free
// of boundary conditions, the rest with boundary conditions. We operate
// on the output region because input has been copied to output.
typedef NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<OutputImageType>
FaceCalculatorType;
typedef typename FaceCalculatorType::FaceListType FaceListType;
FaceCalculatorType faceCalculator;
FaceListType faceList = faceCalculator(output, regionToProcess, radius);
typename FaceListType::iterator fIt = faceList.begin();
// Ask the function object for a pointer to a data structure it
// will use to manage any global values it needs. We'll pass this
// back to the function object at each calculation and then
// again so that the function object can use it to determine a
// time step for this iteration.
globalData = df->GetGlobalDataPointer();
// Process the non-boundary region.
NeighborhoodIteratorType nD(radius, output, *fIt);
UpdateIteratorType nU(m_UpdateBuffer, *fIt);
nD.GoToBegin();
while( !nD.IsAtEnd() )
{
nU.Value() = df->ComputeUpdate(nD, globalData);
++nD;
++nU;
}
// Process each of the boundary faces.
NeighborhoodIteratorType bD;
UpdateIteratorType bU;
for (++fIt; fIt != faceList.end(); ++fIt)
{
bD = NeighborhoodIteratorType(radius, output, *fIt);
bU = UpdateIteratorType (m_UpdateBuffer, *fIt);
bD.GoToBegin();
bU.GoToBegin();
while ( !bD.IsAtEnd() )
{
bU.Value() = df->ComputeUpdate(bD, globalData);
++bD;
++bU;
}
}
// Ask the finite difference function to compute the time step for
// this iteration. We give it the global data pointer to use, then
// ask it to free the global data memory.
timeStep = df->ComputeGlobalTimeStep(globalData);
df->ReleaseGlobalDataPointer(globalData);
return timeStep;
}
template <class TInputImage, class TOutputImage>
void
DenseFiniteDifferenceImageFilter<TInputImage, TOutputImage>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
}
}// end namespace itk
#endif
|