20 #ifndef __itkImageSource_txx
21 #define __itkImageSource_txx
24 #include "vnl/vnl_math.h"
32 template<
class TOutputImage>
38 typename TOutputImage::Pointer output
39 =
static_cast<TOutputImage*
>(this->MakeOutput(0).GetPointer());
46 this->ReleaseDataBeforeUpdateFlagOff();
52 template<
class TOutputImage>
57 return static_cast<DataObject*
>(TOutputImage::New().GetPointer());
63 template<
class TOutputImage>
68 if (this->GetNumberOfOutputs() < 1)
74 return static_cast<TOutputImage*
>
82 template<
class TOutputImage>
87 TOutputImage* out =
dynamic_cast<TOutputImage*
>
90 itkWarningMacro ( <<
"dynamic_cast to output type failed" );
98 template<
class TOutputImage>
103 this->GraftNthOutput(0, graft);
110 template<
class TOutputImage>
115 if ( idx >= this->GetNumberOfOutputs() )
117 itkExceptionMacro(<<
"Requested to graft output " << idx <<
118 " but this filter only has " << this->GetNumberOfOutputs() <<
" Outputs.");
123 itkExceptionMacro(<<
"Requested to graft output that is a NULL pointer" );
131 output->
Graft( graft );
135 template <
class TOutputImage>
142 const typename TOutputImage::SizeType& requestedRegionSize
143 = outputPtr->GetRequestedRegion().GetSize();
146 typename TOutputImage::IndexType splitIndex;
147 typename TOutputImage::SizeType splitSize;
150 splitRegion = outputPtr->GetRequestedRegion();
151 splitIndex = splitRegion.GetIndex();
152 splitSize = splitRegion.GetSize();
155 splitAxis = outputPtr->GetImageDimension() - 1;
156 while (requestedRegionSize[splitAxis] == 1)
161 itkDebugMacro(
" Cannot Split");
167 typename TOutputImage::SizeType::SizeValueType range = requestedRegionSize[splitAxis];
168 int valuesPerThread = Math::Ceil<int>(range/(double)num);
169 int maxThreadIdUsed = Math::Ceil<int>(range/(double)valuesPerThread) - 1;
172 if (i < maxThreadIdUsed)
174 splitIndex[splitAxis] += i*valuesPerThread;
175 splitSize[splitAxis] = valuesPerThread;
177 if (i == maxThreadIdUsed)
179 splitIndex[splitAxis] += i*valuesPerThread;
181 splitSize[splitAxis] = splitSize[splitAxis] - i*valuesPerThread;
185 splitRegion.SetIndex( splitIndex );
186 splitRegion.SetSize( splitSize );
188 itkDebugMacro(
" Split Piece: " << splitRegion );
190 return maxThreadIdUsed + 1;
194 template <
class TOutputImage>
200 typename ImageBaseType::Pointer outputPtr;
203 for (
unsigned int i=0; i < this->GetNumberOfOutputs(); i++)
216 outputPtr->Allocate();
222 template <
class TOutputImage>
229 this->AllocateOutputs();
234 this->BeforeThreadedGenerateData();
240 this->GetMultiThreader()->SetNumberOfThreads(this->GetNumberOfThreads());
241 this->GetMultiThreader()->SetSingleMethod(this->ThreaderCallback, &str);
244 this->GetMultiThreader()->SingleMethodExecute();
248 this->AfterThreadedGenerateData();
255 template <
class TOutputImage>
265 OStringStream message;
266 message <<
"itk::ERROR: " << this->GetNameOfClass()
267 <<
"(" <<
this <<
"): " <<
"Subclass should override this method!!!";
268 ExceptionObject e_(__FILE__, __LINE__, message.str().c_str(),ITK_LOCATION);
275 template <
class TOutputImage>
281 int total, threadId, threadCount;
290 typename TOutputImage::RegionType splitRegion;
291 total = str->
Filter->SplitRequestedRegion(threadId, threadCount,
294 if (threadId < total)
296 str->
Filter->ThreadedGenerateData(splitRegion, threadId);