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
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkGridImageSource_hxx
#define itkGridImageSource_hxx
#include "itkGaussianKernelFunction.h"
#include "itkImageLinearIteratorWithIndex.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkTotalProgressReporter.h"
#include <algorithm> // For min.
namespace itk
{
template <typename TOutputImage>
GridImageSource<TOutputImage>::GridImageSource()
{
this->m_Sigma.Fill(0.5);
this->m_GridSpacing.Fill(4.0);
this->m_GridOffset.Fill(0.0);
this->m_WhichDimensions.Fill(true);
this->m_KernelFunction = dynamic_cast<KernelFunctionType *>(GaussianKernelFunction<double>::New().GetPointer());
this->DynamicMultiThreadingOn();
this->ThreaderUpdateProgressOff();
}
template <typename TOutputImage>
void
GridImageSource<TOutputImage>::BeforeThreadedGenerateData()
{
ImageType * output = this->GetOutput(0);
this->m_PixelArrays = PixelArrayContainerType::New();
m_PixelArrays->Initialize();
for (unsigned int i = 0; i < ImageDimension; ++i)
{
this->m_GridOffset[i] = std::min(this->m_GridOffset[i], this->m_GridSpacing[i]);
PixelArrayType pixels = m_PixelArrays->CreateElementAt(i);
pixels.set_size(this->GetSize()[i]);
pixels.fill(1);
if (this->m_WhichDimensions[i])
{
ImageLinearIteratorWithIndex<ImageType> It(output, output->GetRequestedRegion());
It.SetDirection(i);
// Add two extra functions in the front and one in the back to ensure
// coverage.
unsigned int numberOfGaussians =
Math::Ceil<unsigned int>(this->GetSize()[i] * output->GetSpacing()[i] / this->m_GridSpacing[i]) + 4u;
for (It.GoToBegin(); !It.IsAtEndOfLine(); ++It)
{
typename ImageType::IndexType index = It.GetIndex();
typename ImageType::PointType point;
output->TransformIndexToPhysicalPoint(index, point);
RealType val = 0;
for (unsigned int j = 0; j < numberOfGaussians; ++j)
{
RealType num = point[i] - static_cast<RealType>(j - 2) * this->m_GridSpacing[i] - output->GetOrigin()[i] -
this->m_GridOffset[i];
val += this->m_KernelFunction->Evaluate(num / this->m_Sigma[i]);
}
pixels[index[i]] = val;
}
pixels = 1.0 - pixels / pixels.max_value();
}
this->m_PixelArrays->SetElement(i, pixels);
}
}
template <typename TOutputImage>
void
GridImageSource<TOutputImage>::DynamicThreadedGenerateData(const ImageRegionType & outputRegionForThread)
{
ImageType * output = this->GetOutput(0);
TotalProgressReporter progress(this, output->GetRequestedRegion().GetNumberOfPixels());
for (ImageRegionIteratorWithIndex<ImageType> It(output, outputRegionForThread); !It.IsAtEnd(); ++It)
{
RealType val = 1.0;
typename ImageType::IndexType index = It.GetIndex();
for (unsigned int i = 0; i < ImageDimension; ++i)
{
val *= this->m_PixelArrays->GetElement(i)[index[i]];
}
It.Set(static_cast<PixelType>(m_Scale * val));
progress.CompletedPixel();
}
}
template <typename TOutputImage>
void
GridImageSource<TOutputImage>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Output image information: " << std::endl;
os << indent << " Scale : " << this->GetScale() << std::endl;
os << indent << "Grid information: " << std::endl;
os << indent << " WhichDimensions : " << this->GetWhichDimensions() << std::endl;
os << indent << " Kernel : " << this->GetKernelFunction() << std::endl;
os << indent << " Sigma : " << this->GetSigma() << std::endl;
os << indent << " Grid spacing : " << this->GetGridSpacing() << std::endl;
os << indent << " Grid offset : " << this->GetGridOffset() << std::endl;
os << indent << "Pixel arrays: " << m_PixelArrays << std::endl;
}
} // end namespace itk
#endif
|